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.bo.controller.demandes.occupation;
35  
36  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.DETAILS_EMPRISE;
37  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.LISTE_EMPRISES_RECAP;
38  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.SAVE_EMPRISE;
39  
40  import java.util.List;
41  import java.util.Map;
42  
43  import javax.servlet.http.HttpServletRequest;
44  
45  import com.mysql.jdbc.StringUtils;
46  
47  import fr.paris.lutece.plugins.ctv.bo.Emprise;
48  import fr.paris.lutece.plugins.ctv.bo.controller.demandes.AbstractCtvJspBean;
49  import fr.paris.lutece.plugins.ctv.business.IEmpriseService;
50  import fr.paris.lutece.plugins.ctv.constant.Document;
51  import fr.paris.lutece.plugins.ctv.handler.CtvUploadEmpriseDocumentHandler;
52  import fr.paris.lutece.plugins.ctv.service.EmpriseControllerService;
53  import fr.paris.lutece.plugins.ctv.service.FormulaireDemandeOccupationBoServiceImpl;
54  import fr.paris.lutece.plugins.ctv.service.IEmpriseControllerService;
55  import fr.paris.lutece.plugins.ctv.service.IFormulaireDemandeOccupationBoService;
56  import fr.paris.lutece.portal.service.spring.SpringContextService;
57  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
58  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
59  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
60  
61  @Controller( controllerJsp = "emprises.jsp", controllerPath = "jsp/admin/plugins/ctv/gestionDemandes/occupation/etapes", right = "DEMANDE_OCCUPATION" )
62  public class EmpriseJspBean extends AbstractCtvJspBean
63  {
64  
65      private static final long serialVersionUID = 6184585319481739227L;
66  
67      // Service
68      private IEmpriseControllerService empriseControllerService = EmpriseControllerService.getInstance( );
69  
70      // Template
71      private static final String TEMPLATE_EMPRISES = "admin/plugins/ctv/etapes/recapEmprises.html";
72      private static final String TEMPLATE_DETAIL_EMPRISES = "admin/plugins/ctv/etapes/detailsEmprise.html";
73  
74      // Page Details Emprises
75  
76      private static final String PAGE_DETAIL_EMPRISE = "emprises.jsp?view=details_emprise&emprise_id=";
77      // Title
78      private static final String EMPRISES_TITLE = "ctv.etape.occupation_principale.title";
79  
80      private static final String VIEW_RECAP_EMPRISES = "recap_emprises";
81      private static final String VIEW_VALIDATE_CARTO = "validate_carto";
82      private static final String RELOAD_CARTO = "reload_carto";
83  
84      private static final String MARK_BASE_URL = "base_url";
85      private static final String MARK_PREVIOUS_EMPRISE_ID = "previous_emprise_id";
86      private static final String MARK_NEXT_EMPRISE_ID = "next_emprise_id";
87  
88      protected IFormulaireDemandeOccupationBoService formulaireService = FormulaireDemandeOccupationBoServiceImpl.getInstance( );
89  
90      protected IEmpriseService empriseService = SpringContextService.getBean( "empriseService" );
91  
92      private CtvUploadEmpriseDocumentHandler ctvUploadEmpriseDocumentHandler = SpringContextService
93              .getBean( CtvUploadEmpriseDocumentHandler.CTV_EMPRISE_UPLOAD_HANDLER );
94  
95      @View( defaultView = true, value = VIEW_RECAP_EMPRISES )
96      public String getRecapEmprisesView( HttpServletRequest request )
97      {
98          Map<String, Object> model = getModel( );
99          empriseControllerService.alimenterModel( request, model );
100         formulaireService.alimentationModelBO( request, model );
101         int numDemande = formulaireService.getNumDemande( request );
102         fillUserPossibleActions( request, model, numDemande );
103         fillModelCommons( request, model );
104         List<Emprise> emprises = empriseService.findEmprisesByDemandeId( formulaireService.getNumDemande( request ) );
105         model.put( "emprises", emprises );
106         return getPage( EMPRISES_TITLE, TEMPLATE_EMPRISES, model );
107     }
108 
109     @View( value = DETAILS_EMPRISE )
110     public String getDetailsEmprise( HttpServletRequest request )
111     {
112         Map<String, Object> model = getModel( );
113         empriseControllerService.fillModelForDetailEmprise( request, model );
114         formulaireService.alimentationModelBO( request, model );
115         int numDemande = formulaireService.getNumDemande( request );
116         fillUserPossibleActions( request, model, numDemande );
117         fillModelCommons( request, model );
118         fillModelNavigation( request, model );
119         model.put( "controlForm", true );
120         return getPage( EMPRISES_TITLE, TEMPLATE_DETAIL_EMPRISES, model );
121     }
122 
123     @Action( ACTION_SAVE )
124     public String doSaveAction( HttpServletRequest request )
125     {
126         empriseControllerService.updateEmpriseFromRequest( request, false );
127         int numDemande = formulaireService.getNumDemande( request );
128         fillLastUser( numDemande, request );
129         ctvUploadEmpriseDocumentHandler.storeFilesOnDisk( request, Document.FORMULAIRE_MULTI_CASES );
130         // Récupération de la redirection
131         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
132         if ( StringUtils.isNullOrEmpty( nextStepPath ) )
133         {
134             return redirectView( request, VIEW_RECAP_EMPRISES );
135         }
136         else
137             if ( nextStepPath.contains( DETAILS_EMPRISE ) )
138             {
139                 return redirectView( request, DETAILS_EMPRISE );
140             }
141             else
142             {
143                 return redirect( request, nextStepPath );
144             }
145     }
146 
147     @Action( ACTION_SAVE_AND_GO_PREVIOUS_EMPRISE )
148     public String saveAndGoPreviousEmprise( HttpServletRequest request )
149     {
150         empriseControllerService.updateEmpriseFromRequest( request, false );
151         int numDemande = formulaireService.getNumDemande( request );
152         fillLastUser( numDemande, request );
153         ctvUploadEmpriseDocumentHandler.storeFilesOnDisk( request, Document.FORMULAIRE_MULTI_CASES );
154         Map<String, Object> model = getModel( );
155         formulaireService.alimentationModel( request, model );
156         fillModelNavigation( request, model );
157         return redirect( request, PAGE_DETAIL_EMPRISE + model.get( MARK_PREVIOUS_EMPRISE_ID ) );
158     }
159 
160     @Action( ACTION_SAVE_AND_GO_NEXT_EMPRISE )
161     public String saveAndGoNextEmprise( HttpServletRequest request )
162     {
163         empriseControllerService.updateEmpriseFromRequest( request, false );
164         int numDemande = formulaireService.getNumDemande( request );
165         fillLastUser( numDemande, request );
166         ctvUploadEmpriseDocumentHandler.storeFilesOnDisk( request, Document.FORMULAIRE_MULTI_CASES );
167         Map<String, Object> model = getModel( );
168         formulaireService.alimentationModel( request, model );
169         fillModelNavigation( request, model );
170         return redirect( request, PAGE_DETAIL_EMPRISE + model.get( MARK_NEXT_EMPRISE_ID ) );
171     }
172 
173     @Action( SAVE_EMPRISE )
174     public String saveEmprise( HttpServletRequest request )
175     {
176         empriseControllerService.registerConverters( );
177         empriseControllerService.populateEmprise( request );
178         int numDemande = formulaireService.getNumDemande( request );
179         fillLastUser( numDemande, request );
180         return redirectView( request, DETAILS_EMPRISE );
181     }
182 
183     private void fillModelCommons( HttpServletRequest request, Map<String, Object> model )
184     {
185         int numDemande = formulaireService.getNumDemande( request );
186         fillUserPossibleActions( request, model, numDemande );
187         model.put( MARK_PATH_TABLEAU_DE_BORD, PATH_TABLEAU_DE_BORD );
188         model.put( MARK_PAGE_TITLE, TABLEAU_BORD_PAGE_TITLE );
189         model.put( MARK_BASE_URL, getControllerPath( ) + "/" + getControllerJsp( ) + "?" );
190         model.put( "controlForm", true );
191     }
192 
193     @Action( VIEW_VALIDATE_CARTO )
194     public String validateCarto( HttpServletRequest request )
195     {
196         empriseControllerService.updateEmpriseFromRequest( request, true );
197         int numDemande = formulaireService.getNumDemande( request );
198         fillLastUser( numDemande, request );
199         return redirectView( request, DETAILS_EMPRISE );
200     }
201 
202     @Action( RELOAD_CARTO )
203     public String reloadCarto( HttpServletRequest request )
204     {
205         return redirectView( request, DETAILS_EMPRISE );
206     }
207 
208     @Action( ACTION_ABANDON )
209     public String abandon( HttpServletRequest request )
210     {
211         formulaireService.removeFilesFromSession( request );
212         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
213         if ( StringUtils.isNullOrEmpty( nextStepPath ) )
214         {
215             return redirect( request, "../demandeOccupation.jsp" );
216         }
217         else
218             if ( nextStepPath.contains( DETAILS_EMPRISE ) )
219             {
220                 return redirectView( request, DETAILS_EMPRISE );
221             }
222             else
223             {
224                 return redirect( request, nextStepPath );
225             }
226     }
227 
228     @Action( ACTION_ABANDON_AND_GO_PREVIOUS_EMPRISE )
229     public String abandonAndGoPreviousEmprise( HttpServletRequest request )
230     {
231         formulaireService.removeFilesFromSession( request );
232         Map<String, Object> model = getModel( );
233         formulaireService.alimentationModel( request, model );
234         fillModelNavigation( request, model );
235         return redirect( request, PAGE_DETAIL_EMPRISE + model.get( MARK_PREVIOUS_EMPRISE_ID ) );
236     }
237 
238     @Action( ACTION_ABANDON_AND_GO_NEXT_EMPRISE )
239     public String abandonAndGoNextEmprise( HttpServletRequest request )
240     {
241         formulaireService.removeFilesFromSession( request );
242         Map<String, Object> model = getModel( );
243         formulaireService.alimentationModel( request, model );
244         fillModelNavigation( request, model );
245         return redirect( request, PAGE_DETAIL_EMPRISE + model.get( MARK_NEXT_EMPRISE_ID ) );
246     }
247 
248     private void fillModelNavigation( HttpServletRequest request, Map<String, Object> model )
249     {
250         List<Emprise> emprises = (List<Emprise>) model.get( LISTE_EMPRISES_RECAP );
251         if ( null == emprises )
252         {
253             return;
254         }
255 
256         int empriseId = empriseControllerService.getCurrentEmpriseId( request );
257         int index = -1;
258 
259         // Recherche de l'index de l'emprise
260         for ( int i = 0; i < emprises.size( ); i++ )
261         {
262             if ( emprises.get( i ).getEmpriseId( ) == empriseId )
263             {
264                 index = i;
265                 break;
266             }
267         }
268 
269         // Emprise précédente
270         int previous = index - 1;
271         if ( previous > -1 )
272         {
273             model.put( MARK_PREVIOUS_EMPRISE_ID, emprises.get( previous ).getEmpriseId( ) );
274         }
275 
276         // Emprise suivante
277         int next = index + 1;
278         if ( next < emprises.size( ) )
279         {
280             model.put( MARK_NEXT_EMPRISE_ID, emprises.get( next ).getEmpriseId( ) );
281         }
282     }
283 
284 }