View Javadoc
1   package de.dlr.shepard.neo4Core.services;
2   
3   import de.dlr.shepard.neo4Core.dao.BasicReferenceDAO;
4   import de.dlr.shepard.neo4Core.dao.UserDAO;
5   import de.dlr.shepard.neo4Core.entities.BasicReference;
6   import de.dlr.shepard.util.DateHelper;
7   import de.dlr.shepard.util.QueryParamHelper;
8   import io.quarkus.logging.Log;
9   import jakarta.enterprise.context.RequestScoped;
10  import jakarta.inject.Inject;
11  import java.util.List;
12  
13  @RequestScoped
14  public class BasicReferenceService {
15  
16    private BasicReferenceDAO basicReferenceDAO;
17    private UserDAO userDAO;
18    private DateHelper dateHelper;
19  
20    BasicReferenceService() {}
21  
22    @Inject
23    public BasicReferenceService(BasicReferenceDAO basicReferenceDAO, UserDAO userDAO, DateHelper dateHelper) {
24      this.basicReferenceDAO = basicReferenceDAO;
25      this.userDAO = userDAO;
26      this.dateHelper = dateHelper;
27    }
28  
29    /**
30     * Searches the neo4j database for a BasicReference
31     *
32     * @param shepardId identifies the searched BasicReference
33     *
34     * @return the BasicReference with the given id or null
35     */
36    public BasicReference getReferenceByShepardId(long shepardId) {
37      BasicReference basicReference = basicReferenceDAO.findByShepardId(shepardId);
38      if (basicReference == null || basicReference.isDeleted()) {
39        Log.errorf("Basic Reference with id %s is null or deleted", shepardId);
40        return null;
41      }
42      return basicReference;
43    }
44  
45    /**
46     * Searches the database for BasicReferences.
47     *
48     * @param dataObjectShepardId identifies the DataObject
49     * @param params              encapsulates possible parameters
50     * @return a List of BasicReferences
51     */
52    public List<BasicReference> getAllBasicReferencesByDataObjectShepardId(
53      long dataObjectShepardId,
54      QueryParamHelper params
55    ) {
56      var references = basicReferenceDAO.findByDataObjectShepardId(dataObjectShepardId, params);
57      return references;
58    }
59  
60    /**
61     * Set the deleted flag for the Reference
62     *
63     * @param basicReferenceShepardId identifies the BasicReference to be deleted
64     * @param username                identifies the user
65     * @return a boolean to identify if the BasicReference was successfully removed
66     */
67    public boolean deleteReferenceByShepardId(long basicReferenceShepardId, String username) {
68      var user = userDAO.find(username);
69  
70      var basicReference = basicReferenceDAO.findByShepardId(basicReferenceShepardId);
71      basicReference.setDeleted(true);
72      basicReference.setUpdatedAt(dateHelper.getDate());
73      basicReference.setUpdatedBy(user);
74  
75      basicReferenceDAO.createOrUpdate(basicReference);
76      return true;
77    }
78  }