View Javadoc
1   /*
2    * Copyright (c) 2002-2023, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * Default implementation of an attribute manager
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       * Returns the Create HTML template corresponding to this attribute
89       * @return The Create HTML template corresponding to this attribute
90       */
91      abstract protected String getCreateTemplate(  );
92  
93      /**
94       * Returns the Modify HTML template corresponding to this attribute
95       * @return the Modify HTML template corresponding to this attribute
96       */
97      abstract protected String getModifyTemplate(  );
98  
99      /**
100      * Returns the Create HTML template for parameters corresponding to this
101      * attribute
102      * @return the Create HTML template for parameters corresponding to this
103      *         attribute
104      */
105     abstract protected String getCreateParametersTemplate(  );
106 
107     /**
108      * Returns the Modify HTML template for parameters corresponding to this
109      * attribute
110      * @return the Modify HTML template for parameters corresponding to this
111      *         attribute
112      */
113     abstract protected String getModifyParametersTemplate(  );
114 
115     /**
116      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
196      */
197     public String getCreateParametersFormHtml( Locale locale )
198     {
199         return getCreateParametersFormHtml( null, locale );
200     }
201 
202     /**
203      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
273      */
274     public List<AttributeTypeParameter> getExtraParameters( Locale locale )
275     {
276         return AttributeTypeHome.getAttributeTypeParameterList( getAttributeTypeCode(  ), locale );
277     }
278 
279     /**
280      * {@inheritDoc}
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      * {@inheritDoc}
297      */
298     public void setAttributeTypeCode( String strCode )
299     {
300         _strAttributeTypeCode = strCode;
301     }
302 
303     /**
304      * {@inheritDoc}
305      */
306     public String getAttributeTypeCode(  )
307     {
308         return _strAttributeTypeCode;
309     }
310 
311     /**
312      * {@inheritDoc}
313      */
314     public String validateValue( int nAttributeId, String strValue, Locale locale )
315     {
316         // Regular expression validation
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      * {@inheritDoc}
342      */
343     public String validateValueParameters( List<AttributeTypeParameter> listParameters, Locale locale )
344     {
345         return null;
346     }
347 
348     /**
349      * {@inheritDoc}
350      */
351     public String getAttributeXmlValue( Document document, DocumentAttribute attribute )
352     {
353         return attribute.getTextValue(  );
354     }
355 
356     /**
357      * Get the attribute type parameters of an attribute
358      * @param nAttributeId The id of the attribute to get the parameter of
359      * @param locale The locale
360      * @return The list of parameters
361      */
362     public Collection<AttributeTypeParameter> getAttributeParametersValues( int nAttributeId, Locale locale )
363     {
364         return DocumentAttributeHome.getAttributeParametersValues( nAttributeId, locale );
365     }
366 
367     /**
368      * {@inheritDoc}
369      */
370     public boolean canBeUsedAsThumbnail(  )
371     {
372         return false;
373     }
374 
375     /**
376      * {@inheritDoc}
377      */
378     public List<AttributeTypeParameter> getValueParameters( HttpServletRequest request, Locale locale )
379     {
380         return null;
381     }
382 
383     /**
384      * Get parameters map
385      * @param nAttributeId the attribute identifier
386      * @param locale The current locale
387      * @return a map of parameters
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         // Set all missing parameters with their default values
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 }