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 = String.format(
96 "ID ERROR - Collection Reference with id %s is null or deleted",
97 collectionReferenceShepardId
98 );
99 Log.error(errorMsg);
100 throw new InvalidPathException(errorMsg);
101 }
102
103 if (reference.getDataObject() == null || !reference.getDataObject().getShepardId().equals(dataObjectShepardId)) {
104 Log.error("ID ERROR - There is no association between dataObject and reference");
105 throw new InvalidPathException("ID ERROR - There is no association between dataObject and reference");
106 }
107
108 return reference;
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 @Override
126 public CollectionReference createReference(
127 long collectionShepardId,
128 long dataObjectShepardId,
129 CollectionReferenceIO collectionReference
130 ) {
131 DataObject dataObject = dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId);
132 collectionService.assertIsAllowedToEditCollection(collectionShepardId);
133
134 User user = userService.getCurrentUser();
135 Collection referenced;
136 try {
137 referenced = collectionService.getCollection(collectionReference.getReferencedCollectionId());
138 } catch (InvalidPathException e) {
139 throw new InvalidBodyException(
140 String.format(
141 "The referenced collection with id %d could not be found.",
142 collectionReference.getReferencedCollectionId()
143 )
144 );
145 } catch (InvalidAuthException e) {
146 throw new InvalidAuthException(
147 String.format(
148 "You do not have permissions to access the referenced collection with id %d.",
149 collectionReference.getReferencedCollectionId()
150 )
151 );
152 }
153
154 CollectionReference toCreate = new CollectionReference();
155 toCreate.setCreatedAt(dateHelper.getDate());
156 toCreate.setCreatedBy(user);
157 toCreate.setDataObject(dataObject);
158 toCreate.setName(collectionReference.getName());
159 toCreate.setReferencedCollection(referenced);
160 toCreate.setRelationship(collectionReference.getRelationship());
161
162 CollectionReference created = collectionReferenceDAO.createOrUpdate(toCreate);
163 created.setShepardId(created.getId());
164 created = collectionReferenceDAO.createOrUpdate(created);
165 versionService.attachToVersionOfVersionableEntityAndReturnVersion(dataObject.getId(), created.getId());
166 return created;
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 @Override
184 public void deleteReference(long collectionShepardId, long dataObjectShepardId, long collectionReferenceShepardId) {
185 CollectionReference old = getReference(
186 collectionShepardId,
187 dataObjectShepardId,
188 collectionReferenceShepardId,
189 null
190 );
191 collectionService.assertIsAllowedToEditCollection(collectionShepardId);
192
193 User user = userService.getCurrentUser();
194 old.setDeleted(true);
195 old.setUpdatedAt(dateHelper.getDate());
196 old.setUpdatedBy(user);
197 collectionReferenceDAO.createOrUpdate(old);
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217 public Collection getPayload(
218 long collectionShepardId,
219 long dataObjectShepardId,
220 long collectionReferenceShepardId,
221 UUID versionUID
222 ) {
223 CollectionReference reference = getReference(
224 collectionShepardId,
225 dataObjectShepardId,
226 collectionReferenceShepardId,
227 versionUID
228 );
229
230 if (reference.getReferencedCollection() == null || reference.getReferencedCollection().isDeleted()) {
231 String errorMsg = String.format(
232 "Collection referenced by CollectionReference with id %s cannot be found or is deleted",
233 reference.getShepardId()
234 );
235 Log.errorf(errorMsg);
236 throw new NotFoundException(errorMsg);
237 }
238
239 try {
240 return collectionService.getCollectionWithDataObjectsAndIncomingReferences(
241 reference.getReferencedCollection().getShepardId()
242 );
243 } catch (InvalidPathException e) {
244 throw new NotFoundException(
245 String.format(
246 "The referenced collection with id %d could not be found.",
247 reference.getReferencedCollection().getShepardId()
248 )
249 );
250 }
251 }
252 }