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.JUSTIFICATIFS_CONVENTION;
40  import static fr.paris.lutece.plugins.ctv.constant.Document.JUSTIFICATIFS_URBANISME;
41  import static fr.paris.lutece.plugins.ctv.constant.Document.JUSTIFICATIF_IMPOSSIBILITE_USAGE_LOCAUX;
42  import static fr.paris.lutece.plugins.ctv.constant.Document.NOTE_CALCUL;
43  import static fr.paris.lutece.plugins.ctv.constant.Document.PLAN_COMPLEMENTAIRE;
44  import static fr.paris.lutece.plugins.ctv.constant.Document.PLAN_DETAILS;
45  import static fr.paris.lutece.plugins.ctv.constant.Document.ROC_AUTRES;
46  import static fr.paris.lutece.plugins.ctv.constant.Document.ROC_PV;
47  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.COMMENTAIRE_USAGER;
48  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.DEMANDE;
49  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.EMPRISES_IN_ERROR;
50  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_CHANTIER;
51  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_DEMANDEUR;
52  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_EMPRISE;
53  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_EXECUTANT_TRAVAUX;
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.ChantierOccupation;
71  import fr.paris.lutece.plugins.ctv.bo.Convention;
72  import fr.paris.lutece.plugins.ctv.bo.DetailsEmprise;
73  import fr.paris.lutece.plugins.ctv.bo.Emprise;
74  import fr.paris.lutece.plugins.ctv.bo.Emprise.TypeEmprise;
75  import fr.paris.lutece.plugins.ctv.bo.EntiteDemande;
76  import fr.paris.lutece.plugins.ctv.business.IReferentielsService;
77  import fr.paris.lutece.plugins.ctv.group.IEchafaudageValidationGroup;
78  import fr.paris.lutece.plugins.ctv.group.IEntiteDemandeValidationGroup;
79  import fr.paris.lutece.plugins.ctv.handler.CtvUploadEmpriseDocumentHandler;
80  import fr.paris.lutece.plugins.ctv.util.Utils;
81  import fr.paris.lutece.plugins.ctv.view.object.DemandeReading;
82  import fr.paris.lutece.portal.service.spring.SpringContextService;
83  
84  public class FormulaireDemandeOccupationServiceImpl extends AbstractFormulaireService
85  {
86  
87      public static final String LIBELLE_NATURE_CHANTIER = "libelleNatureChantier";
88  
89      private CtvUploadEmpriseDocumentHandler ctvUploadEmpriseDocumentHandler = SpringContextService
90              .getBean( CtvUploadEmpriseDocumentHandler.CTV_EMPRISE_UPLOAD_HANDLER );
91  
92      protected FormulaireDemandeOccupationServiceImpl( )
93      {
94      }
95  
96      public static final IFormulaire formulaireDemandeOccupationService = new FormulaireDemandeOccupationServiceImpl( );
97  
98      private static IReferentielsService referentielsService = SpringContextService.getBean( "referentielsService" );
99  
100     public static IFormulaire getInstance( )
101     {
102         return formulaireDemandeOccupationService;
103     }
104 
105     @Override
106     public void alimentationModel( HttpServletRequest request, Map<String, Object> model )
107     {
108         model.put( "controlForm", request.getParameter( "complete" ) != null );
109         model.put( SESSION_NUM_DEMANDE_ID, getNumDemande( request ) );
110 
111         int numDemande = getNumDemande( request );
112         DemandeReading demande = demandeService.findByNumDemande( numDemande );
113         if ( null != demande )
114         {
115             model.put( DEMANDE, demande );
116             model.put( LIBELLE_NATURE_CHANTIER, chantierService.getNatureTravauxLibelleByNatureChantier( demande.getChantierNature( ) ) );
117 
118             List<Convention> conventions = conventionService.getAllConventionByDemandeId( getNumDemande( request ) );
119             if ( ( null != conventions ) && ( !conventions.isEmpty( ) ) )
120             {
121                 model.put( COMMENTAIRE_USAGER, conventions.get( 0 ).getConventionCommentaireUsager( ) );
122             }
123         }
124         alimentationRecapitulatif( request, model );
125         model.put( NATURES_CHANTIER, referentielsService.getNatureTravauxOccupation( ) );
126     }
127 
128     private void alimentationRecapitulatif( HttpServletRequest request, Map<String, Object> model )
129     {
130 
131         HashMap<String, List<String>> requiredFielsInError = new HashMap<String, List<String>>( );
132         Map<String, Map<String, String>> fieldsInError = new HashMap<String, Map<String, String>>( );
133         Map<String, Map<String, String>> emprisesInError = new HashMap<>( );
134 
135         EntiteDemande entiteDemandeur = entiteDemandeService.getDemandeurByNumDemande( getNumDemande( request ) );
136 
137         model.put( ETAPE_DEMANDEUR, entiteDemandeur );
138         Utils.controlRequiredFieldsOccupationDemandeur( ETAPE_DEMANDEUR, entiteDemandeur != null ? entiteDemandeur : new EntiteDemande( ), requiredFielsInError );
139         Utils.checkFields( ETAPE_DEMANDEUR, entiteDemandeur, fieldsInError, IEntiteDemandeValidationGroup.class, null );
140 
141         ChantierOccupation chantier = chantierService.getChantierOccupationByNumDemande( getNumDemande( request ) );
142         model.put( ETAPE_CHANTIER, chantier );
143         Utils.controlRequiredFields( ETAPE_CHANTIER, chantier != null ? chantier : new ChantierOccupation( ), requiredFielsInError );
144 
145         EntiteDemande entiteExecutantDemande = entiteDemandeService.getExecutantByNumDemande( getNumDemande( request ) );
146         model.put( ETAPE_EXECUTANT_TRAVAUX, entiteExecutantDemande );
147         Utils.controlRequiredFieldsOccupationExecutant( ETAPE_EXECUTANT_TRAVAUX, entiteExecutantDemande != null ? entiteExecutantDemande : new EntiteDemande( ),
148                 requiredFielsInError );
149         Utils.checkFields( ETAPE_EXECUTANT_TRAVAUX, entiteExecutantDemande, fieldsInError, IEntiteDemandeValidationGroup.class, null );
150 
151         HashMap<String, List<String>> requiredFielsEmprisesError = new HashMap<String, List<String>>( );
152         List<Emprise> emprises = empriseService.findEmprisesByDemandeId( getNumDemande( request ) );
153         HashMap<String, List<String>> requiredFielsEmprisesInError = new HashMap<String, List<String>>( );
154         for ( Emprise emprise : emprises )
155         {
156             Integer numEmprise = emprise.getEmpriseNumero( );
157 
158             if ( emprise.getEmpriseType( ).equals( TypeEmprise.ECHAFAUDAGE ) )
159             {
160                 int empriseId = emprise.getEmpriseId( );
161                 DetailsEmprise detailsEmprise = empriseService.findDetailsEmpriseById( empriseId );
162                 if ( null != detailsEmprise )
163                 {
164                     List<Object> msgArgs = new ArrayList<>( );
165                     msgArgs.add( numEmprise );
166                     Utils.checkFields( numEmprise.toString( ), detailsEmprise, emprisesInError, IEchafaudageValidationGroup.class, msgArgs );
167                 }
168             }
169 
170             DetailsEmprise detailsEmprise = empriseService.findDetailsEmpriseById( emprise.getEmpriseId( ) );
171             emprise.setDetails( detailsEmprise );
172             emprisesInError.putAll( putMessagesErrorsForEmprise( emprise, detailsEmprise, emprisesInError, getNumDemande( request ) ) );
173 
174             Utils.controlRequiredFields( "emprise_" + numEmprise, emprise, requiredFielsEmprisesInError );
175             if ( ( request.getParameter( "emprise_id" ) != null ) && ( emprise.getEmpriseId( ) == Integer.parseInt( request.getParameter( "emprise_id" ) ) ) )
176             {
177                 Utils.controlRequiredFields( ETAPE_EMPRISE, emprise, requiredFielsEmprisesError );
178                 requiredFielsInError.putAll( requiredFielsEmprisesError );
179 
180             }
181         }
182 
183         Utils.insertRequiredErrors( emprisesInError, requiredFielsEmprisesInError );
184         if ( ( emprises == null ) || emprises.isEmpty( ) )
185         {
186             requiredFielsInError.put( "emprises", Arrays.asList( "emprise" ) );
187         }
188 
189         putUploadedFiles( request, model, emprises );
190         controlRequiredFiles( request, requiredFielsInError, chantier );
191         model.put( LISTE_EMPRISES_RECAP, emprises );
192         model.put( REQUIRED_FIELS_IN_ERROR, requiredFielsInError );
193         model.put( FIELDS_IN_ERROR, fieldsInError );
194         model.put( EMPRISES_IN_ERROR, emprisesInError );
195     }
196 
197     private void putUploadedFiles( HttpServletRequest request, Map<String, Object> model, List<Emprise> emprises )
198     {
199         putUploadedFilesForDocuments( request, model, JUSTIFICATIFS_URBANISME, ETAT_DES_LIEUX, PLAN_COMPLEMENTAIRE, CROQUIS, PLAN_DETAILS, NOTE_CALCUL,
200                 JUSTIFICATIF_IMPOSSIBILITE_USAGE_LOCAUX, AUTRES_DOCUMENTS, ROC_PV, ROC_AUTRES );
201 
202         getempriseDocumentFormulairesMultiCases( request, model, emprises );
203         getempriseDocumentAutorisations( request, model, emprises );
204     }
205 
206     private void controlRequiredFiles( HttpServletRequest request, HashMap<String, List<String>> requiredFielsInError, ChantierOccupation chantier )
207     {
208         ArrayList<String> filesInError = new ArrayList<String>( );
209         List<FileItem> etatDesLieux = ctvDemandeUploadHandler.getListUploadedFiles( ETAT_DES_LIEUX, request.getSession( ) );
210         if ( ( etatDesLieux == null ) || etatDesLieux.isEmpty( ) )
211         {
212             filesInError.add( ETAT_DES_LIEUX );
213         }
214 
215         List<FileItem> justificatifsConvention = ctvDemandeUploadHandler.getListUploadedFiles(JUSTIFICATIFS_CONVENTION, request.getSession( ) );
216         int numDemande = getNumDemande( request );
217         DemandeReading demande = demandeService.findByNumDemande( numDemande );
218 
219         if ((demande.isDemandeConvention()) && (( justificatifsConvention == null ) || ( justificatifsConvention.isEmpty()) ))  {
220             filesInError.add( JUSTIFICATIFS_CONVENTION );
221             if ( requiredFielsInError.get( ETAPE_DEMANDEUR ) != null ) {
222                 requiredFielsInError.get( ETAPE_DEMANDEUR ).add( JUSTIFICATIFS_CONVENTION );
223             } else {
224                 requiredFielsInError.put( ETAPE_DEMANDEUR, Arrays.asList( JUSTIFICATIFS_CONVENTION ) );
225             }
226         }
227 
228         List<FileItem> justificatifsUrbanisme = ctvDemandeUploadHandler.getListUploadedFiles( JUSTIFICATIFS_URBANISME, request.getSession( ) );
229         if ( ( ( justificatifsUrbanisme == null ) || justificatifsUrbanisme.isEmpty( ) ) && ( chantier != null )
230                 && ( chantier.getChantierNumUrbanisme( ) != null ) && !"".equals( chantier.getChantierNumUrbanisme( ).trim( ) ) )
231         {
232             filesInError.add( JUSTIFICATIFS_URBANISME );
233             if ( requiredFielsInError.get( ETAPE_CHANTIER ) != null )
234             {
235                 requiredFielsInError.get( ETAPE_CHANTIER ).add( JUSTIFICATIFS_URBANISME );
236             }
237             else
238             {
239                 requiredFielsInError.put( ETAPE_CHANTIER, Arrays.asList( JUSTIFICATIFS_URBANISME ) );
240             }
241         }
242         if ( !filesInError.isEmpty( ) )
243         {
244             requiredFielsInError.put( "documents", filesInError );
245         }
246     }
247 
248 }