1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
72
73
74 public class WhatsNewService
75 {
76 private static WhatsNewService _singleton;
77
78
79
80
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
94
95 public void init( )
96 {
97 }
98
99
100
101
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
120
121
122
123
124 public boolean isPageOutOfDate( int nPageId, Timestamp limitTimestamp )
125 {
126 return WhatsNewHome.isPageOutOfDate( nPageId, limitTimestamp );
127 }
128
129
130
131
132
133
134
135 public boolean isPortletOutOfDate( int nPortletId, Timestamp limitTimestamp )
136 {
137 return WhatsNewHome.isPortletOutOfDate( nPortletId, limitTimestamp );
138 }
139
140
141
142
143
144
145
146
147 public boolean isDocumentOutOfDate( PortletDocumentLink pdLink, Timestamp limitTimestamp, Plugin plugin )
148 {
149 return WhatsNewHome.isDocumentOutOfDate( pdLink, limitTimestamp, plugin );
150 }
151
152
153
154
155
156 public ReferenceList getComboDays( )
157 {
158
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
181
182
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
199
200
201
202
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
221
222
223
224
225 public Collection<IWhatsNew> getPagesByCriterias( Timestamp limitTimestamp, Locale locale )
226 {
227 return WhatsNewHome.selectPagesByCriterias( limitTimestamp, locale );
228 }
229
230
231
232
233
234
235
236 public Collection<IWhatsNew> getPortletsByCriterias( Timestamp limitTimestamp, Locale locale )
237 {
238 return WhatsNewHome.selectPortletsByCriterias( limitTimestamp, locale );
239 }
240
241
242
243
244
245
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
264
265
266
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
279
280
281
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
294
295
296
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
310
311
312
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
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
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
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
358
359
360
361
362
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 }