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.style;
35
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41
42 import javax.servlet.http.HttpServletRequest;
43
44 import org.apache.commons.collections.CollectionUtils;
45
46 import fr.paris.lutece.portal.business.portlet.PortletHome;
47 import fr.paris.lutece.portal.business.portlet.PortletImpl;
48 import fr.paris.lutece.portal.business.portlet.PortletTypeHome;
49 import fr.paris.lutece.portal.business.style.Style;
50 import fr.paris.lutece.portal.business.style.StyleHome;
51 import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
52 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
53 import fr.paris.lutece.portal.service.message.AdminMessage;
54 import fr.paris.lutece.portal.service.message.AdminMessageService;
55 import fr.paris.lutece.portal.service.security.SecurityTokenService;
56 import fr.paris.lutece.portal.service.template.AppTemplateService;
57 import fr.paris.lutece.portal.service.util.AppPropertiesService;
58 import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
59 import fr.paris.lutece.portal.web.constants.Messages;
60 import fr.paris.lutece.portal.web.constants.Parameters;
61 import fr.paris.lutece.portal.web.util.LocalizedPaginator;
62 import fr.paris.lutece.util.html.AbstractPaginator;
63 import fr.paris.lutece.util.html.HtmlTemplate;
64 import fr.paris.lutece.util.sort.AttributeComparator;
65
66
67
68
69 public class StylesJspBean extends AdminFeaturesPageJspBean
70 {
71
72
73
74
75
76
77
78 public static final String RIGHT_MANAGE_STYLE = "CORE_STYLES_MANAGEMENT";
79
80
81
82
83 private static final long serialVersionUID = 7138319350433775587L;
84
85
86 private static final String MARK_STYLE_LIST = "style_list";
87 private static final String MARK_PORTLET_TYPE_LIST = "portlet_type_list";
88 private static final String MARK_PORTAL_COMPONENT_LIST = "portal_component_list";
89 private static final String MARK_STYLE = "style";
90 private static final String MARK_PAGINATOR = "paginator";
91 private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
92
93
94 private static final String PROPERTY_STYLES_PER_PAGE = "paginator.style.itemsPerPage";
95
96
97 private static final String TEMPLATE_MANAGE_STYLES = "admin/style/manage_styles.html";
98 private static final String TEMPLATE_CREATE_STYLE = "admin/style/create_style.html";
99 private static final String TEMPLATE_MODIFY_STYLE = "admin/style/modify_style.html";
100
101
102 private static final int PORTAL_COMPONENT_ID_PORTLET = 0;
103
104
105 private static final String JSP_DO_REMOVE_STYLE = "jsp/admin/style/DoRemoveStyle.jsp";
106 private static final String JSP_DO_REMOVE_STYLESHEET = "jsp/admin/style/DoRemoveStyleSheet.jsp";
107
108
109 private static final String MESSAGE_CANT_DELETE_STYLE_PORTLETS = "portal.style.message.cannotDeleteStylePorlets";
110
111 private static final String MESSAGE_CONFIRM_DELETE_STYLE = "portal.style.message.confirmDeleteStyle";
112 private static final String MESSAGE_CREATE_STYLE_INVALID_FORMAT_ID = "portal.style.message.createStyle.InvalidIdFormat";
113 private static final String MESSAGE_CREATE_STYLE_ID_ALREADY_EXISTS = "portal.style.message.createStyle.idAlreadyExists";
114 private static final String MESSAGE_CREATE_STYLE_COMPONENT_EXISTS = "portal.style.message.createStyle.componentHasAlreadyAStyle";
115 private static final String MESSAGE_CONFIRM_DELETE_STYLESHEET = "portal.style.message.stylesheetConfirmDelete";
116 private int _nItemsPerPage;
117 private String _strCurrentPageIndex;
118
119
120
121
122
123
124
125
126 public String getStylesManagement( HttpServletRequest request )
127 {
128 List<Style> listStyles = (List<Style>) StyleHome.getStylesList( );
129
130 String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
131 String strAscSort = null;
132
133 if ( strSortedAttributeName != null )
134 {
135 strAscSort = request.getParameter( Parameters.SORTED_ASC );
136
137 boolean bIsAscSort = Boolean.parseBoolean( strAscSort );
138
139 Collections.sort( listStyles, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
140 }
141
142 int defaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_STYLES_PER_PAGE, 10 );
143 _strCurrentPageIndex = AbstractPaginator.getPageIndex( request, AbstractPaginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
144 _nItemsPerPage = AbstractPaginator.getItemsPerPage( request, AbstractPaginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, defaultItemsPerPage );
145
146 String strURL = getHomeUrl( request );
147
148 if ( strSortedAttributeName != null )
149 {
150 strURL += ( "?" + Parameters.SORTED_ATTRIBUTE_NAME + "=" + strSortedAttributeName );
151 }
152
153 if ( strAscSort != null )
154 {
155 strURL += ( "&" + Parameters.SORTED_ASC + "=" + strAscSort );
156 }
157
158 LocalizedPaginator<Style> paginator = new LocalizedPaginator<>( listStyles, _nItemsPerPage, strURL, AbstractPaginator.PARAMETER_PAGE_INDEX,
159 _strCurrentPageIndex, getLocale( ) );
160
161 Map<String, Object> model = new HashMap<>( );
162 model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
163 model.put( MARK_PAGINATOR, paginator );
164 model.put( MARK_STYLE_LIST, paginator.getPageItems( ) );
165
166 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_STYLES, getLocale( ), model );
167
168 return getAdminPage( template.getHtml( ) );
169 }
170
171
172
173
174
175
176
177
178 public String getCreateStyle( HttpServletRequest request )
179 {
180 Map<String, Object> model = new HashMap<>( );
181 model.put( MARK_PORTLET_TYPE_LIST, PortletTypeHome.getPortletsTypesList( getLocale( ) ) );
182 model.put( MARK_PORTAL_COMPONENT_LIST, StyleHome.getPortalComponentList( ) );
183 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_CREATE_STYLE ) );
184
185 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_STYLE, getLocale( ), model );
186
187 return getAdminPage( template.getHtml( ) );
188 }
189
190
191
192
193
194
195
196
197
198
199 public String doCreateStyle( HttpServletRequest request ) throws AccessDeniedException
200 {
201 String strId = request.getParameter( Parameters.STYLE_ID );
202
203
204 if ( request.getParameter( Parameters.STYLE_ID ).equals( "" ) || request.getParameter( Parameters.STYLE_NAME ).equals( "" ) )
205 {
206 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
207 }
208
209 int nId;
210
211 try
212 {
213 nId = Integer.parseInt( strId );
214 }
215 catch( NumberFormatException nb )
216 {
217 return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_INVALID_FORMAT_ID, AdminMessage.TYPE_STOP );
218 }
219
220 Style styleExisting = StyleHome.findByPrimaryKey( nId );
221
222 if ( styleExisting != null )
223 {
224 return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_ID_ALREADY_EXISTS, AdminMessage.TYPE_STOP );
225 }
226
227 int nPortalComponentId = Integer.parseInt( request.getParameter( Parameters.PORTAL_COMPONENT ) );
228
229 if ( StyleHome.checkStylePortalComponent( nPortalComponentId ) && ( nPortalComponentId != PORTAL_COMPONENT_ID_PORTLET ) )
230 {
231 return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_COMPONENT_EXISTS, AdminMessage.TYPE_STOP );
232 }
233
234 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_CREATE_STYLE ) )
235 {
236 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
237 }
238
239
240 Styleal/business/style/Style.html#Style">Style style = new Style( );
241 style.setId( nId );
242 style.setDescription( request.getParameter( Parameters.STYLE_NAME ) );
243 style.setPortalComponentId( nPortalComponentId );
244
245 String strPortletTypeId = request.getParameter( Parameters.PORTLET_TYPE );
246 strPortletTypeId = ( strPortletTypeId != null ) ? strPortletTypeId : "";
247 style.setPortletTypeId( strPortletTypeId );
248 StyleHome.create( style );
249
250 return getHomeUrl( request );
251 }
252
253
254
255
256
257
258
259
260 public String getModifyStyle( HttpServletRequest request )
261 {
262 String strIdStyles = request.getParameter( Parameters.STYLE_ID );
263 int nStyleId = Integer.parseInt( strIdStyles );
264
265 Map<String, Object> model = new HashMap<>( );
266 model.put( MARK_STYLE, StyleHome.findByPrimaryKey( nStyleId ) );
267 model.put( MARK_PORTLET_TYPE_LIST, PortletTypeHome.getPortletsTypesList( getLocale( ) ) );
268 model.put( MARK_PORTAL_COMPONENT_LIST, StyleHome.getPortalComponentList( ) );
269 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MODIFY_STYLE ) );
270
271 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_STYLE, getLocale( ), model );
272
273 return getAdminPage( template.getHtml( ) );
274 }
275
276
277
278
279
280
281
282
283
284
285 public String doModifyStyle( HttpServletRequest request ) throws AccessDeniedException
286 {
287 int nStyleId = Integer.parseInt( request.getParameter( Parameters.STYLE_ID ) );
288
289
290 String strPortletTypeId = request.getParameter( Parameters.PORTLET_TYPE );
291 strPortletTypeId = ( strPortletTypeId != null ) ? strPortletTypeId : "";
292
293 int nPortalComponentId = Integer.parseInt( request.getParameter( Parameters.PORTAL_COMPONENT ) );
294 String strStyleDescription = request.getParameter( Parameters.STYLE_NAME );
295
296 if ( strStyleDescription.trim( ).equals( "" ) )
297 {
298 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
299 }
300
301 Style style = StyleHome.findByPrimaryKey( nStyleId );
302 int nPortalComponentOld = style.getPortalComponentId( );
303
304 if ( StyleHome.checkStylePortalComponent( nPortalComponentId ) && ( nPortalComponentId != PORTAL_COMPONENT_ID_PORTLET )
305 && ( nPortalComponentId != nPortalComponentOld ) )
306 {
307 return AdminMessageService.getMessageUrl( request, MESSAGE_CREATE_STYLE_COMPONENT_EXISTS, AdminMessage.TYPE_STOP );
308 }
309 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MODIFY_STYLE ) )
310 {
311 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
312 }
313
314 style.setPortletTypeId( strPortletTypeId );
315 style.setPortalComponentId( nPortalComponentId );
316 style.setDescription( strStyleDescription );
317 StyleHome.update( style );
318
319 return getHomeUrl( request );
320 }
321
322
323
324
325
326
327
328
329 public String getConfirmRemoveStyle( HttpServletRequest request )
330 {
331 String strId = request.getParameter( Parameters.STYLE_ID );
332 int nId = Integer.parseInt( strId );
333 Collection<PortletImpl> listPortlets = PortletHome.getPortletListByStyle( nId );
334 Collection<StyleSheet> listStyleSheets = StyleHome.getStyleSheetList( nId );
335
336 if ( CollectionUtils.isNotEmpty( listPortlets ) )
337 {
338 return AdminMessageService.getMessageUrl( request, MESSAGE_CANT_DELETE_STYLE_PORTLETS, AdminMessage.TYPE_STOP );
339 }
340
341 if ( CollectionUtils.isNotEmpty( listStyleSheets ) )
342 {
343 for ( StyleSheet styleSheet : listStyleSheets )
344 {
345 Object [ ] args = {
346 styleSheet.getDescription( )
347 };
348
349 Map<String, Object> parameters = new HashMap<>( );
350 parameters.put( Parameters.STYLESHEET_ID, Integer.toString( styleSheet.getId( ) ) );
351 parameters.put( Parameters.STYLE_ID, Integer.toString( styleSheet.getStyleId( ) ) );
352 parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_DO_REMOVE_STYLESHEET ) );
353 return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE_STYLESHEET, args, null, JSP_DO_REMOVE_STYLESHEET, null,
354 AdminMessage.TYPE_CONFIRMATION, parameters );
355 }
356 }
357
358 Map<String, String> parameters = new HashMap<>( );
359 parameters.put( Parameters.STYLE_ID, Integer.toString( nId ) );
360 parameters.put( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, JSP_DO_REMOVE_STYLE ) );
361
362 return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE_STYLE, JSP_DO_REMOVE_STYLE, AdminMessage.TYPE_CONFIRMATION, parameters );
363 }
364
365
366
367
368
369
370
371
372
373
374 public String doRemoveStyle( HttpServletRequest request ) throws AccessDeniedException
375 {
376 if ( !SecurityTokenService.getInstance( ).validate( request, JSP_DO_REMOVE_STYLE ) )
377 {
378 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
379 }
380 String strId = request.getParameter( Parameters.STYLE_ID );
381 int nId = Integer.parseInt( strId );
382 StyleHome.remove( nId );
383
384 return getHomeUrl( request );
385 }
386 }