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 | |
import java.util.Map; |
20 | |
|
21 | |
import javax.persistence.EntityManager; |
22 | |
import javax.persistence.PersistenceContext; |
23 | |
import javax.persistence.Query; |
24 | |
import javax.xml.namespace.QName; |
25 | |
|
26 | |
import org.kuali.rice.core.config.ConfigContext; |
27 | |
import org.kuali.rice.core.jpa.criteria.Criteria; |
28 | |
import org.kuali.rice.core.jpa.criteria.QueryByCriteria; |
29 | |
import org.kuali.rice.core.util.OrmUtils; |
30 | |
import org.kuali.rice.core.util.RiceUtilities; |
31 | |
import org.kuali.rice.ksb.messaging.PersistedMessage; |
32 | |
import org.kuali.rice.ksb.messaging.PersistedMessagePayload; |
33 | |
import org.kuali.rice.ksb.messaging.dao.MessageQueueDAO; |
34 | |
import org.kuali.rice.ksb.util.KSBConstants; |
35 | |
|
36 | |
|
37 | 0 | public class MessageQueueDAOJpaImpl implements MessageQueueDAO { |
38 | |
|
39 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MessageQueueDAOJpaImpl.class); |
40 | |
|
41 | |
@PersistenceContext |
42 | |
private EntityManager entityManager; |
43 | |
|
44 | |
@SuppressWarnings("unchecked") |
45 | |
public List<PersistedMessage> findAll() { |
46 | 0 | if (LOG.isDebugEnabled()) { |
47 | 0 | LOG.debug("Returning all persisted messages"); |
48 | |
} |
49 | |
|
50 | 0 | Query query = entityManager.createNamedQuery("PersistedMessage.FindAll"); |
51 | 0 | return (List<PersistedMessage>) query.getResultList(); |
52 | |
} |
53 | |
|
54 | |
@SuppressWarnings("unchecked") |
55 | |
public List<PersistedMessage> findAll(int maxRows) { |
56 | 0 | if (LOG.isDebugEnabled()) { |
57 | 0 | LOG.debug("Finding next " + maxRows + " messages"); |
58 | |
} |
59 | |
|
60 | 0 | Query query = entityManager.createNamedQuery("PersistedMessage.FindAll"); |
61 | 0 | query.setMaxResults(maxRows); |
62 | |
|
63 | 0 | return (List<PersistedMessage>) query.getResultList(); |
64 | |
} |
65 | |
|
66 | |
|
67 | |
public PersistedMessagePayload findByPersistedMessageByRouteQueueId(Long routeQueueId) { |
68 | 0 | return (PersistedMessagePayload) entityManager.find(PersistedMessagePayload.class, routeQueueId); |
69 | |
} |
70 | |
|
71 | |
|
72 | |
public PersistedMessage findByRouteQueueId(Long routeQueueId) { |
73 | 0 | return (PersistedMessage) entityManager.find(PersistedMessage.class, routeQueueId); |
74 | |
} |
75 | |
|
76 | |
@SuppressWarnings("unchecked") |
77 | |
public List<PersistedMessage> findByServiceName(QName serviceName, String methodName) { |
78 | 0 | if (LOG.isDebugEnabled()) { |
79 | 0 | LOG.debug("Finding messages for service name " + serviceName); |
80 | |
} |
81 | |
|
82 | 0 | Query query = entityManager.createNamedQuery("PersistedMessage.FindByServiceName"); |
83 | 0 | query.setParameter("serviceName", serviceName); |
84 | 0 | query.setParameter("methodName", methodName); |
85 | |
|
86 | 0 | return (List<PersistedMessage>) query.getResultList(); |
87 | |
} |
88 | |
|
89 | |
@SuppressWarnings("unchecked") |
90 | |
public List<PersistedMessage> findByValues(Map<String, String> criteriaValues, int maxRows) { |
91 | 0 | Criteria criteria = new Criteria(PersistedMessage.class.getName()); |
92 | 0 | for(Map.Entry<String, String> entry : criteriaValues.entrySet()) { |
93 | 0 | criteria.eq(entry.getKey(), entry.getValue()); |
94 | |
} |
95 | |
|
96 | 0 | QueryByCriteria query = new QueryByCriteria(entityManager, criteria); |
97 | |
|
98 | 0 | return query.toQuery().getResultList(); |
99 | |
} |
100 | |
|
101 | |
@SuppressWarnings("unchecked") |
102 | |
public List<PersistedMessage> getNextDocuments(Integer maxDocuments) { |
103 | 0 | String serviceNamespace = ConfigContext.getCurrentContextConfig().getServiceNamespace(); |
104 | |
|
105 | 0 | Query query = entityManager.createNamedQuery("PersistedMessage.GetNextDocuments"); |
106 | 0 | query.setParameter("serviceNamespace", serviceNamespace); |
107 | 0 | query.setParameter("queueStatus", KSBConstants.ROUTE_QUEUE_EXCEPTION); |
108 | 0 | query.setParameter("ipNumber", RiceUtilities.getIpNumber()); |
109 | |
|
110 | 0 | if (maxDocuments != null) |
111 | 0 | query.setMaxResults(maxDocuments); |
112 | |
|
113 | 0 | return (List<PersistedMessage>) query.getResultList(); |
114 | |
} |
115 | |
|
116 | |
|
117 | |
public void remove(PersistedMessage routeQueue) { |
118 | 0 | if (LOG.isDebugEnabled()) { |
119 | 0 | LOG.debug("Removing message " + routeQueue); |
120 | |
} |
121 | |
|
122 | 0 | PersistedMessagePayload persistedMessagePayload = (PersistedMessagePayload) entityManager.find(PersistedMessagePayload.class, routeQueue.getRouteQueueId()); |
123 | 0 | PersistedMessage persistedMessage = (PersistedMessage) entityManager.find(PersistedMessage.class, routeQueue.getRouteQueueId()); |
124 | |
|
125 | 0 | if(persistedMessagePayload != null) { |
126 | 0 | entityManager.remove(persistedMessagePayload); |
127 | |
} |
128 | |
|
129 | 0 | if(persistedMessage != null) { |
130 | 0 | entityManager.remove(persistedMessage); |
131 | |
} |
132 | 0 | } |
133 | |
|
134 | |
|
135 | |
public void save(PersistedMessage routeQueue) { |
136 | 0 | if(routeQueue.getRouteQueueId() == null) { |
137 | 0 | entityManager.persist(routeQueue); |
138 | |
|
139 | 0 | routeQueue.getPayload().setRouteQueueId(routeQueue.getRouteQueueId()); |
140 | 0 | entityManager.persist(routeQueue.getPayload()); |
141 | |
} |
142 | |
else { |
143 | 0 | entityManager.merge(routeQueue); |
144 | 0 | entityManager.merge(routeQueue.getPayload()); |
145 | |
} |
146 | 0 | } |
147 | |
|
148 | |
public EntityManager getEntityManager() { |
149 | 0 | return this.entityManager; |
150 | |
} |
151 | |
|
152 | |
public void setEntityManager(EntityManager entityManager) { |
153 | 0 | this.entityManager = entityManager; |
154 | 0 | } |
155 | |
|
156 | |
} |