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.intention;
35  
36  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_DOWNLOAD;
37  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ANNULER_EMPRISE;
38  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ANNULER_INTENTION;
39  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ANNULER_INTENTION_INSTRUCTEUR;
40  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.A_MODIFIER_EMPRISE;
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_EMPRISE;
46  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.KEY_DATE_FIN_RESEAU;
47  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.KEY_DATE_FIN_VOIRIE;
48  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.KEY_DATE_GEL;
49  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.MODIFIER_DATE_MASSE;
50  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.READ_COMMENTAIRE_CONFERENCE;
51  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.RECAP_EMPRISES;
52  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.RELOAD_CARTO;
53  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.SAVE_DETAILS_EMPRISE;
54  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.SAVE_EMPRISE;
55  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.SEND_DEMANDE;
56  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.SESSION_NUM_DEMANDE_ID;
57  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TRANSFORMER_INTENTION;
58  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.VALIDATE_CARTO;
59  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.VALIDER_EMPRISE;
60  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.VALIDER_TRANSFERT;
61  
62  import java.util.ArrayList;
63  import java.util.Date;
64  import java.util.HashMap;
65  import java.util.List;
66  import java.util.Map;
67  
68  import javax.servlet.http.HttpServletRequest;
69  
70  import org.apache.commons.fileupload.FileItem;
71  
72  import com.mysql.jdbc.StringUtils;
73  
74  import fr.paris.lutece.plugins.ctv.bo.AlerteDemande.TypeAlerteDemande;
75  import fr.paris.lutece.plugins.ctv.bo.ChantierIntention;
76  import fr.paris.lutece.plugins.ctv.bo.Emprise;
77  import fr.paris.lutece.plugins.ctv.bo.EntiteDemande;
78  import fr.paris.lutece.plugins.ctv.bo.controller.demandes.AbstractCtvJspBean;
79  import fr.paris.lutece.plugins.ctv.business.IAlerteDemandeService;
80  import fr.paris.lutece.plugins.ctv.business.IChantierService;
81  import fr.paris.lutece.plugins.ctv.business.IEmpriseService;
82  import fr.paris.lutece.plugins.ctv.business.IEntiteDemandeService;
83  import fr.paris.lutece.plugins.ctv.constant.CtvAction;
84  import fr.paris.lutece.plugins.ctv.constant.Document;
85  import fr.paris.lutece.plugins.ctv.constant.OuiNonEnum;
86  import fr.paris.lutece.plugins.ctv.constant.StatutDemande;
87  import fr.paris.lutece.plugins.ctv.constant.StatutEmprise;
88  import fr.paris.lutece.plugins.ctv.constant.TypeDemande;
89  import fr.paris.lutece.plugins.ctv.constant.TypeProfileCtv;
90  import fr.paris.lutece.plugins.ctv.constants.CtvConstants;
91  import fr.paris.lutece.plugins.ctv.handler.CtvUploadDemandeDocumentHandler;
92  import fr.paris.lutece.plugins.ctv.handler.CtvUploadEmpriseDocumentHandler;
93  import fr.paris.lutece.plugins.ctv.service.EmpriseIntentionControllerService;
94  import fr.paris.lutece.plugins.ctv.service.FormulaireDemandeIntentionServiceImpl;
95  import fr.paris.lutece.plugins.ctv.service.IEmpriseControllerService;
96  import fr.paris.lutece.plugins.ctv.service.IFormulaire;
97  import fr.paris.lutece.plugins.ctv.view.object.DemandeReading;
98  import fr.paris.lutece.plugins.unittree.service.unit.IUnitService;
99  import fr.paris.lutece.portal.service.datastore.DatastoreService;
100 import fr.paris.lutece.portal.service.spring.SpringContextService;
101 import fr.paris.lutece.portal.service.util.AppPropertiesService;
102 import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
103 import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
104 import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
105 import fr.paris.lutece.util.bean.BeanUtil;
106 
107 @Controller( controllerPath = "jsp/admin/plugins/ctv/gestionDemandes/intention/etapes", controllerJsp = "emprises.jsp", right = "DEMANDE_INTENTION" )
108 public class EmpriseJspBean extends AbstractCtvJspBean
109 {
110 
111     private static final String uploadBaseDirectory = AppPropertiesService.getProperty( "ctv.directory.upload" );
112     private static final String MODIF_DATES_EN_MASE_KO = "modifDatesEnMaseKo";
113 
114     /** Actions */
115 
116     private static final long serialVersionUID = 2872842115920125391L;
117 
118     private IFormulaire formulaireService = FormulaireDemandeIntentionServiceImpl.getInstance( );
119     private IEmpriseControllerService empriseCtrlService = EmpriseIntentionControllerService.getInstance( );
120     private IEmpriseService empriseService = SpringContextService.getBean( "empriseService" );
121     protected IChantierService chantierService = SpringContextService.getBean( "chantierService" );
122     protected CtvUploadDemandeDocumentHandler ctvDemandeUploadHandler = SpringContextService
123             .getBean( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER );
124     private CtvUploadEmpriseDocumentHandler ctvUploadEmpriseDocumentHandler = SpringContextService
125             .getBean( CtvUploadEmpriseDocumentHandler.CTV_EMPRISE_UPLOAD_HANDLER );
126     protected IEntiteDemandeService entiteDemandeService = SpringContextService.getBean( "entiteDemandeService" );
127     private IUnitService unitService = SpringContextService.getBean( IUnitService.BEAN_UNIT_SERVICE );
128     private static IAlerteDemandeService alerteDemandeService = SpringContextService.getBean( "alerteDemandeService" );
129 
130     private static final String MARK_BASE_URL = "base_url";
131 
132     @View( value = RECAP_EMPRISES, defaultView = true )
133     public String getRecapEmprises( HttpServletRequest request )
134     {
135         Map<String, Object> model = getModel( );
136 
137         formulaireService.sauvegardeEtape( request, model, ETAPE_EMPRISE );
138 
139         List<Emprise> emprises = empriseService.findEmprisesByDemandeIdWithDetails( formulaireService.getNumDemande( request ) );
140 
141         if ( isEmprisesExistantes( emprises ) )
142         {
143             int numDemande = formulaireService.getNumDemande( request );
144             fillUserPossibleActions( request, model, numDemande );
145             model.put( MARK_BASE_URL, "jsp/admin/plugins/ctv/gestionDemandes/intention/etapes/emprises.jsp?" );
146             model.put( "emprises", emprises );
147             model.put( "sommeSurface", empriseService.getTotalSurface( emprises ) );
148             empriseCtrlService.fillModelForDetailEmprise( request, model );
149             formulaireService.alimentationModel( request, model );
150             String modifDatesEnMaseKo = request.getParameter( MODIF_DATES_EN_MASE_KO );
151             if ( modifDatesEnMaseKo != null )
152             {
153                 model.put( MODIF_DATES_EN_MASE_KO, true );
154             }
155             model.put( "sectors", unitService.findAllSectors( ) );
156             return getPage( "", "admin/plugins/ctv/demandeIntention/etapesIntention/recapEmprisesIntention.html", model );
157         }
158         else
159         {
160             return redirectView( request, ETAPE_EMPRISE );
161         }
162     }
163 
164     @View( value = ETAPE_EMPRISE )
165     public String getOccupationPrincipale( HttpServletRequest request )
166     {
167         request.getSession( ).removeAttribute( EMPRISE_ID );
168         Map<String, Object> model = getModel( );
169         int numDemande = formulaireService.getNumDemande( request );
170         boolean isConcessionnaire = ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.CONCESSIONNAIRE, getPlugin( ), request );
171         model.put( "isConcessionnaire", isConcessionnaire );
172         fillUserPossibleActions( request, model, numDemande );
173         empriseCtrlService.initModel( model, request );
174         empriseCtrlService.alimenterModel( request, model );
175 
176         return getPage( "", "admin/plugins/ctv/demandeIntention/etapesIntention/emprisesIntention.html", model );
177     }
178 
179     @View( value = DETAILS_EMPRISE )
180     public String getDetailsEmprise( HttpServletRequest request )
181     {
182         Map<String, Object> model = getModel( );
183         int numDemande = formulaireService.getNumDemande( request );
184         boolean isConcessionnaire = ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.CONCESSIONNAIRE, getPlugin( ), request );
185         fillUserPossibleActions( request, model, numDemande );
186         empriseCtrlService.fillModelForDetailEmprise( request, model );
187         model.put( "controlForm", true );
188         model.put( "isConcessionnaire", isConcessionnaire );
189         return getPage( "", "admin/plugins/ctv/demandeIntention/etapesIntention/detailsEmpriseIntention.html", model );
190     }
191 
192     @Action( ACTION_COMPLETE_LATER )
193     public String completeLater( HttpServletRequest request )
194     {
195         int demandeId = formulaireService.getNumDemande( request );
196         String profile = "";
197         if ( request.getParameter( "commentaire" ) != null )
198         {
199             updateCommentaireFromRequest( request, demandeId );
200             formulaireService.removeFilesFromSession( request );
201         }
202         else
203         {
204             updateAllDetailsEmpriseFromRequest( request );
205         }
206 
207         if ( request.getSession( ).getAttribute( "profile_default" ) != null )
208         {
209             profile = ( (TypeProfileCtv) request.getSession( ).getAttribute( "profile_default" ) ).getKey( );
210         }
211 
212         return redirect( request, getAbsolutePath( request, PATH_TABLEAU_DE_BORD + "?profile_default=" + profile ) );
213     }
214 
215     @Action( ACTION_DOWNLOAD )
216     public void download( HttpServletRequest request )
217     {
218         String typeFichier = request.getParameter( CtvConstants.TYPE_FICHIER );
219         String nomFichier = request.getParameter( CtvConstants.NOM_FICHIER );
220         FileItem fileItem = ctvUploadEmpriseDocumentHandler.getFileItemByName( request, typeFichier, nomFichier );
221         if ( fileItem != null )
222         {
223             download( fileItem.get( ), fileItem.getName( ), fileItem.getName( ) );
224         }
225     }
226 
227     @Action( DELETE_EMPRISE )
228     public String deleteEmprise( HttpServletRequest request )
229     {
230         if ( request.getParameter( EMPRISE_ID_PARAM_REQUEST ) != null )
231         {
232             int empriseId = Integer.parseInt( request.getParameter( EMPRISE_ID_PARAM_REQUEST ) );
233             int numDemande = formulaireService.getNumDemande( request );
234             empriseService.deleteEmprise( numDemande, empriseId );
235         }
236         return redirectView( request, RECAP_EMPRISES );
237     }
238 
239     @Action( ANNULER_EMPRISE )
240     public String annulerEmprise( HttpServletRequest request )
241     {
242         if ( request.getParameter( EMPRISE_ID_PARAM_REQUEST ) != null )
243         {
244             int empriseId = Integer.parseInt( request.getParameter( EMPRISE_ID_PARAM_REQUEST ) );
245             empriseService.updateStatut( empriseId, StatutEmprise.ANNULEE );
246         }
247         int numDemande = formulaireService.getNumDemande( request );
248         fillLastUser( numDemande, request );
249         return redirectView( request, RECAP_EMPRISES );
250     }
251 
252     /*
253      * Dessiner une emprise
254      */
255     @Action( SAVE_EMPRISE )
256     public String saveEmprise( HttpServletRequest request )
257     {
258         Map<String, Object> model = getModel( );
259         empriseCtrlService.registerConverters( );
260         empriseCtrlService.populateEmprise( request );
261         int numDemande = formulaireService.getNumDemande( request );
262         fillLastUser( numDemande, request );
263         fillUserPossibleActions( request, model, numDemande );
264         if ( model.get( CtvAction.AJOUT_EMPRISE.name( ) ) != null )
265         {
266             int empriseId = (int) request.getSession( ).getAttribute( EMPRISE_ID );
267             Emprise emprise = empriseService.findEmpriseById( empriseId );
268             soumettreEmprise( empriseId, emprise );
269             empriseService.updateStatut( empriseId, StatutEmprise.A_COORDONNEE );
270             insertAlertTypeSuperviseur( numDemande );
271         }
272         return redirectView( request, DETAILS_EMPRISE );
273     }
274 
275     @Action( ACTION_ABANDON )
276     public String abandon( HttpServletRequest request )
277     {
278         formulaireService.removeFilesFromSession( request );
279         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
280         if ( StringUtils.isNullOrEmpty( nextStepPath ) )
281         {
282             return redirect( request, "../demandeIntention.jsp" );
283         }
284         else
285             if ( nextStepPath.contains( DETAILS_EMPRISE ) )
286             {
287                 return redirectView( request, DETAILS_EMPRISE );
288             }
289             else
290             {
291                 return redirect( request, nextStepPath );
292             }
293     }
294 
295     private void soumettreEmprise( int empriseId, Emprise emprise )
296     {
297         if ( emprise.getEmpriseStatut( ).equals( StatutEmprise.BROUILLON ) )
298         {
299             empriseService.insertDateSoumission( empriseId, new Date( ) );
300         }
301     }
302 
303     private void insertAlertTypeSuperviseur( int numDemande )
304     {
305         DemandeReading demande = demandeService.findByNumDemande( numDemande );
306         if ( demande.getDemandeType( ).equals( TypeDemande.BARRAGE_US ) || demande.getDemandeType( ).equals( TypeDemande.TRAVAUX_IMPERATIFS_NON_PROGRAMMES ) )
307         {
308             alerteDemandeService.insertAlerte( TypeAlerteDemande.SUPERVISEUR, numDemande );
309         }
310     }
311 
312     @Action( VALIDATE_CARTO )
313     public String validateCarto( HttpServletRequest request )
314     {
315         empriseCtrlService.updateEmpriseFromRequest( request, true );
316         int numDemande = formulaireService.getNumDemande( request );
317         fillLastUser( numDemande, request );
318         return redirectView( request, DETAILS_EMPRISE );
319     }
320 
321     @Action( RELOAD_CARTO )
322     public String reloadCarto( HttpServletRequest request )
323     {
324         empriseCtrlService.registerConverters( );
325         Emprise emprise = new Emprise( );
326         BeanUtil.populate( emprise, request );
327 
328         String geoJSON = "";
329 
330         if( ( (Integer) emprise.getEmpriseId( ) != null ) && ( emprise.getEmpriseId( ) != 0 )) {
331             geoJSON = empriseService.findGeoJsonEmpriseById( emprise.getEmpriseId( ) );
332         }
333 
334         if ( !"".equals( emprise.getMapPreviewDataUrl( ) ) || !geoJSON.isEmpty( ) )
335         {
336             return redirectView( request, DETAILS_EMPRISE );
337         }
338         else
339         {
340             return redirectView( request, ETAPE_EMPRISE );
341         }
342     }
343 
344     /*
345      * valider l'emprise
346      */
347     @Action( SAVE_DETAILS_EMPRISE )
348     public String saveDetailsEmprise( HttpServletRequest request )
349     {
350         updateAllDetailsEmpriseFromRequest( request );
351         return redirectView( request, RECAP_EMPRISES );
352     }
353 
354     private boolean isEmprisesExistantes( List<Emprise> emprises )
355     {
356         return ( emprises != null ) && !emprises.isEmpty( );
357     }
358 
359     @Action( SEND_DEMANDE )
360     public String sendDemande( HttpServletRequest request )
361     {
362         validationDemande( request );
363         String profile = "";
364         if ( request.getSession( ).getAttribute( "profile_default" ) != null )
365         {
366             profile = ( (TypeProfileCtv) request.getSession( ).getAttribute( "profile_default" ) ).getKey( );
367         }
368         return redirect( request, getAbsolutePath( request, PATH_TABLEAU_DE_BORD + "?profile_default=" + profile ) );
369     }
370 
371     @Action( ACTION_SAVE )
372     public String save( HttpServletRequest request )
373     {
374         int demandeId = formulaireService.getNumDemande( request );
375         String commentaire = request.getParameter( "commentaire" );
376         ctvUploadEmpriseDocumentHandler.storeFilesOnDisk( request, Document.FORMULAIRE_MULTI_CASES );
377         if ( commentaire == null )
378         {
379             empriseCtrlService.updateEmpriseFromRequest( request, false );
380         }
381         else
382         {
383             updateCommentaireFromRequest( request, demandeId );
384             updateDemandeFromRequest( request );
385         }
386         fillLastUser( demandeId, request );
387         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
388         if ( StringUtils.isNullOrEmpty( nextStepPath ) )
389         {
390             return redirectView( request, RECAP_EMPRISES );
391         }
392         else
393             if ( nextStepPath.contains( DETAILS_EMPRISE ) )
394             {
395                 return redirectView( request, DETAILS_EMPRISE );
396             }
397             else
398             {
399                 return redirect( request, nextStepPath );
400             }
401     }
402 
403     private void updateDemandeFromRequest( HttpServletRequest request )
404     {
405         String demandeJustifiee = request.getParameter( "demandeJustifiee" );
406         if ( demandeJustifiee != null )
407         {
408             int numDemande = formulaireService.getNumDemande( request );
409             demandeService.updateDemandeJustification( numDemande, OuiNonEnum.valueOf( demandeJustifiee ) );
410         }
411     }
412 
413     @Action( ANNULER_INTENTION )
414     public String annulerIntention( HttpServletRequest request )
415     {
416 
417         int numDemande = formulaireService.getNumDemande( request );
418         String profile = "";
419         List<Emprise> emprises = empriseService.findEmprisesByDemandeId( numDemande );
420         for ( Emprise emprise : emprises )
421         {
422             empriseService.deleteEmprise( numDemande, emprise.getEmpriseId( ) );
423         }
424         if ( request.getSession( ).getAttribute( "profile_default" ) != null )
425         {
426             profile = ( (TypeProfileCtv) request.getSession( ).getAttribute( "profile_default" ) ).getKey( );
427         }
428 
429         ChantierIntention chantier = chantierService.getChantierIntentionByNumDemande( numDemande );
430         EntiteDemande demandeur = entiteDemandeService.getDemandeurByNumDemande( numDemande );
431         EntiteDemande executant = entiteDemandeService.getExecutantByNumDemande( numDemande );
432         if ( chantier != null )
433         {
434             chantierService.deleteChantierIntentionById( chantier.getChantierId( ) );
435         }
436         if ( demandeur != null )
437         {
438             entiteDemandeService.deleteEntiteById( demandeur.getDemandeurId( ) );
439         }
440         if ( executant != null )
441         {
442             entiteDemandeService.deleteEntiteById( executant.getDemandeurId( ) );
443         }
444         demandeService.deleteDemandeById( numDemande );
445         formulaireService.removeFilesFromSession( request );
446         return redirect( request, getAbsolutePath( request, PATH_TABLEAU_DE_BORD + "?profile_default=" + profile ) );
447     }
448 
449     @Action( ANNULER_INTENTION_INSTRUCTEUR )
450     public String annulerDemandeInstructeur( HttpServletRequest request )
451     {
452         int numDemande = formulaireService.getNumDemande( request );
453         demandeService.updateDemandeStatut( numDemande, StatutDemande.ANNULEE );
454         List<Emprise> emprises = empriseService.findEmprisesByDemandeId( numDemande );
455         for ( Emprise emprise : emprises )
456         {
457             empriseService.updateStatut( emprise.getEmpriseId( ), StatutEmprise.ANNULEE );
458         }
459         alerteDemandeService.insertAlerte( TypeAlerteDemande.ANNULEE, numDemande );
460         fillLastUser( numDemande, request );
461         return redirectView( request, RECAP_EMPRISES );
462     }
463 
464     @Action( TRANSFORMER_INTENTION )
465     public void transformerIntention( HttpServletRequest request )
466     {
467 
468         String [ ] empriseIdsSelected = request.getParameterValues( "coche" );
469         List<Integer> empriseIdList = new ArrayList<>( );
470         for ( String empriseId : empriseIdsSelected )
471         {
472             empriseIdList.add( Integer.parseInt( empriseId ) );
473         }
474         empriseService.transformerEmprises( empriseIdList );
475         int demandeIntentionId = formulaireService.getNumDemande( request );
476 
477         DemandeReading intention = demandeService.findByNumDemande( demandeIntentionId );
478         if ( ( intention != null ) && ( intention.getDemandeType( ) == TypeDemande.BARRAGE_US ) )
479         {
480             validationDemande( request );
481         }
482 
483         Map<String, Integer> dateSettings = initDateSettingsMap( );
484         int paramDelaiDateGel = Integer.parseInt( DatastoreService.getDataValue( "parametre.numeric.intervention.duree.periode.gel", "0" ) );
485         int idDemandeIntervention = demandeService.transformerIntention( demandeIntentionId, empriseIdList, dateSettings, paramDelaiDateGel,
486                 uploadBaseDirectory );
487         request.getSession( ).setAttribute( SESSION_NUM_DEMANDE_ID, idDemandeIntervention );
488         redirect( request, "../../intervention/etapes/demandeur.jsp" );
489 
490     }
491 
492     @Action( A_MODIFIER_EMPRISE )
493     public String aModifierEmprise( HttpServletRequest request )
494     {
495         if ( request.getParameter( EMPRISE_ID_PARAM_REQUEST ) != null )
496         {
497             int empriseId = Integer.parseInt( request.getParameter( EMPRISE_ID_PARAM_REQUEST ) );
498             empriseService.updateStatut( empriseId, StatutEmprise.A_MODIFIER );
499         }
500         int numDemande = formulaireService.getNumDemande( request );
501         fillLastUser( numDemande, request );
502         return redirectView( request, RECAP_EMPRISES );
503     }
504 
505     @Action( VALIDER_EMPRISE )
506     public String validerEmprise( HttpServletRequest request )
507     {
508         if ( request.getParameter( EMPRISE_ID_PARAM_REQUEST ) != null )
509         {
510             int empriseId = Integer.parseInt( request.getParameter( EMPRISE_ID_PARAM_REQUEST ) );
511             empriseService.updateStatut( empriseId, StatutEmprise.COORDONNEE );
512         }
513         int numDemande = formulaireService.getNumDemande( request );
514         fillLastUser( numDemande, request );
515         return redirectView( request, RECAP_EMPRISES );
516     }
517 
518     @Action( VALIDER_TRANSFERT )
519     public String validerTransfert( HttpServletRequest request )
520     {
521         Map<String, Object> model = getModel( );
522         int numDemande = formulaireService.getNumDemande( request );
523         fillUserPossibleActions( request, model, numDemande );
524 
525         String secteurParam = request.getParameter( "secteur" );
526         if ( secteurParam != null )
527         {
528             int idSecteur = Integer.parseInt( secteurParam );
529             demandeService.updateSecteur( numDemande, idSecteur );
530             demandeService.setLastInstructeur( null, numDemande );
531             demandeService.updateDemandeTransfered( true, numDemande );
532             demandeService.updateUserOrigineTransfert( getUser( ).getUserId( ), numDemande );
533         }
534 
535         return redirectView( request, RECAP_EMPRISES );
536     }
537 
538     private void validationDemande( HttpServletRequest request )
539     {
540 
541         int demandeId = formulaireService.getNumDemande( request );
542         updateCommentaireFromRequest( request, demandeId );
543         demandeService.updateDemandeStatut( demandeId, StatutDemande.EN_COURS );
544         DemandeReading demande = demandeService.findByNumDemande( demandeId );
545         if ( demande.getDemandeDateSoumission( ) == null )
546         {
547             demandeService.updateDateSoumission( demandeId );
548         }
549         empriseService.updateStatutAllEmprises( demandeId, StatutEmprise.A_COORDONNEE, false );
550         insertAlertTypeSuperviseur( demandeId );
551         ctvDemandeUploadHandler.removeFilesInSession( request );
552         String x = request.getParameter( "x" );
553         String y = request.getParameter( "y" );
554         if(( x != null ) && ( y != null ))
555         {
556             demandeService.updateDemandeSecteur( demandeId, x, y );
557         }
558 
559         // màj empriseAEnvoyerASavoi pour les demandes dont le demandeur siret est non taxable
560         String demandeurSiret = entiteDemandeService.getDemandeurByNumDemande( demandeId ).getSirenSiret( );
561         String numSiretNonTaxables = DatastoreService.getDataValue( "parametre.numeric.flux.savoi2.siret.non.taxable", "" );
562 
563         if ( numSiretNonTaxables.contains( demandeurSiret ) ) {
564             empriseService.updateEmpriseConvention( demandeId, false );
565         }
566     }
567 
568     private void updateCommentaireFromRequest( HttpServletRequest request, int demandeId )
569     {
570         String commentaire = request.getParameter( "commentaire" );
571         ChantierIntention chantier = chantierService.getChantierIntentionByNumDemande( demandeId );
572         chantier.setChantierCommentaire( commentaire );
573         chantierService.updateCommentaire( chantier );
574     }
575 
576     @Action( MODIFIER_DATE_MASSE )
577     public String mofidifierDateMasse( HttpServletRequest request )
578     {
579         String [ ] empriseIdsSelected = request.getParameterValues( "coche" );
580 
581         List<Integer> empriseIdList = new ArrayList<>( );
582         for ( String empriseId : empriseIdsSelected )
583         {
584             empriseIdList.add( Integer.parseInt( empriseId ) );
585         }
586 
587         Emprise empriseDates = new Emprise( );
588         populate( empriseDates, request );
589         Date dateDebut = empriseDates.getEmpriseDateDebut( );
590         Date dateFin = empriseDates.getEmpriseDateFin( );
591 
592         if ( ( ( dateDebut != null ) || ( dateFin != null ) ) && ( empriseIdsSelected != null ) )
593         {
594             if ( ( dateDebut != null ) && ( dateFin != null ) && dateDebut.after( dateFin ) )
595             {
596                 Map<String, String> parameters = new HashMap<String, String>( );
597                 parameters.put( MODIF_DATES_EN_MASE_KO, "true" );
598                 return redirect( request, RECAP_EMPRISES, parameters );
599             }
600 
601             empriseService.updateDateEnMasse( empriseIdList, empriseDates );
602             int numDemande = formulaireService.getNumDemande( request );
603             fillLastUser( numDemande, request );
604         }
605 
606         return redirectView( request, RECAP_EMPRISES );
607     }
608 
609     private Map<String, Integer> initDateSettingsMap( )
610     {
611         Map<String, Integer> map = new HashMap<>( );
612         map.put( KEY_DATE_GEL, Integer.parseInt( DatastoreService.getDataValue( "parametre.numeric.intervention.duree.periode.gel", "0" ) ) );
613         map.put( KEY_DATE_FIN_RESEAU,
614                 Integer.parseInt( DatastoreService.getDataValue( "parametre.numeric.intervention.fin.travaux.reseaux.remise.circulation", "0" ) ) );
615         map.put( KEY_DATE_FIN_VOIRIE,
616                 Integer.parseInt( DatastoreService.getDataValue( "parametre.numeric.intervention.fin.travaux.voirie.remise.circulation", "0" ) ) );
617 
618         return map;
619     }
620 
621     @Action( READ_COMMENTAIRE_CONFERENCE )
622     public String readCommentaireConference( HttpServletRequest request )
623     {
624         int empriseId = Integer.parseInt( request.getParameter( "empriseId" ) );
625         empriseService.updateEmpriseCommentaireConferenceRead( true, empriseId );
626         return redirectView( request, RECAP_EMPRISES );
627     }
628 
629     private void updateAllDetailsEmpriseFromRequest( HttpServletRequest request )
630     {
631         empriseCtrlService.updateEmpriseFromRequest( request, false );
632         int numDemande = formulaireService.getNumDemande( request );
633         fillLastUser( numDemande, request );
634         ctvUploadEmpriseDocumentHandler.storeFilesOnDisk( request, Document.FORMULAIRE_MULTI_CASES );
635     }
636 
637 }