View Javadoc
1   package de.dlr.shepard.context.references.structureddata.daos;
2   
3   import de.dlr.shepard.common.util.Constants;
4   import de.dlr.shepard.common.util.CypherQueryHelper;
5   import de.dlr.shepard.common.util.TraversalRules;
6   import de.dlr.shepard.context.references.structureddata.entities.StructuredDataReference;
7   import de.dlr.shepard.context.version.daos.VersionableEntityDAO;
8   import jakarta.enterprise.context.RequestScoped;
9   import java.util.Collections;
10  import java.util.List;
11  import java.util.stream.StreamSupport;
12  
13  @RequestScoped
14  public class StructuredDataReferenceDAO extends VersionableEntityDAO<StructuredDataReference> {
15  
16    /**
17     * Searches the database for references.
18     *
19     * @param dataObjectId identifies the dataObject
20     * @return a List of references
21     */
22    public List<StructuredDataReference> findByDataObjectNeo4jId(long dataObjectId) {
23      String query =
24        "MATCH (d:DataObject)-[hr:has_reference]->%s WHERE ID(d)=%d ".formatted(
25            CypherQueryHelper.getObjectPart("r", "StructuredDataReference", false),
26            dataObjectId
27          ) +
28        CypherQueryHelper.getReturnPart("r");
29      var queryResult = findByQuery(query, Collections.emptyMap());
30      List<StructuredDataReference> result = StreamSupport.stream(queryResult.spliterator(), false)
31        .filter(r -> r.getDataObject() != null)
32        .filter(r -> r.getDataObject().getId().equals(dataObjectId))
33        .toList();
34      return result;
35    }
36  
37    public List<StructuredDataReference> findReachableReferencesByShepardId(
38      TraversalRules traversalRule,
39      long collectionShepardId,
40      long startShepardId,
41      String userName
42    ) {
43      String query = getSearchForReachableReferencesByShepardIdQuery(
44        traversalRule,
45        collectionShepardId,
46        startShepardId,
47        userName
48      );
49      var queryResult = findByQuery(query, Collections.emptyMap());
50      List<StructuredDataReference> ret = StreamSupport.stream(queryResult.spliterator(), false).toList();
51      return ret;
52    }
53  
54    public List<StructuredDataReference> findReachableReferencesByNeo4jId(
55      TraversalRules traversalRule,
56      long collectionShepardId,
57      long startShepardId,
58      String userName
59    ) {
60      String query = getSearchForReachableReferencesByNeo4jIdQuery(
61        traversalRule,
62        collectionShepardId,
63        startShepardId,
64        userName
65      );
66      var queryResult = findByQuery(query, Collections.emptyMap());
67      List<StructuredDataReference> ret = StreamSupport.stream(queryResult.spliterator(), false).toList();
68      return ret;
69    }
70  
71    public List<StructuredDataReference> findReachableReferencesByNeo4jId(
72      long collectionId,
73      long startId,
74      String userName
75    ) {
76      String query = getSearchForReachableReferencesQuery(collectionId, startId, userName);
77      var queryResult = findByQuery(query, Collections.emptyMap());
78      List<StructuredDataReference> ret = StreamSupport.stream(queryResult.spliterator(), false).toList();
79      return ret;
80    }
81  
82    public List<StructuredDataReference> findReachableReferencesByShepardId(
83      long collectionShepardId,
84      long startShepardId,
85      String userName
86    ) {
87      String query = getSearchForReachableReferencesByShepardIdQuery(collectionShepardId, startShepardId, userName);
88      var queryResult = findByQuery(query, Collections.emptyMap());
89      List<StructuredDataReference> ret = StreamSupport.stream(queryResult.spliterator(), false).toList();
90      return ret;
91    }
92  
93    public List<StructuredDataReference> findReachableReferencesByNeo4jId(long collectionId, String userName) {
94      String query = getSearchForReachableReferencesQuery(collectionId, userName);
95      var queryResult = findByQuery(query, Collections.emptyMap());
96      List<StructuredDataReference> ret = StreamSupport.stream(queryResult.spliterator(), false).toList();
97      return ret;
98    }
99  
100   public List<StructuredDataReference> findReachableReferencesByShepardId(long collectionShepardId, String userName) {
101     String query = getSearchForReachableReferencesByShepardIdQuery(collectionShepardId, userName);
102     var queryResult = findByQuery(query, Collections.emptyMap());
103     List<StructuredDataReference> ret = StreamSupport.stream(queryResult.spliterator(), false).toList();
104     return ret;
105   }
106 
107   public List<StructuredDataReference> findByDataObjectShepardId(long dataObjectShepardId) {
108     String query =
109       String.format(
110         "MATCH (d:DataObject)-[hr:has_reference]->%s WHERE d." + Constants.SHEPARD_ID + "=%d ",
111         CypherQueryHelper.getObjectPart("r", "StructuredDataReference", false),
112         dataObjectShepardId
113       ) +
114       CypherQueryHelper.getReturnPart("r");
115     var queryResult = findByQuery(query, Collections.emptyMap());
116     List<StructuredDataReference> result = StreamSupport.stream(queryResult.spliterator(), false)
117       .filter(r -> r.getDataObject() != null)
118       .filter(r -> r.getDataObject().getShepardId().equals(dataObjectShepardId))
119       .toList();
120     return result;
121   }
122 
123   @Override
124   public Class<StructuredDataReference> getEntityType() {
125     return StructuredDataReference.class;
126   }
127 }