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        String.format(
25          "MATCH (d:DataObject)-[hr:has_reference]->%s WHERE ID(d)=%d ",
26          CypherQueryHelper.getObjectPart("r", "StructuredDataReference", false),
27          dataObjectId
28        ) +
29        CypherQueryHelper.getReturnPart("r");
30      var queryResult = findByQuery(query, Collections.emptyMap());
31      List<StructuredDataReference> result = StreamSupport.stream(queryResult.spliterator(), false)
32        .filter(r -> r.getDataObject() != null)
33        .filter(r -> r.getDataObject().getId().equals(dataObjectId))
34        .toList();
35      return result;
36    }
37  
38    public List<StructuredDataReference> findReachableReferencesByShepardId(
39      TraversalRules traversalRule,
40      long collectionShepardId,
41      long startShepardId,
42      String userName
43    ) {
44      String query = getSearchForReachableReferencesByShepardIdQuery(
45        traversalRule,
46        collectionShepardId,
47        startShepardId,
48        userName
49      );
50      var queryResult = findByQuery(query, Collections.emptyMap());
51      List<StructuredDataReference> ret = StreamSupport.stream(queryResult.spliterator(), false).toList();
52      return ret;
53    }
54  
55    public List<StructuredDataReference> findReachableReferencesByNeo4jId(
56      TraversalRules traversalRule,
57      long collectionShepardId,
58      long startShepardId,
59      String userName
60    ) {
61      String query = getSearchForReachableReferencesByNeo4jIdQuery(
62        traversalRule,
63        collectionShepardId,
64        startShepardId,
65        userName
66      );
67      var queryResult = findByQuery(query, Collections.emptyMap());
68      List<StructuredDataReference> ret = StreamSupport.stream(queryResult.spliterator(), false).toList();
69      return ret;
70    }
71  
72    public List<StructuredDataReference> findReachableReferencesByNeo4jId(
73      long collectionId,
74      long startId,
75      String userName
76    ) {
77      String query = getSearchForReachableReferencesQuery(collectionId, startId, userName);
78      var queryResult = findByQuery(query, Collections.emptyMap());
79      List<StructuredDataReference> ret = StreamSupport.stream(queryResult.spliterator(), false).toList();
80      return ret;
81    }
82  
83    public List<StructuredDataReference> findReachableReferencesByShepardId(
84      long collectionShepardId,
85      long startShepardId,
86      String userName
87    ) {
88      String query = getSearchForReachableReferencesByShepardIdQuery(collectionShepardId, startShepardId, userName);
89      var queryResult = findByQuery(query, Collections.emptyMap());
90      List<StructuredDataReference> ret = StreamSupport.stream(queryResult.spliterator(), false).toList();
91      return ret;
92    }
93  
94    public List<StructuredDataReference> findReachableReferencesByNeo4jId(long collectionId, String userName) {
95      String query = getSearchForReachableReferencesQuery(collectionId, userName);
96      var queryResult = findByQuery(query, Collections.emptyMap());
97      List<StructuredDataReference> ret = StreamSupport.stream(queryResult.spliterator(), false).toList();
98      return ret;
99    }
100 
101   public List<StructuredDataReference> findReachableReferencesByShepardId(long collectionShepardId, String userName) {
102     String query = getSearchForReachableReferencesByShepardIdQuery(collectionShepardId, userName);
103     var queryResult = findByQuery(query, Collections.emptyMap());
104     List<StructuredDataReference> ret = StreamSupport.stream(queryResult.spliterator(), false).toList();
105     return ret;
106   }
107 
108   public List<StructuredDataReference> findByDataObjectShepardId(long dataObjectShepardId) {
109     String query =
110       String.format(
111         "MATCH (d:DataObject)-[hr:has_reference]->%s WHERE d." + Constants.SHEPARD_ID + "=%d ",
112         CypherQueryHelper.getObjectPart("r", "StructuredDataReference", false),
113         dataObjectShepardId
114       ) +
115       CypherQueryHelper.getReturnPart("r");
116     var queryResult = findByQuery(query, Collections.emptyMap());
117     List<StructuredDataReference> result = StreamSupport.stream(queryResult.spliterator(), false)
118       .filter(r -> r.getDataObject() != null)
119       .filter(r -> r.getDataObject().getShepardId().equals(dataObjectShepardId))
120       .toList();
121     return result;
122   }
123 
124   @Override
125   public Class<StructuredDataReference> getEntityType() {
126     return StructuredDataReference.class;
127   }
128 }