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