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.ClientAppDAO;
24  import org.apache.syncope.core.persistence.api.entity.Realm;
25  import org.apache.syncope.core.persistence.api.entity.am.ClientApp;
26  import org.apache.syncope.core.persistence.api.entity.policy.AccessPolicy;
27  import org.apache.syncope.core.persistence.api.entity.policy.AttrReleasePolicy;
28  import org.apache.syncope.core.persistence.api.entity.policy.AuthPolicy;
29  import org.apache.syncope.core.persistence.api.entity.policy.Policy;
30  import org.apache.syncope.core.persistence.api.entity.policy.TicketExpirationPolicy;
31  
32  public abstract class AbstractClientAppDAO<C extends ClientApp> extends AbstractDAO<C> implements ClientAppDAO<C> {
33  
34      protected StringBuilder getByPolicyQuery(
35              final Class<? extends Policy> policyClass,
36              final Class<? extends C> clientAppJPAClass) {
37  
38          StringBuilder query = new StringBuilder("SELECT e FROM ").
39                  append(clientAppJPAClass.getSimpleName()).
40                  append(" e WHERE e.");
41  
42          if (AuthPolicy.class.isAssignableFrom(policyClass)) {
43              query.append("authPolicy");
44          } else if (AccessPolicy.class.isAssignableFrom(policyClass)) {
45              query.append("accessPolicy");
46          } else if (AttrReleasePolicy.class.isAssignableFrom(policyClass)) {
47              query.append("attrReleasePolicy");
48          } else if (TicketExpirationPolicy.class.isAssignableFrom(policyClass)) {
49              query.append("ticketExpirationPolicy");
50          }
51  
52          return query;
53      }
54  
55      protected List<C> findByPolicy(
56              final Policy policy,
57              final Class<C> reference,
58              final Class<? extends C> clientAppJPAClass) {
59  
60          TypedQuery<C> query = entityManager().createQuery(
61                  getByPolicyQuery(policy.getClass(), clientAppJPAClass).append("=:policy").toString(), reference);
62          query.setParameter("policy", policy);
63          return query.getResultList();
64      }
65  
66      protected List<C> findByRealm(
67              final Realm realm,
68              final Class<C> reference,
69              final Class<? extends C> clientAppJPAClass) {
70  
71          TypedQuery<C> query = entityManager().createQuery(
72                  "SELECT e FROM " + clientAppJPAClass.getSimpleName() + " e WHERE e.realm=:realm", reference);
73          query.setParameter("realm", realm);
74          return query.getResultList();
75      }
76  }