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.service;
35  
36  import static fr.paris.lutece.plugins.ctv.constant.Document.AUTRES_DOCUMENTS;
37  import static fr.paris.lutece.plugins.ctv.constant.Document.CROQUIS;
38  import static fr.paris.lutece.plugins.ctv.constant.Document.ETAT_DES_LIEUX;
39  import static fr.paris.lutece.plugins.ctv.constant.Document.FORMULAIRE_MULTI_CASES;
40  import static fr.paris.lutece.plugins.ctv.constant.Document.JUSTIFICATIFS_CONVENTION;
41  import static fr.paris.lutece.plugins.ctv.constant.Document.NOTE_CALCUL;
42  import static fr.paris.lutece.plugins.ctv.constant.Document.PLAN_EXPLOITATION;
43  import static fr.paris.lutece.plugins.ctv.constant.Document.PLAN_SITUATION;
44  import static fr.paris.lutece.plugins.ctv.constant.Document.ROC_AUTRES;
45  import static fr.paris.lutece.plugins.ctv.constant.Document.ROC_PV;
46  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.COMMENTAIRE_USAGER;
47  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.DEMANDE;
48  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.EMPRISES_IN_ERROR;
49  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_CHANTIER;
50  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_DEMANDEUR;
51  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_EMPRISE;
52  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_EXECUTANT_TRAVAUX;
53  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_SUPERVISEUR;
54  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.FIELDS_IN_ERROR;
55  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.LISTE_EMPRISES_RECAP;
56  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.NATURES_CHANTIER;
57  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.REQUIRED_FIELS_IN_ERROR;
58  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.SESSION_NUM_DEMANDE_ID;
59  
60  import java.util.ArrayList;
61  import java.util.Arrays;
62  import java.util.HashMap;
63  import java.util.List;
64  import java.util.Map;
65  
66  import javax.servlet.http.HttpServletRequest;
67  
68  import org.apache.commons.fileupload.FileItem;
69  
70  import fr.paris.lutece.plugins.ctv.bo.ChantierIntervention;
71  import fr.paris.lutece.plugins.ctv.bo.Conflit;
72  import fr.paris.lutece.plugins.ctv.bo.Convention;
73  import fr.paris.lutece.plugins.ctv.bo.DetailsEmprise;
74  import fr.paris.lutece.plugins.ctv.bo.Emprise;
75  import fr.paris.lutece.plugins.ctv.bo.EntiteDemande;
76  import fr.paris.lutece.plugins.ctv.bo.InfosSuperviseur;
77  import fr.paris.lutece.plugins.ctv.business.IConflitService;
78  import fr.paris.lutece.plugins.ctv.business.IInfosSuperviseurService;
79  import fr.paris.lutece.plugins.ctv.business.IReferentielsService;
80  import fr.paris.lutece.plugins.ctv.group.IEntiteDemandeValidationGroup;
81  import fr.paris.lutece.plugins.ctv.util.Utils;
82  import fr.paris.lutece.plugins.ctv.view.object.DemandeReading;
83  import fr.paris.lutece.portal.business.user.AdminUser;
84  import fr.paris.lutece.portal.business.user.AdminUserHome;
85  import fr.paris.lutece.portal.service.datastore.DatastoreService;
86  import fr.paris.lutece.portal.service.spring.SpringContextService;
87  
88  public class FormulaireDemandeInterventionServiceImpl extends AbstractFormulaireService
89  {
90      public static final String                    LIBELLE_NATURE_CHANTIER = "libelleNatureChantier";
91  
92      private static IReferentielsService           referentielsService     = SpringContextService.getBean( "referentielsService" );
93  
94      private static final IConflitService          conflitService          = SpringContextService.getBean( "conflitService" );
95  
96      private static final IInfosSuperviseurService infosSuperviseurService = SpringContextService.getBean( "infosSuperviseurService" );
97  
98      protected FormulaireDemandeInterventionServiceImpl( )
99      {
100     }
101 
102     public static final IFormulaire formulaireDemandeInterventionService = new FormulaireDemandeInterventionServiceImpl( );
103 
104     public static IFormulaire getInstance( )
105     {
106         return formulaireDemandeInterventionService;
107     }
108 
109     @Override
110     public void alimentationModel( HttpServletRequest request, Map<String, Object> model )
111     {
112         model.put( "controlForm", request.getParameter( "complete" ) != null );
113         model.put( SESSION_NUM_DEMANDE_ID, getNumDemande( request ) );
114 
115         int numDemande = getNumDemande( request );
116         DemandeReading demande = demandeService.findByNumDemande( numDemande );
117         if ( null != demande )
118         {
119             Integer origineTransfertUserId = demande.getDemandeOrigineTransfertUserId( );
120             if ( ( origineTransfertUserId != null ) && ( origineTransfertUserId > 0 ) )
121             {
122                 AdminUser origineTransfertUser = AdminUserHome.findByPrimaryKey( origineTransfertUserId );
123                 demande.setDemandeOrigineTransfertUserPrenom( origineTransfertUser.getFirstName( ) );
124                 demande.setDemandeOrigineTransfertUserNom( origineTransfertUser.getLastName( ) );
125             }
126 
127             boolean isExoForIntervention = checkExonerationIntervention();
128             model.put( "exoIntervention", isExoForIntervention );
129 
130             List<Convention> conventions = conventionService.getAllConventionByDemandeId( getNumDemande( request ) );
131             if ( isExoForIntervention && ( null != conventions ) && ( !conventions.isEmpty( ) ) )
132             {
133                 model.put( COMMENTAIRE_USAGER, conventions.get( 0 ).getConventionCommentaireUsager( ) );
134             }
135             if (demande.getExonerationTaxe( ) == null) {
136                 demande.setExonerationTaxeConvention( );
137             }
138 
139             model.put( DEMANDE, demande );
140             model.put( LIBELLE_NATURE_CHANTIER, chantierService.getNatureTravauxLibelleByNatureChantier( demande.getChantierNature( ) ) );
141         }
142 
143         String numProjet = demandeService.getNumProjetByNumDemande( numDemande );
144         fillInfosFilDArianeProjet( model, numProjet );
145 
146         alimentationRecapitulatif( request, model );
147         model.put( NATURES_CHANTIER, referentielsService.getNatureTravauxIntentionIntervention( ) );
148     }
149 
150     private void alimentationRecapitulatif( HttpServletRequest request, Map<String, Object> model )
151     {
152 
153         HashMap<String, List<String>> requiredFielsInError = new HashMap<String, List<String>>( );
154         Map<String, Map<String, String>> fieldsInError = new HashMap<String, Map<String, String>>( );
155         Map<String, Map<String, String>> emprisesInError = new HashMap<>( );
156 
157         int id = getNumDemande( request );
158 
159         EntiteDemande entiteDemaneur = entiteDemandeService.getDemandeurByNumDemande( id );
160         model.put( ETAPE_DEMANDEUR, entiteDemaneur );
161         Utils.controlRequiredFieldsIntDemandeur( ETAPE_DEMANDEUR, entiteDemaneur != null ? entiteDemaneur : new EntiteDemande( ), requiredFielsInError );
162         Utils.checkFields( ETAPE_DEMANDEUR, entiteDemaneur, fieldsInError, IEntiteDemandeValidationGroup.class, null );
163 
164         ChantierIntervention chantierIntervention = chantierService.getChantierInterventionByNumDemande( getNumDemande( request ) );
165         model.put( ETAPE_CHANTIER, chantierIntervention );
166         Utils.controlRequiredFields( ETAPE_CHANTIER, chantierIntervention != null ? chantierIntervention : new ChantierIntervention( ), requiredFielsInError );
167 
168         List<Emprise> emprises = empriseService.findEmprisesByDemandeId( getNumDemande( request ) );
169         HashMap<String, List<String>> requiredFielsEmprisesError = new HashMap<String, List<String>>( );
170 
171         HashMap<String, List<String>> requiredFielsEmprisesInError = new HashMap<String, List<String>>( );
172         for ( Emprise emprise : emprises )
173         {
174 
175             Integer numEmprise = emprise.getEmpriseNumero( );
176             int empriseId = emprise.getEmpriseId( );
177 
178             DetailsEmprise detailsEmprise = empriseService.findDetailsEmpriseById( empriseId );
179             emprisesInError.putAll( putMessagesErrorsForEmprise( emprise, detailsEmprise, emprisesInError, getNumDemande( request ) ) );
180             emprise.setDetails( detailsEmprise );
181             Utils.controlRequiredFields( "emprise_" + numEmprise, emprise, requiredFielsEmprisesInError );
182             if ( ( request.getParameter( "emprise_id" ) != null ) && ( emprise.getEmpriseId( ) == Integer.parseInt( request.getParameter( "emprise_id" ) ) ) )
183             {
184                 Utils.controlRequiredFields( ETAPE_EMPRISE, emprise, requiredFielsEmprisesError );
185                 if ( requiredFielsEmprisesError.get( ETAPE_EMPRISE ) == null )
186                 {
187                     requiredFielsEmprisesError.put( ETAPE_EMPRISE, new ArrayList<String>( ) );
188                 }
189                 if ( emprise.getEmpriseDateFinTravauxVoirie( ) == null )
190                 {
191                     requiredFielsEmprisesError.get( ETAPE_EMPRISE ).add( "empriseDateFinTravauxVoirie" );
192                 }
193                 if ( emprise.getEmpriseDateFinTravauxReseau( ) == null )
194                 {
195                     requiredFielsEmprisesError.get( ETAPE_EMPRISE ).add( "empriseDateFinTravauxReseau" );
196                 }
197                 requiredFielsInError.putAll( requiredFielsEmprisesError );
198             }
199 
200         }
201 
202         Utils.insertRequiredErrors( emprisesInError, requiredFielsEmprisesInError );
203 
204         if ( ( emprises == null ) || emprises.isEmpty( ) )
205         {
206             requiredFielsInError.put( "emprises", Arrays.asList( "emprise" ) );
207         }
208 
209         model.put( "emprises", emprises );
210 
211         putUploadedFiles( request, model, emprises );
212         model.put( LISTE_EMPRISES_RECAP, emprises );
213         model.put( REQUIRED_FIELS_IN_ERROR, requiredFielsInError );
214         model.put( FIELDS_IN_ERROR, fieldsInError );
215         model.put( EMPRISES_IN_ERROR, emprisesInError );
216 
217         EntiteDemande entiteExecutantDemande = entiteDemandeService.getExecutantByNumDemande( getNumDemande( request ) );
218         model.put( ETAPE_EXECUTANT_TRAVAUX, entiteExecutantDemande );
219         Utils.controlRequiredFieldsIntExecutant( ETAPE_EXECUTANT_TRAVAUX, entiteExecutantDemande != null ? entiteExecutantDemande : new EntiteDemande( ), requiredFielsInError );
220         Utils.checkFields( ETAPE_EXECUTANT_TRAVAUX, entiteExecutantDemande, fieldsInError, IEntiteDemandeValidationGroup.class, null );
221 
222         // Etape conflits
223         int numDemande = ( int ) model.get( SESSION_NUM_DEMANDE_ID );
224         Conflit conflit = conflitService.findConflitByNumDemande( numDemande );
225         model.put( "recap_conflit", conflit != null );
226         InfosSuperviseur infosSuperviseur = infosSuperviseurService.findByNumDemande( numDemande );
227         model.put( ETAPE_SUPERVISEUR, infosSuperviseur );
228 
229         controlRequiredFiles( request, requiredFielsInError );
230         model.put( REQUIRED_FIELS_IN_ERROR, requiredFielsInError );
231         model.put( FIELDS_IN_ERROR, fieldsInError );
232 
233     }
234 
235     private void putUploadedFiles( HttpServletRequest request, Map<String, Object> model, List<Emprise> emprises )
236     {
237 
238         putUploadedFilesForDocuments( request, model, ETAT_DES_LIEUX, PLAN_EXPLOITATION, CROQUIS, PLAN_SITUATION, NOTE_CALCUL, AUTRES_DOCUMENTS, FORMULAIRE_MULTI_CASES, ROC_PV, ROC_AUTRES );
239 
240         getempriseDocumentFormulairesMultiCases( request, model, emprises );
241         getempriseDocumentAutorisations( request, model, emprises );
242     }
243 
244     private void controlRequiredFiles( HttpServletRequest request, HashMap<String, List<String>> requiredFielsInError )
245     {
246         ArrayList<String> filesInError = new ArrayList<String>( );
247 
248         List<FileItem> etatDesLieux = ctvDemandeUploadHandler.getListUploadedFiles( ETAT_DES_LIEUX, request.getSession( ) );
249         if ( ( etatDesLieux == null ) || etatDesLieux.isEmpty( ) )
250         {
251             filesInError.add( ETAT_DES_LIEUX );
252         }
253 
254         if ( !filesInError.isEmpty( ) )
255         {
256             requiredFielsInError.put( "documents", filesInError );
257         }
258 
259         List<FileItem> justificatifsConvention = ctvDemandeUploadHandler.getListUploadedFiles( JUSTIFICATIFS_CONVENTION, request.getSession( ) );
260         int numDemande = getNumDemande( request );
261         DemandeReading demande = demandeService.findByNumDemande( numDemande );
262         if ( ( demande.isDemandeConvention( ) == true ) && ( ( justificatifsConvention == null ) || ( justificatifsConvention.isEmpty( ) ) ) )
263         {
264             filesInError.add( JUSTIFICATIFS_CONVENTION );
265             if ( requiredFielsInError.get( ETAPE_DEMANDEUR ) != null ) {
266                 requiredFielsInError.get( ETAPE_DEMANDEUR ).add( JUSTIFICATIFS_CONVENTION );
267             } else {
268                 requiredFielsInError.put( ETAPE_DEMANDEUR, Arrays.asList( JUSTIFICATIFS_CONVENTION ) );
269             }
270         }
271     }
272 
273     private boolean checkExonerationIntervention()
274     {
275         String paramExoIntervention = (DatastoreService.getDataValue( "parametre.numeric.flux.savoi2.acces.bloc.exoneration.intervention", "Non" )).trim( ).toUpperCase( );
276         return ("OUI".equals( paramExoIntervention ) || "TRUE".equals( paramExoIntervention ) || "1".equals( paramExoIntervention ));
277     }
278 
279 }