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.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
61
62 public class CalendarService
63 {
64
65 private static final String NONE = "-";
66 private static final String SERVICE_NAME = "Calendar Cache Service";
67
68
69 private static final String KEY_CALENDAR = "calendar";
70
71
72 private ICacheKeyService _cksCalendar;
73 private CalendarCacheService _cacheCalendar = CalendarCacheService.getInstance( );
74
75
76 public CalendarService( )
77 {
78 init( );
79 }
80
81
82
83
84 private void init( )
85 {
86 _cacheCalendar.initCache( );
87 }
88
89
90
91
92
93 public String getName( )
94 {
95 return SERVICE_NAME;
96 }
97
98
99
100
101
102 public void setCalendarCacheKeyService( ICacheKeyService cacheKeyService )
103 {
104 _cksCalendar = cacheKeyService;
105 }
106
107
108
109
110
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
125
126
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
147
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
165
166
167
168
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
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
204
205
206
207
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
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
243
244
245
246
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
258
259
260
261 public Map<String, String> getCalendarParameters( Plugin plugin )
262 {
263 return CalendarParameterHome.findAll( plugin );
264 }
265
266
267
268
269
270
271 public void doCreateAgenda( AgendaResource agenda, Plugin plugin )
272 {
273 CalendarHome.createAgenda( agenda, plugin );
274
275
276 removeCache( );
277 }
278
279
280
281
282
283
284 public void doModifyAgenda( AgendaResource agenda, Plugin plugin )
285 {
286 CalendarHome.updateAgenda( agenda, plugin );
287
288
289 removeCache( );
290 removeCache( agenda.getId( ) );
291 }
292
293
294
295
296
297
298 public void doRemoveAgenda( int nAgendaId, Plugin plugin )
299 {
300 CalendarHome.removeAgenda( nAgendaId, plugin );
301
302
303 removeCache( );
304 removeCache( nAgendaId );
305 }
306
307
308
309
310 public void resetCache( )
311 {
312 _cacheCalendar.resetCache( );
313 }
314
315
316
317
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
330
331
332 public void removeCache( int nAgendaId )
333 {
334 _cacheCalendar.removeCache( getKey( nAgendaId ) );
335 }
336
337
338
339
340 public void removeCache( )
341 {
342 _cacheCalendar.removeCache( getKey( ) );
343 }
344
345
346
347
348
349 public int getCacheSize( )
350 {
351 return _cacheCalendar.getCacheSize( );
352 }
353
354
355
356
357
358
359
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
371
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 }