1 package de.dlr.shepard.context.references.dataobject.services;
2
3 import de.dlr.shepard.auth.users.entities.User;
4 import de.dlr.shepard.auth.users.services.UserService;
5 import de.dlr.shepard.common.exceptions.InvalidAuthException;
6 import de.dlr.shepard.common.exceptions.InvalidBodyException;
7 import de.dlr.shepard.common.exceptions.InvalidPathException;
8 import de.dlr.shepard.common.util.DateHelper;
9 import de.dlr.shepard.context.collection.entities.Collection;
10 import de.dlr.shepard.context.collection.entities.DataObject;
11 import de.dlr.shepard.context.collection.services.CollectionService;
12 import de.dlr.shepard.context.collection.services.DataObjectService;
13 import de.dlr.shepard.context.references.IReferenceService;
14 import de.dlr.shepard.context.references.dataobject.daos.CollectionReferenceDAO;
15 import de.dlr.shepard.context.references.dataobject.entities.CollectionReference;
16 import de.dlr.shepard.context.references.dataobject.io.CollectionReferenceIO;
17 import de.dlr.shepard.context.version.services.VersionService;
18 import io.quarkus.logging.Log;
19 import jakarta.enterprise.context.RequestScoped;
20 import jakarta.inject.Inject;
21 import jakarta.ws.rs.NotFoundException;
22 import java.util.List;
23 import java.util.UUID;
24
25 @RequestScoped
26 public class CollectionReferenceService implements IReferenceService<CollectionReference, CollectionReferenceIO> {
27
28 @Inject
29 CollectionReferenceDAO collectionReferenceDAO;
30
31 @Inject
32 DataObjectService dataObjectService;
33
34 @Inject
35 VersionService versionService;
36
37 @Inject
38 UserService userService;
39
40 @Inject
41 DateHelper dateHelper;
42
43 @Inject
44 CollectionService collectionService;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 @Override
60 public List<CollectionReference> getAllReferencesByDataObjectId(
61 long collectionShepardId,
62 long dataObjectShepardId,
63 UUID versionUID
64 ) {
65 dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId, versionUID);
66 List<CollectionReference> references = collectionReferenceDAO.findByDataObjectShepardId(dataObjectShepardId);
67 return references;
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 @Override
85 public CollectionReference getReference(
86 long collectionShepardId,
87 long dataObjectShepardId,
88 long collectionReferenceShepardId,
89 UUID versionUID
90 ) {
91 dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId, versionUID);
92
93 CollectionReference reference = collectionReferenceDAO.findByShepardId(collectionReferenceShepardId, versionUID);
94 if (reference == null || reference.isDeleted()) {
95 String errorMsg =
96 "ID ERROR - Collection Reference with id %s is null or deleted".formatted(collectionReferenceShepardId);
97 Log.error(errorMsg);
98 throw new InvalidPathException(errorMsg);
99 }
100
101 if (reference.getDataObject() == null || !reference.getDataObject().getShepardId().equals(dataObjectShepardId)) {
102 Log.error("ID ERROR - There is no association between dataObject and reference");
103 throw new InvalidPathException("ID ERROR - There is no association between dataObject and reference");
104 }
105
106 return reference;
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 @Override
124 public CollectionReference createReference(
125 long collectionShepardId,
126 long dataObjectShepardId,
127 CollectionReferenceIO collectionReference
128 ) {
129 DataObject dataObject = dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId);
130 collectionService.assertIsAllowedToEditCollection(collectionShepardId);
131
132 User user = userService.getCurrentUser();
133 Collection referenced;
134 try {
135 referenced = collectionService.getCollection(collectionReference.getReferencedCollectionId());
136 } catch (InvalidPathException e) {
137 throw new InvalidBodyException(
138 "The referenced collection with id %d could not be found.".formatted(
139 collectionReference.getReferencedCollectionId()
140 )
141 );
142 } catch (InvalidAuthException e) {
143 throw new InvalidAuthException(
144 "You do not have permissions to access the referenced collection with id %d.".formatted(
145 collectionReference.getReferencedCollectionId()
146 )
147 );
148 }
149
150 CollectionReference toCreate = new CollectionReference();
151 toCreate.setCreatedAt(dateHelper.getDate());
152 toCreate.setCreatedBy(user);
153 toCreate.setDataObject(dataObject);
154 toCreate.setName(collectionReference.getName());
155 toCreate.setReferencedCollection(referenced);
156 toCreate.setRelationship(collectionReference.getRelationship());
157
158 CollectionReference created = collectionReferenceDAO.createOrUpdate(toCreate);
159 created.setShepardId(created.getId());
160 created = collectionReferenceDAO.createOrUpdate(created);
161 versionService.attachToVersionOfVersionableEntityAndReturnVersion(dataObject.getId(), created.getId());
162 return created;
163 }
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179 @Override
180 public void deleteReference(long collectionShepardId, long dataObjectShepardId, long collectionReferenceShepardId) {
181 CollectionReference old = getReference(
182 collectionShepardId,
183 dataObjectShepardId,
184 collectionReferenceShepardId,
185 null
186 );
187 collectionService.assertIsAllowedToEditCollection(collectionShepardId);
188
189 User user = userService.getCurrentUser();
190 old.setDeleted(true);
191 old.setUpdatedAt(dateHelper.getDate());
192 old.setUpdatedBy(user);
193 collectionReferenceDAO.createOrUpdate(old);
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 public Collection getPayload(
214 long collectionShepardId,
215 long dataObjectShepardId,
216 long collectionReferenceShepardId,
217 UUID versionUID
218 ) {
219 CollectionReference reference = getReference(
220 collectionShepardId,
221 dataObjectShepardId,
222 collectionReferenceShepardId,
223 versionUID
224 );
225
226 if (reference.getReferencedCollection() == null || reference.getReferencedCollection().isDeleted()) {
227 String errorMsg =
228 "Collection referenced by CollectionReference with id %s cannot be found or is deleted".formatted(
229 reference.getShepardId()
230 );
231 Log.errorf(errorMsg);
232 throw new NotFoundException(errorMsg);
233 }
234
235 try {
236 return collectionService.getCollectionWithDataObjectsAndIncomingReferences(
237 reference.getReferencedCollection().getShepardId()
238 );
239 } catch (InvalidPathException e) {
240 throw new NotFoundException(
241 "The referenced collection with id %d could not be found.".formatted(
242 reference.getReferencedCollection().getShepardId()
243 )
244 );
245 }
246 }
247 }