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.portlet;
35  
36  import fr.paris.lutece.portal.business.XmlContent;
37  import fr.paris.lutece.portal.business.page.Page;
38  import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
39  import fr.paris.lutece.util.xml.XmlUtil;
40  
41  import java.sql.Timestamp;
42  
43  import java.util.Map;
44  
45  import javax.servlet.http.HttpServletRequest;
46  
47  /**
48   * This class represents business objects Portlet. It is the base class of all portlets. It is abstract and the implementation of the interface XmlContent is
49   * compulsory.
50   */
51  public abstract class Portlet implements XmlContent
52  {
53      // //////////////////////////////////////////////////////////////////////////
54      // Publics variables common to all the portlets
55      public static final int STATUS_PUBLISHED = 0;
56      public static final int STATUS_UNPUBLISHED = 1;
57      public static final int FLAG_DISPLAY_ON_SMALL_DEVICE = 0x00000001;
58      public static final int FLAG_DISPLAY_ON_NORMAL_DEVICE = 0x00000010;
59      public static final int FLAG_DISPLAY_ON_LARGE_DEVICE = 0x00000100;
60      public static final int FLAG_DISPLAY_ON_XLARGE_DEVICE = 0x00001000;
61      private static final String VALUE_TRUE = "1";
62      private static final String VALUE_FALSE = "0";
63  
64      // //////////////////////////////////////////////////////////////////////////
65      // Privates variables common to all the portlets
66      private static final int MODE_NORMAL = 0;
67      private static final int MODE_ADMIN = 1;
68      private int _nId;
69      private int _nPageId;
70      private int _nStyleId;
71      private int _nColumn;
72      private int _nOrder;
73      private int _nStatus;
74      private int _nAcceptAlias;
75      private int _nDisplayPortletTitle;
76      private String _strName;
77      private String _strPortletTypeId;
78      private String _strPortletTypeName;
79      private String _strUrlCreation;
80      private String _strUrlUpdate;
81      private String _strPluginName;
82      private String _strHomeClassName;
83      private String _strRole;
84      private Timestamp _dateUpdate;
85      private int _nDeviceFlags;
86  
87      // //////////////////////////////////////////////////////////////////////////
88      // Accessors
89  
90      /**
91       * Returns the identifier of this portlet.
92       *
93       * @return the portlet identifier
94       */
95      public int getId( )
96      {
97          return _nId;
98      }
99  
100     /**
101      * Sets the identifier of the portlet to the specified int.
102      *
103      * @param nId
104      *            the new identifier
105      */
106     public void setId( int nId )
107     {
108         _nId = nId;
109     }
110 
111     /**
112      * Returns the style identifier of this portlet
113      *
114      * @return the style identifier
115      */
116     public int getStyleId( )
117     {
118         return _nStyleId;
119     }
120 
121     /**
122      * Sets the identifier of the portlet style with the specified int.
123      *
124      * @param nStyleId
125      *            the new style identifier
126      */
127     public void setStyleId( int nStyleId )
128     {
129         _nStyleId = nStyleId;
130     }
131 
132     /**
133      * Returns the page identifier associated to this portlet
134      *
135      * @return the page identifier
136      */
137     public int getPageId( )
138     {
139         return _nPageId;
140     }
141 
142     /**
143      * Sets the identifier of the portlet style with the specified int.
144      *
145      * @param nPageId
146      *            The identifier of the page
147      */
148     public void setPageId( int nPageId )
149     {
150         _nPageId = nPageId;
151     }
152 
153     /**
154      * Returns the identifier of this portlet.
155      *
156      * @return the portlet identifier
157      */
158     public int getStatus( )
159     {
160         return _nStatus;
161     }
162 
163     /**
164      * Sets the identifier of the portlet to the specified int.
165      *
166      * @param nStatus
167      *            the new status
168      */
169     public void setStatus( int nStatus )
170     {
171         _nStatus = nStatus;
172     }
173 
174     /**
175      * Returns the name of this portlet
176      *
177      * @return the portlet name
178      */
179     public String getName( )
180     {
181         return _strName;
182     }
183 
184     /**
185      * Sets the name of this portlet to the specified string.
186      *
187      * @param strName
188      *            the new name
189      */
190     public void setName( String strName )
191     {
192         _strName = strName;
193     }
194 
195     /**
196      * Returns the identifier of the portlet type of this portlet which caracterizes the portlet.
197      *
198      * @return the portlet type identifier
199      */
200     public String getPortletTypeId( )
201     {
202         return _strPortletTypeId;
203     }
204 
205     /**
206      * Sets the identifier of the portlet type to the specified int.
207      *
208      * @param strPortletTypeId
209      *            the portlet type identifier
210      */
211     public void setPortletTypeId( String strPortletTypeId )
212     {
213         _strPortletTypeId = strPortletTypeId;
214     }
215 
216     /**
217      * Returns the portlet type name of this portlet
218      *
219      * @return the portlet type name
220      */
221     public String getPortletTypeName( )
222     {
223         return _strPortletTypeName;
224     }
225 
226     /**
227      * Sets the name of this portlet type with the specified string.
228      *
229      * @param strPortletTypeName
230      *            the new portlet type name
231      */
232     public void setPortletTypeName( String strPortletTypeName )
233     {
234         _strPortletTypeName = strPortletTypeName;
235     }
236 
237     /**
238      * Returns the url of the program which manages the creation of a portlet
239      *
240      * @return the url of the creation
241      */
242     public String getUrlCreation( )
243     {
244         return _strUrlCreation;
245     }
246 
247     /**
248      * Sets the url of the program which creates this portlet
249      *
250      * @param strUrlCreation
251      *            The url of creation
252      */
253     public void setUrlCreation( String strUrlCreation )
254     {
255         _strUrlCreation = strUrlCreation;
256     }
257 
258     /**
259      * Returns the url of the program which manages the update of a portlet
260      *
261      * @return the url of the program as a String
262      */
263     public String getUrlUpdate( )
264     {
265         return _strUrlUpdate;
266     }
267 
268     /**
269      * Sets the url of the program which updates this portlet
270      *
271      * @param strUrlUpdate
272      *            The url of update
273      */
274     public void setUrlUpdate( String strUrlUpdate )
275     {
276         _strUrlUpdate = strUrlUpdate;
277     }
278 
279     /**
280      * Returns the date of update of this portlet
281      *
282      * @return the update date
283      */
284     public Timestamp getDateUpdate( )
285     {
286         return _dateUpdate;
287     }
288 
289     /**
290      * Sets the date of update of this portlet with the specified date.
291      *
292      * @param dateUpdate
293      *            the new date
294      */
295     public void setDateUpdate( Timestamp dateUpdate )
296     {
297         _dateUpdate = dateUpdate;
298     }
299 
300     /**
301      * Return the number of the column of this portlet in the page
302      *
303      * @return the number of the column
304      */
305     public int getColumn( )
306     {
307         return _nColumn;
308     }
309 
310     /**
311      * Sets the number of the column of this portlet in its page with the specified int.
312      *
313      * @param nColumn
314      *            the new number of column
315      */
316     public void setColumn( int nColumn )
317     {
318         _nColumn = nColumn;
319     }
320 
321     /**
322      * Returns the order of this portlet in the page which contains it.
323      *
324      * @return the order
325      */
326     public int getOrder( )
327     {
328         return _nOrder;
329     }
330 
331     /**
332      * Sets the order of this portlet in its page with the specified int.
333      *
334      * @param nType
335      *            the new order
336      */
337     public void setOrder( int nType )
338     {
339         _nOrder = nType;
340     }
341 
342     /**
343      * Gets device display flags
344      * 
345      * @return Flags
346      */
347     public int getDeviceDisplayFlags( )
348     {
349         return _nDeviceFlags;
350     }
351 
352     /**
353      * Check if a flag is setted
354      * 
355      * @param nFlag
356      *            The flag to check
357      * @return true if the flag is set, otherwise false
358      */
359     public boolean hasDeviceDisplayFlag( int nFlag )
360     {
361         return ( _nDeviceFlags & nFlag ) != 0;
362     }
363 
364     /**
365      * Set device display flags
366      * 
367      * @param nFlags
368      *            Flags
369      */
370     public void setDeviceDisplayFlags( int nFlags )
371     {
372         _nDeviceFlags = nFlags;
373     }
374 
375     /**
376      * Returns the name of the java class which manages this type of portlet.
377      *
378      * @return the java class name
379      */
380     public String getHomeClassName( )
381     {
382         return _strHomeClassName;
383     }
384 
385     /**
386      * Sets the name of the java class which manages this type of portlet with the specified string.
387      *
388      * @param strHomeClassName
389      *            The Home Class name
390      */
391     public void setHomeClassName( String strHomeClassName )
392     {
393         _strHomeClassName = strHomeClassName;
394     }
395 
396     /**
397      * Indicates if this portlet can be modified after its creation or not.
398      *
399      * @return 1 if the portlet can be updated, 0 if not
400      */
401     public int getAcceptAlias( )
402     {
403         return _nAcceptAlias;
404     }
405 
406     /**
407      * Sets the flag which indicates that this portlet can be have a title or not.
408      *
409      * @param nDisplayPortletTitle
410      *            The flag
411      */
412     public void setDisplayPortletTitle( int nDisplayPortletTitle )
413     {
414         _nDisplayPortletTitle = nDisplayPortletTitle;
415     }
416 
417     /**
418      * Indicates if this portlet can be modified have a title or not.
419      *
420      * @return 1 if the portlet can be have a title, 0 if not
421      */
422     public int getDisplayPortletTitle( )
423     {
424         return _nDisplayPortletTitle;
425     }
426 
427     /**
428      * Sets the flag which indicates that this portlet can be updated or not.
429      *
430      * @param nAcceptAlias
431      *            The flag
432      */
433     public void setAcceptAlias( int nAcceptAlias )
434     {
435         _nAcceptAlias = nAcceptAlias;
436     }
437 
438     /**
439      * Get the plugin Name
440      *
441      * @return The pluginName
442      */
443     public String getPluginName( )
444     {
445         return _strPluginName;
446     }
447 
448     /**
449      * Sets the flag which indicates that this portlet can be updated or not.
450      *
451      * @param strPluginName
452      *            The flag
453      */
454     public void setPluginName( String strPluginName )
455     {
456         _strPluginName = strPluginName;
457     }
458 
459     /**
460      * Gets the portlet's role
461      * 
462      * @return page's role as a String
463      * @since v2.5
464      */
465     public String getRole( )
466     {
467         _strRole = ( _strRole == null ) ? Page.ROLE_NONE : _strRole;
468 
469         return _strRole;
470     }
471 
472     /**
473      * Sets the portlet's role
474      * 
475      * @param strRole
476      *            The role
477      * @since v2.5
478      */
479     public void setRole( String strRole )
480     {
481         _strRole = ( ( strRole == null ) || ( strRole.equals( "" ) ) ) ? Page.ROLE_NONE : strRole;
482     }
483 
484     // //////////////////////////////////////////////////////////////////////////
485     // Operations
486 
487     /**
488      * This method copies the fields of the portlet specified in this portlet.
489      *
490      * @param portlet
491      *            the portlet to copy
492      */
493     public void copy( Portlet portlet )
494     {
495         setId( portlet.getId( ) );
496         setPortletTypeId( portlet.getPortletTypeId( ) );
497         setPageId( portlet.getPageId( ) );
498         setStyleId( portlet.getStyleId( ) );
499         setName( portlet.getName( ) );
500         setPortletTypeName( portlet.getPortletTypeName( ) );
501         setUrlCreation( portlet.getUrlCreation( ) );
502         setUrlUpdate( portlet.getUrlUpdate( ) );
503         setDateUpdate( portlet.getDateUpdate( ) );
504         setColumn( portlet.getColumn( ) );
505         setOrder( portlet.getOrder( ) );
506         setAcceptAlias( portlet.getAcceptAlias( ) );
507         setPluginName( portlet.getPluginName( ) );
508         setDisplayPortletTitle( portlet.getDisplayPortletTitle( ) );
509         setDeviceDisplayFlags( portlet.getDeviceDisplayFlags( ) );
510         setStatus( portlet.getStatus( ) );
511         setRole( portlet.getRole( ) );
512     }
513 
514     /**
515      * Add the common tags to all the portlets to the XML document
516      *
517      * @param strPortlet
518      *            The string buffer which contains the XML content of this portlet
519      * @return The XML content of this portlet encapsulated by the common tags
520      */
521     protected String addPortletTags( StringBuffer strPortlet )
522     {
523         StringBuffer strXml = new StringBuffer( );
524         XmlUtil.beginElement( strXml, TAG_PORTLET );
525         XmlUtil.addElementHtml( strXml, TAG_PORTLET_NAME, getName( ) );
526         XmlUtil.addElement( strXml, TAG_PORTLET_ID, getId( ) );
527         XmlUtil.addElement( strXml, TAG_PAGE_ID, getPageId( ) );
528         XmlUtil.addElement( strXml, TAG_PLUGIN_NAME, getPluginName( ) );
529         XmlUtil.addElement( strXml, TAG_DISPLAY_PORTLET_TITLE, getDisplayPortletTitle( ) );
530 
531         String strDisplayOnSmallDevice = ( ( getDeviceDisplayFlags( ) & FLAG_DISPLAY_ON_SMALL_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
532         XmlUtil.addElement( strXml, TAG_DISPLAY_ON_SMALL_DEVICE, strDisplayOnSmallDevice );
533 
534         String strDisplayOnNormalDevice = ( ( getDeviceDisplayFlags( ) & FLAG_DISPLAY_ON_NORMAL_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
535         XmlUtil.addElement( strXml, TAG_DISPLAY_ON_NORMAL_DEVICE, strDisplayOnNormalDevice );
536 
537         String strDisplayOnLargeDevice = ( ( getDeviceDisplayFlags( ) & FLAG_DISPLAY_ON_LARGE_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
538         XmlUtil.addElement( strXml, TAG_DISPLAY_ON_LARGE_DEVICE, strDisplayOnLargeDevice );
539 
540         String strDisplayOnXLargeDevice = ( ( getDeviceDisplayFlags( ) & FLAG_DISPLAY_ON_XLARGE_DEVICE ) != 0 ) ? VALUE_TRUE : VALUE_FALSE;
541         XmlUtil.addElement( strXml, TAG_DISPLAY_ON_XLARGE_DEVICE, strDisplayOnXLargeDevice );
542 
543         strXml.append( strPortlet.toString( ) );
544         XmlUtil.endElement( strXml, TAG_PORTLET );
545 
546         return strXml.toString( );
547     }
548 
549     /**
550      * Recovers the stylesheet of the portlet according to the mode
551      *
552      * @param nMode
553      *            the selected mode.
554      * @return the name of the stylesheet file
555      */
556     public String getXslFile( int nMode )
557     {
558         StyleSheet xsl;
559 
560         // Added in v1.3
561         // Use the same stylesheet for normal or admin mode
562         switch( nMode )
563         {
564             case MODE_NORMAL:
565             case MODE_ADMIN:
566                 xsl = PortletHome.getXsl( getId( ), MODE_NORMAL );
567 
568                 break;
569 
570             default:
571                 xsl = PortletHome.getXsl( getId( ), nMode );
572 
573                 break;
574         }
575 
576         return xsl.getFile( );
577     }
578 
579     /**
580      * Recovers the stylesheet of the portlet according to the mode
581      *
582      * @param nMode
583      *            the selected mode.
584      * @return the content of the stylesheet file
585      */
586     public byte [ ] getXslSource( int nMode )
587     {
588         StyleSheet xsl;
589 
590         // Added in v1.3
591         // Use the same stylesheet for normal or admin mode
592         switch( nMode )
593         {
594             case MODE_NORMAL:
595             case MODE_ADMIN:
596                 xsl = PortletHome.getXsl( getId( ), MODE_NORMAL );
597 
598                 break;
599 
600             default:
601                 xsl = PortletHome.getXsl( getId( ), nMode );
602 
603                 break;
604         }
605 
606         return xsl.getSource( );
607     }
608 
609     /**
610      * Recovers the parameters to use with the stylesheet at the time of the transformation.<br>
611      * By default, portlets do not return any parameter
612      *
613      * @return a collection of the type Dictionary (Use the Hashtable implementation)
614      */
615     public Map<String, String> getXslParams( )
616     {
617         return null;
618     }
619 
620     /**
621      * Remove the portlet. This method MUST be overloaded on the level of the implementation of each portlet
622      */
623     public abstract void remove( );
624 
625     /**
626      * Check if the content of this portlet is generated by xml and xsl, or if it manage its own content generation
627      * 
628      * @return True if the content must be generated from XML and XSL, false if it must be generated by the {@link #getHtmlContent(HttpServletRequest request)}
629      *         method
630      */
631     public boolean isContentGeneratedByXmlAndXsl( )
632     {
633         return true;
634     }
635 
636     /**
637      * Get the HTML content of the portlet. If the content must be generated from XML and XSL, then this method should return null. This method should only be
638      * overrided if the method {@link #isContentGeneratedByXmlAndXsl()} returns true
639      * 
640      * @param request
641      *            The request
642      * @return The HTML content of the portlet, or null if the content is generated from XML and XSL
643      */
644     public String getHtmlContent( HttpServletRequest request )
645     {
646         return null;
647     }
648 
649     /**
650      * Check if the content of the portlet can be put in cache if the current user is not authenticated. If a cache is disabled for a portlet, then every page
651      * that contains a portlet of this type will NOT use the page cache, and portlet contents of this portlet type will not be saved into portlet cache.<br>
652      * WARNING : Overrides this method with extreme care : disabling page cache can cause severe performance issues !
653      * 
654      * @return True if the content of the portlet can be put in cache if the user is not authenticated, false otherwise.<br>
655      *         The default value is true.
656      * @see #canBeCachedForConnectedUsers() {@link #canBeCachedForConnectedUsers()} for cache for authenticated users
657      */
658     public boolean canBeCachedForAnonymousUsers( )
659     {
660         return true;
661     }
662 
663     /**
664      * Check if the content of the portlet can be put in cache if the current user is authenticated. If a cache is disabled for a portlet, then every page that
665      * contains a portlet of this type will NOT use the page cache, and portlet contents of this portlet type will not be saved into portlet cache.<br>
666      * WARNING : Overrides this method with extreme care : disabling page cache can cause severe performance issues !
667      * 
668      * @return True if the content of the portlet can be put in cache if the user is authenticated, false otherwise.<br>
669      *         The default value is true.
670      * @see #canBeCachedForAnonymousUsers() {@link #canBeCachedForAnonymousUsers()} for cache for anonymous users
671      */
672     public boolean canBeCachedForConnectedUsers( )
673     {
674         return true;
675     }
676 }