View Javadoc
1   package de.dlr.shepard.context.references.basicreference.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.InvalidPathException;
7   import de.dlr.shepard.common.util.DateHelper;
8   import de.dlr.shepard.common.util.QueryParamHelper;
9   import de.dlr.shepard.context.collection.services.CollectionService;
10  import de.dlr.shepard.context.collection.services.DataObjectService;
11  import de.dlr.shepard.context.references.basicreference.daos.BasicReferenceDAO;
12  import de.dlr.shepard.context.references.basicreference.entities.BasicReference;
13  import io.quarkus.logging.Log;
14  import jakarta.enterprise.context.RequestScoped;
15  import jakarta.inject.Inject;
16  import java.util.List;
17  
18  @RequestScoped
19  public class BasicReferenceService {
20  
21    @Inject
22    BasicReferenceDAO basicReferenceDAO;
23  
24    @Inject
25    UserService userService;
26  
27    @Inject
28    DataObjectService dataObjectService;
29  
30    @Inject
31    CollectionService collectionService;
32  
33    @Inject
34    DateHelper dateHelper;
35  
36    /**
37     * Searches the neo4j database for a BasicReference
38     *
39     * @param shepardId identifies the searched BasicReference
40     *
41     * @return the BasicReference with the given id or null
42     * @throws InvalidPathException if dataobject or collection could not be found Id
43     * @throws InvalidAuthException if user has no read permission on collection
44     */
45    public BasicReference getReference(long collectionShepardId, long dataObjectShepardId, long shepardId) {
46      dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId);
47  
48      BasicReference basicReference = basicReferenceDAO.findByShepardId(shepardId);
49      if (basicReference == null || basicReference.isDeleted()) {
50        String errorMsg = String.format("ID ERROR - Basic Reference with id %s is null or deleted", shepardId);
51        Log.error(errorMsg);
52        throw new InvalidPathException(errorMsg);
53      }
54  
55      if (
56        basicReference.getDataObject() != null &&
57        !basicReference.getDataObject().getShepardId().equals(dataObjectShepardId)
58      ) {
59        String errorMsg = "ID ERROR - There is no association between dataObject and reference";
60        Log.error(errorMsg);
61        throw new InvalidPathException(errorMsg);
62      }
63  
64      return basicReference;
65    }
66  
67    /**
68     * Searches the database for BasicReferences.
69     *
70     * @param collectionShepardId
71     * @param dataObjectShepardId identifies the DataObject
72     * @param params              encapsulates possible parameters
73     * @return a List of BasicReferences
74     * @throws InvalidPathException if dataobject or collection could not be found Id
75     * @throws InvalidAuthException if user has no read permission on collection
76     */
77    public List<BasicReference> getAllBasicReferences(
78      long collectionShepardId,
79      long dataObjectShepardId,
80      QueryParamHelper params
81    ) {
82      dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId);
83  
84      var references = basicReferenceDAO.findByDataObjectShepardId(dataObjectShepardId, params);
85      return references;
86    }
87  
88    /**
89     * Set the deleted flag for the Reference
90     *
91     * @param basicReferenceShepardId identifies the BasicReference to be deleted
92     * @return a boolean to identify if the BasicReference was successfully removed
93     */
94    public void deleteReference(long collectionShepardId, long dataObjectShepardId, long basicReferenceShepardId) {
95      var basicReference = getReference(collectionShepardId, dataObjectShepardId, basicReferenceShepardId);
96      collectionService.assertIsAllowedToEditCollection(collectionShepardId);
97  
98      User user = userService.getCurrentUser();
99  
100     basicReference.setDeleted(true);
101     basicReference.setUpdatedAt(dateHelper.getDate());
102     basicReference.setUpdatedBy(user);
103 
104     basicReferenceDAO.createOrUpdate(basicReference);
105   }
106 }