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 RELATIONSHIP
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.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 }