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