View Javadoc
1   package de.dlr.shepard.neo4Core.services;
2   
3   import java.util.List;
4   
5   import de.dlr.shepard.neo4Core.dao.CollectionDAO;
6   import de.dlr.shepard.neo4Core.dao.PermissionsDAO;
7   import de.dlr.shepard.neo4Core.dao.UserDAO;
8   import de.dlr.shepard.neo4Core.entities.Collection;
9   import de.dlr.shepard.neo4Core.entities.Permissions;
10  import de.dlr.shepard.neo4Core.entities.User;
11  import de.dlr.shepard.neo4Core.io.CollectionIO;
12  import de.dlr.shepard.util.DateHelper;
13  import de.dlr.shepard.util.PermissionType;
14  import de.dlr.shepard.util.QueryParamHelper;
15  import lombok.extern.slf4j.Slf4j;
16  
17  @Slf4j
18  public class CollectionService {
19  
20  	private CollectionDAO collectionDAO = new CollectionDAO();
21  	private UserDAO userDAO = new UserDAO();
22  	private PermissionsDAO permissionsDAO = new PermissionsDAO();
23  	private DateHelper dateHelper = new DateHelper();
24  
25  	/**
26  	 * Creates a Collection and stores it in Neo4J
27  	 *
28  	 * @param collection to be stored
29  	 * @param username   of the related user
30  	 * @return the created collection
31  	 */
32  	public Collection createCollection(CollectionIO collection, String username) {
33  		User user = userDAO.find(username);
34  		Collection toCreate = new Collection();
35  		toCreate.setAttributes(collection.getAttributes());
36  		toCreate.setCreatedBy(user);
37  		toCreate.setCreatedAt(dateHelper.getDate());
38  		toCreate.setDescription(collection.getDescription());
39  		toCreate.setName(collection.getName());
40  		Collection created = collectionDAO.createOrUpdate(toCreate);
41  		created.setShepardId(created.getId());
42  		created = collectionDAO.createOrUpdate(created);
43  		permissionsDAO.createOrUpdate(new Permissions(created, user, PermissionType.Private));
44  		return created;
45  	}
46  
47  	/**
48  	 * Searches the database for all Collections
49  	 *
50  	 * @param params   encapsulates possible parameters
51  	 * @param username the name of the user
52  	 * @return a list of Collections
53  	 */
54  	public List<Collection> getAllCollectionsByShepardId(QueryParamHelper params, String username) {
55  		List<Collection> queryResult = collectionDAO.findAllCollectionsByShepardId(params, username);
56  		List<Collection> collections = queryResult.stream().map(this::cutDeleted).toList();
57  		return collections;
58  	}
59  
60  	public Collection getCollectionByShepardId(long shepardId) {
61  		Collection collection = collectionDAO.findByShepardId(shepardId);
62  		if (collection == null || collection.isDeleted()) {
63  			log.error("Collection with id {} is null or deleted", shepardId);
64  			return null;
65  		}
66  		cutDeleted(collection);
67  		return collection;
68  	}
69  
70  	/**
71  	 * Updates a Collection with new Attributes.
72  	 *
73  	 * @param shepardId  identifies the Collection
74  	 * @param collection which contains the new Attributes
75  	 * @param username   of the related user
76  	 * @return updated Collection
77  	 */
78  	public Collection updateCollectionByShepardId(long shepardId, CollectionIO collection, String username) {
79  		Collection old = collectionDAO.findByShepardId(shepardId);
80  		old.setUpdatedBy(userDAO.find(username));
81  		old.setUpdatedAt(dateHelper.getDate());
82  		old.setAttributes(collection.getAttributes());
83  		old.setDescription(collection.getDescription());
84  		old.setName(collection.getName());
85  		Collection updated = collectionDAO.createOrUpdate(old);
86  		cutDeleted(updated);
87  		return updated;
88  	}
89  
90  	/**
91  	 * Deletes a Collection in Neo4j
92  	 *
93  	 * @param shepardId identifies the Collection
94  	 * @param username  of the related user
95  	 * @return a boolean to determine if Collection was successfully deleted
96  	 */
97  	public boolean deleteCollectionByShepardId(long shepardId, String username) {
98  		var date = dateHelper.getDate();
99  		var user = userDAO.find(username);
100 		var result = collectionDAO.deleteCollectionByShepardId(shepardId, user, date);
101 		return result;
102 	}
103 
104 	private Collection cutDeleted(Collection collection) {
105 		var dataObjects = collection.getDataObjects().stream().filter(d -> !d.isDeleted()).toList();
106 		collection.setDataObjects(dataObjects);
107 		return collection;
108 	}
109 }