View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.ctv.controller.front;
35  
36  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.GET_EMPRISES_PDF;
37  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.XPAGE_EDITION;
38  
39  import java.io.BufferedInputStream;
40  import java.io.File;
41  import java.io.FileInputStream;
42  import java.io.FileNotFoundException;
43  import java.io.FileOutputStream;
44  import java.io.IOException;
45  import java.sql.Connection;
46  import java.util.List;
47  import java.util.zip.ZipEntry;
48  import java.util.zip.ZipOutputStream;
49  
50  import javax.servlet.http.HttpServletRequest;
51  import javax.servlet.http.HttpSession;
52  
53  import org.apache.commons.fileupload.FileItem;
54  import org.apache.commons.io.FileUtils;
55  import org.apache.commons.io.IOUtils;
56  import org.apache.commons.logging.Log;
57  import org.apache.commons.logging.LogFactory;
58  
59  import fr.paris.lutece.plugins.ctv.bo.Emprise;
60  import fr.paris.lutece.plugins.ctv.business.IEmpriseService;
61  import fr.paris.lutece.plugins.ctv.constants.CtvConstants;
62  import fr.paris.lutece.plugins.ctv.handler.CtvUploadEmpriseDocumentHandler;
63  import fr.paris.lutece.plugins.ctv.service.IEditionService;
64  import fr.paris.lutece.portal.service.database.AppConnectionService;
65  import fr.paris.lutece.portal.service.spring.SpringContextService;
66  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
67  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
68  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
69  
70  @Controller( xpageName = XPAGE_EDITION )
71  public class EditionApp extends MVCApplication
72  {
73  
74      /**
75       *
76       */
77      private static final long serialVersionUID = 2102201517620167192L;
78      private static final String ACTION_GET_PERMIS_STATIONNEMENT = "get_permis_stationnement";
79      private static final String ACTION_GET_PERMISSION_VOIRIE = "get_permission_voirie";
80      private static final String ACTION_GET_AUTORISATION_PROLONGATION_OCCUPATION = "get_autorisation_prolongation_occupation";
81      private static final String ACTION_GET_AUTORISATION_PROLONGATION_INTERVENTION = "get_autorisation_prolongation_intervention";
82      private static final String ACTION_GET_AUTORISATION_INTERVENTION = "get_autorisation_intervention";
83      private static final String ACTION_GET_ANNULATION_EMPRISE = "get_annulation_emprise";
84      private static final IEditionService editionService = SpringContextService.getBean( "editionService" );
85      private IEmpriseService empriseService = SpringContextService.getBean( "empriseService" );
86      private static final Log LOGGER = LogFactory.getLog( EditionApp.class );
87      private static final String PARAMETER_NUM_DEMANDE = "numDemande";
88      private static final String PARAMETER_EMPRISE_ID = "empriseId";
89      private static final String PARAMETER_NUM_PROLONGATION = "prolongationNum";
90      private static final CtvUploadEmpriseDocumentHandler ctvEmpriseUploadHandler = SpringContextService
91              .getBean( CtvUploadEmpriseDocumentHandler.CTV_EMPRISE_UPLOAD_HANDLER );
92  
93      @Action( ACTION_GET_PERMIS_STATIONNEMENT )
94      public void getPermisStationnement( HttpServletRequest request )
95      {
96          Connection connection = AppConnectionService.getDefaultConnectionService( ).getConnection( );
97          String contextUrl = request.getContextPath( );
98          int numDemande = Integer.parseInt( request.getParameter( PARAMETER_NUM_DEMANDE ) );
99          int empriseId = Integer.parseInt( request.getParameter( PARAMETER_EMPRISE_ID ) );
100         byte [ ] edition = editionService.getPermisStationnement( connection, contextUrl, numDemande, empriseId );
101         download( edition, "permis_stationnement.pdf", CtvConstants.MIME_PDF );
102     }
103 
104     @Action( ACTION_GET_PERMISSION_VOIRIE )
105     public void getPermissionVoirie( HttpServletRequest request )
106     {
107         Connection connection = AppConnectionService.getDefaultConnectionService( ).getConnection( );
108         String contextUrl = request.getContextPath( );
109         int numDemande = Integer.parseInt( request.getParameter( PARAMETER_NUM_DEMANDE ) );
110         int empriseId = Integer.parseInt( request.getParameter( PARAMETER_EMPRISE_ID ) );
111         byte [ ] edition = editionService.getPermissionVoirie( connection, contextUrl, numDemande, empriseId );
112         download( edition, "permission_voirie.pdf", CtvConstants.MIME_PDF );
113     }
114 
115     @Action( ACTION_GET_AUTORISATION_PROLONGATION_OCCUPATION )
116     public void getPermissionAutorisationProlongationOccupation( HttpServletRequest request )
117     {
118         Connection connection = AppConnectionService.getDefaultConnectionService( ).getConnection( );
119         String contextUrl = request.getContextPath( );
120         int numDemande = Integer.parseInt( request.getParameter( PARAMETER_NUM_DEMANDE ) );
121         int empriseId = Integer.parseInt( request.getParameter( PARAMETER_EMPRISE_ID ) );
122         int prolongationNum = Integer.parseInt( request.getParameter( PARAMETER_NUM_PROLONGATION ) );
123         byte [ ] edition = editionService.getPermissionProlongationOccupation( connection, contextUrl, numDemande, empriseId, prolongationNum );
124         download( edition, "Autorisation_Prolongation_Occupation.pdf", CtvConstants.MIME_PDF );
125     }
126 
127     @Action( ACTION_GET_AUTORISATION_PROLONGATION_INTERVENTION )
128     public void getPermissionAutorisationProlongationIntervention( HttpServletRequest request )
129     {
130         Connection connection = AppConnectionService.getDefaultConnectionService( ).getConnection( );
131         String contextUrl = request.getContextPath( );
132         int numDemande = Integer.parseInt( request.getParameter( PARAMETER_NUM_DEMANDE ) );
133         int empriseId = Integer.parseInt( request.getParameter( PARAMETER_EMPRISE_ID ) );
134         int prolongationNum = Integer.parseInt( request.getParameter( PARAMETER_NUM_PROLONGATION ) );
135         byte [ ] edition = editionService.getAutorisationProlongationIntervention( connection, contextUrl, numDemande, empriseId, prolongationNum );
136         download( edition, "Autorisation_Prolongation_Intervention.pdf", CtvConstants.MIME_PDF );
137     }
138 
139     @Action( ACTION_GET_AUTORISATION_INTERVENTION )
140     public void getAutorisationIntervention( HttpServletRequest request )
141     {
142         Connection connection = AppConnectionService.getDefaultConnectionService( ).getConnection( );
143         String contextUrl = request.getContextPath( );
144         int numDemande = Integer.parseInt( request.getParameter( PARAMETER_NUM_DEMANDE ) );
145         int empriseId = Integer.parseInt( request.getParameter( PARAMETER_EMPRISE_ID ) );
146         byte [ ] edition = editionService.getAutorisationIntervention( connection, contextUrl, numDemande, empriseId );
147         download( edition, "Autorisation_Intervention.pdf", CtvConstants.MIME_PDF );
148     }
149 
150     @Action( ACTION_GET_ANNULATION_EMPRISE )
151     public void getAnnulationEmprise( HttpServletRequest request )
152     {
153         Connection connection = AppConnectionService.getDefaultConnectionService( ).getConnection( );
154         String contextUrl = request.getContextPath( );
155         int numDemande = Integer.parseInt( request.getParameter( PARAMETER_NUM_DEMANDE ) );
156         int empriseId = Integer.parseInt( request.getParameter( PARAMETER_EMPRISE_ID ) );
157         byte [ ] edition = editionService.getAnnulationEmprise( connection, contextUrl, numDemande, empriseId );
158         download( edition, "Confirmation_Annulation_Emprise_" + empriseId + ".pdf", CtvConstants.MIME_PDF );
159     }
160 
161     @Action( GET_EMPRISES_PDF )
162     public void getEmprisesPdf( HttpServletRequest request ) throws IOException
163     {
164         Connection connection = AppConnectionService.getDefaultConnectionService( ).getConnection( );
165         String contextUrl = request.getContextPath( );
166         int numDemande = Integer.parseInt( request.getParameter( PARAMETER_NUM_DEMANDE ) );
167         List<Emprise> listEmprise = empriseService.findEmprisesByDemandeId( numDemande );
168         try
169         {
170             File zipTempFile = File.createTempFile( numDemande + "-autorisations", ".zip" );
171             zipTempFile.deleteOnExit( );
172             FileOutputStream fos = new FileOutputStream( zipTempFile );
173             ZipOutputStream zipOS = new ZipOutputStream( fos );
174             for ( Emprise emprise : listEmprise )
175             {
176                 int empriseId = emprise.getEmpriseId( );
177                 String empriseAdresse = emprise.getEmpriseAdresseDebut( );
178                 byte [ ] edition = editionService.getInfoEmprise( connection, contextUrl, numDemande, empriseId );
179                 int numEmprise = empriseService.findEmpriseById( empriseId ).getEmpriseNumero( );
180                 File infoEmpriseTempFile = File.createTempFile( numDemande + "-N_" + numEmprise + "-" + empriseAdresse, ".pdf" );
181                 infoEmpriseTempFile.deleteOnExit( );
182                 FileUtils.writeByteArrayToFile( infoEmpriseTempFile, edition );
183                 writeToZipFile( infoEmpriseTempFile, zipOS, numDemande, empriseId, empriseAdresse );
184             }
185             zipOS.close( );
186             fos.close( );
187             BufferedInputStream inputStream = new BufferedInputStream( new FileInputStream( zipTempFile ) );
188             byte [ ] data = IOUtils.toByteArray( inputStream );
189 
190             inputStream.close( );
191             download( data, numDemande + ".zip", CtvConstants.MIME_ZIP );
192 
193         }
194         catch( FileNotFoundException e )
195         {
196             LOGGER.error( e.getMessage( ) );
197         }
198         catch( IOException e )
199         {
200             LOGGER.error( "[EditionApp.getEmprisesPdf]Erreur lors de la fermeture du InputStream", e );
201         }
202 
203 
204     }
205 
206     @Action( "get_documents_autorisations" )
207     public void getDocumentsAutorisations( HttpServletRequest request )
208     {
209         int empriseId = Integer.parseInt( request.getParameter( PARAMETER_EMPRISE_ID ) );
210         String nomFichier = request.getParameter( "nomFichier" );
211         HttpSession session = request.getSession( );
212         ctvEmpriseUploadHandler.removeFilesInSession( request );
213         session.setAttribute( CtvConstants.EMPRISE_ID, empriseId );
214         FileItem file = ctvEmpriseUploadHandler.getFileItemByName( request, "autorisations", nomFichier );
215         download( file.get( ), nomFichier, CtvConstants.MIME_PDF );
216     }
217 
218     private void writeToZipFile( File file, ZipOutputStream zipStream, int numDemande, int empriseId, String empriseAdresse )
219             throws FileNotFoundException, IOException
220     {
221 
222         FileInputStream fis = new FileInputStream( file );
223         BufferedInputStream buffi = new BufferedInputStream( fis, 2048 );
224         int numEmprise = empriseService.findEmpriseById( empriseId ).getEmpriseNumero( );
225         String zipItemNameString = numDemande + "-N_" + numEmprise + "-" + empriseAdresse + ".pdf";
226         byte [ ] zipItemNameBytes = zipItemNameString.getBytes( "UTF8" );
227         String zipItemNameUtf8 = new String( zipItemNameBytes, "UTF8" );
228         ZipEntry zipEntry = new ZipEntry( zipItemNameUtf8 );
229         zipStream.putNextEntry( zipEntry );
230         byte [ ] bytes = new byte [ 2048];
231         int length;
232         while ( ( length = buffi.read( bytes ) ) != -1 )
233         {
234             zipStream.write( bytes, 0, length );
235         }
236         zipStream.closeEntry( );
237         buffi.close( );
238         fis.close( );
239     }
240 }