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.tdb;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import com.google.gson.Gson;
44  import com.google.gson.GsonBuilder;
45  
46  import fr.paris.lutece.plugins.ctv.bo.controller.demandes.AbstractCtvJspBean;
47  import fr.paris.lutece.plugins.ctv.constant.TypeProfileCtv;
48  import fr.paris.lutece.plugins.ctv.service.ITableauDeBordControllerService;
49  import fr.paris.lutece.plugins.ctv.service.TableauDeBordConcessionnaireControllerService;
50  import fr.paris.lutece.plugins.ctv.service.TableauDeBordInstructionControllerService;
51  import fr.paris.lutece.portal.business.user.AdminUser;
52  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
53  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
54  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
55  
56  @Controller( controllerPath = "jsp/admin/plugins/ctv/tableauDeBord", controllerJsp = "tableauDeBord.jsp", right = "TABLEAU_DE_BORD" )
57  public class TableauDeBordJspBean extends AbstractCtvJspBean
58  {
59  
60      private static final long serialVersionUID = 7395395576430410733L;
61  
62      private static ITableauDeBordControllerService tableauDeBordInstructionControllerService = TableauDeBordInstructionControllerService.getInstance( );
63  
64      private static ITableauDeBordControllerService tableauDeBordConcessionnaireControllerService = TableauDeBordConcessionnaireControllerService.getInstance( );
65  
66      @View( defaultView = true, value = VIEW_TABLEAU_DE_BORD )
67      public String getTableauDeBord( HttpServletRequest request )
68      {
69          String selectedProfileInRequest = request.getParameter( "profile_default" );
70          if ( selectedProfileInRequest == null && request.getSession( ).getAttribute( "profile_default" ) != null )
71          {
72              selectedProfileInRequest = ( (TypeProfileCtv) request.getSession( ).getAttribute( "profile_default" ) ).getKey( );
73          }
74          AdminUser user = getUser( );
75          String selectedProfile = "";
76          List<String> userProfiles = ctvProfileService.getUserProfilesKeys( getUser( ), getPlugin( ) );
77          boolean isConcessionnaire = false;
78          selectedProfile = selectDefaultProfileByPriority( request, userProfiles );
79  
80          if ( selectedProfileInRequest != null && userProfiles.contains( selectedProfileInRequest ) )
81          {
82              selectedProfile = selectedProfileInRequest;
83          }
84  
85          request.getSession( ).setAttribute( "profile_default", TypeProfileCtv.getEnum( selectedProfile ) );
86  
87          isConcessionnaire = TypeProfileCtv.getEnum( selectedProfile ) == TypeProfileCtv.CONCESSIONNAIRE;
88          if ( isConcessionnaire )
89          {
90              return tableauDeBordConcessionnaireControllerService.getTableauDeBord( request, user );
91          }
92          else
93          {
94              return tableauDeBordInstructionControllerService.getTableauDeBord( request, user );
95          }
96  
97      }
98  
99      @Action( "getProfiles" )
100     public String getProfiles( HttpServletRequest request )
101     {
102         List<String> profiles = ctvProfileService.getUserProfilesKeys( getUser( ), getPlugin( ) );
103 
104         TypeProfileCtv selectedProfileInSession = (TypeProfileCtv) request.getSession( ).getAttribute( "profile_default" );
105         if ( selectedProfileInSession == null )
106         {
107             selectedProfileInSession = TypeProfileCtv.getEnum( request.getParameter( "profile_default" ) );
108         }
109 
110         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>( );
111         if ( profiles.size( ) > 1 )
112         {
113 
114             for ( String profile : profiles )
115             {
116                 Map<String, Object> map = new HashMap<String, Object>( );
117                 map.put( "key", profile );
118                 if ( profile.equals( selectedProfileInSession.getKey( ) ) )
119                 {
120                     map.put( "selected", true );
121                 }
122                 list.add( map );
123             }
124         }
125 
126         GsonBuilder gsonBuilder = new GsonBuilder( );
127         Gson gson = gsonBuilder.create( );
128         return gson.toJson( list );
129     }
130 
131     private String selectDefaultProfileByPriority( HttpServletRequest request, List<String> userProfiles )
132     {
133         String strSelectedProfile = "";
134         if ( userProfiles.size( ) > 1 )
135         {
136             String selectedProfileInRequest = request.getParameter( "profile_default" );
137             TypeProfileCtv selectedProfil = null;
138             if ( selectedProfileInRequest == null
139                     || ( selectedProfileInRequest != null && !userProfiles.contains( TypeProfileCtv.getEnum( selectedProfileInRequest ) ) ) )
140             {
141 
142                 if ( userProfiles.contains( TypeProfileCtv.ADMIN_FONC.getKey( ) ) )
143                 {
144                     selectedProfil = TypeProfileCtv.ADMIN_FONC;
145                 }
146                 else
147                     if ( userProfiles.contains( TypeProfileCtv.SUPERVISEUR_2.getKey( ) ) )
148                     {
149                         selectedProfil = TypeProfileCtv.SUPERVISEUR_2;
150                     }
151                     else
152                         if ( userProfiles.contains( TypeProfileCtv.SUPERVISEUR_1.getKey( ) ) )
153                         {
154                             selectedProfil = TypeProfileCtv.SUPERVISEUR_1;
155                         }
156                         else
157                             if ( userProfiles.contains( TypeProfileCtv.INSTRUCTEUR.getKey( ) ) )
158                             {
159                                 selectedProfil = TypeProfileCtv.INSTRUCTEUR;
160                             }
161                             else
162                                 if ( userProfiles.contains( TypeProfileCtv.CONCESSIONNAIRE.getKey( ) ) )
163                                 {
164                                     selectedProfil = TypeProfileCtv.CONCESSIONNAIRE;
165                                 }
166 
167                 if ( selectedProfil != null )
168                 {
169                     request.getSession( ).setAttribute( "profile_default", selectedProfil );
170                     strSelectedProfile = selectedProfil.getKey( );
171                 }
172             }
173         }
174         else
175         {
176             strSelectedProfile = userProfiles.get( 0 );
177         }
178 
179         return strSelectedProfile;
180     }
181 
182 }