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.occupation;
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.FormulaireDemandeOccupationBoServiceImpl;
53  import fr.paris.lutece.plugins.ctv.service.IFormulaireDemandeOccupationBoService;
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 = "demandeOccupation.jsp", controllerPath = "jsp/admin/plugins/ctv/gestionDemandes/occupation", right = "DEMANDE_OCCUPATION" )
68  public class DemandeOccupationJspBean extends AbstractCtvJspBean
69  {
70  
71      private static final long serialVersionUID = 8164933729988156488L;
72  
73      private static final String REMOVE_TRANSFERT = "remove_transfert";
74      private static final String MARK_STATUT_SIGNE = "signe";
75      // Views
76      private static final String VIEW_INSTRUCTION_DEMANDE_OCCUPATION = "instructionDemandeOccupation";
77  
78      // Properties for page titles
79      private static final String PROPERTY_PAGE_TITLE_INSTRUCTION_DEMANDE_OCCUPATION = "ctv.instructionDemandeOccupation.pageTitle";
80  
81      // templates
82      private static final String TEMPLATE_INSTRUCTION = "/admin/plugins/ctv/instructionDemandeOccupation.html";
83  
84      // Markers
85      private static final String MARK_NUM_DEMANDE = "numDemande";
86  
87      // Parameter
88      private static final String PARAMETER_NUM_DEMANDE = "num_demande";
89      private static final String MARK_ENABLE_SIGNER_BUTTON = "enable_signer_button";
90  
91      private IUnitService unitService = SpringContextService.getBean( IUnitService.BEAN_UNIT_SERVICE );
92  
93      protected IFormulaireDemandeOccupationBoService formulaireService = FormulaireDemandeOccupationBoServiceImpl.getInstance( );
94  
95      protected CtvUploadDemandeDocumentHandler ctvDemandeUploadHandler = SpringContextService
96              .getBean( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER );
97  
98      /**
99       * Vue permettant de voir les informations liées à la demande
100      *
101      * @param request
102      *            the HttpRequest
103      * @return
104      */
105     @View( value = VIEW_INSTRUCTION_DEMANDE_OCCUPATION, defaultView = true )
106     public String getInstructionDemandeOccupation( HttpServletRequest request )
107     {
108         Map<String, Object> model = getModel( );
109 
110         String numDemandeStr = request.getParameter( PARAMETER_NUM_DEMANDE );
111         Integer numDemande = null;
112         // Récupération de la demande à instruire
113         if ( !StringUtils.isNullOrEmpty( numDemandeStr ) )
114         {
115             numDemande = Integer.parseInt( numDemandeStr );
116         }
117 
118         // Si la demande à instruire est différente de la dernière, vidage
119         // des
120         // PJ
121         Integer numDemandeSession = formulaireService.getNumDemande( request );
122         if ( ( null == numDemandeSession ) || ( ( numDemande != null ) && !numDemandeSession.equals( numDemande ) ) )
123         {
124             request.getSession( ).setAttribute( CtvConstants.SESSION_NUM_DEMANDE_ID, numDemande );
125             formulaireService.removeFilesFromSession( request );
126         }
127         formulaireService.alimentationModel( request, model );
128         formulaireService.alimentationModelBO( request, model );
129 
130         DemandeReading demande = (DemandeReading) model.get( CtvConstants.DEMANDE );
131         boolean canAccessDemande = canAccessDemande( demande, request );
132         model.put( "canAccessDemande", canAccessDemande );
133 
134         if ( !TypeDemande.OCCUPATION.equals( demande.getDemandeType( ) ) )
135         {
136             return redirect( request, "../../tableauDeBord/tableauDeBord.jsp" );
137         }
138 
139         model.put( MARK_PATH_TABLEAU_DE_BORD, PATH_TABLEAU_DE_BORD );
140         model.put( MARK_PAGE_TITLE, TABLEAU_BORD_PAGE_TITLE );
141         model.put( MARK_ETAPE, CtvConstants.ETAPE_INSTRUCTION );
142         model.put( Document.ROC_PV + "_listUploadedFiles", ctvDemandeUploadHandler.getListUploadedFiles( Document.ROC_PV, request.getSession( ) ) );
143         model.put( Document.ROC_AUTRES + "_listUploadedFiles", ctvDemandeUploadHandler.getListUploadedFiles( Document.ROC_AUTRES, request.getSession( ) ) );
144         model.put( Document.JUSTIFICATIFS_CONVENTION + "_listUploadedFiles", ctvDemandeUploadHandler.getListUploadedFiles( Document.JUSTIFICATIFS_CONVENTION, request.getSession( ) ) );
145         numDemande = numDemande != null ? numDemande : numDemandeSession;
146         fillUserPossibleActions( request, model, numDemande );
147 
148         boolean isAdminFonc = ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.ADMIN_FONC, getPlugin( ), request );
149 
150         if ( ( model.get( MARK_STATUT_SIGNE ) == StatutDemande.SIGNE ) && !isAdminFonc)
151         {
152             model.put( MARK_ENABLE_SIGNER_BUTTON, true );
153         }
154         putAlerteDemandeByProfile( model, TypeProfileCtv.INSTRUCTEUR, TypeAlerteDemande.ANNULEE, numDemande, request );
155         putAlerteDemandeByProfile( model, TypeProfileCtv.INSTRUCTEUR, TypeAlerteDemande.SIGNE_INSTRUCTEUR, numDemande, request );
156         putAlerteDemandeByProfile( model, TypeProfileCtv.CONCESSIONNAIRE, TypeAlerteDemande.SIGNE_CONCESSIONNAIRE, numDemande, request );
157         putAlerteDemandeByProfile( model, TypeProfileCtv.CONCESSIONNAIRE, TypeAlerteDemande.PROLONGATION_REFUSEE, numDemande, request );
158         return getPage( PROPERTY_PAGE_TITLE_INSTRUCTION_DEMANDE_OCCUPATION, TEMPLATE_INSTRUCTION, model );
159     }
160 
161     @Action( REMOVE_TRANSFERT )
162     public String removeTransfert( HttpServletRequest request )
163     {
164         int numDemande = formulaireService.getNumDemande( request );
165         demandeService.updateDemandeTransfered( false, numDemande );
166         return redirectView( request, VIEW_INSTRUCTION_DEMANDE_OCCUPATION );
167     }
168 
169     @Action( "remove_alert" )
170     public String removeAlert( HttpServletRequest request )
171     {
172         int idAlerte = Integer.parseInt( request.getParameter( "idAlerte" ) );
173         alerteDemandeService.removeAlerte( idAlerte );
174         return redirectView( request, VIEW_INSTRUCTION_DEMANDE_OCCUPATION );
175     }
176 
177     @Action( ACTION_ABANDON )
178     public String abandon( HttpServletRequest request )
179     {
180         formulaireService.removeFilesFromSession( request );
181         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
182         if ( StringUtils.isNullOrEmpty( nextStepPath ) )
183         {
184             return redirect( request, "../demandeOccupation.jsp" );
185         }
186         return redirect( request, nextStepPath );
187     }
188 
189     private boolean canAccessDemande( DemandeReading demande, HttpServletRequest request )
190     {
191         boolean canAccessDemande = true;
192         boolean isConcessionnaire = ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.CONCESSIONNAIRE, getPlugin( ), request );
193         if ( isConcessionnaire )
194         {
195             Unit concession = unitService.getUnitUserByType( getUser( ), TypeUnit.CONCESSIONNAIRE );
196             List<Integer> concessionId = unitService.getAllUnitsIdByUnit( concession );
197             List<String> listLabelUnits = new ArrayList<String>( );
198             for ( int unitId : concessionId )
199             {
200                 Unit unit = unitService.getUnit( unitId, false );
201                 listLabelUnits.add( unit.getLabel( ) );
202             }
203             if ( !listLabelUnits.contains( demande.getRaisonSociale( ) ) )
204             {
205                 canAccessDemande = false;
206             }
207         }
208         return canAccessDemande;
209     }
210 
211 }