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.intervention;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Map;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  import com.mysql.jdbc.StringUtils;
43  
44  import fr.paris.lutece.plugins.ctv.bo.AlerteDemande.TypeAlerteDemande;
45  import fr.paris.lutece.plugins.ctv.bo.controller.demandes.AbstractCtvJspBean;
46  import fr.paris.lutece.plugins.ctv.constant.Document;
47  import fr.paris.lutece.plugins.ctv.constant.StatutDemande;
48  import fr.paris.lutece.plugins.ctv.constant.TypeDemande;
49  import fr.paris.lutece.plugins.ctv.constant.TypeProfileCtv;
50  import fr.paris.lutece.plugins.ctv.constants.CtvConstants;
51  import fr.paris.lutece.plugins.ctv.handler.CtvUploadDemandeDocumentHandler;
52  import fr.paris.lutece.plugins.ctv.service.FormulaireDemandeInterventionServiceImpl;
53  import fr.paris.lutece.plugins.ctv.service.IFormulaire;
54  import fr.paris.lutece.plugins.ctv.view.object.DemandeReading;
55  import fr.paris.lutece.plugins.unittree.business.unit.Unit;
56  import fr.paris.lutece.plugins.unittree.business.unit.Unit.TypeUnit;
57  import fr.paris.lutece.plugins.unittree.service.unit.IUnitService;
58  import fr.paris.lutece.portal.service.spring.SpringContextService;
59  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
60  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
61  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
62  
63  /**
64   * This class provides the user interface to accueil jsp
65   */
66  
67  @Controller( controllerJsp = "demandeIntervention.jsp", controllerPath = "jsp/admin/plugins/ctv/gestionDemandes/intervention", right = "TABLEAU_DE_BORD" )
68  public class DemandeInterventionJspBean extends AbstractCtvJspBean
69  {
70  
71      private static final long serialVersionUID = -4733201685972340506L;
72  
73      private static final String REMOVE_TRANSFERT = "remove_transfert";
74      private static final String MARK_ENABLE_SIGNER_BUTTON = "enable_signer_button";
75      private static final String MARK_STATUT_SIGNE = "signe";
76  
77      // Views
78      private static final String VIEW_INSTRUCTION_DEMANDE_INTERVENTION = "instructionDemandeIntervention";
79  
80      private static final String TEMPLATE_INSTRUCTION_INTERVENTION = "/admin/plugins/ctv/instructionDemandeIntervention.html";
81  
82      // Parameter
83      private static final String PARAMETER_NUM_DEMANDE = "num_demande";
84  
85      protected IFormulaire formulaireInterventionService = FormulaireDemandeInterventionServiceImpl.getInstance( );
86  
87      private IUnitService unitService = SpringContextService.getBean( IUnitService.BEAN_UNIT_SERVICE );
88      
89      protected CtvUploadDemandeDocumentHandler ctvDemandeUploadHandler = SpringContextService
90              .getBean( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER );
91  
92      /**
93       * Vue permettant de voir les informations liées à la demande
94       *
95       * @param request
96       *            the HttpRequest
97       * @return
98       */
99      @View( value = VIEW_INSTRUCTION_DEMANDE_INTERVENTION, defaultView = true )
100     public String getInstructionDemandeIntervention( HttpServletRequest request )
101     {
102         Map<String, Object> model = getModel( );
103 
104         String numDemandeStr = request.getParameter( PARAMETER_NUM_DEMANDE );
105         Integer numDemande = null;
106         // Récupération de la demande à instruire
107         if ( !StringUtils.isNullOrEmpty( numDemandeStr ) )
108         {
109             numDemande = Integer.parseInt( numDemandeStr );
110         }
111 
112         // Si la demande à instruire est différente de la dernière, vidage
113         // des
114         // PJ
115         Integer numDemandeSession = formulaireInterventionService.getNumDemande( request );
116         if ( ( null == numDemandeSession ) || ( ( numDemande != null ) && !numDemandeSession.equals( numDemande ) ) )
117         {
118             request.getSession( ).setAttribute( CtvConstants.SESSION_NUM_DEMANDE_ID, numDemande );
119             formulaireInterventionService.removeFilesFromSession( request );
120         }
121 
122         formulaireInterventionService.alimentationModel( request, model );
123 
124         DemandeReading demande = (DemandeReading) model.get( CtvConstants.DEMANDE );
125         boolean canAccessDemande = canAccessDemande( demande, request );
126         model.put( "canAccessDemande", canAccessDemande( demande, request ) );
127 
128         if ( !TypeDemande.INTERVENTION.equals( demande.getDemandeType( ) ) )
129         {
130             return redirect( request, "../../tableauDeBord/tableauDeBord.jsp" );
131         }
132         else
133             if ( StatutDemande.BROUILLON.equals( demande.getDemandeStatut( ) ) )
134             {
135                 return redirect( request, "etapes/demandeur.jsp" );
136             }
137 
138         model.put( MARK_ETAPE, CtvConstants.ETAPE_INSTRUCTION );
139         model.put( Document.JUSTIFICATIFS_CONVENTION + "_listUploadedFiles", ctvDemandeUploadHandler.getListUploadedFiles( Document.JUSTIFICATIFS_CONVENTION, request.getSession( ) ) );
140 
141         numDemande = numDemande != null ? numDemande : numDemandeSession;
142         fillUserPossibleActions( request, model, numDemande );
143 
144         boolean isAdminFonc = ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.ADMIN_FONC, getPlugin( ), request );
145 
146         if ( ( model.get( MARK_STATUT_SIGNE ) == StatutDemande.SIGNE ) && (!isAdminFonc))
147         {
148             model.put( MARK_ENABLE_SIGNER_BUTTON, true );
149         }
150         putAlerteDemandeByProfile( model, TypeProfileCtv.INSTRUCTEUR, TypeAlerteDemande.ANNULEE, numDemande, request );
151         putAlerteDemandeByProfile( model, TypeProfileCtv.INSTRUCTEUR, TypeAlerteDemande.SIGNE_INSTRUCTEUR, numDemande, request );
152         putAlerteDemandeByProfile( model, TypeProfileCtv.CONCESSIONNAIRE, TypeAlerteDemande.SIGNE_CONCESSIONNAIRE, numDemande, request );
153         putAlerteDemandeByProfile( model, TypeProfileCtv.CONCESSIONNAIRE, TypeAlerteDemande.PROLONGATION_REFUSEE, numDemande, request );
154         return getPage( "", TEMPLATE_INSTRUCTION_INTERVENTION, model );
155 
156     }
157 
158     @Action( REMOVE_TRANSFERT )
159     public String removeTransfert( HttpServletRequest request )
160     {
161         int numDemande = formulaireInterventionService.getNumDemande( request );
162         demandeService.updateDemandeTransfered( false, numDemande );
163         return redirectView( request, VIEW_INSTRUCTION_DEMANDE_INTERVENTION );
164     }
165 
166     @Action( "remove_alert" )
167     public String removeAlert( HttpServletRequest request )
168     {
169         int idAlerte = Integer.parseInt( request.getParameter( "idAlerte" ) );
170         alerteDemandeService.removeAlerte( idAlerte );
171         return redirectView( request, VIEW_INSTRUCTION_DEMANDE_INTERVENTION );
172     }
173 
174     @Action( ACTION_ABANDON )
175     public String abandon( HttpServletRequest request )
176     {
177         formulaireInterventionService.removeFilesFromSession( request );
178         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
179         if ( StringUtils.isNullOrEmpty( nextStepPath ) )
180         {
181             return redirect( request, "../demandeIntervention.jsp" );
182         }
183         return redirect( request, nextStepPath );
184     }
185 
186     private boolean canAccessDemande( DemandeReading demande, HttpServletRequest request )
187     {
188         boolean canAccessDemande = true;
189         boolean isConcessionnaire = ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.CONCESSIONNAIRE, getPlugin( ), request );
190         if ( isConcessionnaire )
191         {
192             Unit concession = unitService.getUnitUserByType( getUser( ), TypeUnit.CONCESSIONNAIRE );
193             List<Integer> concessionId = unitService.getAllUnitsIdByUnit( concession );
194             List<String> listLabelUnits = new ArrayList<String>( );
195             for ( int unitId : concessionId )
196             {
197                 Unit unit = unitService.getUnit( unitId, false );
198                 listLabelUnits.add( unit.getLabel( ) );
199             }
200             if ( !listLabelUnits.contains( demande.getRaisonSociale( ) ) )
201             {
202                 canAccessDemande = false;
203             }
204         }
205         return canAccessDemande;
206     }
207 
208 }