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_ABANDON;
37  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_COMPLETE_LATER;
38  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_DOWNLOAD;
39  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_NEXT;
40  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_SAVE;
41  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.CIVILITES;
42  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_CHANTIER;
43  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_DEMANDEUR;
44  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.EXONERATION_DE_TAXE;
45  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.MAX_FILE_SIZE;
46  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PAGE_PORTAL;
47  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PARAMETER_NEXT_STEP;
48  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PROFILS_ENTITE;
49  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.SESSION_NUM_DEMANDE_ID;
50  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TEMPLATE_DEMANDEUR;
51  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.XPAGE_ACCUEIL;
52  
53  import java.util.Date;
54  import java.util.HashMap;
55  import java.util.Map;
56  
57  import javax.servlet.http.HttpServletRequest;
58  
59  import org.apache.commons.beanutils.ConvertUtils;
60  import org.apache.commons.fileupload.FileItem;
61  import org.apache.commons.logging.Log;
62  import org.apache.commons.logging.LogFactory;
63  
64  import com.mysql.jdbc.StringUtils;
65  
66  import fr.paris.lutece.plugins.ctv.bo.ChantierOccupation;
67  import fr.paris.lutece.plugins.ctv.bo.DemandeWriting;
68  import fr.paris.lutece.plugins.ctv.bo.EntiteDemande;
69  import fr.paris.lutece.plugins.ctv.business.IChantierService;
70  import fr.paris.lutece.plugins.ctv.business.IDemandeService;
71  import fr.paris.lutece.plugins.ctv.business.IEntiteDemandeService;
72  import fr.paris.lutece.plugins.ctv.constant.Document;
73  import fr.paris.lutece.plugins.ctv.constant.StatutTaxationDemande;
74  import fr.paris.lutece.plugins.ctv.converter.CustomDateConverter;
75  import fr.paris.lutece.plugins.ctv.converter.EnumConverter;
76  import fr.paris.lutece.plugins.ctv.handler.CtvUploadDemandeDocumentHandler;
77  import fr.paris.lutece.plugins.ctv.service.FormulaireDemandeOccupationServiceImpl;
78  import fr.paris.lutece.plugins.ctv.service.IFormulaire;
79  import fr.paris.lutece.plugins.ctv.util.Utils;
80  import fr.paris.lutece.portal.service.datastore.DatastoreService;
81  import fr.paris.lutece.portal.service.security.LuteceUser;
82  import fr.paris.lutece.portal.service.security.SecurityService;
83  import fr.paris.lutece.portal.service.security.UserNotSignedException;
84  import fr.paris.lutece.portal.service.spring.SpringContextService;
85  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
86  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
87  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
88  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
89  import fr.paris.lutece.portal.web.xpages.XPage;
90  
91  @Controller( xpageName = ETAPE_DEMANDEUR )
92  public class DemandeurApp extends MVCApplication
93  {
94  
95      /**
96       *
97       */
98      private static final long               serialVersionUID        = 1L;
99      private IEntiteDemandeService           entiteDemandeService    = SpringContextService.getBean( "entiteDemandeService" );
100     private IDemandeService                 demandeService          = SpringContextService.getBean( "demandeService" );
101     private IFormulaire                     formulaireService       = FormulaireDemandeOccupationServiceImpl.getInstance( );
102     private static IChantierService         chantierService         = SpringContextService.getBean( "chantierService" );
103     public static final String              COMPLETE_TRUE           = "&complete=true";
104     private CtvUploadDemandeDocumentHandler ctvDemandeUploadHandler = SpringContextService.getBean( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER );
105     private final static Log                LOGGER                  = LogFactory.getLog( DemandeurApp.class );
106 
107     @View( value = ETAPE_DEMANDEUR, defaultView = true )
108     public XPage getIdentification( HttpServletRequest request ) throws UserNotSignedException
109     {
110 
111         Utils.getUser( request );
112 
113         LuteceUser user = SecurityService.getInstance( ).getRemoteUser( request );
114 
115         formulaireService.removeFilesFromSession( request );
116 
117         initNumDemande( request );
118 
119         Map<String, Object> model = initModel( );
120 
121         model.put( "nonTaxable", DatastoreService.getDataValue( "parametre.numeric.flux.savoi2.siret.non.taxable", "" ) );
122 
123         model.put( "email", user.getUserInfo( "EMAIL" ) );
124 
125         formulaireService.alimentationModel( request, model );
126 
127         formulaireService.sauvegardeEtape( request, model, ETAPE_DEMANDEUR );
128 
129         model.put( Document.JUSTIFICATIFS_CONVENTION + "_listUploadedFiles", ctvDemandeUploadHandler.getListUploadedFiles( Document.JUSTIFICATIFS_CONVENTION, request.getSession( ) ) );
130 
131 
132         return getXPage( TEMPLATE_DEMANDEUR, request.getLocale( ), model );
133     }
134 
135     @Action( ACTION_NEXT )
136     public XPage nextPage( HttpServletRequest request )
137     {
138         storeFiles( request );
139         updateDemandeurFromRequest( request );
140         updateDemandeFromRequest( request );
141         return redirectToPageChantier( request );
142     }
143 
144     private XPage redirectToPageChantier( HttpServletRequest request )
145     {
146         if ( request.getSession( ).getAttribute( SESSION_NUM_DEMANDE_ID ).toString( ) != null )
147         {
148             return returnChantierPage( request, request.getSession( ).getAttribute( SESSION_NUM_DEMANDE_ID ).toString( ) );
149         } else
150         {
151             return redirect( request, PAGE_PORTAL + ETAPE_CHANTIER );
152         }
153     }
154 
155     @Action( ACTION_COMPLETE_LATER )
156     public XPage completeLater( HttpServletRequest request )
157     {
158         storeFiles( request );
159         updateDemandeurFromRequest( request );
160         updateDemandeFromRequest( request );
161         return redirect( request, PAGE_PORTAL + XPAGE_ACCUEIL );
162     }
163 
164     @Action( ACTION_SAVE )
165     public XPage doSaveAction( HttpServletRequest request )
166     {
167         storeFiles( request );
168         updateDemandeurFromRequest( request );
169         updateDemandeFromRequest( request );
170         // Récupération de la redirection
171         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
172         if ( StringUtils.isNullOrEmpty( nextStepPath ) )
173         {
174             return redirect( request, PAGE_PORTAL + XPAGE_ACCUEIL );
175         }
176         return redirect( request, PAGE_PORTAL + nextStepPath );
177     }
178 
179     @Action( ACTION_ABANDON )
180     public XPage doAbandonAction( HttpServletRequest request )
181     {
182         formulaireService.removeFilesFromSession( request );
183         // Récupération de la redirection
184         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
185         if ( StringUtils.isNullOrEmpty( nextStepPath ) )
186         {
187             return redirect( request, PAGE_PORTAL + XPAGE_ACCUEIL );
188         }
189         return redirect( request, PAGE_PORTAL + nextStepPath );
190     }
191 
192     @Action( ACTION_DOWNLOAD )
193     public XPage download( HttpServletRequest request )
194     {
195         FileItem fileItem = formulaireService.getFileItemFromRequest( request );
196         if ( fileItem != null )
197         {
198             return download( fileItem.get( ), fileItem.getName( ), fileItem.getName( ) );
199         }
200         return null;
201     }
202 
203     private HashMap<String, Object> initModel( )
204     {
205         HashMap<String, Object> model = new HashMap<>( );
206         model.put( CIVILITES, EntiteDemande.Civilite.values( ) );
207         model.put( PROFILS_ENTITE, EntiteDemande.ProfilEntite.values( ) );
208         model.put( EXONERATION_DE_TAXE, DemandeWriting.ExonerationTaxe.values( ) );
209         model.put( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER, ctvDemandeUploadHandler );
210         model.put( MAX_FILE_SIZE, ctvDemandeUploadHandler.getMaxFileSize( ) );
211         return model;
212     }
213 
214     private void initNumDemande( HttpServletRequest request ) throws UserNotSignedException
215     {
216         if ( isNumDemandeExiste( request ) )
217         {
218             clearNumDemande( request );
219             setNumDemande( request );
220         }
221     }
222 
223     private void setNumDemande( HttpServletRequest request ) throws UserNotSignedException
224     {
225 
226         LuteceUser user = Utils.getUser( request );
227         String guid = user.getName( );
228         int demandeOccupation = demandeService.createDemandeOccupation( );
229         demandeService.addUserInDemandeOccupation( guid, demandeOccupation );
230 
231         request.getSession( ).setAttribute( SESSION_NUM_DEMANDE_ID, demandeOccupation );
232     }
233 
234     private void clearNumDemande( HttpServletRequest request )
235     {
236         request.getSession( ).removeAttribute( SESSION_NUM_DEMANDE_ID );
237     }
238 
239     private boolean isNumDemandeExiste( HttpServletRequest request )
240     {
241         return request.getParameter( "creation" ) != null;
242     }
243 
244     private void updateDemandeurFromRequest( HttpServletRequest request )
245     {
246         register( );
247         EntiteDemande demandeur = new EntiteDemande( );
248         populate( demandeur, request );
249         entiteDemandeService.updateDemandeur( formulaireService.getNumDemande( request ), demandeur );
250     }
251 
252     private void updateDemandeFromRequest( HttpServletRequest request )
253     {
254         EntiteDemande demandeur = new EntiteDemande( );
255         populate( demandeur, request );
256         DemandeWriting demande = new DemandeWriting( );
257         populate( demande, request );
258 
259         String numSiretNonTaxables = DatastoreService.getDataValue("parametre.numeric.flux.savoi2.siret.non.taxable", "");
260         int numDemande = formulaireService.getNumDemande( request );
261         String demandeurSiret = entiteDemandeService.getDemandeurByNumDemande(numDemande).getSirenSiret();
262 
263         if (demandeurSiret.isEmpty() || !numSiretNonTaxables.contains(demandeurSiret)) {
264 
265             if (( (demande.getExonerationTaxe().getExoneration()) != null )
266                     && "Oui".equals(demande.getExonerationTaxe().getExoneration())) {
267                 demande.setDemandeConvention(true);
268                 demande.setDemandeTaxation(StatutTaxationDemande.EXONERATION_A_VERIFIER);
269             } else {
270                 demande.setDemandeConvention(false);
271                 demande.setDemandeTaxation(StatutTaxationDemande.A_TAXER);
272             }
273             //cas spécifique des numéros siret non taxables
274         } else if (!demandeurSiret.isEmpty() && numSiretNonTaxables.contains(demandeurSiret)) {
275             demande.setDemandeConvention(false);
276             demande.setDemandeTaxation(StatutTaxationDemande.NON_TAXABLE);
277         }
278         demandeService.updateDemandeConvention( numDemande, demande.isDemandeConvention( ) );
279         demandeService.updateDemandeTaxation( numDemande, demande.getDemandeTaxation( ) );
280     }
281 
282     private void register( )
283     {
284         ConvertUtils.register( new EnumConverter( ), EntiteDemande.ProfilEntite.class );
285         ConvertUtils.register( new EnumConverter( ), EntiteDemande.Civilite.class );
286         ConvertUtils.register( new EnumConverter( ), DemandeWriting.ExonerationTaxe.class );
287         ConvertUtils.register( new CustomDateConverter( ), Date.class );
288         ConvertUtils.register( new EnumConverter( ), StatutTaxationDemande.class );
289     }
290 
291     private XPage returnChantierPage( HttpServletRequest request, String numDemande )
292     {
293         int demandeId = Integer.parseInt( numDemande );
294         ChantierOccupation chantier = chantierService.getChantierOccupationByNumDemande( demandeId );
295         if ( chantier != null )
296         {
297             return redirect( request, PAGE_PORTAL + ETAPE_CHANTIER + COMPLETE_TRUE );
298         } else
299         {
300             return redirect( request, PAGE_PORTAL + ETAPE_CHANTIER );
301         }
302     }
303 
304     private void storeFiles( HttpServletRequest request )
305     {
306         ctvDemandeUploadHandler.storeFilesOnDisk( request, Document.JUSTIFICATIFS_CONVENTION );
307     }
308 
309 }