View Javadoc
1   package de.dlr.shepard.context.references.uri.services;
2   
3   import de.dlr.shepard.auth.security.AuthenticationContext;
4   import de.dlr.shepard.auth.users.entities.User;
5   import de.dlr.shepard.auth.users.services.UserService;
6   import de.dlr.shepard.common.exceptions.InvalidAuthException;
7   import de.dlr.shepard.common.exceptions.InvalidPathException;
8   import de.dlr.shepard.common.util.DateHelper;
9   import de.dlr.shepard.context.collection.entities.DataObject;
10  import de.dlr.shepard.context.collection.services.CollectionService;
11  import de.dlr.shepard.context.collection.services.DataObjectService;
12  import de.dlr.shepard.context.references.IReferenceService;
13  import de.dlr.shepard.context.references.uri.daos.URIReferenceDAO;
14  import de.dlr.shepard.context.references.uri.entities.URIReference;
15  import de.dlr.shepard.context.references.uri.io.URIReferenceIO;
16  import de.dlr.shepard.context.version.services.VersionService;
17  import io.quarkus.logging.Log;
18  import jakarta.enterprise.context.RequestScoped;
19  import jakarta.inject.Inject;
20  import java.util.List;
21  import java.util.UUID;
22  
23  @RequestScoped
24  public class URIReferenceService implements IReferenceService<URIReference, URIReferenceIO> {
25  
26    @Inject
27    URIReferenceDAO uRIReferenceDAO;
28  
29    @Inject
30    DataObjectService dataObjectService;
31  
32    @Inject
33    CollectionService collectionService;
34  
35    @Inject
36    UserService userService;
37  
38    @Inject
39    VersionService versionService;
40  
41    @Inject
42    DateHelper dateHelper;
43  
44    @Inject
45    AuthenticationContext authenticationContext;
46  
47    /**
48     * Gets URIReference list for a given dataobject.
49     *
50     * @param collectionShepardId
51     * @param dataObjectShepardId
52     * @param versionUID the version UUID
53     * @return List<URIReference>
54     * @throws InvalidPathException If collection or dataobject cannot be found, or no association between dataobject and collection exists
55     * @throws InvalidAuthException If user has no read permissions on collection or dataobject specified by request path
56     */
57    @Override
58    public List<URIReference> getAllReferencesByDataObjectId(
59      long collectionShepardId,
60      long dataObjectShepardId,
61      UUID versionUID
62    ) {
63      dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId, versionUID);
64  
65      var references = uRIReferenceDAO.findByDataObjectShepardId(dataObjectShepardId);
66      return references;
67    }
68  
69    /**
70     * Gets URIReference by shepard id.
71     *
72     * @param collectionShepardId
73     * @param dataObjectShepardId
74     * @param uriReferenceShepardId
75     * @param versionUID the version UUID
76     * @return URIReference
77     * @throws InvalidPathException If reference with Id does not exist or is deleted, or if collection or dataObject Id of path is not valid
78     * @throws InvalidAuthException If user has no read permissions on collection or dataobject specified by request path
79     */
80    public URIReference getReference(
81      long collectionShepardId,
82      long dataObjectShepardId,
83      long uriReferenceShepardId,
84      UUID versionUID
85    ) {
86      dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId, versionUID);
87  
88      URIReference reference = uRIReferenceDAO.findByShepardId(uriReferenceShepardId, versionUID);
89      if (reference == null || reference.isDeleted()) {
90        String errorMsg = String.format("ID ERROR - URI Reference with id %s is null or deleted", uriReferenceShepardId);
91        Log.error(errorMsg);
92        throw new InvalidPathException(errorMsg);
93      }
94  
95      if (reference.getDataObject() == null || !reference.getDataObject().getShepardId().equals(dataObjectShepardId)) {
96        String errorMsg = "ID ERROR - There is no association between dataObject and reference";
97        Log.error(errorMsg);
98        throw new InvalidPathException(errorMsg);
99      }
100 
101     return reference;
102   }
103 
104   /**
105    * Creates a new URIReference
106    *
107    * @param collectionShepardId
108    * @param dataObjectShepardId DataObject id for the reference to be created
109    * @param uriReference Reference object
110    * @return URIReference
111    * @throws InvalidPathException if collection or dataobject specified by their Ids are null or deleted
112    * @throws InvalidAuthException if user has no permission to edit referencing collection or no read permissions on referenced container
113    */
114   @Override
115   public URIReference createReference(long collectionShepardId, long dataObjectShepardId, URIReferenceIO uriReference) {
116     DataObject dataObject = dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId);
117     collectionService.assertIsAllowedToEditCollection(collectionShepardId);
118 
119     User user = userService.getCurrentUser();
120 
121     var toCreate = new URIReference();
122     toCreate.setCreatedAt(dateHelper.getDate());
123     toCreate.setCreatedBy(user);
124     toCreate.setDataObject(dataObject);
125     toCreate.setName(uriReference.getName());
126     toCreate.setUri(uriReference.getUri());
127     toCreate.setRelationship(uriReference.getRelationship());
128 
129     var created = uRIReferenceDAO.createOrUpdate(toCreate);
130     created.setShepardId(created.getId());
131     created = uRIReferenceDAO.createOrUpdate(created);
132     versionService.attachToVersionOfVersionableEntityAndReturnVersion(dataObject.getId(), created.getId());
133     return created;
134   }
135 
136   /**
137    * Deletes the URI reference.
138    *
139    * @param collectionShepardId
140    * @param dataObjectShepardId
141    * @param uriReferenceShepardId
142    * @throws InvalidPathException if collection or dataobject specified by their Ids are null or deleted
143    * @throws InvalidAuthException if user has no permissions to edit the collection, which the reference is assigned to
144    */
145   @Override
146   public void deleteReference(long collectionShepardId, long dataObjectShepardId, long uriReferenceShepardId) {
147     var old = getReference(collectionShepardId, dataObjectShepardId, uriReferenceShepardId, null);
148     collectionService.assertIsAllowedToEditCollection(collectionShepardId);
149 
150     User user = userService.getCurrentUser();
151 
152     old.setDeleted(true);
153     old.setUpdatedAt(dateHelper.getDate());
154     old.setUpdatedBy(user);
155 
156     uRIReferenceDAO.createOrUpdate(old);
157   }
158 }