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.SyncopeClientException;
27 import org.apache.syncope.common.lib.to.AnyTypeClassTO;
28 import org.apache.syncope.common.lib.types.ClientExceptionType;
29 import org.apache.syncope.common.lib.types.IdRepoEntitlement;
30 import org.apache.syncope.core.persistence.api.dao.AnyTypeClassDAO;
31 import org.apache.syncope.core.persistence.api.dao.DuplicateException;
32 import org.apache.syncope.core.persistence.api.dao.NotFoundException;
33 import org.apache.syncope.core.persistence.api.entity.AnyTypeClass;
34 import org.apache.syncope.core.provisioning.api.data.AnyTypeClassDataBinder;
35 import org.springframework.security.access.prepost.PreAuthorize;
36 import org.springframework.transaction.annotation.Transactional;
37
38 public class AnyTypeClassLogic extends AbstractTransactionalLogic<AnyTypeClassTO> {
39
40 protected final AnyTypeClassDataBinder binder;
41
42 protected final AnyTypeClassDAO anyTypeClassDAO;
43
44 public AnyTypeClassLogic(final AnyTypeClassDataBinder binder, final AnyTypeClassDAO anyTypeClassDAO) {
45 this.binder = binder;
46 this.anyTypeClassDAO = anyTypeClassDAO;
47 }
48
49 @PreAuthorize("hasRole('" + IdRepoEntitlement.ANYTYPECLASS_READ + "')")
50 @Transactional(readOnly = true)
51 public AnyTypeClassTO read(final String key) {
52 AnyTypeClass anyType = anyTypeClassDAO.find(key);
53 if (anyType == null) {
54 LOG.error("Could not find anyType '" + key + '\'');
55
56 throw new NotFoundException(key);
57 }
58
59 return binder.getAnyTypeClassTO(anyType);
60 }
61
62 @PreAuthorize("hasRole('" + IdRepoEntitlement.ANYTYPECLASS_LIST + "')")
63 @Transactional(readOnly = true)
64 public List<AnyTypeClassTO> list() {
65 return anyTypeClassDAO.findAll().stream().map(binder::getAnyTypeClassTO).collect(Collectors.toList());
66 }
67
68 @PreAuthorize("hasRole('" + IdRepoEntitlement.ANYTYPECLASS_CREATE + "')")
69 public AnyTypeClassTO create(final AnyTypeClassTO anyTypeClassTO) {
70 if (StringUtils.isBlank(anyTypeClassTO.getKey())) {
71 SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
72 sce.getElements().add(AnyTypeClass.class.getSimpleName() + " name");
73 throw sce;
74 }
75 if (anyTypeClassDAO.find(anyTypeClassTO.getKey()) != null) {
76 throw new DuplicateException(anyTypeClassTO.getKey());
77 }
78 return binder.getAnyTypeClassTO(anyTypeClassDAO.save(binder.create(anyTypeClassTO)));
79 }
80
81 @PreAuthorize("hasRole('" + IdRepoEntitlement.ANYTYPECLASS_UPDATE + "')")
82 public AnyTypeClassTO update(final AnyTypeClassTO anyTypeClassTO) {
83 AnyTypeClass anyType = anyTypeClassDAO.find(anyTypeClassTO.getKey());
84 if (anyType == null) {
85 LOG.error("Could not find anyTypeClass '" + anyTypeClassTO.getKey() + '\'');
86 throw new NotFoundException(anyTypeClassTO.getKey());
87 }
88
89 binder.update(anyType, anyTypeClassTO);
90 anyType = anyTypeClassDAO.save(anyType);
91
92 return binder.getAnyTypeClassTO(anyType);
93 }
94
95 @PreAuthorize("hasRole('" + IdRepoEntitlement.ANYTYPECLASS_DELETE + "')")
96 public AnyTypeClassTO delete(final String key) {
97 AnyTypeClass anyTypeClass = anyTypeClassDAO.find(key);
98 if (anyTypeClass == null) {
99 LOG.error("Could not find anyTypeClass '" + key + '\'');
100
101 throw new NotFoundException(key);
102 }
103
104 AnyTypeClassTO deleted = binder.getAnyTypeClassTO(anyTypeClass);
105 anyTypeClassDAO.delete(key);
106 return deleted;
107 }
108
109 @Override
110 protected AnyTypeClassTO resolveReference(final Method method, final Object... args)
111 throws UnresolvedReferenceException {
112
113 String key = null;
114
115 if (ArrayUtils.isNotEmpty(args)) {
116 for (int i = 0; key == null && i < args.length; i++) {
117 if (args[i] instanceof String) {
118 key = (String) args[i];
119 } else if (args[i] instanceof AnyTypeClassTO) {
120 key = ((AnyTypeClassTO) args[i]).getKey();
121 }
122 }
123 }
124
125 if (StringUtils.isNotBlank(key)) {
126 try {
127 return binder.getAnyTypeClassTO(anyTypeClassDAO.find(key));
128 } catch (Throwable ignore) {
129 LOG.debug("Unresolved reference", ignore);
130 throw new UnresolvedReferenceException(ignore);
131 }
132 }
133
134 throw new UnresolvedReferenceException();
135 }
136
137 }