1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
88
89
90
91
92
93
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
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
126
127
128
129
130
131
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
156
157
158
159
160
161
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
184
185
186
187
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
205
206
207
208
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
233
234
235
236
237
238
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
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
273
274
275
276
277
278
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
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
312
313
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
330
331
332
333
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
356
357
358
359
360
361
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
383
384
385
386
387
388 private String getDashboardUrl( HttpServletRequest request )
389 {
390 return getAdminDashboardsUrl( request, ANCHOR_ADMIN_DASHBOARDS );
391 }
392
393 }