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