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.conventions;
35  
36  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PARAMETER_ADD;
37  
38  import java.util.ArrayList;
39  import java.util.Arrays;
40  import java.util.Date;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.stream.Collectors;
44  
45  import javax.servlet.http.HttpServletRequest;
46  import javax.servlet.http.HttpSession;
47  
48  import org.apache.commons.beanutils.ConvertUtils;
49  import org.apache.commons.fileupload.FileItem;
50  
51  import fr.paris.lutece.plugins.ctv.bo.Convention;
52  import fr.paris.lutece.plugins.ctv.bo.EntiteDemande;
53  import fr.paris.lutece.plugins.ctv.bo.SearchBeanForConvention;
54  import fr.paris.lutece.plugins.ctv.bo.controller.demandes.AbstractCtvJspBean;
55  import fr.paris.lutece.plugins.ctv.business.IConventionService;
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.constant.Document;
60  import fr.paris.lutece.plugins.ctv.constant.StatutTaxationDemande;
61  import fr.paris.lutece.plugins.ctv.constants.CtvConstants;
62  import fr.paris.lutece.plugins.ctv.converter.CustomDateConverter;
63  import fr.paris.lutece.plugins.ctv.converter.EnumConverter;
64  import fr.paris.lutece.plugins.ctv.handler.CtvUploadDemandeDocumentHandler;
65  import fr.paris.lutece.plugins.ctv.mailtask.EmailTask;
66  import fr.paris.lutece.plugins.ctv.view.object.DemandeReading;
67  import fr.paris.lutece.plugins.gismap.business.AddressParamHome;
68  import fr.paris.lutece.portal.service.spring.SpringContextService;
69  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
70  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
71  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
72  
73  @Controller( controllerPath = "jsp/admin/plugins/ctv/gestionConvention", controllerJsp = "gestionConvention.jsp", right = "GESTION_DES_CONVENTIONS" )
74  public class GestionConventionJspBean  extends AbstractCtvJspBean
75  {
76  
77      private static final long serialVersionUID = 1772228925518151231L;
78      private IConventionService conventionService = SpringContextService.getBean( "conventionService" );
79      private IDemandeService demandeService = SpringContextService.getBean( "demandeService" );
80      private IEmpriseService empriseService = SpringContextService.getBean( "empriseService" );
81      private IEntiteDemandeService entiteDemandeService = SpringContextService.getBean( "entiteDemandeService" );
82      private CtvUploadDemandeDocumentHandler ctvDemandeUploadHandler = SpringContextService
83              .getBean( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER );
84      private EmailTasks/ctv/mailtask/EmailTask.html#EmailTask">EmailTask emailTask = new EmailTask( );
85  
86      List<String> validationConvention = Arrays.asList("Oui", "Non");
87  
88      @View( defaultView = true, value = "view_convention" )
89      public String getTableauDeBord( HttpServletRequest request )
90      {
91          register( );
92          List<StatutTaxationDemande> statutsTaxation = getStatutsTaxationForRecherche();
93          List<DemandeReading> demandesWithConvention = demandeService.getListDemandesWithConvention();
94          putFileNames( demandesWithConvention, request );
95          Map<String, Object> model = getModel( );
96          model.put( PARAMETER_ADD, AddressParamHome.getAddressParameters( ) );
97          model.put("demandes", demandesWithConvention);
98          putPathDemande( model );
99          model.put("validation_convention", validationConvention );
100         model.put( "statutTaxation", statutsTaxation );
101         SearchBeanForConvention searchBean = new SearchBeanForConvention();
102         searchBean.setStatutTaxation("EXONERATION_A_VERIFIER");
103         model.put( "searchCriteria", searchBean );
104 
105         return getPage( GESTION_EMAIL_PAGE_TITLE, TEMPLATE_MENU_CONVENTION, model );
106 
107     }
108 
109     @Action( "rechercheConventions" )
110     public String displayConfigEmail( HttpServletRequest request )
111     {
112         register( );
113         List<StatutTaxationDemande> statutsTaxation = getStatutsTaxationForRecherche();
114         Map<String, Object> model = getModel( );
115         Convention test=  new Convention( );
116         populate( test, request );
117 
118         SearchBeanForConvention searchBean = generateSearchBean(request);
119         List<DemandeReading> demandesWithConvention = demandeService.getListDemandesConventionByCriteria(searchBean);
120         putFileNames( demandesWithConvention, request );
121         model.put("demandes", demandesWithConvention);
122         putPathDemande( model );
123         model.put( PARAMETER_ADD, AddressParamHome.getAddressParameters( ) );
124         model.put("validation_convention", validationConvention );
125         model.put( "statutTaxation", statutsTaxation );
126         model.put( "searchCriteria", searchBean );
127 
128         return getPage( GESTION_EMAIL_PAGE_TITLE, TEMPLATE_MENU_CONVENTION, model );
129 
130     }
131 
132     @Action( "gererConvention" )
133     public String gestionConvention( HttpServletRequest request )
134     {
135         register( );
136         Map<String, Object> model = getModel( );
137         int idDemande = Integer.parseInt( request.getParameter( "idDemande" ) );
138         List<Convention> listConventions = conventionService.getAllConventionByDemandeId( idDemande ) ;
139         DemandeReading demande = demandeService.findByNumDemande( idDemande );
140         EntiteDemande demandeur = entiteDemandeService.getDemandeurByNumDemande( idDemande );
141         putFileNames( Arrays.asList(demande), request );
142 
143         model.put( "conventions", listConventions );
144         model.put( "demande", demande );
145         model.put( "demandeur", demandeur );
146         model.put( "user", getUser( ));
147         putPathDemande( model );
148 
149         return getPage( GESTION_EMAIL_PAGE_TITLE, TEMPLATE_MANAGE_CONVENTION, model );
150 
151     }
152 
153     @Action( "updateConvention")
154     public String updateConvention( HttpServletRequest request )
155     {
156         register( );
157         Integer idDemande = Integer.valueOf(request.getParameter( "numDemande"));
158         String paramValidation = request.getParameter( "validationConvention" );
159         Boolean isConventionValide = false;
160 
161         if (paramValidation != null) {
162             isConventionValide = Boolean.parseBoolean( paramValidation );
163         }
164         //Ajout d'une ligne d'historisation de convention
165         Convention convention = new Convention( );
166         convention.setConventionAgent( request.getParameter( "agent" ) );
167         convention.setConventionCommentaireInterne( request.getParameter( "commentaireInterne" ) );
168         convention.setConventionCommentaireUsager( request.getParameter( "commentaireUsager" ) );
169         convention.setConventionDateValidationRefus( new Date( ) );
170         convention.setConventionDemandeId( idDemande );
171         convention.setConventionValidation( isConventionValide );
172         conventionService.insert( convention );
173 
174         //Update de la demande si convention non valide
175         demandeService.updateTaxationDemandeConvention( idDemande, isConventionValide );
176 
177         //Update des emprises selon la convention (si valide : emprise(s) pas à compléter | si non valide, emprise(s) à compléter)
178         empriseService.updateEmpriseConvention( idDemande, !isConventionValide );
179 
180         if (Boolean.TRUE.equals(isConventionValide))
181         {
182             emailTask.sendEmail( CtvConstants.EMAIL_VALIDATION_CONVENTION, idDemande, request.getLocale( ), request, -1 );
183         }
184         else
185         {
186             emailTask.sendEmail( CtvConstants.EMAIL_REFUS_CONVENTION, idDemande, request.getLocale( ), request, -1 );
187         }
188 
189         return redirectView( request, "rechercheConventions" );
190 
191     }
192 
193     private void putPathDemande( Map<String, Object> model )
194     {
195         model.put( MARK_PATH_DEMANDE_INTENTION, PATH_DEMANDE_INTENTION );
196         model.put( MARK_PATH_DEMANDE_OCCUPATION, PATH_DEMANDE_OCCUPATION );
197         model.put( MARK_PATH_DEMANDE_INTERVENTION, PATH_DEMANDE_INTERVENTION );
198     }
199 
200     private void putFileNames( List<DemandeReading> demandesWithConvention, HttpServletRequest request )
201     {
202         //récupere les pièces jointes pour chaque demande de la liste générée avec ou sans critères de recherche
203         for ( DemandeReading dwc : demandesWithConvention )
204         {
205             ctvDemandeUploadHandler.removeFilesInSession( request );
206             HttpSession session = request.getSession( );
207             session.setAttribute(CtvConstants.SESSION_NUM_DEMANDE_ID, Integer.toString(dwc.getDemandeNumero()) );
208             List<String> fileNames = ctvDemandeUploadHandler.getListUploadedFiles(Document.JUSTIFICATIFS_CONVENTION, request.getSession( ) ).stream()
209                     .map(FileItem::getName)
210                     .collect(Collectors.toList());
211             dwc.setFileNames(fileNames);
212             request.removeAttribute(CtvConstants.SESSION_NUM_DEMANDE_ID);
213         }
214     }
215 
216     private SearchBeanForConvention generateSearchBean( HttpServletRequest request )
217     {
218         SearchBeanForConvention searchBean = new SearchBeanForConvention();
219         ConvertUtils.register( new CustomDateConverter( ), Date.class );
220         ConvertUtils.register( new EnumConverter( ), StatutTaxationDemande.class );
221         populate( searchBean, request );
222         return searchBean;
223     }
224 
225     private void register( )
226     {
227         ConvertUtils.register( new EnumConverter( ), StatutTaxationDemande.class );
228     }
229 
230     private List<StatutTaxationDemande> getStatutsTaxationForRecherche()
231     {
232         List<StatutTaxationDemande> statutsTaxation =  new ArrayList<>();
233         statutsTaxation.add( StatutTaxationDemande.EXONERATION_A_VERIFIER );
234         statutsTaxation.add( StatutTaxationDemande.EXONERATION_A_COMPLETER );
235         statutsTaxation.add( StatutTaxationDemande.NON_TAXABLE );
236 
237         return statutsTaxation;
238     }
239 
240 }