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.intention;
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.FormulaireDemandeIntentionServiceImpl;
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   * Copyright (c) 2002-2016, Mairie de Paris
64   * All rights reserved.
65   *
66   * Redistribution and use in source and binary forms, with or without
67   * modification, are permitted provided that the following conditions
68   * are met:
69   *
70   *  1. Redistributions of source code must retain the above copyright notice
71   *     and the following disclaimer.
72   *
73   *  2. Redistributions in binary form must reproduce the above copyright notice
74   *     and the following disclaimer in the documentation and/or other materials
75   *     provided with the distribution.
76   *
77   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
78   *     contributors may be used to endorse or promote products derived from
79   *     this software without specific prior written permission.
80   *
81   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
82   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
85   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
86   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
87   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
88   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
89   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
90   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
91   * POSSIBILITY OF SUCH DAMAGE.
92   *
93   * License 1.0
94   */
95  
96  /**
97   * This class provides the user interface to accueil jsp
98   */
99  
100 @Controller( controllerJsp = "demandeIntention.jsp", controllerPath = "jsp/admin/plugins/ctv/gestionDemandes/intention", right = "DEMANDE_INTENTION" )
101 public class DemandeIntentionJspBean extends AbstractCtvJspBean
102 {
103 
104     private static final String REMOVE_TRANSFERT = "remove_transfert";
105 
106     // Views
107     private static final String VIEW_INSTRUCTION_DEMANDE_INTENTION = "instructionDemandeIntention";
108 
109     // Properties for page titles
110     private static final String PROPERTY_PAGE_TITLE_INSTRUCTION_DEMANDE_INTENTION = "ctv.instructionDemandeIntention.pageTitle";
111 
112     // templates
113     private static final String TEMPLATE_INSTRUCTION = "/admin/plugins/ctv/instructionDemandeIntention.html";
114 
115     // Markers
116     private static final String MARK_NUM_DEMANDE = "numDemande";
117 
118     // Parameter
119     private static final String PARAMETER_NUM_DEMANDE = "num_demande";
120 
121     protected IFormulaire formulaireIntentionService = FormulaireDemandeIntentionServiceImpl.getInstance( );
122 
123     private IUnitService unitService = SpringContextService.getBean( IUnitService.BEAN_UNIT_SERVICE );
124     
125     protected CtvUploadDemandeDocumentHandler ctvDemandeUploadHandler = SpringContextService
126             .getBean( CtvUploadDemandeDocumentHandler.CTV_DEMANDE_UPLOAD_HANDLER );
127 
128     /**
129      * Vue permettant de voir les informations liées à la demande
130      *
131      * @param request
132      *            the HttpRequest
133      * @return
134      */
135     @View( value = VIEW_INSTRUCTION_DEMANDE_INTENTION, defaultView = true )
136     public String getInstructionDemandeIntention( HttpServletRequest request )
137     {
138         formulaireIntentionService.removeFilesFromSession( request );
139         Map<String, Object> model = getModel( );
140         DemandeReading demande;
141         String numDemandeStr = request.getParameter( PARAMETER_NUM_DEMANDE );
142         Integer numDemande = null;
143         // Récupération de la demande à instruire
144         if ( !StringUtils.isNullOrEmpty( numDemandeStr ) )
145         {
146             numDemande = Integer.parseInt( numDemandeStr );
147             demande = demandeService.findByNumDemande( numDemande );
148         }
149         else
150         {
151             demande = (DemandeReading) model.get( CtvConstants.DEMANDE );
152         }
153 
154         // Si la demande à instruire est différente de la dernière, vidage
155         // des
156         // PJ
157         Integer numDemandeSession = formulaireIntentionService.getNumDemande( request );
158         if ( ( null != numDemandeSession ) && ( demande == null ) )
159         {
160             demande = demandeService.findByNumDemande( numDemandeSession );
161         }
162 
163         if ( ( null == numDemandeSession ) || ( ( numDemande != null ) && !numDemandeSession.equals( numDemande ) ) )
164         {
165             request.getSession( ).setAttribute( CtvConstants.SESSION_NUM_DEMANDE_ID, numDemande );
166         }
167         formulaireIntentionService.alimentationModel( request, model );
168         TypeDemande demandeType = demande.getDemandeType( );
169         model.put( MARK_PATH_TABLEAU_DE_BORD, PATH_TABLEAU_DE_BORD );
170         model.put( MARK_PAGE_TITLE, TABLEAU_BORD_PAGE_TITLE );
171 
172         model.put( MARK_ETAPE, CtvConstants.ETAPE_INSTRUCTION );
173         numDemande = numDemande != null ? numDemande : numDemandeSession;
174         model.put( MARK_NUM_DEMANDE, numDemande );
175         model.put( Document.JUSTIFICATIFS_CONVENTION + "_listUploadedFiles", ctvDemandeUploadHandler.getListUploadedFiles( Document.JUSTIFICATIFS_CONVENTION, request.getSession( ) ) );
176         fillUserPossibleActions( request, model, numDemande );
177 
178         putAlerteDemandeByProfile( model, TypeProfileCtv.INSTRUCTEUR, TypeAlerteDemande.ANNULEE, numDemande, request );
179         putAlerteDemandeByProfile( model, TypeProfileCtv.CONCESSIONNAIRE, TypeAlerteDemande.PROLONGATION_REFUSEE, numDemande, request );
180 
181         if ( TypeDemande.BARRAGE_US.equals( demandeType ) || TypeDemande.TRAVAUX_IMPERATIFS_NON_PROGRAMMES.equals( demandeType ) )
182         {
183             putAlerteDemandeByProfile( model, TypeProfileCtv.SUPERVISEUR_1, TypeAlerteDemande.SUPERVISEUR, numDemande, request );
184         }
185 
186         boolean canAccessDemande = canAccessDemande( demande, request );
187         model.put( "canAccessDemande", canAccessDemande );
188 
189         if ( !demandeType.isIntentionType( ) )
190         {
191             return redirect( request, "../../tableauDeBord/tableauDeBord.jsp" );
192         }
193         else
194             if ( StatutDemande.BROUILLON.equals( demande.getDemandeStatut( ) ) )
195             {
196                 return redirect( request, "etapes/demandeur.jsp" );
197             }
198 
199         return getPage( PROPERTY_PAGE_TITLE_INSTRUCTION_DEMANDE_INTENTION, TEMPLATE_INSTRUCTION, model );
200     }
201 
202     @Action( REMOVE_TRANSFERT )
203     public String removeTransfert( HttpServletRequest request )
204     {
205         int numDemande = formulaireIntentionService.getNumDemande( request );
206         demandeService.updateDemandeTransfered( false, numDemande );
207         return redirectView( request, VIEW_INSTRUCTION_DEMANDE_INTENTION );
208     }
209 
210     @Action( "remove_alert" )
211     public String removeAlert( HttpServletRequest request )
212     {
213         int idAlerte = Integer.parseInt( request.getParameter( "idAlerte" ) );
214         alerteDemandeService.removeAlerte( idAlerte );
215         return redirectView( request, VIEW_INSTRUCTION_DEMANDE_INTENTION );
216     }
217 
218     @Action( ACTION_ABANDON )
219     public String abandon( HttpServletRequest request )
220     {
221         formulaireIntentionService.removeFilesFromSession( request );
222         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
223         if ( StringUtils.isNullOrEmpty( nextStepPath ) )
224         {
225             return redirect( request, "../demandeIntention.jsp" );
226         }
227         return redirect( request, nextStepPath );
228     }
229 
230     private boolean canAccessDemande( DemandeReading demande, HttpServletRequest request )
231     {
232         boolean canAccessDemande = true;
233         boolean isConcessionnaire = ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.CONCESSIONNAIRE, getPlugin( ), request );
234         String currentProfile = ctvProfileService.getCurrentProfile( getUser( ), getPlugin( ), request );
235         if ( isConcessionnaire )
236         {
237             Unit concession = unitService.getUnitUserByType( getUser( ), TypeUnit.CONCESSIONNAIRE );
238             List<Integer> concessionId = unitService.getAllUnitsIdByUnit( concession );
239             List<String> listLabelUnits = new ArrayList<String>( );
240             for ( int unitId : concessionId )
241             {
242                 Unit unit = unitService.getUnit( unitId, false );
243                 listLabelUnits.add( unit.getLabel( ) );
244             }
245             if ( !listLabelUnits.contains( demande.getRaisonSociale( ) ) )
246             {
247                 canAccessDemande = false;
248             }
249         }
250         if ( !ctvProfileService.hasRight( currentProfile, "DEMANDE_INTENTION", getPlugin( ) ) )
251         {
252             canAccessDemande = false;
253         }
254 
255         return canAccessDemande;
256     }
257 }