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.service;
35  
36  import fr.paris.lutece.plugins.calendar.business.CalendarHome;
37  import fr.paris.lutece.plugins.calendar.business.OccurrenceEvent;
38  import fr.paris.lutece.plugins.calendar.business.SimpleEvent;
39  import fr.paris.lutece.plugins.calendar.service.cache.EventListCacheService;
40  import fr.paris.lutece.plugins.calendar.web.CalendarView;
41  import fr.paris.lutece.plugins.calendar.web.Constants;
42  import fr.paris.lutece.plugins.calendar.web.EventList;
43  import fr.paris.lutece.portal.service.cache.ICacheKeyService;
44  import fr.paris.lutece.portal.service.plugin.Plugin;
45  import fr.paris.lutece.portal.service.plugin.PluginService;
46  import fr.paris.lutece.portal.service.security.LuteceUser;
47  import fr.paris.lutece.portal.service.util.AppLogService;
48  import fr.paris.lutece.portal.service.util.AppPropertiesService;
49  import fr.paris.lutece.portal.web.PortalJspBean;
50  
51  import java.util.HashMap;
52  import java.util.List;
53  import java.util.Map;
54  
55  import org.apache.commons.lang.StringUtils;
56  
57  
58  /**
59   * This class provides an event list service.
60   */
61  public class EventListService
62  {
63      // CONSTANTS
64      private static final String KEY_CALENDAR = "calendar";
65  
66      // VARIABLES
67      private ICacheKeyService _cksEventList;
68      private EventListCacheService _cacheEventList = EventListCacheService.getInstance( );
69      private CalendarService _calendarService;
70  
71      /**
72       * Private constructor
73       */
74      public EventListService( )
75      {
76          init( );
77      }
78  
79      /**
80       * Init the service
81       */
82      private void init( )
83      {
84          _cacheEventList.initCache( );
85      }
86  
87      /**
88       * Load an eventlist object from its keyname
89       * @param nViewType The viewtype of the EventList
90       * @return An EventList object
91       */
92      public EventList newEventList( int nViewType )
93      {
94          EventList eventlist = null;
95          String strEventListKeyName = StringUtils.EMPTY;
96  
97          switch ( nViewType )
98          {
99          case CalendarView.TYPE_DAY:
100             strEventListKeyName = AppPropertiesService.getProperty( Constants.PROPERTY_EVENTLIST_VIEW_DAY );
101 
102             break;
103 
104         case CalendarView.TYPE_WEEK:
105             strEventListKeyName = AppPropertiesService.getProperty( Constants.PROPERTY_EVENTLIST_VIEW_WEEK );
106 
107             break;
108 
109         case CalendarView.TYPE_MONTH:
110             strEventListKeyName = AppPropertiesService.getProperty( Constants.PROPERTY_EVENTLIST_VIEW_MONTH );
111 
112             break;
113 
114         default:
115         }
116 
117         if ( StringUtils.isNotEmpty( strEventListKeyName ) )
118         {
119             String strClassKey = Constants.PROPERTY_EVENTLIST + strEventListKeyName + Constants.SUFFIX_CLASS;
120             String strClass = AppPropertiesService.getProperty( strClassKey );
121 
122             try
123             {
124                 eventlist = (EventList) Class.forName( strClass ).newInstance( );
125             }
126             catch ( ClassNotFoundException e )
127             {
128                 AppLogService.error( e.getMessage( ), e );
129             }
130             catch ( IllegalAccessException e )
131             {
132                 AppLogService.error( e.getMessage( ), e );
133             }
134             catch ( InstantiationException e )
135             {
136                 AppLogService.error( e.getMessage( ), e );
137             }
138         }
139 
140         return eventlist;
141     }
142 
143     /**
144      * Get the list of SimpleEvent. If the cache is enable, then it will
145      * retrieve from the cache.
146      * @param nAgendaId the agenda ID
147      * @param nSortEvents 1 if it must be sorted ascendly, 0 otherwis
148      * @return a list of {@link SimpleEvent}
149      */
150     public List<SimpleEvent> getSimpleEvents( int nAgendaId, int nSortEvents )
151     {
152         String strKey = getKey( nAgendaId );
153         List<SimpleEvent> listEvents = (List<SimpleEvent>) _cacheEventList.getFromCache( strKey );
154         if ( listEvents == null )
155         {
156             Plugin plugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
157             listEvents = CalendarHome.findEventsList( nAgendaId, nSortEvents, plugin );
158             _cacheEventList.putInCache( strKey, listEvents );
159         }
160 
161         return listEvents;
162     }
163 
164     /**
165      * Get the list of SimpleEvent from the user login. If the cache is enable,
166      * then it will
167      * retrieve from the cache.
168      * @param nAgendaId the agenda ID
169      * @param user the {@link LuteceUser}
170      * @return a list of {@link SimpleEvent}
171      */
172     public List<SimpleEvent> getSimpleEventsByUserLogin( int nAgendaId, LuteceUser user )
173     {
174         String strKey = getKey( nAgendaId, user );
175         List<SimpleEvent> listEvents = (List<SimpleEvent>) _cacheEventList.getFromCache( strKey );
176         if ( listEvents == null )
177         {
178             Plugin plugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
179             listEvents = CalendarHome
180                     .findEventsListByUserLogin( nAgendaId, Constants.SORT_ASC, plugin, user.getName( ) );
181             _cacheEventList.putInCache( strKey, listEvents );
182         }
183 
184         return listEvents;
185     }
186 
187     /**
188      * Get the list of OccurrenceEvent. If the cache is enable, then it will
189      * retrieve from the cache.
190      * @param nAgendaId the agenda ID
191      * @param nEventId the event ID
192      * @param nIsAscSort 1 if it must be sorted ascendly, 0 otherwis
193      * @param plugin {@link Plugin}
194      * @return a list of {@link OccurrenceEvent}
195      */
196     public List<OccurrenceEvent> getOccurrenceEvents( int nAgendaId, int nEventId, int nIsAscSort, Plugin plugin )
197     {
198         return CalendarHome.findOccurrencesList( nAgendaId, nEventId, nIsAscSort, plugin );
199     }
200 
201     /**
202      * Get the event from a given event ID.
203      * @param nEventId the event ID
204      * @param plugin {@link Plugin}
205      * @return a {@link SimpleEvent}
206      */
207     public SimpleEvent getEvent( int nEventId, Plugin plugin )
208     {
209         return CalendarHome.findEvent( nEventId, plugin );
210     }
211 
212     /**
213      * Get the number repitition days of an event from a given event ID
214      * @param nEventId the event id
215      * @param plugin {@link Plugin}
216      * @return the number of repitition day
217      */
218     public int getRepititionDays( int nEventId, Plugin plugin )
219     {
220         return CalendarHome.getRepetitionDays( nEventId, plugin );
221     }
222 
223     /**
224      * Add an event and reset the caches
225      * @param event the event
226      * @param user the {@link LuteceUser}
227      * @param plugin {@link Plugin}
228      */
229     public void doAddEvent( SimpleEvent event, LuteceUser user, Plugin plugin )
230     {
231         String strUserLogin = ( user == null ) ? StringUtils.EMPTY : user.getName( );
232         CalendarHome.createEvent( event, plugin, strUserLogin );
233 
234         // Reset caches
235         _cacheEventList.removeCache( getKey( event.getIdCalendar( ) ) );
236         if ( user != null )
237         {
238             _cacheEventList.removeCache( getKey( event.getIdCalendar( ), user ) );
239         }
240         _calendarService.removeCache( event.getIdCalendar( ) );
241     }
242 
243     /**
244      * Modify an OccurrenceEvent and reset the caches
245      * @param occurrenceEvent the event
246      * @param plugin {@link Plugin}
247      */
248     public void doModifyOccurrenceEvent( OccurrenceEvent occurrenceEvent, Plugin plugin )
249     {
250         CalendarHome.updateOccurrence( occurrenceEvent, plugin );
251 
252         // Reset caches
253         _calendarService.removeCache( occurrenceEvent.getIdCalendar( ) );
254     }
255 
256     /**
257      * Modify a SimpleEvent and reset the caches
258      * @param event the event
259      * @param bPeriociteModify true if the periodicity has to be updated, false
260      *            otherwise
261      * @param user the {@link LuteceUser}
262      * @param plugin {@link Plugin}
263      */
264     public void doModifySimpleEvent( SimpleEvent event, boolean bPeriociteModify, LuteceUser user, Plugin plugin )
265     {
266         CalendarHome.updateEvent( event, bPeriociteModify, plugin );
267 
268         // Reset caches
269         _cacheEventList.removeCache( getKey( event.getIdCalendar( ) ) );
270         if ( user != null )
271         {
272             _cacheEventList.removeCache( getKey( event.getIdCalendar( ), user ) );
273         }
274         _calendarService.removeCache( event.getIdCalendar( ) );
275     }
276 
277     /**
278      * Remove an event and reset the caches
279      * @param nAgendaId the agenda ID
280      * @param nEventId the event ID
281      * @param user the {@link LuteceUser}
282      * @param plugin {@link Plugin}
283      */
284     public void doRemoveEvent( int nAgendaId, int nEventId, LuteceUser user, Plugin plugin )
285     {
286         CalendarHome.removeEvent( nAgendaId, nEventId, plugin );
287         // Reset caches
288         _cacheEventList.removeCache( getKey( nAgendaId ) );
289         if ( user != null )
290         {
291             _cacheEventList.removeCache( getKey( nAgendaId, user ) );
292         }
293     }
294 
295     // SETTERS
296 
297     /**
298      * Set the agenda cache key service
299      * @param cacheKeyService the cache key service
300      */
301     public void setEventListCacheKeyService( ICacheKeyService cacheKeyService )
302     {
303         _cksEventList = cacheKeyService;
304     }
305 
306     /**
307      * Set the calendar service
308      * @param calendarService The calendar service
309      */
310     public void setCalendarService( CalendarService calendarService )
311     {
312         _calendarService = calendarService;
313     }
314 
315     // CACHE KEYS
316 
317     /**
318      * Get the cache key for the agenda
319      * @param nAgendaId the ID of the agenda
320      * @return the key
321      */
322     private String getKey( int nAgendaId )
323     {
324         Map<String, String> mapParams = new HashMap<String, String>( );
325         mapParams.put( KEY_CALENDAR, Integer.toString( nAgendaId ) );
326 
327         return _cksEventList.getKey( mapParams, PortalJspBean.MODE_HTML, null );
328     }
329 
330     /**
331      * Get the cache key for the agenda
332      * @param nAgendaId the ID of the agenda
333      * @param user The Lutece user
334      * @return the key
335      */
336     private String getKey( int nAgendaId, LuteceUser user )
337     {
338         Map<String, String> mapParams = new HashMap<String, String>( );
339         mapParams.put( KEY_CALENDAR, Integer.toString( nAgendaId ) );
340 
341         return _cksEventList.getKey( mapParams, PortalJspBean.MODE_HTML, user );
342     }
343 }