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.controller.front;
35  
36  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_NEXT;
37  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_CONFIRMATION;
38  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_VALIDATION;
39  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.MON_ESPACE_DEMANDEUR;
40  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PAGE_PORTAL;
41  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PARAMETER_ADD;
42  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.SUPPRIMER_BROUILLON;
43  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TEMPLATE_VALIDATION;
44  
45  import java.util.HashMap;
46  import java.util.List;
47  import java.util.Map;
48  
49  import javax.servlet.http.HttpServletRequest;
50  
51  import fr.paris.lutece.plugins.ctv.bo.ChantierOccupation;
52  import fr.paris.lutece.plugins.ctv.bo.Emprise;
53  import fr.paris.lutece.plugins.ctv.bo.EntiteDemande;
54  import fr.paris.lutece.plugins.ctv.bo.InfosSuperviseur;
55  import fr.paris.lutece.plugins.ctv.business.IChantierService;
56  import fr.paris.lutece.plugins.ctv.business.IDemandeService;
57  import fr.paris.lutece.plugins.ctv.business.IEmpriseService;
58  import fr.paris.lutece.plugins.ctv.business.IEntiteDemandeService;
59  import fr.paris.lutece.plugins.ctv.business.IInfosSuperviseurService;
60  import fr.paris.lutece.plugins.ctv.constant.StatutDemande;
61  import fr.paris.lutece.plugins.ctv.constant.StatutEmprise;
62  import fr.paris.lutece.plugins.ctv.constants.CtvConstants;
63  import fr.paris.lutece.plugins.ctv.handler.CtvUploadDemandeDocumentHandler;
64  import fr.paris.lutece.plugins.ctv.mailtask.EmailTask;
65  import fr.paris.lutece.plugins.ctv.service.FormulaireDemandeOccupationServiceImpl;
66  import fr.paris.lutece.plugins.ctv.service.IFormulaire;
67  import fr.paris.lutece.plugins.ctv.util.Utils;
68  import fr.paris.lutece.plugins.ctv.view.object.DemandeReading;
69  import fr.paris.lutece.plugins.gismap.business.AddressParamHome;
70  import fr.paris.lutece.portal.service.datastore.DatastoreService;
71  import fr.paris.lutece.portal.service.security.UserNotSignedException;
72  import fr.paris.lutece.portal.service.spring.SpringContextService;
73  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
74  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
75  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
76  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
77  import fr.paris.lutece.portal.web.xpages.XPage;
78  
79  @Controller( xpageName = ETAPE_VALIDATION )
80  public class ValidationApp extends MVCApplication
81  {
82  
83      /**
84       *
85       */
86      private static final long serialVersionUID = 1L;
87      private IFormulaire formulaireService = FormulaireDemandeOccupationServiceImpl.getInstance( );
88      protected CtvUploadDemandeDocumentHandler ctvDemandeUploadHandler = SpringContextService
89              .getBean( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER );
90      private IDemandeService demandeService = SpringContextService.getBean( "demandeService" );
91      protected IChantierService chantierService = SpringContextService.getBean( "chantierService" );
92      protected IEmpriseService empriseService = SpringContextService.getBean( "empriseService" );
93      protected IEntiteDemandeService entiteDemandeService = SpringContextService.getBean( "entiteDemandeService" );
94      private EmailTasktv/mailtask/EmailTask.html#EmailTask">EmailTask emailTask = new EmailTask( );
95  
96      protected static final IInfosSuperviseurService infosSuperviseurService = SpringContextService.getBean( "infosSuperviseurService" );
97  
98      @View( value = ETAPE_VALIDATION, defaultView = true )
99      public XPage getValidation( HttpServletRequest request ) throws UserNotSignedException
100     {
101         Utils.getUser( request );
102         Map<String, Object> model = new HashMap<String, Object>( );
103         formulaireService.alimentationModel( request, model );
104         formulaireService.sauvegardeEtape( request, model, ETAPE_VALIDATION );
105         int numDemande = formulaireService.getNumDemande( request );
106         InfosSuperviseur oldInfosSuperviseur = infosSuperviseurService.findByNumDemande( numDemande );
107         model.put( "superviseur", oldInfosSuperviseur );
108         model.put( PARAMETER_ADD, AddressParamHome.getAddressParameters( ) );
109         return getXPage( TEMPLATE_VALIDATION, request.getLocale( ), model );
110     }
111 
112     @Action( ACTION_NEXT )
113     public XPage nextPage( HttpServletRequest request )
114     {
115         validationDemande( request );
116         emailTask.sendEmail( CtvConstants.EMAIL_CONFIRMATION, formulaireService.getNumDemande( request ), request.getLocale( ), request, -1 );
117         return redirect( request, PAGE_PORTAL + ETAPE_CONFIRMATION );
118     }
119 
120     @Action( SUPPRIMER_BROUILLON )
121     public XPage supprimerBrouillon( HttpServletRequest request )
122     {
123         int numDemande = formulaireService.getNumDemande( request );
124         List<Emprise> emprises = empriseService.findEmprisesByDemandeId( numDemande );
125         for ( Emprise emprise : emprises )
126         {
127             empriseService.deleteEmprise( numDemande, emprise.getEmpriseId( ) );
128         }
129         ChantierOccupation chantier = chantierService.getChantierOccupationByNumDemande( numDemande );
130         EntiteDemande demandeur = entiteDemandeService.getDemandeurByNumDemande( numDemande );
131         EntiteDemande executant = entiteDemandeService.getExecutantByNumDemande( numDemande );
132         if ( chantier != null )
133         {
134             chantierService.deleteChantierOccupationById( chantier.getChantierId( ) );
135         }
136 
137         if ( demandeur != null )
138         {
139             entiteDemandeService.deleteEntiteById( demandeur.getDemandeurId( ) );
140         }
141         if ( executant != null )
142         {
143             entiteDemandeService.deleteEntiteById( executant.getDemandeurId( ) );
144         }
145 
146         demandeService.deleteDemandeById( numDemande );
147         formulaireService.removeFilesFromSession( request );
148         return redirect( request, PAGE_PORTAL + MON_ESPACE_DEMANDEUR );
149     }
150 
151     private void validationDemande( HttpServletRequest request )
152     {
153         int demandeId = formulaireService.getNumDemande( request );
154         demandeService.updateDemandeStatut( demandeId, StatutDemande.A_TRAITER );
155         DemandeReading demande = demandeService.findByNumDemande( demandeId );
156         boolean premiereSoumission = false;
157 
158         if ( demande.getDemandeDateSoumission( ) == null )
159         {
160             premiereSoumission = true;
161             demandeService.updateDateSoumission( demandeId );
162         }
163         empriseService.updateStatutAllEmprises( demandeId, StatutEmprise.A_TRAITER, true );
164 
165         int demandeSectorId = demande.getDemandeSectorId( );
166         if ( demandeSectorId == 0 )
167         {
168             String x = request.getParameter( "x" );
169             String y = request.getParameter( "y" );
170             demandeService.updateDemandeSecteur( demandeId, x, y );
171         }
172         ctvDemandeUploadHandler.removeFilesInSession( request );
173         InfosSuperviseur oldInfosSuperviseur = infosSuperviseurService.findByNumDemande( demandeId );
174         InfosSuperviseur infos = new InfosSuperviseur( );
175         if ( oldInfosSuperviseur == null )
176         {
177             infos.setInfosSuperviseurPrescriptionParticuliere( DatastoreService
178                     .getDataValue( "sitelabels.site_property.bloc.qualification.prescription.particuliere", "prescriptions particulières par défaut" ) );
179             infosSuperviseurService.update( infos, demandeId );
180         }
181         else
182             if ( oldInfosSuperviseur.getInfosSuperviseurPrescriptionParticuliere( ) == null )
183             {
184                 oldInfosSuperviseur.setInfosSuperviseurPrescriptionParticuliere( DatastoreService
185                         .getDataValue( "sitelabels.site_property.bloc.qualification.prescription.particuliere", "prescriptions particulières par défaut" ) );
186                 infosSuperviseurService.update( oldInfosSuperviseur, demandeId );
187 
188             }
189 
190         //màj empriseAEnvoyerASavoi pour les demandes dont le demandeur siret est non taxable
191         String demandeurSiret = entiteDemandeService.getDemandeurByNumDemande( demandeId ).getSirenSiret( );
192         String numSiretNonTaxables = DatastoreService.getDataValue( "parametre.numeric.flux.savoi2.siret.non.taxable", "" );
193         if ( ( ( demandeurSiret != null ) && !"".equals(demandeurSiret) ) && numSiretNonTaxables.contains( demandeurSiret ) ) {
194             empriseService.updateEmpriseConvention( demandeId, false );
195         }
196         else if ( premiereSoumission && Boolean.TRUE.equals(demande.isDemandeConvention()) )
197         {
198             emailTask.sendEmail( CtvConstants.EMAIL_SOUMISSION_CONVENTION, demandeId, request.getLocale( ), request, -1 );
199         }
200     }
201 }