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.OIDCRPClientAppDAO;
25  import org.apache.syncope.core.persistence.api.entity.Realm;
26  import org.apache.syncope.core.persistence.api.entity.am.OIDCRPClientApp;
27  import org.apache.syncope.core.persistence.api.entity.policy.Policy;
28  import org.apache.syncope.core.persistence.jpa.entity.am.JPAOIDCRPClientApp;
29  import org.springframework.transaction.annotation.Transactional;
30  
31  public class JPAOIDCRPClientAppDAO extends AbstractClientAppDAO<OIDCRPClientApp> implements OIDCRPClientAppDAO {
32  
33      @Override
34      public OIDCRPClientApp find(final String key) {
35          return entityManager().find(JPAOIDCRPClientApp.class, key);
36      }
37  
38      private OIDCRPClientApp find(final String column, final Object value) {
39          TypedQuery<OIDCRPClientApp> query = entityManager().createQuery(
40                  "SELECT e FROM " + JPAOIDCRPClientApp.class.getSimpleName() + " e WHERE e." + column + "=:value",
41                  OIDCRPClientApp.class);
42          query.setParameter("value", value);
43  
44          OIDCRPClientApp 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 OIDCRPClientApp findByClientAppId(final Long clientAppId) {
56          return find("clientAppId", clientAppId);
57      }
58  
59      @Override
60      public OIDCRPClientApp findByName(final String name) {
61          return find("name", name);
62      }
63  
64      @Override
65      public OIDCRPClientApp findByClientId(final String clientId) {
66          return find("clientId", clientId);
67      }
68  
69      @Override
70      public List<OIDCRPClientApp> findByPolicy(final Policy policy) {
71          return findByPolicy(policy, OIDCRPClientApp.class, JPAOIDCRPClientApp.class);
72      }
73  
74      @Override
75      public List<OIDCRPClientApp> findByRealm(final Realm realm) {
76          return findByRealm(realm, OIDCRPClientApp.class, JPAOIDCRPClientApp.class);
77      }
78  
79      @Transactional(readOnly = true)
80      @Override
81      public List<OIDCRPClientApp> findAll() {
82          TypedQuery<OIDCRPClientApp> query = entityManager().createQuery(
83                  "SELECT e FROM " + JPAOIDCRPClientApp.class.getSimpleName() + " e", OIDCRPClientApp.class);
84  
85          return query.getResultList();
86      }
87  
88      @Override
89      public OIDCRPClientApp save(final OIDCRPClientApp clientApp) {
90          ((JPAOIDCRPClientApp) clientApp).list2json();
91          return entityManager().merge(clientApp);
92      }
93  
94      @Override
95      public void delete(final String key) {
96          OIDCRPClientApp rpTO = find(key);
97          if (rpTO == null) {
98              return;
99          }
100 
101         delete(rpTO);
102     }
103 
104     @Override
105     public void deleteByClientId(final String clientId) {
106         OIDCRPClientApp rpTO = findByClientId(clientId);
107         if (rpTO == null) {
108             return;
109         }
110         delete(rpTO);
111     }
112 
113     @Override
114     public void delete(final OIDCRPClientApp clientApp) {
115         entityManager().remove(clientApp);
116     }
117 }