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;
35  
36  import static fr.paris.lutece.plugins.ctv.constant.Document.ROC_AUTRES;
37  import static fr.paris.lutece.plugins.ctv.constant.Document.ROC_PV;
38  import static fr.paris.lutece.plugins.ctv.constant.Document.SAISINE_AUTRE_SERVICE;
39  import static fr.paris.lutece.plugins.ctv.constant.Document.SAISINE_COMMISSARIAT;
40  import static fr.paris.lutece.plugins.ctv.constant.Document.SAISINE_PREFECTURE_POLICE;
41  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACCEPTATION_FIN_TRAVAUX_RESEAU;
42  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACCEPTATION_FIN_TRAVAUX_VOIRIE;
43  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACCEPTATION_PROLONGATION;
44  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACCEPTATION_REMISE_EN_CIRCULATION;
45  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ANNULATION_EMPRISE;
46  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.DECLARATION_FIN_TRAVAUX_CIRCULATION;
47  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.DECLARATION_FIN_TRAVAUX_RESEAU;
48  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.DECLARATION_FIN_TRAVAUX_VOIRIE;
49  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.DECLARATION_PROLONGATION;
50  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.MAX_FILE_SIZE;
51  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.REFUS_FIN_TRAVAUX_CIRCULATION;
52  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.REFUS_FIN_TRAVAUX_RESEAU;
53  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.REFUS_FIN_TRAVAUX_VOIRIE;
54  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.REFUS_PROLONGATION;
55  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TEMPLATE_PROJET;
56  
57  import java.util.ArrayList;
58  import java.util.Calendar;
59  import java.util.Date;
60  import java.util.HashMap;
61  import java.util.List;
62  import java.util.Map;
63  import java.util.Set;
64  
65  import javax.servlet.http.HttpServletRequest;
66  import javax.servlet.http.HttpSession;
67  
68  import org.apache.commons.fileupload.FileItem;
69  
70  import fr.paris.lutece.plugins.ctv.bo.AlerteDemande.TypeAlerteDemande;
71  import fr.paris.lutece.plugins.ctv.bo.DemandeFinTravaux;
72  import fr.paris.lutece.plugins.ctv.bo.DetailsEmprise;
73  import fr.paris.lutece.plugins.ctv.bo.DetailsEmprise.TypeConfigurationSpecifique;
74  import fr.paris.lutece.plugins.ctv.bo.Emprise;
75  import fr.paris.lutece.plugins.ctv.bo.ItemReferentiel.TypeReferentiel;
76  import fr.paris.lutece.plugins.ctv.bo.Prolongation;
77  import fr.paris.lutece.plugins.ctv.bo.StatutProlongation;
78  import fr.paris.lutece.plugins.ctv.business.IAlerteDemandeService;
79  import fr.paris.lutece.plugins.ctv.business.IDemandeFinTravauxService;
80  import fr.paris.lutece.plugins.ctv.business.IDemandeService;
81  import fr.paris.lutece.plugins.ctv.business.IEmpriseService;
82  import fr.paris.lutece.plugins.ctv.business.IProlongationService;
83  import fr.paris.lutece.plugins.ctv.business.IReferentielsService;
84  import fr.paris.lutece.plugins.ctv.constant.StatutDemande;
85  import fr.paris.lutece.plugins.ctv.constant.StatutEmprise;
86  import fr.paris.lutece.plugins.ctv.constant.TypeDemande;
87  import fr.paris.lutece.plugins.ctv.constant.TypeFinTravaux;
88  import fr.paris.lutece.plugins.ctv.constant.TypeProfileCtv;
89  import fr.paris.lutece.plugins.ctv.constants.CtvConstants;
90  import fr.paris.lutece.plugins.ctv.handler.CtvUploadDemandeDocumentHandler;
91  import fr.paris.lutece.plugins.ctv.handler.CtvUploadEmpriseDocumentHandler;
92  import fr.paris.lutece.plugins.ctv.mailtask.EmailTask;
93  import fr.paris.lutece.plugins.ctv.util.GestionPDFUtil;
94  import fr.paris.lutece.plugins.ctv.view.object.DemandeReading;
95  import fr.paris.lutece.plugins.unittree.business.unit.Unit;
96  import fr.paris.lutece.plugins.unittree.business.unit.Unit.TypeUnit;
97  import fr.paris.lutece.plugins.unittree.service.unit.IUnitService;
98  import fr.paris.lutece.portal.business.user.AdminUser;
99  import fr.paris.lutece.portal.service.datastore.DatastoreService;
100 import fr.paris.lutece.portal.service.spring.SpringContextService;
101 import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
102 import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
103 import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
104 
105 @Controller( controllerJsp = "projet.jsp", controllerPath = "jsp/admin/plugins/ctv", right = "TABLEAU_DE_BORD" )
106 public class ProjetJspBean extends AbstractCtvJspBean
107 {
108 
109     private static final String SELECTED_DEMANDE_ID = "selected_demande_id";
110 
111     private static final long serialVersionUID = 1L;
112 
113     private static IReferentielsService referentielsService = SpringContextService.getBean( "referentielsService" );
114     private IEmpriseService empriseService = SpringContextService.getBean( "empriseService" );
115     private IDemandeFinTravauxService demandeFinTravauxService = SpringContextService.getBean( "demandeFinTravauxService" );
116     private IUnitService unitService = SpringContextService.getBean( IUnitService.BEAN_UNIT_SERVICE );
117 
118     private IProlongationService prolongationService = SpringContextService.getBean( "prolongationService" );
119     protected IDemandeService demandeService = SpringContextService.getBean( "demandeService" );
120     private static IAlerteDemandeService alerteDemandeService = SpringContextService.getBean( "alerteDemandeService" );
121     private static final CtvUploadDemandeDocumentHandler ctvDemandeUploadHandler = SpringContextService
122             .getBean( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER );
123     private static final CtvUploadEmpriseDocumentHandler ctvEmpriseUploadHandler = SpringContextService
124             .getBean( CtvUploadEmpriseDocumentHandler.CTV_EMPRISE_UPLOAD_HANDLER );
125 
126     protected static final EmailTasks/ctv/mailtask/EmailTask.html#EmailTask">EmailTask emailTask = new EmailTask( );
127 
128     @View( value = "projet", defaultView = true )
129     public String getProjet( HttpServletRequest request )
130     {
131         Map<String, Object> model = initModel( request );
132         return getPage( "", TEMPLATE_PROJET, model );
133 
134     }
135 
136     private Map<String, Object> initModel( HttpServletRequest request )
137     {
138         Map<String, Object> model = new HashMap<>( );
139         String numProjet = request.getParameter( "num_projet" );
140         String currentProfile = ctvProfileService.getCurrentProfile( getUser( ), getPlugin( ), request );
141 
142         AdminUser user = getUser( );
143         boolean isSuperviseur1 = ctvProfileService.hasUserProfile( user, TypeProfileCtv.SUPERVISEUR_1, getPlugin( ), request );
144         boolean isSuperviseur2 = ctvProfileService.hasUserProfile( user, TypeProfileCtv.SUPERVISEUR_2, getPlugin( ), request );
145         model.put( "isSuperviseur", isSuperviseur1 || isSuperviseur2 );
146 
147         if ( numProjet != null )
148         {
149             List<DemandeReading> listDemande = new ArrayList<>( );
150             DemandeReading demandeOrigine = fillInfosFilDArianeProjet( model, numProjet );
151             if ( demandeOrigine.getDemandeType( ).isIntentionType( ) && ctvProfileService.hasRight( currentProfile, "DEMANDE_INTENTION", getPlugin( ) ) )
152             {
153                 listDemande = getListDemandesByProfile( numProjet, TypeDemande.INTERVENTION, request, model );
154             }
155             else
156                 if ( ( demandeOrigine.getDemandeType( ) == TypeDemande.OCCUPATION )
157                         && ctvProfileService.hasRight( currentProfile, "DEMANDE_OCCUPATION", getPlugin( ) ) )
158                 {
159                     listDemande = getListDemandesByProfile( numProjet, TypeDemande.OCCUPATION, request, model );
160                 }
161 
162             String selectedDemandeId = request.getParameter( SELECTED_DEMANDE_ID );
163             Integer numDemande = selectedDemandeId != null ? Integer.parseInt( selectedDemandeId ) : null;
164             model.put( SELECTED_DEMANDE_ID, numDemande );
165 
166             if ( numDemande != null )
167             {
168                 boolean hasUserSameSectionBySector = false;
169                 if ( isSuperviseur1 )
170                 {
171                     hasUserSameSectionBySector = unitService.hasUserSameSubdivisionBySector( getUser( ).getUserId( ), demandeOrigine.getDemandeSectorId( ) );
172                 }
173                 else
174                     if ( isSuperviseur2 )
175                     {
176                         hasUserSameSectionBySector = unitService.hasUserSameSectionBySector( getUser( ).getUserId( ), demandeOrigine.getDemandeSectorId( ) );
177                     }
178                 model.put( "hasUserSameSectionBySector", hasUserSameSectionBySector );
179 
180                 fillUserPossibleActions( request, model, numDemande );
181                 ctvDemandeUploadHandler.removeFilesInSession( request );
182                 HttpSession session = request.getSession( );
183                 session.setAttribute( CtvConstants.SESSION_NUM_DEMANDE_ID, numDemande );
184                 model.put( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER, ctvDemandeUploadHandler );
185                 model.put( MAX_FILE_SIZE, ctvDemandeUploadHandler.getMaxFileSize( ) );
186                 model.put( "executionTravauxDocument_listUploadedFiles", ctvDemandeUploadHandler.getListUploadedFiles( "executionTravauxDocument", session ) );
187                 model.put( "demandeDocument_listUploadedFiles", ctvDemandeUploadHandler.getListUploadedFiles( "demandeDocument", session ) );
188                 model.put( "demandeDocument_listUploadedFiles_saisinePrefecturePolice",
189                         ctvDemandeUploadHandler.getListUploadedFiles( SAISINE_PREFECTURE_POLICE, session ) );
190                 model.put( "demandeDocument_listUploadedFiles_saisineCommissariat",
191                         ctvDemandeUploadHandler.getListUploadedFiles( SAISINE_COMMISSARIAT, session ) );
192                 model.put( "demandeDocument_listUploadedFiles_saisineAutreService",
193                         ctvDemandeUploadHandler.getListUploadedFiles( SAISINE_AUTRE_SERVICE, session ) );
194                 model.put( "demandeDocument_listUploadedFiles_pv_roc", ctvDemandeUploadHandler.getListUploadedFiles( ROC_PV, session ) );
195                 model.put( "demandeDocument_listUploadedFiles_roc_autres", ctvDemandeUploadHandler.getListUploadedFiles( ROC_AUTRES, session ) );
196 
197                 List<Emprise> emprises = empriseService.findEmprisesByDemandeId( numDemande );
198                 Map<String, List<FileItem>> documentsAutorisation = new HashMap<String, List<FileItem>>( );
199                 for ( Emprise emprise : emprises )
200                 {
201                     ctvEmpriseUploadHandler.removeFilesInSession( request );
202                     session.setAttribute( CtvConstants.EMPRISE_ID, emprise.getEmpriseId( ) );
203                     List<FileItem> uploadedFiles = ctvEmpriseUploadHandler.getListUploadedFiles( "autorisations", session );
204                     documentsAutorisation.put( Integer.toString( emprise.getEmpriseId( ) ), uploadedFiles );
205                 }
206                 model.put( "documentsAutorisation", documentsAutorisation );
207 
208             }
209             else
210             {
211                 if ( listDemande != null )
212                 {
213                     for ( DemandeReading demandeReading : listDemande )
214                     {
215                         putAlerteDemandeByProfile( model, TypeProfileCtv.INSTRUCTEUR, TypeAlerteDemande.PJ_DEMANDEUR, demandeReading.getDemandeNumero( ),
216                                 request );
217                         putAlerteDemandeByProfile( model, TypeProfileCtv.CONCESSIONNAIRE, TypeAlerteDemande.COMPTE_RENDU_VISITE,
218                                 demandeReading.getDemandeNumero( ), request );
219                         putAlerteDemandeByProfile( model, TypeProfileCtv.CONCESSIONNAIRE, TypeAlerteDemande.PJ_INSTRUCTEUR, demandeReading.getDemandeNumero( ),
220                                 request );
221                         putAlerteDemandeByProfile( model, TypeProfileCtv.CONCESSIONNAIRE, TypeAlerteDemande.CIRCULATION_ACCEPTEE,
222                                 demandeReading.getDemandeNumero( ), request );
223                         putAlerteDemandeByProfile( model, TypeProfileCtv.CONCESSIONNAIRE, TypeAlerteDemande.FIN_TRAVAUX_REFUSEE,
224                                 demandeReading.getDemandeNumero( ), request );
225                     }
226                 }
227 
228             }
229             model.put( "listDemande", listDemande );
230         }
231         model.put( "types_visite", referentielsService.getItemReferentielList( TypeReferentiel.TYPE_VISITE ) );
232 
233         if ( isSuperviseur1 || isSuperviseur2 )
234         {
235             model.put( "motifs_annulation", referentielsService.getItemReferentielList( TypeReferentiel.MOTIF_ANNULATION ) );
236         }
237 
238         int finTravauxTolerance = Integer.parseInt( DatastoreService.getDataValue( "parametre.numeric.intervention.fin.travaux.tolerance.declaration", "7" ) );
239         model.put( "finTravauxTolerance", finTravauxTolerance );
240         putPathDemande( model );
241         return model;
242     }
243 
244     private void putPathDemande( Map<String, Object> model )
245     {
246         model.put( MARK_PATH_DEMANDE_INTENTION, PATH_DEMANDE_INTENTION );
247         model.put( MARK_PATH_DEMANDE_OCCUPATION, PATH_DEMANDE_OCCUPATION );
248         model.put( MARK_PATH_DEMANDE_INTERVENTION, PATH_DEMANDE_INTERVENTION );
249     }
250 
251     public List<DemandeReading> getListDemandesByProfile( String numProjet, TypeDemande demandeType, HttpServletRequest request, Map<String, Object> model )
252     {
253         boolean isConcessionnaire = hasProfile( TypeProfileCtv.CONCESSIONNAIRE, request );
254         if ( isConcessionnaire )
255         {
256             Unit concession = unitService.getUnitUserByType( getUser( ), TypeUnit.CONCESSIONNAIRE );
257             List<Integer> concessionId = unitService.getAllUnitsIdByUnit( concession );
258             List<DemandeReading> demandes = demandeService.findAllDemandesByNumProjetAndType( numProjet, demandeType, null, concessionId );
259             List<String> listLabelUnits = new ArrayList<String>( );
260             DemandeReading demandeOrigine = (DemandeReading) model.get( "demandeOrigine" );
261 
262             for ( int unitId : concessionId )
263             {
264                 Unit unit = unitService.getUnit( unitId, false );
265                 listLabelUnits.add( unit.getLabel( ) );
266             }
267 
268             if ( ( listLabelUnits != null ) && !listLabelUnits.contains( demandeOrigine.getRaisonSociale( ) ) )
269             {
270                 model.remove( "demandeOrigine" );
271             }
272 
273             return demandes;
274         }
275         else
276         {
277             return demandeService.findAllDemandesByNumProjetAndType( numProjet, demandeType, StatutDemande.BROUILLON, null );
278         }
279     }
280 
281     @Action( DECLARATION_FIN_TRAVAUX_RESEAU )
282     public String declarerFinTravauxReseaux( HttpServletRequest request )
283     {
284         int empriseId = Integer.parseInt( request.getParameter( "empriseId" ) );
285         return declarationFinDeTravaux( request, TypeFinTravaux.RESEAU_DECLAREE, StatutEmprise.FIN_TRAVAUX_RESEAUX, empriseId );
286     }
287 
288     @Action( ANNULATION_EMPRISE )
289     public String annulationEmprise( HttpServletRequest request )
290     {
291         int empriseId = Integer.parseInt( request.getParameter( "empriseId" ) );
292         if ( empriseId > 0 )
293         {
294             int numDemande = Integer.parseInt( request.getParameter( SELECTED_DEMANDE_ID ) );
295             DemandeReading demande = demandeService.findByNumDemande( numDemande );
296             String motifAnnulationEmprise = request.getParameter( "motifAnnulationEmprise" );
297             String commentaireAnnulationEmprise = request.getParameter( "commentaireAnnulationEmprise" );
298             empriseService.updateAnnulationData( empriseId, motifAnnulationEmprise, commentaireAnnulationEmprise, getUser( ).getUserId( ) );
299             String contextPath = request.getContextPath( );
300             int errorNumber = GestionPDFUtil.gestionPDF( demande, GestionPDFUtil.MARK_TYPE_ANNULATION, Integer.toString( empriseId ), contextPath );
301             if ( errorNumber > 0 )
302             {
303                 return getErrorGenerationPdfPage( request, errorNumber );
304             }
305             empriseService.updateStatut( empriseId, StatutEmprise.ANNULEE );
306             List<Emprise> emprises = empriseService.findEmprisesByDemandeId( numDemande );
307             if ( ( emprises != null ) && !emprises.isEmpty( ) )
308             {
309                 int countAnnulees = 0;
310                 for ( Emprise emprise : emprises )
311                 {
312                     if ( StatutEmprise.ANNULEE.equals( emprise.getEmpriseStatut( ) ) )
313                     {
314                         countAnnulees++;
315                     }
316                 }
317 
318                 if ( emprises.size( ) == countAnnulees )
319                 {
320                     demandeService.updateDemandeStatut( numDemande, StatutDemande.ANNULEE );
321                 }
322             }
323             fillLastUser( numDemande, request );
324         }
325         return getProjet( request );
326     }
327 
328     @Action( DECLARATION_FIN_TRAVAUX_VOIRIE )
329     public String declarerFinTravauxVoirie( HttpServletRequest request )
330     {
331         int empriseId = Integer.parseInt( request.getParameter( "empriseId" ) );
332         Emprise emprise = empriseService.findEmpriseById( empriseId );
333         StatutEmprise statutEmprise = emprise.getEmpriseStatut( );
334         if ( StatutEmprise.REFECTION_EN_COURS.equals( statutEmprise ) )
335         {
336             statutEmprise = StatutEmprise.FIN_TRAVAUX_VOIRIE;
337         }
338         return declarationFinDeTravaux( request, TypeFinTravaux.VOIRIE_DECLAREE, statutEmprise, empriseId );
339     }
340 
341     @Action( DECLARATION_FIN_TRAVAUX_CIRCULATION )
342     public String declarerFinTravauxCirculation( HttpServletRequest request )
343     {
344         int empriseId = Integer.parseInt( request.getParameter( "empriseId" ) );
345         Emprise emprise = empriseService.findEmpriseById( empriseId );
346         StatutEmprise statutEmprise = emprise.getEmpriseStatut( );
347         if ( StatutEmprise.REFECTION_TERMINEE.equals( statutEmprise ) )
348         {
349             statutEmprise = StatutEmprise.REMISE_EN_CIRCULATION;
350         }
351         return declarationFinDeTravaux( request, TypeFinTravaux.CIRCULATION_DECLAREE, statutEmprise, empriseId );
352     }
353 
354     @Action( DECLARATION_PROLONGATION )
355     public String declarerProlongation( HttpServletRequest request )
356     {
357 
358         int empriseId = Integer.parseInt( request.getParameter( "empriseId" ) );
359         Emprise emprise = empriseService.findEmpriseById( empriseId );
360 
361         DemandeFinTravaux demandeFinTravaux = new DemandeFinTravaux( );
362         populate( demandeFinTravaux, request );
363 
364         if ( demandeFinTravaux.getFinTravauxReseauxDeclaree( ) != null )
365         {
366             emprise.setEmpriseDateFinTravauxReseauGestion( demandeFinTravaux.getFinTravauxReseauxDeclaree( ) );
367         }
368         if ( demandeFinTravaux.getFinTravauxVoirieDeclaree( ) != null )
369         {
370             emprise.setEmpriseDateFinTravauxVoirieGestion( demandeFinTravaux.getFinTravauxVoirieDeclaree( ) );
371         }
372         if ( demandeFinTravaux.getFinTravauxCirculationDeclaree( ) != null )
373         {
374             emprise.setEmpriseDateFinRemiseEnCirculationGestion( demandeFinTravaux.getFinTravauxCirculationDeclaree( ) );
375         }
376 
377         empriseService.updateEmprise( empriseId, emprise, false );
378         empriseService.updateStatut( empriseId, StatutEmprise.PROLONGATION );
379 
380         Prolongation prolongation = new Prolongation( );
381         prolongation.setProlongationHorodatageValidation( new Date( ) );
382         prolongation.setProlongationEmpriseId( empriseId );
383         prolongation.setProlongationStatut( StatutProlongation.DEMANDEE );
384 
385         prolongation.setProlongationReseauxDeclaree( emprise.getEmpriseDateFinTravauxReseauGestion( ) );
386         prolongation.setProlongationVoirieDeclaree( emprise.getEmpriseDateFinTravauxVoirieGestion( ) );
387         prolongation.setProlongationCirculationDeclaree( emprise.getEmpriseDateFinRemiseEnCirculationGestion( ) );
388         prolongation.setProlongationCommentaire( request.getParameter( "commentaireProlongation" ) );
389 
390         prolongationService.insertProlongation( prolongation );
391         return getProjet( request );
392     }
393 
394     @Action( ACCEPTATION_FIN_TRAVAUX_RESEAU )
395     public String accepterFinTravauxReseaux( HttpServletRequest request )
396     {
397         int empriseId = Integer.parseInt( request.getParameter( "empriseId" ) );
398         DemandeFinTravaux maDemande = demandeFinTravauxService.findDemandeFinTravauxByEmpriseId( empriseId );
399         empriseService.updateCompteurRelance(empriseId, null);
400         if ( ( maDemande == null ) || ( maDemande.getFinTravauxVoirieHorodatageDeclaration( ) == null ) )
401         {
402             return acceptationFinDeTravaux( request, TypeFinTravaux.RESEAU_VALIDEE, StatutEmprise.REFECTION_EN_COURS, empriseId );
403         }
404         else
405         {
406             return acceptationFinDeTravaux( request, TypeFinTravaux.RESEAU_VALIDEE, StatutEmprise.FIN_TRAVAUX_VOIRIE, empriseId );
407         }
408     }
409 
410     @Action( ACCEPTATION_FIN_TRAVAUX_VOIRIE )
411     public String accepterFinTravauxVoirie( HttpServletRequest request )
412     {
413         int empriseId = Integer.parseInt( request.getParameter( "empriseId" ) );
414         DemandeFinTravaux maDemande = demandeFinTravauxService.findDemandeFinTravauxByEmpriseId( empriseId );
415         empriseService.updateCompteurRelance(empriseId, null);
416         if ( maDemande.getFinTravauxCirculationHorodatageDeclaration( ) == null )
417         {
418             return acceptationFinDeTravaux( request, TypeFinTravaux.VOIRIE_VALIDEE, StatutEmprise.REFECTION_TERMINEE, empriseId );
419         }
420         else
421         {
422             return acceptationFinDeTravaux( request, TypeFinTravaux.VOIRIE_VALIDEE, StatutEmprise.REMISE_EN_CIRCULATION, empriseId );
423         }
424     }
425 
426     @Action( ACCEPTATION_REMISE_EN_CIRCULATION )
427     public String accepterRemiseEnCirculation( HttpServletRequest request )
428     {
429         String idEmprise = request.getParameter( "empriseId" );
430 		int empriseId = Integer.parseInt( idEmprise );
431         empriseService.updateCompteurRelance(empriseId, null);
432         return acceptationFinDeTravaux( request, TypeFinTravaux.CIRCULATION_VALIDEE, StatutEmprise.GARANTIE, empriseId );
433     }
434 
435     @Action( REFUS_FIN_TRAVAUX_RESEAU )
436     public String refusFinTravauxReseaux( HttpServletRequest request )
437     {
438         int empriseId = Integer.parseInt( request.getParameter( "empriseId" ) );
439         if ( empriseId > 0 )
440         {
441             empriseService.updateStatut( empriseId, StatutEmprise.TRAVAUX_EN_COURS );
442             DemandeFinTravaux demandeFinTravaux = demandeFinTravauxService.findDemandeFinTravauxByEmpriseId( empriseId );
443             demandeFinTravaux.setFinTravauxType( TypeFinTravaux.RESEAU_REFUSEE );
444             demandeFinTravauxService.updateDemandeFinTravaux( demandeFinTravaux );
445 
446             // MAJ des dates après refus
447             Emprise emprise = empriseService.findEmpriseById( empriseId );
448 
449             int numDemande = Integer.parseInt( request.getParameter( SELECTED_DEMANDE_ID ) );
450             DemandeReading maDemande = demandeService.findByNumDemande( numDemande );
451 
452             if ( emprise.getEmpriseDateFinTravauxReseauAcceptee( ) != null )
453             {
454                 emprise.setEmpriseDateFinTravauxReseauGestion( emprise.getEmpriseDateFinTravauxReseauAcceptee( ) );
455             }
456             else
457             {
458                 emprise.setEmpriseDateFinTravauxReseauGestion( emprise.getEmpriseDateFinTravauxReseau( ) );
459             }
460 
461             if ( emprise.getEmpriseDateFinTravauxVoirieAcceptee( ) != null )
462             {
463                 emprise.setEmpriseDateFinTravauxVoirieGestion( emprise.getEmpriseDateFinTravauxVoirieAcceptee( ) );
464             }
465             else
466             {
467                 emprise.setEmpriseDateFinTravauxVoirieGestion( emprise.getEmpriseDateFinTravauxVoirie( ) );
468             }
469 
470             if ( emprise.getEmpriseDateFinRemiseEnCirculationAcceptee( ) != null )
471             {
472                 emprise.setEmpriseDateFinRemiseEnCirculationGestion( emprise.getEmpriseDateFinRemiseEnCirculationAcceptee( ) );
473             }
474             else
475             {
476                 emprise.setEmpriseDateFinRemiseEnCirculationGestion( emprise.getEmpriseDateFin( ) );
477             }
478             empriseService.updateEmprise( empriseId, emprise, false );
479 
480             addAlerteDemande( request, TypeAlerteDemande.FIN_TRAVAUX_REFUSEE );
481             if ( maDemande.getDemandeType( ).equals( TypeDemande.OCCUPATION ) )
482             {
483                 emailTask.sendEmail( CtvConstants.EMAIL_REFUS_DEMANDE_TRAVAUX_RESEAUX, numDemande, getLocale( ), request, empriseId );
484             }
485             fillLastUser( numDemande, request );
486         }
487         return getProjet( request );
488     }
489 
490     @Action( REFUS_FIN_TRAVAUX_VOIRIE )
491     public String refusFinTravauxVoirie( HttpServletRequest request )
492     {
493         int empriseId = Integer.parseInt( request.getParameter( "empriseId" ) );
494         if ( empriseId > 0 )
495         {
496             empriseService.updateStatut( empriseId, StatutEmprise.REFECTION_EN_COURS );
497             DemandeFinTravaux demandeFinTravaux = demandeFinTravauxService.findDemandeFinTravauxByEmpriseId( empriseId );
498             demandeFinTravaux.setFinTravauxType( TypeFinTravaux.VOIRIE_REFUSEE );
499             demandeFinTravauxService.updateDemandeFinTravaux( demandeFinTravaux );
500 
501             // MAJ des dates après refus
502             Emprise emprise = empriseService.findEmpriseById( empriseId );
503 
504             if ( emprise.getEmpriseDateFinTravauxVoirieAcceptee( ) != null )
505             {
506                 emprise.setEmpriseDateFinTravauxVoirieGestion( emprise.getEmpriseDateFinTravauxVoirieAcceptee( ) );
507             }
508             else
509             {
510                 emprise.setEmpriseDateFinTravauxVoirieGestion( emprise.getEmpriseDateFinTravauxVoirie( ) );
511             }
512 
513             if ( emprise.getEmpriseDateFinRemiseEnCirculationAcceptee( ) != null )
514             {
515                 emprise.setEmpriseDateFinRemiseEnCirculationGestion( emprise.getEmpriseDateFinRemiseEnCirculationAcceptee( ) );
516             }
517             else
518             {
519                 emprise.setEmpriseDateFinRemiseEnCirculationGestion( emprise.getEmpriseDateFin( ) );
520             }
521 
522             empriseService.updateEmprise( empriseId, emprise, false );
523 
524             addAlerteDemande( request, TypeAlerteDemande.FIN_TRAVAUX_REFUSEE );
525             int numDemande = Integer.parseInt( request.getParameter( SELECTED_DEMANDE_ID ) );
526             DemandeReading maDemande = demandeService.findByNumDemande( numDemande );
527             if ( maDemande.getDemandeType( ).equals( TypeDemande.OCCUPATION ) )
528             {
529                 emailTask.sendEmail( CtvConstants.EMAIL_REFUS_DEMANDE_TRAVAUX_VOIRIE, numDemande, getLocale( ), request, empriseId );
530             }
531             fillLastUser( numDemande, request );
532         }
533         return getProjet( request );
534     }
535 
536     @Action( REFUS_FIN_TRAVAUX_CIRCULATION )
537     public String refusFinTravauxCirculation( HttpServletRequest request )
538     {
539         int empriseId = Integer.parseInt( request.getParameter( "empriseId" ) );
540         if ( empriseId > 0 )
541         {
542             empriseService.updateStatut( empriseId, StatutEmprise.REFECTION_TERMINEE );
543             DemandeFinTravaux demandeFinTravaux = demandeFinTravauxService.findDemandeFinTravauxByEmpriseId( empriseId );
544             demandeFinTravaux.setFinTravauxType( TypeFinTravaux.CIRCULATION_REFUSEE );
545             demandeFinTravauxService.updateDemandeFinTravaux( demandeFinTravaux );
546 
547             // MAJ des dates après refus
548             Emprise emprise = empriseService.findEmpriseById( empriseId );
549             if ( emprise.getEmpriseDateFinRemiseEnCirculationAcceptee( ) != null )
550             {
551                 emprise.setEmpriseDateFinRemiseEnCirculationGestion( emprise.getEmpriseDateFinRemiseEnCirculationAcceptee( ) );
552             }
553             else
554             {
555                 emprise.setEmpriseDateFinRemiseEnCirculationGestion( emprise.getEmpriseDateFin( ) );
556             }
557             empriseService.updateEmprise( empriseId, emprise, false );
558 
559             addAlerteDemande( request, TypeAlerteDemande.FIN_TRAVAUX_REFUSEE );
560             int numDemande = Integer.parseInt( request.getParameter( SELECTED_DEMANDE_ID ) );
561             DemandeReading maDemande = demandeService.findByNumDemande( numDemande );
562             if ( maDemande.getDemandeType( ).equals( TypeDemande.OCCUPATION ) )
563             {
564                 emailTask.sendEmail( CtvConstants.EMAIL_REFUS_DEMANDE_REMISE_EN_CIRCULATION, numDemande, getLocale( ), request, empriseId );
565             }
566             fillLastUser( numDemande, request );
567         }
568         return getProjet( request );
569     }
570 
571     @Action( REFUS_PROLONGATION )
572     public String refusProlongation( HttpServletRequest request )
573     {
574 
575         int empriseId = Integer.parseInt( request.getParameter( "empriseId" ) );
576         int numDemande = Integer.parseInt( request.getParameter( SELECTED_DEMANDE_ID ) );
577 
578         AdminUser user = getUser( );
579         boolean isSuperviseur1 = ctvProfileService.hasUserProfile( user, TypeProfileCtv.SUPERVISEUR_1, getPlugin( ), request );
580         boolean isSuperviseur2 = ctvProfileService.hasUserProfile( user, TypeProfileCtv.SUPERVISEUR_2, getPlugin( ), request );
581 
582         if ( ( request.getParameter( "prolongationId" ) != null ) && !request.getParameter( "prolongationId" ).isEmpty( ) )
583         {
584             int prolongationId = Integer.parseInt( request.getParameter( "prolongationId" ) );
585             if ( ( empriseId > 0 ) && ( prolongationId > 0 ) )
586             {
587                 Prolongation prolongation = prolongationService.findLastProlongationByEmpriseId( empriseId );
588                 Prolongation oldProlongationAccepted = prolongationService.findLastProlongationAcceptedByEmpriseId( empriseId );
589 
590                 prolongationService.updateStatut( prolongationId, StatutProlongation.REFUSEE );
591                 DemandeFinTravaux maDemandeFinTravaux = demandeFinTravauxService.findDemandeFinTravauxByEmpriseId( empriseId );
592                 Emprise emprise = empriseService.findEmpriseById( empriseId );
593 
594                 if ( isSuperviseur1 || isSuperviseur2 )
595                 {
596                     prolongation.setProlongationHorodatageSignature( new Date( ) );
597                 }
598                 else
599                 {
600                     prolongation.setProlongationHorodatageValidation( new Date( ) );
601                 }
602                 if ( ( maDemandeFinTravaux == null ) || ( maDemandeFinTravaux.getFinTravauxReseauxHorodatageDeclaration( ) == null ) )
603                 {
604                     empriseService.updateStatut( empriseId, StatutEmprise.TRAVAUX_EN_COURS );
605 
606                     if ( oldProlongationAccepted == null )
607                     {
608                         emprise.setEmpriseDateFinTravauxReseauGestion( emprise.getEmpriseDateFinTravauxReseau( ) );
609                         emprise.setEmpriseDateFinTravauxVoirieGestion( emprise.getEmpriseDateFinTravauxVoirie( ) );
610                         emprise.setEmpriseDateFinRemiseEnCirculationGestion( emprise.getEmpriseDateFin( ) );
611                     }
612                     else
613                     {
614                         emprise.setEmpriseDateFinTravauxReseauGestion( oldProlongationAccepted.getProlongationReseauxDeclaree( ) );
615                         emprise.setEmpriseDateFinTravauxVoirieGestion( oldProlongationAccepted.getProlongationVoirieDeclaree( ) );
616                         emprise.setEmpriseDateFinRemiseEnCirculationGestion( oldProlongationAccepted.getProlongationCirculationDeclaree( ) );
617                     }
618                 }
619                 if ( ( maDemandeFinTravaux != null ) && ( ( maDemandeFinTravaux.getFinTravauxReseauxHorodatageValidation( ) != null )
620                         && ( maDemandeFinTravaux.getFinTravauxVoirieHorodatageDeclaration( ) == null ) ) )
621                 {
622                     empriseService.updateStatut( empriseId, StatutEmprise.REFECTION_EN_COURS );
623                     emprise.setEmpriseDateFinTravauxReseauGestion( maDemandeFinTravaux.getFinTravauxReseauxDeclaree( ) );
624                     if ( oldProlongationAccepted == null )
625                     {
626                         emprise.setEmpriseDateFinTravauxVoirieGestion( maDemandeFinTravaux.getFinTravauxVoirieDeclaree( ) );
627                         emprise.setEmpriseDateFinRemiseEnCirculationGestion( maDemandeFinTravaux.getFinTravauxCirculationDeclaree( ) );
628                     }
629                     else
630                     {
631                         emprise.setEmpriseDateFinTravauxVoirieGestion( oldProlongationAccepted.getProlongationVoirieDeclaree( ) );
632                         emprise.setEmpriseDateFinRemiseEnCirculationGestion( oldProlongationAccepted.getProlongationCirculationDeclaree( ) );
633                     }
634                 }
635                 if ( ( maDemandeFinTravaux != null ) && ( ( maDemandeFinTravaux.getFinTravauxVoirieHorodatageValidation( ) != null )
636                         && ( maDemandeFinTravaux.getFinTravauxCirculationHorodatageDeclaration( ) == null ) ) )
637                 {
638                     empriseService.updateStatut( empriseId, StatutEmprise.REFECTION_TERMINEE );
639                     emprise.setEmpriseDateFinTravauxReseauGestion( maDemandeFinTravaux.getFinTravauxReseauxDeclaree( ) );
640                     emprise.setEmpriseDateFinTravauxVoirieGestion( maDemandeFinTravaux.getFinTravauxVoirieDeclaree( ) );
641                     if ( oldProlongationAccepted == null )
642                     {
643                         emprise.setEmpriseDateFinRemiseEnCirculationGestion( maDemandeFinTravaux.getFinTravauxCirculationDeclaree( ) );
644                     }
645                     else
646                     {
647                         emprise.setEmpriseDateFinRemiseEnCirculationGestion( oldProlongationAccepted.getProlongationCirculationDeclaree( ) );
648                     }
649                 }
650 
651                 empriseService.updateEmprise( empriseId, emprise, false );
652                 prolongationService.updateProlongation( prolongation );
653 
654                 DemandeReading maDemande = demandeService.findByNumDemande( numDemande );
655                 if ( TypeDemande.OCCUPATION.equals( maDemande.getDemandeType( ) ) )
656                 {
657                     emailTask.sendEmail( CtvConstants.EMAIL_REFUS_PROLONGATION, numDemande, getLocale( ), request, empriseId );
658                 }
659                 fillLastUser( numDemande, request );
660                 addAlerteDemande( request, TypeAlerteDemande.PROLONGATION_REFUSEE );
661 
662             }
663         }
664         return getProjet( request );
665     }
666 
667     @Action( ACCEPTATION_PROLONGATION )
668     public String acceptationProlongation( HttpServletRequest request )
669     {
670         int empriseId = Integer.parseInt( request.getParameter( "empriseId" ) );
671         int numDemande = Integer.parseInt( request.getParameter( SELECTED_DEMANDE_ID ) );
672 
673         AdminUser user = getUser( );
674         boolean isSuperviseur1 = ctvProfileService.hasUserProfile( user, TypeProfileCtv.SUPERVISEUR_1, getPlugin( ), request );
675         boolean isSuperviseur2 = ctvProfileService.hasUserProfile( user, TypeProfileCtv.SUPERVISEUR_2, getPlugin( ), request );
676 
677         if ( ( empriseId > 0 ) && ( prolongationService.findLastProlongationByEmpriseId( empriseId ) != null ) )
678         {
679 
680             DemandeFinTravaux demandeFinTravaux = new DemandeFinTravaux( );
681             populate( demandeFinTravaux, request );
682 
683             Prolongation prolongation = prolongationService.findLastProlongationByEmpriseId( empriseId );
684 
685             Emprise emprise = empriseService.findEmpriseById( empriseId );
686 
687             if ( ( demandeFinTravaux != null ) && ( demandeFinTravaux.getFinTravauxReseauxDeclaree( ) != null ) )
688             {
689                 prolongation.setProlongationReseauxDeclaree( demandeFinTravaux.getFinTravauxReseauxDeclaree( ) );
690                 emprise.setEmpriseDateFinTravauxReseauAcceptee( demandeFinTravaux.getFinTravauxReseauxDeclaree( ) );
691                 emprise.setEmpriseDateFinTravauxReseauGestion( demandeFinTravaux.getFinTravauxReseauxDeclaree( ) );
692             }
693             if ( ( demandeFinTravaux != null ) && ( demandeFinTravaux.getFinTravauxVoirieDeclaree( ) != null ) )
694             {
695                 prolongation.setProlongationVoirieDeclaree( demandeFinTravaux.getFinTravauxVoirieDeclaree( ) );
696                 emprise.setEmpriseDateFinTravauxVoirieAcceptee( demandeFinTravaux.getFinTravauxVoirieDeclaree( ) );
697                 emprise.setEmpriseDateFinTravauxVoirieGestion( demandeFinTravaux.getFinTravauxVoirieDeclaree( ) );
698             }
699             if ( ( demandeFinTravaux != null ) && ( demandeFinTravaux.getFinTravauxCirculationDeclaree( ) != null ) )
700             {
701                 prolongation.setProlongationCirculationDeclaree( demandeFinTravaux.getFinTravauxCirculationDeclaree( ) );
702                 emprise.setEmpriseDateFinRemiseEnCirculationAcceptee( demandeFinTravaux.getFinTravauxCirculationDeclaree( ) );
703                 emprise.setEmpriseDateFinRemiseEnCirculationGestion( demandeFinTravaux.getFinTravauxCirculationDeclaree( ) );
704             }
705 
706 
707             DemandeReading maDemande = demandeService.findByNumDemande( numDemande );
708             prolongationService.updateProlongation( prolongation );
709 
710             if ( isSuperviseur1 || isSuperviseur2 )
711             {
712                 prolongation.setProlongationHorodatageSignature( new Date( ) );
713                 prolongation.setProlongationSuperviseurId( user.getUserId( ) );
714                 prolongationService.updateStatut( prolongation.getProlongationId( ), StatutProlongation.ACCEPTEE );
715                 prolongationService.updateProlongation( prolongation );
716                 // generation autorisation PDF
717                 String contextPath = request.getContextPath( );
718                 int errorNumber = GestionPDFUtil.gestionPDF( maDemande, GestionPDFUtil.MARK_TYPE_PROLONGATION, Integer.toString( empriseId ), contextPath );
719                 if ( errorNumber > 0 )
720                 {
721                     return getErrorGenerationPdfPage( request, errorNumber );
722                 }
723 
724                 demandeFinTravaux = demandeFinTravauxService.findDemandeFinTravauxByEmpriseId( empriseId );
725                 if ( ( demandeFinTravaux == null ) || ( demandeFinTravaux.getFinTravauxReseauxHorodatageDeclaration( ) == null ) )
726                 {
727                     empriseService.updateStatut( empriseId, StatutEmprise.TRAVAUX_EN_COURS );
728                 }
729                 else
730                     if ( ( demandeFinTravaux.getFinTravauxReseauxHorodatageValidation( ) != null )
731                             && ( demandeFinTravaux.getFinTravauxVoirieHorodatageDeclaration( ) == null ) )
732                     {
733                         empriseService.updateStatut( empriseId, StatutEmprise.REFECTION_EN_COURS );
734                     }
735                     else
736                         if ( ( demandeFinTravaux.getFinTravauxVoirieHorodatageValidation( ) != null )
737                                 && ( demandeFinTravaux.getFinTravauxCirculationHorodatageDeclaration( ) == null ) )
738                         {
739                             empriseService.updateStatut( empriseId, StatutEmprise.REFECTION_TERMINEE );
740                         }
741                 if ( maDemande.getDemandeType( ).equals( TypeDemande.OCCUPATION ) )
742                 {
743                     emailTask.sendEmail( CtvConstants.EMAIL_ACCEPTATION_PROLONGATION, numDemande, getLocale( ), request, empriseId );
744                 }
745                 else
746                     if ( maDemande.getDemandeType( ).equals( TypeDemande.INTERVENTION ) )
747                     {
748                         emailTask.sendEmail( CtvConstants.EMAIL_ACCEPTATION_PROLONGATION_INTERVENTION, numDemande, getLocale( ), request, empriseId );
749                     }
750                 alerteDemandeService.insertAlerte( TypeAlerteDemande.SIGNE_CONCESSIONNAIRE, numDemande );
751             }
752             else
753             {
754                 prolongation.setProlongationHorodatageValidation( new Date( ) );
755             }
756             prolongationService.updateProlongation( prolongation );
757             empriseService.updateEmprise( empriseId, emprise, false );
758             empriseService.updateCompteurRelance(empriseId, null);
759             fillLastUser( numDemande, request );
760         }
761 
762         return getProjet( request );
763     }
764 
765     private String getErrorGenerationPdfPage( HttpServletRequest request, int errorNumber )
766     {
767         Map<String, Object> model = initModel( request );
768         model.put( GestionPDFUtil.MARK_ERROR_GEN_PDF, errorNumber );
769         return getPage( "", TEMPLATE_PROJET, model );
770     }
771 
772     private String declarationFinDeTravaux( HttpServletRequest request, TypeFinTravaux type, StatutEmprise statutEmprise, int empriseId )
773     {
774         if ( empriseId > 0 )
775         {
776             DemandeFinTravaux demandeFinTravaux = new DemandeFinTravaux( );
777             demandeFinTravaux.setFinTravauxType( type );
778             populate( demandeFinTravaux, request );
779             Emprise emprise = empriseService.findEmpriseById( empriseId );
780 
781             boolean reseauDeclare = type.equals( TypeFinTravaux.RESEAU_DECLAREE );
782             boolean voirieDeclaree = reseauDeclare || type.equals( TypeFinTravaux.VOIRIE_DECLAREE );
783             boolean circulationDeclaree = voirieDeclaree || type.equals( TypeFinTravaux.CIRCULATION_DECLAREE );
784 
785             if ( reseauDeclare )
786             {
787                 emprise.setEmpriseDateFinTravauxReseauGestion( demandeFinTravaux.getFinTravauxReseauxDeclaree( ) );
788             }
789             if ( voirieDeclaree )
790             {
791                 emprise.setEmpriseDateFinTravauxVoirieGestion( demandeFinTravaux.getFinTravauxVoirieDeclaree( ) );
792             }
793             if ( circulationDeclaree )
794             {
795                 emprise.setEmpriseDateFinRemiseEnCirculationGestion( demandeFinTravaux.getFinTravauxCirculationDeclaree( ) );
796             }
797 
798             empriseService.updateEmprise( empriseId, emprise, false );
799             empriseService.updateStatut( empriseId, statutEmprise );
800             demandeFinTravauxService.upsertDemandeFinTravaux( demandeFinTravaux );
801         }
802         return getProjet( request );
803     }
804 
805     private String acceptationFinDeTravaux( HttpServletRequest request, TypeFinTravaux type, StatutEmprise statutEmprise, int empriseId )
806     {
807 
808         if ( empriseId > 0 )
809         {
810             Emprise emprise = empriseService.findEmpriseById( empriseId );
811             emprise.setEmpriseStatut( statutEmprise );
812 
813             DemandeFinTravaux demandeFinTravaux = new DemandeFinTravaux( );
814             populate( demandeFinTravaux, request );
815 
816             boolean reseauValide = type.equals( TypeFinTravaux.RESEAU_VALIDEE );
817             boolean voirieValidee = type.equals( TypeFinTravaux.VOIRIE_VALIDEE );
818             boolean circulationValidee = type.equals( TypeFinTravaux.CIRCULATION_VALIDEE );
819 
820             if ( reseauValide )
821             {
822                 emprise.setEmpriseDateFinTravauxReseauAcceptee( demandeFinTravaux.getFinTravauxReseauxDeclaree( ) );
823             }
824             if ( voirieValidee )
825             {
826                 emprise.setEmpriseDateFinTravauxVoirieAcceptee( demandeFinTravaux.getFinTravauxVoirieDeclaree( ) );
827             }
828             if ( circulationValidee )
829             {
830                 emprise.setEmpriseDateFinRemiseEnCirculationAcceptee( demandeFinTravaux.getFinTravauxCirculationDeclaree( ) );
831 
832                 if ( "true".equals( request.getParameter( "hasGel" ) ) )
833                 {
834                     populateDateDeGel( request, emprise );
835 
836                 }
837                 else
838                 {
839                     emprise.setEmpriseDateDeGel( null );
840                 }
841                 addAlerteDemande( request, TypeAlerteDemande.CIRCULATION_ACCEPTEE );
842                 int numDemande = Integer.parseInt( request.getParameter( SELECTED_DEMANDE_ID ) );
843                 DemandeReading maDemande = demandeService.findByNumDemande( numDemande );
844                 if ( maDemande.getDemandeType( ).equals( TypeDemande.OCCUPATION )
845                         && ( ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.SUPERVISEUR_1, getPlugin( ), request )
846                                 || ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.SUPERVISEUR_2, getPlugin( ), request )
847                                 || ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.INSTRUCTEUR, getPlugin( ), request ) ) )
848                 {
849                     emailTask.sendEmail( CtvConstants.EMAIL_ACCEPTATION_REMISE_EN_CIRCULATION, numDemande, getLocale( ), request, empriseId );
850                 }
851 
852             }
853 
854             empriseService.updateEmprise( empriseId, emprise, false );
855             empriseService.updateStatut( empriseId, statutEmprise );
856             demandeFinTravaux.setFinTravauxType( type );
857             demandeFinTravauxService.updateDemandeFinTravaux( demandeFinTravaux );
858             String numDemande = request.getParameter( SELECTED_DEMANDE_ID );
859             int demandeId = Integer.parseInt( numDemande );
860             fillLastUser( demandeId, request );
861             DetailsEmprise details = empriseService.findDetailsEmpriseById( empriseId );
862             Set<TypeConfigurationSpecifique> confSpe = details.getConfigurationsSpecifiques( );
863             if ( confSpe != null )
864             {
865                 if ( confSpe.contains( TypeConfigurationSpecifique.EMPRISE_MODIFIANT_SOL ) )
866                 {
867                     Emprise empriseExisting = empriseService.findEmpriseById( empriseId );
868                     if ( "OUI".equals( empriseExisting.getEmpriseVueEnConference( ) ) && "true".equals( request.getParameter( "hasGel" ) ) )
869                     {
870                         Date empriseDateFin = empriseExisting.getEmpriseDateFinRemiseEnCirculationGestion( );
871                         if ( empriseDateFin != null )
872                         {
873                             int paramDelaiDateGel = Integer
874                                     .parseInt( DatastoreService.getDataValue( "parametre.numeric.intervention.duree.periode.gel", "3" ) );
875                             empriseService.updateDateDeGel( empriseId, addDate( empriseDateFin, paramDelaiDateGel ) );
876                         }
877                     }
878                 }
879             }
880             
881             if(circulationValidee && StatutEmprise.GARANTIE.equals(statutEmprise)) {
882             	String contextPath = request.getContextPath( );
883                 DemandeReading demande = demandeService.findByNumDemande(emprise.getEmpriseDemandeId());
884                 int errorNumber = GestionPDFUtil.gestionPDF( demande, GestionPDFUtil.MARK_TYPE_GARANTIE, String.valueOf(empriseId), contextPath );
885                 if ( errorNumber > 0 )
886                 {
887                     return getErrorGenerationPdfPage( request, errorNumber );
888                 }
889             }
890         }
891         return getProjet( request );
892     }
893 
894     private Date addDate( Date empriseDateFinRemiseEnCirculationGestion, int paramDelaiDateGel )
895     {
896         Calendar calendar = Calendar.getInstance( );
897         calendar.setTime( empriseDateFinRemiseEnCirculationGestion );
898         calendar.add( Calendar.YEAR, paramDelaiDateGel );
899         return new Date( calendar.getTimeInMillis( ) );
900     }
901 
902     private void populateDateDeGel( HttpServletRequest request, Emprise emprise )
903     {
904         Emprise empriseFromRequest = new Emprise( );
905         populate( empriseFromRequest, request );
906         request.getParameter( "empriseDateDeGel" );
907         if ( empriseFromRequest.getEmpriseDateDeGel( ) != null )
908         {
909             emprise.setEmpriseDateDeGel( empriseFromRequest.getEmpriseDateDeGel( ) );
910         }
911     }
912 
913     private void addAlerteDemande( HttpServletRequest request, TypeAlerteDemande type )
914     {
915         String numDemande = request.getParameter( SELECTED_DEMANDE_ID );
916         alerteDemandeService.insertAlerte( type, Integer.parseInt( numDemande ) );
917     }
918 
919     @Action( "valider_documents" )
920     public String validerDocuments( HttpServletRequest request )
921     {
922         boolean storedDemandeDocument = ctvDemandeUploadHandler.storeFilesOnDisk( request, "demandeDocument" );
923         if ( storedDemandeDocument )
924         {
925             addAlerteDemande( request, TypeAlerteDemande.PJ_INSTRUCTEUR );
926         }
927         String numDemande = request.getParameter( SELECTED_DEMANDE_ID );
928         int demandeId = Integer.parseInt( numDemande );
929         fillLastUser( demandeId, request );
930         return getProjet( request );
931     }
932 
933     @Action( "valider_executions_travaux" )
934     public String validerExecutionsTravaux( HttpServletRequest request )
935     {
936         String commentaire = request.getParameter( "commentaire" );
937         String compteTiers = request.getParameter( "compteTiers" );
938         String numDemande = request.getParameter( SELECTED_DEMANDE_ID );
939         demandeService.updateExecutionTravaux( numDemande, compteTiers, commentaire );
940         boolean storedExecutionTravauxDocument = ctvDemandeUploadHandler.storeFilesOnDisk( request, "executionTravauxDocument" );
941         if ( storedExecutionTravauxDocument )
942         {
943             addAlerteDemande( request, TypeAlerteDemande.PJ_DEMANDEUR );
944         }
945         int demandeId = Integer.parseInt( numDemande );
946         fillLastUser( demandeId, request );
947         return getProjet( request );
948     }
949 
950     @Action( "saisie_commentaires" )
951     public String saisieCommentaires( HttpServletRequest request )
952     {
953         int empriseId = Integer.parseInt( request.getParameter( "empriseId" ) );
954         boolean isConcessionnaire = hasProfile( TypeProfileCtv.CONCESSIONNAIRE, request );
955         if ( isConcessionnaire )
956         {
957             String commentaire = request.getParameter( "commentaireDemandeur" );
958             empriseService.updateCommentaireDemandeur( empriseId, commentaire );
959         }
960         else
961         {
962             String commentaire = request.getParameter( "commentaireInstructeur" );
963             empriseService.updateCommentaireInstructeur( empriseId, commentaire );
964 
965         }
966         String numDemande = request.getParameter( SELECTED_DEMANDE_ID );
967         int demandeId = Integer.parseInt( numDemande );
968         fillLastUser( demandeId, request );
969         return getProjet( request );
970     }
971 
972     private boolean hasProfile( TypeProfileCtv type, HttpServletRequest request )
973     {
974         AdminUser user = getUser( );
975         return ctvProfileService.hasUserProfile( user, type, getPlugin( ), request );
976     }
977 
978     @Action( "remove_alert" )
979     public String removeAlert( HttpServletRequest request )
980     {
981         int idAlerte = Integer.parseInt( request.getParameter( "idAlerte" ) );
982         alerteDemandeService.removeAlerte( idAlerte );
983         return getProjet( request );
984     }
985 
986 }