1 package de.dlr.shepard.context.references.spatialdata.services;
2
3 import com.fasterxml.jackson.core.JsonProcessingException;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import de.dlr.shepard.auth.security.AuthenticationContext;
6 import de.dlr.shepard.auth.users.entities.User;
7 import de.dlr.shepard.auth.users.services.UserService;
8 import de.dlr.shepard.common.exceptions.InvalidBodyException;
9 import de.dlr.shepard.common.exceptions.InvalidPathException;
10 import de.dlr.shepard.common.exceptions.InvalidRequestException;
11 import de.dlr.shepard.common.util.DateHelper;
12 import de.dlr.shepard.context.collection.services.CollectionService;
13 import de.dlr.shepard.context.collection.services.DataObjectService;
14 import de.dlr.shepard.context.references.IReferenceService;
15 import de.dlr.shepard.context.references.spatialdata.daos.SpatialDataReferenceDAO;
16 import de.dlr.shepard.context.references.spatialdata.entities.SpatialDataReference;
17 import de.dlr.shepard.context.references.spatialdata.io.SpatialDataReferenceIO;
18 import de.dlr.shepard.context.version.services.VersionService;
19 import de.dlr.shepard.data.spatialdata.io.SpatialDataPointIO;
20 import de.dlr.shepard.data.spatialdata.model.SpatialDataContainer;
21 import de.dlr.shepard.data.spatialdata.services.SpatialDataContainerService;
22 import de.dlr.shepard.data.spatialdata.services.SpatialDataPointService;
23 import io.quarkus.logging.Log;
24 import jakarta.enterprise.context.RequestScoped;
25 import jakarta.inject.Inject;
26 import jakarta.ws.rs.NotFoundException;
27 import java.util.List;
28 import java.util.UUID;
29
30 @RequestScoped
31 public class SpatialDataReferenceService implements IReferenceService<SpatialDataReference, SpatialDataReferenceIO> {
32
33 @Inject
34 SpatialDataReferenceDAO spatialDataReferenceDAO;
35
36 @Inject
37 SpatialDataPointService dataPointService;
38
39 @Inject
40 DataObjectService dataObjectService;
41
42 @Inject
43 DateHelper dateHelper;
44
45 @Inject
46 UserService userService;
47
48 @Inject
49 CollectionService collectionService;
50
51 @Inject
52 VersionService versionService;
53
54 @Inject
55 SpatialDataContainerService containerService;
56
57 @Inject
58 AuthenticationContext authenticationContext;
59
60 @Override
61 public List<SpatialDataReference> getAllReferencesByDataObjectId(
62 long collectionShepardId,
63 long dataObjectShepardId,
64 UUID versionUID
65 ) {
66 dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId, versionUID);
67
68 var references = spatialDataReferenceDAO.findByDataObjectShepardId(dataObjectShepardId);
69 return references;
70 }
71
72 @Override
73 public SpatialDataReference getReference(
74 long collectionShepardId,
75 long dataObjectShepardId,
76 long shepardId,
77 UUID versionUID
78 ) {
79 dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId, versionUID);
80
81 SpatialDataReference spatialDataReference = spatialDataReferenceDAO.findByShepardId(shepardId, versionUID);
82 if (spatialDataReference == null || spatialDataReference.isDeleted()) {
83 String errorMsg = String.format("ID ERROR - SpatialData Reference with id %s is null or deleted", shepardId);
84 Log.error(errorMsg);
85 throw new InvalidPathException(errorMsg);
86 }
87
88 if (
89 spatialDataReference.getDataObject() == null ||
90 !spatialDataReference.getDataObject().getShepardId().equals(dataObjectShepardId)
91 ) {
92 String errorMsg = String.format("ID ERROR - There is no association between dataObject and reference", shepardId);
93 Log.error(errorMsg);
94 throw new InvalidPathException(errorMsg);
95 }
96 return spatialDataReference;
97 }
98
99 @Override
100 public SpatialDataReference createReference(
101 long collectionShepardId,
102 long dataObjectShepardId,
103 SpatialDataReferenceIO spatialDataReferenceIO
104 ) {
105 var dataObject = dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId);
106 collectionService.assertIsAllowedToEditCollection(collectionShepardId);
107
108 User user = userService.getCurrentUser();
109
110 SpatialDataContainer container;
111 try {
112 container = containerService.getContainer(spatialDataReferenceIO.getSpatialDataContainerId());
113 } catch (InvalidPathException e) {
114 throw new InvalidBodyException(e.getMessage());
115 }
116
117 ObjectMapper objectMapper = new ObjectMapper();
118 var toCreate = new SpatialDataReference();
119 toCreate.setCreatedAt(dateHelper.getDate());
120 toCreate.setCreatedBy(user);
121 toCreate.setDataObject(dataObject);
122 toCreate.setName(spatialDataReferenceIO.getName());
123 if (spatialDataReferenceIO.getGeometryFilter() != null) {
124 try {
125 toCreate.setGeometryFilter(objectMapper.writeValueAsString(spatialDataReferenceIO.getGeometryFilter()));
126 } catch (JsonProcessingException e) {
127 throw new InvalidRequestException("Failed to parse geometry filter");
128 }
129 }
130 if (spatialDataReferenceIO.getMeasurementsFilter() != null) {
131 try {
132 toCreate.setMeasurementsFilter(objectMapper.writeValueAsString(spatialDataReferenceIO.getMeasurementsFilter()));
133 } catch (JsonProcessingException e) {
134 throw new InvalidRequestException("Failed to parse measurement filter");
135 }
136 }
137 if (spatialDataReferenceIO.getMetadataFilter() != null) {
138 try {
139 toCreate.setMetadata(objectMapper.writeValueAsString(spatialDataReferenceIO.getMetadataFilter()));
140 } catch (JsonProcessingException e) {
141 throw new InvalidRequestException("Failed to parse metadata filter");
142 }
143 }
144 toCreate.setStartTime(spatialDataReferenceIO.getStartTime());
145 toCreate.setEndTime(spatialDataReferenceIO.getEndTime());
146 toCreate.setLimit(spatialDataReferenceIO.getLimit());
147 toCreate.setSkip(spatialDataReferenceIO.getSkip());
148 toCreate.setSpatialDataContainer(container);
149
150 List<String> validationErrors = toCreate.toSpatialDataQueryParams().validate();
151 if (!validationErrors.isEmpty()) {
152 throw new InvalidRequestException(
153 "The specified parameters contain the following errors: " + String.join(", ", validationErrors)
154 );
155 }
156 SpatialDataReference created = spatialDataReferenceDAO.createOrUpdate(toCreate);
157 created.setShepardId(created.getId());
158 created = spatialDataReferenceDAO.createOrUpdate(created);
159 versionService.attachToVersionOfVersionableEntityAndReturnVersion(dataObject.getId(), created.getId());
160 return created;
161 }
162
163 @Override
164 public void deleteReference(long collectionShepardId, long dataObjectShepardId, long shepardId) {
165 SpatialDataReference spatialDataReference = getReference(collectionShepardId, dataObjectShepardId, shepardId, null);
166 collectionService.assertIsAllowedToEditCollection(collectionShepardId);
167
168 User user = userService.getCurrentUser();
169 spatialDataReference.setDeleted(true);
170 spatialDataReference.setUpdatedBy(user);
171 spatialDataReference.setUpdatedAt(dateHelper.getDate());
172 spatialDataReferenceDAO.createOrUpdate(spatialDataReference);
173 }
174
175 public List<SpatialDataPointIO> getReferencePayload(
176 long collectionShepardId,
177 long dataObjectShepardId,
178 long spatialDataReferenceId
179 ) {
180 SpatialDataReference reference = getReference(
181 collectionShepardId,
182 dataObjectShepardId,
183 spatialDataReferenceId,
184 null
185 );
186
187 if (reference.getSpatialDataContainer() == null || reference.getSpatialDataContainer().isDeleted()) {
188 String errorMsg = String.format(
189 "Referenced SpatialDataContainer is not set or deleted in SpatialDataReference with id %s",
190 reference.getId()
191 );
192 Log.error(errorMsg);
193 throw new NotFoundException(errorMsg);
194 }
195
196 try {
197
198 return dataPointService.getSpatialDataPointIOs(
199 reference.getSpatialDataContainer().getId(),
200 reference.toSpatialDataQueryParams()
201 );
202 } catch (InvalidPathException ex) {
203 Log.error(ex.getMessage());
204 throw new NotFoundException(ex.getMessage());
205 }
206 }
207 }