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(
53        FeatureSourceRepository featureSourceRepository, FormRepository formRepository) {
54      this.featureSourceRepository = featureSourceRepository;
55      this.formRepository = formRepository;
56    }
57  
58    @Transactional
59    @GetMapping
60    @Timed(value = "get_layer_description", description = "Get layer description")
61    public ResponseEntity<Serializable> getAppLayerDescription(
62        @ModelAttribute Application application,
63        @ModelAttribute AppTreeLayerNode appTreeLayerNode,
64        @ModelAttribute GeoService service,
65        @ModelAttribute GeoServiceLayer layer) {
66  
67      if (layer == null) {
68        throw new ResponseStatusException(
69            HttpStatus.NOT_FOUND, "Can't find layer " + appTreeLayerNode);
70      }
71  
72      TMFeatureType tmft = service.findFeatureTypeForLayer(layer, featureSourceRepository);
73      if (tmft == null) {
74        throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Layer does not have feature type");
75      }
76  
77      LayerDetails r =
78          new LayerDetails()
79              .id(appTreeLayerNode.getId())
80              .serviceId(appTreeLayerNode.getServiceId())
81              .featureTypeName(tmft.getName())
82              .geometryAttribute(tmft.getDefaultGeometryAttribute())
83              // TODO deduce from tmft.getDefaultGeometryAttribute()
84              .geometryAttributeIndex(null /* TODO */)
85              .geometryType(
86                  tmft.getDefaultGeometryDescriptor()
87                      .map(TMAttributeDescriptor::getType)
88                      .map(TMAttributeType::getValue)
89                      .map(TMGeometryType::fromValue)
90                      .orElse(null))
91              .editable(TMFeatureTypeHelper.isEditable(application, appTreeLayerNode, tmft));
92  
93      AppLayerSettings appLayerSettings = application.getAppLayerSettings(appTreeLayerNode);
94  
95      Form form;
96      if (appLayerSettings.getFormId() != null) {
97        form = formRepository.findById(appLayerSettings.getFormId()).orElse(null);
98        if (form != null) {
99          r.setForm(new LayerDetailsForm().options(form.getOptions()).fields(form.getFields()));
100       }
101     }
102 
103     Set<String> readOnlyAttributes =
104         TMFeatureTypeHelper.getReadOnlyAttributes(tmft, appLayerSettings);
105 
106     getConfiguredAttributes(tmft, appLayerSettings).values().stream()
107         .map(
108             pair -> {
109               TMAttributeDescriptor a = pair.getLeft();
110               return new Attribute()
111                   .featureType(tmft.getId())
112                   .key(a.getName())
113                   // Only return generic 'geometry' type for now, frontend doesn't
114                   // handle different geometry types. For the default geometry
115                   // attribute there is a specific geometry type set
116                   .type(isGeometry(a.getType()) ? TMAttributeType.GEOMETRY : a.getType())
117                   // primary key can never be edited
118                   .editable(
119                       !a.getName().equals(tmft.getPrimaryKeyAttribute())
120                           && !readOnlyAttributes.contains(a.getName()))
121                   .defaultValue(a.getDefaultValue())
122                   .nullable(a.getNullable());
123             })
124         .forEach(r::addAttributesItem);
125 
126     return ResponseEntity.ok(r);
127   }
128 }