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.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
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
75
76 public WhatsNewPortlet( )
77 {
78 setPortletTypeId( WhatsNewPortletService.getInstance( ).getPortletTypeId( ) );
79 }
80
81
82
83
84
85 public boolean getShowDocuments( )
86 {
87 return _bShowDocuments;
88 }
89
90
91
92
93
94 public void setShowDocuments( boolean bShowDocuments )
95 {
96 _bShowDocuments = bShowDocuments;
97 }
98
99
100
101
102
103 public boolean getShowPortlets( )
104 {
105 return _bShowPortlets;
106 }
107
108
109
110
111
112 public void setShowPortlets( boolean bPortlets )
113 {
114 _bShowPortlets = bPortlets;
115 }
116
117
118
119
120
121 public boolean getShowPages( )
122 {
123 return _bShowPages;
124 }
125
126
127
128
129
130 public void setShowPages( boolean bPages )
131 {
132 _bShowPages = bPages;
133 }
134
135
136
137
138
139 public int getPeriod( )
140 {
141 return _nPeriod;
142 }
143
144
145
146
147
148 public void setPeriod( int nPeriod )
149 {
150 this._nPeriod = nPeriod;
151 }
152
153
154
155
156
157 public int getNbElementsMax( )
158 {
159 return _nNbElementsMax;
160 }
161
162
163
164
165
166 public void setNbElementsMax( int nElementsMax )
167 {
168 _nNbElementsMax = nElementsMax;
169 }
170
171
172
173
174
175 public int getElementsOrder( )
176 {
177 return _nElementsOrder;
178 }
179
180
181
182
183
184 public void setElementsOrder( int nOrder )
185 {
186 _nElementsOrder = nOrder;
187 }
188
189
190
191
192
193 public boolean getAscSort( )
194 {
195 return _bIsAscSort;
196 }
197
198
199
200
201
202 public void setAscSort( boolean bIsAscSort )
203 {
204 _bIsAscSort = bIsAscSort;
205 }
206
207
208
209
210
211
212 public String getXmlDocument( HttpServletRequest request )
213 {
214 return XmlUtil.getXmlHeader( ) + getXml( request );
215 }
216
217
218
219
220
221 public boolean getDynamic( )
222 {
223 return _bIsDynamic;
224 }
225
226
227
228
229
230 public void setDynamic( boolean bIsDynamic )
231 {
232 _bIsDynamic = bIsDynamic;
233 }
234
235
236
237
238
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
310
311
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
330
331 XmlUtil.addElement( strXml, WhatsNewConstants.TAG_WHATS_NEW_NUMBER_DISPLAY, _nNbElementsMax );
332
333
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
346
347 public void update( )
348 {
349 WhatsNewPortletService.getInstance( ).update( this );
350 }
351
352
353
354
355 public void remove( )
356 {
357 WhatsNewPortletService.getInstance( ).remove( this );
358 }
359 }