1   package ch.ige.edossier.web.struts;
2   
3   import java.io.*;
4   import java.text.SimpleDateFormat;
5   import java.util.List;
6   import javax.servlet.http.HttpServletRequest;
7   import javax.servlet.http.HttpServletResponse;
8   import javax.servlet.http.HttpSession;
9   import org.apache.log4j.Logger;
10  import org.apache.struts.action.*;
11  import org.apache.struts.upload.FormFile;
12  import ch.ige.edossier.web.server.EDossierControl;
13  import ch.ige.edossier.web.vo.AnswerVO;
14  import ch.ige.edossier.web.vo.ImageVO;
15  import ch.ige.edossier.web.vo.InterceptionVO;
16  
17  /**
18   * B32.03 - eDossier-Interceptions - Diplomarbeit an der Software-Schule Schweiz<br>
19   * KANN-ZIEL [K4] - Mailversand an Markenprüfer (Pflichtenheft b32.03_PF_eDossier.pdf, Seite 17)<br>
20   * KANN-ZIEL [K6] - Attachement (Pflichtenheft b32.03_PF_eDossier.pdf, Seite 17)<br>
21   * NICE-TO_HAVE-ZIEL [N2] - Korrekturvorschläge (Pflichtenheft b32.03_PF_eDossier.pdf, Seite 17)<br>
22   * Die Klasse AnswerAction speichert die Antwort zu einer Beanstandung
23   * <p>
24   * Copyright (c) 2004, Eidgenössisches Institut für Geistiges Eigentum
25   * @author    Anita Rueegsegger, Marc Bouquet
26   * @version   $Id: AnswerAction.java,v 1.13 2004/11/02 02:01:47 bouquet Exp $
27   */
28  public class AnswerAction extends Action
29  {
30    // Attribute für Log4j-Logging
31    private static final Logger LOG = Logger.getLogger( AnswerAction.class );
32  
33    /**
34     * Die Methode execute wird aufgerufen, wenn auf der interception_detail.jsp der Button Antworten angewählt wird
35     * In der Methode werden die Daten für die Antwort aufbereitet
36     * @param actionMapping ActionMapping
37     * @param actionForm ActionForm
38     * @param httpServletRequest HttpServletRequest
39     * @param httpServletResponse HttpServletResponse
40     * @throws Exception
41     * @return ActionForward
42     */
43    public ActionForward execute( ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse )
44    {
45      ActionErrors errors = new ActionErrors();
46      HttpSession session = httpServletRequest.getSession();
47      AnswerForm answerForm = ( AnswerForm )actionForm;
48      EDossierFacade eFacade = new EDossierControl();
49  
50      try
51      {
52        // Prüfen ob, eine gültige Session vorhanden ist.
53        if( session == null || ( ( Boolean )session.getAttribute( "token" ) ).booleanValue() == false )
54        {
55          return( actionMapping.findForward( "index" ) );
56        }
57  
58        // bereinigen der Session
59        if( ( ImageVO )session.getAttribute( "imageVO" ) != null )
60        {
61          session.removeAttribute( "imageVO" );
62        }
63  
64        InterceptionVO interceptionVO = ( InterceptionVO )session.getAttribute( "interceptionVO" );
65        AnswerVO answerVO = fillAnswerVO( answerForm, interceptionVO );
66  
67        LOG.info( "AnswerVO: " + answerVO.toString() );
68  
69        if( answerForm.getUploadData() != null && answerForm.getUploadData().getFileSize() > 0 )
70        {
71          byte[] attachement = loadAttachement( answerForm, errors );
72  
73          if( attachement == null )
74          {
75            saveErrors( httpServletRequest, errors );
76            return( new ActionForward( actionMapping.getInput() ) );
77          }
78          else
79          {
80            answerVO.setAttachement( attachement );
81          }
82        }
83  
84        if( eFacade.saveAnswer( answerVO, getLocale( httpServletRequest ).getLanguage() ) )
85        {
86          answerVO = eFacade.getAnswer( interceptionVO.getInterceptionId() );
87          this.reloadSessionObjects( eFacade, session );
88  
89          if( answerVO != null )
90          {
91            ImageVO imageVO = eFacade.getAttachement( answerVO );
92  
93            // prüft ob das Attachement ein Bild ist
94            if( imageVO != null )
95            {
96              if( !imageVO.getImageType().equals( "application/pdf" ) )
97              {
98                // Attachment wird auf null gesetzt, da es ein Bild ist
99                answerVO.setAttachement( null );
100               // imageVO mit den Bild-Daten wird in der Session gespeichert
101               session.setAttribute( "imageVO", imageVO );
102             }
103           }
104           session.setAttribute( "answerVO", answerVO );
105           return( actionMapping.findForward( "forward" ) );
106         }
107         else
108         {
109           errors.add( "answerVO", new ActionError( "error.noanswer" ) );
110           saveErrors( httpServletRequest, errors );
111           return( new ActionForward( actionMapping.getInput() ) );
112         }
113       }
114       else
115       {
116         errors.add( "answerVO", new ActionError( "error.answer.send" ) );
117         saveErrors( httpServletRequest, errors );
118         return( actionMapping.findForward( "self" ) );
119       }
120     }
121     catch( Exception ex )
122     {
123       LOG.debug( "Message: " + ex.getMessage() );
124       System.out.println( "Message: " + ex.getMessage() );
125       errors.add( "answerVO", new ActionError( "error.answer.save" ) );
126       saveErrors( httpServletRequest, errors );
127       return( new ActionForward( actionMapping.getInput() ) );
128     }
129   }
130 
131   /**
132    * Holt die Daten aus der ActionForm und füllt sie in das Value-Object ab
133    * @param answerForm AnswerForm
134    * @param interceptionVO InterceptionVO Value-Object der aktuellen Beanstandung
135    * @return AnswerVO
136    */
137   private AnswerVO fillAnswerVO( AnswerForm answerForm, InterceptionVO interceptionVO )
138   {
139     AnswerVO answerVO = new AnswerVO();
140     answerVO.setInterceptionId( interceptionVO.getInterceptionId() );
141     answerVO.setName( answerForm.getName() );
142     answerVO.setEmail( answerForm.getEmail() );
143     answerVO.setTelNr( answerForm.getTelNr() );
144     answerVO.setText( answerForm.getText() );
145     answerVO.setStandardtext( answerForm.getStandardtext().equals( "" ) ? null : answerForm.getStandardtext() );
146     answerVO.setMaEmail( interceptionVO.getMaEmail() );
147     answerVO.setSubject( interceptionVO.getTitel() );
148     answerVO.setFileName( answerForm.getUploadData().getFileName() );
149     answerVO.setMimeType( answerForm.getUploadData().getContentType() );
150     return answerVO;
151   }
152 
153   /**
154    * Die Methode aktualisiert das Session-Objekt aufgrund eines geänderten Status in der Beanstandung
155    * @param eFacade stellt Zugriff auf Datenbank zur Verfügung
156    * @param session HttpSession aktuelle Session
157    * @throws Exception Unerwarteter Fehler
158    */
159   private void reloadSessionObjects( EDossierFacade eFacade, HttpSession session ) throws Exception
160   {
161     InterceptionVO interceptionVO = ( InterceptionVO )session.getAttribute( "interceptionVO" );
162     if( interceptionVO != null )
163     {
164       int dossierId = interceptionVO.getDossierId();
165 
166       // aktualisieren der Interceptions (Dossiers nicht erforderlich da Statusänderung kein Einfluss)
167       List listDossiers = eFacade.getInterceptions( dossierId );
168       if( listDossiers != null && listDossiers.size() > 0 )
169       {
170         session.setAttribute( "alInterceptionVO", listDossiers );
171       }
172 
173       interceptionVO = eFacade.getInterception( interceptionVO.getInterceptionId() );
174       if( interceptionVO != null )
175       {
176         session.setAttribute( "interceptionVO", interceptionVO );
177         setAnswerDatOnSession( session, interceptionVO );
178       }
179     }
180     else
181     {
182       throw new Exception( "Beim Aktualisieren der Session ist ein Fehler aufgetreten, InterceptionVO nicht gefunden oder leer" );
183     }
184   }
185 
186   /**
187    * Speichert das Antwortdatum in der Session
188    * @param session HttpSession aktuelle Session
189    * @param interceptionVO aktuelles InterceptionVO (Beanstandungsinformationen)
190    */
191   private void setAnswerDatOnSession( HttpSession session, InterceptionVO interceptionVO )
192   {
193     SimpleDateFormat sdf = new SimpleDateFormat( "dd.MM.yyyy" );
194 
195     String answer_dat = sdf.format( interceptionVO.getAnswerDat() );
196     session.setAttribute( "answer_dat", answer_dat );
197   }
198 
199   /**
200    * Diese Methode überprüft die Upload-Datei und fügt sie in das Answer-Value-Object ein.
201    * @param answerForm AnswerForm
202    * @param errors ActionErrors
203    * @return byte[] Attachement
204    */
205   private byte[] loadAttachement( AnswerForm answerForm, ActionErrors errors )
206   {
207     try
208     {
209       FormFile file = answerForm.getUploadData();
210       String contentType = file.getContentType();
211 
212       if( contentType.equals( "application/pdf" ) || contentType.equals( "image/pjpeg" ) || contentType.equals( "image/gif" ) || contentType.equals( "image/x-png" ) )
213       {
214         // only write files out that are less than 1MB
215         if( file.getFileSize() < ( 4 * 1024000 ) )
216         {
217           ByteArrayOutputStream baos = new ByteArrayOutputStream();
218           InputStream stream = file.getInputStream();
219           byte[] buffer = new byte[ 8192 ];
220           int bytesRead = 0;
221 
222           while( ( bytesRead = stream.read( buffer, 0, 8192 ) ) != -1 )
223           {
224             baos.write( buffer, 0, bytesRead );
225           }
226           stream.close();
227           return baos.toByteArray();
228         }
229         else
230         {
231           LOG.warn( "Datei ist grösser als 4MB!" );
232           errors.add( "filesize", new ActionError( "error.answer.filesize" ) );
233           return null;
234         }
235       }
236       else
237       {
238         LOG.warn( "Ungültiger Mime-Type!" );
239         errors.add( "uploadMimeType", new ActionError( "error.answer.mimetype" ) );
240         return null;
241       }
242     }
243     catch( FileNotFoundException fnfex )
244     {
245       LOG.error( "FileNotFoundException: " + fnfex.getMessage() );
246       errors.add( "filesize", new ActionError( "error.answer.filenotfound" ) );
247       return null;
248     }
249     catch( IOException ioex )
250     {
251       LOG.error( "IOException: " + ioex.getMessage() );
252       errors.add( "filesize", new ActionError( "error.answer.ioexception" ) );
253       return null;
254     }
255   }
256 }
257