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.List;
22  import javax.persistence.TypedQuery;
23  import org.apache.syncope.common.lib.types.AnyTypeKind;
24  import org.apache.syncope.core.persistence.api.dao.AnyTypeDAO;
25  import org.apache.syncope.core.persistence.api.dao.RemediationDAO;
26  import org.apache.syncope.core.persistence.api.entity.AnyType;
27  import org.apache.syncope.core.persistence.api.entity.AnyTypeClass;
28  import org.apache.syncope.core.persistence.jpa.entity.JPAAnyType;
29  import org.springframework.transaction.annotation.Transactional;
30  
31  public class JPAAnyTypeDAO extends AbstractDAO<AnyType> implements AnyTypeDAO {
32  
33      protected final RemediationDAO remediationDAO;
34  
35      public JPAAnyTypeDAO(final RemediationDAO remediationDAO) {
36          this.remediationDAO = remediationDAO;
37      }
38  
39      @Transactional(readOnly = true)
40      @Override
41      public AnyType find(final String key) {
42          return entityManager().find(JPAAnyType.class, key);
43      }
44  
45      @Transactional(readOnly = true)
46      @Override
47      public AnyType findUser() {
48          return find(AnyTypeKind.USER.name());
49      }
50  
51      @Transactional(readOnly = true)
52      @Override
53      public AnyType findGroup() {
54          return find(AnyTypeKind.GROUP.name());
55      }
56  
57      @Override
58      public List<AnyType> findByTypeClass(final AnyTypeClass anyTypeClass) {
59          TypedQuery<AnyType> query = entityManager().createQuery(
60                  "SELECT e FROM " + JPAAnyType.class.getSimpleName() + " e WHERE :anyTypeClass MEMBER OF e.classes",
61                  AnyType.class);
62          query.setParameter("anyTypeClass", anyTypeClass);
63  
64          return query.getResultList();
65      }
66  
67      @Override
68      public List<AnyType> findAll() {
69          TypedQuery<AnyType> query = entityManager().createQuery(
70                  "SELECT e FROM " + JPAAnyType.class.getSimpleName() + " e ", AnyType.class);
71          return query.getResultList();
72      }
73  
74      @Override
75      public AnyType save(final AnyType anyType) {
76          return entityManager().merge(anyType);
77      }
78  
79      @Override
80      public void delete(final String key) {
81          AnyType anyType = find(key);
82          if (anyType == null) {
83              return;
84          }
85  
86          if (anyType.equals(findUser()) || anyType.equals(findGroup())) {
87              throw new IllegalArgumentException(key + " cannot be deleted");
88          }
89  
90          remediationDAO.findByAnyType(anyType).forEach(remediation -> {
91              remediation.setAnyType(null);
92              remediationDAO.delete(remediation);
93          });
94  
95          entityManager().remove(anyType);
96      }
97  }