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.ACTION_ABANDON;
37  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_COMPLETE_LATER;
38  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_DOWNLOAD;
39  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_NEXT;
40  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_SAVE;
41  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.DELETE_EMPRISE;
42  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.DETAILS_EMPRISE;
43  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.EMPRISE_ID;
44  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.EMPRISE_ID_PARAM_REQUEST;
45  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_DOCUMENTS_JOINTS;
46  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_EMPRISE;
47  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.NOM_FICHIER;
48  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PAGE_PORTAL;
49  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PARAMETER_NEXT_STEP;
50  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.RECAP_EMPRISES;
51  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.SAVE_DETAILS_EMPRISE;
52  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.SAVE_EMPRISE;
53  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TEMPLATE_DETAILS_EMPRISE;
54  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TEMPLATE_EMPRISES;
55  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TEMPLATE_RECAP_EMPRISES;
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.List;
60  import java.util.Map;
61  import java.util.Set;
62  
63  import javax.servlet.http.HttpServletRequest;
64  
65  import org.apache.commons.fileupload.FileItem;
66  import org.apache.commons.lang3.StringUtils;
67  
68  import fr.paris.lutece.plugins.ctv.bo.DetailsEmprise;
69  import fr.paris.lutece.plugins.ctv.bo.DetailsEmprise.TypeConfigurationSpecifique;
70  import fr.paris.lutece.plugins.ctv.bo.Emprise;
71  import fr.paris.lutece.plugins.ctv.business.IEmpriseService;
72  import fr.paris.lutece.plugins.ctv.constant.Document;
73  import fr.paris.lutece.plugins.ctv.constants.CtvConstants;
74  import fr.paris.lutece.plugins.ctv.handler.CtvUploadEmpriseDocumentHandler;
75  import fr.paris.lutece.plugins.ctv.service.EmpriseControllerService;
76  import fr.paris.lutece.plugins.ctv.service.FormulaireDemandeOccupationServiceImpl;
77  import fr.paris.lutece.plugins.ctv.service.IEmpriseControllerService;
78  import fr.paris.lutece.plugins.ctv.service.IFormulaire;
79  import fr.paris.lutece.plugins.ctv.util.EmpriseMappingUtils;
80  import fr.paris.lutece.plugins.ctv.util.Utils;
81  import fr.paris.lutece.portal.service.security.UserNotSignedException;
82  import fr.paris.lutece.portal.service.spring.SpringContextService;
83  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
84  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
85  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
86  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
87  import fr.paris.lutece.portal.web.xpages.XPage;
88  import fr.paris.lutece.util.bean.BeanUtil;
89  
90  @Controller( xpageName = ETAPE_EMPRISE )
91  public class EmpriseApp extends MVCApplication
92  {
93  
94      private static final long serialVersionUID = 6677743106943663571L;
95      private IFormulaire formulaireService = FormulaireDemandeOccupationServiceImpl.getInstance( );
96      private IEmpriseControllerService empriseCtrlService = EmpriseControllerService.getInstance( );
97      private IEmpriseService empriseService = SpringContextService.getBean( "empriseService" );
98      private CtvUploadEmpriseDocumentHandler ctvUploadEmpriseDocumentHandler = SpringContextService
99              .getBean( CtvUploadEmpriseDocumentHandler.CTV_EMPRISE_UPLOAD_HANDLER );
100 
101     private static final String MARK_BASE_URL = "base_url";
102 
103     @View( value = RECAP_EMPRISES, defaultView = true )
104     public XPage getRecapEmprises( HttpServletRequest request ) throws UserNotSignedException
105     {
106         Utils.getUser( request );
107         Map<String, Object> model = getModel( );
108         formulaireService.sauvegardeEtape( request, model, ETAPE_EMPRISE );
109         List<Emprise> emprises = empriseService.findEmprisesByDemandeId( formulaireService.getNumDemande( request ) );
110         if ( isEmprisesExistantes( emprises ) )
111         {
112             model.put( MARK_BASE_URL, CtvConstants.PAGE_PORTAL_FULL + ETAPE_EMPRISE );
113             model.put( "emprises", emprises );
114             formulaireService.alimentationModel( request, model );
115             return getXPage( TEMPLATE_RECAP_EMPRISES, request.getLocale( ), model );
116         }
117         else
118         {
119             return redirectView( request, ETAPE_EMPRISE );
120         }
121     }
122 
123     @View( value = ETAPE_EMPRISE )
124     public XPage getCreationEmprise( HttpServletRequest request ) throws UserNotSignedException
125     {
126         Utils.getUser( request );
127         request.getSession( ).removeAttribute( EMPRISE_ID );
128         Map<String, Object> model = getModel( );
129         empriseCtrlService.initModel( model, request );
130         empriseCtrlService.alimenterModel( request, model );
131 
132         return getXPage( TEMPLATE_EMPRISES, request.getLocale( ), model );
133     }
134 
135     @View( value = DETAILS_EMPRISE )
136     public XPage getDetailsEmprise( HttpServletRequest request ) throws UserNotSignedException
137     {
138         Utils.getUser( request );
139         ctvUploadEmpriseDocumentHandler.removeFilesInSession( request );
140         Map<String, Object> model = getModel( );
141         empriseCtrlService.fillModelForDetailEmprise( request, model );
142         return getXPage( TEMPLATE_DETAILS_EMPRISE, request.getLocale( ), model );
143     }
144 
145     @Action( ACTION_NEXT )
146     public XPage nextPage( HttpServletRequest request )
147     {
148         return redirect( request, PAGE_PORTAL + ETAPE_DOCUMENTS_JOINTS );
149     }
150 
151     @Action( ACTION_COMPLETE_LATER )
152     public XPage completeLater( HttpServletRequest request )
153     {
154         return redirect( request, PAGE_PORTAL + XPAGE_ACCUEIL );
155     }
156 
157     @Action( ACTION_DOWNLOAD )
158     public XPage download( HttpServletRequest request )
159     {
160         String typeFichier = request.getParameter( TYPE_FICHIER );
161         String nomFichier = request.getParameter( NOM_FICHIER );
162         FileItem fileItem = ctvUploadEmpriseDocumentHandler.getFileItemByName( request, typeFichier, nomFichier );
163         if ( fileItem != null )
164         {
165             return download( fileItem.get( ), fileItem.getName( ), fileItem.getName( ) );
166         }
167         return null;
168     }
169 
170     @Action( DELETE_EMPRISE )
171     public XPage deleteEmprise( HttpServletRequest request )
172     {
173         if ( request.getParameter( EMPRISE_ID_PARAM_REQUEST ) != null )
174         {
175             int empriseId = Integer.parseInt( request.getParameter( EMPRISE_ID_PARAM_REQUEST ) );
176             int numDemande = formulaireService.getNumDemande( request );
177             empriseService.deleteEmprise( numDemande, empriseId );
178         }
179         return redirectView( request, PAGE_PORTAL + RECAP_EMPRISES );
180     }
181 
182     @Action( SAVE_EMPRISE )
183     public XPage saveEmprise( HttpServletRequest request )
184     {
185         empriseCtrlService.registerConverters( );
186         empriseCtrlService.populateEmprise( request );
187 
188         return redirectView( request, DETAILS_EMPRISE );
189     }
190 
191     @Action( "validate_carto" )
192     public XPage validateCarto( HttpServletRequest request )
193     {
194         empriseCtrlService.updateEmpriseFromRequest( request, true );
195         return redirectView( request, DETAILS_EMPRISE );
196     }
197 
198     @Action( "reload_carto" )
199     public XPage reloadCarto( HttpServletRequest request )
200     {
201         empriseCtrlService.registerConverters( );
202         Emprise emprise = new Emprise( );
203         BeanUtil.populate( emprise, request );
204         if ( !"".equals( emprise.getMapPreviewDataUrl( ) ) )
205         {
206             return redirectView( request, DETAILS_EMPRISE );
207         }
208         else
209         {
210             return redirectView( request, ETAPE_EMPRISE );
211         }
212     }
213 
214     @Action( SAVE_DETAILS_EMPRISE )
215     public XPage saveDetailsEmprise( HttpServletRequest request )
216     {
217         empriseCtrlService.updateEmpriseFromRequest( request, false );
218         storeFiles( request );
219         return redirectView( request, RECAP_EMPRISES );
220     }
221 
222     private void storeFiles( HttpServletRequest request )
223     {
224         DetailsEmprise details = new DetailsEmprise( );
225         EmpriseMappingUtils.map( request, details );
226         Set<TypeConfigurationSpecifique> configurationsSpecifiques = details.getConfigurationsSpecifiques( );
227         if ( configurationsSpecifiques != null && ( configurationsSpecifiques.contains( TypeConfigurationSpecifique.EMPRISE_MODIFIANT_SOL )
228                 || configurationsSpecifiques.contains( TypeConfigurationSpecifique.EMPRISE_MODIFIANT_SOUS_SOL ) ) )
229         {
230             ctvUploadEmpriseDocumentHandler.storeFilesOnDisk( request, Document.FORMULAIRE_MULTI_CASES );
231         }
232     }
233 
234     @Action( ACTION_SAVE )
235     public XPage doSaveAction( HttpServletRequest request )
236     {
237         empriseCtrlService.updateEmpriseFromRequest( request, false );
238         storeFiles( request );
239         // Récupération de la redirection
240         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
241         if ( StringUtils.isAnyEmpty( nextStepPath ) )
242         {
243             return redirect( request, PAGE_PORTAL + XPAGE_ACCUEIL );
244         }
245         else
246             if ( nextStepPath.contains( DETAILS_EMPRISE ) )
247             {
248                 return redirectView( request, DETAILS_EMPRISE );
249             }
250             else
251             {
252                 return redirect( request, PAGE_PORTAL + nextStepPath );
253             }
254     }
255 
256     @Action( ACTION_ABANDON )
257     public XPage doAbandonAction( HttpServletRequest request )
258     {
259         formulaireService.removeFilesFromSession( request );
260         // Récupération de la redirection
261         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
262         if ( StringUtils.isAnyEmpty( nextStepPath ) )
263         {
264             return redirect( request, PAGE_PORTAL + XPAGE_ACCUEIL );
265         }
266         else
267             if ( nextStepPath.contains( DETAILS_EMPRISE ) )
268             {
269                 return redirectView( request, DETAILS_EMPRISE );
270             }
271             else
272             {
273                 return redirect( request, PAGE_PORTAL + nextStepPath );
274             }
275     }
276 
277     private boolean isEmprisesExistantes( List<Emprise> emprises )
278     {
279         return emprises != null && !emprises.isEmpty( );
280     }
281 }