View Javadoc
1   package de.dlr.shepard.neo4Core.services;
2   
3   import de.dlr.shepard.neo4Core.dao.DataObjectDAO;
4   import de.dlr.shepard.neo4Core.dao.URIReferenceDAO;
5   import de.dlr.shepard.neo4Core.dao.UserDAO;
6   import de.dlr.shepard.neo4Core.dao.VersionDAO;
7   import de.dlr.shepard.neo4Core.entities.URIReference;
8   import de.dlr.shepard.neo4Core.entities.Version;
9   import de.dlr.shepard.neo4Core.io.URIReferenceIO;
10  import de.dlr.shepard.util.DateHelper;
11  import io.quarkus.logging.Log;
12  import jakarta.enterprise.context.RequestScoped;
13  import jakarta.inject.Inject;
14  import java.util.List;
15  
16  @RequestScoped
17  public class URIReferenceService implements IReferenceService<URIReference, URIReferenceIO> {
18  
19    private URIReferenceDAO uRIReferenceDAO;
20    private DataObjectDAO dataObjectDAO;
21    private UserDAO userDAO;
22    private VersionDAO versionDAO;
23    private DateHelper dateHelper;
24  
25    URIReferenceService() {}
26  
27    @Inject
28    public URIReferenceService(
29      URIReferenceDAO uRIReferenceDAO,
30      DataObjectDAO dataObjectDAO,
31      UserDAO userDAO,
32      VersionDAO versionDAO,
33      DateHelper dateHelper
34    ) {
35      this.uRIReferenceDAO = uRIReferenceDAO;
36      this.dataObjectDAO = dataObjectDAO;
37      this.userDAO = userDAO;
38      this.versionDAO = versionDAO;
39      this.dateHelper = dateHelper;
40    }
41  
42    @Override
43    public List<URIReference> getAllReferencesByDataObjectShepardId(long dataObjectShepardId) {
44      var references = uRIReferenceDAO.findByDataObjectShepardId(dataObjectShepardId);
45      return references;
46    }
47  
48    @Override
49    public URIReference getReferenceByShepardId(long uriReferenceShepardId) {
50      var reference = uRIReferenceDAO.findByShepardId(uriReferenceShepardId);
51      if (reference == null || reference.isDeleted()) {
52        Log.errorf("URI Reference with id %s is null or deleted", uriReferenceShepardId);
53        return null;
54      }
55      return reference;
56    }
57  
58    @Override
59    public URIReference createReferenceByShepardId(
60      long dataObjectShepardId,
61      URIReferenceIO uriReference,
62      String username
63    ) {
64      var user = userDAO.find(username);
65      var dataObject = dataObjectDAO.findLightByShepardId(dataObjectShepardId);
66  
67      var toCreate = new URIReference();
68      toCreate.setCreatedAt(dateHelper.getDate());
69      toCreate.setCreatedBy(user);
70      toCreate.setDataObject(dataObject);
71      toCreate.setName(uriReference.getName());
72      toCreate.setUri(uriReference.getUri());
73  
74      var created = uRIReferenceDAO.createOrUpdate(toCreate);
75      created.setShepardId(created.getId());
76      created = uRIReferenceDAO.createOrUpdate(created);
77      Version version = versionDAO.findVersionLightByNeo4jId(dataObject.getId());
78      versionDAO.createLink(created.getId(), version.getUid());
79      return created;
80    }
81  
82    @Override
83    public boolean deleteReferenceByShepardId(long uriReferenceShepardId, String username) {
84      var user = userDAO.find(username);
85  
86      var old = uRIReferenceDAO.findByShepardId(uriReferenceShepardId);
87      old.setDeleted(true);
88      old.setUpdatedAt(dateHelper.getDate());
89      old.setUpdatedBy(user);
90  
91      uRIReferenceDAO.createOrUpdate(old);
92      return true;
93    }
94  }