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