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.persistence.jpa.dao;
20  
21  import java.util.stream.Collectors;
22  import org.apache.syncope.core.persistence.api.dao.PlainAttrValueDAO;
23  import org.apache.syncope.core.persistence.api.entity.AnyUtils;
24  import org.apache.syncope.core.persistence.api.entity.PlainAttr;
25  import org.apache.syncope.core.persistence.api.entity.PlainAttrValue;
26  import org.apache.syncope.core.persistence.api.entity.anyobject.APlainAttrUniqueValue;
27  import org.apache.syncope.core.persistence.api.entity.anyobject.APlainAttrValue;
28  import org.apache.syncope.core.persistence.api.entity.group.GPlainAttrUniqueValue;
29  import org.apache.syncope.core.persistence.api.entity.group.GPlainAttrValue;
30  import org.apache.syncope.core.persistence.api.entity.user.UPlainAttrUniqueValue;
31  import org.apache.syncope.core.persistence.api.entity.user.UPlainAttrValue;
32  import org.apache.syncope.core.persistence.jpa.entity.AbstractPlainAttrValue;
33  import org.apache.syncope.core.persistence.jpa.entity.anyobject.JPAAPlainAttrUniqueValue;
34  import org.apache.syncope.core.persistence.jpa.entity.anyobject.JPAAPlainAttrValue;
35  import org.apache.syncope.core.persistence.jpa.entity.group.JPAGPlainAttrUniqueValue;
36  import org.apache.syncope.core.persistence.jpa.entity.group.JPAGPlainAttrValue;
37  import org.apache.syncope.core.persistence.jpa.entity.user.JPAUPlainAttrUniqueValue;
38  import org.apache.syncope.core.persistence.jpa.entity.user.JPAUPlainAttrValue;
39  
40  public class JPAPlainAttrValueDAO extends AbstractDAO<PlainAttrValue> implements PlainAttrValueDAO {
41  
42      @SuppressWarnings("unchecked")
43      public static <T extends PlainAttrValue> Class<? extends AbstractPlainAttrValue> getEntityReference(
44              final Class<T> reference) {
45  
46          return AbstractPlainAttrValue.class.isAssignableFrom(reference)
47                  ? (Class<? extends AbstractPlainAttrValue>) reference
48                  : reference.equals(GPlainAttrValue.class)
49                  ? JPAGPlainAttrValue.class
50                  : reference.equals(GPlainAttrUniqueValue.class)
51                  ? JPAGPlainAttrUniqueValue.class
52                  : reference.equals(APlainAttrValue.class)
53                  ? JPAAPlainAttrValue.class
54                  : reference.equals(APlainAttrUniqueValue.class)
55                  ? JPAAPlainAttrUniqueValue.class
56                  : reference.equals(UPlainAttrValue.class)
57                  ? JPAUPlainAttrValue.class
58                  : reference.equals(UPlainAttrUniqueValue.class)
59                  ? JPAUPlainAttrUniqueValue.class
60                  : null;
61      }
62  
63      @Override
64      public void deleteAll(final PlainAttr<?> attr, final AnyUtils anyUtils) {
65          if (attr.getUniqueValue() == null) {
66              attr.getValues().stream().map(PlainAttrValue::getKey).collect(Collectors.toSet()).forEach(attrValueKey -> {
67                  PlainAttrValue attrValue = anyUtils.plainAttrValueClass().cast(
68                          entityManager().find(getEntityReference(anyUtils.plainAttrValueClass()), attrValueKey));
69                  if (attrValue != null) {
70                      entityManager().remove(attrValue);
71                      attr.getValues().remove(attrValue);
72                  }
73              });
74          } else {
75              entityManager().remove(attr.getUniqueValue());
76              attr.setUniqueValue(null);
77          }
78      }
79  }