View Javadoc
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     * Gets CollectionReference list for a given dataobject.
48     *
49     * @param collectionShepardId
50     * @param dataObjectShepardId
51     * @param versionUID          the collections UUID
52     * @return List<CollectionReference>
53     * @throws InvalidPathException If collection or dataobject cannot be found, or
54     *                              no association between dataobject and collection
55     *                              exists
56     * @throws InvalidAuthException If user has no read permissions on collection or
57     *                              dataobject specified by request path
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     * Gets CollectionReference by Collection Id.
72     *
73     * @param collectionShepardId
74     * @param dataObjectShepardId
75     * @param collectionReferenceShepardId
76     * @param versionUID                   the collections UUID
77     * @return CollectionReference
78     * @throws InvalidPathException If collection reference with Id does not exist
79     *                              or is deleted, or if collection or dataobject Id
80     *                              of path is not valid
81     * @throws InvalidAuthException If user has no read permissions on collection or
82     *                              dataobject specified by request path
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    * Creates a new collection reference.
111    *
112    * @param collectionShepardId
113    * @param dataObjectShepardId
114    * @return CollectionReference
115    * @throws InvalidPathException if collection or dataobject specified by their
116    *                              Ids are null or deleted
117    * @throws InvalidAuthException if user has no permissions to request the
118    *                              collection, which the reference is assigned to
119    * @throws InvalidBodyException if user has no permissions to access the
120    *                              referenced collection or the referenced
121    *                              collection cannot be found
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    * Deletes the collection reference.
167    *
168    * @param collectionShepardId
169    * @param dataObjectShepardId
170    * @param collectionReferenceShepardId
171    * @throws InvalidPathException if collection or dataobject specified by their
172    *                              Ids are null or deleted
173    * @throws InvalidAuthException if user has no permissions to request the
174    *                              collection, which the reference is assigned to
175    * @throws InvalidBodyException If user has no permissions to access the
176    *                              referenced collection or the referenced
177    *                              collection cannot be found
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    * Returns the payload specified in the collection reference.
198    *
199    * @param collectionShepardId
200    * @param dataObjectShepardId
201    * @param collectionReferenceShepardId
202    * @param versionUID
203    * @return Collection
204    * @throws InvalidPathException if collection or dataobject specified by their
205    *                              Ids are null or deleted, or if the referenced
206    *                              collection has been deleted
207    * @throws InvalidAuthException if user has no permissions to request the
208    *                              collection, which the reference is assigned to
209    * @throws InvalidBodyException If user has no permissions to access the
210    *                              referenced collection or the referenced
211    *                              collection cannot be found
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 }