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.calendar.business.portlet;
35
36 import java.text.SimpleDateFormat;
37 import java.util.Calendar;
38 import java.util.Date;
39 import java.util.GregorianCalendar;
40 import java.util.Iterator;
41 import java.util.List;
42 import java.util.Locale;
43
44 import javax.servlet.http.HttpServletRequest;
45
46 import org.apache.commons.lang.StringUtils;
47
48 import fr.paris.lutece.plugins.calendar.business.CalendarHome;
49 import fr.paris.lutece.plugins.calendar.business.Event;
50 import fr.paris.lutece.plugins.calendar.business.SimpleEvent;
51 import fr.paris.lutece.plugins.calendar.service.CalendarPlugin;
52 import fr.paris.lutece.plugins.calendar.service.EventImageResourceService;
53 import fr.paris.lutece.plugins.calendar.service.Utils;
54 import fr.paris.lutece.plugins.calendar.service.XMLUtils;
55 import fr.paris.lutece.plugins.calendar.service.search.CalendarSearchService;
56 import fr.paris.lutece.plugins.calendar.web.Constants;
57 import fr.paris.lutece.portal.business.portlet.Portlet;
58 import fr.paris.lutece.portal.service.i18n.I18nService;
59 import fr.paris.lutece.portal.service.plugin.Plugin;
60 import fr.paris.lutece.portal.service.plugin.PluginService;
61 import fr.paris.lutece.portal.service.util.AppPathService;
62 import fr.paris.lutece.util.date.DateUtil;
63 import fr.paris.lutece.util.xml.XmlUtil;
64
65
66
67
68
69 public class MiniCalendarPortlet extends Portlet
70 {
71
72 private static final String TAG_EVENTS = "events";
73 private static final String TAG_URL = "url";
74 private static final String TAG_EVENT_ID = "event-id";
75 private static final String TAG_AGENDA_ID = "agenda-id";
76 private static final String TAG_TOP_EVENTS = "top-events";
77 private static final String TAG_TOP_EVENT = "top-event";
78 private static final String TAG_TOP_EVENT_TITLE = "top-event-title";
79 private static final String TAG_EVENT = "event";
80 private static final String TAG_EVENT_TITLE = "event-title";
81 private static final String TAG_EVENT_DATETIME_BEGIN = "event-datetime-begin";
82 private static final String TAG_EVENT_DESCRIPTION = "event-description";
83 private static final String TAG_DATE = "date";
84 private static final String TAG_DATE_END = "date-end";
85
86
87 private static final String JSP_PORTAL_URL = "jsp/site/Portal.jsp";
88
89
90 private static final String POINT_INTERROGATION = "?";
91 private static final String EGAL = "=";
92 private static final String PARAM_PAGE_ID = "page_id";
93
94
95 private static Calendar _cal;
96
97
98
99
100
101
102 public void setPluginName( String strPluginName )
103 {
104 super.setPluginName( strPluginName );
105 }
106
107
108
109
110
111
112
113 public String getXmlDocument( HttpServletRequest request )
114 {
115 return XmlUtil.getXmlHeader( ) + getXml( request );
116 }
117
118
119
120
121
122
123
124 public String getXml( HttpServletRequest request )
125 {
126 StringBuffer strXml = new StringBuffer( );
127 Locale locale = null;
128
129 if ( _cal == null )
130 {
131 _cal = new GregorianCalendar( );
132 }
133
134 if ( request != null )
135 {
136 locale = request.getLocale( );
137
138 if ( ( request.getParameter( Constants.PARAMETER_MONTH ) != null )
139 && ( request.getParameter( Constants.PARAMETER_MONTH ) ).equals( Constants.PARAMETER_NEXT ) )
140 {
141 _cal.add( Calendar.MONTH, 1 );
142 }
143 else if ( ( request.getParameter( Constants.PARAMETER_MONTH ) != null )
144 && ( request.getParameter( Constants.PARAMETER_MONTH ) ).equals( Constants.PARAMETER_PREV ) )
145 {
146 _cal.add( Calendar.MONTH, -1 );
147 }
148
149 XmlUtil.addElement( strXml, TAG_URL, AppPathService.getBaseUrl( request ) + JSP_PORTAL_URL
150 + POINT_INTERROGATION + PARAM_PAGE_ID + EGAL );
151 }
152 else
153 {
154 locale = I18nService.getDefaultLocale( );
155 }
156
157
158 strXml.append( XMLUtils.getXMLPortletCalendar( locale, _cal, request ) );
159
160
161 Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
162
163 boolean topevent = MiniCalendarPortletHome.showTopEvent( );
164
165 if ( topevent )
166 {
167 XmlUtil.beginElement( strXml, TAG_TOP_EVENTS );
168
169 List<SimpleEvent> listEvents = CalendarHome.findTopEventList( plugin );
170 Iterator<SimpleEvent> i = listEvents.iterator( );
171
172 while ( i.hasNext( ) )
173 {
174 XmlUtil.beginElement( strXml, TAG_TOP_EVENT );
175
176 SimpleEvent event = i.next( );
177 XmlUtil.addElement( strXml, TAG_TOP_EVENT_TITLE, ( event.getTitle( ) != null ) ? event.getTitle( ) : "" );
178 XmlUtil.addElement( strXml, TAG_EVENT_ID, event.getId( ) );
179 XmlUtil.addElement( strXml, TAG_AGENDA_ID, event.getIdCalendar( ) );
180 XmlUtil.endElement( strXml, TAG_TOP_EVENT );
181
182 EventImageResourceService.getInstance( ).getResourceImageEvent( event.getId( ) );
183 }
184
185 XmlUtil.endElement( strXml, TAG_TOP_EVENTS );
186 }
187
188 String strDateBegin = request != null ? request.getParameter( Constants.PARAMETER_DATE ) : StringUtils.EMPTY;
189
190
191 if ( StringUtils.isBlank( strDateBegin ) )
192 {
193 strDateBegin = DateUtil.getCurrentDateString( locale );
194 }
195
196 String[] arrayCategory = null;
197 String[] arrayCalendar = Utils.getCalendarIds( request );
198 String strQuery = StringUtils.EMPTY;
199 List<Event> listEvent = null;
200 Plugin pluginCalendar = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
201
202 Date dateBegin = DateUtil.formatDate( strDateBegin,
203 request != null ? request.getLocale( ) : Locale.getDefault( ) );
204
205 String strDateEnd = request != null ? request.getParameter( Constants.PARAMETER_DATE_END ) : StringUtils.EMPTY;
206 Date dateEnd = null;
207 if ( StringUtils.isNotBlank( strDateEnd ) )
208 {
209 dateEnd = DateUtil.formatDate( strDateEnd, request != null ? request.getLocale( ) : Locale.getDefault( ) );
210 }
211 if ( dateEnd == null )
212 {
213 dateEnd = dateBegin;
214 }
215
216 listEvent = CalendarSearchService.getInstance( ).getSearchResults( arrayCalendar, arrayCategory, strQuery,
217 dateBegin, dateEnd, pluginCalendar );
218 if ( listEvent != null )
219 {
220
221 SimpleDateFormat sdf = new SimpleDateFormat( "HH:mm" );
222 boolean bisModification;
223 Date date1;
224 Date date2;
225 Event temporaryEvent;
226 do
227 {
228 bisModification = true;
229 for ( int j = 0; j < listEvent.size( ) - 1; j++ )
230 {
231 try
232 {
233
234 date1 = sdf.parse( listEvent.get( j ).getDateTimeStart( ) );
235 }
236 catch ( Exception e )
237 {
238 date1 = new Date( 0 );
239 }
240 try
241 {
242
243 date2 = sdf.parse( listEvent.get( j + 1 ).getDateTimeStart( ) );
244 }
245 catch ( Exception e )
246 {
247 date2 = new Date( 0 );
248 }
249 if ( date1.after( date2 ) )
250 {
251 temporaryEvent = listEvent.get( j + 1 );
252 listEvent.set( j + 1, listEvent.get( j ) );
253 listEvent.set( j, temporaryEvent );
254 bisModification = false;
255 }
256
257 }
258
259 }
260 while ( !bisModification );
261
262 XmlUtil.beginElement( strXml, TAG_EVENTS );
263 for ( Event event : listEvent )
264 {
265 XmlUtil.beginElement( strXml, TAG_EVENT );
266
267 if ( StringUtils.isNotBlank( strDateEnd ) && !strDateBegin.equals( strDateEnd ) )
268 {
269 XmlUtil.addElement( strXml, TAG_DATE, DateUtil.getDateString( event.getDate( ), locale ) );
270 XmlUtil.addElement( strXml, TAG_DATE_END, DateUtil.getDateString( event.getDateEnd( ), locale ) );
271 }
272
273 else
274 {
275 XmlUtil.addElement( strXml, TAG_DATE, strDateBegin );
276 XmlUtil.addElement( strXml, TAG_DATE_END, StringUtils.EMPTY );
277 }
278 XmlUtil.addElementHtml( strXml, TAG_EVENT_TITLE, ( event.getTitle( ) != null ) ? event.getTitle( )
279 : StringUtils.EMPTY );
280 XmlUtil.addElement( strXml, TAG_EVENT_DATETIME_BEGIN,
281 ( event.getDateTimeStart( ) != null ) ? event.getDateTimeStart( ) : StringUtils.EMPTY );
282 XmlUtil.addElementHtml( strXml, TAG_EVENT_DESCRIPTION,
283 ( event.getDescription( ) != null ) ? event.getDescription( ) : StringUtils.EMPTY );
284 XmlUtil.addElement( strXml, TAG_EVENT_ID, event.getId( ) );
285 XmlUtil.endElement( strXml, TAG_EVENT );
286
287 }
288 XmlUtil.endElement( strXml, TAG_EVENTS );
289 }
290
291 String str = addPortletTags( strXml );
292
293 return str;
294 }
295
296
297
298
299 public void update( )
300 {
301 MiniCalendarPortletHome.getInstance( ).update( this );
302 }
303
304
305
306
307 public void remove( )
308 {
309 MiniCalendarPortletHome.getInstance( ).remove( this );
310 }
311
312 @Override
313 public boolean canBeCachedForAnonymousUsers( )
314 {
315 return false;
316 }
317
318 @Override
319 public boolean canBeCachedForConnectedUsers( )
320 {
321 return false;
322 }
323 }