View Javadoc
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     * Searches the database for collections.
26     *
27     * @param params   encapsulates possible parameters
28     * @param username the name of the user
29     * @return a list of collections
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 = String.format(
39        "MATCH %s WHERE %s WITH c",
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     * Searches the database for collections.
61     *
62     * @param params   encapsulates possible parameters
63     * @param username the name of the user
64     * @return a list of collections
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 = String.format(
76        "MATCH %s WHERE %s AND %s WITH %s",
77        CypherQueryHelper.getObjectPartWithVersion(collectionVariable, "Collection", params.hasName(), versionVariable),
78        CypherQueryHelper.getReadableByQuery(collectionVariable, username),
79        CypherQueryHelper.getVersionHeadPart(versionVariable),
80        collectionVariable
81      );
82      if (params.hasOrderByAttribute()) {
83        query +=
84          " " + CypherQueryHelper.getOrderByPart(collectionVariable, params.getOrderByAttribute(), params.getOrderDesc());
85      }
86      if (params.hasPagination()) {
87        query += " " + CypherQueryHelper.getPaginationPart();
88      }
89      query += " " + CypherQueryHelper.getReturnPart(collectionVariable);
90      ArrayList<Collection> result = new ArrayList<Collection>();
91      for (Collection col : findByQuery(query, paramsMap)) {
92        if (matchName(col, params.getName())) {
93          result.add(col);
94        }
95      }
96      return result;
97    }
98  
99    /**
100    * Delete collection and all related dataObjects and references
101    *
102    * @param shepardId identifies the collection
103    * @param updatedBy current date
104    * @param updatedAt current user
105    * @return whether the deletion was successful or not
106    */
107   public boolean deleteCollectionByShepardId(long shepardId, User updatedBy, Date updatedAt) {
108     Collection collection = findByShepardId(shepardId);
109     collection.setUpdatedBy(updatedBy);
110     collection.setUpdatedAt(updatedAt);
111     collection.setDeleted(true);
112     createOrUpdate(collection);
113     String query = String.format(
114       """
115       MATCH (c:Collection {shepardId:%d}) OPTIONAL MATCH (c)-[:has_dataobject]->(d:DataObject) \
116       OPTIONAL MATCH (d)-[:has_reference]->(r:BasicReference) \
117       FOREACH (n in [c,d,r] | SET n.deleted = true)""",
118       shepardId
119     );
120     boolean result = runQuery(query, Collections.emptyMap());
121     return result;
122   }
123 
124   private boolean matchName(Collection col, String name) {
125     return name == null || col.getName().equalsIgnoreCase(name);
126   }
127 }