View Javadoc
1   package de.dlr.shepard.neo4Core.services;
2   
3   import de.dlr.shepard.exceptions.InvalidAuthException;
4   import de.dlr.shepard.exceptions.InvalidBodyException;
5   import de.dlr.shepard.exceptions.InvalidRequestException;
6   import de.dlr.shepard.mongoDB.StructuredDataPayload;
7   import de.dlr.shepard.mongoDB.StructuredDataService;
8   import de.dlr.shepard.neo4Core.dao.DataObjectDAO;
9   import de.dlr.shepard.neo4Core.dao.StructuredDataContainerDAO;
10  import de.dlr.shepard.neo4Core.dao.StructuredDataDAO;
11  import de.dlr.shepard.neo4Core.dao.StructuredDataReferenceDAO;
12  import de.dlr.shepard.neo4Core.dao.UserDAO;
13  import de.dlr.shepard.neo4Core.dao.VersionDAO;
14  import de.dlr.shepard.neo4Core.entities.StructuredDataReference;
15  import de.dlr.shepard.neo4Core.entities.Version;
16  import de.dlr.shepard.neo4Core.io.StructuredDataReferenceIO;
17  import de.dlr.shepard.security.PermissionsUtil;
18  import de.dlr.shepard.util.AccessType;
19  import de.dlr.shepard.util.DateHelper;
20  import io.quarkus.logging.Log;
21  import jakarta.enterprise.context.RequestScoped;
22  import jakarta.inject.Inject;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  @RequestScoped
27  public class StructuredDataReferenceService
28    implements IReferenceService<StructuredDataReference, StructuredDataReferenceIO> {
29  
30    private StructuredDataReferenceDAO structuredDataReferenceDAO;
31    private DataObjectDAO dataObjectDAO;
32    private StructuredDataContainerDAO containerDAO;
33    private StructuredDataDAO structuredDataDAO;
34    private UserDAO userDAO;
35    private VersionDAO versionDAO;
36    private DateHelper dateHelper;
37    private StructuredDataService structuredDataService;
38    private PermissionsUtil permissionsUtil;
39  
40    StructuredDataReferenceService() {}
41  
42    @Inject
43    public StructuredDataReferenceService(
44      StructuredDataReferenceDAO structuredDataReferenceDAO,
45      DataObjectDAO dataObjectDAO,
46      StructuredDataContainerDAO containerDAO,
47      StructuredDataDAO structuredDataDAO,
48      UserDAO userDAO,
49      VersionDAO versionDAO,
50      DateHelper dateHelper,
51      StructuredDataService structuredDataService,
52      PermissionsUtil permissionsUtil
53    ) {
54      this.structuredDataReferenceDAO = structuredDataReferenceDAO;
55      this.dataObjectDAO = dataObjectDAO;
56      this.containerDAO = containerDAO;
57      this.structuredDataDAO = structuredDataDAO;
58      this.userDAO = userDAO;
59      this.versionDAO = versionDAO;
60      this.dateHelper = dateHelper;
61      this.structuredDataService = structuredDataService;
62      this.permissionsUtil = permissionsUtil;
63    }
64  
65    @Override
66    public StructuredDataReference createReferenceByShepardId(
67      long dataObjectShepardId,
68      StructuredDataReferenceIO structuredDataReference,
69      String username
70    ) {
71      var user = userDAO.find(username);
72      var dataObject = dataObjectDAO.findLightByShepardId(dataObjectShepardId);
73      var container = containerDAO.findLightByNeo4jId(structuredDataReference.getStructuredDataContainerId());
74      if (container == null || container.isDeleted()) throw new InvalidBodyException("invalid container");
75      var toCreate = new StructuredDataReference();
76      toCreate.setCreatedAt(dateHelper.getDate());
77      toCreate.setCreatedBy(user);
78      toCreate.setDataObject(dataObject);
79      toCreate.setName(structuredDataReference.getName());
80      toCreate.setStructuredDataContainer(container);
81  
82      // Get existing structured data
83      for (var oid : structuredDataReference.getStructuredDataOids()) {
84        var structuredData = structuredDataDAO.find(container.getId(), oid);
85        if (structuredData != null) {
86          toCreate.addStructuredData(structuredData);
87        } else {
88          Log.warnf("Could not find structured data with oid: %s", oid);
89        }
90      }
91  
92      StructuredDataReference created = structuredDataReferenceDAO.createOrUpdate(toCreate);
93      created.setShepardId(created.getId());
94      created = structuredDataReferenceDAO.createOrUpdate(created);
95      Version version = versionDAO.findVersionLightByNeo4jId(dataObject.getId());
96      versionDAO.createLink(created.getId(), version.getUid());
97      return created;
98    }
99  
100   @Override
101   public List<StructuredDataReference> getAllReferencesByDataObjectShepardId(long dataObjectShepardId) {
102     var references = structuredDataReferenceDAO.findByDataObjectShepardId(dataObjectShepardId);
103     return references;
104   }
105 
106   /**
107    * Searches the neo4j database for a StructuredDataReference
108    *
109    * @param shepardId identifies the searched StructuredDataReference
110    *
111    * @return the StructuredDataReference with the given id or null
112    */
113   @Override
114   public StructuredDataReference getReferenceByShepardId(long shepardId) {
115     StructuredDataReference structuredDataReference = structuredDataReferenceDAO.findByShepardId(shepardId);
116     if (structuredDataReference == null || structuredDataReference.isDeleted()) {
117       Log.errorf("Structured Data Reference with id %s is null or deleted", shepardId);
118       return null;
119     }
120     return structuredDataReference;
121   }
122 
123   /**
124    * set the deleted flag for the Reference
125    *
126    * @param structuredDataReferenceShepardId identifies the StructuredDataReference to be deleted
127    * @param username the deleting user
128    * @return a boolean to identify if the StructuredDataReference was successfully removed
129    */
130   @Override
131   public boolean deleteReferenceByShepardId(long structuredDataReferenceShepardId, String username) {
132     StructuredDataReference structuredDataReference = structuredDataReferenceDAO.findByShepardId(
133       structuredDataReferenceShepardId
134     );
135     var user = userDAO.find(username);
136     structuredDataReference.setDeleted(true);
137     structuredDataReference.setUpdatedBy(user);
138     structuredDataReference.setUpdatedAt(dateHelper.getDate());
139     structuredDataReferenceDAO.createOrUpdate(structuredDataReference);
140     return true;
141   }
142 
143   /**
144    * Returns all structured data objects with payload. The payload attribute is null when the container is not accessible.
145    *
146    * @param structuredDataReferenceShepardId identifies the sd reference
147    * @param username the current user
148    * @return a list of StructuredDataPayload
149    */
150   public List<StructuredDataPayload> getAllPayloadsByShepardId(long structuredDataReferenceShepardId, String username) {
151     StructuredDataReference reference = structuredDataReferenceDAO.findByShepardId(structuredDataReferenceShepardId);
152 
153     // Return empty structured data objects when the container is not accessible
154     if (
155       reference.getStructuredDataContainer() == null ||
156       reference.getStructuredDataContainer().isDeleted() ||
157       !permissionsUtil.isAccessTypeAllowedForUser(
158         reference.getStructuredDataContainer().getId(),
159         AccessType.Read,
160         username
161       )
162     ) return reference.getStructuredDatas().stream().map(sd -> new StructuredDataPayload(sd, null)).toList();
163 
164     String mongoId = reference.getStructuredDataContainer().getMongoId();
165     var result = new ArrayList<StructuredDataPayload>(reference.getStructuredDatas().size());
166     for (var structuredData : reference.getStructuredDatas()) {
167       var payload = structuredDataService.getPayload(mongoId, structuredData.getOid());
168       if (payload != null) result.add(payload);
169       else result.add(new StructuredDataPayload(structuredData, null));
170     }
171     return result;
172   }
173 
174   /**
175    * Returns a specific StructuredDataPayload
176    *
177    * @param structuredDataReferenceShepardId identifies the sd reference
178    * @param oid identifies the structured data
179    * @param username the current user
180    * @return StructuredDataPayload
181    * @throws InvalidRequestException when container is not accessible
182    * @throws InvalidAuthException when the user is not authorized to access the container
183    */
184   public StructuredDataPayload getPayloadByShepardId(
185     long structuredDataReferenceShepardId,
186     String oid,
187     String username
188   ) {
189     StructuredDataReference reference = structuredDataReferenceDAO.findByShepardId(structuredDataReferenceShepardId);
190     if (
191       reference.getStructuredDataContainer() == null || reference.getStructuredDataContainer().isDeleted()
192     ) throw new InvalidRequestException("The structured data container in question is not accessible");
193 
194     long containerId = reference.getStructuredDataContainer().getId();
195     if (
196       !permissionsUtil.isAccessTypeAllowedForUser(containerId, AccessType.Read, username)
197     ) throw new InvalidAuthException("You are not authorized to access this structured data");
198 
199     String mongoId = reference.getStructuredDataContainer().getMongoId();
200     return structuredDataService.getPayload(mongoId, oid);
201   }
202 }