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 javax.persistence.TypedQuery;
23 import org.apache.commons.lang3.StringUtils;
24 import org.apache.syncope.core.persistence.api.dao.EntityCacheDAO;
25 import org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO;
26 import org.apache.syncope.core.persistence.api.dao.ImplementationDAO;
27 import org.apache.syncope.core.persistence.api.entity.Implementation;
28 import org.apache.syncope.core.persistence.jpa.entity.JPAExternalResource;
29 import org.apache.syncope.core.persistence.jpa.entity.JPAImplementation;
30 import org.apache.syncope.core.spring.implementation.ImplementationManager;
31 import org.springframework.transaction.annotation.Transactional;
32
33 public class JPAImplementationDAO extends AbstractDAO<Implementation> implements ImplementationDAO {
34
35 protected final ExternalResourceDAO resourceDAO;
36
37 protected final EntityCacheDAO entityCacheDAO;
38
39 public JPAImplementationDAO(final ExternalResourceDAO resourceDAO, final EntityCacheDAO entityCacheDAO) {
40 this.resourceDAO = resourceDAO;
41 this.entityCacheDAO = entityCacheDAO;
42 }
43
44 @Transactional(readOnly = true)
45 @Override
46 public Implementation find(final String key) {
47 return entityManager().find(JPAImplementation.class, key);
48 }
49
50 @Override
51 public List<Implementation> findByType(final String type) {
52 TypedQuery<Implementation> query = entityManager().createQuery(
53 "SELECT e FROM " + JPAImplementation.class.getSimpleName() + " e WHERE e.type=:type ORDER BY e.id ASC",
54 Implementation.class);
55 query.setParameter("type", type);
56 return query.getResultList();
57 }
58
59 @Override
60 public List<Implementation> findByTypeAndKeyword(final String type, final String keyword) {
61 if (StringUtils.isBlank(keyword)) {
62 return findByType(type);
63 }
64
65 TypedQuery<Implementation> query = entityManager().createQuery(
66 "SELECT e FROM " + JPAImplementation.class.getSimpleName() + " e "
67 + "WHERE e.type=:type "
68 + "AND e.id LIKE :keyword "
69 + "ORDER BY e.id ASC",
70 Implementation.class);
71 query.setParameter("type", type);
72 query.setParameter("keyword", keyword);
73 return query.getResultList();
74 }
75
76 @Override
77 public List<Implementation> findAll() {
78 TypedQuery<Implementation> query = entityManager().createQuery(
79 "SELECT e FROM " + JPAImplementation.class.getSimpleName() + " e", Implementation.class);
80 return query.getResultList();
81 }
82
83 @Override
84 public Implementation save(final Implementation implementation) {
85 Implementation merged = entityManager().merge(implementation);
86
87 ImplementationManager.purge(merged.getKey());
88
89 resourceDAO.findByProvisionSorter(merged).
90 forEach(resource -> entityCacheDAO.evict(JPAExternalResource.class, resource.getKey()));
91
92 return merged;
93 }
94
95 @Override
96 public void delete(final String key) {
97 Implementation implementation = find(key);
98 if (implementation == null) {
99 return;
100 }
101
102 entityManager().remove(implementation);
103 ImplementationManager.purge(key);
104 }
105 }