View Javadoc
1   package de.dlr.shepard.neo4Core.dao;
2   
3   import de.dlr.shepard.neo4Core.entities.BasicReference;
4   import de.dlr.shepard.neo4Core.entities.Collection;
5   import de.dlr.shepard.neo4Core.entities.DataObject;
6   import de.dlr.shepard.neo4Core.entities.FileContainer;
7   import de.dlr.shepard.neo4Core.entities.StructuredDataContainer;
8   import de.dlr.shepard.neo4Core.entities.TimeseriesContainer;
9   import de.dlr.shepard.neo4Core.entities.User;
10  import de.dlr.shepard.neo4j.NeoConnector;
11  import de.dlr.shepard.util.CypherQueryHelper;
12  import de.dlr.shepard.util.CypherQueryHelper.Neighborhood;
13  import jakarta.enterprise.context.RequestScoped;
14  import java.util.ArrayList;
15  import java.util.Collections;
16  import java.util.List;
17  import java.util.stream.StreamSupport;
18  import org.neo4j.ogm.session.Session;
19  
20  @RequestScoped
21  public class SearchDAO {
22  
23    protected Session session = null;
24  
25    public SearchDAO() {
26      session = NeoConnector.getInstance().getNeo4jSession();
27    }
28  
29    public List<Collection> findCollections(String selectionQuery, String collectionVariable) {
30      String query = selectionQuery + emitCollectionReturnPart(collectionVariable);
31      Iterable<Collection> collections = session.query(Collection.class, query, Collections.emptyMap());
32      var ret = StreamSupport.stream(collections.spliterator(), false).toList();
33      return ret;
34    }
35  
36    public List<DataObject> findDataObjects(String selectionQuery, String dataObjectVariable) {
37      String query = selectionQuery + emitDataObjectReturnPart(dataObjectVariable);
38      Iterable<DataObject> collections = session.query(DataObject.class, query, Collections.emptyMap());
39      var ret = StreamSupport.stream(collections.spliterator(), false).toList();
40      return ret;
41    }
42  
43    public List<BasicReference> findReferences(String selectionQuery, String referenceVariable) {
44      String query = selectionQuery + emitReferencesReturnPart(referenceVariable);
45      Iterable<BasicReference> collections = session.query(BasicReference.class, query, Collections.emptyMap());
46      var ret = StreamSupport.stream(collections.spliterator(), false).toList();
47      return ret;
48    }
49  
50    public List<FileContainer> findFileContainers(String selectionQuery, String containerVariable) {
51      String query = selectionQuery + emitContainerReturnPart(containerVariable);
52      Iterable<FileContainer> fileContainers = session.query(FileContainer.class, query, Collections.emptyMap());
53      List<FileContainer> ret = new ArrayList<>();
54      fileContainers.forEach(ret::add);
55      return ret;
56    }
57  
58    public List<StructuredDataContainer> findStructuredDataContainers(String selectionQuery, String containerVariable) {
59      String query = selectionQuery + emitContainerReturnPart(containerVariable);
60      Iterable<StructuredDataContainer> structuredDataContainers = session.query(
61        StructuredDataContainer.class,
62        query,
63        Collections.emptyMap()
64      );
65      List<StructuredDataContainer> ret = new ArrayList<>();
66      structuredDataContainers.forEach(ret::add);
67      return ret;
68    }
69  
70    public List<TimeseriesContainer> findTimeseriesContainers(String selectionQuery, String containerVariable) {
71      String query = selectionQuery + emitContainerReturnPart(containerVariable);
72      Iterable<TimeseriesContainer> timeseriesContainers = session.query(
73        TimeseriesContainer.class,
74        query,
75        Collections.emptyMap()
76      );
77      List<TimeseriesContainer> ret = new ArrayList<>();
78      timeseriesContainers.forEach(ret::add);
79      return ret;
80    }
81  
82    public List<User> findUsers(String selectionQuery, String userVariable) {
83      String query = selectionQuery + emitUserReturnPart(userVariable);
84      Iterable<User> users = session.query(User.class, query, Collections.emptyMap());
85      List<User> ret = new ArrayList<>();
86      users.forEach(ret::add);
87      return ret;
88    }
89  
90    private String emitContainerReturnPart(String containerVariable) {
91      return (
92        " WITH " + containerVariable + " " + CypherQueryHelper.getReturnPart(containerVariable, Neighborhood.ESSENTIAL)
93      );
94    }
95  
96    private String emitCollectionReturnPart(String collectionVariable) {
97      return String.format(
98        " WITH %s MATCH path=(%s)-[]->(u:User) RETURN %s, nodes(path), relationships(path)",
99        collectionVariable,
100       collectionVariable,
101       collectionVariable
102     );
103   }
104 
105   private String emitDataObjectReturnPart(String dataObjectVariable) {
106     return String.format(
107       " WITH %s MATCH path=(c:Collection)-[]->(%s)-[]->(u:User) RETURN %s, nodes(path), relationships(path)",
108       dataObjectVariable,
109       dataObjectVariable,
110       dataObjectVariable
111     );
112   }
113 
114   private String emitReferencesReturnPart(String referenceVariable) {
115     return String.format(
116       " WITH %s MATCH path=(c:Collection)-[]->(d:DataObject)-[]->(%s)-[]->(u:User) RETURN %s, nodes(path), relationships(path)",
117       referenceVariable,
118       referenceVariable,
119       referenceVariable
120     );
121   }
122 
123   private String emitUserReturnPart(String userVariable) {
124     return String.format(
125       " WITH %s MATCH path=(%s:User)<-[:belongs_to|subscribed_by*0..1]-(n) RETURN %s, nodes(path), relationships(path)",
126       userVariable,
127       userVariable,
128       userVariable
129     );
130   }
131 }