1 package de.dlr.shepard.neo4Core.dao;
2
3 import de.dlr.shepard.neo4Core.entities.BasicReference;
4 import de.dlr.shepard.util.CypherQueryHelper;
5 import de.dlr.shepard.util.QueryParamHelper;
6 import jakarta.enterprise.context.RequestScoped;
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.stream.StreamSupport;
13
14 @RequestScoped
15 public class BasicReferenceDAO extends VersionableEntityDAO<BasicReference> {
16
17 @Override
18 public Class<BasicReference> getEntityType() {
19 return BasicReference.class;
20 }
21
22
23
24
25
26
27
28
29 public List<BasicReference> findByDataObjectNeo4jId(long dataObjectId, QueryParamHelper params) {
30 String query;
31 Map<String, Object> paramsMap = new HashMap<>();
32 paramsMap.put("name", params.getName());
33 if (params.hasPagination()) {
34 paramsMap.put("offset", params.getPagination().getOffset());
35 paramsMap.put("size", params.getPagination().getSize());
36 }
37 query = String.format(
38 "MATCH (d:DataObject)-[hr:has_reference]->%s WHERE ID(d)=%d WITH r",
39 CypherQueryHelper.getObjectPart("r", "BasicReference", params.hasName()),
40 dataObjectId
41 );
42 if (params.hasOrderByAttribute()) {
43 query += " " + CypherQueryHelper.getOrderByPart("r", params.getOrderByAttribute(), params.getOrderDesc());
44 }
45 if (params.hasPagination()) {
46 query += " " + CypherQueryHelper.getPaginationPart();
47 }
48 query += " " + CypherQueryHelper.getReturnPart("r");
49 var result = new ArrayList<BasicReference>();
50 for (var ref : findByQuery(query, paramsMap)) {
51 if (matchDataObject(ref, dataObjectId) && matchName(ref, params.getName())) {
52 result.add(ref);
53 }
54 }
55 return result;
56 }
57
58
59
60
61
62
63
64
65 public List<BasicReference> findByDataObjectShepardId(long dataObjectShepardId, QueryParamHelper params) {
66 String query;
67 Map<String, Object> paramsMap = new HashMap<>();
68 paramsMap.put("name", params.getName());
69 if (params.hasPagination()) {
70 paramsMap.put("offset", params.getPagination().getOffset());
71 paramsMap.put("size", params.getPagination().getSize());
72 }
73 query = String.format(
74 "MATCH (d:DataObject)-[hr:has_reference]->%s WHERE d.shepardId=%d WITH br",
75 CypherQueryHelper.getObjectPart("br", "BasicReference", params.hasName()),
76 dataObjectShepardId
77 );
78 if (params.hasOrderByAttribute()) {
79 query += " " + CypherQueryHelper.getOrderByPart("br", params.getOrderByAttribute(), params.getOrderDesc());
80 }
81 if (params.hasPagination()) {
82 query += " " + CypherQueryHelper.getPaginationPart();
83 }
84 query += " " + CypherQueryHelper.getReturnPart("br");
85 var result = new ArrayList<BasicReference>();
86 for (var ref : findByQuery(query, paramsMap)) {
87 if (matchDataObjectByShepardId(ref, dataObjectShepardId) && matchName(ref, params.getName())) {
88 result.add(ref);
89 }
90 }
91 return result;
92 }
93
94 private boolean matchDataObject(BasicReference ref, long dataObjectId) {
95 return ref.getDataObject() != null && ref.getDataObject().getId().equals(dataObjectId);
96 }
97
98 private boolean matchDataObjectByShepardId(BasicReference ref, long dataObjectShepardId) {
99 return ref.getDataObject() != null && ref.getDataObject().getShepardId().equals(dataObjectShepardId);
100 }
101
102 private boolean matchName(BasicReference ref, String name) {
103 return name == null || ref.getName().equalsIgnoreCase(name);
104 }
105
106 public List<BasicReference> getBasicReferencesByQuery(String query) {
107 var queryResult = findByQuery(query, Collections.emptyMap());
108 List<BasicReference> ret = StreamSupport.stream(queryResult.spliterator(), false).toList();
109 return ret;
110 }
111 }