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.MultiAgenda;
38  import fr.paris.lutece.plugins.calendar.business.parameter.CalendarParameterHome;
39  import fr.paris.lutece.plugins.calendar.service.cache.CalendarCacheService;
40  import fr.paris.lutece.plugins.calendar.web.Constants;
41  import fr.paris.lutece.portal.business.user.AdminUser;
42  import fr.paris.lutece.portal.service.cache.ICacheKeyService;
43  import fr.paris.lutece.portal.service.plugin.Plugin;
44  import fr.paris.lutece.portal.service.plugin.PluginService;
45  import fr.paris.lutece.portal.service.security.SecurityService;
46  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
47  import fr.paris.lutece.portal.web.PortalJspBean;
48  
49  import java.util.ArrayList;
50  import java.util.HashMap;
51  import java.util.List;
52  import java.util.Map;
53  
54  import javax.servlet.http.HttpServletRequest;
55  
56  import org.apache.commons.lang.StringUtils;
57  
58  
59  /**
60   * CalendarService
61   */
62  public class CalendarService
63  {
64      // CONSTANTS
65      private static final String NONE = "-";
66      private static final String SERVICE_NAME = "Calendar Cache Service";
67  
68      // CACHE KEYS
69      private static final String KEY_CALENDAR = "calendar";
70  
71      // VARIABLES
72      private ICacheKeyService _cksCalendar;
73      private CalendarCacheService _cacheCalendar = CalendarCacheService.getInstance( );
74  
75      /** Creates a new instance of CalendarService */
76      public CalendarService( )
77      {
78          init( );
79      }
80  
81      /**
82       * Initialize the CalendarService
83       */
84      private void init( )
85      {
86          _cacheCalendar.initCache( );
87      }
88  
89      /**
90       * Get the service name
91       * @return the service name
92       */
93      public String getName( )
94      {
95          return SERVICE_NAME;
96      }
97  
98      /**
99       * Set the agenda cache key service
100      * @param cacheKeyService the cache key service
101      */
102     public void setCalendarCacheKeyService( ICacheKeyService cacheKeyService )
103     {
104         _cksCalendar = cacheKeyService;
105     }
106 
107     /**
108      * Get the agenda
109      * @param strAgendaKeyName the agenda key name
110      * @return The AgendaResource object
111      */
112     public AgendaResource getAgendaResource( String strAgendaKeyName )
113     {
114         AgendaResource agenda = null;
115         if ( StringUtils.isNotBlank( strAgendaKeyName ) && StringUtils.isNumeric( strAgendaKeyName ) )
116         {
117             int nAgendaId = Integer.parseInt( strAgendaKeyName );
118             agenda = getAgendaResource( nAgendaId );
119         }
120         return agenda;
121     }
122 
123     /**
124      * Returns the agenda
125      * @param nAgendaId the ID agenda
126      * @return The AgendaResource object
127      */
128     public AgendaResource getAgendaResource( int nAgendaId )
129     {
130         String strKey = getKey( nAgendaId );
131 
132         AgendaResource agenda = (AgendaResource) _cacheCalendar.getFromCache( strKey );
133 
134         if ( agenda == null )
135         {
136             Plugin plugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
137             agenda = CalendarHome.findAgendaResource( nAgendaId, plugin );
138             Utils.loadAgendaOccurrences( agenda, plugin );
139             _cacheCalendar.putInCache( strKey, agenda );
140         }
141 
142         return agenda;
143     }
144 
145     /**
146      * Return the list of agenda IDs
147      * @return the list of agenda IDs
148      */
149     public List<Integer> getAgendaIds( )
150     {
151         String strKey = getKey( );
152         List<Integer> listIds = (List<Integer>) _cacheCalendar.getFromCache( strKey );
153         if ( listIds == null )
154         {
155             Plugin plugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
156             listIds = CalendarHome.findCalendarIds( plugin );
157             _cacheCalendar.putInCache( strKey, listIds );
158         }
159 
160         return listIds;
161     }
162 
163     /**
164      * Get the multiAgenda. Only the agenda the user has access to are put
165      * in the multiagenda (same role of the agenda, or the user has the role
166      * manager)
167      * @param request {@link HttpServletRequest}
168      * @return the multiagenda
169      */
170     public MultiAgenda getMultiAgenda( HttpServletRequest request )
171     {
172         MultiAgenda multiAgenda = new MultiAgenda( );
173         for ( int nAgendaId : getAgendaIds( ) )
174         {
175             AgendaResource agenda = getAgendaResource( nAgendaId );
176 
177             if ( agenda != null && agenda.getAgenda( ) != null )
178             {
179                 // Check security access
180                 String strRole = agenda.getRole( );
181 
182                 if ( StringUtils.isNotBlank( strRole ) && request != null
183                         && !Constants.PROPERTY_ROLE_NONE.equals( strRole ) )
184                 {
185                     if ( SecurityService.isAuthenticationEnable( ) )
186                     {
187                         if ( SecurityService.getInstance( ).isUserInRole( request, strRole ) )
188                         {
189                             multiAgenda.addAgenda( agenda.getAgenda( ) );
190                         }
191                     }
192                 }
193                 else
194                 {
195                     multiAgenda.addAgenda( agenda.getAgenda( ) );
196                 }
197             }
198         }
199         return multiAgenda;
200     }
201 
202     /**
203      * Get the list of agendas. Only the agenda the user has access to are put
204      * in the multiagenda (same role of the agenda, or the user has the role
205      * manager)
206      * @param request {@link HttpServletRequest}
207      * @return the list of agenda
208      */
209     public List<AgendaResource> getAgendaResources( HttpServletRequest request )
210     {
211         List<AgendaResource> listAgendas = new ArrayList<AgendaResource>( );
212         for ( int nAgendaId : getAgendaIds( ) )
213         {
214             AgendaResource agenda = getAgendaResource( nAgendaId );
215 
216             if ( agenda != null && agenda.getAgenda( ) != null )
217             {
218                 // Check security access
219                 String strRole = agenda.getRole( );
220 
221                 if ( StringUtils.isNotBlank( strRole ) && request != null
222                         && !Constants.PROPERTY_ROLE_NONE.equals( strRole ) )
223                 {
224                     if ( SecurityService.isAuthenticationEnable( ) )
225                     {
226                         if ( SecurityService.getInstance( ).isUserInRole( request, strRole ) )
227                         {
228                             listAgendas.add( agenda );
229                         }
230                     }
231                 }
232                 else
233                 {
234                     listAgendas.add( agenda );
235                 }
236             }
237         }
238         return listAgendas;
239     }
240 
241     /**
242      * Get the list of agenda resources. Returns only the AgendaResource
243      * that the AdminUser has the right to (WorkingGroup wise).
244      * @param user the {@link AdminUser}
245      * @param plugin {@link Plugin}
246      * @return a list of {@AgendaResource}
247      */
248     public List<AgendaResource> getAgendaResources( AdminUser user, Plugin plugin )
249     {
250         List<AgendaResource> listAgendas = CalendarHome.findAgendaResourcesList( plugin );
251         listAgendas = (List<AgendaResource>) AdminWorkgroupService.getAuthorizedCollection( listAgendas, user );
252 
253         return listAgendas;
254     }
255 
256     /**
257      * Get the calendar default parameters
258      * @param plugin {@link Plugin}
259      * @return a map that defines the plugin-calendar parameters
260      */
261     public Map<String, String> getCalendarParameters( Plugin plugin )
262     {
263         return CalendarParameterHome.findAll( plugin );
264     }
265 
266     /**
267      * Create an agenda and reset the caches.
268      * @param agenda the {@link AgendaResource}
269      * @param plugin {@link Plugin}
270      */
271     public void doCreateAgenda( AgendaResource agenda, Plugin plugin )
272     {
273         CalendarHome.createAgenda( agenda, plugin );
274 
275         // Reset cache
276         removeCache( );
277     }
278 
279     /**
280      * Modify an agenda and reset the caches.
281      * @param agenda {@link AgendaResource}
282      * @param plugin {@link Plugin}
283      */
284     public void doModifyAgenda( AgendaResource agenda, Plugin plugin )
285     {
286         CalendarHome.updateAgenda( agenda, plugin );
287 
288         // Reset caches
289         removeCache( );
290         removeCache( agenda.getId( ) );
291     }
292 
293     /**
294      * Remove an agenda and reset the caches.
295      * @param nAgendaId the agenda ID
296      * @param plugin {@link Plugin}
297      */
298     public void doRemoveAgenda( int nAgendaId, Plugin plugin )
299     {
300         CalendarHome.removeAgenda( nAgendaId, plugin );
301 
302         // Reset caches
303         removeCache( );
304         removeCache( nAgendaId );
305     }
306 
307     /**
308      * Reset cache
309      */
310     public void resetCache( )
311     {
312         _cacheCalendar.resetCache( );
313     }
314 
315     /**
316      * Reset the cache from the given agenda ID
317      * @param strAgendaId the agenda ID
318      */
319     public void removeCache( String strAgendaId )
320     {
321         if ( StringUtils.isNotBlank( strAgendaId ) && StringUtils.isNumeric( strAgendaId ) )
322         {
323             int nAgendaId = Integer.parseInt( strAgendaId );
324             removeCache( nAgendaId );
325         }
326     }
327 
328     /**
329      * Reset the cache from the given agenda ID
330      * @param nAgendaId the agenda ID
331      */
332     public void removeCache( int nAgendaId )
333     {
334         _cacheCalendar.removeCache( getKey( nAgendaId ) );
335     }
336 
337     /**
338      * Reset the cache
339      */
340     public void removeCache( )
341     {
342         _cacheCalendar.removeCache( getKey( ) );
343     }
344 
345     /**
346      * Get the cache size
347      * @return the cache size
348      */
349     public int getCacheSize( )
350     {
351         return _cacheCalendar.getCacheSize( );
352     }
353 
354     // CACHE KEYS
355 
356     /**
357      * Get the cache key for the agenda
358      * @param nAgendaId the ID of the agenda
359      * @return the key
360      */
361     private String getKey( int nAgendaId )
362     {
363         Map<String, String> mapParams = new HashMap<String, String>( );
364         mapParams.put( KEY_CALENDAR, Integer.toString( nAgendaId ) );
365 
366         return _cksCalendar.getKey( mapParams, PortalJspBean.MODE_HTML, null );
367     }
368 
369     /**
370      * Get the cache key for the agenda
371      * @return the key
372      */
373     private String getKey( )
374     {
375         Map<String, String> mapParams = new HashMap<String, String>( );
376         mapParams.put( KEY_CALENDAR, NONE );
377 
378         return _cksCalendar.getKey( mapParams, PortalJspBean.MODE_HTML, null );
379     }
380 }