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 java.util.Optional;
23  import javax.persistence.Query;
24  import javax.persistence.TypedQuery;
25  import org.apache.syncope.core.persistence.api.dao.AuthProfileDAO;
26  import org.apache.syncope.core.persistence.api.entity.am.AuthProfile;
27  import org.apache.syncope.core.persistence.jpa.entity.am.JPAAuthProfile;
28  
29  public class JPAAuthProfileDAO extends AbstractDAO<AuthProfile> implements AuthProfileDAO {
30  
31      @Override
32      public AuthProfile find(final String key) {
33          return entityManager().find(JPAAuthProfile.class, key);
34      }
35  
36      @Override
37      public int count() {
38          Query query = entityManager().createQuery(
39                  "SELECT COUNT(e) FROM  " + JPAAuthProfile.class.getSimpleName() + " e");
40          return ((Number) query.getSingleResult()).intValue();
41      }
42  
43      @Override
44      public List<AuthProfile> findAll(final int page, final int itemsPerPage) {
45          TypedQuery<AuthProfile> query = entityManager().createQuery(
46                  "SELECT e FROM " + JPAAuthProfile.class.getSimpleName() + " e ORDER BY e.owner ASC",
47                  AuthProfile.class);
48  
49          // page starts from 1, while setFirtResult() starts from 0
50          query.setFirstResult(itemsPerPage * (page <= 0 ? 0 : page - 1));
51  
52          if (itemsPerPage >= 0) {
53              query.setMaxResults(itemsPerPage);
54          }
55  
56          return query.getResultList();
57      }
58  
59      @Override
60      public Optional<AuthProfile> findByOwner(final String owner) {
61          TypedQuery<AuthProfile> query = entityManager().createQuery(
62                  "SELECT e FROM " + JPAAuthProfile.class.getSimpleName()
63                  + " e WHERE e.owner=:owner", AuthProfile.class);
64          query.setParameter("owner", owner);
65  
66          List<AuthProfile> result = query.getResultList();
67          return result.isEmpty() ? Optional.empty() : Optional.of(result.get(0));
68      }
69  
70      @Override
71      public AuthProfile save(final AuthProfile profile) {
72          return entityManager().merge(profile);
73      }
74  
75      @Override
76      public void delete(final String key) {
77          AuthProfile authProfile = find(key);
78          if (authProfile == null) {
79              return;
80          }
81          delete(authProfile);
82      }
83  
84      @Override
85      public void delete(final AuthProfile authProfile) {
86          entityManager().remove(authProfile);
87      }
88  }