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.constants.CtvConstants.DEMANDE;
46  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.EMPRISES_IN_ERROR;
47  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_CHANTIER;
48  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_DEMANDEUR;
49  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_EMPRISE;
50  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.FIELDS_IN_ERROR;
51  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.LISTE_EMPRISES_RECAP;
52  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.NATURES_CHANTIER;
53  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.NEW_EMPRISE;
54  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.REQUIRED_FIELS_IN_ERROR;
55  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.SESSION_NUM_DEMANDE_ID;
56  
57  import java.util.ArrayList;
58  import java.util.Arrays;
59  import java.util.Collections;
60  import java.util.Comparator;
61  import java.util.HashMap;
62  import java.util.List;
63  import java.util.Map;
64  
65  import javax.servlet.ServletRequest;
66  import javax.servlet.http.HttpServletRequest;
67  
68  import org.apache.commons.fileupload.FileItem;
69  
70  import fr.paris.lutece.plugins.ctv.bo.ChantierIntention;
71  import fr.paris.lutece.plugins.ctv.bo.DetailsEmprise;
72  import fr.paris.lutece.plugins.ctv.bo.Emprise;
73  import fr.paris.lutece.plugins.ctv.bo.Emprise.TypeEmprise;
74  import fr.paris.lutece.plugins.ctv.bo.EntiteDemande;
75  import fr.paris.lutece.plugins.ctv.bo.EntiteDemande.Civilite;
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.plugins.unittree.business.unit.Unit;
83  import fr.paris.lutece.plugins.unittree.business.unit.Unit.TypeUnit;
84  import fr.paris.lutece.plugins.unittree.service.unit.IUnitService;
85  import fr.paris.lutece.portal.business.user.AdminUser;
86  import fr.paris.lutece.portal.business.user.AdminUserHome;
87  import fr.paris.lutece.portal.business.user.attribute.AdminUserField;
88  import fr.paris.lutece.portal.business.user.attribute.IAttribute;
89  import fr.paris.lutece.portal.service.admin.AdminUserService;
90  import fr.paris.lutece.portal.service.i18n.I18nService;
91  import fr.paris.lutece.portal.service.spring.SpringContextService;
92  import fr.paris.lutece.portal.service.user.attribute.AdminUserFieldService;
93  import fr.paris.lutece.portal.service.user.attribute.AttributeService;
94  
95  public class FormulaireDemandeIntentionServiceImpl extends AbstractFormulaireService
96  {
97      public static final String LIBELLE_NATURE_CHANTIER = "libelleNatureChantier";
98  
99      private static final AttributeService attributeService = AttributeService.getInstance( );
100 
101     private IUnitService unitService = SpringContextService.getBean( IUnitService.BEAN_UNIT_SERVICE );
102 
103     private static IReferentielsService referentielsService = SpringContextService.getBean( "referentielsService" );
104 
105     protected FormulaireDemandeIntentionServiceImpl( )
106     {
107     }
108 
109     public static final IFormulaire formulaireDemandeIntentionService = new FormulaireDemandeIntentionServiceImpl( );
110 
111     private CtvUploadEmpriseDocumentHandler ctvUploadEmpriseDocumentHandler = SpringContextService
112             .getBean( CtvUploadEmpriseDocumentHandler.CTV_EMPRISE_UPLOAD_HANDLER );
113 
114     public static IFormulaire getInstance( )
115     {
116         return formulaireDemandeIntentionService;
117     }
118 
119     @Override
120     public void alimentationModel( HttpServletRequest request, Map<String, Object> model )
121     {
122         model.put( "controlForm", request.getParameter( "complete" ) != null );
123         model.put( SESSION_NUM_DEMANDE_ID, getNumDemande( request ) );
124         int numDemande = getNumDemande( request );
125         DemandeReading demande = demandeService.findByNumDemande( numDemande );
126         if ( null != demande )
127         {
128             int origineTransfertUserId = demande.getDemandeOrigineTransfertUserId( );
129             if ( origineTransfertUserId > 0 )
130             {
131                 AdminUser origineTransfertUser = AdminUserHome.findByPrimaryKey( origineTransfertUserId );
132                 demande.setDemandeOrigineTransfertUserPrenom( origineTransfertUser.getFirstName( ) );
133                 demande.setDemandeOrigineTransfertUserNom( origineTransfertUser.getLastName( ) );
134             }
135 
136             model.put( DEMANDE, demande );
137             model.put( LIBELLE_NATURE_CHANTIER, chantierService.getNatureTravauxLibelleByNatureChantier( demande.getChantierNature( ) ) );
138         }
139 
140         String numProjet = demandeService.getNumProjetByNumDemande( numDemande );
141         fillInfosFilDArianeProjet( model, numProjet );
142 
143         alimentationRecapitulatif( request, model );
144         mapConcessionnaire( request, model );
145         model.put( NATURES_CHANTIER, referentielsService.getNatureTravauxIntentionIntervention( ) );
146     }
147 
148     private void mapConcessionnaire( HttpServletRequest request, Map<String, Object> model )
149     {
150         AdminUser user = AdminUserService.getAdminUser( request );
151         EntiteDemande demandeur = (EntiteDemande) model.get( ETAPE_DEMANDEUR );
152         Unit concessionnaire = unitService.getUnitUserByType( user, TypeUnit.CONCESSIONNAIRE );
153         if ( ( concessionnaire != null ) && ( demandeur == null ) )
154         {
155             demandeur = new EntiteDemande( );
156             demandeur.setRaisonSociale( concessionnaire.getLabel( ) );
157             demandeur.setSirenSiret( concessionnaire.getSirensiret( ) );
158             demandeur.setReprensentantTitre( getUserCivilite( request, user ) );
159             demandeur.setReprensentantNom( user.getLastName( ) );
160             demandeur.setReprensentantPrenom( user.getFirstName( ) );
161             demandeur.setAdresseCodePostal( concessionnaire.getCodepostal( ) );
162             demandeur.setAdresseNumero( concessionnaire.getNumero( ) );
163             demandeur.setAdresseVille( concessionnaire.getVille( ) );
164             demandeur.setAdresseVoie( concessionnaire.getVoie( ) );
165             demandeur.setAdressePays( concessionnaire.getPays( ) );
166             demandeur.setContactTelFixe( getUserAttributeValueByKey( "ctv.attribute.telephone.fixe.title", user, request ) );
167             demandeur.setContactTelPortable( getUserAttributeValueByKey( "ctv.attribute.telephone.portable.title", user, request ) );
168             demandeur.setContactCourriel( user.getEmail( ) );
169             model.put( ETAPE_DEMANDEUR, demandeur );
170         }
171     }
172 
173     private Civilite getUserCivilite( HttpServletRequest request, AdminUser user )
174     {
175         String civiliteValue = getUserAttributeValueByKey( "ctv.attribute.civilite.title", user, request );
176         return Civilite.getEnum( civiliteValue );
177     }
178 
179     @SuppressWarnings( "unchecked" )
180     private String getUserAttributeValueByKey( String key, AdminUser user, ServletRequest request )
181     {
182         Map<String, Object> fields = AdminUserFieldService.getAdminUserFields( user.getUserId( ), request.getLocale( ) );
183         Integer idAttibute = getAttributeIdByTitleKey( key, request );
184         List<AdminUserField> adminUserFields = (List<AdminUserField>) fields.get( idAttibute.toString( ) );
185         AdminUserField adminUserField = Collections.max( adminUserFields, new Comparator<AdminUserField>( )
186         {
187             @Override
188             public int compare( AdminUserField auf1, AdminUserField auf2 )
189             {
190                 if ( auf2.getIdUserField( ) > auf1.getIdUserField( ) )
191                 {
192                     return -1;
193                 }
194                 if ( auf2.getIdUserField( ) == auf1.getIdUserField( ) )
195                 {
196                     return 0;
197                 }
198                 return 1;
199             }
200         } );
201 
202         if ( adminUserField != null )
203         {
204             return adminUserField.getValue( );
205         }
206         return null;
207     }
208 
209     private int getAttributeIdByTitleKey( String key, ServletRequest request )
210     {
211         List<IAttribute> attributes = attributeService.getAllAttributesWithoutFields( request.getLocale( ) );
212         for ( IAttribute attribute : attributes )
213         {
214             String title = I18nService.getLocalizedString( key, request.getLocale( ) );
215             if ( title.equals( attribute.getTitle( ) ) )
216             {
217                 return attribute.getIdAttribute( );
218             }
219         }
220         return 0;
221     }
222 
223     private void alimentationRecapitulatif( HttpServletRequest request, Map<String, Object> model )
224     {
225 
226         HashMap<String, List<String>> requiredFielsInError = new HashMap<String, List<String>>( );
227         Map<String, Map<String, String>> fieldsInError = new HashMap<String, Map<String, String>>( );
228         Map<String, Map<String, String>> emprisesInError = new HashMap<>( );
229         int id = getNumDemande( request );
230 
231         EntiteDemande entiteDemaneur = entiteDemandeService.getDemandeurByNumDemande( id );
232         model.put( ETAPE_DEMANDEUR, entiteDemaneur );
233         Utils.controlRequiredFieldsIntDemandeur( ETAPE_DEMANDEUR, entiteDemaneur != null ? entiteDemaneur : new EntiteDemande( ), requiredFielsInError );
234         Utils.checkFields( ETAPE_DEMANDEUR, entiteDemaneur, fieldsInError, IEntiteDemandeValidationGroup.class, null );
235 
236         ChantierIntention chantierIntention = chantierService.getChantierIntentionByNumDemande( getNumDemande( request ) );
237         model.put( ETAPE_CHANTIER, chantierIntention );
238         Utils.controlRequiredFields( ETAPE_CHANTIER, chantierIntention != null ? chantierIntention : new ChantierIntention( ), requiredFielsInError );
239 
240         List<Emprise> emprises = empriseService.findEmprisesByDemandeId( getNumDemande( request ) );
241         HashMap<String, List<String>> requiredFielsEmprisesInError = new HashMap<String, List<String>>( );
242         HashMap<String, List<String>> requiredFielsEmprisesError = new HashMap<String, List<String>>( );
243 
244         // Check detail emprises only if not creation
245         if ( request.getSession( ).getAttribute( NEW_EMPRISE ) == null )
246         {
247             for ( Emprise emprise : emprises )
248             {
249                 Integer numEmprise = emprise.getEmpriseNumero( );
250 
251                 if ( emprise.getEmpriseType( ).equals( TypeEmprise.ECHAFAUDAGE ) )
252                 {
253                     int empriseId = emprise.getEmpriseId( );
254                     DetailsEmprise detailsEmprise = empriseService.findDetailsEmpriseById( empriseId );
255                     if ( null != detailsEmprise )
256                     {
257                         List<Object> msgArgs = new ArrayList<>( );
258                         msgArgs.add( numEmprise );
259                         Utils.checkFields( numEmprise.toString( ), detailsEmprise, emprisesInError, IEchafaudageValidationGroup.class, msgArgs );
260                     }
261                 }
262 
263                 DetailsEmprise detailsEmprise = empriseService.findDetailsEmpriseById( emprise.getEmpriseId( ) );
264                 emprise.setDetails( detailsEmprise );
265                 emprisesInError.putAll( putMessagesErrorsForEmprise( emprise, detailsEmprise, emprisesInError, getNumDemande( request ) ) );
266                 Utils.controlRequiredFields( "emprise_" + numEmprise, emprise, requiredFielsEmprisesInError );
267                 /*
268                  * added control fields intention emprise step
269                  */
270                 if ( ( request.getParameter( "emprise_id" ) != null )
271                         && ( emprise.getEmpriseId( ) == Integer.parseInt( request.getParameter( "emprise_id" ) ) ) )
272                 {
273                     Utils.controlRequiredFields( ETAPE_EMPRISE, emprise, requiredFielsEmprisesError );
274                     requiredFielsInError.putAll( requiredFielsEmprisesError );
275 
276                 }
277 
278             }
279 
280         }
281         else
282         {
283             request.getSession( ).removeAttribute( NEW_EMPRISE );
284         }
285 
286         Utils.insertRequiredErrors( emprisesInError, requiredFielsEmprisesInError );
287 
288         if ( ( emprises == null ) || emprises.isEmpty( ) )
289         {
290             requiredFielsInError.put( "emprises", Arrays.asList( "emprise" ) );
291         }
292 
293         putUploadedFiles( request, model, emprises );
294         model.put( LISTE_EMPRISES_RECAP, emprises );
295         model.put( REQUIRED_FIELS_IN_ERROR, requiredFielsInError );
296         model.put( FIELDS_IN_ERROR, fieldsInError );
297         model.put( EMPRISES_IN_ERROR, emprisesInError );
298 
299         controlRequiredFiles( request, requiredFielsInError );
300     }
301 
302     private void putUploadedFiles( HttpServletRequest request, Map<String, Object> model, List<Emprise> emprises )
303     {
304         putUploadedFilesForDocuments( request, model, JUSTIFICATIFS_URBANISME, ETAT_DES_LIEUX, PLAN_COMPLEMENTAIRE, CROQUIS, PLAN_DETAILS, NOTE_CALCUL,
305                 JUSTIFICATIF_IMPOSSIBILITE_USAGE_LOCAUX, AUTRES_DOCUMENTS );
306         getempriseDocumentFormulairesMultiCases( request, model, emprises );
307         getempriseDocumentAutorisations( request, model, emprises );
308     }
309 
310     @Override
311     public boolean hasErrors( Map<String, Object> model )
312     {
313         return false;
314     }
315 
316     private void controlRequiredFiles( HttpServletRequest request, HashMap<String, List<String>> requiredFielsInError )
317     {
318         ArrayList<String> filesInError = new ArrayList<String>( );
319         List<FileItem> justificatifsConvention = ctvDemandeUploadHandler.getListUploadedFiles(JUSTIFICATIFS_CONVENTION, request.getSession( ) );
320         int numDemande = getNumDemande( request );
321         DemandeReading demande = demandeService.findByNumDemande( numDemande );
322         if ((demande.isDemandeConvention()) && (( justificatifsConvention == null ) || ( justificatifsConvention.isEmpty()) ))  {
323             filesInError.add( JUSTIFICATIFS_CONVENTION );
324             if ( requiredFielsInError.get( ETAPE_DEMANDEUR ) != null ) {
325             	requiredFielsInError.get( ETAPE_DEMANDEUR ).add( JUSTIFICATIFS_CONVENTION );
326             } else {
327             	requiredFielsInError.put( ETAPE_DEMANDEUR, Arrays.asList( JUSTIFICATIFS_CONVENTION ) );
328             }
329         }
330     }
331 
332 }