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.service;
35  
36  import java.sql.Timestamp;
37  import java.util.ArrayList;
38  import java.util.Calendar;
39  import java.util.Collection;
40  import java.util.GregorianCalendar;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Locale;
44  import java.util.Map;
45  import java.util.StringTokenizer;
46  
47  import org.apache.commons.lang.StringUtils;
48  
49  import fr.paris.lutece.plugins.whatsnew.business.IWhatsNew;
50  import fr.paris.lutece.plugins.whatsnew.business.PortletDocumentLink;
51  import fr.paris.lutece.plugins.whatsnew.business.WhatsNewHome;
52  import fr.paris.lutece.plugins.whatsnew.business.WhatsNewType;
53  import fr.paris.lutece.plugins.whatsnew.business.WhatsNewTypeDocument;
54  import fr.paris.lutece.plugins.whatsnew.business.portlet.WhatsNewPortlet;
55  import fr.paris.lutece.plugins.whatsnew.service.portlet.WhatsNewPortletService;
56  import fr.paris.lutece.plugins.whatsnew.utils.constants.WhatsNewConstants;
57  import fr.paris.lutece.portal.business.portlet.Portlet;
58  import fr.paris.lutece.portal.business.portlet.PortletHome;
59  import fr.paris.lutece.portal.service.database.AppConnectionService;
60  import fr.paris.lutece.portal.service.plugin.Plugin;
61  import fr.paris.lutece.portal.service.plugin.PluginService;
62  import fr.paris.lutece.portal.service.spring.SpringContextService;
63  import fr.paris.lutece.portal.service.template.AppTemplateService;
64  import fr.paris.lutece.portal.service.util.AppPropertiesService;
65  import fr.paris.lutece.util.ReferenceList;
66  import fr.paris.lutece.util.html.HtmlTemplate;
67  
68  
69  /**
70   *
71   * WhatsNewService
72   *
73   */
74  public class WhatsNewService
75  {
76      private static WhatsNewService _singleton;
77  
78      /**
79       * Return the WhatsNewService singleton
80       * @return the WhatsNewService singleton
81       */
82      public static WhatsNewService getInstance(  )
83      {
84          if ( _singleton == null )
85          {
86              _singleton = new WhatsNewService(  );
87          }
88  
89          return _singleton;
90      }
91  
92      /**
93       * Init
94       */
95      public void init(  )
96      {
97      }
98  
99      /**
100      * Check if the plugin-document is activated and its pool is not null
101      * @return true if the plugin-document is activated, false otherwise
102      */
103     public boolean isPluginDocumentActivated(  )
104     {
105         boolean bPluginDocumentActivated = false;
106         Plugin pluginDocument = PluginService.getPlugin( WhatsNewConstants.DOCUMENT_PLUGIN_NAME );
107 
108         if ( PluginService.isPluginEnable( WhatsNewConstants.DOCUMENT_PLUGIN_NAME ) &&
109                 ( pluginDocument.getDbPoolName(  ) != null ) &&
110                 !AppConnectionService.NO_POOL_DEFINED.equals( pluginDocument.getDbPoolName(  ) ) )
111         {
112             bPluginDocumentActivated = true;
113         }
114 
115         return bPluginDocumentActivated;
116     }
117 
118     /**
119      * Check if the given page is out of date
120      * @param nPageId the page ID
121      * @param limitTimestamp the date limit
122      * @return true if it is out of date, false otherwise
123      */
124     public boolean isPageOutOfDate( int nPageId, Timestamp limitTimestamp )
125     {
126         return WhatsNewHome.isPageOutOfDate( nPageId, limitTimestamp );
127     }
128 
129     /**
130      * Check if the given portlet is out of date
131      * @param nPortletId the portlet ID
132      * @param limitTimestamp the date limit
133      * @return true if it is out of date, false otherwise
134      */
135     public boolean isPortletOutOfDate( int nPortletId, Timestamp limitTimestamp )
136     {
137         return WhatsNewHome.isPortletOutOfDate( nPortletId, limitTimestamp );
138     }
139 
140     /**
141      * Check if the given document is out of date
142      * @param pdLink {@link PortletDocumentLink}
143      * @param limitTimestamp the date limit
144      * @param plugin {@link Plugin}
145      * @return true if it is out of date, false otherwise
146      */
147     public boolean isDocumentOutOfDate( PortletDocumentLink pdLink, Timestamp limitTimestamp, Plugin plugin )
148     {
149         return WhatsNewHome.isDocumentOutOfDate( pdLink, limitTimestamp, plugin );
150     }
151 
152     /**
153      * Initialize the number of days' combo
154      * @return the html code of the combo
155      */
156     public ReferenceList getComboDays(  )
157     {
158         // Returns the list stored in the property file and the default value
159         String strListe = AppPropertiesService.getProperty( WhatsNewConstants.PROPERTY_FRAGMENT_DAYS_COMBO_LIST );
160 
161         ReferenceList comboDaysList = new ReferenceList(  );
162 
163         StringTokenizer strTokSemiColon = new StringTokenizer( strListe, WhatsNewConstants.DELIMITER_SEMI_COLON );
164 
165         while ( strTokSemiColon.hasMoreTokens(  ) )
166         {
167             StringTokenizer strTokComa = new StringTokenizer( strTokSemiColon.nextToken(  ),
168                     WhatsNewConstants.DELIMITER_COMA );
169 
170             while ( strTokComa.hasMoreTokens(  ) )
171             {
172                 comboDaysList.addItem( Integer.parseInt( strTokComa.nextToken(  ) ), strTokComa.nextToken(  ) );
173             }
174         }
175 
176         return comboDaysList;
177     }
178 
179     /**
180      * Get the list of WhatsNewType
181      * @param locale {@link Locale}
182      * @return a list of {@link WhatsNewType}
183      */
184     public List<WhatsNewType> getWhatsNewType( Locale locale )
185     {
186         List<WhatsNewType> listWhatsNewTypes = new ArrayList<WhatsNewType>(  );
187 
188         for ( IWhatsNew whatsNew : SpringContextService.getBeansOfType( IWhatsNew.class ) )
189         {
190             whatsNew.setWhatsNewType( locale );
191             listWhatsNewTypes.add( whatsNew.getWhatsNewType(  ) );
192         }
193 
194         return listWhatsNewTypes;
195     }
196 
197     /**
198      * Get the timestamp correspnding to a given day in the past.
199      * It is calculated from a period to remove from the current date.
200      * @param nDays the number of days between the current day and the timestamp to calculate.
201      * @param locale {@link Locale}
202      * @return the timestamp corresponding to the limit in the past for the period given in days, with hours, minutesn sec. and millisec. set to 0.
203      */
204     public Timestamp getTimestampFromPeriodAndCurrentDate( int nDays, Locale locale )
205     {
206         Calendar currentCalendar = new GregorianCalendar( locale );
207         currentCalendar.set( Calendar.HOUR_OF_DAY, 0 );
208         currentCalendar.set( Calendar.MINUTE, 0 );
209         currentCalendar.set( Calendar.SECOND, 0 );
210         currentCalendar.set( Calendar.MILLISECOND, 0 );
211 
212         currentCalendar.add( Calendar.DATE, -nDays );
213 
214         Timestamp timestampCurrent = new Timestamp( currentCalendar.getTimeInMillis(  ) );
215 
216         return timestampCurrent;
217     }
218 
219     /**
220      * Get the pages by criteria
221      * @param limitTimestamp the limit time
222      * @param locale {@link Locale}
223      * @return a collection of {@link IWhatsNew}
224      */
225     public Collection<IWhatsNew> getPagesByCriterias( Timestamp limitTimestamp, Locale locale )
226     {
227         return WhatsNewHome.selectPagesByCriterias( limitTimestamp, locale );
228     }
229 
230     /**
231      * Get the portlets by criteria
232      * @param limitTimestamp the limit time
233      * @param locale {@link Locale}
234      * @return a collection of {@link IWhatsNew}
235      */
236     public Collection<IWhatsNew> getPortletsByCriterias( Timestamp limitTimestamp, Locale locale )
237     {
238         return WhatsNewHome.selectPortletsByCriterias( limitTimestamp, locale );
239     }
240 
241     /**
242      * Get the documents by criteria
243      * @param limitTimestamp the limit time
244      * @param locale {@link Locale}
245      * @return a collection of {@link IWhatsNew}
246      */
247     public Collection<IWhatsNew> getDocumentsByCriterias( Timestamp limitTimestamp, Locale locale )
248     {
249     	Collection<IWhatsNew> listWhatsNews = WhatsNewHome.selectDocumentsByCriterias( limitTimestamp, locale );
250     	for ( IWhatsNew whatsNew : listWhatsNews )
251     	{
252     		Portlet portlet = PortletHome.findByPrimaryKey( whatsNew.getPortletId(  ) );
253     		if ( portlet != null )
254     		{
255     			WhatsNewTypeDocument document = (WhatsNewTypeDocument) whatsNew;
256     			document.setAssociatedPortletName( portlet.getName(  ) );
257     		}
258     	}
259         return listWhatsNews;
260     }
261 
262     /**
263      * Get the moderated portlets from the given whatsNewPortletId
264      * @param nWhatsNewPortletId the ID of the portlet
265      * @param locale {@link Locale}
266      * @return a collection of {@link IWhatsNew}
267      */
268     public Collection<IWhatsNew> getModeratedPortlets( int nWhatsNewPortletId, Locale locale )
269     {
270         Plugin plugin = PluginService.getPlugin( WhatsNewPlugin.PLUGIN_NAME );
271         List<Integer> listPortletIds = WhatsNewPortletService.getInstance(  )
272                                                              .getPortletIdsFromWhatsNewId( nWhatsNewPortletId, plugin );
273 
274         return WhatsNewHome.selectPortlets( listPortletIds, locale );
275     }
276 
277     /**
278      * Get the moderated pages from the given whatsNewPortletId
279      * @param nWhatsNewPortletId the ID of the page
280      * @param locale {@link Locale}
281      * @return a collection of {@link IWhatsNew}
282      */
283     public Collection<IWhatsNew> getModeratedPages( int nWhatsNewPortletId, Locale locale )
284     {
285         Plugin plugin = PluginService.getPlugin( WhatsNewPlugin.PLUGIN_NAME );
286         List<Integer> listPagesIds = WhatsNewPortletService.getInstance(  )
287                                                            .getPageIdsFromWhatsNewId( nWhatsNewPortletId, plugin );
288 
289         return WhatsNewHome.selectPages( listPagesIds, locale );
290     }
291 
292     /**
293      * Get the moderated document from the given whatsNewPortletId
294      * @param nWhatsNewPortletId the ID of the portlet
295      * @param locale {@link Locale}
296      * @return a collection of {@link IWhatsNew}
297      */
298     public Collection<IWhatsNew> getModeratedDocuments( int nWhatsNewPortletId, Locale locale )
299     {
300         Plugin plugin = PluginService.getPlugin( WhatsNewPlugin.PLUGIN_NAME );
301         List<PortletDocumentLink> listPortletDocumentLinks = WhatsNewPortletService.getInstance(  )
302                                                                                    .getDocumentsFromWhatsNewId( nWhatsNewPortletId,
303                 plugin );
304 
305         return WhatsNewHome.selectDocuments( listPortletDocumentLinks, locale );
306     }
307 
308     /**
309      * Get the HTML code of the moderated elements list
310      * @param whatsNewPortlet the portlet
311      * @param locale {@link Locale}
312      * @return a HTML code
313      */
314     public String getModeratedElementsListHtml( WhatsNewPortlet whatsNewPortlet, Locale locale )
315     {
316         StringBuilder sbHtml = new StringBuilder(  );
317 
318         if ( !whatsNewPortlet.getDynamic(  ) )
319         {
320             Timestamp limitTimestamp = getTimestampFromPeriodAndCurrentDate( whatsNewPortlet.getPeriod(  ), locale );
321 
322             // PAGES
323             if ( whatsNewPortlet.getShowPages(  ) )
324             {
325             	IWhatsNew whatsNew = (IWhatsNew) SpringContextService.getBean( WhatsNewConstants.BEAN_WHATSNEW_TYPE_PAGE );
326                 whatsNew.setWhatsNewType( locale );
327                 List<IWhatsNew> listElements = (List<IWhatsNew>) getPagesByCriterias( limitTimestamp, locale );
328                 List<IWhatsNew> listModeratedElements = (List<IWhatsNew>) getModeratedPages( whatsNewPortlet.getId(  ), locale );
329                 sbHtml.append( getHtmlModeratedElements( whatsNew, listElements, listModeratedElements, locale ) );
330             }
331             
332             // PORTLETS
333             if ( whatsNewPortlet.getShowPortlets(  ) )
334             {
335             	IWhatsNew whatsNew = (IWhatsNew) SpringContextService.getBean( WhatsNewConstants.BEAN_WHATSNEW_TYPE_PORTLET );
336                 whatsNew.setWhatsNewType( locale );
337                 List<IWhatsNew> listElements = (List<IWhatsNew>) getPortletsByCriterias( limitTimestamp, locale );
338                 List<IWhatsNew> listModeratedElements = (List<IWhatsNew>) getModeratedPortlets( whatsNewPortlet.getId(  ), locale );
339                 sbHtml.append( getHtmlModeratedElements( whatsNew, listElements, listModeratedElements, locale ) );
340             }
341             
342             // DOCUMENTS
343             if ( whatsNewPortlet.getShowDocuments(  ) )
344             {
345             	IWhatsNew whatsNew = (IWhatsNew) SpringContextService.getBean( WhatsNewConstants.BEAN_WHATSNEW_TYPE_DOCUMENT );
346                 whatsNew.setWhatsNewType( locale );
347                 List<IWhatsNew> listElements = (List<IWhatsNew>) getDocumentsByCriterias( limitTimestamp, locale );
348                 List<IWhatsNew> listModeratedElements = (List<IWhatsNew>) getModeratedDocuments( whatsNewPortlet.getId(  ), locale );
349                 sbHtml.append( getHtmlModeratedElements( whatsNew, listElements, listModeratedElements, locale ) );
350             }
351         }
352 
353         return sbHtml.toString(  );
354     }
355 
356     /**
357      * Get the html code form the moderated element
358      * @param whatsNew {@link IWhatsNew}
359      * @param listElements the list of elements
360      * @param listModeratedElements the list of moderated elements
361      * @param locale {@link Locale}
362      * @return a HTML code
363      */
364     private String getHtmlModeratedElements( IWhatsNew whatsNew, List<IWhatsNew> listElements,
365         List<IWhatsNew> listModeratedElements, Locale locale )
366     {
367         String strHtml = StringUtils.EMPTY;
368 
369         if ( ( listElements != null ) && ( whatsNew != null ) )
370         {
371             Map<String, Object> model = new HashMap<String, Object>(  );
372             model.put( WhatsNewConstants.MARK_ELEMENTS_LIST, listElements );
373             model.put( WhatsNewConstants.MARK_MODERATED_ELEMENTS_LIST, listModeratedElements );
374 
375             HtmlTemplate template = AppTemplateService.getTemplate( whatsNew.getTemplateModeratedElement(  ), locale,
376                     model );
377             strHtml = template.getHtml(  );
378         }
379 
380         return strHtml;
381     }
382 }