1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }