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.portal.service.datastore;
35
36 import fr.paris.lutece.portal.business.datastore.DataEntity;
37 import fr.paris.lutece.portal.business.datastore.DataEntityHome;
38 import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
39 import fr.paris.lutece.portal.service.template.FreeMarkerTemplateService;
40 import fr.paris.lutece.portal.service.util.AppLogService;
41 import fr.paris.lutece.portal.service.util.AppPathService;
42 import fr.paris.lutece.portal.service.util.NoDatabaseException;
43 import fr.paris.lutece.util.ReferenceList;
44
45 import java.util.List;
46 import java.util.regex.Matcher;
47 import java.util.regex.Pattern;
48
49
50
51
52 public final class DatastoreService
53 {
54 public static final String VALUE_TRUE = "true";
55 public static final String VALUE_FALSE = "false";
56 private static final String DATASTORE_KEY = "dskey";
57 private static final Pattern PATTERN_DATASTORE_KEY = Pattern.compile( "#" + DATASTORE_KEY + "\\{(.*?)\\}" );
58 static final String VALUE_MISSING = "DS Value Missing";
59 private static AbstractCacheableService _cache;
60 private static boolean _bDatabase = true;
61
62
63
64
65 private DatastoreService( )
66 {
67 }
68
69
70
71
72 public static void init( )
73 {
74 FreeMarkerTemplateService.getInstance( ).setSharedVariable( DATASTORE_KEY, new DatastoreTemplateMethod( ) );
75 }
76
77
78
79
80
81
82
83
84
85
86 public static String getDataValue( String strKey, String strDefault )
87 {
88 try
89 {
90 if ( _bDatabase )
91 {
92 DataEntity entity = null;
93
94 if ( _cache != null )
95 {
96 entity = (DataEntity) _cache.getFromCache( strKey );
97 }
98
99 if ( entity == null )
100 {
101 entity = DataEntityHome.findByPrimaryKey( strKey );
102
103 if ( entity == null )
104 {
105 return strDefault;
106 }
107
108 if ( _cache != null )
109 {
110 _cache.putInCache( strKey, entity );
111 }
112 }
113
114 return entity.getValue( );
115 }
116 }
117 catch( NoDatabaseException e )
118 {
119 disableDatastore( e );
120 }
121
122 return strDefault;
123 }
124
125
126
127
128
129
130
131
132
133
134 public static String getInstanceDataValue( String strKey, String strDefault )
135 {
136 String strInstanceKey = getInstanceKey( strKey );
137
138 return getDataValue( strInstanceKey, strDefault );
139 }
140
141
142
143
144
145
146
147
148
149 public static void setDataValue( String strKey, String strValue )
150 {
151 try
152 {
153 if ( _bDatabase )
154 {
155 DataEntityl/business/datastore/DataEntity.html#DataEntity">DataEntity p = new DataEntity( strKey, strValue );
156 DataEntity entity = DataEntityHome.findByPrimaryKey( strKey );
157
158 if ( entity != null )
159 {
160 DataEntityHome.update( p );
161
162 if ( _cache != null )
163 {
164 _cache.removeKey( strKey );
165 }
166 }
167 else
168 {
169 DataEntityHome.create( p );
170 }
171 }
172 }
173 catch( NoDatabaseException e )
174 {
175 disableDatastore( e );
176 }
177 }
178
179
180
181
182
183
184
185
186
187 public static void setInstanceDataValue( String strKey, String strValue )
188 {
189 String strInstanceKey = getInstanceKey( strKey );
190 setDataValue( strInstanceKey, strValue );
191 }
192
193
194
195
196
197
198
199 public static void removeData( String strKey )
200 {
201 try
202 {
203 if ( _bDatabase )
204 {
205 DataEntityHome.remove( strKey );
206
207 if ( _cache != null )
208 {
209 _cache.removeKey( strKey );
210 }
211 }
212 }
213 catch( NoDatabaseException e )
214 {
215 disableDatastore( e );
216 }
217 }
218
219
220
221
222
223
224
225 public static void removeInstanceData( String strKey )
226 {
227 String strInstanceKey = getInstanceKey( strKey );
228 removeData( strInstanceKey );
229 }
230
231
232
233
234
235
236
237 public static void removeDataByPrefix( String strPrefix )
238 {
239 try
240 {
241 if ( _bDatabase )
242 {
243 List<DataEntity> listEntities = DataEntityHome.findAll( );
244
245 for ( DataEntity entity : listEntities )
246 {
247 if ( entity.getKey( ).startsWith( strPrefix ) )
248 {
249 removeData( entity.getKey( ) );
250 }
251 }
252 }
253 }
254 catch( NoDatabaseException e )
255 {
256 disableDatastore( e );
257 }
258 }
259
260
261
262
263
264
265
266 public static void removeInstanceDataByPrefix( String strPrefix )
267 {
268 String strInstancePrefix = getInstanceKey( strPrefix );
269 removeDataByPrefix( strInstancePrefix );
270 }
271
272
273
274
275
276
277
278
279 public static ReferenceList getDataByPrefix( String strPrefix )
280 {
281 ReferenceListnceList.html#ReferenceList">ReferenceList list = new ReferenceList( );
282
283 try
284 {
285 if ( _bDatabase )
286 {
287 List<DataEntity> listEntities = DataEntityHome.findAll( );
288
289 for ( DataEntity entity : listEntities )
290 {
291 if ( entity.getKey( ).startsWith( strPrefix ) )
292 {
293 list.addItem( entity.getKey( ), entity.getValue( ) );
294 }
295 }
296 }
297 }
298 catch( NoDatabaseException e )
299 {
300 disableDatastore( e );
301 }
302
303 return list;
304 }
305
306
307
308
309
310
311
312
313 public static ReferenceList getInstanceDataByPrefix( String strPrefix )
314 {
315 String strInstancePrefix = getInstanceKey( strPrefix );
316
317 return getDataByPrefix( strInstancePrefix );
318 }
319
320
321
322
323
324
325
326
327 public static String replaceKeys( String strSource )
328 {
329 String result = strSource;
330
331 if ( strSource != null )
332 {
333 Matcher matcher = PATTERN_DATASTORE_KEY.matcher( strSource );
334
335 if ( matcher.find( ) )
336 {
337 StringBuffer sb = new StringBuffer( );
338
339 do
340 {
341 String strKey = matcher.group( 1 );
342 String strValue = DatastoreService.getDataValue( strKey, VALUE_MISSING );
343
344 if ( VALUE_MISSING.equals( strValue ) )
345 {
346 AppLogService.error( "Datastore Key missing : {} - Please fix to avoid performance issues.", strKey );
347 }
348
349 matcher.appendReplacement( sb, strValue );
350 }
351 while ( matcher.find( ) );
352
353 matcher.appendTail( sb );
354 result = sb.toString( );
355 }
356 }
357
358 return result;
359 }
360
361
362
363
364
365
366
367
368 public static boolean existsKey( String strKey )
369 {
370 try
371 {
372 if ( _bDatabase )
373 {
374 DataEntity entity = null;
375
376 if ( _cache != null )
377 {
378 entity = (DataEntity) _cache.getFromCache( strKey );
379 }
380
381 if ( entity == null )
382 {
383 entity = DataEntityHome.findByPrimaryKey( strKey );
384
385 if ( entity == null )
386 {
387 return false;
388 }
389 }
390
391 return true;
392 }
393 }
394 catch( NoDatabaseException e )
395 {
396 disableDatastore( e );
397 }
398
399 return false;
400 }
401
402
403
404
405
406
407
408
409 public static boolean existsInstanceKey( String strKey )
410 {
411 String strInstanceKey = getInstanceKey( strKey );
412
413 return existsKey( strInstanceKey );
414 }
415
416
417
418
419 public static void startCache( )
420 {
421 _cache = new DatastoreCacheService( );
422 AppLogService.info( "Datastore's cache started." );
423 }
424
425
426
427
428
429
430
431 private static void disableDatastore( NoDatabaseException e )
432 {
433 _bDatabase = false;
434 AppLogService.error( "##### CRITICAL ERROR ##### : Datastore has been disabled due to a NoDatabaseException catched", e );
435 }
436
437
438
439
440
441
442
443
444 private static String getInstanceKey( String strKey )
445 {
446 if ( !AppPathService.isDefaultWebappInstance( ) )
447 {
448 StringBuilder sbInstanceKey = new StringBuilder( );
449 sbInstanceKey.append( AppPathService.getWebappInstance( ) ).append( "." ).append( strKey );
450
451 return sbInstanceKey.toString( );
452 }
453
454 return strKey;
455 }
456 }