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