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.List;
22  import java.util.stream.Collectors;
23  import org.apache.syncope.common.lib.to.AnyTypeClassTO;
24  import org.apache.syncope.core.persistence.api.dao.AnyTypeDAO;
25  import org.apache.syncope.core.persistence.api.dao.DerSchemaDAO;
26  import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO;
27  import org.apache.syncope.core.persistence.api.dao.VirSchemaDAO;
28  import org.apache.syncope.core.persistence.api.entity.AnyType;
29  import org.apache.syncope.core.persistence.api.entity.AnyTypeClass;
30  import org.apache.syncope.core.persistence.api.entity.DerSchema;
31  import org.apache.syncope.core.persistence.api.entity.EntityFactory;
32  import org.apache.syncope.core.persistence.api.entity.PlainSchema;
33  import org.apache.syncope.core.persistence.api.entity.VirSchema;
34  import org.apache.syncope.core.provisioning.api.data.AnyTypeClassDataBinder;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  public class AnyTypeClassDataBinderImpl implements AnyTypeClassDataBinder {
39  
40      protected static final Logger LOG = LoggerFactory.getLogger(AnyTypeClassDataBinder.class);
41  
42      protected final PlainSchemaDAO plainSchemaDAO;
43  
44      protected final DerSchemaDAO derSchemaDAO;
45  
46      protected final VirSchemaDAO virSchemaDAO;
47  
48      protected final AnyTypeDAO anyTypeDAO;
49  
50      protected final EntityFactory entityFactory;
51  
52      public AnyTypeClassDataBinderImpl(
53              final PlainSchemaDAO plainSchemaDAO,
54              final DerSchemaDAO derSchemaDAO,
55              final VirSchemaDAO virSchemaDAO,
56              final AnyTypeDAO anyTypeDAO,
57              final EntityFactory entityFactory) {
58  
59          this.plainSchemaDAO = plainSchemaDAO;
60          this.derSchemaDAO = derSchemaDAO;
61          this.virSchemaDAO = virSchemaDAO;
62          this.anyTypeDAO = anyTypeDAO;
63          this.entityFactory = entityFactory;
64      }
65  
66      @Override
67      public AnyTypeClass create(final AnyTypeClassTO anyTypeClassTO) {
68          AnyTypeClass anyTypeClass = entityFactory.newEntity(AnyTypeClass.class);
69          update(anyTypeClass, anyTypeClassTO);
70          return anyTypeClass;
71      }
72  
73      @Override
74      public void update(final AnyTypeClass anyTypeClass, final AnyTypeClassTO anyTypeClassTO) {
75          if (anyTypeClass.getKey() == null) {
76              anyTypeClass.setKey(anyTypeClassTO.getKey());
77          }
78  
79          plainSchemaDAO.findByAnyTypeClasses(List.of(anyTypeClass)).
80                  forEach(schema -> schema.setAnyTypeClass(null));
81  
82          anyTypeClass.getPlainSchemas().clear();
83          anyTypeClassTO.getPlainSchemas().forEach(schemaName -> {
84              PlainSchema schema = plainSchemaDAO.find(schemaName);
85              if (schema == null || schema.getAnyTypeClass() != null) {
86                  LOG.debug("Invalid or already in use" + PlainSchema.class.getSimpleName()
87                          + "{}, ignoring...", schemaName);
88              } else {
89                  anyTypeClass.add(schema);
90              }
91          });
92  
93          derSchemaDAO.findByAnyTypeClasses(List.of(anyTypeClass)).
94                  forEach((schema) -> schema.setAnyTypeClass(null));
95  
96          anyTypeClass.getDerSchemas().clear();
97          anyTypeClassTO.getDerSchemas().forEach(schemaName -> {
98              DerSchema schema = derSchemaDAO.find(schemaName);
99              if (schema == null || schema.getAnyTypeClass() != null) {
100                 LOG.debug("Invalid or already in use" + DerSchema.class.getSimpleName()
101                         + "{}, ignoring...", schemaName);
102             } else {
103                 anyTypeClass.add(schema);
104             }
105         });
106 
107         virSchemaDAO.findByAnyTypeClasses(List.of(anyTypeClass)).
108                 forEach(schema -> schema.setAnyTypeClass(null));
109 
110         anyTypeClass.getVirSchemas().clear();
111         anyTypeClassTO.getVirSchemas().forEach(schemaName -> {
112             VirSchema schema = virSchemaDAO.find(schemaName);
113             if (schema == null || schema.getAnyTypeClass() != null) {
114                 LOG.debug("Invalid or already in use" + VirSchema.class.getSimpleName()
115                         + "{}, ignoring...", schemaName);
116             } else {
117                 anyTypeClass.add(schema);
118             }
119         });
120     }
121 
122     @Override
123     public AnyTypeClassTO getAnyTypeClassTO(final AnyTypeClass anyTypeClass) {
124         AnyTypeClassTO anyTypeClassTO = new AnyTypeClassTO();
125 
126         anyTypeClassTO.setKey(anyTypeClass.getKey());
127 
128         anyTypeClassTO.getInUseByTypes().addAll(
129                 anyTypeDAO.findByTypeClass(anyTypeClass).stream().map(AnyType::getKey).collect(Collectors.toList()));
130 
131         anyTypeClassTO.getPlainSchemas().addAll(
132                 anyTypeClass.getPlainSchemas().stream().map(PlainSchema::getKey).collect(Collectors.toList()));
133         anyTypeClassTO.getDerSchemas().addAll(
134                 anyTypeClass.getDerSchemas().stream().map(DerSchema::getKey).collect(Collectors.toList()));
135         anyTypeClassTO.getVirSchemas().addAll(
136                 anyTypeClass.getVirSchemas().stream().map(VirSchema::getKey).collect(Collectors.toList()));
137 
138         return anyTypeClassTO;
139     }
140 }