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.CommonsInclude;
37 import fr.paris.lutece.portal.service.datastore.DatastoreService;
38 import fr.paris.lutece.portal.service.spring.SpringContextService;
39 import fr.paris.lutece.portal.service.util.AppLogService;
40 import fr.paris.lutece.util.ReferenceList;
41 import java.util.List;
42
43
44
45
46 public class CommonsService
47 {
48 private static final String DSKEY_CURRENT_COMMONS_INCLUDE = "core.templates.currentCommonsInclude";
49
50 private CommonsService( )
51 {
52
53 }
54
55
56
57
58
59
60 public static List<CommonsInclude> getCommonsIncludes( )
61 {
62 return SpringContextService.getBeansOfType( CommonsInclude.class );
63 }
64
65
66
67
68
69
70
71 public static void activateCommons( String strKey )
72 {
73 IFreeMarkerTemplateService serviceFMT = FreeMarkerTemplateService.getInstance( );
74
75 CommonsInclude ciNew = getCommonsInclude( strKey );
76
77 if ( ciNew == null )
78 {
79 return;
80 }
81
82 CommonsInclude ciCurrent = getCurrentCommonsInclude( );
83
84
85 List<String> listAutoIncludes = serviceFMT.getAutoIncludes( );
86 if ( ciCurrent != null )
87 {
88 for ( String strExclude : ciCurrent.getFiles( ) )
89 {
90 if ( ( listAutoIncludes != null ) && listAutoIncludes.contains( strExclude ) )
91 {
92 serviceFMT.removeAutoInclude( strExclude );
93 AppLogService.info( "Existing Freemarker AutoInclude removed : {}", strExclude );
94 }
95 }
96 }
97
98 for ( String strInclude : ciNew.getFiles( ) )
99 {
100 if ( ( listAutoIncludes != null ) && !listAutoIncludes.contains( strInclude ) )
101 {
102 serviceFMT.addAutoInclude( strInclude );
103 AppLogService.info( "New Freemarker AutoInclude added : {}", strInclude );
104 }
105 }
106
107 setNewCommonsInclude( ciNew );
108 }
109
110
111
112
113
114
115 public static ReferenceList getCommonsIncludeList( )
116 {
117 ReferenceListnceList.html#ReferenceList">ReferenceList list = new ReferenceList( );
118 for ( CommonsInclude ci : getCommonsIncludes( ) )
119 {
120 list.addItem( ci.getKey( ), ci.getName( ) );
121 }
122 return list;
123 }
124
125
126
127
128
129
130 public static String getCurrentCommonsKey( )
131 {
132 CommonsInclude ciCurrent = getCurrentCommonsInclude( );
133
134 if ( ciCurrent != null )
135 {
136 return ciCurrent.getKey( );
137 }
138 else
139 {
140 return null;
141 }
142 }
143
144
145
146
147
148
149
150
151 public static CommonsInclude getCommonsInclude( String strKey )
152 {
153 for ( CommonsInclude ci : getCommonsIncludes( ) )
154 {
155 if ( ci.getKey( ).equals( strKey ) )
156 {
157 return ci;
158 }
159 }
160 return null;
161 }
162
163
164
165
166
167
168 public static CommonsInclude getDefaultCommonsInclude( )
169 {
170
171 for ( CommonsInclude ci : getCommonsIncludes( ) )
172 {
173 if ( ci.isDefault( ) )
174 {
175 return ci;
176 }
177 }
178
179
180 if ( getCommonsIncludes( ).size( ) > 0 )
181 {
182 return getCommonsIncludes( ).get( 0 );
183 }
184
185 return null;
186 }
187
188
189
190
191
192
193 public static CommonsInclude getCurrentCommonsInclude( )
194 {
195 String strCurrentCommonsIncludeKey = DatastoreService.getInstanceDataValue( DSKEY_CURRENT_COMMONS_INCLUDE, null );
196
197 if ( strCurrentCommonsIncludeKey != null )
198 {
199 CommonsInclude ci = getCommonsInclude( strCurrentCommonsIncludeKey );
200 if ( ci != null )
201 {
202 return ci;
203 }
204 }
205
206 CommonsInclude ci = getDefaultCommonsInclude( );
207 if ( ci != null )
208 {
209 setNewCommonsInclude( ci );
210 return ci;
211 }
212 else
213 {
214 return null;
215 }
216
217 }
218
219
220
221
222
223
224
225 private static void setNewCommonsInclude( CommonsInclude ciNew )
226 {
227 DatastoreService.setDataValue( DSKEY_CURRENT_COMMONS_INCLUDE, ciNew.getKey( ) );
228 }
229
230 }