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.template;
35
36 import fr.paris.lutece.portal.business.template.CommonsImport;
37 import fr.paris.lutece.portal.business.template.CommonsInclude;
38 import fr.paris.lutece.portal.service.datastore.DatastoreService;
39 import fr.paris.lutece.portal.service.spring.SpringContextService;
40 import fr.paris.lutece.portal.service.util.AppLogService;
41 import fr.paris.lutece.util.ReferenceList;
42 import java.util.List;
43 import java.util.Map;
44
45 import org.apache.commons.lang3.StringUtils;
46
47
48
49
50 public class CommonsService
51 {
52 private static final String DSKEY_CURRENT_COMMONS_INCLUDE = "core.templates.currentCommonsInclude";
53
54 private CommonsService( )
55 {
56
57 }
58
59
60
61
62
63
64 public static List<CommonsInclude> getCommonsIncludes( )
65 {
66 return SpringContextService.getBeansOfType( CommonsInclude.class );
67 }
68
69
70
71
72
73
74 public static List<CommonsImport> getCommonsImports( )
75 {
76 return SpringContextService.getBeansOfType( CommonsImport.class );
77 }
78
79
80
81
82
83
84
85 public static void activateCommons( String strKey )
86 {
87 CommonsInclude comIncNew = getCommonsInclude( strKey );
88 activateCommonsInclude( comIncNew );
89 CommonsImport comImpNew = getCommonsImport( strKey );
90 activateCommonsImport( comImpNew );
91
92 if ( comIncNew != null || comImpNew != null )
93 {
94 setNewCommonsKey( strKey );
95 }
96 }
97
98
99
100
101
102
103
104 private static void activateCommonsInclude( CommonsInclude ciNew )
105 {
106 if ( ciNew == null )
107 {
108 return;
109 }
110
111 IFreeMarkerTemplateService serviceFMT = FreeMarkerTemplateService.getInstance( );
112
113 CommonsInclude ciCurrent = getCurrentCommonsInclude( );
114
115
116 List<String> listAutoIncludes = serviceFMT.getAutoIncludes( );
117 if ( ciCurrent != null )
118 {
119 for ( String strExclude : ciCurrent.getFiles( ) )
120 {
121 if ( ( listAutoIncludes != null ) && listAutoIncludes.contains( strExclude ) )
122 {
123 serviceFMT.removeAutoInclude( strExclude );
124 AppLogService.info( "Existing Freemarker AutoInclude removed : {}", strExclude );
125 }
126 }
127 }
128
129 for ( String strInclude : ciNew.getFiles( ) )
130 {
131 if ( ( listAutoIncludes != null ) && !listAutoIncludes.contains( strInclude ) )
132 {
133 serviceFMT.addAutoInclude( strInclude );
134 AppLogService.info( "New Freemarker AutoInclude added : {}", strInclude );
135 }
136 }
137 }
138
139
140
141
142
143
144
145 private static void activateCommonsImport( CommonsImport ciNew )
146 {
147 if ( ciNew == null )
148 {
149 return;
150 }
151
152 IFreeMarkerTemplateService serviceFMT = FreeMarkerTemplateService.getInstance( );
153
154 CommonsImport ciCurrent = getCurrentCommonsImport( );
155
156
157 Map<String,String> mapAutoImports = serviceFMT.getAutoImports( );
158 if ( ciCurrent != null )
159 {
160 for ( Map.Entry<String, String> mapFilesEntry : ciCurrent.getMapFiles( ).entrySet( ) )
161 {
162 serviceFMT.removeAutoImport( mapFilesEntry.getKey( ) );
163 AppLogService.info( "Existing Freemarker AutoImport removed : {} as {}", mapFilesEntry.getValue( ), mapFilesEntry.getKey( ) );
164 }
165 }
166
167 for ( Map.Entry<String, String> mapFilesEntry : ciNew.getMapFiles( ).entrySet( ) )
168 {
169 serviceFMT.addAutoImport( mapFilesEntry.getKey( ), mapFilesEntry.getValue( ) );
170 AppLogService.info( "New Freemarker AutoImport added : {} as {}", mapFilesEntry.getValue( ), mapFilesEntry.getKey( ) );
171 }
172 }
173
174
175
176
177
178
179 public static ReferenceList getCommonsList( )
180 {
181 ReferenceListnceList.html#ReferenceList">ReferenceList list = new ReferenceList( );
182 for ( CommonsInclude ci : getCommonsIncludes( ) )
183 {
184 list.addItem( ci.getKey( ), ci.getName( ) );
185 }
186 for ( CommonsImport ci : getCommonsImports( ) )
187 {
188 if ( !list.stream( ).anyMatch( item -> StringUtils.equals( item.getCode( ), ci.getKey( ) ) ) )
189 {
190 list.addItem( ci.getKey( ), ci.getName( ) );
191 }
192 }
193 return list;
194 }
195
196
197
198
199
200
201 public static String getCurrentCommonsKey( )
202 {
203 String strCurrentKey = null;
204
205 CommonsInclude comIncCurrent = getCurrentCommonsInclude( );
206
207 if ( comIncCurrent != null )
208 {
209 strCurrentKey = comIncCurrent.getKey( );
210 }
211
212 CommonsImport comImpCurrent = getCurrentCommonsImport( );
213
214 if ( comImpCurrent != null )
215 {
216 strCurrentKey = comImpCurrent.getKey( );
217 }
218
219 return strCurrentKey;
220 }
221
222
223
224
225
226
227
228
229 public static CommonsInclude getCommonsInclude( String strKey )
230 {
231 for ( CommonsInclude ci : getCommonsIncludes( ) )
232 {
233 if ( ci.getKey( ).equals( strKey ) )
234 {
235 return ci;
236 }
237 }
238 return null;
239 }
240
241
242
243
244
245
246
247
248 public static CommonsImport getCommonsImport( String strKey )
249 {
250 for ( CommonsImport ci : getCommonsImports( ) )
251 {
252 if ( ci.getKey( ).equals( strKey ) )
253 {
254 return ci;
255 }
256 }
257 return null;
258 }
259
260
261
262
263
264
265 public static CommonsInclude getDefaultCommonsInclude( )
266 {
267
268 for ( CommonsInclude ci : getCommonsIncludes( ) )
269 {
270 if ( ci.isDefault( ) )
271 {
272 return ci;
273 }
274 }
275
276
277 if ( getCommonsIncludes( ).size( ) > 0 )
278 {
279 return getCommonsIncludes( ).get( 0 );
280 }
281
282 return null;
283 }
284
285
286
287
288
289
290 public static CommonsImport getDefaultCommonsImport( )
291 {
292
293 for ( CommonsImport ci : getCommonsImports( ) )
294 {
295 if ( ci.isDefault( ) )
296 {
297 return ci;
298 }
299 }
300
301
302 if ( getCommonsImports( ).size( ) > 0 )
303 {
304 return getCommonsImports( ).get( 0 );
305 }
306
307 return null;
308 }
309
310
311
312
313
314
315 public static CommonsInclude getCurrentCommonsInclude( )
316 {
317 String strCurrentCommonsKey = DatastoreService.getInstanceDataValue( DSKEY_CURRENT_COMMONS_INCLUDE, null );
318
319 if ( strCurrentCommonsKey != null )
320 {
321 CommonsInclude ci = getCommonsInclude( strCurrentCommonsKey );
322 if ( ci != null )
323 {
324 return ci;
325 }
326 }
327
328 CommonsInclude ci = getDefaultCommonsInclude( );
329 if ( ci != null )
330 {
331 setNewCommonsKey( ci.getKey( ) );
332 return ci;
333 }
334
335 return null;
336 }
337
338
339
340
341
342
343 public static CommonsImport getCurrentCommonsImport( )
344 {
345 String strCurrentCommonsKey = DatastoreService.getInstanceDataValue( DSKEY_CURRENT_COMMONS_INCLUDE, null );
346
347 if ( strCurrentCommonsKey != null )
348 {
349 CommonsImport ci = getCommonsImport( strCurrentCommonsKey );
350 if ( ci != null )
351 {
352 return ci;
353 }
354 }
355
356 CommonsImport ci = getDefaultCommonsImport( );
357 if ( ci != null )
358 {
359 setNewCommonsKey( ci.getKey( ) );
360 return ci;
361 }
362
363 return null;
364 }
365
366
367
368
369
370
371
372 private static void setNewCommonsKey( String strNewKey )
373 {
374 DatastoreService.setDataValue( DSKEY_CURRENT_COMMONS_INCLUDE, strNewKey );
375 }
376
377 }