1
2
3
4
5
6 package org.tailormap.api.controller;
7
8 import static org.tailormap.api.persistence.helper.TMAttributeTypeHelper.isGeometry;
9 import static org.tailormap.api.persistence.helper.TMFeatureTypeHelper.getConfiguredAttributes;
10
11 import io.micrometer.core.annotation.Timed;
12 import java.io.Serializable;
13 import java.util.Set;
14 import org.springframework.http.HttpStatus;
15 import org.springframework.http.MediaType;
16 import org.springframework.http.ResponseEntity;
17 import org.springframework.transaction.annotation.Transactional;
18 import org.springframework.validation.annotation.Validated;
19 import org.springframework.web.bind.annotation.GetMapping;
20 import org.springframework.web.bind.annotation.ModelAttribute;
21 import org.springframework.web.bind.annotation.RequestMapping;
22 import org.springframework.web.server.ResponseStatusException;
23 import org.tailormap.api.annotation.AppRestController;
24 import org.tailormap.api.persistence.Application;
25 import org.tailormap.api.persistence.Form;
26 import org.tailormap.api.persistence.GeoService;
27 import org.tailormap.api.persistence.TMFeatureType;
28 import org.tailormap.api.persistence.helper.TMFeatureTypeHelper;
29 import org.tailormap.api.persistence.json.AppLayerSettings;
30 import org.tailormap.api.persistence.json.AppTreeLayerNode;
31 import org.tailormap.api.persistence.json.GeoServiceLayer;
32 import org.tailormap.api.persistence.json.TMAttributeDescriptor;
33 import org.tailormap.api.persistence.json.TMAttributeType;
34 import org.tailormap.api.persistence.json.TMGeometryType;
35 import org.tailormap.api.repository.FeatureSourceRepository;
36 import org.tailormap.api.repository.FormRepository;
37 import org.tailormap.api.viewer.model.Attribute;
38 import org.tailormap.api.viewer.model.LayerDetails;
39 import org.tailormap.api.viewer.model.LayerDetailsForm;
40
41 @AppRestController
42 @Validated
43 @RequestMapping(
44 path = "${tailormap-api.base-path}/{viewerKind}/{viewerName}/layer/{appLayerId}/describe",
45 produces = MediaType.APPLICATION_JSON_VALUE)
46 public class LayerDescriptionController {
47
48 private final FeatureSourceRepository featureSourceRepository;
49
50 private final FormRepository formRepository;
51
52 public LayerDescriptionController(FeatureSourceRepository featureSourceRepository, FormRepository formRepository) {
53 this.featureSourceRepository = featureSourceRepository;
54 this.formRepository = formRepository;
55 }
56
57 @Transactional
58 @GetMapping
59 @Timed(value = "get_layer_description", description = "Get layer description")
60 public ResponseEntity<Serializable> getAppLayerDescription(
61 @ModelAttribute Application application,
62 @ModelAttribute AppTreeLayerNode appTreeLayerNode,
63 @ModelAttribute GeoService service,
64 @ModelAttribute GeoServiceLayer layer) {
65
66 if (layer == null) {
67 throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Can't find layer " + appTreeLayerNode);
68 }
69
70 TMFeatureType tmft = service.findFeatureTypeForLayer(layer, featureSourceRepository);
71 if (tmft == null) {
72 throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Layer does not have feature type");
73 }
74
75 LayerDetails r = new LayerDetails()
76 .id(appTreeLayerNode.getId())
77 .serviceId(appTreeLayerNode.getServiceId())
78 .featureTypeName(tmft.getName())
79 .geometryAttribute(tmft.getDefaultGeometryAttribute())
80
81 .geometryAttributeIndex(null )
82 .geometryType(tmft.getDefaultGeometryDescriptor()
83 .map(TMAttributeDescriptor::getType)
84 .map(TMAttributeType::getValue)
85 .map(TMGeometryType::fromValue)
86 .orElse(null))
87 .editable(TMFeatureTypeHelper.isEditable(application, appTreeLayerNode, tmft));
88
89 AppLayerSettings appLayerSettings = application.getAppLayerSettings(appTreeLayerNode);
90
91 Form form;
92 if (appLayerSettings.getFormId() != null) {
93 form = formRepository.findById(appLayerSettings.getFormId()).orElse(null);
94 if (form != null) {
95 r.setForm(new LayerDetailsForm().options(form.getOptions()).fields(form.getFields()));
96 }
97 }
98
99 Set<String> readOnlyAttributes = TMFeatureTypeHelper.getReadOnlyAttributes(tmft, appLayerSettings);
100
101 getConfiguredAttributes(tmft, appLayerSettings).values().stream()
102 .map(pair -> {
103 TMAttributeDescriptor a = pair.getLeft();
104 return new Attribute()
105 .featureType(tmft.getId())
106 .key(a.getName())
107
108
109
110 .type(isGeometry(a.getType()) ? TMAttributeType.GEOMETRY : a.getType())
111
112 .editable(!a.getName().equals(tmft.getPrimaryKeyAttribute())
113 && !readOnlyAttributes.contains(a.getName()))
114 .defaultValue(a.getDefaultValue())
115 .nullable(a.getNullable());
116 })
117 .forEach(r::addAttributesItem);
118
119 return ResponseEntity.ok(r);
120 }
121 }