1 package de.dlr.shepard.context.references.file.services;
2
3 import de.dlr.shepard.auth.permission.services.PermissionsService;
4 import de.dlr.shepard.auth.security.AuthenticationContext;
5 import de.dlr.shepard.auth.users.entities.User;
6 import de.dlr.shepard.auth.users.services.UserService;
7 import de.dlr.shepard.common.exceptions.InvalidAuthException;
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.mongoDB.NamedInputStream;
12 import de.dlr.shepard.common.util.DateHelper;
13 import de.dlr.shepard.context.collection.services.CollectionService;
14 import de.dlr.shepard.context.collection.services.DataObjectService;
15 import de.dlr.shepard.context.references.IReferenceService;
16 import de.dlr.shepard.context.references.file.daos.FileReferenceDAO;
17 import de.dlr.shepard.context.references.file.entities.FileReference;
18 import de.dlr.shepard.context.references.file.io.FileReferenceIO;
19 import de.dlr.shepard.context.version.services.VersionService;
20 import de.dlr.shepard.data.file.daos.ShepardFileDAO;
21 import de.dlr.shepard.data.file.entities.FileContainer;
22 import de.dlr.shepard.data.file.entities.ShepardFile;
23 import de.dlr.shepard.data.file.services.FileContainerService;
24 import de.dlr.shepard.data.file.services.FileService;
25 import io.quarkus.logging.Log;
26 import jakarta.enterprise.context.RequestScoped;
27 import jakarta.inject.Inject;
28 import jakarta.ws.rs.NotFoundException;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.UUID;
32
33 @RequestScoped
34 public class FileReferenceService implements IReferenceService<FileReference, FileReferenceIO> {
35
36 @Inject
37 FileReferenceDAO fileReferenceDAO;
38
39 @Inject
40 DataObjectService dataObjectService;
41
42 @Inject
43 FileContainerService fileContainerService;
44
45 @Inject
46 ShepardFileDAO fileDAO;
47
48 @Inject
49 VersionService versionService;
50
51 @Inject
52 DateHelper dateHelper;
53
54 @Inject
55 FileService fileService;
56
57 @Inject
58 PermissionsService permissionsService;
59
60 @Inject
61 CollectionService collectionService;
62
63 @Inject
64 AuthenticationContext authenticationContext;
65
66 @Inject
67 UserService userService;
68
69
70
71
72
73
74
75
76
77
78
79 @Override
80 public List<FileReference> getAllReferencesByDataObjectId(
81 long collectionShepardId,
82 long dataObjectShepardId,
83 UUID versionUID
84 ) {
85 dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId, versionUID);
86
87 List<FileReference> references = fileReferenceDAO.findByDataObjectShepardId(dataObjectShepardId);
88 return references;
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102 @Override
103 public FileReference getReference(
104 long collectionShepardId,
105 long dataObjectShepardId,
106 long shepardId,
107 UUID versionUID
108 ) {
109 dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId, versionUID);
110
111 FileReference fileReference = fileReferenceDAO.findByShepardId(shepardId, versionUID);
112 if (fileReference == null || fileReference.isDeleted()) {
113 String errorMsg = "ID ERROR - File Reference with id %s is null or deleted".formatted(shepardId);
114 Log.error(errorMsg);
115 throw new InvalidPathException(errorMsg);
116 }
117
118 if (
119 fileReference.getDataObject() == null || !fileReference.getDataObject().getShepardId().equals(dataObjectShepardId)
120 ) {
121 String errorMsg = "ID ERROR - There is no association between dataObject and reference";
122 Log.error(errorMsg);
123 throw new InvalidPathException(errorMsg);
124 }
125
126 return fileReference;
127 }
128
129
130
131
132
133
134
135
136
137
138
139 @Override
140 public FileReference createReference(
141 long collectionShepardId,
142 long dataObjectShepardId,
143 FileReferenceIO fileReference
144 ) {
145 var dataObject = dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId);
146 collectionService.assertIsAllowedToEditCollection(collectionShepardId);
147
148 User user = userService.getCurrentUser();
149
150 FileContainer container;
151 try {
152 container = fileContainerService.getContainer(fileReference.getFileContainerId());
153 } catch (InvalidPathException | InvalidAuthException ex) {
154 Log.error(ex.getMessage());
155 throw new InvalidBodyException(ex.getMessage());
156 }
157
158 var toCreate = new FileReference();
159 toCreate.setCreatedAt(dateHelper.getDate());
160 toCreate.setCreatedBy(user);
161 toCreate.setDataObject(dataObject);
162 toCreate.setName(fileReference.getName());
163 toCreate.setFileContainer(container);
164
165
166 for (var oid : fileReference.getFileOids()) {
167 var file = fileDAO.find(container.getId(), oid);
168 if (file != null) {
169 toCreate.addFile(file);
170 } else {
171 Log.warnf("Could not find file with oid: %s", oid);
172 }
173 }
174
175 var created = fileReferenceDAO.createOrUpdate(toCreate);
176 created.setShepardId(created.getId());
177 created = fileReferenceDAO.createOrUpdate(created);
178 versionService.attachToVersionOfVersionableEntityAndReturnVersion(dataObject.getId(), created.getId());
179 return created;
180 }
181
182
183
184
185
186
187
188
189
190
191 @Override
192 public void deleteReference(long collectionShepardId, long dataObjectShepardId, long fileReferenceShepardId) {
193 FileReference fileReference = getReference(collectionShepardId, dataObjectShepardId, fileReferenceShepardId, null);
194 collectionService.assertIsAllowedToEditCollection(collectionShepardId);
195
196 User user = userService.getCurrentUser();
197 fileReference.setDeleted(true);
198 fileReference.setUpdatedBy(user);
199 fileReference.setUpdatedAt(dateHelper.getDate());
200 fileReferenceDAO.createOrUpdate(fileReference);
201 }
202
203
204
205
206
207
208
209
210
211
212
213 public List<ShepardFile> getFiles(
214 long collectionShepardId,
215 long dataObjectShepardId,
216 long fileReferenceShepardId,
217 UUID versionUID
218 ) {
219 FileReference reference = getReference(
220 collectionShepardId,
221 dataObjectShepardId,
222 fileReferenceShepardId,
223 versionUID
224 );
225
226 if (reference.getFileContainer() == null || reference.getFileContainer().isDeleted()) {
227 String errorMsg =
228 "Referenced FileContainer is not set or deleted in FileReference with id %s".formatted(reference.getId());
229 Log.error(errorMsg);
230 throw new NotFoundException(errorMsg);
231 }
232
233 try {
234 fileContainerService.getContainer(reference.getFileContainer().getId());
235 } catch (InvalidPathException ex) {
236 Log.error(ex.getMessage());
237 throw new NotFoundException(ex.getMessage());
238 }
239
240 return reference.getFiles();
241 }
242
243
244
245
246
247
248
249
250
251
252
253
254
255 public NamedInputStream getPayload(
256 long collectionShepardId,
257 long dataObjectShepardId,
258 long fileReferenceShepardId,
259 String oid,
260 UUID versionUID
261 ) {
262 FileReference reference = getReference(
263 collectionShepardId,
264 dataObjectShepardId,
265 fileReferenceShepardId,
266 versionUID
267 );
268
269 if (reference.getFileContainer() == null || reference.getFileContainer().isDeleted()) {
270 String errorMsg =
271 "FileContainer with id %s is not set or deleted in FileReference".formatted(reference.getFileContainer());
272 Log.error(errorMsg);
273 throw new NotFoundException(errorMsg);
274 }
275
276 try {
277
278 fileContainerService.getContainer(reference.getFileContainer().getId());
279 } catch (InvalidPathException e) {
280 Log.error(e.getMessage());
281 throw new NotFoundException(e.getMessage());
282 }
283
284 String mongoId = reference.getFileContainer().getMongoId();
285 return fileService.getPayload(mongoId, oid);
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301 public List<NamedInputStream> getAllPayloads(
302 long collectionShepardId,
303 long dataObjectShepardId,
304 long fileReferenceShepardId
305 ) {
306 FileReference reference = getReference(collectionShepardId, dataObjectShepardId, fileReferenceShepardId, null);
307
308 if (reference.getFileContainer() == null || reference.getFileContainer().isDeleted()) {
309 String errorMsg =
310 "Referenced FileContainer is not set or deleted in FileReference with id %s".formatted(reference.getId());
311 Log.error(errorMsg);
312 throw new NotFoundException(errorMsg);
313 }
314
315 try {
316
317 fileContainerService.getContainer(reference.getFileContainer().getId());
318 } catch (InvalidPathException ex) {
319 throw new NotFoundException(ex.getMessage());
320 }
321
322 List<ShepardFile> files = reference.getFiles();
323
324 var result = new ArrayList<NamedInputStream>(files.size());
325 for (var file : files) {
326 NamedInputStream nis;
327 try {
328 nis = fileService.getPayload(reference.getFileContainer().getMongoId(), file.getOid());
329 result.add(nis);
330 } catch (NotFoundException e) {
331 result.add(new NamedInputStream(file.getOid(), null, file.getFilename(), 0L));
332 }
333 }
334 return result;
335 }
336 }