1 package ch.ige.edossier.web.struts;
2
3 import java.util.Locale;
4 import javax.servlet.http.HttpServletRequest;
5 import javax.servlet.http.HttpServletResponse;
6 import javax.servlet.http.HttpSession;
7 import org.apache.log4j.Logger;
8 import org.apache.struts.action.*;
9 import org.apache.struts.util.MessageResources;
10 import ch.ige.edossier.web.server.EDossierControl;
11 import ch.ige.edossier.web.vo.ContactVO;
12 import ch.ige.edossier.web.vo.MailSenderVO;
13
14
22 public class ContactAction extends Action
23 {
24 private static final Logger LOG = Logger.getLogger( ContactAction.class );
26
27
37 public ActionForward execute( ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse )
38 {
39 ActionErrors errors = new ActionErrors();
40 HttpSession session = httpServletRequest.getSession( false );
41 ContactForm contactForm = ( ContactForm )actionForm;
42 MessageResources messages = getResources( httpServletRequest );
43 EDossierFacade eFacade = new EDossierControl();
44
45 try
46 {
47 if( session == null || ( ( Boolean )session.getAttribute( "token" ) ).booleanValue() == false )
49 {
50 return( actionMapping.findForward( "index" ) );
51 }
52
53 ContactVO contactVO = fillContactVO( contactForm );
54
55 if( eFacade.sendMail( fillMailVO( contactVO, getLocale( httpServletRequest ).getLanguage(), messages ) ) )
56 {
57 session.setAttribute( "contactVO", contactVO );
58 contactForm.reset( actionMapping, httpServletRequest );
59 return( actionMapping.findForward( "forward" ) );
60 }
61 else
62 {
63 errors.add( "contact", new ActionError( "error.contact.message" ) );
64 saveErrors( httpServletRequest, errors );
65 return( actionMapping.findForward( "self" ) );
66 }
67 }
68 catch( Exception ex )
69 {
70 LOG.debug( "Message: " + ex.getMessage() );
71 errors.add( "contact", new ActionError( "error.contact.message", ex.getMessage() ) );
72 saveErrors( httpServletRequest, errors );
73 contactForm.reset( actionMapping, httpServletRequest );
74 return( new ActionForward( actionMapping.getInput() ) );
75 }
76 }
77
78
83 private ContactVO fillContactVO( ContactForm contactForm )
84 {
85 ContactVO contactVO = new ContactVO();
86 contactVO.setCbxSection( contactForm.getCbxSection() );
87 contactVO.setCbxTo( contactForm.getCbxTo() );
88 contactVO.setName( contactForm.getTxfName() );
89 contactVO.setFirm( contactForm.getTxfFirm() );
90 contactVO.setStreet( contactForm.getTxfStreet() );
91 contactVO.setNumber( contactForm.getTxfNr() );
92 contactVO.setZipCode( contactForm.getTxfZipCode() );
93 contactVO.setCity( contactForm.getTxfCity() );
94 contactVO.setPhone( contactForm.getTxfPhone() );
95 contactVO.setEmail( contactForm.getTxfEmail() );
96 contactVO.setMessage( contactForm.getTxfMessage() );
97 return contactVO;
98 }
99
100
107 private MailSenderVO fillMailVO( ContactVO contactVO, String languageCd, MessageResources messages )
108 {
109 MailSenderVO mailSenderVO = new MailSenderVO();
110 Locale local = new Locale( languageCd, "" );
111
112 mailSenderVO.setAdressTo( contactVO.getCbxSection() );
113 mailSenderVO.setAdressCc( contactVO.getEmail() );
114 mailSenderVO.setAdressFrom( contactVO.getEmail() );
115 mailSenderVO.setSubject( messages.getMessage( local, "mail.contact.subject" ) );
116
117 StringBuffer buf = new StringBuffer();
118 buf.append( messages.getMessage( local, "mail.body.auto" ) ); buf.append( messages.getMessage( local, "mail.contact.body.to" ) + " " );
120 buf.append( contactVO.getCbxSection() );
121 buf.append( messages.getMessage( local, "mail.contact.body.msg" ) );
122 buf.append( contactVO.getMessage() );
123 buf.append( messages.getMessage( local, "mail.contact.body.from" ) );
124 buf.append( contactVO.getCbxTo() );
125 buf.append( "\n" );
126 buf.append( contactVO.getName() );
127 buf.append( "\n" );
128 buf.append( contactVO.getFirm() );
129 buf.append( "\n" );
130 buf.append( contactVO.getStreet() );
131 buf.append( " " );
132 buf.append( contactVO.getNumber() );
133 buf.append( "\n" );
134 buf.append( contactVO.getZipCode() );
135 buf.append( " " );
136 buf.append( contactVO.getCity() );
137 buf.append( messages.getMessage( local, "mail.contact.body.phone" ) );
138 buf.append( contactVO.getPhone() );
139 buf.append( messages.getMessage( local, "mail.contact.body.email" ) );
140 buf.append( contactVO.getEmail() );
141 mailSenderVO.setBody( buf.toString() );
142
143 return mailSenderVO;
144 }
145 }
146