1 package de.dlr.shepard.neo4Core.services;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import de.dlr.shepard.exceptions.InvalidAuthException;
7 import de.dlr.shepard.exceptions.InvalidBodyException;
8 import de.dlr.shepard.mongoDB.StructuredDataPayload;
9 import de.dlr.shepard.mongoDB.StructuredDataService;
10 import de.dlr.shepard.neo4Core.dao.DataObjectDAO;
11 import de.dlr.shepard.neo4Core.dao.StructuredDataContainerDAO;
12 import de.dlr.shepard.neo4Core.dao.StructuredDataDAO;
13 import de.dlr.shepard.neo4Core.dao.StructuredDataReferenceDAO;
14 import de.dlr.shepard.neo4Core.dao.UserDAO;
15 import de.dlr.shepard.neo4Core.entities.StructuredDataReference;
16 import de.dlr.shepard.neo4Core.io.StructuredDataReferenceIO;
17 import de.dlr.shepard.security.PermissionsUtil;
18 import de.dlr.shepard.util.AccessType;
19 import de.dlr.shepard.util.DateHelper;
20 import lombok.extern.slf4j.Slf4j;
21
22 @Slf4j
23 public class StructuredDataReferenceService
24 implements IReferenceService<StructuredDataReference, StructuredDataReferenceIO> {
25
26 private StructuredDataReferenceDAO structuredDataReferenceDAO = new StructuredDataReferenceDAO();
27 private DataObjectDAO dataObjectDAO = new DataObjectDAO();
28 private StructuredDataContainerDAO containerDAO = new StructuredDataContainerDAO();
29 private StructuredDataDAO structuredDataDAO = new StructuredDataDAO();
30 private UserDAO userDAO = new UserDAO();
31 private DateHelper dateHelper = new DateHelper();
32 private StructuredDataService structuredDataService = new StructuredDataService();
33 private PermissionsUtil permissionsUtil = new PermissionsUtil();
34
35 @Override
36 public StructuredDataReference createReferenceByShepardId(long dataObjectShepardId,
37 StructuredDataReferenceIO structuredDataReference, String username) {
38 var user = userDAO.find(username);
39 var dataObject = dataObjectDAO.findLightByShepardId(dataObjectShepardId);
40 var container = containerDAO.findLightByNeo4jId(structuredDataReference.getStructuredDataContainerId());
41 if (container == null || container.isDeleted())
42 throw new InvalidBodyException("invalid container");
43 var toCreate = new StructuredDataReference();
44 toCreate.setCreatedAt(dateHelper.getDate());
45 toCreate.setCreatedBy(user);
46 toCreate.setDataObject(dataObject);
47 toCreate.setName(structuredDataReference.getName());
48 toCreate.setStructuredDataContainer(container);
49
50
51 for (var oid : structuredDataReference.getStructuredDataOids()) {
52 var structuredData = structuredDataDAO.find(container.getId(), oid);
53 if (structuredData != null) {
54 toCreate.addStructuredData(structuredData);
55 } else {
56 log.warn("Could not find structured data with oid: {}", oid);
57 }
58 }
59
60 StructuredDataReference created = structuredDataReferenceDAO.createOrUpdate(toCreate);
61 created.setShepardId(created.getId());
62 created = structuredDataReferenceDAO.createOrUpdate(created);
63 return created;
64 }
65
66 @Override
67 public List<StructuredDataReference> getAllReferencesByDataObjectShepardId(long dataObjectShepardId) {
68 var references = structuredDataReferenceDAO.findByDataObjectShepardId(dataObjectShepardId);
69 return references;
70 }
71
72
73
74
75
76
77
78
79 @Override
80 public StructuredDataReference getReferenceByShepardId(long shepardId) {
81 StructuredDataReference structuredDataReference = structuredDataReferenceDAO.findByShepardId(shepardId);
82 if (structuredDataReference == null || structuredDataReference.isDeleted()) {
83 log.error("Structured Data Reference with id {} is null or deleted", shepardId);
84 return null;
85 }
86 return structuredDataReference;
87 }
88
89
90
91
92
93
94
95
96
97
98 @Override
99 public boolean deleteReferenceByShepardId(long structuredDataReferenceShepardId, String username) {
100 StructuredDataReference structuredDataReference = structuredDataReferenceDAO
101 .findByShepardId(structuredDataReferenceShepardId);
102 var user = userDAO.find(username);
103 structuredDataReference.setDeleted(true);
104 structuredDataReference.setUpdatedBy(user);
105 structuredDataReference.setUpdatedAt(dateHelper.getDate());
106 structuredDataReferenceDAO.createOrUpdate(structuredDataReference);
107 return true;
108 }
109
110 public List<StructuredDataPayload> getAllPayloadsByShepardId(long structuredDataReferenceShepardId,
111 String username) {
112 StructuredDataReference reference = structuredDataReferenceDAO
113 .findByShepardId(structuredDataReferenceShepardId);
114 if (reference.getStructuredDataContainer() == null || reference.getStructuredDataContainer().isDeleted())
115 return reference.getStructuredDatas().stream().map(sd -> new StructuredDataPayload(sd, null)).toList();
116
117 long containerId = reference.getStructuredDataContainer().getId();
118 String mongoId = reference.getStructuredDataContainer().getMongoId();
119 if (!permissionsUtil.isAllowed(containerId, AccessType.Read, username))
120 return reference.getStructuredDatas().stream().map(sd -> new StructuredDataPayload(sd, null)).toList();
121
122 var result = new ArrayList<StructuredDataPayload>(reference.getStructuredDatas().size());
123 for (var structuredData : reference.getStructuredDatas()) {
124 var payload = structuredDataService.getPayload(mongoId, structuredData.getOid());
125 if (payload != null)
126 result.add(payload);
127 else
128 result.add(new StructuredDataPayload(structuredData, null));
129 }
130 return result;
131 }
132
133 public StructuredDataPayload getPayloadByShepardId(long structuredDataReferenceShepardId, String oid,
134 String username) {
135 StructuredDataReference reference = structuredDataReferenceDAO
136 .findByShepardId(structuredDataReferenceShepardId);
137
138 long containerId = reference.getStructuredDataContainer().getId();
139 String mongoId = reference.getStructuredDataContainer().getMongoId();
140 if (!permissionsUtil.isAllowed(containerId, AccessType.Read, username))
141 throw new InvalidAuthException("You are not authorized to access this structured data");
142 return structuredDataService.getPayload(mongoId, oid);
143 }
144 }