View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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 java.io.IOException;
37  import java.util.ArrayList;
38  import java.util.Date;
39  import java.util.List;
40  import java.util.Map;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  import org.apache.commons.beanutils.BeanUtilsBean;
46  import org.apache.commons.beanutils.ConvertUtilsBean;
47  
48  import fr.paris.lutece.plugins.ctv.bo.AlerteDemande;
49  import fr.paris.lutece.plugins.ctv.bo.AlerteDemande.TypeAlerteDemande;
50  import fr.paris.lutece.plugins.ctv.bo.InfosSuperviseur;
51  import fr.paris.lutece.plugins.ctv.business.IActionProfilStatutDemandeService;
52  import fr.paris.lutece.plugins.ctv.business.IAlerteDemandeService;
53  import fr.paris.lutece.plugins.ctv.business.ICoucheService;
54  import fr.paris.lutece.plugins.ctv.business.IDemandeService;
55  import fr.paris.lutece.plugins.ctv.business.IEmpriseService;
56  import fr.paris.lutece.plugins.ctv.business.IInfosSuperviseurService;
57  import fr.paris.lutece.plugins.ctv.constant.CtvAction;
58  import fr.paris.lutece.plugins.ctv.constant.StatutDemande;
59  import fr.paris.lutece.plugins.ctv.constant.StatutTaxationDemande;
60  import fr.paris.lutece.plugins.ctv.constant.TypeProfileCtv;
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.service.CtvProfileService;
65  import fr.paris.lutece.plugins.ctv.service.ICtvProfileService;
66  import fr.paris.lutece.plugins.ctv.view.object.CountEmpriseViewObject;
67  import fr.paris.lutece.plugins.ctv.view.object.DatasExtractExcel;
68  import fr.paris.lutece.plugins.ctv.view.object.DemandeReading;
69  import fr.paris.lutece.plugins.ctv.view.object.SearchEmprisesViewObject;
70  import fr.paris.lutece.plugins.unittree.service.unit.IUnitService;
71  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
72  import fr.paris.lutece.portal.service.spring.SpringContextService;
73  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
74  
75  /**
76   * AbstractctvJspBean JSP Bean abstract class for JSP Bean
77   */
78  public abstract class AbstractCtvJspBean extends MVCAdminJspBean
79  {
80  
81      private static final long serialVersionUID = 7051304353338238501L;
82  
83      // Marks
84      protected static final String MARK_ETAPE = "etape";
85      protected static final String MARK_PATH_DEMANDE_OCCUPATION = "link_demande_occupation";
86      protected static final String MARK_PATH_DEMANDE_INTENTION = "link_demande_intention";
87      protected static final String MARK_PATH_TABLEAU_DE_BORD = "link_tableau_de_bord";
88      protected static final String MARK_PATH_DEMANDE_INTERVENTION = "link_demande_intervention";
89      protected static final String MARK_PAGE_TITLE = "page_title";
90      private static final String MARK_IS_BO = "is_bo";
91      protected static final String MARK_EDIT_MODE = "edit_mode";
92      protected static final String MARK_TARGET_VIEW = "target_view";
93      protected static final String MARK_CREATE = "CREATE";
94      private static final String MARK_LISTE_DEMANDES_TO_EXCEL = "list_demandes_to_excel";
95      private static final String MARK_DISPLAY_SIGNER_BUTTON = "display_signer_button";
96      private static final String MARK_ENABLE_SIGNER_BUTTON = "enable_signer_button";
97      private static final String MARK_STATUT_SIGNE = "signe";
98      private static final String RIGHT_RECUPERER_DEMANDE = "can_recuperer_demande";
99  
100     // Parameters
101     protected static final String PARAMETER_NEXT_STEP = "nextStep";
102 
103     // Titles
104     protected static final String TABLEAU_BORD_PAGE_TITLE = "ctv.tableauDeBord.pageTitle";
105     protected static final String GESTION_EMAIL_PAGE_TITLE = "ctv.gestion.email.pageTitle";
106     protected static final String GESTION_CONVENTION_PAGE_TITLE = "ctv.gestion.convention.pageTitle";
107     protected static final String GESTION_COUCHE_PAGE_TITLE = "ctv.gestion.couche.pageTitle";
108     protected static final String GESTION_COMPTES_PAGE_TITLE = "ctv.gestion.comptes.pageTitle";
109     // A SETTER
110     protected static final String DEMANDE_INTENTION_PAGE_TITLE = "ctv.demande.intention.pageTitle";
111 
112     // JSP
113     // Templates
114     protected static final String TEMPLATE_TABLEAU_DE_BORD = "/admin/plugins/ctv/tableauDeBord/tableau_de_bord.html";
115     protected static final String TEMPLATE_TABLEAU_DE_BORD_CONCESSIONNAIRE = "/admin/plugins/ctv/tableauDeBord/tableau_de_bord_concessionnaire.html";
116     protected static final String TEMPLATE_GESTION_EMAIL = "/admin/plugins/ctv/gestionEmail/gestionEmail.html";
117     protected static final String TEMPLATE_LIST_EMAIL = "/admin/plugins/ctv/gestionEmail/listEmail.html";
118     protected static final String TEMPLATE_LIST_CONFERENCE_COORDINATION = "/admin/plugins/ctv/conferenceCoordination/gestionConferenceCoordination.html";
119     protected static final String TEMPLATE_MANAGE_CONFERENCE_COORDINATION = "/admin/plugins/ctv/conferenceCoordination/editionConferenceCoordination.html";
120     protected static final String TEMPLATE_MENU_CONVENTION = "/admin/plugins/ctv/gestionConvention/menuConvention.html";
121     protected static final String TEMPLATE_MANAGE_CONVENTION = "/admin/plugins/ctv/gestionConvention/gestionConvention.html";
122     protected static final String TEMPLATE_MENU_COUCHE = "/admin/plugins/ctv/gestionCouche/menuCouche.html";
123     protected static final String TEMPLATE_MANAGE_COUCHE = "/admin/plugins/ctv/gestionCouche/gestionCouche.html";
124     protected static final String TEMPLATE_MANAGE_COMPTES = "/admin/plugins/ctv/gestionComptes/gestionComptes.html";
125     protected static final String TEMPLATE_HISTORIQUE_COMPTES = "/admin/plugins/ctv/gestionComptes/historiqueComptes.html";
126 
127     // Path
128     protected static final String PATH_DEMANDE_OCCUPATION = "jsp/admin/plugins/ctv/gestionDemandes/occupation/demandeOccupation.jsp";
129     protected static final String PATH_DEMANDE_INTENTION = "jsp/admin/plugins/ctv/gestionDemandes/intention/demandeIntention.jsp";
130     protected static final String PATH_DEMANDE_INTERVENTION = "jsp/admin/plugins/ctv/gestionDemandes/intervention/demandeIntervention.jsp";
131 
132     protected static final String PATH_TABLEAU_DE_BORD = "jsp/admin/plugins/ctv/tableauDeBord/tableauDeBord.jsp";
133 
134     protected static final String PATH_PAGE_PROJET = "jsp/admin/plugins/ctv/projet.jsp";
135 
136     // VUES
137     protected static final String VIEW_TABLEAU_DE_BORD = "tableauDeBord";
138     protected static final String VIEW_LIST_EMAIL = "listEmail";
139     protected static final String VIEW_LIST_CONFERENCE_COORDINATION = "gestionConferenceCoordination";
140 
141     // ACTION
142     protected static final String ACTION_SAVE = "save";
143     protected static final String ACTION_UPDATE = "update";
144     protected static final String ACTION_NEXT = "next";
145     protected static final String ACTION_COMPLETE_LATER = "complete_later";
146     protected static final String ACTION_DISCARD = "discard";
147     protected static final String ACTION_ABANDON = "abandon";
148 
149     protected static final String ACTION_ABANDON_AND_GO_PREVIOUS_EMPRISE = "abandonAndGoPreviousEmprise";
150     protected static final String ACTION_SAVE_AND_GO_PREVIOUS_EMPRISE = "saveAndGoPreviousEmprise";
151 
152     protected static final String ACTION_ABANDON_AND_GO_NEXT_EMPRISE = "abandonAndGoNextEmprise";
153     protected static final String ACTION_SAVE_AND_GO_NEXT_EMPRISE = "saveAndGoNextEmprise";
154 
155     // Services
156     protected IDemandeService demandeService = SpringContextService.getBean( "demandeService" );
157     protected ICtvProfileService ctvProfileService = CtvProfileService.getInstance( );
158     private static final IActionProfilStatutDemandeService actionProfilStatutService = SpringContextService.getBean( "actionProfilStatutDemandeService" );
159     private IUnitService unitService = SpringContextService.getBean( IUnitService.BEAN_UNIT_SERVICE );
160     protected static IEmpriseService empriseService = SpringContextService.getBean( "empriseService" );
161     protected static IAlerteDemandeService alerteDemandeService = SpringContextService.getBean( "alerteDemandeService" );
162     protected static final IInfosSuperviseurService infosSuperviseurService = SpringContextService.getBean( "infosSuperviseurService" );
163     protected ICoucheService coucheService = SpringContextService.getBean( "coucheService" );
164 
165     /**
166      * Liste des actions possibles selon le statut de la demande, pour l'utilisateur connecté
167      */
168 
169     // Constante
170 
171     public AbstractCtvJspBean( )
172     {
173         super( );
174 
175         // Enregistrement des Converters
176         ConvertUtilsBean convertUtilsBean = BeanUtilsBean.getInstance( ).getConvertUtils( );
177         convertUtilsBean.register( new CustomDateConverter( ), Date.class );
178         convertUtilsBean.register( new EnumConverter( ), StatutTaxationDemande.class );
179 
180     }
181 
182     /**
183      * Get a model Object filled with default values
184      *
185      * @return The model
186      */
187     @Override
188     protected Map<String, Object> getModel( )
189     {
190         Map<String, Object> model = super.getModel( );
191         model.put( MARK_IS_BO, true );
192 
193         return model;
194     }
195 
196     private List<CtvAction> getUserActions( DemandeReading demande, HttpServletRequest request )
197     {
198         List<String> userProfilesKeys = ctvProfileService.getUserProfilesKeys( getUser( ), getPlugin( ), request );
199         return actionProfilStatutService.getActionsByStatusAndDemandeType( demande, userProfilesKeys );
200     }
201 
202     protected DemandeReading fillInfosFilDArianeProjet( Map<String, Object> model, String numProjet )
203     {
204         DemandeReading demandeOrigine = demandeService.findOrigineDemandeByNumProjet( numProjet );
205         CountEmpriseViewObject empriseCountViewObject = empriseService.countEmpriseTransform( demandeOrigine.getDemandeNumero( ) );
206         model.put( "empriseCount", empriseCountViewObject );
207         model.put( "demandeOrigine", demandeOrigine );
208         return demandeOrigine;
209     }
210 
211     /**
212      * Ne dois être utilisé qu'après que le model ait été rempli avec le statut de la demande
213      *
214      * @param numDemande
215      */
216     protected void fillUserPossibleActions( HttpServletRequest request, Map<String, Object> model, int numDemande )
217     {
218         DemandeReading demande = demandeService.findByNumDemande( numDemande );
219         int demandeSectorId = demande.getDemandeSectorId( );
220         List<CtvAction> possibleActions = getUserActions( demande, request );
221         boolean isInstructeur = ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.INSTRUCTEUR, getPlugin( ), request );
222         boolean isSuperviseur1 = ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.SUPERVISEUR_1, getPlugin( ), request );
223         boolean isSuperviseur2 = ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.SUPERVISEUR_2, getPlugin( ), request )
224                 || ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.ADMIN_FONC, getPlugin( ), request );
225         boolean isSameSubdivision = unitService.hasUserSameSubdivisionBySector( getUser( ).getUserId( ), demandeSectorId );
226         boolean isSameSection = unitService.hasUserSameSectionBySector( getUser( ).getUserId( ), demandeSectorId );
227         for ( CtvAction action : possibleActions )
228         {
229             boolean isModify = CtvAction.ACTION_MODIFY_STEP.equals( action ) || CtvAction.CAN_WRITE.equals( action )
230                     || CtvAction.INSTRUCTION_BO_PROJECT_MODIFY.equals( action );
231             if ( isModify )
232             {
233                 if ( ( isInstructeur || isSuperviseur1 ) && !isSameSubdivision )
234                 {
235                     continue;
236                 }
237                 else
238                     if ( isSuperviseur2 && !isSameSection )
239                     {
240                         continue;
241                     }
242             }
243             model.put( action.name( ), true );
244         }
245 
246         if ( ( ( isInstructeur || isSuperviseur1 ) && isSameSubdivision ) || ( isSuperviseur2 && isSameSection ) )
247         {
248             model.put( RIGHT_RECUPERER_DEMANDE, true );
249         }
250         if ( ( isSuperviseur1 && isSameSubdivision ) || ( isSuperviseur2 && isSameSection ) )
251         {
252             InfosSuperviseur infosSuperviseur = infosSuperviseurService.findByNumDemande( numDemande );
253             boolean hasAutorisation = ( infosSuperviseur != null ) && ( ( infosSuperviseur.getInfosSuperviseurTypeAutorisation( ) != null )
254                     || ( infosSuperviseur.getInfosSuperviseurAutorisation( ) != null ) );
255             model.remove( MARK_ENABLE_SIGNER_BUTTON );
256             if ( hasAutorisation )
257             {
258                 model.put( MARK_STATUT_SIGNE, StatutDemande.SIGNE );
259             }
260             model.put( MARK_DISPLAY_SIGNER_BUTTON, true );
261         }
262     }
263 
264     protected void fillLastUser( int numDemande, HttpServletRequest request )
265     {
266         boolean isInstructeur = ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.INSTRUCTEUR, getPlugin( ), request );
267         boolean isSuperviseur1 = ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.SUPERVISEUR_1, getPlugin( ), request );
268         boolean isSuperviseur2 = ctvProfileService.hasUserProfile( getUser( ), TypeProfileCtv.SUPERVISEUR_2, getPlugin( ), request );
269         if ( isInstructeur && !isSuperviseur1 && !isSuperviseur2 )
270         {
271             demandeService.setLastInstructeur( getUser( ).getUserId( ), numDemande );
272         }
273         else
274             if ( isSuperviseur1 || isSuperviseur2 )
275             {
276                 demandeService.setLastSuperviseur( getUser( ).getUserId( ), numDemande );
277             }
278     }
279 
280     public String processController( HttpServletRequest request, HttpServletResponse response, boolean ctvProcess ) throws AccessDeniedException, IOException
281     {
282         if ( request.getSession( ).getAttribute( CtvConstants.SESSION_NUM_DEMANDE_ID ) == null )
283         {
284             response.sendRedirect( getAbsolutePath( request, PATH_TABLEAU_DE_BORD ) );
285             return null;
286         }
287         return super.processController( request, response );
288     }
289 
290     protected void putAlerteDemandeByProfile( Map<String, Object> model, TypeProfileCtv profile, TypeAlerteDemande typeAlerte, int numDemande,
291             HttpServletRequest request )
292     {
293         boolean hasProfile = ctvProfileService.hasUserProfile( getUser( ), profile, getPlugin( ), request );
294         if ( hasProfile )
295         {
296             AlerteDemande alerte = alerteDemandeService.hasAlerte( typeAlerte, numDemande );
297             if ( alerte != null )
298             {
299                 model.put( "alerteDemande", alerte );
300                 model.put( "demandeNumero", numDemande );
301             }
302         }
303     }
304 
305     public void insertListDemandesForExcel( Map<String, Object> model, List<SearchEmprisesViewObject> listeDemandes )
306     {
307         if ( ( listeDemandes != null ) && !listeDemandes.isEmpty( ) )
308         {
309             List<Integer> idEmprises = new ArrayList<>( );
310             for ( SearchEmprisesViewObject demande : listeDemandes )
311             {
312                 String EmpriseId = demande.getEmpriseId( );
313                 if ( EmpriseId != null )
314                 {
315                     idEmprises.add( Integer.parseInt( EmpriseId ) );
316                 }
317             }
318             if ( !idEmprises.isEmpty( ) )
319             {
320                 List<DatasExtractExcel> listeDemandesToExcel = demandeService.getListDemandesToExcelWithOthersByDemandesId( idEmprises );
321                 model.put( MARK_LISTE_DEMANDES_TO_EXCEL, listeDemandesToExcel );
322             }
323         }
324     }
325 
326     protected String getAbsolutePath( HttpServletRequest request, String path )
327     {
328         return request.getContextPath( ) + "/" + path;
329     }
330 
331 }