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.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   * This class represents the business object CalendarPortlet.
68   */
69  public class MiniCalendarPortlet extends Portlet
70  {
71      // The names of the XML tags
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      //JSP
87      private static final String JSP_PORTAL_URL = "jsp/site/Portal.jsp";
88  
89      //Commons
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      //Session variable
95      private static Calendar _cal;
96  
97      /**
98       * Sets the name of the plugin associated with this portlet.
99       * 
100      * @param strPluginName The plugin name.
101      */
102     public void setPluginName( String strPluginName )
103     {
104         super.setPluginName( strPluginName );
105     }
106 
107     /**
108      * Returns the Xml code of the Archive portlet with XML heading
109      * 
110      * @param request The HTTP servlet request
111      * @return the Xml code of the Archive portlet
112      */
113     public String getXmlDocument( HttpServletRequest request )
114     {
115         return XmlUtil.getXmlHeader( ) + getXml( request );
116     }
117 
118     /**
119      * Returns the Xml code of the Calendar portlet without XML heading
120      * 
121      * @param request The HTTP servlet request
122      * @return the Xml code of the Archive portlet content
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         //Load the xml calendar	    	
158         strXml.append( XMLUtils.getXMLPortletCalendar( locale, _cal, request ) );
159 
160         //Top event section            
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                 //Register the image
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         // If there is no date begin in the parameter, then get today's date
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         // If there is a date end, then it is a search on a date interval
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             //Sort events by DateTimeStart using bubble sort
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                 // Search on date interval, then display date begin and date end
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                 // Else only display the date on which the user has clicked
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      * Updates the current instance of the CalendarPortlet object
298      */
299     public void update( )
300     {
301         MiniCalendarPortletHome.getInstance( ).update( this );
302     }
303 
304     /**
305      * Removes the current instance of the CalendarPortlet object
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 }