1 package de.dlr.shepard.neo4Core.dao;
2
3 import de.dlr.shepard.neo4Core.entities.Collection;
4 import de.dlr.shepard.neo4Core.entities.User;
5 import de.dlr.shepard.util.CypherQueryHelper;
6 import de.dlr.shepard.util.QueryParamHelper;
7 import jakarta.enterprise.context.RequestScoped;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.Date;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15 @RequestScoped
16 public class CollectionDAO extends VersionableEntityDAO<Collection> {
17
18 @Override
19 public Class<Collection> getEntityType() {
20 return Collection.class;
21 }
22
23
24
25
26
27
28
29
30 public List<Collection> findAllCollectionsByNeo4jId(QueryParamHelper params, String username) {
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 var query = String.format(
38 "MATCH %s WHERE %s WITH c",
39 CypherQueryHelper.getObjectPart("c", "Collection", params.hasName()),
40 CypherQueryHelper.getReadableByQuery("c", username)
41 );
42 if (params.hasOrderByAttribute()) {
43 query += " " + CypherQueryHelper.getOrderByPart("c", params.getOrderByAttribute(), params.getOrderDesc());
44 }
45 if (params.hasPagination()) {
46 query += " " + CypherQueryHelper.getPaginationPart();
47 }
48 query += " " + CypherQueryHelper.getReturnPart("c");
49 var result = new ArrayList<Collection>();
50 for (var col : findByQuery(query, paramsMap)) {
51 if (matchName(col, params.getName())) {
52 result.add(col);
53 }
54 }
55 return result;
56 }
57
58
59
60
61
62
63
64
65 public List<Collection> findAllCollectionsByShepardId(QueryParamHelper params, String username) {
66 String versionVariable = "v";
67 String collectionVariable = "c";
68 Map<String, Object> paramsMap = new HashMap<>();
69 paramsMap.put("name", params.getName());
70 if (params.hasPagination()) {
71 paramsMap.put("offset", params.getPagination().getOffset());
72 paramsMap.put("size", params.getPagination().getSize());
73 }
74 String query = String.format(
75 "MATCH %s WHERE %s AND %s WITH %s",
76 CypherQueryHelper.getObjectPartWithVersion(collectionVariable, "Collection", params.hasName(), versionVariable),
77 CypherQueryHelper.getReadableByQuery(collectionVariable, username),
78 CypherQueryHelper.getVersionHeadPart(versionVariable),
79 collectionVariable
80 );
81 if (params.hasOrderByAttribute()) {
82 query +=
83 " " + CypherQueryHelper.getOrderByPart(collectionVariable, params.getOrderByAttribute(), params.getOrderDesc());
84 }
85 if (params.hasPagination()) {
86 query += " " + CypherQueryHelper.getPaginationPart();
87 }
88 query += " " + CypherQueryHelper.getReturnPart(collectionVariable);
89 ArrayList<Collection> result = new ArrayList<Collection>();
90 for (Collection col : findByQuery(query, paramsMap)) {
91 if (matchName(col, params.getName())) {
92 result.add(col);
93 }
94 }
95 return result;
96 }
97
98
99
100
101
102
103
104
105
106 public boolean deleteCollectionByShepardId(long shepardId, User updatedBy, Date updatedAt) {
107 Collection collection = findByShepardId(shepardId);
108 collection.setUpdatedBy(updatedBy);
109 collection.setUpdatedAt(updatedAt);
110 collection.setDeleted(true);
111 createOrUpdate(collection);
112 String query = String.format(
113 """
114 MATCH (c:Collection {shepardId:%d}) OPTIONAL MATCH (c)-[:has_dataobject]->(d:DataObject) \
115 OPTIONAL MATCH (d)-[:has_reference]->(r:BasicReference) \
116 FOREACH (n in [c,d,r] | SET n.deleted = true)""",
117 shepardId
118 );
119 boolean result = runQuery(query, Collections.emptyMap());
120 return result;
121 }
122
123 private boolean matchName(Collection col, String name) {
124 return name == null || col.getName().equalsIgnoreCase(name);
125 }
126 }