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.FORMULAIRE_MULTI_CASES;
37  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.EMPRISES_IN_ERROR;
38  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_GESTION_CONFLITS;
39  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.FIELDS_IN_ERROR;
40  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.KEY_ETAPE;
41  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.NOM_FICHIER;
42  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.REQUIRED_FIELS_IN_ERROR;
43  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.SESSION_NUM_DEMANDE_ID;
44  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TYPE_FICHIER;
45  
46  import java.util.ArrayList;
47  import java.util.Date;
48  import java.util.HashMap;
49  import java.util.List;
50  import java.util.Map;
51  
52  import javax.servlet.http.HttpServletRequest;
53  
54  import org.apache.commons.fileupload.FileItem;
55  import org.apache.commons.lang3.StringUtils;
56  
57  import fr.paris.lutece.plugins.ctv.bo.DetailsEmprise;
58  import fr.paris.lutece.plugins.ctv.bo.DetailsEmprise.TypeLocalisation;
59  import fr.paris.lutece.plugins.ctv.bo.Emprise;
60  import fr.paris.lutece.plugins.ctv.business.IChantierService;
61  import fr.paris.lutece.plugins.ctv.business.IConventionService;
62  import fr.paris.lutece.plugins.ctv.business.IDemandeService;
63  import fr.paris.lutece.plugins.ctv.business.IEmpriseService;
64  import fr.paris.lutece.plugins.ctv.business.IEntiteDemandeService;
65  import fr.paris.lutece.plugins.ctv.constant.TypeDemande;
66  import fr.paris.lutece.plugins.ctv.constants.CtvConstants;
67  import fr.paris.lutece.plugins.ctv.handler.AbstractCtvUploadHandler;
68  import fr.paris.lutece.plugins.ctv.handler.CtvUploadConferenceCoordinationDocumentHandler;
69  import fr.paris.lutece.plugins.ctv.handler.CtvUploadDemandeDocumentHandler;
70  import fr.paris.lutece.plugins.ctv.handler.CtvUploadEmpriseDocumentHandler;
71  import fr.paris.lutece.plugins.ctv.view.object.CountEmpriseViewObject;
72  import fr.paris.lutece.plugins.ctv.view.object.DemandeReading;
73  import fr.paris.lutece.plugins.sitelabels.service.LabelService;
74  import fr.paris.lutece.portal.service.spring.SpringContextService;
75  
76  public abstract class AbstractFormulaireService implements IFormulaire
77  {
78  
79      private static final String ERREUR_DATES_OBLIGATOIRES = "sitelabels.site_property.emprises.erreur.dates.obligatoires";
80      private static final String ERREUR_CHRONOLOGIE_INCOHERENTE = "sitelabels.site_property.emprises.erreur.chronologie.incoherente";
81      private static final String ERREUR_LOCALISATION_OBLIGATOIRE = "sitelabels.site_property.emprises.erreur.localisation.obligatoire";
82  
83      protected static final IDemandeService demandeService = SpringContextService.getBean( "demandeService" );
84  
85      protected static final IChantierService chantierService = SpringContextService.getBean( "chantierService" );
86  
87      protected static final IEntiteDemandeService entiteDemandeService = SpringContextService.getBean( "entiteDemandeService" );
88  
89      protected static final IEmpriseService empriseService = SpringContextService.getBean( "empriseService" );
90  
91      protected static final IConventionService conventionService = SpringContextService.getBean( "conventionService" );
92  
93      protected static final CtvUploadDemandeDocumentHandler ctvDemandeUploadHandler = SpringContextService
94              .getBean( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER );
95  
96      protected static final CtvUploadEmpriseDocumentHandler ctvEmpriseUploadHandler = SpringContextService
97              .getBean( CtvUploadEmpriseDocumentHandler.CTV_EMPRISE_UPLOAD_HANDLER );
98  
99      private static final CtvUploadConferenceCoordinationDocumentHandler ctvConferenceCoordinationUploadHandler = SpringContextService
100             .getBean( CtvUploadConferenceCoordinationDocumentHandler.CTV_CONFERENCE_COORDINATION_UPLOAD_HANDLER );
101 
102     @Override
103     public int getNumDemande( HttpServletRequest request )
104     {
105         if ( request.getParameter( "num_demande_projet" ) != null )
106         {
107             request.getSession( ).setAttribute( SESSION_NUM_DEMANDE_ID, request.getParameter( "num_demande_projet" ) );
108             return Integer.parseInt( String.valueOf( request.getSession( ).getAttribute( SESSION_NUM_DEMANDE_ID ) ) );
109         }
110         if ( null == request.getSession( ).getAttribute( SESSION_NUM_DEMANDE_ID ) )
111         {
112             return 0;
113         }
114         return Integer.parseInt( String.valueOf( request.getSession( ).getAttribute( SESSION_NUM_DEMANDE_ID ) ) );
115     }
116 
117     @Override
118     public void sauvegardeEtape( HttpServletRequest request, Map<String, Object> model, String etape )
119     {
120         model.put( KEY_ETAPE, etape );
121     }
122 
123     @Override
124     public void removeFilesFromSession( HttpServletRequest request )
125     {
126         ctvDemandeUploadHandler.removeFilesInSession( request );
127         ctvEmpriseUploadHandler.removeFilesInSession( request );
128     }
129 
130     @Override
131     public FileItem getFileItemFromRequest( HttpServletRequest request )
132     {
133 
134         FileItem file = getFileItemByHandler( ctvDemandeUploadHandler, request );
135 
136         if ( file == null )
137         {
138             file = getFileItemByHandler( ctvEmpriseUploadHandler, request );
139         }
140         else
141         {
142             return file;
143         }
144         if ( file == null )
145         {
146             return getFileItemByHandler( ctvConferenceCoordinationUploadHandler, request );
147         }
148 
149         return file;
150     }
151 
152     private FileItem getFileItemByHandler( AbstractCtvUploadHandler handler, HttpServletRequest request )
153     {
154         String typeFichierParam = request.getParameter( TYPE_FICHIER );
155         String nomFichierParam = request.getParameter( NOM_FICHIER );
156         String id = request.getParameter( "id" );
157         if ( StringUtils.isNotBlank( id ) )
158         {
159             handler.removeFilesInSession( request );
160             request.getSession( ).setAttribute( handler.getFileIdNameInSession( ), id );
161         }
162 
163         return handler.getFileItemByName( request, typeFichierParam, nomFichierParam );
164     }
165 
166     @Override
167     public boolean hasErrors( Map<String, Object> model )
168     {
169         Map<String, List<String>> requiredFielsInError = (Map<String, List<String>>) model.get( REQUIRED_FIELS_IN_ERROR );
170         Map<String, Map<String, String>> fieldsInError = (Map<String, Map<String, String>>) model.get( FIELDS_IN_ERROR );
171         Map<String, Map<String, String>> emprisesInError = (Map<String, Map<String, String>>) model.get( EMPRISES_IN_ERROR );
172 
173         // VĂ©rification des erreurs
174         if ( !requiredFielsInError.isEmpty( ) || !emprisesInError.isEmpty( ) )
175         {
176             return true;
177         }
178         // L'erreur de la gestion des conflits, ne doit pas empĂȘcher la
179         // validation
180         return ( ( fieldsInError.size( ) > 1 ) || ( ( fieldsInError.size( ) == 1 ) && ( fieldsInError.get( ETAPE_GESTION_CONFLITS ) == null ) ) );
181     }
182 
183     @Override
184     public Map<String, Map<String, String>> putMessagesErrorsForEmprise( Emprise emprise, DetailsEmprise detailsEmprise,
185             Map<String, Map<String, String>> emprisesInError, int numDemande )
186     {
187 
188         Map<String, String> errorsList = new HashMap<>( );
189 
190         boolean checkNullDate = ( emprise.getEmpriseDateDebut( ) != null ) && ( emprise.getEmpriseDateFinTravauxReseau( ) != null )
191                 && ( emprise.getEmpriseDateFinTravauxVoirie( ) != null ) && ( emprise.getEmpriseDateFin( ) != null );
192 
193         boolean checkNullDateIntentionOccupation = ( emprise.getEmpriseDateDebut( ) != null ) && ( emprise.getEmpriseDateFin( ) != null );
194 
195         Date dateReseau = emprise.getEmpriseDateFinTravauxReseau( );
196         Date dateVoirie = emprise.getEmpriseDateFinTravauxVoirie( );
197         Date dateDebut = emprise.getEmpriseDateDebut( );
198         Date dateFin = emprise.getEmpriseDateFin( );
199 
200         DemandeReading demande = demandeService.findByNumDemande( numDemande );
201         boolean isIntervention = TypeDemande.INTERVENTION.equals( demande.getDemandeType( ) );
202         boolean isOccupation = TypeDemande.OCCUPATION.equals( demande.getDemandeType( ) );
203 
204         if ( ( !checkNullDate && isIntervention ) || ( !checkNullDateIntentionOccupation && !isIntervention ) )
205         {
206             String erreur = LabelService.findByPrimaryKey( ERREUR_DATES_OBLIGATOIRES ).getValue( );
207             errorsList.put( "empriseDateDebut", erreur );
208         }
209         else
210         {
211             if ( ( isIntervention && datesInterventionIncoherentes( dateReseau, dateVoirie, dateDebut, dateFin ) )
212                     || ( !isIntervention && datesIntentionOccupationIncoherentes( dateDebut, dateFin ) ) )
213             {
214                 String erreur = LabelService.findByPrimaryKey( ERREUR_CHRONOLOGIE_INCOHERENTE ).getValue( );
215                 errorsList.put( "empriseDateDebut", erreur );
216             }
217         }
218         boolean isLocalisationOk = false;
219         boolean isChauseeOK = false;
220         boolean isTrottoirOK = false;
221         Map<TypeLocalisation, Double> mapLocalisation = detailsEmprise.getLocalisationsEmprise( );
222 
223         if ( mapLocalisation.containsKey( TypeLocalisation.EMPRISE_TROTTOIR ) || mapLocalisation.containsKey( TypeLocalisation.EMPRISE_CHAUSSEE ) )
224         {
225             isLocalisationOk = true;
226         }
227 
228         if ( !isLocalisationOk && !isOccupation )
229         {
230             String erreur = LabelService.findByPrimaryKey( ERREUR_LOCALISATION_OBLIGATOIRE ).getValue( );
231             errorsList.put( "localisation", erreur );
232         }
233 
234         for ( Map.Entry<TypeLocalisation, Double> entry : mapLocalisation.entrySet( ) )
235         {
236             if ( entry.getKey( ).equals( TypeLocalisation.EMPRISE_CHAUSSEE ) && ( entry.getValue( ) != 0.0 ) )
237             {
238                 isChauseeOK = true;
239             }
240             if ( entry.getKey( ).equals( TypeLocalisation.EMPRISE_TROTTOIR ) && ( entry.getValue( ) != 0.0 ) )
241             {
242                 isTrottoirOK = true;
243             }
244         }
245 
246         if ( mapLocalisation.containsKey( TypeLocalisation.EMPRISE_TROTTOIR ) )
247         {
248             isLocalisationOk = isLocalisationOk && isTrottoirOK;
249         }
250 
251         if ( mapLocalisation.containsKey( TypeLocalisation.EMPRISE_CHAUSSEE ) )
252         {
253             isLocalisationOk = isLocalisationOk && isChauseeOK;
254         }
255         if ( mapLocalisation.containsKey( TypeLocalisation.EMPRISE_CHAUSSEE ) && mapLocalisation.containsKey( TypeLocalisation.EMPRISE_TROTTOIR ) )
256         {
257             isLocalisationOk = isLocalisationOk && isChauseeOK && isTrottoirOK;
258         }
259 
260         if ( !isLocalisationOk && isOccupation )
261         {
262             String erreur = LabelService.findByPrimaryKey( ERREUR_LOCALISATION_OBLIGATOIRE ).getValue( );
263             errorsList.put( "localisation", erreur );
264         }
265 
266         if ( !errorsList.isEmpty( ) )
267         {
268             Integer numEmprise = emprise.getEmpriseNumero( );
269             emprisesInError.put( numEmprise.toString( ), errorsList );
270         }
271         return emprisesInError;
272     }
273 
274     private boolean datesIntentionOccupationIncoherentes( Date dateDebut, Date dateFin )
275     {
276         return !( dateDebut.before( dateFin ) || dateDebut.equals( dateFin ) );
277     }
278 
279     private boolean datesInterventionIncoherentes( Date dateReseau, Date dateVoirie, Date dateDebut, Date dateFin )
280     {
281         return !( ( ( dateDebut.before( dateReseau ) ) || ( dateDebut.equals( dateReseau ) ) )
282                 && ( ( dateReseau.before( dateVoirie ) ) || ( dateReseau.equals( dateVoirie ) ) )
283                 && ( ( dateVoirie.before( dateFin ) ) || ( dateVoirie.equals( dateFin ) ) ) );
284     }
285 
286     protected DemandeReading fillInfosFilDArianeProjet( Map<String, Object> model, String numProjet )
287     {
288         DemandeReading demandeOrigine = demandeService.findOrigineDemandeByNumProjet( numProjet );
289         CountEmpriseViewObject empriseCountViewObject = empriseService.countEmpriseTransform( demandeOrigine.getDemandeNumero( ) );
290         model.put( "empriseCount", empriseCountViewObject );
291         model.put( "demandeOrigine", demandeOrigine );
292         return demandeOrigine;
293     }
294 
295     protected void getempriseDocumentFormulairesMultiCases( HttpServletRequest request, Map<String, Object> model, List<Emprise> emprises )
296     {
297         Map<String, List<FileItem>> empriseDocumentFormulairesMultiCases = new HashMap<>( );
298         Object save = request.getSession( ).getAttribute( CtvConstants.EMPRISE_ID );
299         for ( Emprise emprise : emprises )
300         {
301             request.getSession( ).setAttribute( CtvConstants.EMPRISE_ID, emprise.getEmpriseId( ) );
302             empriseDocumentFormulairesMultiCases.put( Integer.toString( emprise.getEmpriseId( ) ),
303                     ctvEmpriseUploadHandler.getListUploadedFiles( FORMULAIRE_MULTI_CASES, request.getSession( ) ) );
304             ctvEmpriseUploadHandler.removeFilesInSession( request );
305             request.getSession( ).removeAttribute( CtvConstants.EMPRISE_ID );
306         }
307         request.getSession( ).setAttribute( CtvConstants.EMPRISE_ID, save );
308         model.put( FORMULAIRE_MULTI_CASES + "_listUploadedFiles", empriseDocumentFormulairesMultiCases );
309     }
310 
311     protected void getempriseDocumentAutorisations( HttpServletRequest request, Map<String, Object> model, List<Emprise> emprises )
312     {
313         List<FileItem> fileList = new ArrayList<>( );
314         Object save = request.getSession( ).getAttribute( CtvConstants.EMPRISE_ID );
315         for ( Emprise emprise : emprises )
316         {
317             request.getSession( ).setAttribute( CtvConstants.EMPRISE_ID, emprise.getEmpriseId( ) );
318             fileList.addAll( ctvEmpriseUploadHandler.getListUploadedFiles( "autorisations", request.getSession( ) ) );
319             ctvEmpriseUploadHandler.removeFilesInSession( request );
320             request.getSession( ).removeAttribute( CtvConstants.EMPRISE_ID );
321         }
322         request.getSession( ).setAttribute( CtvConstants.EMPRISE_ID, save );
323         model.put( "autorisations" + "_listUploadedFiles", fileList );
324     }
325 
326     protected void putUploadedFilesForDocuments( HttpServletRequest request, Map<String, Object> model, String... fieldsName )
327     {
328         for ( String fieldName : fieldsName )
329         {
330             model.put( fieldName + "_listUploadedFiles", ctvDemandeUploadHandler.getListUploadedFiles( fieldName, request.getSession( ) ) );
331         }
332     }
333 
334 }