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.Right;
39  import fr.paris.lutece.portal.business.right.RightHome;
40  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
41  import fr.paris.lutece.portal.service.message.AdminMessage;
42  import fr.paris.lutece.portal.service.message.AdminMessageService;
43  import fr.paris.lutece.portal.service.security.SecurityTokenService;
44  import fr.paris.lutece.portal.service.template.AppTemplateService;
45  import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
46  import fr.paris.lutece.portal.web.constants.Messages;
47  import fr.paris.lutece.portal.web.dashboard.AdminDashboardJspBean;
48  import fr.paris.lutece.util.ReferenceList;
49  import fr.paris.lutece.util.html.HtmlTemplate;
50  import fr.paris.lutece.util.url.UrlItem;
51  
52  import java.io.Serializable;
53  
54  import java.util.HashMap;
55  import java.util.Map;
56  
57  import javax.servlet.http.HttpServletRequest;
58  
59  import org.apache.commons.collections.CollectionUtils;
60  
61  /**
62   * FeaturesGroupJspBean
63   */
64  public class FeaturesGroupJspBean extends AdminFeaturesPageJspBean
65  {
66      public static final String RIGHT_FEATURES_MANAGEMENT = "CORE_FEATURES_MANAGEMENT";
67      private static final long serialVersionUID = -8573499137269541850L;
68      private static final String TEMPLATE_CREATE_GROUP = "admin/features/create_group.html";
69      private static final String TEMPLATE_MODIFY_GROUP = "admin/features/modify_group.html";
70      private static final String PARAMETER_GROUP_ID = "group_id";
71      private static final String PARAMETER_GROUP_NAME = "group_name";
72      private static final String PARAMETER_GROUP_DESCRIPTION = "group_description";
73      private static final String PARAMETER_GROUP_ORDER = "group_order";
74      private static final String PARAMETER_GROUP_ICON = "group_icon";
75      private static final String PARAMETER_ORDER_ID = "order_id";
76      private static final String PARAMETER_RIGHT_ID = "right_id";
77      private static final String JSP_REMOVE_GROUPS = "jsp/admin/features/DoRemoveGroup.jsp";
78      private static final String MESSAGE_CONFIRM_DELETE = "portal.features.message.confirmDeleteGroup";
79      private static final String MESSAGE_RIGHT_ALREADY_ASSIGN = "portal.features.message.rightAlreadyAssign";
80      private static final String MARK_ORDER_LIST = "order_list";
81      private static final String MARK_FEATURE_GROUP = "feature_group";
82      private static final String MARK_DEFAULT_ORDER = "order_default";
83      private static final String REGEX_ID = "^[\\d]+$";
84      private static final String ANCHOR_ADMIN_DASHBOARDS = "features_management";
85  
86      /**
87       * Dispatch a feature to a given group
88       * 
89       * @param request
90       *            The HTTP request
91       * @return The next URL to redirect after processing
92       * @throws AccessDeniedException
93       *             if the security token is invalid
94       */
95      public String doDispatchFeature( HttpServletRequest request ) throws AccessDeniedException
96      {
97          if ( !SecurityTokenService.getInstance( ).validate( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) )
98          {
99              throw new AccessDeniedException( ERROR_INVALID_TOKEN );
100         }
101         String strRightId = request.getParameter( PARAMETER_RIGHT_ID );
102         String strGroupName = request.getParameter( PARAMETER_GROUP_NAME );
103         String strOrderId = request.getParameter( PARAMETER_ORDER_ID );
104         Right right = RightHome.findByPrimaryKey( strRightId );
105         UrlItem/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( getDashboardUrl( request ) );
106 
107         if ( ( strGroupName != null ) )
108         {
109             // Set the old group as anchor
110             url.setAnchor( right.getFeatureGroup( ) );
111             right.setFeatureGroup( strGroupName.equals( "" ) ? null : strGroupName );
112         }
113 
114         if ( ( strOrderId != null ) && strOrderId.matches( REGEX_ID ) )
115         {
116             right.setOrder( Integer.parseInt( strOrderId ) );
117         }
118 
119         RightHome.update( right );
120 
121         return url.getUrl( );
122     }
123 
124     /**
125      * Dispatch a feature group
126      *
127      * @param request
128      *            The HTTP request
129      * @return The next URL to redirect after processing
130      * @throws AccessDeniedException
131      *             if the security token is invalid
132      */
133     public String doDispatchFeatureGroup( HttpServletRequest request ) throws AccessDeniedException
134     {
135         if ( !SecurityTokenService.getInstance( ).validate( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) )
136         {
137             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
138         }
139         String strGroupId = request.getParameter( PARAMETER_GROUP_ID );
140         String strOrderId = request.getParameter( PARAMETER_ORDER_ID );
141         FeatureGroup featureGroup = FeatureGroupHome.findByPrimaryKey( strGroupId );
142         UrlItem/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( getDashboardUrl( request ) );
143 
144         if ( ( strOrderId != null ) && strOrderId.matches( REGEX_ID ) )
145         {
146             featureGroup.setOrder( Integer.parseInt( strOrderId ) );
147         }
148 
149         FeatureGroupHome.update( featureGroup );
150 
151         return url.getUrl( );
152     }
153 
154     /**
155      * Reinitialize feature orders
156      * 
157      * @param request
158      *            The {@link HttpServletRequest}
159      * @return The next URL to redirect after processing
160      * @throws AccessDeniedException
161      *             if the security token is invalid
162      */
163     public String doReinitFeatures( HttpServletRequest request ) throws AccessDeniedException
164     {
165         if ( !SecurityTokenService.getInstance( ).validate( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) )
166         {
167             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
168         }
169         String strGroupId = request.getParameter( PARAMETER_GROUP_ID );
170         RightHome.reinitFeatureOrders( strGroupId );
171 
172         UrlItem/url/UrlItem.html#UrlItem">UrlItem url = new UrlItem( getDashboardUrl( request ) );
173 
174         if ( ( strGroupId != null ) )
175         {
176             url.setAnchor( strGroupId );
177         }
178 
179         return url.getUrl( );
180     }
181 
182     /**
183      * Returns the Create Group page
184      * 
185      * @param request
186      *            The HTTP request
187      * @return The HTML page
188      */
189     public String getCreateGroup( HttpServletRequest request )
190     {
191         int nCount = FeatureGroupHome.getFeatureGroupsCount( ) + 1;
192 
193         Map<String, Serializable> model = new HashMap<>( );
194         model.put( MARK_ORDER_LIST, getOrderRefList( ) );
195         model.put( MARK_DEFAULT_ORDER, String.valueOf( nCount ) );
196         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) );
197 
198         HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_CREATE_GROUP, getLocale( ), model );
199 
200         return getAdminPage( t.getHtml( ) );
201     }
202 
203     /**
204      * Returns the Modify Group page
205      * 
206      * @param request
207      *            The HTTP request
208      * @return The HTML page
209      */
210     public String getModifyGroup( HttpServletRequest request )
211     {
212         String strGroupId = request.getParameter( PARAMETER_GROUP_ID );
213 
214         FeatureGroup group = FeatureGroupHome.findByPrimaryKey( strGroupId );
215 
216         if ( group == null )
217         {
218             return getDashboardUrl( request );
219         }
220 
221         Map<String, Object> model = new HashMap<>( );
222         model.put( MARK_ORDER_LIST, getOrderRefList( ) );
223         model.put( MARK_FEATURE_GROUP, group );
224         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) );
225 
226         HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_MODIFY_GROUP, getLocale( ), model );
227 
228         return getAdminPage( t.getHtml( ) );
229     }
230 
231     /**
232      * Create the group
233      * 
234      * @param request
235      *            The HTTP request
236      * @return The next URL to redirect after processing
237      * @throws AccessDeniedException
238      *             if the security token is invalid
239      */
240     public String doCreateGroup( HttpServletRequest request ) throws AccessDeniedException
241     {
242         String strGroupId = request.getParameter( PARAMETER_GROUP_ID );
243         String strGroupName = request.getParameter( PARAMETER_GROUP_NAME );
244         String strGroupDescription = request.getParameter( PARAMETER_GROUP_DESCRIPTION );
245         String strGroupOrder = request.getParameter( PARAMETER_GROUP_ORDER );
246         String strGroupIcon = request.getParameter( PARAMETER_GROUP_ICON );
247 
248         // Mandatory fields
249         if ( strGroupId.equals( "" ) || strGroupName.equals( "" ) || strGroupDescription.equals( "" ) )
250         {
251             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
252         }
253         if ( !SecurityTokenService.getInstance( ).validate( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) )
254         {
255             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
256         }
257 
258         FeatureGroupness/right/FeatureGroup.html#FeatureGroup">FeatureGroup group = new FeatureGroup( );
259         group.setId( strGroupId );
260         group.setLabelKey( strGroupName );
261         group.setDescriptionKey( strGroupDescription );
262         group.setIcon( strGroupIcon );
263 
264         FeatureGroupHome.create( group );
265         group.setOrder( Integer.parseInt( strGroupOrder ) );
266         FeatureGroupHome.update( group );
267 
268         return getDashboardUrl( request );
269     }
270 
271     /**
272      * Modify the group
273      * 
274      * @param request
275      *            The HTTP request
276      * @return The next URL to redirect after processing
277      * @throws AccessDeniedException
278      *             is the security token is invalid
279      */
280     public String doModifyGroup( HttpServletRequest request ) throws AccessDeniedException
281     {
282         String strGroupId = request.getParameter( PARAMETER_GROUP_ID );
283         String strGroupName = request.getParameter( PARAMETER_GROUP_NAME );
284         String strGroupDescription = request.getParameter( PARAMETER_GROUP_DESCRIPTION );
285         String strGroupOrder = request.getParameter( PARAMETER_GROUP_ORDER );
286         String strGroupIcon = request.getParameter( PARAMETER_GROUP_ICON );
287 
288         // Mandatory fields
289         if ( strGroupId.equals( "" ) || strGroupName.equals( "" ) || strGroupDescription.equals( "" ) )
290         {
291             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
292         }
293         if ( !SecurityTokenService.getInstance( ).validate( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) )
294         {
295             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
296         }
297 
298         FeatureGroup group = FeatureGroupHome.findByPrimaryKey( strGroupId );
299         group.setLabelKey( strGroupName );
300         group.setDescriptionKey( strGroupDescription );
301         group.setOrder( Integer.parseInt( strGroupOrder ) );
302         group.setIcon( strGroupIcon );
303 
304         FeatureGroupHome.update( group );
305 
306         return getDashboardUrl( request );
307 
308     }
309 
310     /**
311      * Generate an HTML combo of available group order
312      * 
313      * @return The reference list of orders
314      */
315     private ReferenceList getOrderRefList( )
316     {
317         int nGroupsCount = FeatureGroupHome.getFeatureGroupsCount( );
318         ReferenceListt.html#ReferenceList">ReferenceList listOrders = new ReferenceList( );
319 
320         for ( int i = 0; i < nGroupsCount; i++ )
321         {
322             listOrders.addItem( i + 1, Integer.toString( i + 1 ) );
323         }
324 
325         return listOrders;
326     }
327 
328     /**
329      * Returns the Remove page
330      * 
331      * @param request
332      *            The HTTP request
333      * @return The HTML page
334      */
335     public String getRemoveGroup( HttpServletRequest request )
336     {
337         String strGroupId = request.getParameter( PARAMETER_GROUP_ID );
338 
339         String strUrl = JSP_REMOVE_GROUPS;
340         Map<String, Object> parameters = new HashMap<>( );
341         parameters.put( PARAMETER_GROUP_ID, strGroupId );
342         parameters.put( SecurityTokenService.PARAMETER_TOKEN,
343                 SecurityTokenService.getInstance( ).getToken( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) );
344         FeatureGroup group = FeatureGroupHome.findByPrimaryKey( strGroupId );
345         group.setLocale( getUser( ).getLocale( ) );
346 
347         Object [ ] messageArgs = {
348                 group.getLabel( )
349         };
350 
351         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE, messageArgs, null, strUrl, "", AdminMessage.TYPE_CONFIRMATION, parameters );
352     }
353 
354     /**
355      * Remove the group
356      * 
357      * @param request
358      *            The HTTP request
359      * @return The next URL to redirect after processing
360      * @throws AccessDeniedException
361      *             if the security token is invalid
362      */
363     public String doRemoveGroup( HttpServletRequest request ) throws AccessDeniedException
364     {
365         String strGroupId = request.getParameter( PARAMETER_GROUP_ID );
366 
367         if ( CollectionUtils.isNotEmpty( RightHome.getRightsList( strGroupId ) ) )
368         {
369             return AdminMessageService.getMessageUrl( request, MESSAGE_RIGHT_ALREADY_ASSIGN, AdminMessage.TYPE_STOP );
370         }
371         if ( !SecurityTokenService.getInstance( ).validate( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) )
372         {
373             throw new AccessDeniedException( ERROR_INVALID_TOKEN );
374         }
375 
376         FeatureGroupHome.remove( strGroupId );
377 
378         return getDashboardUrl( request );
379     }
380 
381     /**
382      * Returns the dashboard URL
383      * 
384      * @param request
385      *            The HTTP request
386      * @return the dashboard URL
387      */
388     private String getDashboardUrl( HttpServletRequest request )
389     {
390         return getAdminDashboardsUrl( request, ANCHOR_ADMIN_DASHBOARDS );
391     }
392 
393 }