1 package de.dlr.shepard.context.references.basicreference.services;
2
3 import de.dlr.shepard.auth.users.entities.User;
4 import de.dlr.shepard.auth.users.services.UserService;
5 import de.dlr.shepard.common.exceptions.InvalidAuthException;
6 import de.dlr.shepard.common.exceptions.InvalidPathException;
7 import de.dlr.shepard.common.util.DateHelper;
8 import de.dlr.shepard.common.util.QueryParamHelper;
9 import de.dlr.shepard.context.collection.services.CollectionService;
10 import de.dlr.shepard.context.collection.services.DataObjectService;
11 import de.dlr.shepard.context.references.basicreference.daos.BasicReferenceDAO;
12 import de.dlr.shepard.context.references.basicreference.entities.BasicReference;
13 import io.quarkus.logging.Log;
14 import jakarta.enterprise.context.RequestScoped;
15 import jakarta.inject.Inject;
16 import java.util.List;
17
18 @RequestScoped
19 public class BasicReferenceService {
20
21 @Inject
22 BasicReferenceDAO basicReferenceDAO;
23
24 @Inject
25 UserService userService;
26
27 @Inject
28 DataObjectService dataObjectService;
29
30 @Inject
31 CollectionService collectionService;
32
33 @Inject
34 DateHelper dateHelper;
35
36
37
38
39
40
41
42
43
44
45 public BasicReference getReference(long collectionShepardId, long dataObjectShepardId, long shepardId) {
46 dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId);
47
48 BasicReference basicReference = basicReferenceDAO.findByShepardId(shepardId);
49 if (basicReference == null || basicReference.isDeleted()) {
50 String errorMsg = String.format("ID ERROR - Basic Reference with id %s is null or deleted", shepardId);
51 Log.error(errorMsg);
52 throw new InvalidPathException(errorMsg);
53 }
54
55 if (
56 basicReference.getDataObject() != null &&
57 !basicReference.getDataObject().getShepardId().equals(dataObjectShepardId)
58 ) {
59 String errorMsg = "ID ERROR - There is no association between dataObject and reference";
60 Log.error(errorMsg);
61 throw new InvalidPathException(errorMsg);
62 }
63
64 return basicReference;
65 }
66
67
68
69
70
71
72
73
74
75
76
77 public List<BasicReference> getAllBasicReferences(
78 long collectionShepardId,
79 long dataObjectShepardId,
80 QueryParamHelper params
81 ) {
82 dataObjectService.getDataObject(collectionShepardId, dataObjectShepardId);
83
84 var references = basicReferenceDAO.findByDataObjectShepardId(dataObjectShepardId, params);
85 return references;
86 }
87
88
89
90
91
92
93
94 public void deleteReference(long collectionShepardId, long dataObjectShepardId, long basicReferenceShepardId) {
95 var basicReference = getReference(collectionShepardId, dataObjectShepardId, basicReferenceShepardId);
96 collectionService.assertIsAllowedToEditCollection(collectionShepardId);
97
98 User user = userService.getCurrentUser();
99
100 basicReference.setDeleted(true);
101 basicReference.setUpdatedAt(dateHelper.getDate());
102 basicReference.setUpdatedBy(user);
103
104 basicReferenceDAO.createOrUpdate(basicReference);
105 }
106 }