View Javadoc
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.Optional;
24  import java.util.stream.Collectors;
25  import org.apache.commons.lang3.ArrayUtils;
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.syncope.common.lib.SyncopeClientException;
28  import org.apache.syncope.common.lib.to.AnyTypeTO;
29  import org.apache.syncope.common.lib.types.ClientExceptionType;
30  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
31  import org.apache.syncope.core.persistence.api.dao.AnyObjectDAO;
32  import org.apache.syncope.core.persistence.api.dao.AnyTypeDAO;
33  import org.apache.syncope.core.persistence.api.dao.DuplicateException;
34  import org.apache.syncope.core.persistence.api.dao.NotFoundException;
35  import org.apache.syncope.core.persistence.api.entity.AnyType;
36  import org.apache.syncope.core.provisioning.api.data.AnyTypeDataBinder;
37  import org.springframework.dao.InvalidDataAccessApiUsageException;
38  import org.springframework.security.access.prepost.PreAuthorize;
39  import org.springframework.transaction.annotation.Transactional;
40  
41  public class AnyTypeLogic extends AbstractTransactionalLogic<AnyTypeTO> {
42  
43      protected final AnyTypeDataBinder binder;
44  
45      protected final AnyTypeDAO anyTypeDAO;
46  
47      protected final AnyObjectDAO anyObjectDAO;
48  
49      public AnyTypeLogic(
50              final AnyTypeDataBinder binder,
51              final AnyTypeDAO anyTypeDAO,
52              final AnyObjectDAO anyObjectDAO) {
53  
54          this.binder = binder;
55          this.anyTypeDAO = anyTypeDAO;
56          this.anyObjectDAO = anyObjectDAO;
57      }
58  
59      @PreAuthorize("hasRole('" + IdRepoEntitlement.ANYTYPE_READ + "')")
60      @Transactional(readOnly = true)
61      public AnyTypeTO read(final String key) {
62          AnyType anyType = Optional.ofNullable(anyTypeDAO.find(key)).
63                  orElseThrow(() -> new NotFoundException("AnyType " + key));
64  
65          return binder.getAnyTypeTO(anyType);
66      }
67  
68      @PreAuthorize("hasRole('" + IdRepoEntitlement.ANYTYPE_LIST + "')")
69      @Transactional(readOnly = true)
70      public List<AnyTypeTO> list() {
71          return anyTypeDAO.findAll().stream().map(binder::getAnyTypeTO).collect(Collectors.toList());
72      }
73  
74      @PreAuthorize("hasRole('" + IdRepoEntitlement.ANYTYPE_CREATE + "')")
75      public AnyTypeTO create(final AnyTypeTO anyTypeTO) {
76          if (StringUtils.isBlank(anyTypeTO.getKey())) {
77              SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
78              sce.getElements().add(AnyType.class.getSimpleName() + " key");
79              throw sce;
80          }
81          if (anyTypeDAO.find(anyTypeTO.getKey()) != null) {
82              throw new DuplicateException(anyTypeTO.getKey());
83          }
84  
85          return binder.getAnyTypeTO(anyTypeDAO.save(binder.create(anyTypeTO)));
86      }
87  
88      @PreAuthorize("hasRole('" + IdRepoEntitlement.ANYTYPE_UPDATE + "')")
89      public void update(final AnyTypeTO anyTypeTO) {
90          AnyType anyType = Optional.ofNullable(anyTypeDAO.find(anyTypeTO.getKey())).
91                  orElseThrow(() -> new NotFoundException("AnyType " + anyTypeTO.getKey()));
92  
93          binder.update(anyType, anyTypeTO);
94          anyTypeDAO.save(anyType);
95      }
96  
97      @PreAuthorize("hasRole('" + IdRepoEntitlement.ANYTYPE_DELETE + "')")
98      public AnyTypeTO delete(final String key) {
99          AnyType anyType = Optional.ofNullable(anyTypeDAO.find(key)).
100                 orElseThrow(() -> new NotFoundException("AnyType " + key));
101 
102         Integer anyObjects = anyObjectDAO.countByType().get(anyType);
103         if (anyObjects != null && anyObjects > 0) {
104             LOG.error("{} AnyObject instances found for {}, aborting", anyObjects, anyType);
105 
106             SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidAnyType);
107             sce.getElements().add("AnyObject instances found for " + key);
108             throw sce;
109         }
110 
111         try {
112             return binder.delete(anyType);
113         } catch (IllegalArgumentException | InvalidDataAccessApiUsageException e) {
114             SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidRequest);
115             sce.getElements().add(e.getMessage());
116             throw sce;
117         }
118     }
119 
120     @Override
121     protected AnyTypeTO resolveReference(final Method method, final Object... args)
122             throws UnresolvedReferenceException {
123 
124         String key = null;
125 
126         if (ArrayUtils.isNotEmpty(args)) {
127             for (int i = 0; key == null && i < args.length; i++) {
128                 if (args[i] instanceof String) {
129                     key = (String) args[i];
130                 } else if (args[i] instanceof AnyTypeTO) {
131                     key = ((AnyTypeTO) args[i]).getKey();
132                 }
133             }
134         }
135 
136         if (StringUtils.isNotBlank(key)) {
137             try {
138                 return binder.getAnyTypeTO(anyTypeDAO.find(key));
139             } catch (Throwable ignore) {
140                 LOG.debug("Unresolved reference", ignore);
141                 throw new UnresolvedReferenceException(ignore);
142             }
143         }
144 
145         throw new UnresolvedReferenceException();
146     }
147 }