1 package de.dlr.shepard.neo4Core.services;
2
3 import de.dlr.shepard.neo4Core.dao.CollectionDAO;
4 import de.dlr.shepard.neo4Core.dao.PermissionsDAO;
5 import de.dlr.shepard.neo4Core.dao.UserDAO;
6 import de.dlr.shepard.neo4Core.dao.VersionDAO;
7 import de.dlr.shepard.neo4Core.entities.Collection;
8 import de.dlr.shepard.neo4Core.entities.Permissions;
9 import de.dlr.shepard.neo4Core.entities.Version;
10 import de.dlr.shepard.neo4Core.io.CollectionIO;
11 import de.dlr.shepard.util.Constants;
12 import de.dlr.shepard.util.DateHelper;
13 import de.dlr.shepard.util.PermissionType;
14 import de.dlr.shepard.util.QueryParamHelper;
15 import io.quarkus.logging.Log;
16 import jakarta.enterprise.context.RequestScoped;
17 import jakarta.inject.Inject;
18 import java.util.Date;
19 import java.util.List;
20 import java.util.UUID;
21
22 @RequestScoped
23 public class CollectionService {
24
25 private CollectionDAO collectionDAO;
26 private UserDAO userDAO;
27 private PermissionsDAO permissionsDAO;
28 private DateHelper dateHelper;
29 private VersionDAO versionDAO;
30
31 CollectionService() {}
32
33 @Inject
34 public CollectionService(
35 CollectionDAO collectionDAO,
36 UserDAO userDAO,
37 PermissionsDAO permissionsDAO,
38 DateHelper dateHelper,
39 VersionDAO versionDAO
40 ) {
41 this.collectionDAO = collectionDAO;
42 this.userDAO = userDAO;
43 this.permissionsDAO = permissionsDAO;
44 this.dateHelper = dateHelper;
45 this.versionDAO = versionDAO;
46 }
47
48
49
50
51
52
53
54
55 public Collection createCollection(CollectionIO collection, String username) {
56 Date date = dateHelper.getDate();
57 var user = userDAO.find(username);
58 var toCreate = new Collection();
59 toCreate.setAttributes(collection.getAttributes());
60 toCreate.setCreatedBy(user);
61 toCreate.setCreatedAt(date);
62 toCreate.setDescription(collection.getDescription());
63 toCreate.setName(collection.getName());
64 var createdCollection = collectionDAO.createOrUpdate(toCreate);
65
66 Version nullVersion = new Version(Constants.HEAD, Constants.HEAD_VERSION, date, user);
67 Version savedNullVersion = versionDAO.createOrUpdate(nullVersion);
68
69 long collectionId = createdCollection.getId();
70 createdCollection.setShepardId(collectionId);
71 createdCollection.setVersion(savedNullVersion);
72 var updated = collectionDAO.createOrUpdate(createdCollection);
73 permissionsDAO.createOrUpdate(new Permissions(updated, user, PermissionType.Private));
74
75 return updated;
76 }
77
78
79
80
81
82
83
84
85 public List<Collection> getAllCollectionsByShepardId(QueryParamHelper params, String username) {
86 List<Collection> queryResult = collectionDAO.findAllCollectionsByShepardId(params, username);
87 List<Collection> collections = queryResult.stream().map(this::cutDeleted).toList();
88 return collections;
89 }
90
91 public Collection getCollectionByShepardId(long shepardId, UUID versionUID) {
92 Collection ret;
93 String errorMsg;
94 if (versionUID == null) {
95 ret = collectionDAO.findByShepardId(shepardId);
96 errorMsg = String.format("Collection with id %s is null or deleted", shepardId);
97 } else {
98 ret = collectionDAO.findByShepardId(shepardId, versionUID);
99 errorMsg = String.format("Collection with id %s and versionUID %s is null or deleted", shepardId, versionUID);
100 }
101 if (ret == null || ret.isDeleted()) {
102 Log.error(errorMsg);
103 return null;
104 }
105 cutDeleted(ret);
106 return ret;
107 }
108
109 public Collection getCollectionByShepardId(long shepardId) {
110 return getCollectionByShepardId(shepardId, null);
111 }
112
113
114
115
116
117
118
119
120
121
122 public Collection updateCollectionByShepardId(long shepardId, CollectionIO collection, String username) {
123 Collection old = collectionDAO.findByShepardId(shepardId);
124 old.setUpdatedBy(userDAO.find(username));
125 old.setUpdatedAt(dateHelper.getDate());
126 old.setAttributes(collection.getAttributes());
127 old.setDescription(collection.getDescription());
128 old.setName(collection.getName());
129 Collection updated = collectionDAO.createOrUpdate(old);
130 cutDeleted(updated);
131 return updated;
132 }
133
134
135
136
137
138
139
140
141 public boolean deleteCollectionByShepardId(long shepardId, String username) {
142 var date = dateHelper.getDate();
143 var user = userDAO.find(username);
144 var result = collectionDAO.deleteCollectionByShepardId(shepardId, user, date);
145 return result;
146 }
147
148 private Collection cutDeleted(Collection collection) {
149 var dataObjects = collection.getDataObjects().stream().filter(d -> !d.isDeleted()).toList();
150 collection.setDataObjects(dataObjects);
151 return collection;
152 }
153 }