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.core.persistence.api.dao.ApplicationDAO;
24  import org.apache.syncope.core.persistence.api.dao.RoleDAO;
25  import org.apache.syncope.core.persistence.api.dao.UserDAO;
26  import org.apache.syncope.core.persistence.api.entity.Application;
27  import org.apache.syncope.core.persistence.api.entity.Privilege;
28  import org.apache.syncope.core.persistence.jpa.entity.JPAApplication;
29  import org.apache.syncope.core.persistence.jpa.entity.JPAPrivilege;
30  
31  public class JPAApplicationDAO extends AbstractDAO<Application> implements ApplicationDAO {
32  
33      protected final RoleDAO roleDAO;
34  
35      protected final UserDAO userDAO;
36  
37      public JPAApplicationDAO(final RoleDAO roleDAO, final UserDAO userDAO) {
38          this.roleDAO = roleDAO;
39          this.userDAO = userDAO;
40      }
41  
42      @Override
43      public Application find(final String key) {
44          return entityManager().find(JPAApplication.class, key);
45      }
46  
47      @Override
48      public Privilege findPrivilege(final String key) {
49          return entityManager().find(JPAPrivilege.class, key);
50      }
51  
52      @Override
53      public List<Application> findAll() {
54          TypedQuery<Application> query = entityManager().createQuery(
55                  "SELECT e FROM " + JPAApplication.class.getSimpleName() + " e ", Application.class);
56          return query.getResultList();
57      }
58  
59      @Override
60      public Application save(final Application application) {
61          return entityManager().merge(application);
62      }
63  
64      @Override
65      public void delete(final Application application) {
66          application.getPrivileges().forEach(privilege -> {
67              roleDAO.findByPrivilege(privilege).
68                      forEach(role -> role.getPrivileges().remove(privilege));
69              userDAO.findLinkedAccountsByPrivilege(privilege).
70                      forEach(account -> account.getPrivileges().remove(privilege));
71  
72              privilege.setApplication(null);
73          });
74          application.getPrivileges().clear();
75  
76          entityManager().remove(application);
77      }
78  
79      @Override
80      public void delete(final String key) {
81          Application application = find(key);
82          if (application == null) {
83              return;
84          }
85  
86          delete(application);
87      }
88  }