Coverage Report - org.kuali.rice.ksb.messaging.dao.impl.MessageQueueDAOJpaImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageQueueDAOJpaImpl
0%
0/56
0%
0/20
2
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.ksb.messaging.dao.impl;
 17  
 
 18  
 import org.kuali.rice.core.api.config.CoreConfigHelper;
 19  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 20  
 import org.kuali.rice.core.api.util.RiceUtilities;
 21  
 import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
 22  
 import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
 23  
 import org.kuali.rice.ksb.messaging.PersistedMessageBO;
 24  
 import org.kuali.rice.ksb.messaging.PersistedMessagePayload;
 25  
 import org.kuali.rice.ksb.messaging.dao.MessageQueueDAO;
 26  
 import org.kuali.rice.ksb.util.KSBConstants;
 27  
 
 28  
 import javax.persistence.EntityManager;
 29  
 import javax.persistence.PersistenceContext;
 30  
 import javax.persistence.Query;
 31  
 import javax.xml.namespace.QName;
 32  
 import java.util.List;
 33  
 import java.util.Map;
 34  
 
 35  
 
 36  0
 public class MessageQueueDAOJpaImpl implements MessageQueueDAO {
 37  
     
 38  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MessageQueueDAOJpaImpl.class);
 39  
     
 40  
     @PersistenceContext
 41  
     private EntityManager entityManager;
 42  
     
 43  
     @SuppressWarnings("unchecked")
 44  
     public List<PersistedMessageBO> findAll() {
 45  0
         if (LOG.isDebugEnabled()) {
 46  0
             LOG.debug("Returning all persisted messages");
 47  
         }
 48  
         
 49  0
         Query query = entityManager.createNamedQuery("PersistedMessageBO.FindAll");
 50  0
         return (List<PersistedMessageBO>) query.getResultList();
 51  
     }
 52  
 
 53  
     @SuppressWarnings("unchecked")
 54  
     public List<PersistedMessageBO> findAll(int maxRows) {
 55  0
         if (LOG.isDebugEnabled()) {
 56  0
             LOG.debug("Finding next " + maxRows + " messages");
 57  
         }
 58  
         
 59  0
         Query query = entityManager.createNamedQuery("PersistedMessageBO.FindAll");
 60  0
         query.setMaxResults(maxRows);
 61  
         
 62  0
         return (List<PersistedMessageBO>) query.getResultList();
 63  
     }
 64  
 
 65  
     
 66  
     public PersistedMessagePayload findByPersistedMessageByRouteQueueId(Long routeQueueId) {
 67  0
         return (PersistedMessagePayload) entityManager.find(PersistedMessagePayload.class, routeQueueId);
 68  
     }
 69  
 
 70  
     
 71  
     public PersistedMessageBO findByRouteQueueId(Long routeQueueId) {
 72  0
         return (PersistedMessageBO) entityManager.find(PersistedMessageBO.class, routeQueueId);
 73  
     }
 74  
 
 75  
     @SuppressWarnings("unchecked")
 76  
     public List<PersistedMessageBO> findByServiceName(QName serviceName, String methodName) {
 77  0
         if (LOG.isDebugEnabled()) {
 78  0
             LOG.debug("Finding messages for service name " + serviceName);
 79  
         }
 80  
         
 81  0
         Query query = entityManager.createNamedQuery("PersistedMessageBO.FindByServiceName");
 82  0
         query.setParameter("serviceName", serviceName.toString());
 83  0
         query.setParameter("methodName", methodName);
 84  
         
 85  0
         return (List<PersistedMessageBO>) query.getResultList();
 86  
     }
 87  
 
 88  
     @SuppressWarnings("unchecked")
 89  
     public List<PersistedMessageBO> findByValues(Map<String, String> criteriaValues, int maxRows) {
 90  0
         Criteria criteria = new Criteria(PersistedMessageBO.class.getName());
 91  0
         for(Map.Entry<String, String> entry : criteriaValues.entrySet()) {
 92  0
             criteria.eq(entry.getKey(), entry.getValue());
 93  
         }
 94  
         
 95  0
         QueryByCriteria query = new QueryByCriteria(entityManager, criteria);
 96  
         
 97  0
         return query.toQuery().getResultList();
 98  
     }
 99  
 
 100  
     @SuppressWarnings("unchecked")
 101  
     public List<PersistedMessageBO> getNextDocuments(Integer maxDocuments) {
 102  0
         String applicationId = CoreConfigHelper.getApplicationId();
 103  
         
 104  0
         Query query = entityManager.createNamedQuery("PersistedMessageBO.GetNextDocuments");
 105  0
         query.setParameter("applicationId", applicationId);
 106  0
         query.setParameter("queueStatus", KSBConstants.ROUTE_QUEUE_EXCEPTION);
 107  0
         query.setParameter("ipNumber", RiceUtilities.getIpNumber());
 108  
         
 109  0
         if (maxDocuments != null)
 110  0
             query.setMaxResults(maxDocuments);
 111  
         
 112  0
         return (List<PersistedMessageBO>) query.getResultList();
 113  
     }
 114  
 
 115  
     
 116  
     public void remove(PersistedMessageBO routeQueue) {
 117  0
         if (LOG.isDebugEnabled()) {
 118  0
             LOG.debug("Removing message " + routeQueue);
 119  
         }
 120  0
         if (routeQueue.getRouteQueueId() == null) {
 121  0
             throw new RiceRuntimeException("can't delete a PersistedMessageBO with no id");
 122  
         }
 123  
         
 124  0
         routeQueue = entityManager.merge(routeQueue);
 125  0
         entityManager.remove(routeQueue);
 126  
 
 127  0
         if (routeQueue.getPayload() != null) {
 128  0
             PersistedMessagePayload payload =  entityManager.merge(routeQueue.getPayload());
 129  0
             entityManager.remove(payload);
 130  
         }
 131  0
     }
 132  
 
 133  
     
 134  
     public void save(PersistedMessageBO routeQueue) {
 135  0
         if (LOG.isDebugEnabled()) {
 136  0
             LOG.debug("Persisting message " + routeQueue);
 137  
         }
 138  0
         PersistedMessageBO jpaInstance = entityManager.merge(routeQueue);
 139  0
         Long routeQueueId = jpaInstance.getRouteQueueId();
 140  0
         Integer verNo = jpaInstance.getLockVerNbr();
 141  0
         routeQueue.setRouteQueueId(routeQueueId);
 142  0
         routeQueue.setLockVerNbr(verNo);
 143  
 
 144  0
         if (routeQueue.getPayload() != null) {
 145  0
             routeQueue.getPayload().setRouteQueueId(routeQueueId);
 146  0
             entityManager.merge(routeQueue.getPayload());
 147  
         }
 148  0
     }
 149  
 
 150  
     public EntityManager getEntityManager() {
 151  0
         return this.entityManager;
 152  
     }
 153  
 
 154  
     public void setEntityManager(EntityManager entityManager) {
 155  0
         this.entityManager = entityManager;
 156  0
     }
 157  
 
 158  
 }