001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package edu.iu.uis.eden.messaging.dao;
017:
018: import java.util.List;
019: import java.util.Map;
020:
021: import javax.xml.namespace.QName;
022:
023: import org.apache.commons.lang.StringUtils;
024: import org.apache.ojb.broker.query.Criteria;
025: import org.apache.ojb.broker.query.QueryByCriteria;
026: import org.kuali.rice.RiceConstants;
027: import org.kuali.rice.core.Core;
028: import org.kuali.rice.util.RiceUtilities;
029: import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
030:
031: import edu.iu.uis.eden.messaging.PersistedMassagePayload;
032: import edu.iu.uis.eden.messaging.PersistedMessage;
033:
034: public class MessageQueueDAOOjbImpl extends PersistenceBrokerDaoSupport
035: implements MessageQueueDAO {
036:
037: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
038: .getLogger(MessageQueueDAOOjbImpl.class);
039:
040: public void remove(PersistedMessage routeQueue) {
041: if (LOG.isDebugEnabled()) {
042: LOG.debug("Removing message " + routeQueue);
043: }
044: Criteria crit = new Criteria();
045: crit.addEqualTo("routeQueueId", routeQueue.getRouteQueueId());
046: getPersistenceBrokerTemplate().deleteByQuery(
047: new QueryByCriteria(PersistedMessage.class, crit));
048:
049: crit = new Criteria();
050: crit.addEqualTo("routeQueueId", routeQueue.getPayload()
051: .getRouteQueueId());
052: getPersistenceBrokerTemplate()
053: .deleteByQuery(
054: new QueryByCriteria(
055: PersistedMassagePayload.class, crit));
056: }
057:
058: public void save(PersistedMessage routeQueue) {
059: if (LOG.isDebugEnabled()) {
060: LOG.debug("Persisting message " + routeQueue);
061: }
062: getPersistenceBrokerTemplate().store(routeQueue);
063: routeQueue.getPayload().setRouteQueueId(
064: routeQueue.getRouteQueueId());
065: getPersistenceBrokerTemplate().store(routeQueue.getPayload());
066: }
067:
068: @SuppressWarnings("unchecked")
069: public List<PersistedMessage> findAll() {
070: if (LOG.isDebugEnabled()) {
071: LOG.debug("Returning all persisted messages");
072: }
073: return (List<PersistedMessage>) getPersistenceBrokerTemplate()
074: .getCollectionByQuery(
075: new QueryByCriteria(PersistedMessage.class));
076: }
077:
078: @SuppressWarnings("unchecked")
079: public List<PersistedMessage> findAll(int maxRows) {
080: if (LOG.isDebugEnabled()) {
081: LOG.debug("Finding next " + maxRows + " messages");
082: }
083: QueryByCriteria query = new QueryByCriteria(
084: PersistedMessage.class);
085: query.setStartAtIndex(0);
086: query.setEndAtIndex(maxRows);
087: return (List<PersistedMessage>) getPersistenceBrokerTemplate()
088: .getCollectionByQuery(query);
089: }
090:
091: @SuppressWarnings("unchecked")
092: public List<PersistedMessage> findByValues(
093: Map<String, String> criteriaValues, int maxRows) {
094: Criteria crit = new Criteria();
095: String value = null;
096: for (String key : criteriaValues.keySet()) {
097: value = criteriaValues.get(key);
098: if (StringUtils.isBlank(key) && StringUtils.isBlank(value)) {
099: throw new IllegalArgumentException(
100: "Either the key or value was blank in criteriaValues ("
101: + key + "=" + value + ")");
102: }
103:
104: // auto-wildcard the statement
105: if (!key.equals("routeQueueId")) {
106: if (value.contains("*")) {
107: value = value.replace("*", "%");
108: } else {
109: value = value.concat("%");
110: }
111: }
112: if (!StringUtils.containsOnly(value, "%")) {
113: crit.addLike(key, value);
114: }
115: }
116: QueryByCriteria query = new QueryByCriteria(
117: PersistedMessage.class, crit);
118: query.setFetchSize(maxRows);
119: query.setStartAtIndex(0);
120: query.setEndAtIndex(maxRows);
121: return (List<PersistedMessage>) getPersistenceBrokerTemplate()
122: .getCollectionByQuery(query);
123: }
124:
125: public PersistedMessage findByRouteQueueId(Long routeQueueId) {
126: Criteria criteria = new Criteria();
127: criteria.addEqualTo("routeQueueId", routeQueueId);
128: return (PersistedMessage) getPersistenceBrokerTemplate()
129: .getObjectByQuery(
130: new QueryByCriteria(PersistedMessage.class,
131: criteria));
132: }
133:
134: public PersistedMassagePayload findByPersistedMessageByRouteQueueId(
135: Long routeQueueId) {
136: Criteria criteria = new Criteria();
137: criteria.addEqualTo("routeQueueId", routeQueueId);
138: return (PersistedMassagePayload) getPersistenceBrokerTemplate()
139: .getObjectByQuery(
140: new QueryByCriteria(
141: PersistedMassagePayload.class, criteria));
142: }
143:
144: @SuppressWarnings("unchecked")
145: public List<PersistedMessage> getNextDocuments(Integer maxDocuments) {
146: Criteria crit = new Criteria();
147: String messagingEntity = Core.getCurrentContextConfig()
148: .getMessageEntity();
149: crit.addEqualTo("messageEntity", messagingEntity);
150: crit.addNotEqualTo("queueStatus",
151: RiceConstants.ROUTE_QUEUE_EXCEPTION);
152: crit.addEqualTo("ipNumber", RiceUtilities.getIpNumber());
153:
154: QueryByCriteria query = new QueryByCriteria(
155: PersistedMessage.class, crit);
156: query.addOrderByAscending("queuePriority");
157: query.addOrderByAscending("routeQueueId");
158: query.addOrderByAscending("queueDate");
159: if (maxDocuments != null)
160: query.setEndAtIndex(maxDocuments.intValue());
161: return (List) getPersistenceBrokerTemplate()
162: .getCollectionByQuery(query);
163: }
164:
165: @SuppressWarnings("unchecked")
166: public List<PersistedMessage> findByServiceName(QName serviceName,
167: String methodName) {
168: if (LOG.isDebugEnabled()) {
169: LOG.debug("Finding messages for service name "
170: + serviceName);
171: }
172: Criteria crit = new Criteria();
173: crit.addEqualTo("serviceName", serviceName.toString());
174: crit.addEqualTo("methodName", methodName);
175: return (List<PersistedMessage>) getPersistenceBrokerTemplate()
176: .getCollectionByQuery(
177: new QueryByCriteria(PersistedMessage.class,
178: crit));
179: }
180:
181: }
|