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.document.service.attributes;
35
36 import fr.paris.lutece.plugins.document.business.Document;
37 import fr.paris.lutece.plugins.document.business.attributes.AttributeTypeHome;
38 import fr.paris.lutece.plugins.document.business.attributes.AttributeTypeParameter;
39 import fr.paris.lutece.plugins.document.business.attributes.DocumentAttribute;
40 import fr.paris.lutece.plugins.document.business.attributes.DocumentAttributeHome;
41 import fr.paris.lutece.plugins.document.business.attributes.IMapProvider;
42 import fr.paris.lutece.plugins.document.business.attributes.MapProviderManager;
43 import fr.paris.lutece.plugins.document.service.AttributeManager;
44 import fr.paris.lutece.portal.business.regularexpression.RegularExpression;
45 import fr.paris.lutece.portal.service.regularexpression.RegularExpressionService;
46 import fr.paris.lutece.portal.service.template.AppTemplateService;
47 import fr.paris.lutece.portal.service.util.AppLogService;
48 import fr.paris.lutece.util.html.HtmlTemplate;
49
50 import org.apache.commons.lang3.StringUtils;
51
52 import org.codehaus.jackson.JsonNode;
53 import org.codehaus.jackson.map.ObjectMapper;
54
55 import java.io.IOException;
56
57 import java.util.Collection;
58 import java.util.HashMap;
59 import java.util.List;
60 import java.util.Locale;
61 import java.util.Map;
62
63 import javax.servlet.http.HttpServletRequest;
64
65
66
67
68
69 public abstract class DefaultManager implements AttributeManager
70 {
71 private static final String MARK_ATTRIBUTE = "attribute";
72 private static final String MARK_ATTRIBUTE_PARAMETERS = "parameters";
73 private static final String MARK_DOCUMENT = "document";
74 private static final String MARK_LOCALE = "locale";
75 private static final String MARK_BASE_URL = "base_url";
76 private static final String GEOLOC_JSON_PATH_FEATURESS = "features";
77 private static final String GEOLOC_JSON_PATH_PROPERTIES = "properties";
78 private static final String GEOLOC_JSON_PATH_GEOMETRY = "geometry";
79 private static final String GEOLOC_JSON_PATH_COORDINATES = "coordinates";
80 private static final String GEOLOC_JSON_PATH_ADDRESS = "address";
81 private static final String MARK_GISMAP_GEOMETRY = "gismap_geometry";
82 private static final String MARK_GISMAP_X = "gismap_x";
83 private static final String MARK_GISMAP_Y = "gismap_y";
84 private static final String MARK_GISMAP_ADDRESS = "gismap_address";
85 private String _strAttributeTypeCode;
86
87
88
89
90
91 abstract protected String getCreateTemplate( );
92
93
94
95
96
97 abstract protected String getModifyTemplate( );
98
99
100
101
102
103
104
105 abstract protected String getCreateParametersTemplate( );
106
107
108
109
110
111
112
113 abstract protected String getModifyParametersTemplate( );
114
115
116
117
118 public String getCreateFormHtml( DocumentAttribute attribute, Locale locale, String strBaseUrl )
119 {
120 if ( attribute.getCodeAttributeType( ).equals( "geoloc" ) )
121 {
122 attribute.setMapProvider( MapProviderManager.getMapProvider( "gismap" ) );
123 }
124
125 Map<String, Object> model = new HashMap<String, Object>( );
126 model.put( MARK_ATTRIBUTE, attribute );
127 model.put( MARK_ATTRIBUTE_PARAMETERS, getParameters( attribute.getId( ), locale ) );
128 model.put( MARK_LOCALE, locale );
129 model.put( MARK_BASE_URL, strBaseUrl );
130
131 HtmlTemplate template = AppTemplateService.getTemplate( getCreateTemplate( ), locale, model );
132
133 return template.getHtml( );
134 }
135
136
137
138
139 public String getModifyFormHtml( DocumentAttribute attribute, Document document, Locale locale, String strBaseUrl )
140 {
141 Map<String, Object> model = new HashMap<String, Object>( );
142 model.put( MARK_ATTRIBUTE, attribute );
143 model.put( MARK_ATTRIBUTE_PARAMETERS, getParameters( attribute.getId( ), locale ) );
144 model.put( MARK_DOCUMENT, document );
145 model.put( MARK_LOCALE, locale );
146 model.put( MARK_BASE_URL, strBaseUrl );
147
148 if ( attribute.getCodeAttributeType( ).equals( "geoloc" ) )
149 {
150 attribute.setMapProvider( MapProviderManager.getMapProvider( "gismap" ) );
151 if(attribute.getMapProvider()!=null)
152 {
153 String strValue = attribute.getTextValue( );
154
155 JsonNode object = null;
156
157 try
158 {
159 object = new ObjectMapper( ).readTree( strValue );
160 }
161 catch ( IOException e )
162 {
163 AppLogService.error( "Erreur ", e );
164 }
165
166 String strGeometry = "";
167 String strAddress = "";
168 String strX = "";
169 String strY = "";
170
171 if ( object != null )
172 {
173 strGeometry = object.path( GEOLOC_JSON_PATH_FEATURESS ).path( 1 ).toString( );
174 strAddress = object.path( GEOLOC_JSON_PATH_FEATURESS ).path( 0 ).path( GEOLOC_JSON_PATH_PROPERTIES )
175 .path( GEOLOC_JSON_PATH_ADDRESS ).toString( );
176 strX = object.path( GEOLOC_JSON_PATH_FEATURESS ).path( 0 ).path( GEOLOC_JSON_PATH_GEOMETRY )
177 .path( GEOLOC_JSON_PATH_COORDINATES ).path(0).toString( );
178 strY = object.path( GEOLOC_JSON_PATH_FEATURESS ).path( 0 ).path( GEOLOC_JSON_PATH_GEOMETRY )
179 .path( GEOLOC_JSON_PATH_COORDINATES ).path(1).toString( );
180 }
181 model.put( MARK_GISMAP_GEOMETRY, strGeometry );
182 model.put( MARK_GISMAP_ADDRESS, strAddress );
183 model.put( MARK_GISMAP_X, strX );
184 model.put( MARK_GISMAP_Y, strY );
185 }
186 }
187
188
189 HtmlTemplate template = AppTemplateService.getTemplate( getModifyTemplate( ), locale, model );
190
191 return template.getHtml( );
192 }
193
194
195
196
197 public String getCreateParametersFormHtml( Locale locale )
198 {
199 return getCreateParametersFormHtml( null, locale );
200 }
201
202
203
204
205 public String getCreateParametersFormHtml( List<AttributeTypeParameter> listParameters, Locale locale )
206 {
207 return getCreateParametersFormHtml( listParameters, locale, null );
208 }
209
210 public String getCreateParametersFormHtml( List<AttributeTypeParameter> listParameters, Locale locale,
211 Map<String, Object> model )
212 {
213 if ( model == null )
214 {
215 model = new HashMap<String, Object>( );
216 }
217
218 String strUrlTemplate = getCreateParametersTemplate( );
219
220 if ( strUrlTemplate == null )
221 {
222 return StringUtils.EMPTY;
223 }
224
225 if ( ( listParameters != null ) && !listParameters.isEmpty( ) )
226 {
227 model.put( MARK_ATTRIBUTE_PARAMETERS, listParameters );
228 }
229 else
230 {
231 model.put( MARK_ATTRIBUTE_PARAMETERS, getExtraParameters( locale ) );
232 }
233
234 model.put( MARK_LOCALE, locale );
235
236 HtmlTemplate template = AppTemplateService.getTemplate( getCreateParametersTemplate( ), locale, model );
237
238 return template.getHtml( );
239 }
240
241
242
243
244 public String getModifyParametersFormHtml( Locale locale, int nAttributeId )
245 {
246 return getModifyParametersFormHtml( locale, nAttributeId, null );
247 }
248
249 public String getModifyParametersFormHtml( Locale locale, int nAttributeId, Map<String, Object> model )
250 {
251 if ( model == null )
252 {
253 model = new HashMap<String, Object>( );
254 }
255
256 String strUrlTemplate = getModifyParametersTemplate( );
257
258 if ( strUrlTemplate == null )
259 {
260 return StringUtils.EMPTY;
261 }
262
263 model.put( MARK_ATTRIBUTE_PARAMETERS, getExtraParametersValues( locale, nAttributeId ) );
264 model.put( MARK_LOCALE, locale );
265
266 HtmlTemplate template = AppTemplateService.getTemplate( strUrlTemplate, locale, model );
267
268 return template.getHtml( );
269 }
270
271
272
273
274 public List<AttributeTypeParameter> getExtraParameters( Locale locale )
275 {
276 return AttributeTypeHome.getAttributeTypeParameterList( getAttributeTypeCode( ), locale );
277 }
278
279
280
281
282 public List<AttributeTypeParameter> getExtraParametersValues( Locale locale, int nAttributeId )
283 {
284 List<AttributeTypeParameter> listParameters = getExtraParameters( locale );
285
286 for ( AttributeTypeParameter parameter : listParameters )
287 {
288 parameter.setValueList( DocumentAttributeHome.getAttributeParameterValues( nAttributeId,
289 parameter.getName( ) ) );
290 }
291
292 return listParameters;
293 }
294
295
296
297
298 public void setAttributeTypeCode( String strCode )
299 {
300 _strAttributeTypeCode = strCode;
301 }
302
303
304
305
306 public String getAttributeTypeCode( )
307 {
308 return _strAttributeTypeCode;
309 }
310
311
312
313
314 public String validateValue( int nAttributeId, String strValue, Locale locale )
315 {
316
317 if ( RegularExpressionService.getInstance( ).isAvailable( ) && ( strValue != null ) &&
318 !strValue.equals( "" ) )
319 {
320 Collection<Integer> colRegularExpression = DocumentAttributeHome.getListRegularExpressionKeyByIdAttribute( nAttributeId );
321
322 if ( ( colRegularExpression != null ) && ( colRegularExpression.size( ) != 0 ) )
323 {
324 for ( Integer nExpressionId : colRegularExpression )
325 {
326 RegularExpression regularExpression = RegularExpressionService.getInstance( )
327 .getRegularExpressionByKey( nExpressionId );
328
329 if ( !RegularExpressionService.getInstance( ).isMatches( strValue, regularExpression ) )
330 {
331 return regularExpression.getErrorMessage( );
332 }
333 }
334 }
335 }
336
337 return null;
338 }
339
340
341
342
343 public String validateValueParameters( List<AttributeTypeParameter> listParameters, Locale locale )
344 {
345 return null;
346 }
347
348
349
350
351 public String getAttributeXmlValue( Document document, DocumentAttribute attribute )
352 {
353 return attribute.getTextValue( );
354 }
355
356
357
358
359
360
361
362 public Collection<AttributeTypeParameter> getAttributeParametersValues( int nAttributeId, Locale locale )
363 {
364 return DocumentAttributeHome.getAttributeParametersValues( nAttributeId, locale );
365 }
366
367
368
369
370 public boolean canBeUsedAsThumbnail( )
371 {
372 return false;
373 }
374
375
376
377
378 public List<AttributeTypeParameter> getValueParameters( HttpServletRequest request, Locale locale )
379 {
380 return null;
381 }
382
383
384
385
386
387
388
389 protected Map<String, List<String>> getParameters( int nAttributeId, Locale locale )
390 {
391 HashMap<String, List<String>> mapParameters = new HashMap<String, List<String>>( );
392 Collection<AttributeTypeParameter> listParameters = getAttributeParametersValues( nAttributeId, locale );
393
394 for ( AttributeTypeParameter parameter : listParameters )
395 {
396 mapParameters.put( parameter.getName( ), parameter.getValueList( ) );
397 }
398
399
400 for ( AttributeTypeParameter parameter : getExtraParameters( locale ) )
401 {
402 if ( !mapParameters.containsKey( parameter.getName( ) ) )
403 {
404 mapParameters.put( parameter.getName( ), parameter.getDefaultValue( ) );
405 }
406 }
407
408 return mapParameters;
409 }
410 }