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.HashMap;
55 import java.util.Iterator;
56 import java.util.List;
57 import java.util.Locale;
58 import java.util.Map;
59
60
61
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
82
83
84
85 protected String getCreateTemplate( )
86 {
87 return TEMPLATE_CREATE_ATTRIBUTE;
88 }
89
90
91
92
93
94
95 protected String getModifyTemplate( )
96 {
97 return TEMPLATE_MODIFY_ATTRIBUTE;
98 }
99
100
101
102
103
104
105 protected String getCreateParametersTemplate( )
106 {
107 return TEMPLATE_CREATE_PARAMETERS_ATTRIBUTE;
108 }
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 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
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
146
147 public String validateValue( int nAttributeId, String strValue, Locale locale )
148 {
149
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
202
203
204
205
206
207
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 }