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.intention;
35  
36  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.CIVILITES;
37  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_DEMANDEUR;
38  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.MAX_FILE_SIZE;
39  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.SESSION_NUM_DEMANDE_ID;
40  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TEMPLATE_DEMANDEUR_INTENTION;
41  
42  import java.util.HashMap;
43  import java.util.Map;
44  
45  import javax.servlet.http.HttpServletRequest;
46  
47  import org.apache.commons.beanutils.ConvertUtils;
48  
49  import com.mysql.jdbc.StringUtils;
50  
51  import fr.paris.lutece.plugins.ctv.bo.DemandeWriting;
52  import fr.paris.lutece.plugins.ctv.bo.EntiteDemande;
53  import fr.paris.lutece.plugins.ctv.bo.EntiteDemande.ProfilEntite;
54  import fr.paris.lutece.plugins.ctv.bo.controller.demandes.AbstractCtvJspBean;
55  import fr.paris.lutece.plugins.ctv.business.IDemandeService;
56  import fr.paris.lutece.plugins.ctv.business.IEntiteDemandeService;
57  import fr.paris.lutece.plugins.ctv.constant.Document;
58  import fr.paris.lutece.plugins.ctv.constant.StatutTaxationDemande;
59  import fr.paris.lutece.plugins.ctv.converter.EnumConverter;
60  import fr.paris.lutece.plugins.ctv.handler.CtvUploadDemandeDocumentHandler;
61  import fr.paris.lutece.plugins.ctv.service.FormulaireDemandeIntentionServiceImpl;
62  import fr.paris.lutece.plugins.ctv.service.IFormulaire;
63  import fr.paris.lutece.portal.service.datastore.DatastoreService;
64  import fr.paris.lutece.portal.service.spring.SpringContextService;
65  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
66  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
67  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
68  
69  @Controller( controllerPath = "jsp/admin/plugins/ctv/gestionDemandes/intention/etapes", controllerJsp = "demandeur.jsp", right = "DEMANDE_INTENTION" )
70  
71  public class DemandeurJspBean extends AbstractCtvJspBean
72  {
73  
74      private static final long serialVersionUID = 6185806539165628650L;
75  
76      private IEntiteDemandeService entiteDemandeService = SpringContextService.getBean( "entiteDemandeService" );
77      private IDemandeService demandeService = SpringContextService.getBean( "demandeService" );
78      protected IFormulaire formulaireIntentionService = FormulaireDemandeIntentionServiceImpl.getInstance( );
79  
80      private static final String EXONERATION_DE_TAXE = "exonerationDeTaxe";
81      protected CtvUploadDemandeDocumentHandler ctvDemandeUploadHandler = SpringContextService
82              .getBean( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER );
83  
84      @View( value = ETAPE_DEMANDEUR, defaultView = true )
85      public String getIdentification( HttpServletRequest request )
86      {
87          initNumDemande( request );
88          Map<String, Object> model = new HashMap<>( );
89          initModel( model );
90          formulaireIntentionService.alimentationModel( request, model );
91          formulaireIntentionService.sauvegardeEtape( request, model, ETAPE_DEMANDEUR );
92          int numDemande = formulaireIntentionService.getNumDemande( request );
93          model.put( Document.JUSTIFICATIFS_CONVENTION + "_listUploadedFiles", ctvDemandeUploadHandler.getListUploadedFiles(Document.JUSTIFICATIFS_CONVENTION, request.getSession( ) ) );
94  
95          fillUserPossibleActions( request, model, numDemande );
96          if ( ( request.getParameter( "complete" ) != null ) || ( entiteDemandeService.getDemandeurByNumDemande( numDemande ) != null ) )
97          {
98              model.put( "controlForm", true );
99          }
100 
101         return getPage( DEMANDE_INTENTION_PAGE_TITLE, TEMPLATE_DEMANDEUR_INTENTION, model );
102     }
103 
104     @Action( ACTION_SAVE )
105     public String save( HttpServletRequest request )
106     {
107         updateDemandeurFromRequest( request );
108         int numDemande = formulaireIntentionService.getNumDemande( request );
109         fillLastUser( numDemande, request );
110         updateDemandeFromRequest( request );
111 
112         // Récupération de la redirection
113         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
114         if ( StringUtils.isNullOrEmpty( nextStepPath ) )
115         {
116             return redirect( request, "demandeur.jsp" );
117         }
118         return redirect( request, nextStepPath );
119     }
120 
121     @Action( ACTION_NEXT )
122     public String next( HttpServletRequest request )
123     {
124         updateDemandeurFromRequest( request );
125         updateDemandeFromRequest( request );
126 
127         return redirect( request, "chantier.jsp" );
128     }
129 
130     @Action( ACTION_COMPLETE_LATER )
131     public String completeLater( HttpServletRequest request )
132     {
133         updateDemandeurFromRequest( request );
134         updateDemandeFromRequest( request );
135         formulaireIntentionService.removeFilesFromSession( request );
136         return redirect( request, getAbsolutePath( request, PATH_TABLEAU_DE_BORD ) );
137     }
138 
139     @Action( ACTION_ABANDON )
140     public String abandon( HttpServletRequest request )
141     {
142         formulaireIntentionService.removeFilesFromSession( request );
143         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
144         if ( StringUtils.isNullOrEmpty( nextStepPath ) )
145         {
146             return redirect( request, "../demandeIntention.jsp" );
147         }
148         return redirect( request, nextStepPath );
149     }
150 
151     private void initModel( Map<String, Object> model )
152     {
153         model.put( CIVILITES, EntiteDemande.Civilite.values( ) );
154         model.put( EXONERATION_DE_TAXE, DemandeWriting.ExonerationTaxe.values( ) );
155         model.put( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER, ctvDemandeUploadHandler );
156         model.put( MAX_FILE_SIZE, ctvDemandeUploadHandler.getMaxFileSize( ) );
157 
158     }
159 
160     private void initNumDemande( HttpServletRequest request )
161     {
162         if ( isNumDemandeExiste( request ) )
163         {
164             clearNumDemande( request );
165             setNumDemande( request );
166         }
167     }
168 
169     private void setNumDemande( HttpServletRequest request )
170     {
171         String typeDemande = request.getParameter( "typeDemande" );
172         int demandeIntention = 0;
173         if ( "BarrageUS".equals( typeDemande ) )
174         {
175             demandeIntention = demandeService.createDemandeBarrageUS( );
176         }
177         else
178             if ( "TravauxImperatifsNonProgrammes".equals( typeDemande ) )
179             {
180                 demandeIntention = demandeService.createDemandeTravauxImperatifsNonProgrammes( );
181             }
182             else
183             {
184                 demandeIntention = demandeService.createDemandeIntention( );
185             }
186         ctvDemandeUploadHandler.removeFilesInSession( request );
187         request.getSession( ).setAttribute( SESSION_NUM_DEMANDE_ID, demandeIntention );
188     }
189 
190     private void clearNumDemande( HttpServletRequest request )
191     {
192         request.getSession( ).removeAttribute( SESSION_NUM_DEMANDE_ID );
193     }
194 
195     private void updateDemandeurFromRequest( HttpServletRequest request )
196     {
197         register( );
198         EntiteDemande demandeur = new EntiteDemande( );
199         populate( demandeur, request );
200         demandeur.setProfilEntite( ProfilEntite.PROFESSIONNEL );
201         entiteDemandeService.updateDemandeur( formulaireIntentionService.getNumDemande( request ), demandeur );
202     }
203 
204     private void updateDemandeFromRequest( HttpServletRequest request )
205     {
206         register( );
207         DemandeWriting demande = new DemandeWriting();
208         populate(demande, request);
209 
210         int numDemande = formulaireIntentionService.getNumDemande( request );
211 
212         String demandeurSiret = entiteDemandeService.getDemandeurByNumDemande(numDemande).getSirenSiret();
213         String numSiretNonTaxables = DatastoreService.getDataValue("parametre.numeric.flux.savoi2.siret.non.taxable", "");
214 
215         if (demandeurSiret.isEmpty() || ( demandeurSiret == null ) || !numSiretNonTaxables.contains(demandeurSiret)) {
216             demande.setDemandeConvention(false);
217             demande.setDemandeTaxation(StatutTaxationDemande.A_TAXER);
218         }
219         else
220         {
221             demande.setDemandeConvention(false);
222             demande.setDemandeTaxation(StatutTaxationDemande.NON_TAXABLE);
223         }
224 
225         demandeService.updateDemandeConvention( numDemande, demande.isDemandeConvention( ) );
226         demandeService.updateDemandeTaxation( numDemande, demande.getDemandeTaxation( ) );
227     }
228 
229     private boolean isNumDemandeExiste( HttpServletRequest request )
230     {
231         return request.getParameter( "creation" ) != null;
232     }
233 
234     private void register( )
235     {
236         ConvertUtils.register( new EnumConverter( ), EntiteDemande.Civilite.class );
237         ConvertUtils.register( new EnumConverter( ), DemandeWriting.ExonerationTaxe.class );
238     }
239 
240 }