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.constant.Document.AUTRES_DOCUMENTS;
37  import static fr.paris.lutece.plugins.ctv.constant.Document.CROQUIS;
38  import static fr.paris.lutece.plugins.ctv.constant.Document.ETAT_DES_LIEUX;
39  import static fr.paris.lutece.plugins.ctv.constant.Document.JUSTIFICATIFS_URBANISME;
40  import static fr.paris.lutece.plugins.ctv.constant.Document.JUSTIFICATIF_IMPOSSIBILITE_USAGE_LOCAUX;
41  import static fr.paris.lutece.plugins.ctv.constant.Document.NOTE_CALCUL;
42  import static fr.paris.lutece.plugins.ctv.constant.Document.PLAN_COMPLEMENTAIRE;
43  import static fr.paris.lutece.plugins.ctv.constant.Document.PLAN_DETAILS;
44  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_ABANDON;
45  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_COMPLETE_LATER;
46  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_DOWNLOAD;
47  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_NEXT;
48  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_SAVE;
49  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_DOCUMENTS_JOINTS;
50  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_VALIDATION;
51  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.MAX_FILE_SIZE;
52  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.NOM_FICHIER;
53  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PAGE_PORTAL;
54  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PARAMETER_NEXT_STEP;
55  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TEMPLATE_DOCUMENTS_JOINTS;
56  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TYPE_FICHIER;
57  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.XPAGE_ACCUEIL;
58  
59  import java.util.ArrayList;
60  import java.util.HashMap;
61  import java.util.List;
62  import java.util.Map;
63  
64  import javax.servlet.http.HttpServletRequest;
65  import javax.servlet.http.HttpSession;
66  
67  import org.apache.commons.fileupload.FileItem;
68  import org.apache.commons.lang3.StringUtils;
69  
70  import fr.paris.lutece.plugins.ctv.bo.Emprise;
71  import fr.paris.lutece.plugins.ctv.business.IDemandeService;
72  import fr.paris.lutece.plugins.ctv.business.IEmpriseService;
73  import fr.paris.lutece.plugins.ctv.constant.Document;
74  import fr.paris.lutece.plugins.ctv.constants.CtvConstants;
75  import fr.paris.lutece.plugins.ctv.handler.CtvUploadDemandeDocumentHandler;
76  import fr.paris.lutece.plugins.ctv.handler.CtvUploadEmpriseDocumentHandler;
77  import fr.paris.lutece.plugins.ctv.service.FormulaireDemandeOccupationServiceImpl;
78  import fr.paris.lutece.plugins.ctv.service.IFormulaire;
79  import fr.paris.lutece.plugins.ctv.util.Utils;
80  import fr.paris.lutece.plugins.ctv.view.object.DemandeReading;
81  import fr.paris.lutece.portal.service.security.LuteceUser;
82  import fr.paris.lutece.portal.service.security.UserNotSignedException;
83  import fr.paris.lutece.portal.service.spring.SpringContextService;
84  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
85  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
86  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
87  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
88  import fr.paris.lutece.portal.web.xpages.XPage;
89  
90  @Controller( xpageName = ETAPE_DOCUMENTS_JOINTS )
91  public class DocumentApp extends MVCApplication
92  {
93  
94      private IFormulaire formulaireService = FormulaireDemandeOccupationServiceImpl.getInstance( );
95      private CtvUploadDemandeDocumentHandler ctvDemandeUploadHandler = SpringContextService
96              .getBean( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER );
97      private CtvUploadEmpriseDocumentHandler ctvUploadEmpriseDocumentHandler = SpringContextService
98              .getBean( CtvUploadEmpriseDocumentHandler.CTV_EMPRISE_UPLOAD_HANDLER );
99      private IDemandeService demandeService = SpringContextService.getBean( "demandeService" );
100     private IEmpriseService empriseService = SpringContextService.getBean( "empriseService" );
101 
102     @View( value = ETAPE_DOCUMENTS_JOINTS, defaultView = true )
103     public XPage getOccupationPrincipale( HttpServletRequest request ) throws UserNotSignedException
104     {
105         LuteceUser user = Utils.getUser( request );
106         String guid = user.getName( );
107         Map<String, Object> model = initModel( );
108         formulaireService.alimentationModel( request, model );
109         formulaireService.sauvegardeEtape( request, model, ETAPE_DOCUMENTS_JOINTS );
110 
111         DemandeReading maDemande = demandeService.findByNumDemande( formulaireService.getNumDemande( request ) );
112         if ( maDemande.isDemandeIsDocumentStepSeen( ) )
113         {
114             model.put( "controlForm", true );
115         }
116 
117         model.put( "ficauto", getFileItemListAutorisations( request, maDemande.getDemandeNumero( ) ) );
118         model.put( "ficroc", getFileItemListRoc( request, maDemande.getDemandeNumero( ) ) );
119         model.put( "ficsaisines", getFileItemListSaisines( request, maDemande.getDemandeNumero( ) ) );
120 
121         return getXPage( TEMPLATE_DOCUMENTS_JOINTS, request.getLocale( ), model );
122     }
123 
124     private List<FileItem> getFileItemListAutorisations( HttpServletRequest request, int demandeNumero )
125     {
126         HttpSession session = request.getSession( );
127         session.setAttribute( CtvConstants.SESSION_NUM_DEMANDE_ID, Integer.toString( demandeNumero ) );
128         List<FileItem> fileList = new ArrayList<>( );
129         List<Emprise> emprises = empriseService.findEmprisesByDemandeId( demandeNumero );
130         for ( Emprise emprise : emprises )
131         {
132             ctvUploadEmpriseDocumentHandler.removeFilesInSession( request );
133             session.setAttribute( CtvConstants.EMPRISE_ID, emprise.getEmpriseId( ) );
134             fileList.addAll( ctvUploadEmpriseDocumentHandler.getListUploadedFiles( "autorisations", session ) );
135         }
136         return fileList;
137     }
138 
139     private List<FileItem> getFileItemListRoc( HttpServletRequest request, int demandeNumero )
140     {
141         HttpSession session = request.getSession( );
142         session.setAttribute( CtvConstants.SESSION_NUM_DEMANDE_ID, Integer.toString( demandeNumero ) );
143         ctvDemandeUploadHandler.removeFilesInSession( request );
144         List<FileItem> fileList = ctvDemandeUploadHandler.getListUploadedFiles( Document.ROC_PV, session );
145         fileList.addAll( ctvDemandeUploadHandler.getListUploadedFiles( Document.ROC_AUTRES, session ) );
146 
147         return fileList;
148     }
149 
150     private List<FileItem> getFileItemListSaisines( HttpServletRequest request, int demandeNumero )
151     {
152         HttpSession session = request.getSession( );
153         session.setAttribute( CtvConstants.SESSION_NUM_DEMANDE_ID, Integer.toString( demandeNumero ) );
154         ctvDemandeUploadHandler.removeFilesInSession( request );
155         List<FileItem> fileList = ctvDemandeUploadHandler.getListUploadedFiles( Document.SAISINE_AUTRE_SERVICE, session );
156         fileList.addAll( ctvDemandeUploadHandler.getListUploadedFiles( Document.SAISINE_COMMISSARIAT, session ) );
157         fileList.addAll( ctvDemandeUploadHandler.getListUploadedFiles( Document.SAISINE_PREFECTURE_POLICE, session ) );
158 
159         return fileList;
160     }
161 
162     private List<FileItem> getFileItemList( HttpServletRequest request, int demandeNumero )
163     {
164         HttpSession session = request.getSession( );
165         session.setAttribute( CtvConstants.SESSION_NUM_DEMANDE_ID, Integer.toString( demandeNumero ) );
166         ctvDemandeUploadHandler.removeFilesInSession( request );
167         List<FileItem> fileList = ctvDemandeUploadHandler.getListUploadedFiles( Document.ROC_PV, session );
168         fileList.addAll( ctvDemandeUploadHandler.getListUploadedFiles( Document.ROC_AUTRES, session ) );
169         fileList.addAll( ctvDemandeUploadHandler.getListUploadedFiles( Document.SAISINE_AUTRE_SERVICE, session ) );
170         fileList.addAll( ctvDemandeUploadHandler.getListUploadedFiles( Document.SAISINE_COMMISSARIAT, session ) );
171         fileList.addAll( ctvDemandeUploadHandler.getListUploadedFiles( Document.SAISINE_PREFECTURE_POLICE, session ) );
172 
173         List<Emprise> emprises = empriseService.findEmprisesByDemandeId( demandeNumero );
174         for ( Emprise emprise : emprises )
175         {
176             ctvUploadEmpriseDocumentHandler.removeFilesInSession( request );
177             HttpSession session2 = request.getSession( );
178             session2.setAttribute( CtvConstants.EMPRISE_ID, emprise.getEmpriseId( ) );
179             fileList.addAll( ctvUploadEmpriseDocumentHandler.getListUploadedFiles( "autorisations", session ) );
180         }
181         return fileList;
182     }
183 
184     @Action( "downLoadFront" )
185     public XPage downLoadFront( HttpServletRequest request )
186     {
187         String fileName = request.getParameter( NOM_FICHIER );
188         Integer numDemande = Integer.parseInt( request.getParameter( "numDemande" ) );
189         byte [ ] content = null;
190         List<FileItem> fileList = getFileItemList( request, numDemande );
191         for ( FileItem file : fileList )
192         {
193             String itemName = file.getName( );
194             if ( itemName.equals( fileName ) )
195             {
196                 content = file.get( );
197                 return download( content, itemName, itemName );
198             }
199         }
200         return null;
201     }
202 
203     @Action( ACTION_NEXT )
204     public XPage nextPage( HttpServletRequest request )
205     {
206         storeFiles( request );
207         return redirect( request, PAGE_PORTAL + ETAPE_VALIDATION );
208     }
209 
210     @Action( ACTION_COMPLETE_LATER )
211     public XPage completeLater( HttpServletRequest request )
212     {
213         storeFiles( request );
214         return redirect( request, PAGE_PORTAL + XPAGE_ACCUEIL );
215     }
216 
217     @Action( ACTION_SAVE )
218     public XPage doSaveAction( HttpServletRequest request )
219     {
220         storeFiles( request );
221         // Récupération de la redirection
222         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
223         if ( StringUtils.isAnyEmpty( nextStepPath ) )
224         {
225             return redirect( request, PAGE_PORTAL + XPAGE_ACCUEIL );
226         }
227         return redirect( request, PAGE_PORTAL + nextStepPath );
228     }
229 
230     @Action( ACTION_ABANDON )
231     public XPage doAbandonAction( HttpServletRequest request )
232     {
233         updateDocumentSeen( request );
234         formulaireService.removeFilesFromSession( request );
235         // Récupération de la redirection
236         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
237         if ( StringUtils.isAnyEmpty( nextStepPath ) )
238         {
239             return redirect( request, PAGE_PORTAL + XPAGE_ACCUEIL );
240         }
241         return redirect( request, PAGE_PORTAL + nextStepPath );
242     }
243 
244     @Action( ACTION_DOWNLOAD )
245     public XPage download( HttpServletRequest request )
246     {
247         FileItem fileDemandeItem = ctvDemandeUploadHandler.getFileItemByName( request, request.getParameter( TYPE_FICHIER ),
248                 request.getParameter( NOM_FICHIER ) );
249         if ( fileDemandeItem != null )
250         {
251             return download( fileDemandeItem.get( ), fileDemandeItem.getName( ), fileDemandeItem.getContentType( ) );
252         }
253         else
254         {
255             ctvUploadEmpriseDocumentHandler.removeFilesInSession( request );
256             String id = request.getParameter( "id" );
257             if ( StringUtils.isNotBlank( id ) )
258             {
259                 request.getSession( ).setAttribute( CtvConstants.EMPRISE_ID, id );
260             }
261             FileItem fileEmpriseItem = ctvUploadEmpriseDocumentHandler.getFileItemByName( request, request.getParameter( TYPE_FICHIER ),
262                     request.getParameter( NOM_FICHIER ) );
263             if ( fileEmpriseItem != null )
264             {
265                 return download( fileEmpriseItem.get( ), fileEmpriseItem.getName( ), fileEmpriseItem.getContentType( ) );
266             }
267         }
268         return null;
269     }
270 
271     private HashMap<String, Object> initModel( )
272     {
273         HashMap<String, Object> model = new HashMap<>( );
274         model.put( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER, ctvDemandeUploadHandler );
275         model.put( MAX_FILE_SIZE, ctvDemandeUploadHandler.getMaxFileSize( ) );
276         return model;
277     }
278 
279     private void storeFiles( HttpServletRequest request )
280     {
281         updateDocumentSeen( request );
282         ctvDemandeUploadHandler.storeFilesOnDisk( request, JUSTIFICATIFS_URBANISME );
283         ctvDemandeUploadHandler.storeFilesOnDisk( request, ETAT_DES_LIEUX );
284         ctvDemandeUploadHandler.storeFilesOnDisk( request, PLAN_COMPLEMENTAIRE );
285         ctvDemandeUploadHandler.storeFilesOnDisk( request, CROQUIS );
286         ctvDemandeUploadHandler.storeFilesOnDisk( request, PLAN_DETAILS );
287         ctvDemandeUploadHandler.storeFilesOnDisk( request, NOTE_CALCUL );
288         ctvDemandeUploadHandler.storeFilesOnDisk( request, JUSTIFICATIF_IMPOSSIBILITE_USAGE_LOCAUX );
289         ctvDemandeUploadHandler.storeFilesOnDisk( request, AUTRES_DOCUMENTS );
290     }
291 
292     private void updateDocumentSeen( HttpServletRequest request )
293     {
294         DemandeReading maDemande = demandeService.findByNumDemande( formulaireService.getNumDemande( request ) );
295         if ( maDemande != null && !maDemande.isDemandeIsDocumentStepSeen( ) )
296         {
297             demandeService.updateIsDocumentStepSeen( maDemande.getDemandeNumero( ), true );
298         }
299     }
300 }