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
28 public class AnswerAction extends Action
29 {
30 private static final Logger LOG = Logger.getLogger( AnswerAction.class );
32
33
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 if( session == null || ( ( Boolean )session.getAttribute( "token" ) ).booleanValue() == false )
54 {
55 return( actionMapping.findForward( "index" ) );
56 }
57
58 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 if( imageVO != null )
95 {
96 if( !imageVO.getImageType().equals( "application/pdf" ) )
97 {
98 answerVO.setAttachement( null );
100 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
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
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 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
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
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 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