View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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   * This class reprsents business objects Page
51   */
52  public class Page implements RBACResource, IExtendableResource
53  {
54      // ///////////////////////////////////////////////////////////////////////////////
55      // Constants
56      public static final String RESOURCE_TYPE = "PAGE";
57      public static final String IMAGE_RESOURCE_TYPE_ID = "page_thumbnail";
58      public static final String ROLE_NONE = "none";
59      private static final String THEME_DEFAULT = "default";
60      private static final String SERVLET_IMAGE_PATH = "image";
61      private static final String CONSTANT_QUESTION_MARK = "?";
62      private static final String CONSTANT_AND = "&";
63      private static final String CONSTANT_EQUALS = "=";
64      private static final String MARK_RESOURCE_TYPE = "resource_type";
65      private static final String MARK_RESOURCE_ID = "id";
66  
67      // Variables declarations
68      private int _nId;
69      private int _nParentPageId;
70      private int _nOrigParentPageId;
71      private int _nOrder;
72      private int _nStatus;
73      private int _nPageTemplateId;
74      private int _nNodeStatus;
75      private String _strMimeType;
76      private String _strRole; /* @since v1.1 */
77      private String _strName;
78      private String _strDescription;
79      private String _strTemplate;
80      private String _strCodeTheme;
81      private byte [ ] _strImageContent;
82      private Timestamp _dateUpdate;
83      private List<Portlet> _listPortlets = new ArrayList<>( );
84      private String _strMetaKeywords;
85      private String _strMetaDescription;
86      private Integer _nIdAuthorizationNode;
87      private boolean _bDisplayDateUpdate;
88      private boolean _bIsManualDateUpdate;
89  
90      /**
91       * Sets the identifier of the page
92       *
93       * @param nId
94       *            the page identifier
95       */
96      public void setId( int nId )
97      {
98          _nId = nId;
99      }
100 
101     /**
102      * Returns the identifier of the page
103      *
104      * @return page identifier
105      */
106     public int getId( )
107     {
108         return _nId;
109     }
110 
111     /**
112      * Sets the identifier of the parent current page
113      *
114      * @param nParentPageId
115      *            the parent page identifier
116      */
117     public void setParentPageId( int nParentPageId )
118     {
119         _nParentPageId = nParentPageId;
120     }
121 
122     /**
123      * Returns the identifier of the parent current page
124      *
125      * @return the parent page identifier
126      */
127     public int getParentPageId( )
128     {
129         return _nParentPageId;
130     }
131 
132     /**
133      * Sets the identifier of the parent page as stored in the db. Only settable by the DAO
134      *
135      * @param nParentPageId
136      *            the parent page identifier
137      * @since 5.1.0
138      */
139     void setOrigParentPageId( int nParentPageId )
140     {
141         _nOrigParentPageId = nParentPageId;
142     }
143 
144     /**
145      * Returns the identifier of the parent page as loaded from the db
146      *
147      * @return the parent page identifier
148      * @since 5.1.0
149      */
150     public int getOrigParentPageId( )
151     {
152         return _nOrigParentPageId;
153     }
154 
155     /**
156      * Returns the ImageContent
157      *
158      * @return The ImageContent
159      */
160     public byte [ ] getImageContent( )
161     {
162         return _strImageContent;
163     }
164 
165     /**
166      * Sets the ImageContent
167      *
168      * @param strImageContent
169      *            The ImageContent
170      */
171     public void setImageContent( byte [ ] strImageContent )
172     {
173         _strImageContent = strImageContent;
174     }
175 
176     /**
177      * Returns the MimeType
178      *
179      * @return The MimeType
180      */
181     public String getMimeType( )
182     {
183         return _strMimeType;
184     }
185 
186     /**
187      * Sets the MimeType
188      *
189      * @param strMimeType
190      *            The MimeType
191      */
192     public void setMimeType( String strMimeType )
193     {
194         _strMimeType = strMimeType;
195     }
196 
197     /**
198      * Sets the name of the page
199      *
200      * @param strName
201      *            The page name
202      */
203     public void setName( String strName )
204     {
205         _strName = strName;
206     }
207 
208     /**
209      * Returns the name of the page
210      *
211      * @return the page name
212      */
213     public String getName( )
214     {
215         return _strName;
216     }
217 
218     /**
219      * Sets the identifier of the template for the page-setting
220      *
221      * @param nPageTemplateId
222      *            the template identifier
223      */
224     public void setPageTemplateId( int nPageTemplateId )
225     {
226         _nPageTemplateId = nPageTemplateId;
227     }
228 
229     /**
230      * Returns the identifier of the template for the page-setting
231      *
232      * @return the template identifier
233      */
234     public int getPageTemplateId( )
235     {
236         return _nPageTemplateId;
237     }
238 
239     /**
240      * Sets the name of the template file for page-setting
241      *
242      * @param strTemplate
243      *            the template filename
244      */
245     public void setTemplate( String strTemplate )
246     {
247         _strTemplate = strTemplate;
248     }
249 
250     /**
251      * Returns the name of the template file for page-setting
252      *
253      * @return the template filename
254      */
255     public String getTemplate( )
256     {
257         return _strTemplate;
258     }
259 
260     /**
261      * Sets the position of the current page into a portlet child pages
262      *
263      * @param nOrder
264      *            the current page position into a portlet child pages
265      */
266     public void setOrder( int nOrder )
267     {
268         _nOrder = nOrder;
269     }
270 
271     /**
272      * Returns the position of the page into a portlet child pages
273      *
274      * @return the current page position
275      */
276     public int getOrder( )
277     {
278         return _nOrder;
279     }
280 
281     /**
282      * Sets the status of the current page (active or not active)
283      *
284      * @param nStatus
285      *            the page status
286      */
287     public void setStatus( int nStatus )
288     {
289         _nStatus = nStatus;
290     }
291 
292     /**
293      * Returns the status of the current page
294      *
295      * @return the current page status
296      */
297     public int getStatus( )
298     {
299         return _nStatus;
300     }
301 
302     /**
303      * Sets the description of the page
304      *
305      * @param strDescription
306      *            the page description
307      */
308     public void setDescription( String strDescription )
309     {
310         _strDescription = strDescription;
311     }
312 
313     /**
314      * Returns the description of the page
315      *
316      * @return the description page
317      */
318     public String getDescription( )
319     {
320         return _strDescription;
321     }
322 
323     /**
324      * Sets the node_status of the page
325      *
326      * @param nNodeStatus
327      *            the node status
328      */
329     public void setNodeStatus( int nNodeStatus )
330     {
331         _nNodeStatus = nNodeStatus;
332     }
333 
334     /**
335      * Returns the NodeStatus of the page
336      *
337      * @return the NodeStatus page
338      */
339     public int getNodeStatus( )
340     {
341         return _nNodeStatus;
342     }
343 
344     /**
345      * Returns the portlets list contained into the page
346      *
347      * @return the portlets list
348      */
349     public List<Portlet> getPortlets( )
350     {
351         return _listPortlets;
352     }
353 
354     /**
355      * Sets the date to which the portlets list has been modified
356      *
357      * @param listPortlets
358      *            the portlet list
359      */
360     public void setPortlets( List<Portlet> listPortlets )
361     {
362         _listPortlets = listPortlets;
363     }
364 
365     /**
366      * Sets the date to which the content page has been modified
367      *
368      * @param dateUpdate
369      *            the date of modification
370      */
371     public void setDateUpdate( Timestamp dateUpdate )
372     {
373         _dateUpdate = dateUpdate;
374     }
375 
376     /**
377      * Returns the date to which the content page has been modified
378      *
379      * @return the date of modification
380      */
381     public Timestamp getDateUpdate( )
382     {
383         return _dateUpdate;
384     }
385 
386     /**
387      * Gets the page role
388      * 
389      * @return page's role as a String
390      * @since v1.1
391      */
392     public String getRole( )
393     {
394         return _strRole;
395     }
396 
397     /**
398      * Sets the page's role
399      * 
400      * @param strRole
401      *            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
423      *            The new Theme of the page.
424      */
425     public void setCodeTheme( String strCodeTheme )
426     {
427         _strCodeTheme = ( ( strCodeTheme == null ) || ( strCodeTheme.equals( "" ) ) ) ? THEME_DEFAULT : strCodeTheme;
428     }
429 
430     /**
431      * Checks if the page is visible for the current user
432      * 
433      * @param request
434      *            The HTTP request
435      * @return true if the page could be shown to the user
436      * @since v1.3.1
437      */
438     public boolean isVisible( HttpServletRequest request )
439     {
440         if ( SecurityService.isAuthenticationEnable( ) && !getRole( ).equals( ROLE_NONE ) )
441         {
442             return SecurityService.getInstance( ).isUserInRole( request, getRole( ) );
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      * 
454      * @return The Resource Type Code
455      */
456     @Override
457     public String getResourceTypeCode( )
458     {
459         return RESOURCE_TYPE;
460     }
461 
462     /**
463      * Returns the resource Id of the current object
464      * 
465      * @return The resource Id of the current object
466      */
467     @Override
468     public String getResourceId( )
469     {
470         return "" + getId( );
471     }
472 
473     /**
474      *
475      * @return the META Name associate to the page
476      */
477     public String getMetaKeywords( )
478     {
479         return _strMetaKeywords;
480     }
481 
482     /**
483      * set the META name
484      * 
485      * @param strMetaKeywords
486      *            the META name
487      */
488     public void setMetaKeywords( String strMetaKeywords )
489     {
490         _strMetaKeywords = strMetaKeywords;
491     }
492 
493     /**
494      *
495      * @return the META description associate to the page
496      */
497     public String getMetaDescription( )
498     {
499         return _strMetaDescription;
500     }
501 
502     /**
503      * set the META description
504      * 
505      * @param strMetaDescription
506      *            the META description
507      */
508     public void setMetaDescription( String strMetaDescription )
509     {
510         _strMetaDescription = strMetaDescription;
511     }
512 
513     /**
514      * set the id of the authorization node
515      * 
516      * @param nIdAutorizationNode
517      *            The authorization node ID
518      */
519     public void setIdAuthorizationNode( Integer nIdAutorizationNode )
520     {
521         _nIdAuthorizationNode = nIdAutorizationNode;
522     }
523 
524     /**
525      * get the id of the authorization node
526      * 
527      * @return the authorization node id
528      */
529     public Integer getIdAuthorizationNode( )
530     {
531         return _nIdAuthorizationNode;
532     }
533 
534     /**
535      * {@inheritDoc}
536      */
537     @Override
538     public String getIdExtendableResource( )
539     {
540         return Integer.toString( _nId );
541     }
542 
543     /**
544      * {@inheritDoc}
545      */
546     @Override
547     public String getExtendableResourceType( )
548     {
549         return RESOURCE_TYPE;
550     }
551 
552     /**
553      * {@inheritDoc}
554      */
555     @Override
556     public String getExtendableResourceName( )
557     {
558         return ( _nId == 1 ) ? PortalService.getSiteName( ) : _strName;
559     }
560 
561     /**
562      * {@inheritDoc}
563      */
564     @Override
565     public String getExtendableResourceDescription( )
566     {
567         return getDescription( );
568     }
569 
570     /**
571      * {@inheritDoc}
572      */
573     @Override
574     public String getExtendableResourceImageUrl( )
575     {
576         if ( ( _strImageContent != null ) && ( _strImageContent.length > 0 ) )
577         {
578             StringBuilder sbUrl = new StringBuilder( SERVLET_IMAGE_PATH );
579             sbUrl.append( CONSTANT_QUESTION_MARK );
580             sbUrl.append( MARK_RESOURCE_TYPE );
581             sbUrl.append( CONSTANT_EQUALS );
582             sbUrl.append( IMAGE_RESOURCE_TYPE_ID );
583             sbUrl.append( CONSTANT_AND );
584             sbUrl.append( MARK_RESOURCE_ID );
585             sbUrl.append( CONSTANT_EQUALS );
586             sbUrl.append( _nId );
587 
588             return sbUrl.toString( );
589         }
590         else
591         {
592             // No image is associated to this resource
593             return null;
594         }
595     }
596 
597     /**
598      * Get the DisplayDateUpdate
599      * 
600      * @return the DisplayDateUpdate boolean
601      */
602     public boolean getDisplayDateUpdate( )
603     {
604         return _bDisplayDateUpdate;
605     }
606 
607     /**
608      * Set DisplayDateUpdate
609      * 
610      * @param bDisplayDateUpdate
611      */
612     public void setDisplayDateUpdate( boolean bDisplayDateUpdate )
613     {
614         _bDisplayDateUpdate = bDisplayDateUpdate;
615     }
616 
617     /**
618      * Get the boolean for manual date update
619      * 
620      * @return the isManualDateUpdate boolean
621      */
622     public boolean getIsManualDateUpdate( )
623     {
624         return _bIsManualDateUpdate;
625     }
626 
627     /**
628      * Set boolean for manual date update
629      * 
630      * @param bIsManualDateUpdate
631      */
632     public void setIsManualDateUpdate( boolean bIsManualDateUpdate )
633     {
634         _bIsManualDateUpdate = bIsManualDateUpdate;
635     }
636 }