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.provisioning.java.data;
20  
21  import java.util.Iterator;
22  import org.apache.syncope.common.lib.SyncopeClientException;
23  import org.apache.syncope.common.lib.to.DynRealmTO;
24  import org.apache.syncope.common.lib.types.ClientExceptionType;
25  import org.apache.syncope.core.persistence.api.dao.AnyTypeDAO;
26  import org.apache.syncope.core.persistence.api.dao.DynRealmDAO;
27  import org.apache.syncope.core.persistence.api.dao.search.SearchCond;
28  import org.apache.syncope.core.persistence.api.entity.AnyType;
29  import org.apache.syncope.core.persistence.api.entity.DynRealm;
30  import org.apache.syncope.core.persistence.api.entity.DynRealmMembership;
31  import org.apache.syncope.core.persistence.api.entity.EntityFactory;
32  import org.apache.syncope.core.persistence.api.search.SearchCondConverter;
33  import org.apache.syncope.core.persistence.api.search.SearchCondVisitor;
34  import org.apache.syncope.core.provisioning.api.data.DynRealmDataBinder;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  public class DynRealmDataBinderImpl implements DynRealmDataBinder {
39  
40      protected static final Logger LOG = LoggerFactory.getLogger(DynRealmDataBinder.class);
41  
42      protected final AnyTypeDAO anyTypeDAO;
43  
44      protected final DynRealmDAO dynRealmDAO;
45  
46      protected final EntityFactory entityFactory;
47  
48      protected final SearchCondVisitor searchCondVisitor;
49  
50      public DynRealmDataBinderImpl(
51              final AnyTypeDAO anyTypeDAO,
52              final DynRealmDAO dynRealmDAO,
53              final EntityFactory entityFactory,
54              final SearchCondVisitor searchCondVisitor) {
55  
56          this.anyTypeDAO = anyTypeDAO;
57          this.dynRealmDAO = dynRealmDAO;
58          this.entityFactory = entityFactory;
59          this.searchCondVisitor = searchCondVisitor;
60      }
61  
62      protected void setDynMembership(final DynRealm dynRealm, final AnyType anyType, final String dynMembershipFIQL) {
63          SearchCond dynMembershipCond = SearchCondConverter.convert(searchCondVisitor, dynMembershipFIQL);
64          if (!dynMembershipCond.isValid()) {
65              SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidSearchParameters);
66              sce.getElements().add(dynMembershipFIQL);
67              throw sce;
68          }
69  
70          DynRealmMembership dynMembership;
71          if (dynRealm.getDynMembership(anyType).isPresent()) {
72              dynMembership = dynRealm.getDynMembership(anyType).get();
73          } else {
74              dynMembership = entityFactory.newEntity(DynRealmMembership.class);
75              dynMembership.setDynRealm(dynRealm);
76              dynMembership.setAnyType(anyType);
77              dynRealm.add(dynMembership);
78          }
79          dynMembership.setFIQLCond(dynMembershipFIQL);
80      }
81  
82      @Override
83      public DynRealm create(final DynRealmTO dynRealmTO) {
84          return update(entityFactory.newEntity(DynRealm.class), dynRealmTO);
85      }
86  
87      @Override
88      public DynRealm update(final DynRealm toBeUpdated, final DynRealmTO dynRealmTO) {
89          toBeUpdated.setKey(dynRealmTO.getKey());
90          DynRealm dynRealm = dynRealmDAO.save(toBeUpdated);
91  
92          for (Iterator<? extends DynRealmMembership> itor = dynRealm.getDynMemberships().iterator(); itor.hasNext();) {
93              DynRealmMembership memb = itor.next();
94              memb.setDynRealm(null);
95              itor.remove();
96          }
97  
98          dynRealmTO.getDynMembershipConds().forEach((type, fiql) -> {
99              AnyType anyType = anyTypeDAO.find(type);
100             if (anyType == null) {
101                 LOG.warn("Ignoring invalid {}: {}", AnyType.class.getSimpleName(), type);
102             } else {
103                 setDynMembership(dynRealm, anyType, fiql);
104             }
105         });
106 
107         return dynRealmDAO.saveAndRefreshDynMemberships(dynRealm);
108     }
109 
110     @Override
111     public DynRealmTO getDynRealmTO(final DynRealm dynRealm) {
112         DynRealmTO dynRealmTO = new DynRealmTO();
113 
114         dynRealmTO.setKey(dynRealm.getKey());
115 
116         dynRealm.getDynMemberships().forEach(memb -> dynRealmTO.getDynMembershipConds().
117                 put(memb.getAnyType().getKey(), memb.getFIQLCond()));
118 
119         return dynRealmTO;
120     }
121 }