1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.ksb.messaging.dao.impl; |
18 | |
|
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.apache.ojb.broker.query.Criteria; |
21 | |
import org.apache.ojb.broker.query.QueryByCriteria; |
22 | |
import org.kuali.rice.core.api.config.CoreConfigHelper; |
23 | |
import org.kuali.rice.core.api.util.RiceUtilities; |
24 | |
import org.kuali.rice.ksb.messaging.PersistedMessageBO; |
25 | |
import org.kuali.rice.ksb.messaging.PersistedMessagePayload; |
26 | |
import org.kuali.rice.ksb.messaging.dao.MessageQueueDAO; |
27 | |
import org.kuali.rice.ksb.util.KSBConstants; |
28 | |
import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport; |
29 | |
|
30 | |
import javax.xml.namespace.QName; |
31 | |
import java.util.List; |
32 | |
import java.util.Map; |
33 | |
|
34 | |
|
35 | 0 | public class MessageQueueDAOOjbImpl extends PersistenceBrokerDaoSupport implements MessageQueueDAO { |
36 | |
|
37 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MessageQueueDAOOjbImpl.class); |
38 | |
|
39 | |
public void remove(PersistedMessageBO routeQueue) { |
40 | 0 | if (LOG.isDebugEnabled()) { |
41 | 0 | LOG.debug("Removing message " + routeQueue); |
42 | |
} |
43 | 0 | Criteria crit = new Criteria(); |
44 | 0 | crit.addEqualTo("routeQueueId", routeQueue.getRouteQueueId()); |
45 | 0 | getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(PersistedMessageBO.class, crit)); |
46 | |
|
47 | 0 | crit = new Criteria(); |
48 | 0 | crit.addEqualTo("routeQueueId", routeQueue.getPayload().getRouteQueueId()); |
49 | 0 | getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(PersistedMessagePayload.class, crit)); |
50 | 0 | } |
51 | |
|
52 | |
public void save(PersistedMessageBO routeQueue) { |
53 | 0 | if (LOG.isDebugEnabled()) { |
54 | 0 | LOG.debug("Persisting message " + routeQueue); |
55 | |
} |
56 | 0 | getPersistenceBrokerTemplate().store(routeQueue); |
57 | 0 | routeQueue.getPayload().setRouteQueueId(routeQueue.getRouteQueueId()); |
58 | 0 | getPersistenceBrokerTemplate().store(routeQueue.getPayload()); |
59 | 0 | } |
60 | |
|
61 | |
@SuppressWarnings("unchecked") |
62 | |
public List<PersistedMessageBO> findAll() { |
63 | 0 | if (LOG.isDebugEnabled()) { |
64 | 0 | LOG.debug("Returning all persisted messages"); |
65 | |
} |
66 | 0 | return (List<PersistedMessageBO>) getPersistenceBrokerTemplate().getCollectionByQuery( |
67 | |
new QueryByCriteria(PersistedMessageBO.class)); |
68 | |
} |
69 | |
|
70 | |
@SuppressWarnings("unchecked") |
71 | |
public List<PersistedMessageBO> findAll(int maxRows) { |
72 | 0 | if (LOG.isDebugEnabled()) { |
73 | 0 | LOG.debug("Finding next " + maxRows + " messages"); |
74 | |
} |
75 | 0 | QueryByCriteria query = new QueryByCriteria(PersistedMessageBO.class); |
76 | 0 | query.setStartAtIndex(0); |
77 | 0 | query.setEndAtIndex(maxRows); |
78 | 0 | return (List<PersistedMessageBO>) getPersistenceBrokerTemplate().getCollectionByQuery(query); |
79 | |
} |
80 | |
|
81 | |
@SuppressWarnings("unchecked") |
82 | |
public List<PersistedMessageBO> findByValues(Map<String, String> criteriaValues, int maxRows) { |
83 | 0 | Criteria crit = new Criteria(); |
84 | 0 | String value = null; |
85 | 0 | for (String key : criteriaValues.keySet()) { |
86 | 0 | value = criteriaValues.get(key); |
87 | 0 | if (StringUtils.isBlank(key) && StringUtils.isBlank(value)) { |
88 | 0 | throw new IllegalArgumentException("Either the key or value was blank in criteriaValues (" + key + "=" |
89 | |
+ value + ")"); |
90 | |
} |
91 | |
|
92 | |
|
93 | 0 | if (!key.equals("routeQueueId")) { |
94 | 0 | if (value.contains("*")) { |
95 | 0 | value = value.replace("*", "%"); |
96 | |
} else { |
97 | 0 | value = value.concat("%"); |
98 | |
} |
99 | |
} |
100 | 0 | if (!StringUtils.containsOnly(value, "%")) { |
101 | 0 | crit.addLike(key, value); |
102 | |
} |
103 | |
} |
104 | 0 | QueryByCriteria query = new QueryByCriteria(PersistedMessageBO.class, crit); |
105 | 0 | query.setFetchSize(maxRows); |
106 | 0 | query.setStartAtIndex(0); |
107 | 0 | query.setEndAtIndex(maxRows); |
108 | 0 | return (List<PersistedMessageBO>) getPersistenceBrokerTemplate().getCollectionByQuery(query); |
109 | |
} |
110 | |
|
111 | |
public PersistedMessageBO findByRouteQueueId(Long routeQueueId) { |
112 | 0 | Criteria criteria = new Criteria(); |
113 | 0 | criteria.addEqualTo("routeQueueId", routeQueueId); |
114 | 0 | return (PersistedMessageBO) getPersistenceBrokerTemplate().getObjectByQuery( |
115 | |
new QueryByCriteria(PersistedMessageBO.class, criteria)); |
116 | |
} |
117 | |
|
118 | |
public PersistedMessagePayload findByPersistedMessageByRouteQueueId(Long routeQueueId) { |
119 | 0 | Criteria criteria = new Criteria(); |
120 | 0 | criteria.addEqualTo("routeQueueId", routeQueueId); |
121 | 0 | return (PersistedMessagePayload) getPersistenceBrokerTemplate().getObjectByQuery( |
122 | |
new QueryByCriteria(PersistedMessagePayload.class, criteria)); |
123 | |
} |
124 | |
|
125 | |
@SuppressWarnings("unchecked") |
126 | |
public List<PersistedMessageBO> getNextDocuments(Integer maxDocuments) { |
127 | 0 | Criteria crit = new Criteria(); |
128 | 0 | String applicationId = CoreConfigHelper.getApplicationId(); |
129 | 0 | crit.addEqualTo("applicationId", applicationId); |
130 | 0 | crit.addNotEqualTo("queueStatus", KSBConstants.ROUTE_QUEUE_EXCEPTION); |
131 | 0 | crit.addEqualTo("ipNumber", RiceUtilities.getIpNumber()); |
132 | |
|
133 | 0 | QueryByCriteria query = new QueryByCriteria(PersistedMessageBO.class, crit); |
134 | 0 | query.addOrderByAscending("queuePriority"); |
135 | 0 | query.addOrderByAscending("routeQueueId"); |
136 | 0 | query.addOrderByAscending("queueDate"); |
137 | 0 | if (maxDocuments != null) |
138 | 0 | query.setEndAtIndex(maxDocuments.intValue()); |
139 | 0 | return (List) getPersistenceBrokerTemplate().getCollectionByQuery(query); |
140 | |
} |
141 | |
|
142 | |
@SuppressWarnings("unchecked") |
143 | |
public List<PersistedMessageBO> findByServiceName(QName serviceName, String methodName) { |
144 | 0 | if (LOG.isDebugEnabled()) { |
145 | 0 | LOG.debug("Finding messages for service name " + serviceName); |
146 | |
} |
147 | 0 | Criteria crit = new Criteria(); |
148 | 0 | crit.addEqualTo("serviceName", serviceName.toString()); |
149 | 0 | crit.addEqualTo("methodName", methodName); |
150 | 0 | return (List<PersistedMessageBO>) getPersistenceBrokerTemplate().getCollectionByQuery( |
151 | |
new QueryByCriteria(PersistedMessageBO.class, crit)); |
152 | |
} |
153 | |
|
154 | |
} |