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;
35  
36  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.MAX_FILE_SIZE;
37  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TEMPLATE_CREATION_CR_VISITE;
38  
39  import java.util.Date;
40  import java.util.HashMap;
41  import java.util.List;
42  import java.util.Map;
43  
44  import javax.servlet.http.HttpServletRequest;
45  
46  import fr.paris.lutece.plugins.ctv.bo.AlerteDemande.TypeAlerteDemande;
47  import fr.paris.lutece.plugins.ctv.bo.CompteRenduVisite;
48  import fr.paris.lutece.plugins.ctv.bo.ItemReferentiel.TypeReferentiel;
49  import fr.paris.lutece.plugins.ctv.business.IAlerteDemandeService;
50  import fr.paris.lutece.plugins.ctv.business.ICompteRenduVisiteService;
51  import fr.paris.lutece.plugins.ctv.business.IDemandeService;
52  import fr.paris.lutece.plugins.ctv.business.IReferentielsService;
53  import fr.paris.lutece.plugins.ctv.constants.CtvConstants;
54  import fr.paris.lutece.plugins.ctv.handler.CtvUploadCompteRenduVisiteHandler;
55  import fr.paris.lutece.plugins.ctv.util.Utils;
56  import fr.paris.lutece.portal.service.spring.SpringContextService;
57  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
58  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
59  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
60  
61  @Controller( controllerJsp = "creationCompteRenduvisite.jsp", controllerPath = "jsp/admin/plugins/ctv/gestionCompteRenduVisites", right = "CREATION_CR_VISITE" )
62  public class CreationCRVisiteJspBean extends AbstractCtvJspBean
63  {
64  
65      private static final String CREATION_CR_VISITE = "creation_cr_visite";
66  
67      private CtvUploadCompteRenduVisiteHandler ctvUploadCompteRenduVisiteHandler = SpringContextService
68              .getBean( CtvUploadCompteRenduVisiteHandler.CTV_CR_VISITE_UPLOAD_HANDLER );
69  
70      private static ICompteRenduVisiteService compteRenduVisiteService = SpringContextService.getBean( "compteRenduVisiteService" );
71  
72      private static IReferentielsService referentielsService = SpringContextService.getBean( "referentielsService" );
73  
74      private static IDemandeService demandeService = SpringContextService.getBean( "demandeService" );
75  
76      private static IAlerteDemandeService alerteDemandeService = SpringContextService.getBean( "alerteDemandeService" );
77  
78      @View( value = CREATION_CR_VISITE, defaultView = true )
79      public String getCreationCRVisite( HttpServletRequest request )
80      {
81          return initCreationCRVisite( request, null, null );
82      }
83  
84      private String initCreationCRVisite( HttpServletRequest request, CompteRenduVisite compteRenduVisite, HashMap<String, List<String>> errors )
85      {
86          Map<String, Object> model = new HashMap<>( );
87          model.put( CtvUploadCompteRenduVisiteHandler.CTV_CR_VISITE_UPLOAD_HANDLER, ctvUploadCompteRenduVisiteHandler );
88          model.put( MAX_FILE_SIZE, ctvUploadCompteRenduVisiteHandler.getMaxFileSize( ) );
89          model.put( "compteRenduVisite_listUploadedFiles",
90                  ctvUploadCompteRenduVisiteHandler.getListUploadedFiles( "compteRenduVisite", request.getSession( ) ) );
91          model.put( "types_visite", referentielsService.getItemReferentielList( TypeReferentiel.TYPE_VISITE ) );
92          String numProjet = request.getParameter( "num_projet" );
93          model.put( "num_projet", numProjet );
94          String numDemande = request.getParameter( "num_demande" );
95          model.put( "num_demande", numDemande );
96          if ( compteRenduVisite != null )
97          {
98              model.put( "compteRenduVisite", compteRenduVisite );
99          }
100         if ( errors != null )
101         {
102             for ( String errorType : errors.keySet( ) )
103             {
104                 List<String> listFieldName = errors.get( errorType );
105                 if ( !listFieldName.isEmpty( ) )
106                 {
107                     model.put( errorType, listFieldName );
108                 }
109             }
110         }
111         fillInfosFilDArianeProjet( model, numProjet );
112 
113         model.put( "demande", demandeService.findByNumDemande( Integer.parseInt( numDemande ) ) );
114 
115         return getPage( "", TEMPLATE_CREATION_CR_VISITE, model );
116     }
117 
118     @Action( "annuler" )
119     public String annuler( HttpServletRequest request )
120     {
121         ctvUploadCompteRenduVisiteHandler.removeFilesInSession( request );
122         return redirectToProjet( request );
123     }
124 
125     @Action( "valider" )
126     public String valider( HttpServletRequest request )
127     {
128         String numDemande = request.getParameter( "num_demande" );
129         CompteRenduVisite compteRendu = new CompteRenduVisite( );
130         populate( compteRendu, request );
131         int demandeId = Integer.parseInt( numDemande );
132         compteRendu.setDemandeId( demandeId );
133         compteRendu.setDateCreation( new Date( ) );
134 
135         HashMap<String, List<String>> requiredFielsInError = new HashMap<>( );
136         Utils.controlRequiredFields( "errorRequiredFields", compteRendu, requiredFielsInError );
137 
138         if ( !requiredFielsInError.isEmpty( ) )
139         {
140             return initCreationCRVisite( request, compteRendu, requiredFielsInError );
141         }
142 
143         int idCrVisite = compteRenduVisiteService.insertCompteRendu( compteRendu );
144         request.getSession( ).setAttribute( CtvConstants.SESSION_CR_VISITE_ID, idCrVisite );
145         ctvUploadCompteRenduVisiteHandler.storeFilesOnDisk( request, "compteRenduVisite" );
146         ctvUploadCompteRenduVisiteHandler.removeFilesInSession( request );
147         request.getSession( ).removeAttribute( CtvConstants.SESSION_CR_VISITE_ID );
148         alerteDemandeService.insertAlerte( TypeAlerteDemande.COMPTE_RENDU_VISITE, demandeId );
149         fillLastUser( demandeId, request );
150         return redirectToProjet( request );
151     }
152 
153     private String redirectToProjet( HttpServletRequest request )
154     {
155         String numProjet = request.getParameter( "num_projet" );
156         String numDemande = request.getParameter( "num_demande" );
157         return redirect( request, getAbsolutePath( request, PATH_PAGE_PROJET ) + "?num_projet=" + numProjet + "&selected_demande_id=" + numDemande );
158     }
159 
160 }