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.NoResultException;
23  import javax.persistence.TypedQuery;
24  import org.apache.syncope.core.persistence.api.dao.CASSPClientAppDAO;
25  import org.apache.syncope.core.persistence.api.entity.Realm;
26  import org.apache.syncope.core.persistence.api.entity.am.CASSPClientApp;
27  import org.apache.syncope.core.persistence.api.entity.policy.Policy;
28  import org.apache.syncope.core.persistence.jpa.entity.am.JPACASSPClientApp;
29  import org.springframework.transaction.annotation.Transactional;
30  
31  public class JPACASSPClientAppDAO extends AbstractClientAppDAO<CASSPClientApp> implements CASSPClientAppDAO {
32  
33      @Override
34      public CASSPClientApp find(final String key) {
35          return entityManager().find(JPACASSPClientApp.class, key);
36      }
37  
38      private CASSPClientApp find(final String column, final Object value) {
39          TypedQuery<CASSPClientApp> query = entityManager().createQuery(
40                  "SELECT e FROM " + JPACASSPClientApp.class.getSimpleName() + " e WHERE e." + column + "=:value",
41                  CASSPClientApp.class);
42          query.setParameter("value", value);
43  
44          CASSPClientApp result = null;
45          try {
46              result = query.getSingleResult();
47          } catch (final NoResultException e) {
48              LOG.debug("No OIDCRP found with " + column + " {}", value, e);
49          }
50  
51          return result;
52      }
53  
54      @Override
55      public CASSPClientApp findByClientAppId(final Long clientAppId) {
56          return find("clientAppId", clientAppId);
57      }
58  
59      @Override
60      public CASSPClientApp findByName(final String name) {
61          return find("name", name);
62      }
63  
64      @Override
65      public List<CASSPClientApp> findByPolicy(final Policy policy) {
66          return findByPolicy(policy, CASSPClientApp.class, JPACASSPClientApp.class);
67      }
68  
69      @Override
70      public List<CASSPClientApp> findByRealm(final Realm realm) {
71          return findByRealm(realm, CASSPClientApp.class, JPACASSPClientApp.class);
72      }
73  
74      @Transactional(readOnly = true)
75      @Override
76      public List<CASSPClientApp> findAll() {
77          TypedQuery<CASSPClientApp> query = entityManager().createQuery(
78                  "SELECT e FROM " + JPACASSPClientApp.class.getSimpleName() + " e", CASSPClientApp.class);
79  
80          return query.getResultList();
81      }
82  
83      @Override
84      public CASSPClientApp save(final CASSPClientApp clientApp) {
85          return entityManager().merge(clientApp);
86      }
87  
88      @Override
89      public void delete(final String key) {
90          CASSPClientApp rpTO = find(key);
91          if (rpTO == null) {
92              return;
93          }
94  
95          delete(rpTO);
96      }
97  
98      @Override
99      public void delete(final CASSPClientApp clientApp) {
100         entityManager().remove(clientApp);
101     }
102 }