View Javadoc
1   /*
2    * Copyright (C) 2022 B3Partners B.V.
3    *
4    * SPDX-License-Identifier: MIT
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          // TODO deduce from tmft.getDefaultGeometryAttribute()
81          .geometryAttributeIndex(null /* TODO */)
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          .attachmentAttributes(
89              tmft.getSettings().getAttachmentAttributes().stream().toList());
90  
91      AppLayerSettings appLayerSettings = application.getAppLayerSettings(appTreeLayerNode);
92  
93      Form form;
94      if (appLayerSettings.getFormId() != null) {
95        form = formRepository.findById(appLayerSettings.getFormId()).orElse(null);
96        if (form != null) {
97          r.setForm(new LayerDetailsForm().options(form.getOptions()).fields(form.getFields()));
98        }
99      }
100 
101     Set<String> editableAttributes = TMFeatureTypeHelper.getEditableAttributes(tmft, appLayerSettings);
102 
103     getConfiguredAttributes(tmft, appLayerSettings).values().stream()
104         .map(attributeWithSettings -> {
105           TMAttributeDescriptor a = attributeWithSettings.attributeDescriptor();
106           return new Attribute()
107               .name(a.getName())
108               .alias(attributeWithSettings.settings().getTitle())
109               // Only return generic 'geometry' type for now, frontend doesn't
110               // handle different geometry types. For the default geometry
111               // attribute there is a specific geometry type set
112               .type(isGeometry(a.getType()) ? TMAttributeType.GEOMETRY : a.getType())
113               // primary key can never be edited
114               .editable(!a.getName().equals(tmft.getPrimaryKeyAttribute())
115                   && editableAttributes.contains(a.getName()))
116               .defaultValue(a.getDefaultValue())
117               .nullable(a.getNullable());
118         })
119         .forEach(r::addAttributesItem);
120 
121     return ResponseEntity.ok(r);
122   }
123 }