forked from Coded/SIPRP
no message
git-svn-id: https://svn.coded.pt/svn/SIPRP@456 bb69d46d-e84e-40c8-a05a-06db0d633741lxbfYeaa
parent
6d7a5e66aa
commit
3879fe9b3e
@ -0,0 +1,159 @@
|
||||
/*
|
||||
* ServletAux.java
|
||||
*
|
||||
* Created on 10 de Março de 2005, 12:24
|
||||
*/
|
||||
|
||||
package siprp.pagina;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.*;
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.*;
|
||||
|
||||
import org.apache.velocity.*;
|
||||
import org.apache.velocity.app.*;
|
||||
/**
|
||||
*
|
||||
* @author psantos
|
||||
*/
|
||||
public class MailerServlet extends HttpServlet
|
||||
{
|
||||
public void init()
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
String TEMPLATE_DIR = this.getServletContext().getRealPath( "/" ) + "html/";
|
||||
|
||||
Properties props = new Properties();
|
||||
props.setProperty( "file.resource.loader.path", TEMPLATE_DIR );
|
||||
Velocity.init( props );
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
protected String createContent( Hashtable parameters, String[] form_fields, String template )
|
||||
{
|
||||
VelocityContext context = new VelocityContext();
|
||||
StringWriter output = new StringWriter();
|
||||
|
||||
try
|
||||
{
|
||||
for( int i = 0; i < form_fields.length; i++ )
|
||||
{
|
||||
context.put( form_fields[ i ], parameters.get( form_fields[ i ] ) );
|
||||
}
|
||||
|
||||
Velocity.mergeTemplate( template, Velocity.ENCODING_DEFAULT, context, output );
|
||||
|
||||
return output.toString();
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected String showResultPage( boolean success, String success_template, String fail_template )
|
||||
{
|
||||
VelocityContext context = new VelocityContext();
|
||||
StringWriter output = new StringWriter();
|
||||
String template;
|
||||
if( success )
|
||||
{
|
||||
template = success_template;
|
||||
}
|
||||
else
|
||||
{
|
||||
template = fail_template;
|
||||
}
|
||||
try
|
||||
{
|
||||
Velocity.mergeTemplate( template, Velocity.ENCODING_DEFAULT, context, output );
|
||||
return output.toString();
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected boolean sendMail( String from, String destination, String subject, String content )
|
||||
{
|
||||
String smtp_server = "localhost";
|
||||
String mailer = "Evolute Mailer";
|
||||
|
||||
Properties props = System.getProperties();
|
||||
props.put( "mail.smtp.host", smtp_server );
|
||||
|
||||
Session session = Session.getInstance( props, null );
|
||||
Message msg = new MimeMessage( session );
|
||||
try
|
||||
{
|
||||
if( from != null )
|
||||
{
|
||||
msg.setFrom( new InternetAddress( from ) );
|
||||
}
|
||||
msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( destination, true ) );
|
||||
msg.setSubject( subject );
|
||||
|
||||
msg.setContent( content.toString(), "text/plain" );
|
||||
|
||||
msg.setHeader( "X-Mailer", mailer );
|
||||
msg.setSentDate( new Date() );
|
||||
Transport.send( msg );
|
||||
|
||||
return true;
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private String checkParameter( String parameter )
|
||||
{
|
||||
if( parameter != null && parameter.trim().length() > 0 )
|
||||
{
|
||||
return parameter.trim();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected Hashtable parseParameters( Hashtable parameters )
|
||||
{
|
||||
Hashtable tmp = new Hashtable();
|
||||
String[] element;
|
||||
String key;
|
||||
String new_element;
|
||||
for( Enumeration e = parameters.keys(); e.hasMoreElements(); )
|
||||
{
|
||||
key = ( String ) e.nextElement();
|
||||
element = ( String[] ) parameters.get( key );
|
||||
if( element != null )
|
||||
{
|
||||
new_element = checkParameter( element[ 0 ] );
|
||||
if( new_element != null )
|
||||
{
|
||||
tmp.put( key, new_element );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* ScheduleServlet.java
|
||||
*
|
||||
* Created on 10 de Março de 2005, 12:22
|
||||
*/
|
||||
|
||||
package siprp.pagina;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.*;
|
||||
|
||||
import org.apache.velocity.*;
|
||||
import org.apache.velocity.app.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author psantos
|
||||
*/
|
||||
public class ScheduleServlet extends MailerServlet
|
||||
{
|
||||
private final static String EMPRESA = "empresa";
|
||||
private final static String ESTABELECIMENTO = "estabelecimento";
|
||||
private final static String FUNCIONARIO = "funcionario";
|
||||
private final static String MARCACAO_TIPO = "marcacao_tipo";
|
||||
private final static String DATA = "data";
|
||||
private final static String HORA = "hora";
|
||||
|
||||
private final static String[] FORM_FIELDS = new String[]{
|
||||
EMPRESA, ESTABELECIMENTO, FUNCIONARIO,
|
||||
MARCACAO_TIPO, DATA, HORA
|
||||
};
|
||||
|
||||
public void doGet( HttpServletRequest req, HttpServletResponse res )
|
||||
throws IOException
|
||||
{
|
||||
ServletOutputStream out = res.getOutputStream();
|
||||
res.setContentType( "text/html" );
|
||||
|
||||
VelocityContext context = new VelocityContext();
|
||||
StringWriter output = new StringWriter();
|
||||
|
||||
try
|
||||
{
|
||||
Velocity.mergeTemplate( "marcacao/marcacao_menu.html", Velocity.ENCODING_DEFAULT, context, output );
|
||||
|
||||
out.println( output.toString() );
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
public void doPost( HttpServletRequest req, HttpServletResponse res )
|
||||
throws IOException
|
||||
{
|
||||
ServletOutputStream out = res.getOutputStream();
|
||||
res.setContentType( "text/html" );
|
||||
|
||||
// Info de marcacao
|
||||
// Dados que vem no pedido: marcacao_tipo, data, hora
|
||||
Hashtable parameters = parseParameters( new Hashtable( req.getParameterMap() ) );
|
||||
|
||||
// Ir buscar o resto dos parametros à sessão
|
||||
HttpSession session = req.getSession( false );
|
||||
if( session == null )
|
||||
{
|
||||
// timeout
|
||||
return;
|
||||
}
|
||||
|
||||
parameters.put( EMPRESA, session.getAttribute( siprpServlet.sessionCompanyName ) );
|
||||
parameters.put( ESTABELECIMENTO, session.getAttribute( "session_estabelecimento_nome" ) );
|
||||
parameters.put( FUNCIONARIO, session.getAttribute( "session_funcionario_nome" ) );
|
||||
//parameters.put( session.getAttribute( "session_funcionario_numero" ) );
|
||||
|
||||
// String from = MAIL DA EMPRESA
|
||||
|
||||
String marcacao_tipo = ( String )parameters.get( MARCACAO_TIPO );
|
||||
|
||||
String destination = "psantos@evolute.pt";
|
||||
String subject = "Pedido de marcação de " + marcacao_tipo + " via web";
|
||||
String content = createContent( parameters, FORM_FIELDS, "marcacao/marcacao.txt" );
|
||||
// manda mail
|
||||
boolean success = sendMail( null, destination, subject, content );
|
||||
|
||||
// mostra pagina correspondente
|
||||
out.println( showResultPage( success, "marcacaco/marcacao_enviada.html", "marcacaco/marcacao_nao_enviada.html" ) );
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
Empresa: $empresa
|
||||
Estabelecimento: $estabelecimento
|
||||
Funcionário: $funcionario
|
||||
|
||||
$marcacao_tipo no dia $data#if( $marcacao_tipo == 'Consulta' ) às $hora#end.
|
||||
|
||||
Email de resposta
|
||||
@ -0,0 +1,54 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>Marcação de consulta / exame</title>
|
||||
</head>
|
||||
<script language="javascript" src="/siprpWeb/html/css/funcs.js"></script>
|
||||
<script>
|
||||
function validateForm()
|
||||
{
|
||||
alert( document.f.ola.value );
|
||||
if( document.f.marcacao_tipo.value == null )
|
||||
{
|
||||
alert( "Tem que escolher o tipo de marcação." );
|
||||
return false;
|
||||
}
|
||||
return isValid( document.f.data, 0 ) && isValid( document.f.hora, 1 ) ;
|
||||
}
|
||||
</script>
|
||||
<body>
|
||||
<form method='post' action='/siprpWeb/schedule' name='f'>
|
||||
<table>
|
||||
<tr>
|
||||
<td>
|
||||
<input type='radio' id='ola' name='ola' value='ola'>Ola
|
||||
<input type='radio' id='ola' name='ola' value='adeus'>Ola
|
||||
<input type='radio' id='marcacao_tipo' name='marcacao_tipo' value='Consulta' onclick='document.getElementById( "hora" ).disabled = false;'>Consulta
|
||||
<input type='radio' id='marcacao_tipo' name='marcacao_tipo' value='Exame' onclick='document.getElementById( "hora" ).disabled = true;'>Exame
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
Data:<input type='text' id='data' name='data'>dd/mm/aaaa
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
Hora:<input type='text' id='hora' name='hora'>hh:mm
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
Nota: a marcação está sujeita a confirmação via email.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<input type='submit' value='Marcar' onclick='return validateForm()'>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Reference in new issue