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  /**
15   * B32.03 - eDossier-Interceptions - Diplomarbeit an der Software-Schule Schweiz<br>
16   * Die Klasse ContactAction verarbeitet die Daten der ContactForm.
17   * <p>
18   * Copyright (c) 2004, Eidgenössisches Institut für Geistiges Eigentum
19   * @author    Anita Rueegsegger, Marc Bouquet
20   * @version   $Id: ContactAction.java,v 1.9 2004/10/22 01:58:23 bouquet Exp $
21   */
22  public class ContactAction extends Action
23  {
24    // Attribute für Log4j-Logging
25    private static final Logger LOG = Logger.getLogger( ContactAction.class );
26  
27    /**
28     * Die Methode execute wird aufgerufen, wenn auf der contact.jsp der Button Senden gedrückt wird.
29     * In der Methode werden die Daten verarbeitet die in der Form gehalten werden.
30     * @param actionMapping ActionMapping
31     * @param actionForm ActionForm
32     * @param httpServletRequest HttpServletRequest
33     * @param httpServletResponse HttpServletResponse
34     * @throws Exception Unerwarteter Fehler
35     * @return ActionForward
36     */
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        // Prüfen ob, eine gültige Session vorhanden ist.
48        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    /**
79     * Füllt die Daten aus der FormBean in das Contact-Value-Object ab.
80     * @param contactForm ContactForm Form-Bean
81     * @return ContactVO Value-Object
82     */
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   /**
101    * Füllt das MailSender-Value-Object für das Versenden einer Email.
102    * @param contactVO ContactVO Value-Object mit allen Contact-Informationen aus dem FormBean.
103    * @param languageCd String Sprach-Code
104    * @param messages MessageResources Referenz auf Ressourcen-Datei
105    * @return MailSenderVO Value-Object
106    */
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" ) ); // Globale definierte Resource
119     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