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.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
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
86
87
88 protected String getCreateTemplate( )
89 {
90 return TEMPLATE_CREATE_ATTRIBUTE;
91 }
92
93
94
95
96
97 protected String getModifyTemplate( )
98 {
99 return TEMPLATE_MODIFY_ATTRIBUTE;
100 }
101
102
103
104
105
106 protected String getCreateParametersTemplate( )
107 {
108 return TEMPLATE_CREATE_PARAMETERS_ATTRIBUTE;
109 }
110
111
112
113
114
115 protected String getModifyParametersTemplate( )
116 {
117 return TEMPLATE_MODIFY_PARAMETERS_ATTRIBUTE;
118 }
119
120
121
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
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
160
161 public String validateValue( int nAttributeId, String strValue, Locale locale )
162 {
163
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
217
218
219
220
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 }