1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.logic;
20
21 import java.lang.reflect.Method;
22 import java.util.List;
23 import java.util.stream.Collectors;
24 import org.apache.commons.lang3.ArrayUtils;
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.syncope.common.lib.to.RelationshipTypeTO;
27 import org.apache.syncope.common.lib.types.IdRepoEntitlement;
28 import org.apache.syncope.core.persistence.api.dao.NotFoundException;
29 import org.apache.syncope.core.persistence.api.dao.RelationshipTypeDAO;
30 import org.apache.syncope.core.persistence.api.entity.RelationshipType;
31 import org.apache.syncope.core.provisioning.api.data.RelationshipTypeDataBinder;
32 import org.springframework.security.access.prepost.PreAuthorize;
33 import org.springframework.transaction.annotation.Transactional;
34
35 public class RelationshipTypeLogic extends AbstractTransactionalLogic<RelationshipTypeTO> {
36
37 protected final RelationshipTypeDataBinder binder;
38
39 protected final RelationshipTypeDAO relationshipTypeDAO;
40
41 public RelationshipTypeLogic(
42 final RelationshipTypeDataBinder binder,
43 final RelationshipTypeDAO relationshipTypeDAO) {
44
45 this.binder = binder;
46 this.relationshipTypeDAO = relationshipTypeDAO;
47 }
48
49 @PreAuthorize("hasRole('" + IdRepoEntitlement.RELATIONSHIPTYPE_READ + "')")
50 @Transactional(readOnly = true)
51 public RelationshipTypeTO read(final String key) {
52 RelationshipType relationshipType = relationshipTypeDAO.find(key);
53 if (relationshipType == null) {
54 LOG.error("Could not find relationshipType '" + key + '\'');
55
56 throw new NotFoundException(key);
57 }
58
59 return binder.getRelationshipTypeTO(relationshipType);
60 }
61
62 @PreAuthorize("hasRole('" + IdRepoEntitlement.RELATIONSHIPTYPE_LIST + "')")
63 @Transactional(readOnly = true)
64 public List<RelationshipTypeTO> list() {
65 return relationshipTypeDAO.findAll().stream().map(binder::getRelationshipTypeTO).collect(Collectors.toList());
66 }
67
68 @PreAuthorize("hasRole('" + IdRepoEntitlement.RELATIONSHIPTYPE_CREATE + "')")
69 public RelationshipTypeTO create(final RelationshipTypeTO relationshipTypeTO) {
70 return binder.getRelationshipTypeTO(relationshipTypeDAO.save(binder.create(relationshipTypeTO)));
71 }
72
73 @PreAuthorize("hasRole('" + IdRepoEntitlement.RELATIONSHIPTYPE_UPDATE + "')")
74 public RelationshipTypeTO update(final RelationshipTypeTO relationshipTypeTO) {
75 RelationshipType relationshipType = relationshipTypeDAO.find(relationshipTypeTO.getKey());
76 if (relationshipType == null) {
77 LOG.error("Could not find relationshipType '" + relationshipTypeTO.getKey() + '\'');
78 throw new NotFoundException(relationshipTypeTO.getKey());
79 }
80
81 binder.update(relationshipType, relationshipTypeTO);
82 relationshipType = relationshipTypeDAO.save(relationshipType);
83
84 return binder.getRelationshipTypeTO(relationshipType);
85 }
86
87 @PreAuthorize("hasRole('" + IdRepoEntitlement.RELATIONSHIPTYPE_DELETE + "')")
88 public RelationshipTypeTO delete(final String key) {
89 RelationshipType relationshipType = relationshipTypeDAO.find(key);
90 if (relationshipType == null) {
91 LOG.error("Could not find relationshipType '" + key + '\'');
92
93 throw new NotFoundException(key);
94 }
95
96 RelationshipTypeTO deleted = binder.getRelationshipTypeTO(relationshipType);
97
98 relationshipTypeDAO.delete(key);
99
100 return deleted;
101 }
102
103 @Override
104 protected RelationshipTypeTO resolveReference(final Method method, final Object... args)
105 throws UnresolvedReferenceException {
106
107 String key = null;
108
109 if (ArrayUtils.isNotEmpty(args)) {
110 for (int i = 0; key == null && i < args.length; i++) {
111 if (args[i] instanceof Long) {
112 key = (String) args[i];
113 } else if (args[i] instanceof RelationshipTypeTO) {
114 key = ((RelationshipTypeTO) args[i]).getKey();
115 }
116 }
117 }
118
119 if (StringUtils.isNotBlank(key)) {
120 try {
121 return binder.getRelationshipTypeTO(relationshipTypeDAO.find(key));
122 } catch (Throwable ignore) {
123 LOG.debug("Unresolved reference", ignore);
124 throw new UnresolvedReferenceException(ignore);
125 }
126 }
127
128 throw new UnresolvedReferenceException();
129 }
130
131 }