View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.modules.geoloc.service.attributes;
35  
36  import fr.paris.lutece.plugins.document.business.Document;
37  import fr.paris.lutece.plugins.document.business.attributes.AttributeTypeParameter;
38  import fr.paris.lutece.plugins.document.business.attributes.DocumentAttribute;
39  import fr.paris.lutece.plugins.document.business.portlet.DocumentPortlet;
40  import fr.paris.lutece.plugins.document.service.attributes.DefaultManager;
41  import fr.paris.lutece.plugins.leaflet.business.GeolocItem;
42  import fr.paris.lutece.plugins.leaflet.service.IconService;
43  import fr.paris.lutece.portal.business.portlet.PortletHome;
44  import fr.paris.lutece.portal.service.i18n.I18nService;
45  import fr.paris.lutece.portal.service.util.AppLogService;
46  
47  import org.apache.commons.lang.StringUtils;
48  
49  import org.codehaus.jackson.JsonNode;
50  import org.codehaus.jackson.map.ObjectMapper;
51  
52  import java.io.IOException;
53  
54  import java.util.HashMap;
55  import java.util.Iterator;
56  import java.util.List;
57  import java.util.Locale;
58  import java.util.Map;
59  
60  /**
61   * Manager for Text Attribute
62   */
63  public class GeolocManager extends DefaultManager
64  {
65      private static final String MARK_STYLES = "styles";
66      private static final String MARK_ICONS = "icons";
67      private static final String TEMPLATE_CREATE_ATTRIBUTE = "admin/plugins/document/modules/geoloc/attributes/create_geoloc.html";
68      private static final String TEMPLATE_MODIFY_ATTRIBUTE = "admin/plugins/document/modules/geoloc/attributes/modify_geoloc.html";
69      private static final String TEMPLATE_CREATE_PARAMETERS_ATTRIBUTE = "admin/plugins/document/modules/geoloc/attributes/create_parameters_geoloc.html";
70      private static final String TEMPLATE_MODIFY_PARAMETERS_ATTRIBUTE = "admin/plugins/document/modules/geoloc/attributes/modify_parameters_geoloc.html";
71      private static final String KEY_ERROR_INVALID_GEOLOC_VALUE = "module.document.geoloc.attributeType.geoloc.error.invalidvalue";
72      private static final String KEY_ERROR_MISSING_GEOLOC_VALUE = "module.document.geoloc.attributeType.geoloc.error.missingvalue";
73      private static final String KEY_ERROR_NONARRAY_GEOLOC_VALUE = "module.document.geoloc.attributeType.geoloc.error.nonarrayvalue";
74      private static final String KEY_ERROR_SHORTARRAY_GEOLOC_VALUE = "module.document.geoloc.attributeType.geoloc.error.shortarrayvalue";
75      private static final String KEY_ERROR_NONNUMBER_GEOLOC_VALUE = "module.document.geoloc.attributeType.geoloc.error.nonnumbervalue";
76      private static final String GEOLOC_JSON_PATH_GEOMETRY = "geometry";
77      private static final String GEOLOC_JSON_PATH_GEOMETRY_COORDINATES = "coordinates";
78      private static final String TAG_GEOLOC_RESOURCE = "geoloc-resource";
79  
80      /**
81       * Gets the template to enter the attribute value
82       * 
83       * @return The template to enter the attribute value
84       */
85      protected String getCreateTemplate( )
86      {
87          return TEMPLATE_CREATE_ATTRIBUTE;
88      }
89  
90      /**
91       * Gets the template to modify the attribute value
92       * 
93       * @return The template to modify the attribute value
94       */
95      protected String getModifyTemplate( )
96      {
97          return TEMPLATE_MODIFY_ATTRIBUTE;
98      }
99  
100     /**
101      * Gets the template to enter the parameters of the attribute value
102      * 
103      * @return The template to enter the parameters of the attribute value
104      */
105     protected String getCreateParametersTemplate( )
106     {
107         return TEMPLATE_CREATE_PARAMETERS_ATTRIBUTE;
108     }
109 
110     /**
111      * Gets the template to modify the parameters of the attribute value
112      * 
113      * @return The template to modify the parameters of the attribute value
114      */
115     protected String getModifyParametersTemplate( )
116     {
117         return TEMPLATE_MODIFY_PARAMETERS_ATTRIBUTE;
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     public String getCreateParametersFormHtml( List<AttributeTypeParameter> listParameters, Locale locale )
124     {
125         Map<String, Object> model = new HashMap<String, Object>( );
126         model.put( MARK_STYLES, PortletHome.getStylesList( DocumentPortlet.RESOURCE_ID ) );
127         model.put( MARK_ICONS, IconService.getList( ) );
128 
129         return super.getCreateParametersFormHtml( listParameters, locale, model );
130     }
131 
132     /**
133      * {@inheritDoc}
134      */
135     public String getModifyParametersFormHtml( Locale locale, int nAttributeId )
136     {
137         Map<String, Object> model = new HashMap<String, Object>( );
138         model.put( MARK_STYLES, PortletHome.getStylesList( DocumentPortlet.RESOURCE_ID ) );
139         model.put( MARK_ICONS, IconService.getList( ) );
140 
141         return super.getModifyParametersFormHtml( locale, nAttributeId, model );
142     }
143 
144     /**
145      * {@inheritDoc}
146      */
147     public String validateValue( int nAttributeId, String strValue, Locale locale )
148     {
149         // The "required" check is done before, here we must not error if the string is empty or missing
150         if ( ( strValue != null ) && !strValue.equals( "" ) )
151         {
152             JsonNode object;
153 
154             try
155             {
156                 object = new ObjectMapper( ).readTree( strValue );
157             }
158             catch( IOException e )
159             {
160                 return I18nService.getLocalizedString( KEY_ERROR_INVALID_GEOLOC_VALUE, locale );
161             }
162 
163             JsonNode objCoordinates = object.path( GEOLOC_JSON_PATH_GEOMETRY ).path( GEOLOC_JSON_PATH_GEOMETRY_COORDINATES );
164 
165             if ( objCoordinates.isMissingNode( ) )
166             {
167                 return I18nService.getLocalizedString( KEY_ERROR_MISSING_GEOLOC_VALUE, locale );
168             }
169             else
170             {
171                 if ( !objCoordinates.isArray( ) )
172                 {
173                     return I18nService.getLocalizedString( KEY_ERROR_NONARRAY_GEOLOC_VALUE, locale );
174                 }
175                 else
176                 {
177                     Iterator<JsonNode> it = objCoordinates.getElements( );
178 
179                     for ( int i = 0; i < 2; i++ )
180                     {
181                         if ( !it.hasNext( ) )
182                         {
183                             return I18nService.getLocalizedString( KEY_ERROR_SHORTARRAY_GEOLOC_VALUE, locale );
184                         }
185 
186                         JsonNode node = it.next( );
187 
188                         if ( !node.isNumber( ) )
189                         {
190                             return I18nService.getLocalizedString( KEY_ERROR_NONNUMBER_GEOLOC_VALUE, locale );
191                         }
192                     }
193                 }
194             }
195         }
196 
197         return null;
198     }
199 
200     /**
201      * Get the XML data corresponding to the attribute to build the document XML content
202      * 
203      * @param document
204      *            The document
205      * @param attribute
206      *            The attribute
207      * @return The XML value of the attribute
208      */
209     public String getAttributeXmlValue( Document document, DocumentAttribute attribute )
210     {
211         if ( ( attribute.getTextValue( ) != null ) && ( attribute.getTextValue( ).length( ) != 0 ) )
212         {
213             String strValue = attribute.getTextValue( );
214             GeolocItem geolocItem;
215 
216             try
217             {
218                 geolocItem = GeolocItem.fromJSON( strValue );
219             }
220             catch( IOException e )
221             {
222                 AppLogService.error( "Document Geoloc, error generating xml from JSON: " + strValue + ", exception" + e );
223 
224                 return StringUtils.EMPTY;
225             }
226 
227             return geolocItem.toXML( );
228         }
229 
230         return StringUtils.EMPTY;
231     }
232 }