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.plugins.whatsnew.business.portlet;
35  
36  import fr.paris.lutece.plugins.whatsnew.business.IWhatsNew;
37  import fr.paris.lutece.plugins.whatsnew.service.WhatsNewService;
38  import fr.paris.lutece.plugins.whatsnew.service.portlet.WhatsNewPortletService;
39  import fr.paris.lutece.plugins.whatsnew.utils.constants.WhatsNewConstants;
40  import fr.paris.lutece.plugins.whatsnew.utils.sort.WhatsNewComparator;
41  import fr.paris.lutece.portal.business.portlet.Portlet;
42  import fr.paris.lutece.util.xml.XmlUtil;
43  
44  import org.apache.commons.lang.StringUtils;
45  
46  import java.sql.Timestamp;
47  
48  import java.util.ArrayList;
49  import java.util.Collection;
50  import java.util.Collections;
51  import java.util.List;
52  import java.util.Locale;
53  
54  import javax.servlet.http.HttpServletRequest;
55  
56  
57  /**
58   *
59   * WhatsNewPortlet
60   *
61   */
62  public class WhatsNewPortlet extends Portlet
63  {
64      private boolean _bShowDocuments;
65      private boolean _bShowPortlets;
66      private boolean _bShowPages;
67      private boolean _bIsAscSort;
68      private int _nPeriod;
69      private int _nNbElementsMax;
70      private int _nElementsOrder;
71      private boolean _bIsDynamic;
72  
73      /**
74       * Sets the identifier of the portlet type to the value specified in the WhatsNewPortletHome class
75       */
76      public WhatsNewPortlet(  )
77      {
78          setPortletTypeId( WhatsNewPortletService.getInstance(  ).getPortletTypeId(  ) );
79      }
80  
81      /**
82       * Check if the portlet must show the documents or not
83       * @return Returns the boolean that indicates if the user wants to see the documents
84       */
85      public boolean getShowDocuments(  )
86      {
87          return _bShowDocuments;
88      }
89  
90      /**
91       * Set true if the portlet must show the documents
92       * @param bShowDocuments The new boolean that indicates if the user wants to see the articles
93       */
94      public void setShowDocuments( boolean bShowDocuments )
95      {
96          _bShowDocuments = bShowDocuments;
97      }
98  
99      /**
100      * Check if the portlet must show the portlets or not
101      * @return Returns the boolean that indicates if the user wants to see the portlets
102      */
103     public boolean getShowPortlets(  )
104     {
105         return _bShowPortlets;
106     }
107 
108     /**
109      * Set true if the portlet must show the portlets
110      * @param bPortlets The new boolean that indicates if the user wants to see the portlets
111      */
112     public void setShowPortlets( boolean bPortlets )
113     {
114         _bShowPortlets = bPortlets;
115     }
116 
117     /**
118      * Check if the portlet must show the pages or not
119      * @return Returns the boolean that indicates if the user wants to see the pages
120      */
121     public boolean getShowPages(  )
122     {
123         return _bShowPages;
124     }
125 
126     /**
127      * Set true if the portlet must show the pages
128      * @param bPages The new boolean that indicates if the user wants to see the pages
129      */
130     public void setShowPages( boolean bPages )
131     {
132         _bShowPages = bPages;
133     }
134 
135     /**
136      * Get the period
137      * @return Returns the period (number of days)
138      */
139     public int getPeriod(  )
140     {
141         return _nPeriod;
142     }
143 
144     /**
145      * Set the period
146      * @param nPeriod The period to set (number of days)
147      */
148     public void setPeriod( int nPeriod )
149     {
150         this._nPeriod = nPeriod;
151     }
152 
153     /**
154      * Get the maximum number of elements to see in the portlet
155      * @return Returns the nbElementsMax.
156      */
157     public int getNbElementsMax(  )
158     {
159         return _nNbElementsMax;
160     }
161 
162     /**
163      * Set maximum number of elements to see in the portlet
164      * @param nElementsMax The maximum number of elements to see in the portlet.
165      */
166     public void setNbElementsMax( int nElementsMax )
167     {
168         _nNbElementsMax = nElementsMax;
169     }
170 
171     /**
172      * Get the element order
173      * @return Returns order of the elements to show.
174      */
175     public int getElementsOrder(  )
176     {
177         return _nElementsOrder;
178     }
179 
180     /**
181      * Set the element order
182      * @param nOrder The maximum number of elements to see in the portlet.
183      */
184     public void setElementsOrder( int nOrder )
185     {
186         _nElementsOrder = nOrder;
187     }
188 
189     /**
190      * Check if the portlet is sorting ascendingly
191      * @return true if it is sorting ascendingly
192      */
193     public boolean getAscSort(  )
194     {
195         return _bIsAscSort;
196     }
197 
198     /**
199      * Set the sorting attribute
200      * @param bIsAscSort true if it is sorting ascendingly
201      */
202     public void setAscSort( boolean bIsAscSort )
203     {
204         _bIsAscSort = bIsAscSort;
205     }
206 
207     /**
208      * Returns the XML content of the portlet with the XML header
209      * @param request The HTTP Servlet request
210      * @return The XML content of this portlet
211      */
212     public String getXmlDocument( HttpServletRequest request )
213     {
214         return XmlUtil.getXmlHeader(  ) + getXml( request );
215     }
216 
217     /**
218      * Check if the whatsnew is dynamic
219      * @return true if it is dynamic, false otherwise
220      */
221     public boolean getDynamic(  )
222     {
223         return _bIsDynamic;
224     }
225 
226     /**
227      * Set the attribute dynamic of the whatsnew
228      * @param bIsDynamic true if it is dynamic, false otherwise
229      */
230     public void setDynamic( boolean bIsDynamic )
231     {
232         _bIsDynamic = bIsDynamic;
233     }
234 
235     /**
236      * Returns the XML content of the portlet without the XML header
237      * @param request The HTTP Servlet request
238      * @return The Xml content of this portlet
239      */
240     public String getXml( HttpServletRequest request )
241     {
242         StringBuffer strXml = new StringBuffer(  );
243         XmlUtil.beginElement( strXml, WhatsNewConstants.TAG_WHATS_NEW_PORTLET );
244 
245         List<IWhatsNew> listElements = new ArrayList<IWhatsNew>(  );
246         Locale locale;
247 
248         if ( request != null )
249         {
250             locale = request.getLocale(  );
251         }
252         else
253         {
254             locale = Locale.getDefault(  );
255         }
256         Timestamp limitTimestamp = WhatsNewService.getInstance(  ).getTimestampFromPeriodAndCurrentDate( _nPeriod,
257                 locale );
258 
259         if ( _bShowPages )
260         {
261             Collection<IWhatsNew> listPages;
262 
263             if ( !_bIsDynamic )
264             {
265                 listPages = WhatsNewService.getInstance(  ).getModeratedPages( getId(  ), locale );
266             }
267             else
268             {
269                 listPages = WhatsNewService.getInstance(  ).getPagesByCriterias( limitTimestamp, locale );
270             }
271 
272             listElements.addAll( listPages );
273         }
274 
275         if ( _bShowPortlets )
276         {
277             Collection<IWhatsNew> listPortlets;
278 
279             if ( !_bIsDynamic )
280             {
281                 listPortlets = WhatsNewService.getInstance(  ).getModeratedPortlets( getId(  ), locale );
282             }
283             else
284             {
285                 listPortlets = WhatsNewService.getInstance(  ).getPortletsByCriterias( limitTimestamp, locale );
286             }
287 
288             listElements.addAll( listPortlets );
289         }
290 
291         if ( _bShowDocuments && WhatsNewService.getInstance(  ).isPluginDocumentActivated(  ) )
292         {
293             Collection<IWhatsNew> listDocuments;
294 
295             if ( !_bIsDynamic )
296             {
297                 listDocuments = WhatsNewService.getInstance(  ).getModeratedDocuments( getId(  ), locale );
298             }
299             else
300             {
301                 listDocuments = WhatsNewService.getInstance(  ).getDocumentsByCriterias( limitTimestamp, locale );
302             }
303 
304             listElements.addAll( listDocuments );
305         }
306 
307         Collections.sort( listElements, new WhatsNewComparator( _nElementsOrder, _bIsAscSort ) );
308 
309         // retrieve from request the current display id parameter : to paginate the results
310         // the request parameter is postfixed by the portlet id to be able to handle more than
311         // one portlet in a page
312         String strMinDisplay = null;
313 
314         if ( request != null )
315         {
316         	strMinDisplay = request.getParameter( WhatsNewConstants.PARAMETER_MIN_DISPLAY + WhatsNewConstants.UNDERSCORE +
317                     getId(  ) );
318         }
319 
320         if ( StringUtils.isNotBlank( strMinDisplay ) )
321         {
322             XmlUtil.addElement( strXml, WhatsNewConstants.TAG_WHATS_NEW_MIN_DISPLAY, strMinDisplay );
323         }
324         else
325         {
326             XmlUtil.addElement( strXml, WhatsNewConstants.TAG_WHATS_NEW_MIN_DISPLAY, 1 );
327         }
328 
329         // retrieve the number of elements to display in a portlet
330         // this is filtered in the xsl in order to allow easy pagination
331         XmlUtil.addElement( strXml, WhatsNewConstants.TAG_WHATS_NEW_NUMBER_DISPLAY, _nNbElementsMax );
332 
333         // get the xml list of elements
334         for ( IWhatsNew whatsnew : listElements )
335         {
336             strXml.append( whatsnew.getXml( request ) );
337         }
338 
339         XmlUtil.endElement( strXml, WhatsNewConstants.TAG_WHATS_NEW_PORTLET );
340 
341         return addPortletTags( strXml );
342     }
343 
344     /**
345      * Updates the current instance of the form portlet object
346      */
347     public void update(  )
348     {
349         WhatsNewPortletService.getInstance(  ).update( this );
350     }
351 
352     /**
353      * Removes the current instance of the  the form portlet  object
354      */
355     public void remove(  )
356     {
357         WhatsNewPortletService.getInstance(  ).remove( this );
358     }
359 }