1 package de.dlr.shepard.neo4Core.services;
2
3 import java.util.List;
4
5 import de.dlr.shepard.mongoDB.StructuredData;
6 import de.dlr.shepard.mongoDB.StructuredDataPayload;
7 import de.dlr.shepard.mongoDB.StructuredDataService;
8 import de.dlr.shepard.neo4Core.dao.PermissionsDAO;
9 import de.dlr.shepard.neo4Core.dao.StructuredDataContainerDAO;
10 import de.dlr.shepard.neo4Core.dao.UserDAO;
11 import de.dlr.shepard.neo4Core.entities.Permissions;
12 import de.dlr.shepard.neo4Core.entities.StructuredDataContainer;
13 import de.dlr.shepard.neo4Core.io.StructuredDataContainerIO;
14 import de.dlr.shepard.util.DateHelper;
15 import de.dlr.shepard.util.PermissionType;
16 import de.dlr.shepard.util.QueryParamHelper;
17 import lombok.extern.slf4j.Slf4j;
18
19 @Slf4j
20 public class StructuredDataContainerService
21 implements IContainerService<StructuredDataContainer, StructuredDataContainerIO> {
22
23 private StructuredDataContainerDAO structuredDataContainerDAO = new StructuredDataContainerDAO();
24 private StructuredDataService structuredDataService = new StructuredDataService();
25 private PermissionsDAO permissionsDAO = new PermissionsDAO();
26 private UserDAO userDAO = new UserDAO();
27 private DateHelper dateHelper = new DateHelper();
28
29
30
31
32
33
34
35
36 @Override
37 public StructuredDataContainer createContainer(StructuredDataContainerIO structuredDataContainerIO,
38 String username) {
39 var user = userDAO.find(username);
40 String mongoid = structuredDataService.createStructuredDataContainer();
41 var toCreate = new StructuredDataContainer();
42 toCreate.setCreatedAt(dateHelper.getDate());
43 toCreate.setCreatedBy(user);
44 toCreate.setMongoId(mongoid);
45 toCreate.setName(structuredDataContainerIO.getName());
46
47 var created = structuredDataContainerDAO.createOrUpdate(toCreate);
48 permissionsDAO.createOrUpdate(new Permissions(created, user, PermissionType.Private));
49 return created;
50 }
51
52
53
54
55
56
57
58 @Override
59 public StructuredDataContainer getContainer(long id) {
60 StructuredDataContainer structuredDataContainer = structuredDataContainerDAO.find(id);
61 if (structuredDataContainer == null || structuredDataContainer.isDeleted()) {
62 log.error("Structured Data Container with id {} is null or deleted", id);
63 return null;
64 }
65 return structuredDataContainer;
66 }
67
68
69
70
71
72
73
74
75 @Override
76 public List<StructuredDataContainer> getAllContainers(QueryParamHelper params, String username) {
77 var containers = structuredDataContainerDAO.findAllStructuredDataContainers(params, username);
78 return containers;
79 }
80
81
82
83
84
85
86
87
88
89 @Override
90 public boolean deleteContainer(long structuredDataId, String username) {
91 var user = userDAO.find(username);
92 StructuredDataContainer structuredDataContainer = structuredDataContainerDAO.find(structuredDataId);
93 if (structuredDataContainer == null) {
94 return false;
95 }
96 String mongoid = structuredDataContainer.getMongoId();
97 structuredDataContainer.setDeleted(true);
98 structuredDataContainer.setUpdatedAt(dateHelper.getDate());
99 structuredDataContainer.setUpdatedBy(user);
100 structuredDataContainerDAO.createOrUpdate(structuredDataContainer);
101 return structuredDataService.deleteStructuredDataContainer(mongoid);
102 }
103
104
105
106
107
108
109
110
111 public StructuredData createStructuredData(long structuredDataContainerID, StructuredDataPayload payload) {
112 var structuredDataContainer = structuredDataContainerDAO.find(structuredDataContainerID);
113 if (structuredDataContainer == null || structuredDataContainer.isDeleted()) {
114 log.error("Structured Data Container with id {} is null or deleted", structuredDataContainerID);
115 return null;
116 }
117 var result = structuredDataService.createStructuredData(structuredDataContainer.getMongoId(), payload);
118 if (result == null) {
119 log.error("Failed to create structured data");
120 return null;
121 }
122 structuredDataContainer.addStructuredData(result);
123 structuredDataContainerDAO.createOrUpdate(structuredDataContainer);
124 return result;
125 }
126
127
128
129
130
131
132
133
134
135 public StructuredDataPayload getStructuredData(long structuredDataContainerID, String oid) {
136 var structuredDataContainer = structuredDataContainerDAO.findLight(structuredDataContainerID);
137 if (structuredDataContainer == null || structuredDataContainer.isDeleted()) {
138 log.error("Structured Data Container with id {} is null or deleted", structuredDataContainerID);
139 return null;
140 }
141 var result = structuredDataService.getPayload(structuredDataContainer.getMongoId(), oid);
142 return result;
143 }
144
145
146
147
148
149
150
151
152
153 public boolean deleteStructuredData(long structuredDataContainerID, String oid) {
154 var structuredDataContainer = structuredDataContainerDAO.find(structuredDataContainerID);
155 if (structuredDataContainer == null || structuredDataContainer.isDeleted())
156 return false;
157 var result = structuredDataService.deletePayload(structuredDataContainer.getMongoId(), oid);
158 if (result) {
159 var newStructuredDatas = structuredDataContainer.getStructuredDatas().stream()
160 .filter(f -> !f.getOid().equals(oid)).toList();
161 structuredDataContainer.setStructuredDatas(newStructuredDatas);
162 structuredDataContainerDAO.createOrUpdate(structuredDataContainer);
163 }
164 return result;
165 }
166
167 }