1 /*
2 * Copyright (c) 2002-2014, Mairie de Paris
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice
10 * and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice
13 * and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * License 1.0
33 */
34 package fr.paris.lutece.portal.business.page;
35
36 import fr.paris.lutece.portal.business.portlet.Portlet;
37 import fr.paris.lutece.portal.service.portal.PortalService;
38 import fr.paris.lutece.portal.service.rbac.RBACResource;
39 import fr.paris.lutece.portal.service.resource.IExtendableResource;
40 import fr.paris.lutece.portal.service.security.SecurityService;
41
42 import java.sql.Timestamp;
43
44 import java.util.ArrayList;
45 import java.util.List;
46
47 import javax.servlet.http.HttpServletRequest;
48
49
50 /**
51 * This class reprsents business objects Page
52 */
53 public class Page implements RBACResource, IExtendableResource
54 {
55 /////////////////////////////////////////////////////////////////////////////////
56 // Constants
57 public static final String RESOURCE_TYPE = "PAGE";
58 public static final String IMAGE_RESOURCE_TYPE_ID = "page_thumbnail";
59 public static final String ROLE_NONE = "none";
60 private static final String THEME_DEFAULT = "default";
61 private static final String SERVLET_IMAGE_PATH = "image";
62 private static final String CONSTANT_QUESTION_MARK = "?";
63 private static final String CONSTANT_AND = "&";
64 private static final String CONSTANT_EQUALS = "=";
65 private static final String MARK_RESOURCE_TYPE = "resource_type";
66 private static final String MARK_RESOURCE_ID = "id";
67
68 // Variables declarations
69 private int _nId;
70 private int _nParentPageId;
71 private int _nOrigParentPageId;
72 private int _nOrder;
73 private int _nStatus;
74 private int _nPageTemplateId;
75 private int _nNodeStatus;
76 private String _strMimeType;
77 private String _strRole; /* @since v1.1 */
78 private String _strName;
79 private String _strDescription;
80 private String _strTemplate;
81 private String _strCodeTheme;
82 private byte[] _strImageContent;
83 private Timestamp _dateUpdate;
84 private List<Portlet> _listPortlets = new ArrayList<Portlet>( );
85 private String _strMetaKeywords;
86 private String _strMetaDescription;
87 private Integer _nIdAuthorizationNode;
88
89 /**
90 * Initialize the Page
91 */
92
93 /* FIXME PageRoleRemovalListener should not be registered here
94 public static void init( )
95 {
96 // Create removal listeners and register them
97 if ( _listenerRole == null )
98 {
99 _listenerRole = new PageRoleRemovalListener( );
100 RoleRemovalListenerService.getService( ).registerListener( _listenerRole );
101 }
102 }
103 */
104
105 /**
106 * Sets the identifier of the page
107 *
108 * @param nId the page identifier
109 */
110 public void setId( int nId )
111 {
112 _nId = nId;
113 }
114
115 /**
116 * Returns the identifier of the page
117 *
118 * @return page identifier
119 */
120 public int getId( )
121 {
122 return _nId;
123 }
124
125 /**
126 * Sets the identifier of the parent current page
127 *
128 * @param nParentPageId the parent page identifier
129 */
130 public void setParentPageId( int nParentPageId )
131 {
132 _nParentPageId = nParentPageId;
133 }
134
135 /**
136 * Returns the identifier of the parent current page
137 *
138 * @return the parent page identifier
139 */
140 public int getParentPageId( )
141 {
142 return _nParentPageId;
143 }
144
145 /**
146 * Sets the identifier of the parent page as stored
147 * in the db. Only settable by the DAO
148 *
149 * @param nParentPageId the parent page identifier
150 * @since 5.1.0
151 */
152 void setOrigParentPageId( int nParentPageId )
153 {
154 _nOrigParentPageId = nParentPageId;
155 }
156
157 /**
158 * Returns the identifier of the parent page as
159 * loaded from the db
160 *
161 * @return the parent page identifier
162 * @since 5.1.0
163 */
164 public int getOrigParentPageId( )
165 {
166 return _nOrigParentPageId;
167 }
168
169 /**
170 * Returns the ImageContent
171 *
172 * @return The ImageContent
173 */
174 public byte[] getImageContent( )
175 {
176 return _strImageContent;
177 }
178
179 /**
180 * Sets the ImageContent
181 *
182 * @param strImageContent The ImageContent
183 */
184 public void setImageContent( byte[] strImageContent )
185 {
186 _strImageContent = strImageContent;
187 }
188
189 /**
190 * Returns the MimeType
191 *
192 * @return The MimeType
193 */
194 public String getMimeType( )
195 {
196 return _strMimeType;
197 }
198
199 /**
200 * Sets the MimeType
201 *
202 * @param strMimeType The MimeType
203 */
204 public void setMimeType( String strMimeType )
205 {
206 _strMimeType = strMimeType;
207 }
208
209 /**
210 * Sets the name of the page
211 *
212 * @param strName The page name
213 */
214 public void setName( String strName )
215 {
216 _strName = strName;
217 }
218
219 /**
220 * Returns the name of the page
221 *
222 * @return the page name
223 */
224 public String getName( )
225 {
226 return _strName;
227 }
228
229 /**
230 * Sets the identifier of the template for the page-setting
231 *
232 * @param nPageTemplateId the template identifier
233 */
234 public void setPageTemplateId( int nPageTemplateId )
235 {
236 _nPageTemplateId = nPageTemplateId;
237 }
238
239 /**
240 * Returns the identifier of the template for the page-setting
241 *
242 * @return the template identifier
243 */
244 public int getPageTemplateId( )
245 {
246 return _nPageTemplateId;
247 }
248
249 /**
250 * Sets the name of the template file for page-setting
251 *
252 * @param strTemplate the template filename
253 */
254 public void setTemplate( String strTemplate )
255 {
256 _strTemplate = strTemplate;
257 }
258
259 /**
260 * Returns the name of the template file for page-setting
261 *
262 * @return the template filename
263 */
264 public String getTemplate( )
265 {
266 return _strTemplate;
267 }
268
269 /**
270 * Sets the position of the current page into a portlet child pages
271 *
272 * @param nOrder the current page position into a portlet child pages
273 */
274 public void setOrder( int nOrder )
275 {
276 _nOrder = nOrder;
277 }
278
279 /**
280 * Returns the position of the page into a portlet child pages
281 *
282 * @return the current page position
283 */
284 public int getOrder( )
285 {
286 return _nOrder;
287 }
288
289 /**
290 * Sets the status of the current page (active or not active)
291 *
292 * @param nStatus the page status
293 */
294 public void setStatus( int nStatus )
295 {
296 _nStatus = nStatus;
297 }
298
299 /**
300 * Returns the status of the current page
301 *
302 * @return the current page status
303 */
304 public int getStatus( )
305 {
306 return _nStatus;
307 }
308
309 /**
310 * Sets the description of the page
311 *
312 * @param strDescription the page description
313 */
314 public void setDescription( String strDescription )
315 {
316 _strDescription = strDescription;
317 }
318
319 /**
320 * Returns the description of the page
321 *
322 * @return the description page
323 */
324 public String getDescription( )
325 {
326 return _strDescription;
327 }
328
329 /**
330 * Sets the node_status of the page
331 *
332 * @param nNodeStatus the node status
333 */
334 public void setNodeStatus( int nNodeStatus )
335 {
336 _nNodeStatus = nNodeStatus;
337 }
338
339 /**
340 * Returns the NodeStatus of the page
341 *
342 * @return the NodeStatus page
343 */
344 public int getNodeStatus( )
345 {
346 return _nNodeStatus;
347 }
348
349 /**
350 * Returns the portlets list contained into the page
351 *
352 * @return the portlets list
353 */
354 public List<Portlet> getPortlets( )
355 {
356 return _listPortlets;
357 }
358
359 /**
360 * Sets the date to which the portlets list has been modified
361 *
362 * @param listPortlets the portlet list
363 */
364 public void setPortlets( List<Portlet> listPortlets )
365 {
366 _listPortlets = listPortlets;
367 }
368
369 /**
370 * Sets the date to which the content page has been modified
371 *
372 * @param dateUpdate the date of modification
373 */
374 public void setDateUpdate( Timestamp dateUpdate )
375 {
376 _dateUpdate = dateUpdate;
377 }
378
379 /**
380 * Returns the date to which the content page has been modified
381 *
382 * @return the date of modification
383 */
384 public Timestamp getDateUpdate( )
385 {
386 return _dateUpdate;
387 }
388
389 /**
390 * Gets the page role
391 * @return page's role as a String
392 * @since v1.1
393 */
394 public String getRole( )
395 {
396 return _strRole;
397 }
398
399 /**
400 * Sets the page's role
401 * @param strRole The role
402 * @since v1.1
403 */
404 public void setRole( String strRole )
405 {
406 _strRole = ( ( strRole == null ) || ( strRole.equals( "" ) ) ) ? ROLE_NONE : strRole;
407 }
408
409 /**
410 * Returns the theme of the page
411 *
412 * @return The theme of the page as a string.
413 */
414 public String getCodeTheme( )
415 {
416 return _strCodeTheme;
417 }
418
419 /**
420 * Sets the Theme of the page to the specified string.
421 *
422 * @param strCodeTheme The new Theme of the page.
423 */
424 public void setCodeTheme( String strCodeTheme )
425 {
426 _strCodeTheme = ( ( strCodeTheme == null ) || ( strCodeTheme.equals( "" ) ) ) ? THEME_DEFAULT : strCodeTheme;
427 }
428
429 /**
430 * Checks if the page is visible for the current user
431 * @param request The HTTP request
432 * @return true if the page could be shown to the user
433 * @since v1.3.1
434 */
435 public boolean isVisible( HttpServletRequest request )
436 {
437 if ( SecurityService.isAuthenticationEnable( ) )
438 {
439 if ( !getRole( ).equals( ROLE_NONE ) )
440 {
441 return SecurityService.getInstance( ).isUserInRole( request, getRole( ) );
442 }
443 }
444
445 return true;
446 }
447
448 ////////////////////////////////////////////////////////////////////////////
449 // RBAC Resource implementation
450
451 /**
452 * Returns the Resource Type Code that identify the resource type
453 * @return The Resource Type Code
454 */
455 @Override
456 public String getResourceTypeCode( )
457 {
458 return RESOURCE_TYPE;
459 }
460
461 /**
462 * Returns the resource Id of the current object
463 * @return The resource Id of the current object
464 */
465 @Override
466 public String getResourceId( )
467 {
468 return "" + getId( );
469 }
470
471 /**
472 *
473 * @return the META Name associate to the page
474 */
475 public String getMetaKeywords( )
476 {
477 return _strMetaKeywords;
478 }
479
480 /**
481 * set the META name
482 * @param strMetaKeywords the META name
483 */
484 public void setMetaKeywords( String strMetaKeywords )
485 {
486 _strMetaKeywords = strMetaKeywords;
487 }
488
489 /**
490 *
491 * @return the META description associate to the page
492 */
493 public String getMetaDescription( )
494 {
495 return _strMetaDescription;
496 }
497
498 /**
499 * set the META description
500 * @param strMetaDescription the META description
501 */
502 public void setMetaDescription( String strMetaDescription )
503 {
504 _strMetaDescription = strMetaDescription;
505 }
506
507 /**
508 * set the id of the authorization node
509 * @param nIdAutorizationNode The authorization node ID
510 */
511 public void setIdAuthorizationNode( Integer nIdAutorizationNode )
512 {
513 _nIdAuthorizationNode = nIdAutorizationNode;
514 }
515
516 /**
517 * get the id of the authorization node
518 * @return the authorization node id
519 */
520 public Integer getIdAuthorizationNode( )
521 {
522 return _nIdAuthorizationNode;
523 }
524
525 /**
526 * {@inheritDoc}
527 */
528 @Override
529 public String getIdExtendableResource( )
530 {
531 return Integer.toString( _nId );
532 }
533
534 /**
535 * {@inheritDoc}
536 */
537 @Override
538 public String getExtendableResourceType( )
539 {
540 return RESOURCE_TYPE;
541 }
542
543 /**
544 * {@inheritDoc}
545 */
546 @Override
547 public String getExtendableResourceName( )
548 {
549 return ( _nId == 1 ) ? PortalService.getSiteName( ) : _strName;
550 }
551
552 /**
553 * {@inheritDoc}
554 */
555 @Override
556 public String getExtendableResourceDescription( )
557 {
558 return _strDescription;
559 }
560
561 /**
562 * {@inheritDoc}
563 */
564 @Override
565 public String getExtendableResourceImageUrl( )
566 {
567 if ( ( _strImageContent != null ) && ( _strImageContent.length > 0 ) )
568 {
569 StringBuilder sbUrl = new StringBuilder( SERVLET_IMAGE_PATH );
570 sbUrl.append( CONSTANT_QUESTION_MARK );
571 sbUrl.append( MARK_RESOURCE_TYPE );
572 sbUrl.append( CONSTANT_EQUALS );
573 sbUrl.append( IMAGE_RESOURCE_TYPE_ID );
574 sbUrl.append( CONSTANT_AND );
575 sbUrl.append( MARK_RESOURCE_ID );
576 sbUrl.append( CONSTANT_EQUALS );
577 sbUrl.append( _nId );
578
579 return sbUrl.toString( );
580 }
581 else
582 {
583 // No image is associated to this resource
584 return null;
585 }
586 }
587 }