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.FormOptions;
32  import org.tailormap.api.persistence.json.GeoServiceLayer;
33  import org.tailormap.api.persistence.json.TMAttributeDescriptor;
34  import org.tailormap.api.persistence.json.TMAttributeType;
35  import org.tailormap.api.persistence.json.TMGeometryType;
36  import org.tailormap.api.repository.FeatureSourceRepository;
37  import org.tailormap.api.repository.FormRepository;
38  import org.tailormap.api.viewer.model.Attribute;
39  import org.tailormap.api.viewer.model.LayerDetails;
40  import org.tailormap.api.viewer.model.LayerDetailsForm;
41  
42  @AppRestController
43  @Validated
44  @RequestMapping(
45      path = "${tailormap-api.base-path}/{viewerKind}/{viewerName}/layer/{appLayerId}/describe",
46      produces = MediaType.APPLICATION_JSON_VALUE)
47  public class LayerDescriptionController {
48  
49    private final FeatureSourceRepository featureSourceRepository;
50    private final TMFeatureTypeHelper featureTypeHelper;
51    private final FormRepository formRepository;
52  
53    public LayerDescriptionController(
54        FeatureSourceRepository featureSourceRepository,
55        TMFeatureTypeHelper featureTypeHelper,
56        FormRepository formRepository) {
57      this.featureSourceRepository = featureSourceRepository;
58      this.featureTypeHelper = featureTypeHelper;
59      this.formRepository = formRepository;
60    }
61  
62    @Transactional
63    @GetMapping
64    @Timed(value = "get_layer_description", description = "Get layer description")
65    public ResponseEntity<Serializable> getAppLayerDescription(
66        @ModelAttribute Application application,
67        @ModelAttribute AppTreeLayerNode appTreeLayerNode,
68        @ModelAttribute GeoService service,
69        @ModelAttribute GeoServiceLayer layer) {
70  
71      if (layer == null) {
72        throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Can't find layer " + appTreeLayerNode);
73      }
74  
75      TMFeatureType tmft = service.findFeatureTypeForLayer(layer, featureSourceRepository);
76      if (tmft == null) {
77        throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Layer does not have feature type");
78      }
79  
80      LayerDetails r = new LayerDetails()
81          .id(appTreeLayerNode.getId())
82          .serviceId(appTreeLayerNode.getServiceId())
83          .featureTypeName(tmft.getName())
84          .geometryAttribute(tmft.getDefaultGeometryAttribute())
85          // TODO deduce from tmft.getDefaultGeometryAttribute()
86          .geometryAttributeIndex(null /* TODO */)
87          .geometryType(tmft.getDefaultGeometryDescriptor()
88              .map(TMAttributeDescriptor::getType)
89              .map(TMAttributeType::getValue)
90              .map(TMGeometryType::fromValue)
91              .orElse(null))
92          .editable(TMFeatureTypeHelper.isEditable(application, appTreeLayerNode, tmft))
93          .attachmentAttributes(featureTypeHelper.getAttachmentAttributesWithMaxFileUploadSize(tmft).stream()
94              .toList());
95  
96      AppLayerSettings appLayerSettings = application.getAppLayerSettings(appTreeLayerNode);
97  
98      Form form;
99      if (appLayerSettings.getFormId() != null) {
100       form = formRepository.findById(appLayerSettings.getFormId()).orElse(null);
101       if (form != null) {
102         // Leave out the admin comments
103         r.setForm(new LayerDetailsForm()
104             .options(new FormOptions().tabs(form.getOptions().getTabs()))
105             .fields(form.getFields()));
106       }
107     }
108 
109     Set<String> editableAttributes = TMFeatureTypeHelper.getEditableAttributes(tmft, appLayerSettings);
110 
111     getConfiguredAttributes(tmft, appLayerSettings).values().stream()
112         .map(attributeWithSettings -> {
113           TMAttributeDescriptor a = attributeWithSettings.attributeDescriptor();
114           return new Attribute()
115               .name(a.getName())
116               .alias(attributeWithSettings.settings().getTitle())
117               // Only return generic 'geometry' type for now, frontend doesn't
118               // handle different geometry types. For the default geometry
119               // attribute there is a specific geometry type set
120               .type(isGeometry(a.getType()) ? TMAttributeType.GEOMETRY : a.getType())
121               // An editable primary key can only be set when creating a feature, see
122               // EditFeatureController
123               .editable(editableAttributes.contains(a.getName()))
124               .primaryKey(a.getName().equals(tmft.getPrimaryKeyAttribute()))
125               .defaultValue(a.getDefaultValue())
126               .nullable(a.getNullable());
127         })
128         .forEach(r::addAttributesItem);
129 
130     return ResponseEntity.ok(r);
131   }
132 }