1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ksb.messaging.dao.impl;
17
18 import java.util.List;
19
20 import javax.persistence.EntityManager;
21 import javax.persistence.PersistenceContext;
22 import javax.persistence.Query;
23 import javax.xml.namespace.QName;
24
25 import org.kuali.rice.ksb.messaging.FlattenedServiceDefinition;
26 import org.kuali.rice.ksb.messaging.ServiceInfo;
27 import org.kuali.rice.ksb.messaging.dao.ServiceInfoDAO;
28
29
30 public class ServiceInfoDAOJpaImpl implements ServiceInfoDAO {
31
32 @PersistenceContext
33 EntityManager entityManager;
34
35 public void addEntry(ServiceInfo entry) {
36 if(entry.getMessageEntryId() == null) {
37 entityManager.persist(entry.getSerializedServiceNamespace());
38 entry.setFlattenedServiceDefinitionId(entry.getSerializedServiceNamespace().getFlattenedServiceDefinitionId());
39 entityManager.persist(entry);
40 }
41 else {
42 entityManager.merge(entry.getSerializedServiceNamespace());
43 entityManager.merge(entry);
44 }
45 }
46
47 @SuppressWarnings("unchecked")
48 public List<ServiceInfo> fetchAll() {
49 Query query = entityManager.createNamedQuery("ServiceInfo.FetchAll");
50 return (List<ServiceInfo>) query.getResultList();
51 }
52
53 @SuppressWarnings("unchecked")
54 public List<ServiceInfo> fetchAllActive() {
55 Query query = entityManager.createNamedQuery("ServiceInfo.FetchAllActive");
56 return (List<ServiceInfo>) query.getResultList();
57 }
58
59 @SuppressWarnings("unchecked")
60 public List<ServiceInfo> fetchActiveByNameLocalPart(String localPart) {
61 Query query = entityManager.createNamedQuery("ServiceInfo.FetchActiveByName");
62 query.setParameter("serviceName", "{%}" + localPart);
63 return (List<ServiceInfo>) query.getResultList();
64 }
65
66 @SuppressWarnings("unchecked")
67 public List<ServiceInfo> fetchActiveByName(QName serviceName) {
68 Query query = entityManager.createNamedQuery("ServiceInfo.FetchActiveByName");
69 query.setParameter("serviceName", serviceName.toString());
70 return (List<ServiceInfo>) query.getResultList();
71 }
72
73 @SuppressWarnings("unchecked")
74 public List<ServiceInfo> fetchActiveByNamespace(String serviceNamespace) {
75 Query query = entityManager.createNamedQuery("ServiceInfo.FetchActiveByName");
76 query.setParameter("serviceNamespace", "{"+serviceNamespace+"}%");
77 return (List<ServiceInfo>) query.getResultList();
78 }
79
80 @SuppressWarnings("unchecked")
81 public List<ServiceInfo> findLocallyPublishedServices(String ipNumber, String serviceNamespace) {
82 Query query = entityManager.createNamedQuery("ServiceInfo.FindLocallyPublishedServices");
83 query.setParameter("serverIp", ipNumber);
84 query.setParameter("serviceNamespace", serviceNamespace);
85 return (List<ServiceInfo>) query.getResultList();
86 }
87
88 public void removeEntry(ServiceInfo entry) {
89
90
91
92
93 entityManager.remove(entityManager.find(FlattenedServiceDefinition.class, entry.getFlattenedServiceDefinitionId()));
94 entityManager.remove(entityManager.find(ServiceInfo.class, entry.getMessageEntryId()));
95 }
96
97 public ServiceInfo findServiceInfo(Long serviceInfoId) {
98 return entityManager.find(ServiceInfo.class, serviceInfoId);
99 }
100
101 public void removeLocallyPublishedServices(String ipNumber, String serviceNamespace) {
102
103
104
105
106 List<ServiceInfo> localServices = findLocallyPublishedServices(ipNumber, serviceNamespace);
107 for (ServiceInfo localService : localServices) {
108 entityManager.remove(entityManager.find(FlattenedServiceDefinition.class, localService.getFlattenedServiceDefinitionId()));
109 entityManager.remove(entityManager.find(ServiceInfo.class, localService.getMessageEntryId()));
110 }
111 }
112
113 public EntityManager getEntityManager() {
114 return this.entityManager;
115 }
116
117 public void setEntityManager(EntityManager entityManager) {
118 this.entityManager = entityManager;
119 }
120
121 public FlattenedServiceDefinition findFlattenedServiceDefinition(Long flattenedServiceDefinitionId) {
122 return entityManager.find(FlattenedServiceDefinition.class, flattenedServiceDefinitionId);
123 }
124 }