1 package ch.ige.edossier.util;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.util.Iterator;
6 import java.util.List;
7 import javax.servlet.ServletException;
8 import javax.servlet.http.*;
9 import org.apache.log4j.Logger;
10 import ch.ige.edossier.web.server.EDossierControl;
11 import ch.ige.edossier.web.vo.AccountVO;
12 import ch.ige.edossier.web.vo.AnswerVO;
13 import ch.ige.edossier.web.vo.InterceptionVO;
14
15
25 public class PdfServlet extends HttpServlet
26 {
27 private static final Logger LOG = Logger.getLogger( PdfServlet.class );
29 private ByteArrayOutputStream osPdf = null;
30 private byte[] pdf = null;
31 private int dossierId = 0;
32
33
36 public void init()
37 {
38 this.osPdf = new ByteArrayOutputStream();
39 }
40
41
47 public void doGet( HttpServletRequest request, HttpServletResponse response )
48 {
49 HttpSession session = request.getSession();
50 String idInterception = request.getParameter( "idInterception" );
51
52 if( idInterception == null )
53 {
54 getAttachement( session, request.getParameter( "idAnswer" ) );
55 createPdfResponse( response );
56 }
57 else
58 {
59 getInterception( session, idInterception );
60
61 AccountVO accountVO = ( AccountVO )request.getAttribute( "accountVO" );
62 createPdfResponse( response );
63
64 if( setState( Integer.parseInt( idInterception ), 2 ) )
66 {
67 if( updateDossiers( session, accountVO.getAccountId() ) )
69 {
70 updateInterceptions( session, dossierId );
72 }
73 }
74 }
75 }
76
77
81 private void createPdfResponse( HttpServletResponse response )
82 {
83 response.setHeader( "Cache-Control", "max-age=30" );
84 response.setContentType( "application/pdf" );
85
86 try
87 {
88 osPdf.write( pdf );
89 response.setContentLength( osPdf.size() );
90 osPdf.writeTo( response.getOutputStream() );
91 osPdf.flush();
92 }
93 catch( IOException ioex )
94 {
95 response.setStatus( 500 );
96 LOG.error( "IOException bei der Rückgabe des Response: " + ioex.getMessage() );
97 }
98 }
99
100
106 private void getInterception( HttpSession session, String idInterception ) throws NumberFormatException
107 {
108 Iterator iter = ( ( List )session.getAttribute( "alInterceptionVO" ) ).iterator();
109
110 while( iter.hasNext() )
111 {
112 InterceptionVO interceptionVO = ( InterceptionVO )iter.next();
113
114 if( interceptionVO.getInterceptionId() == Integer.parseInt( idInterception ) )
115 {
116 pdf = interceptionVO.getPdf();
117 dossierId = interceptionVO.getDossierId();
118 }
119 }
120 }
121
122
128 private void getAttachement( HttpSession session, String idAnswer ) throws NumberFormatException
129 {
130 AnswerVO answerVO = ( ( AnswerVO )session.getAttribute( "answerVO" ) );
131
132 if( answerVO.getAnswerId() == Integer.parseInt( idAnswer ) )
133 {
134 pdf = answerVO.getAttachement();
135 }
136 }
137
138
145 public void doPost( HttpServletRequest request, HttpServletResponse response ) throws IOException, ServletException
146 {
147 doGet( request, response );
148 }
149
150
156 private boolean setState( int interceptionId, int status )
157 {
158 try
159 {
160 LOG.info( "Der Status wurde auf (" + status + ") aktualisiert!" );
161 return new EDossierControl().setState( interceptionId, status );
162 }
163 catch( Exception sqlex )
164 {
165 LOG.warn( "Der Status der Interception (" + interceptionId + ") mit dem Status (" + status + ") konnte nicht aktualisiert werden: " + sqlex.getMessage() );
166 return false;
167 }
168 }
169
170
176 private boolean updateDossiers( HttpSession session, int accountId )
177 {
178 try
179 {
180 session.setAttribute( "dossierOverviewVO", new EDossierControl().getDossiers( accountId ) );
181 LOG.info( "Die Dossiers wurden mit der Account-Id (" + accountId + ") aktualisiert!" );
182 return true;
183 }
184 catch( Exception ex )
185 {
186 LOG.warn( "Die aktualisierten Dossiers mit der Account-Id (" + accountId + ") konnten nicht geladen werden: " + ex.getMessage() );
187 }
188 return false;
189 }
190
191
196 private void updateInterceptions( HttpSession session, int dossierId )
197 {
198 try
199 {
200 session.setAttribute( "alInterceptionVO", new EDossierControl().getInterceptions( dossierId ) );
201 LOG.info( "Die Interceptions wurden mit der Dossier-Id (" + dossierId + ") aktualisiert!" );
202 }
203 catch( Exception ex )
204 {
205 LOG.warn( "Die aktualisierten Interceptions mit der Dossier-Id (" + dossierId + ") konnten nicht geladen werden: " + ex.getMessage() );
206 }
207 }
208
209
212 public void destroy()
213 {
214 try
215 {
216 osPdf.close();
217 }
218 catch( IOException ex )
219 {
220 LOG.warn( "Pdf-Stream kann nicht geschlossen werden." );
221
222 }
223 }
224 }
225