|
1 | 1 | package org.oskari.control.myfeatures; |
2 | 2 |
|
3 | 3 | import fi.nls.oskari.annotation.Oskari; |
| 4 | +import org.json.JSONArray; |
4 | 5 | import org.oskari.user.User; |
5 | 6 |
|
6 | 7 | import fi.nls.oskari.domain.map.OskariLayer; |
|
38 | 39 | import java.util.Collections; |
39 | 40 | import java.util.Date; |
40 | 41 | import java.util.List; |
| 42 | +import java.util.Map; |
| 43 | +import java.util.Optional; |
41 | 44 | import java.util.UUID; |
42 | 45 | import java.util.stream.Collectors; |
43 | 46 |
|
@@ -218,19 +221,60 @@ private List<FeatureProperties> getProperties(MyFeaturesLayer layer, String lang |
218 | 221 | List<FeatureProperties> props = new ArrayList<>(); |
219 | 222 |
|
220 | 223 | int i = 0; |
| 224 | + JSONObject attributes = layer.getAttributes(); |
| 225 | + |
221 | 226 | for (MyFeaturesFieldInfo field : layer.getLayerFields()) { |
222 | 227 | FeatureProperties p = new FeatureProperties(); |
223 | 228 | p.name = field.getName(); |
224 | 229 | p.type = field.getType().getSimpleType(); |
225 | 230 | p.rawType = field.getType().getOutputBinding().getName(); |
226 | | - p.label = field.getName(); |
227 | | - p.hidden = false; |
228 | | - p.format = null; |
229 | | - p.order = i++; |
| 231 | + p.label = getLocalizedName(field, attributes, lang); |
| 232 | + p.hidden = getHidden(field, attributes); |
| 233 | + p.format = getFieldFormat(field, attributes); |
| 234 | + p.order = getFieldOrder(field, attributes, i); |
230 | 235 | props.add(p); |
231 | 236 | } |
232 | 237 |
|
233 | 238 | return props; |
234 | 239 | } |
235 | 240 |
|
| 241 | + private String getLocalizedName(MyFeaturesFieldInfo field, JSONObject attributes, String lang) { |
| 242 | + JSONObject locale = Optional.ofNullable(attributes.optJSONObject("data")) |
| 243 | + .map(o -> o.optJSONObject("locale")) |
| 244 | + .map(o -> o.optJSONObject(lang, null)).orElse(null); |
| 245 | + return locale == null ? field.getName() : locale.optString(field.getName(), field.getName()); |
| 246 | + } |
| 247 | + |
| 248 | + private Boolean getHidden(MyFeaturesFieldInfo field, JSONObject attributes) { |
| 249 | + List<String> list = getFilter(attributes); |
| 250 | + return !list.contains(field.getName()); |
| 251 | + } |
| 252 | + |
| 253 | + private Map<String, Object> getFieldFormat(MyFeaturesFieldInfo field, JSONObject attributes) { |
| 254 | + JSONObject formatJSON = Optional.ofNullable(attributes.optJSONObject("data")) |
| 255 | + .map(o -> o.optJSONObject("format")) |
| 256 | + .map(o -> o.optJSONObject(field.getName())).orElse(null); |
| 257 | + return formatJSON != null ? formatJSON.toMap() : null; |
| 258 | + } |
| 259 | + |
| 260 | + private List<String> getFilter(JSONObject attributes) { |
| 261 | + JSONArray filter = Optional.ofNullable(attributes.optJSONObject("data")) |
| 262 | + .map(o -> o.optJSONObject("filter")) |
| 263 | + .map(o -> o.optJSONArray("default")).orElse(null); |
| 264 | + List<String> list = filter == null |
| 265 | + ? Collections.emptyList() |
| 266 | + : filter.toList().stream() |
| 267 | + .map(Object::toString) |
| 268 | + .toList(); |
| 269 | + return list; |
| 270 | + } |
| 271 | + |
| 272 | + private int getFieldOrder(MyFeaturesFieldInfo field, JSONObject attributes, int index) { |
| 273 | + List<String> list = getFilter(attributes); |
| 274 | + if (!list.contains(field.getName())) { |
| 275 | + return index; |
| 276 | + } |
| 277 | + |
| 278 | + return list.indexOf(field.getName()); |
| 279 | + } |
236 | 280 | } |
0 commit comments