[SYNCOPE-1262] Last matrix parameter still there
[syncope.git] / core / logic / src / main / java / org / apache / syncope / core / logic / ImplementationLogic.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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.ImplementationTO;
28 import org.apache.syncope.common.lib.types.ClientExceptionType;
29 import org.apache.syncope.common.lib.types.ImplementationType;
30 import org.apache.syncope.common.lib.types.StandardEntitlement;
31 import org.apache.syncope.core.persistence.api.dao.DuplicateException;
32 import org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO;
33 import org.apache.syncope.core.persistence.api.dao.ImplementationDAO;
34 import org.apache.syncope.core.persistence.api.dao.NotFoundException;
35 import org.apache.syncope.core.persistence.api.dao.NotificationDAO;
36 import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO;
37 import org.apache.syncope.core.persistence.api.dao.PolicyDAO;
38 import org.apache.syncope.core.persistence.api.dao.RealmDAO;
39 import org.apache.syncope.core.persistence.api.dao.ReportDAO;
40 import org.apache.syncope.core.persistence.api.dao.TaskDAO;
41 import org.apache.syncope.core.persistence.api.entity.Implementation;
42 import org.apache.syncope.core.provisioning.api.data.ImplementationDataBinder;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.security.access.prepost.PreAuthorize;
45 import org.springframework.stereotype.Component;
46
47 @Component
48 public class ImplementationLogic extends AbstractTransactionalLogic<ImplementationTO> {
49
50 @Autowired
51 private ImplementationDataBinder binder;
52
53 @Autowired
54 private ImplementationDAO implementationDAO;
55
56 @Autowired
57 private ReportDAO reportDAO;
58
59 @Autowired
60 private PolicyDAO policyDAO;
61
62 @Autowired
63 private ExternalResourceDAO resourceDAO;
64
65 @Autowired
66 private TaskDAO taskDAO;
67
68 @Autowired
69 private RealmDAO realmDAO;
70
71 @Autowired
72 private PlainSchemaDAO plainSchemaDAO;
73
74 @Autowired
75 private NotificationDAO notificationDAO;
76
77 @PreAuthorize("hasRole('" + StandardEntitlement.IMPLEMENTATION_LIST + "')")
78 public List<ImplementationTO> list(final ImplementationType type) {
79 return implementationDAO.find(type).stream().
80 map(implementation -> binder.getImplementationTO(implementation)).collect(Collectors.toList());
81 }
82
83 @PreAuthorize("hasRole('" + StandardEntitlement.IMPLEMENTATION_READ + "')")
84 public ImplementationTO read(final ImplementationType type, final String key) {
85 Implementation implementation = implementationDAO.find(key);
86 if (implementation == null) {
87 LOG.error("Could not find implementation '" + key + "'");
88
89 throw new NotFoundException(key);
90 }
91
92 if (implementation.getType() != type) {
93 SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidRequest);
94 sce.getElements().add("Found " + type + ", expected " + implementation.getType());
95 throw sce;
96 }
97
98 return binder.getImplementationTO(implementation);
99 }
100
101 @PreAuthorize("hasRole('" + StandardEntitlement.IMPLEMENTATION_CREATE + "')")
102 public ImplementationTO create(final ImplementationTO implementationTO) {
103 if (StringUtils.isBlank(implementationTO.getKey())) {
104 SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
105 sce.getElements().add("Implementation key");
106 throw sce;
107 }
108
109 Implementation implementation = implementationDAO.find(implementationTO.getKey());
110 if (implementation != null) {
111 throw new DuplicateException(implementationTO.getKey());
112 }
113
114 return binder.getImplementationTO(implementationDAO.save(binder.create(implementationTO)));
115 }
116
117 @PreAuthorize("hasRole('" + StandardEntitlement.IMPLEMENTATION_UPDATE + "')")
118 public ImplementationTO update(final ImplementationTO implementationTO) {
119 Implementation implementation = implementationDAO.find(implementationTO.getKey());
120 if (implementation == null) {
121 LOG.error("Could not find implementation '" + implementationTO.getKey() + "'");
122
123 throw new NotFoundException(implementationTO.getKey());
124 }
125
126 binder.update(implementation, implementationTO);
127 implementation = implementationDAO.save(implementation);
128
129 return binder.getImplementationTO(implementation);
130 }
131
132 @PreAuthorize("hasRole('" + StandardEntitlement.IMPLEMENTATION_DELETE + "')")
133 public void delete(final ImplementationType type, final String key) {
134 Implementation implementation = implementationDAO.find(key);
135 if (implementation == null) {
136 LOG.error("Could not find implementation '" + key + "'");
137
138 throw new NotFoundException(key);
139 }
140
141 if (implementation.getType() != type) {
142 SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidRequest);
143 sce.getElements().add("Found " + type + ", expected " + implementation.getType());
144 throw sce;
145 }
146
147 boolean inUse = false;
148 switch (implementation.getType()) {
149 case REPORTLET:
150 inUse = !reportDAO.findByReportlet(implementation).isEmpty();
151 break;
152
153 case ACCOUNT_RULE:
154 inUse = !policyDAO.findByAccountRule(implementation).isEmpty();
155 break;
156
157 case PASSWORD_RULE:
158 inUse = !policyDAO.findByPasswordRule(implementation).isEmpty();
159 break;
160
161 case ITEM_TRANSFORMER:
162 inUse = !resourceDAO.findByTransformer(implementation).isEmpty();
163 break;
164
165 case TASKJOB_DELEGATE:
166 inUse = !taskDAO.findByDelegate(implementation).isEmpty();
167 break;
168
169 case RECON_FILTER_BUILDER:
170 inUse = !taskDAO.findByReconFilterBuilder(implementation).isEmpty();
171 break;
172
173 case LOGIC_ACTIONS:
174 inUse = !realmDAO.findByLogicActions(implementation).isEmpty();
175 break;
176
177 case PROPAGATION_ACTIONS:
178 inUse = !resourceDAO.findByPropagationActions(implementation).isEmpty();
179 break;
180
181 case PULL_ACTIONS:
182 inUse = !taskDAO.findByPullActions(implementation).isEmpty();
183 break;
184
185 case PUSH_ACTIONS:
186 inUse = !taskDAO.findByPushActions(implementation).isEmpty();
187 break;
188
189 case PULL_CORRELATION_RULE:
190 inUse = !policyDAO.findByCorrelationRule(implementation).isEmpty();
191 break;
192
193 case VALIDATOR:
194 inUse = !plainSchemaDAO.findByValidator(implementation).isEmpty();
195 break;
196
197 case RECIPIENTS_PROVIDER:
198 inUse = !notificationDAO.findByRecipientsProvider(implementation).isEmpty();
199 break;
200
201 default:
202 }
203
204 if (inUse) {
205 SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InUse);
206 sce.getElements().add("This implementation is in use");
207 throw sce;
208 }
209
210 implementationDAO.delete(key);
211 }
212
213 @Override
214 protected ImplementationTO resolveReference(final Method method, final Object... args)
215 throws UnresolvedReferenceException {
216
217 String key = null;
218
219 if (ArrayUtils.isNotEmpty(args)) {
220 for (int i = 0; key == null && i < args.length; i++) {
221 if (args[i] instanceof String) {
222 key = (String) args[i];
223 } else if (args[i] instanceof ImplementationTO) {
224 key = ((ImplementationTO) args[i]).getKey();
225 }
226 }
227 }
228
229 if (StringUtils.isNotBlank(key)) {
230 try {
231 return binder.getImplementationTO(implementationDAO.find(key));
232 } catch (Throwable ignore) {
233 LOG.debug("Unresolved reference", ignore);
234 throw new UnresolvedReferenceException(ignore);
235 }
236 }
237
238 throw new UnresolvedReferenceException();
239 }
240
241 }