View Javadoc
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  		// Get existing structured data
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  	 * Searches the neo4j database for a StructuredDataReference
74  	 *
75  	 * @param shepardId identifies the searched StructuredDataReference
76  	 *
77  	 * @return the StructuredDataReference with the given id or null
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  	 * set the deleted flag for the Reference
91  	 *
92  	 * @param structuredDataReferenceShepardId identifies the
93  	 *                                         StructuredDataReference to be deleted
94  	 * @param username                         the deleting user
95  	 * @return a boolean to identify if the StructuredDataReference was successfully
96  	 *         removed
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 		// TODO: Handle missing container
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 }