From 3879fe9b3e42f3284296cba78cfb8d6f6eba94ca Mon Sep 17 00:00:00 2001 From: P Santos Date: Thu, 10 Mar 2005 18:36:25 +0000 Subject: [PATCH] no message git-svn-id: https://svn.coded.pt/svn/SIPRP@456 bb69d46d-e84e-40c8-a05a-06db0d633741 --- .../classes/siprp/pagina/MailerServlet.java | 159 ++++++++++++++++++ .../classes/siprp/pagina/ScheduleServlet.java | 95 +++++++++++ trunk/html/css/funcs.js | 47 ++++++ trunk/html/marcacao/marcacao.txt | 7 + trunk/html/marcacao/marcacao_menu.html | 54 ++++++ trunk/html/user.html | 16 +- 6 files changed, 373 insertions(+), 5 deletions(-) create mode 100644 trunk/WEB-INF/classes/siprp/pagina/MailerServlet.java create mode 100644 trunk/WEB-INF/classes/siprp/pagina/ScheduleServlet.java create mode 100644 trunk/html/marcacao/marcacao.txt create mode 100644 trunk/html/marcacao/marcacao_menu.html diff --git a/trunk/WEB-INF/classes/siprp/pagina/MailerServlet.java b/trunk/WEB-INF/classes/siprp/pagina/MailerServlet.java new file mode 100644 index 00000000..6ee441ee --- /dev/null +++ b/trunk/WEB-INF/classes/siprp/pagina/MailerServlet.java @@ -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; + } +} diff --git a/trunk/WEB-INF/classes/siprp/pagina/ScheduleServlet.java b/trunk/WEB-INF/classes/siprp/pagina/ScheduleServlet.java new file mode 100644 index 00000000..b4de0e39 --- /dev/null +++ b/trunk/WEB-INF/classes/siprp/pagina/ScheduleServlet.java @@ -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" ) ); + } +} diff --git a/trunk/html/css/funcs.js b/trunk/html/css/funcs.js index 87747220..512e0fb6 100644 --- a/trunk/html/css/funcs.js +++ b/trunk/html/css/funcs.js @@ -15,3 +15,50 @@ function showhide( id ) } } } + +function isValidDate( year, month, day ) +{ + return day > 0 && ( day <= [, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][ month ] || + day == 29 && month == 2 && year % 4 == 0 && ( year % 100 > 0 || year % 400 == 0 ) ); +} + +function isValid( element, type ) +{ + switch( type ) + { + // date + case 0: + if( element.value.search( /^\d\d?\/\d\d?\/\d{1,4}$/ ) != 0 ) + { + alert( "Formato da data incorrecto." ); + window.setTimeout( "document.getElementById( '" + element.name + "').focus()", 1 ); + return false; + } + date_value = element.value.split( "/" ); + if( ! isValidDate( date_value[ 2 ], date_value[ 1 ], date_value[ 0 ] ) ) + { + alert( "Data inválida." ); + window.setTimeout( "document.getElementById( '" + element.name + "').focus()", 1 ); + return false; + } + break; + // integer + case 1: + if( element.value.search( /^\d\d:\d\d$/ ) != 0 ) + { + alert( "Formato da hora incorrecto." ); + window.setTimeout( "document.getElementById( '" + element.name + "' ).focus()", 1 ); + return false; + } + hora_value = element.value.split( "/" ); + if( date_value[ 0 ] > 23 || date_value[ 1 ] > 59 ) + { + alert( "Hora inválida." ); + window.setTimeout( "document.getElementById( '" + element.name + "').focus()", 1 ); + return false; + } + break; + } + + return true; +} diff --git a/trunk/html/marcacao/marcacao.txt b/trunk/html/marcacao/marcacao.txt new file mode 100644 index 00000000..094fae8a --- /dev/null +++ b/trunk/html/marcacao/marcacao.txt @@ -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 \ No newline at end of file diff --git a/trunk/html/marcacao/marcacao_menu.html b/trunk/html/marcacao/marcacao_menu.html new file mode 100644 index 00000000..7dc978c4 --- /dev/null +++ b/trunk/html/marcacao/marcacao_menu.html @@ -0,0 +1,54 @@ + + + + + Marcação de consulta / exame + + + + +
+ + + + + + + + + + + + + + + + +
+ Ola + Ola + Consulta + Exame +
+ Data:dd/mm/aaaa +
+ Hora:hh:mm +
+ Nota: a marcação está sujeita a confirmação via email. +
+ +
+
+ + diff --git a/trunk/html/user.html b/trunk/html/user.html index ba2d8db1..ec73f84f 100644 --- a/trunk/html/user.html +++ b/trunk/html/user.html @@ -89,6 +89,7 @@ a.text:active font-size: 10pt; font-weight: bold; text-decoration: none; + cursor: pointer; } a.text:hover @@ -123,6 +124,14 @@ td.box6 border-width: 1px 1px 1px 0px; } + + @@ -236,13 +245,10 @@ td.box6 - + +
Último ExameRealizadoPróximo ExameÚltima ConsultaRealizadaPróxima ConsultaMarcação
$!funcionario.ultimo_exame $!funcionario.realizado $!funcionario.proximo_exame $!funcionario.ultima_consulta $!funcionario.realizada $!funcionario.proxima_consulta  $!funcionario.ultimo_exame $!funcionario.realizado $!funcionario.proximo_exame $!funcionario.ultima_consulta $!funcionario.realizada $!funcionario.proxima_consulta Marcar
- ##
- ## - ## - ##
#else #foreach( $element in $v1 ) #set ( $counter = $velocityCount - 1 )