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