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 = 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    * Creates a new collection reference.
113    *
114    * @param collectionShepardId
115    * @param dataObjectShepardId
116    * @return CollectionReference
117    * @throws InvalidPathException if collection or dataobject specified by their
118    *                              Ids are null or deleted
119    * @throws InvalidAuthException if user has no permissions to request the
120    *                              collection, which the reference is assigned to
121    * @throws InvalidBodyException if user has no permissions to access the
122    *                              referenced collection or the referenced
123    *                              collection cannot be found
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    * Deletes the collection reference.
171    *
172    * @param collectionShepardId
173    * @param dataObjectShepardId
174    * @param collectionReferenceShepardId
175    * @throws InvalidPathException if collection or dataobject specified by their
176    *                              Ids are null or deleted
177    * @throws InvalidAuthException if user has no permissions to request the
178    *                              collection, which the reference is assigned to
179    * @throws InvalidBodyException If user has no permissions to access the
180    *                              referenced collection or the referenced
181    *                              collection cannot be found
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    * Returns the payload specified in the collection reference.
202    *
203    * @param collectionShepardId
204    * @param dataObjectShepardId
205    * @param collectionReferenceShepardId
206    * @param versionUID
207    * @return Collection
208    * @throws InvalidPathException if collection or dataobject specified by their
209    *                              Ids are null or deleted, or if the referenced
210    *                              collection has been deleted
211    * @throws InvalidAuthException if user has no permissions to request the
212    *                              collection, which the reference is assigned to
213    * @throws InvalidBodyException If user has no permissions to access the
214    *                              referenced collection or the referenced
215    *                              collection cannot be found
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 }