View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.ArrayList;
55  import java.util.HashMap;
56  import java.util.Iterator;
57  import java.util.List;
58  import java.util.Locale;
59  import java.util.Map;
60  
61  
62  /**
63   * Manager for Text Attribute
64   */
65  public class GeolocManager extends DefaultManager
66  {
67      private static final String MARK_STYLES = "styles";
68      private static final String MARK_ICONS = "icons";
69      private static final String MARK_EDIT_TYPES = "edit_types";
70      private static final String TEMPLATE_CREATE_ATTRIBUTE = "admin/plugins/document/modules/geoloc/attributes/create_geoloc_gismap.html";
71      private static final String TEMPLATE_MODIFY_ATTRIBUTE = "admin/plugins/document/modules/geoloc/attributes/modify_geoloc_gismap.html";
72      private static final String TEMPLATE_CREATE_PARAMETERS_ATTRIBUTE = "admin/plugins/document/modules/geoloc/attributes/create_parameters_geoloc.html";
73      private static final String TEMPLATE_MODIFY_PARAMETERS_ATTRIBUTE = "admin/plugins/document/modules/geoloc/attributes/modify_parameters_geoloc.html";
74      private static final String KEY_ERROR_INVALID_GEOLOC_VALUE = "module.document.geoloc.attributeType.geoloc.error.invalidvalue";
75      private static final String KEY_ERROR_MISSING_GEOLOC_VALUE = "module.document.geoloc.attributeType.geoloc.error.missingvalue";
76      private static final String KEY_ERROR_NONARRAY_GEOLOC_VALUE = "module.document.geoloc.attributeType.geoloc.error.nonarrayvalue";
77      private static final String KEY_ERROR_SHORTARRAY_GEOLOC_VALUE = "module.document.geoloc.attributeType.geoloc.error.shortarrayvalue";
78      private static final String KEY_ERROR_NONNUMBER_GEOLOC_VALUE = "module.document.geoloc.attributeType.geoloc.error.nonnumbervalue";
79      private static final String GEOLOC_JSON_PATH_GEOMETRY = "geometry";
80      private static final String GEOLOC_JSON_PATH_GEOMETRY_COORDINATES = "coordinates";
81      private static final String GEOLOC_JSON_PATH_FEATURESS = "features";
82      private static final String TAG_GEOLOC_RESOURCE = "geoloc-resource";
83  
84      /**
85       * Gets the template to enter the attribute value
86       * @return The template to enter the attribute value
87       */
88      protected String getCreateTemplate(  )
89      {
90          return TEMPLATE_CREATE_ATTRIBUTE;
91      }
92  
93      /**
94       * Gets the template to modify the attribute value
95       * @return The template to modify the attribute value
96       */
97      protected String getModifyTemplate(  )
98      {
99          return TEMPLATE_MODIFY_ATTRIBUTE;
100     }
101 
102     /**
103      * Gets the template to enter the parameters of the attribute value
104      * @return The template to enter the parameters of the attribute value
105      */
106     protected String getCreateParametersTemplate(  )
107     {
108         return TEMPLATE_CREATE_PARAMETERS_ATTRIBUTE;
109     }
110 
111     /**
112      * Gets the template to modify the parameters of the attribute value
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         List<String> editTypes = new ArrayList<String>(  );
126         editTypes.add( "Address" );
127         editTypes.add( "Point" );
128         editTypes.add( "Line" );
129         editTypes.add( "Polygon" );
130 
131         Map<String, Object> model = new HashMap<String, Object>(  );
132         model.put( MARK_STYLES, PortletHome.getStylesList( DocumentPortlet.RESOURCE_ID ) );
133         model.put( MARK_ICONS, IconService.getList(  ) );
134         model.put( MARK_EDIT_TYPES, editTypes );
135 
136         return super.getCreateParametersFormHtml( listParameters, locale, model );
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     public String getModifyParametersFormHtml( Locale locale, int nAttributeId )
143     {
144         List<String> editTypes = new ArrayList<String>(  );
145         editTypes.add( "Address" );
146         editTypes.add( "Point" );
147         editTypes.add( "Line" );
148         editTypes.add( "Polygon" );
149 
150         Map<String, Object> model = new HashMap<String, Object>(  );
151         model.put( MARK_STYLES, PortletHome.getStylesList( DocumentPortlet.RESOURCE_ID ) );
152         model.put( MARK_ICONS, IconService.getList(  ) );
153         model.put( MARK_EDIT_TYPES, editTypes );
154 
155         return super.getModifyParametersFormHtml( locale, nAttributeId, model );
156     }
157 
158     /**
159      * {@inheritDoc}
160      */
161     public String validateValue( int nAttributeId, String strValue, Locale locale )
162     {
163         //The "required" check is done before, here we must not error if the string is empty or missing
164         if ( ( strValue != null ) && !strValue.equals( "" ) )
165         {
166             JsonNode object;
167 
168             try
169             {
170                 object = new ObjectMapper(  ).readTree( strValue );
171             }
172             catch ( IOException e )
173             {
174                 return I18nService.getLocalizedString( KEY_ERROR_INVALID_GEOLOC_VALUE, locale );
175             }
176 
177             JsonNode objCoordinates = object.path( GEOLOC_JSON_PATH_FEATURESS ).path( 0 ).path( GEOLOC_JSON_PATH_GEOMETRY )
178                                             .path( GEOLOC_JSON_PATH_GEOMETRY_COORDINATES );
179 
180             if ( objCoordinates.isMissingNode(  ) )
181             {
182                 return I18nService.getLocalizedString( KEY_ERROR_MISSING_GEOLOC_VALUE, locale );
183             }
184             else
185             {
186                 if ( !objCoordinates.isArray(  ) )
187                 {
188                     return I18nService.getLocalizedString( KEY_ERROR_NONARRAY_GEOLOC_VALUE, locale );
189                 }
190                 else
191                 {
192                     Iterator<JsonNode> it = objCoordinates.getElements(  );
193 
194                     for ( int i = 0; i < 2; i++ )
195                     {
196                         if ( !it.hasNext(  ) )
197                         {
198                             return I18nService.getLocalizedString( KEY_ERROR_SHORTARRAY_GEOLOC_VALUE, locale );
199                         }
200 
201                         JsonNode node = it.next(  );
202 
203                         if ( !node.isNumber(  ) )
204                         {
205                             return I18nService.getLocalizedString( KEY_ERROR_NONNUMBER_GEOLOC_VALUE, locale );
206                         }
207                     }
208                 }
209             }
210         }
211 
212         return null;
213     }
214 
215     /**
216      * Get the XML data corresponding to the attribute to build the document XML
217      * content
218      * @param document The document
219      * @param attribute The attribute
220      * @return The XML value of the attribute
221      */
222     public String getAttributeXmlValue( Document document, DocumentAttribute attribute )
223     {
224         if ( ( attribute.getTextValue(  ) != null ) && ( attribute.getTextValue(  ).length(  ) != 0 ) )
225         {
226             String strValue = attribute.getTextValue(  );
227 
228             JsonNode object = null;
229 
230             try
231             {
232                 object = new ObjectMapper(  ).readTree( strValue );
233             }
234             catch ( IOException e )
235             {
236                 AppLogService.error( "Erreur ", e );
237             }
238 
239             JsonNode objCoordinates = object.path( GEOLOC_JSON_PATH_FEATURESS ).path( 0 );
240 
241             strValue = objCoordinates.toString(  );
242 
243             GeolocItem geolocItem;
244 
245             try
246             {
247                 geolocItem = GeolocItem.fromJSON( strValue );
248             }
249             catch ( IOException e )
250             {
251                 AppLogService.error( "Document Geoloc, error generating xml from JSON: " + strValue + ", exception" +
252                     e );
253 
254                 return StringUtils.EMPTY;
255             }
256 
257             return geolocItem.toXML(  );
258         }
259 
260         return StringUtils.EMPTY;
261     }
262 }