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