View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.portal.web.features;
35  
36  import fr.paris.lutece.portal.business.right.FeatureGroup;
37  import fr.paris.lutece.portal.business.right.FeatureGroupHome;
38  import fr.paris.lutece.portal.business.right.RightHome;
39  import fr.paris.lutece.portal.business.user.AdminUser;
40  import fr.paris.lutece.portal.service.dashboard.admin.AdminDashboardComponent;
41  import fr.paris.lutece.portal.service.i18n.I18nService;
42  import fr.paris.lutece.portal.service.security.SecurityTokenService;
43  import fr.paris.lutece.portal.service.template.AppTemplateService;
44  import fr.paris.lutece.portal.web.dashboard.AdminDashboardJspBean;
45  import fr.paris.lutece.util.ReferenceList;
46  import fr.paris.lutece.util.html.HtmlTemplate;
47  import java.util.ArrayList;
48  import java.util.Collection;
49  import java.util.HashMap;
50  import java.util.List;
51  import java.util.Locale;
52  import java.util.Map;
53  import javax.servlet.http.HttpServletRequest;
54  
55  /**
56   * FeaturesAdminDashboardComponent
57   */
58  public class FeaturesAdminDashboardComponent extends AdminDashboardComponent
59  {
60      private static final String TEMPLATE_FEATURES_ADMINDASHBOARD = "admin/dashboard/admin/features_admindashboard.html";
61      private static final String MARK_FEATURE_NO_GROUP = "no_group";
62      private static final String MARK_FEATURE_GROUP_LIST = "feature_group_list";
63      private static final String MARK_FEATURE_GROUP = "feature_group";
64      private static final String MARK_GROUPS_LIST = "groups_list";
65      private static final String MARK_ORDER_LIST = "order_list";
66      private static final String MARK_EXTERNAL_FEATURES_LIST = "external_features_list";
67      private static final String MARK_RIGHT_LIST = "feature_list";
68      private static final String MARK_ORDER_IS_OK = "order_list_state";
69  
70      private static final String NO_GROUP_DESCRIPTION = "portal.features.nogroup.description";
71      private static final String NO_GROUP_LABEL = "portal.features.nogroup.label";
72      private static final int NO_GROUP_ORDER = 0;
73      private static final String NO_GROUP_ID = null;
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public String getDashboardData( AdminUser user, HttpServletRequest request )
80      {
81          List<FeatureGroup> listGroups = FeatureGroupHome.getFeatureGroupsList( );
82          Map<String, Object> model = new HashMap<>( );
83          model.put( MARK_FEATURE_NO_GROUP, getNoGroup( user.getLocale( ) ) );
84          model.put( MARK_FEATURE_GROUP_LIST, getRefListFeatureGroups( user.getLocale( ) ) );
85          model.put( MARK_ORDER_LIST, getOrderRefList( ) );
86          model.put( MARK_GROUPS_LIST, listGroups );
87          model.put( MARK_EXTERNAL_FEATURES_LIST, RightHome.getExternalRightList( ) );
88          model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) );
89  
90          HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_FEATURES_ADMINDASHBOARD, user.getLocale( ), model );
91  
92          return template.getHtml( );
93  
94      }
95  
96      /**
97       * Generate an HTML combo of available group order
98       * 
99       * @return The reference list of orders
100      */
101     private ReferenceList getOrderRefList( )
102     {
103         int nGroupsCount = FeatureGroupHome.getFeatureGroupsCount( );
104         ReferenceListt.html#ReferenceList">ReferenceList listOrders = new ReferenceList( );
105 
106         for ( int i = 0; i < nGroupsCount; i++ )
107         {
108             listOrders.addItem( i + 1, Integer.toString( i + 1 ) );
109         }
110 
111         return listOrders;
112     }
113 
114     /**
115      * Generate a combo containing all available groups
116      *
117      * @param locale
118      *            The locale
119      * @return the reference list of feature groups
120      */
121     private Map<String, Object> getNoGroup( Locale locale )
122     {
123         FeatureGroupss/right/FeatureGroup.html#FeatureGroup">FeatureGroup noGroup = new FeatureGroup( );
124         noGroup.setLocale( locale );
125         noGroup.setId( NO_GROUP_ID );
126         noGroup.setOrder( NO_GROUP_ORDER );
127         noGroup.setDescriptionKey( NO_GROUP_DESCRIPTION );
128         noGroup.setLabelKey( NO_GROUP_LABEL );
129 
130         Map<String, Object> groupMap = new HashMap<>( );
131         groupMap.put( MARK_FEATURE_GROUP, noGroup );
132         groupMap.put( MARK_RIGHT_LIST, I18nService.localizeCollection( RightHome.getRightsList( noGroup.getId( ) ), locale ) );
133         groupMap.put( MARK_ORDER_IS_OK, RightHome.checkFeatureOrders( noGroup.getId( ) ) );
134 
135         return groupMap;
136     }
137 
138     /**
139      * Generate a combo containing all available groups
140      *
141      * @param locale
142      *            The locale
143      * @return the reference list of feature groups
144      */
145     private Collection<HashMap<String, Object>> getRefListFeatureGroups( Locale locale )
146     {
147         Collection<HashMap<String, Object>> colGroupMap = new ArrayList<>( );
148         Collection<FeatureGroup> colGroups = FeatureGroupHome.getFeatureGroupsList( );
149 
150         for ( FeatureGroup fg : colGroups )
151         {
152             HashMap<String, Object> groupMap = new HashMap<>( );
153             groupMap.put( MARK_FEATURE_GROUP, fg );
154             groupMap.put( MARK_RIGHT_LIST, I18nService.localizeCollection( RightHome.getRightsList( fg.getId( ) ), locale ) );
155             groupMap.put( MARK_ORDER_IS_OK, RightHome.checkFeatureOrders( fg.getId( ) ) );
156             colGroupMap.add( groupMap );
157         }
158 
159         return colGroupMap;
160     }
161 
162 }