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.intervention;
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_URBANISME;
40  import static fr.paris.lutece.plugins.ctv.constant.Document.JUSTIFICATIF_IMPOSSIBILITE_USAGE_LOCAUX;
41  import static fr.paris.lutece.plugins.ctv.constant.Document.NOTE_CALCUL;
42  import static fr.paris.lutece.plugins.ctv.constant.Document.PLAN_COMPLEMENTAIRE;
43  import static fr.paris.lutece.plugins.ctv.constant.Document.PLAN_DETAILS;
44  import static fr.paris.lutece.plugins.ctv.constant.Document.PLAN_EXPLOITATION;
45  import static fr.paris.lutece.plugins.ctv.constant.Document.PLAN_SITUATION;
46  import static fr.paris.lutece.plugins.ctv.constant.Document.ROC_AUTRES;
47  import static fr.paris.lutece.plugins.ctv.constant.Document.ROC_PV;
48  import static fr.paris.lutece.plugins.ctv.constant.Document.SAISINE_AUTRE_SERVICE;
49  import static fr.paris.lutece.plugins.ctv.constant.Document.SAISINE_COMMISSARIAT;
50  import static fr.paris.lutece.plugins.ctv.constant.Document.SAISINE_PREFECTURE_POLICE;
51  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ANNULER_INTERVENTION;
52  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.CONFIRMATION_VIEW;
53  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_VALIDATION;
54  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PARAMETER_ADD;
55  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.SUPPRIMER_BROUILLON;
56  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TEMPLATE_CONFIRMATION_INTERVENTION;
57  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TEMPLATE_VALIDATION_INTERVENTION;
58  
59  import java.util.ArrayList;
60  import java.util.HashMap;
61  import java.util.List;
62  import java.util.Map;
63  
64  import javax.servlet.http.HttpServletRequest;
65  
66  import com.mysql.jdbc.StringUtils;
67  
68  import fr.paris.lutece.plugins.ctv.bo.AlerteDemande.TypeAlerteDemande;
69  import fr.paris.lutece.plugins.ctv.bo.ChantierIntervention;
70  import fr.paris.lutece.plugins.ctv.bo.Emprise;
71  import fr.paris.lutece.plugins.ctv.bo.EntiteDemande;
72  import fr.paris.lutece.plugins.ctv.bo.InfosSuperviseur;
73  import fr.paris.lutece.plugins.ctv.bo.controller.demandes.AbstractCtvJspBean;
74  import fr.paris.lutece.plugins.ctv.business.IChantierService;
75  import fr.paris.lutece.plugins.ctv.business.IDemandeService;
76  import fr.paris.lutece.plugins.ctv.business.IEmpriseService;
77  import fr.paris.lutece.plugins.ctv.business.IEntiteDemandeService;
78  import fr.paris.lutece.plugins.ctv.business.IInfosSuperviseurService;
79  import fr.paris.lutece.plugins.ctv.business.ISaisineService;
80  import fr.paris.lutece.plugins.ctv.constant.StatutDemande;
81  import fr.paris.lutece.plugins.ctv.constant.StatutEmprise;
82  import fr.paris.lutece.plugins.ctv.constant.TypeDemande;
83  import fr.paris.lutece.plugins.ctv.constant.TypeProfileCtv;
84  import fr.paris.lutece.plugins.ctv.constants.CtvConstants;
85  import fr.paris.lutece.plugins.ctv.handler.CtvUploadDemandeDocumentHandler;
86  import fr.paris.lutece.plugins.ctv.mailtask.EmailTask;
87  import fr.paris.lutece.plugins.ctv.service.FormulaireDemandeInterventionServiceImpl;
88  import fr.paris.lutece.plugins.ctv.service.IFormulaire;
89  import fr.paris.lutece.plugins.ctv.view.object.DemandeReading;
90  import fr.paris.lutece.plugins.gismap.business.AddressParamHome;
91  import fr.paris.lutece.portal.service.datastore.DatastoreService;
92  import fr.paris.lutece.portal.service.spring.SpringContextService;
93  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
94  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
95  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
96  
97  @Controller( controllerJsp = "validation.jsp", controllerPath = "jsp/admin/plugins/ctv/gestionDemandes/intervention/etapes", right = "TABLEAU_DE_BORD" )
98  public class ValidationJspBean extends AbstractCtvJspBean
99  {
100 
101     private static final long serialVersionUID = 1L;
102     private IFormulaire formulaireService = FormulaireDemandeInterventionServiceImpl.getInstance( );
103     private IDemandeService demandeService = SpringContextService.getBean( "demandeService" );
104     protected CtvUploadDemandeDocumentHandler ctvDemandeUploadHandler = SpringContextService
105             .getBean( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER );
106     private IEmpriseService empriseService = SpringContextService.getBean( "empriseService" );
107     protected IChantierService chantierService = SpringContextService.getBean( "chantierService" );
108     protected IEntiteDemandeService entiteDemandeService = SpringContextService.getBean( "entiteDemandeService" );
109     private ISaisineService saisineService = SpringContextService.getBean( "saisineService" );
110     private EmailTaskgins/ctv/mailtask/EmailTask.html#EmailTask">EmailTask emailTask = new EmailTask( );
111 
112     protected static final IInfosSuperviseurService infosSuperviseurService = SpringContextService.getBean( "infosSuperviseurService" );
113 
114     @View( value = ETAPE_VALIDATION, defaultView = true )
115     public String getValidation( HttpServletRequest request )
116     {
117         Map<String, Object> model = new HashMap<String, Object>( );
118         formulaireService.alimentationModel( request, model );
119         formulaireService.sauvegardeEtape( request, model, ETAPE_VALIDATION );
120         int numDemande = formulaireService.getNumDemande( request );
121         fillUserPossibleActions( request, model, numDemande );
122 
123         InfosSuperviseur oldInfosSuperviseur = infosSuperviseurService.findByNumDemande( numDemande );
124         model.put( "superviseur", oldInfosSuperviseur );
125         model.put( PARAMETER_ADD, AddressParamHome.getAddressParameters( ) );
126         if ( null != request.getParameter( "action" ) )
127         {
128             return redirect( request, getAbsolutePath( request, PATH_TABLEAU_DE_BORD ) );
129         }
130         else
131         {
132             return getPage( "", TEMPLATE_VALIDATION_INTERVENTION, model );
133         }
134 
135     }
136 
137     @Action( ACTION_NEXT )
138     public String nextPage( HttpServletRequest request )
139     {
140         validationDemande( request );
141         return redirectView( request, CONFIRMATION_VIEW );
142     }
143 
144     @Action( SUPPRIMER_BROUILLON )
145     public String supprimerBrouillon( HttpServletRequest request )
146     {
147         int numDemande = formulaireService.getNumDemande( request );
148         List<Emprise> emprisesIntervention = empriseService.findEmprisesByDemandeId( numDemande );
149 
150         if ( !emprisesIntervention.isEmpty( ) )
151         {
152             List<Integer> empriseInterventionId = new ArrayList<>( );
153             for ( Emprise emprise : emprisesIntervention )
154             {
155                 empriseInterventionId.add( emprise.getEmpriseId( ) );
156             }
157 
158             empriseService.updateIntentionTransformeTag( numDemande );
159 
160             for ( int idEmprise : empriseInterventionId )
161             {
162                 empriseService.deleteEmprise( numDemande, idEmprise );
163             }
164 
165         }
166         ChantierIntervention chantier = chantierService.getChantierInterventionByNumDemande( numDemande );
167         EntiteDemande demandeur = entiteDemandeService.getDemandeurByNumDemande( numDemande );
168         EntiteDemande executant = entiteDemandeService.getExecutantByNumDemande( numDemande );
169         if ( chantier != null )
170         {
171             chantierService.deleteChantierInterventionById( chantier.getChantierId( ) );
172         }
173 
174         if ( demandeur != null )
175         {
176             entiteDemandeService.deleteEntiteById( demandeur.getDemandeurId( ) );
177         }
178         if ( executant != null )
179         {
180             entiteDemandeService.deleteEntiteById( executant.getDemandeurId( ) );
181         }
182         demandeService.deleteDemandeById( numDemande );
183         formulaireService.removeFilesFromSession( request );
184         return redirect( request, getAbsolutePath( request, PATH_TABLEAU_DE_BORD ) );
185     }
186 
187     @View( CONFIRMATION_VIEW )
188     public String confirmation( HttpServletRequest request )
189     {
190         String profile = "";
191         if ( request.getSession( ).getAttribute( "profile_default" ) != null )
192         {
193             profile = ( (TypeProfileCtv) request.getSession( ).getAttribute( "profile_default" ) ).getKey( );
194         }
195         Map<String, Object> model = new HashMap<String, Object>( );
196         model.put( "profile", profile );
197 
198         return getPage( "", TEMPLATE_CONFIRMATION_INTERVENTION, model );
199     }
200 
201     private void validationDemande( HttpServletRequest request )
202     {
203         int demandeId = formulaireService.getNumDemande( request );
204         demandeService.updateDemandeStatut( demandeId, StatutDemande.A_TRAITER );
205         DemandeReading demande = demandeService.findByNumDemande( demandeId );
206         boolean premiereSoumission = false;
207         if ( demande.getDemandeDateSoumission( ) == null )
208         {
209         	premiereSoumission = true;
210             demandeService.updateDateSoumission( demandeId );
211 
212         }
213         empriseService.updateStatutAllEmprises( demandeId, StatutEmprise.A_TRAITER, true );
214         insertAlertTypeSuperviseur( demandeId );
215         empriseService.updateTransormationDefinitive( demandeId );
216         String x = request.getParameter( "x" );
217         String y = request.getParameter( "y" );
218         demandeService.updateDemandeSecteur( demandeId, x, y );
219         ctvDemandeUploadHandler.removeFilesInSession( request );
220         InfosSuperviseur oldInfosSuperviseur = infosSuperviseurService.findByNumDemande( demandeId );
221         InfosSuperviseur infos = new InfosSuperviseur( );
222         if ( oldInfosSuperviseur == null )
223         {
224             infos.setInfosSuperviseurPrescriptionParticuliere( DatastoreService
225                     .getDataValue( "sitelabels.site_property.bloc.qualification.prescription.particuliere", "prescriptions particulières par défaut" ) );
226             infosSuperviseurService.update( infos, demandeId );
227         }
228         else
229             if ( oldInfosSuperviseur.getInfosSuperviseurPrescriptionParticuliere( ) == null )
230             {
231                 oldInfosSuperviseur.setInfosSuperviseurPrescriptionParticuliere( DatastoreService
232                         .getDataValue( "sitelabels.site_property.bloc.qualification.prescription.particuliere", "prescriptions particulières par défaut" ) );
233                 infosSuperviseurService.update( oldInfosSuperviseur, demandeId );
234             }
235         // màj empriseAEnvoyerASavoi pour les demandes dont le demandeur siret est non taxable
236         String demandeurSiret = entiteDemandeService.getDemandeurByNumDemande( demandeId ).getSirenSiret( );
237         String numSiretNonTaxables = DatastoreService.getDataValue( "parametre.numeric.flux.savoi2.siret.non.taxable", "" );
238         
239         if ( ( ( demandeurSiret != null ) && !"".equals(demandeurSiret) ) && numSiretNonTaxables.contains( demandeurSiret ) ) {      
240             empriseService.updateEmpriseConvention( demandeId, false );
241         }
242         else if ( premiereSoumission && Boolean.TRUE.equals(demande.isDemandeConvention()) )
243         {
244         	emailTask.sendEmail( CtvConstants.EMAIL_SOUMISSION_CONVENTION, demandeId, request.getLocale( ), request, -1 );
245         }
246     }
247 
248 
249     private void insertAlertTypeSuperviseur( int numDemande )
250     {
251         DemandeReading demande = demandeService.findByNumDemande( numDemande );
252         if ( demande.getDemandeType( ).equals( TypeDemande.INTERVENTION ) )
253         {
254             alerteDemandeService.insertAlerte( TypeAlerteDemande.SUPERVISEUR, numDemande );
255         }
256     }
257 
258     @Action( ANNULER_INTERVENTION )
259     public String annulerIntervention( HttpServletRequest request )
260     {
261         int numDemande = formulaireService.getNumDemande( request );
262         ctvDemandeUploadHandler.removeFilesItem( AUTRES_DOCUMENTS, request.getSession( ) );
263         ctvDemandeUploadHandler.removeFilesItem( JUSTIFICATIF_IMPOSSIBILITE_USAGE_LOCAUX, request.getSession( ) );
264         ctvDemandeUploadHandler.removeFilesItem( NOTE_CALCUL, request.getSession( ) );
265         ctvDemandeUploadHandler.removeFilesItem( PLAN_DETAILS, request.getSession( ) );
266         ctvDemandeUploadHandler.removeFilesItem( CROQUIS, request.getSession( ) );
267         ctvDemandeUploadHandler.removeFilesItem( PLAN_COMPLEMENTAIRE, request.getSession( ) );
268         ctvDemandeUploadHandler.removeFilesItem( ETAT_DES_LIEUX, request.getSession( ) );
269         ctvDemandeUploadHandler.removeFilesItem( JUSTIFICATIFS_URBANISME, request.getSession( ) );
270         ctvDemandeUploadHandler.removeFilesItem( SAISINE_PREFECTURE_POLICE, request.getSession( ) );
271         ctvDemandeUploadHandler.removeFilesItem( SAISINE_COMMISSARIAT, request.getSession( ) );
272         ctvDemandeUploadHandler.removeFilesItem( SAISINE_AUTRE_SERVICE, request.getSession( ) );
273         ctvDemandeUploadHandler.removeFilesItem( ROC_PV, request.getSession( ) );
274         ctvDemandeUploadHandler.removeFilesItem( ROC_AUTRES, request.getSession( ) );
275         ctvDemandeUploadHandler.removeFilesItem( PLAN_EXPLOITATION, request.getSession( ) );
276         ctvDemandeUploadHandler.removeFilesItem( PLAN_SITUATION, request.getSession( ) );
277         saisineService.deleteSaisines( numDemande );
278         infosSuperviseurService.delete( numDemande );
279         empriseService.reverseTransformationDefinitive( numDemande );
280         empriseService.updateIntentionTransformeTag( numDemande );
281         List<Emprise> listEmpriseDemandeIntervention = empriseService.findEmprisesByDemandeId( numDemande );
282         if ( ( listEmpriseDemandeIntervention != null ) && !listEmpriseDemandeIntervention.isEmpty( ) )
283         {
284             for ( Emprise emprise : listEmpriseDemandeIntervention )
285             {
286                 empriseService.deleteDetails( emprise.getEmpriseId( ) );
287                 empriseService.deleteEmprise( numDemande, emprise.getEmpriseId( ) );
288             }
289         }
290 
291         alerteDemandeService.removeAlerteInDemande( numDemande );
292 
293         ChantierIntervention chantier = chantierService.getChantierInterventionByNumDemande( numDemande );
294         EntiteDemande demandeur = entiteDemandeService.getDemandeurByNumDemande( numDemande );
295         EntiteDemande executant = entiteDemandeService.getExecutantByNumDemande( numDemande );
296         if ( chantier != null )
297         {
298             chantierService.deleteChantierInterventionById( chantier.getChantierId( ) );
299         }
300         if ( demandeur != null )
301         {
302             entiteDemandeService.deleteEntiteById( demandeur.getDemandeurId( ) );
303         }
304         if ( executant != null )
305         {
306             entiteDemandeService.deleteEntiteById( executant.getDemandeurId( ) );
307         }
308         formulaireService.removeFilesFromSession( request );
309         demandeService.deleteDemandeById( numDemande );
310         return redirect( request, getAbsolutePath( request, PATH_TABLEAU_DE_BORD ) );
311     }
312 
313     @Action( ACTION_ABANDON )
314     public String abandon( HttpServletRequest request )
315     {
316         formulaireService.removeFilesFromSession( request );
317         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
318         if ( StringUtils.isNullOrEmpty( nextStepPath ) )
319         {
320             return redirect( request, "../demandeIntervention.jsp" );
321         }
322         return redirect( request, nextStepPath );
323     }
324 
325 }