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.portlet;
35
36 import fr.paris.lutece.portal.business.page.PageHome;
37 import fr.paris.lutece.portal.business.portlet.Portlet;
38 import fr.paris.lutece.portal.business.portlet.PortletHome;
39 import fr.paris.lutece.portal.business.portlet.PortletType;
40 import fr.paris.lutece.portal.business.portlet.PortletTypeHome;
41 import fr.paris.lutece.portal.business.role.RoleHome;
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.AppLogService;
46 import fr.paris.lutece.portal.service.util.AppPropertiesService;
47 import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
48 import fr.paris.lutece.portal.web.constants.Messages;
49 import fr.paris.lutece.portal.web.constants.Parameters;
50 import fr.paris.lutece.util.ReferenceList;
51 import fr.paris.lutece.util.html.HtmlTemplate;
52
53 import java.util.HashMap;
54 import java.util.Locale;
55 import java.util.Map;
56
57 import javax.servlet.http.HttpServletRequest;
58
59
60
61
62
63
64
65
66 public abstract class PortletJspBean extends AdminFeaturesPageJspBean
67 {
68
69
70 public static final String RIGHT_MANAGE_ADMIN_SITE = "CORE_ADMIN_SITE";
71
72
73 protected static final String PARAMETER_PAGE_ID = "page_id";
74 protected static final String PARAMETER_PORTLET_ID = "portlet_id";
75 protected static final String PARAMETER_PORTLET_TYPE_ID = "portlet_type_id";
76 private static final long serialVersionUID = -3546292252642160812L;
77
78
79 private static final String MARK_PORTLET = "portlet";
80 private static final String MARK_PORTLET_TYPE = "portletType";
81 private static final String MARK_PORTLET_PAGE_ID = "portlet_page_id";
82 private static final String MARK_PORTLET_ORDER_COMBO = "portlet_order_combo";
83 private static final String MARK_PORTLET_COLUMNS_COMBO = "portlet_columns_combo";
84 private static final String MARK_PORTLET_STYLES_COMBO = "portlet_style_combo";
85 private static final String MARK_PORTLET_ROLES_COMBO = "portlet_role_combo";
86 private static final String MARK_SMALL_CHECKED = "small_checked";
87 private static final String MARK_NORMAL_CHECKED = "normal_checked";
88 private static final String MARK_LARGE_CHECKED = "large_checked";
89 private static final String MARK_XLARGE_CHECKED = "xlarge_checked";
90 private static final String VALUE_CHECKED = "checked=\"checked\"";
91 private static final String VALUE_UNCHECKED = "";
92
93
94 private static final String TEMPLATE_CREATE_PORTLET = "admin/portlet/create_portlet.html";
95 private static final String TEMPLATE_MODIFY_PORTLET = "admin/portlet/modify_portlet.html";
96
97
98 private static final String PROPERTY_LIST_ORDER_MAX = "list.order.max";
99 private static final String PROPERTY_COLUMN_NUM_MAX = "nb.columns";
100
101
102 private static final String MESSAGE_INVALID_PAGE_ID = "portal.site.message.pageIdInvalid";
103
104
105 private static final String JSP_ADMIN_SITE = "../../site/AdminSite.jsp";
106
107
108
109
110
111
112
113 public abstract String getCreate( HttpServletRequest request );
114
115
116
117
118
119
120
121 public abstract String doCreate( HttpServletRequest request );
122
123
124
125
126
127
128
129 public abstract String getModify( HttpServletRequest request );
130
131
132
133
134
135
136
137 public abstract String doModify( HttpServletRequest request );
138
139
140
141
142
143
144
145
146
147
148 private ReferenceList getOrdersList( )
149 {
150 ReferenceList list = new ReferenceList( );
151 int nOrderMax = AppPropertiesService.getPropertyInt( PROPERTY_LIST_ORDER_MAX, 15 );
152
153 for ( int i = 1; i <= nOrderMax; i++ )
154 {
155 list.addItem( i, String.valueOf( i ) );
156 }
157
158 return list;
159 }
160
161
162
163
164
165
166
167 private ReferenceList getColumnsList( )
168 {
169 ReferenceList list = new ReferenceList( );
170 int nColumnNumMax = AppPropertiesService.getPropertyInt( PROPERTY_COLUMN_NUM_MAX, 1 );
171
172 for ( int i = 1; i <= nColumnNumMax; i++ )
173 {
174 list.addItem( i, String.valueOf( i ) );
175 }
176
177 return list;
178 }
179
180
181
182
183
184
185
186
187 protected String setPortletCommonData( HttpServletRequest request, Portlet portlet )
188 {
189 String strErrorKey = null;
190
191
192 String strName = request.getParameter( Parameters.PORTLET_NAME );
193 String strStyleId = request.getParameter( Parameters.STYLE );
194 String strColumn = request.getParameter( Parameters.COLUMN );
195 String strOrder = request.getParameter( Parameters.ORDER );
196 String strAcceptAlias = request.getParameter( Parameters.ACCEPT_ALIAS );
197 String strAcceptPortletTitle = request.getParameter( Parameters.DISPLAY_PORTLET_TITLE );
198 String strPortletTypeId = request.getParameter( Parameters.PORTLET_TYPE_ID );
199 String strRole = request.getParameter( Parameters.ROLE );
200 String strDisplaySmall = request.getParameter( Parameters.DISPLAY_ON_SMALL_DEVICE );
201 String strDisplayNormal = request.getParameter( Parameters.DISPLAY_ON_NORMAL_DEVICE );
202 String strDisplayLarge = request.getParameter( Parameters.DISPLAY_ON_LARGE_DEVICE );
203 String strDisplayXLarge = request.getParameter( Parameters.DISPLAY_ON_XLARGE_DEVICE );
204
205 strName = strName.replaceAll( "\"", "" );
206
207
208 if ( strName.equals( "" ) || strOrder.equals( "" ) || strColumn.equals( "" ) || strAcceptAlias.equals( "" ) ||
209 strAcceptPortletTitle.equals( "" ) )
210 {
211 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
212 }
213
214
215 if ( portlet.isContentGeneratedByXmlAndXsl( ) )
216 {
217 if ( ( strStyleId == null ) || strStyleId.trim( ).equals( "" ) )
218 {
219 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
220 }
221 }
222
223 String strPageId = request.getParameter( PARAMETER_PAGE_ID );
224 int nPageId;
225
226
227 try
228 {
229 nPageId = Integer.parseInt( strPageId );
230
231 if ( !PageHome.checkPageExist( nPageId ) )
232 {
233 return AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_PAGE_ID, AdminMessage.TYPE_STOP );
234 }
235 }
236 catch ( NumberFormatException e )
237 {
238 AppLogService.error( e.getMessage( ), e );
239
240 return AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_PAGE_ID, AdminMessage.TYPE_STOP );
241 }
242
243 int nOrder = Integer.parseInt( strOrder );
244 int nColumn = Integer.parseInt( strColumn );
245 int nAcceptAlias = Integer.parseInt( strAcceptAlias );
246 int nAcceptPortletTitle = Integer.parseInt( strAcceptPortletTitle );
247
248 int nStyleId = portlet.isContentGeneratedByXmlAndXsl( ) ? Integer.parseInt( strStyleId ) : 0;
249
250 int nDeviceDisplayFlags = 0;
251
252 if ( strDisplaySmall != null )
253 {
254 nDeviceDisplayFlags |= Portlet.FLAG_DISPLAY_ON_SMALL_DEVICE;
255 }
256
257 if ( strDisplayNormal != null )
258 {
259 nDeviceDisplayFlags |= Portlet.FLAG_DISPLAY_ON_NORMAL_DEVICE;
260 }
261
262 if ( strDisplayLarge != null )
263 {
264 nDeviceDisplayFlags |= Portlet.FLAG_DISPLAY_ON_LARGE_DEVICE;
265 }
266
267 if ( strDisplayXLarge != null )
268 {
269 nDeviceDisplayFlags |= Portlet.FLAG_DISPLAY_ON_XLARGE_DEVICE;
270 }
271
272 portlet.setName( strName );
273 portlet.setOrder( nOrder );
274 portlet.setColumn( nColumn );
275 portlet.setStyleId( nStyleId );
276 portlet.setPageId( nPageId );
277 portlet.setAcceptAlias( nAcceptAlias );
278 portlet.setDisplayPortletTitle( nAcceptPortletTitle );
279 portlet.setPortletTypeId( strPortletTypeId );
280 portlet.setRole( strRole );
281 portlet.setDeviceDisplayFlags( nDeviceDisplayFlags );
282
283 return strErrorKey;
284 }
285
286
287
288
289
290
291
292
293 protected HtmlTemplate getCreateTemplate( String strPageId, String strPortletTypeId )
294 {
295 return getCreateTemplate( strPageId, strPortletTypeId, new HashMap<String, Object>( ) );
296 }
297
298
299
300
301
302
303
304
305
306 protected HtmlTemplate getCreateTemplate( String strPageId, String strPortletTypeId, Map<String, Object> model )
307 {
308 PortletType portletType = PortletTypeHome.findByPrimaryKey( strPortletTypeId );
309 Locale locale = getLocale( );
310 portletType.setLocale( locale );
311
312 model.put( MARK_PORTLET_TYPE, portletType );
313 model.put( MARK_PORTLET_PAGE_ID, strPageId );
314 model.put( MARK_PORTLET_ORDER_COMBO, getOrdersList( ) );
315 model.put( MARK_PORTLET_COLUMNS_COMBO, getColumnsList( ) );
316 model.put( MARK_PORTLET_STYLES_COMBO, PortletHome.getStylesList( strPortletTypeId ) );
317 model.put( MARK_PORTLET_ROLES_COMBO, RoleHome.getRolesList( getUser( ) ) );
318
319 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_PORTLET, locale, model );
320
321 return template;
322 }
323
324
325
326
327
328
329
330 protected HtmlTemplate getModifyTemplate( Portlet portlet )
331 {
332 return getModifyTemplate( portlet, new HashMap<String, Object>( ) );
333 }
334
335
336
337
338
339
340
341
342 protected HtmlTemplate getModifyTemplate( Portlet portlet, Map<String, Object> model )
343 {
344 PortletType portletType = PortletTypeHome.findByPrimaryKey( portlet.getPortletTypeId( ) );
345 portletType.setLocale( getLocale( ) );
346 model.put( MARK_PORTLET_TYPE, portletType );
347 model.put( MARK_PORTLET, portlet );
348 model.put( MARK_PORTLET_ORDER_COMBO, getOrdersList( ) );
349 model.put( MARK_PORTLET_COLUMNS_COMBO, getColumnsList( ) );
350 model.put( MARK_PORTLET_STYLES_COMBO, PortletHome.getStylesList( portlet.getPortletTypeId( ) ) );
351 model.put( MARK_PORTLET_ROLES_COMBO, RoleHome.getRolesList( getUser( ) ) );
352 putCheckBox( model, MARK_SMALL_CHECKED, portlet.hasDeviceDisplayFlag( Portlet.FLAG_DISPLAY_ON_SMALL_DEVICE ) );
353 putCheckBox( model, MARK_NORMAL_CHECKED, portlet.hasDeviceDisplayFlag( Portlet.FLAG_DISPLAY_ON_NORMAL_DEVICE ) );
354 putCheckBox( model, MARK_LARGE_CHECKED, portlet.hasDeviceDisplayFlag( Portlet.FLAG_DISPLAY_ON_LARGE_DEVICE ) );
355 putCheckBox( model, MARK_XLARGE_CHECKED, portlet.hasDeviceDisplayFlag( Portlet.FLAG_DISPLAY_ON_XLARGE_DEVICE ) );
356
357 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_PORTLET, getLocale( ), model );
358
359 return template;
360 }
361
362
363
364
365
366
367
368
369 protected void putCheckBox( Map<String, Object> model, String strMarkerChecked, boolean bChecked )
370 {
371 String strChecked = ( bChecked ) ? VALUE_CHECKED : VALUE_UNCHECKED;
372 model.put( strMarkerChecked, strChecked );
373 }
374
375
376
377
378
379
380 protected String getPageUrl( int nIdPage )
381 {
382 return JSP_ADMIN_SITE + "?" + PARAMETER_PAGE_ID + "=" + nIdPage;
383 }
384 }