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