/*
 * Copyright 2007-2008 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl2.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.rice.ksb.messaging.dao.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.xml.namespace.QName;

import org.kuali.rice.core.config.ConfigContext;
import org.kuali.rice.core.exception.RiceRuntimeException;
import org.kuali.rice.core.jpa.criteria.Criteria;
import org.kuali.rice.core.jpa.criteria.QueryByCriteria;
import org.kuali.rice.core.util.OrmUtils;
import org.kuali.rice.core.util.RiceUtilities;
import org.kuali.rice.ksb.messaging.PersistedMessage;
import org.kuali.rice.ksb.messaging.PersistedMessagePayload;
import org.kuali.rice.ksb.messaging.dao.MessageQueueDAO;
import org.kuali.rice.ksb.util.KSBConstants;


public class MessageQueueDAOJpaImpl implements MessageQueueDAO {
    
    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MessageQueueDAOJpaImpl.class);
    
    @PersistenceContext
    private EntityManager entityManager;
    
    @SuppressWarnings("unchecked")
    public List<PersistedMessage> findAll() {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Returning all persisted messages");
        }
        
        Query query = entityManager.createNamedQuery("PersistedMessage.FindAll");
        return (List<PersistedMessage>) query.getResultList();
    }

    @SuppressWarnings("unchecked")
    public List<PersistedMessage> findAll(int maxRows) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finding next " + maxRows + " messages");
        }
        
        Query query = entityManager.createNamedQuery("PersistedMessage.FindAll");
        query.setMaxResults(maxRows);
        
        return (List<PersistedMessage>) query.getResultList();
    }

    
    public PersistedMessagePayload findByPersistedMessageByRouteQueueId(Long routeQueueId) {
        return (PersistedMessagePayload) entityManager.find(PersistedMessagePayload.class, routeQueueId);
    }

    
    public PersistedMessage findByRouteQueueId(Long routeQueueId) {
        return (PersistedMessage) entityManager.find(PersistedMessage.class, routeQueueId);
    }

    @SuppressWarnings("unchecked")
    public List<PersistedMessage> findByServiceName(QName serviceName, String methodName) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finding messages for service name " + serviceName);
        }
        
        Query query = entityManager.createNamedQuery("PersistedMessage.FindByServiceName");
        query.setParameter("serviceName", serviceName.toString());
        query.setParameter("methodName", methodName);
        
        return (List<PersistedMessage>) query.getResultList();
    }

    @SuppressWarnings("unchecked")
    public List<PersistedMessage> findByValues(Map<String, String> criteriaValues, int maxRows) {
        Criteria criteria = new Criteria(PersistedMessage.class.getName());
        for(Map.Entry<String, String> entry : criteriaValues.entrySet()) {
            criteria.eq(entry.getKey(), entry.getValue());
        }
        
        QueryByCriteria query = new QueryByCriteria(entityManager, criteria);
        
        return query.toQuery().getResultList();
    }

    @SuppressWarnings("unchecked")
    public List<PersistedMessage> getNextDocuments(Integer maxDocuments) {
        String serviceNamespace = ConfigContext.getCurrentContextConfig().getServiceNamespace();
        
        Query query = entityManager.createNamedQuery("PersistedMessage.GetNextDocuments");
        query.setParameter("serviceNamespace", serviceNamespace);
        query.setParameter("queueStatus", KSBConstants.ROUTE_QUEUE_EXCEPTION);
        query.setParameter("ipNumber", RiceUtilities.getIpNumber());
        
        if (maxDocuments != null)
            query.setMaxResults(maxDocuments);
        
        return (List<PersistedMessage>) query.getResultList();
    }

    
    public void remove(PersistedMessage routeQueue) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Removing message " + routeQueue);
        }
        if (routeQueue.getRouteQueueId() == null) {
            throw new RiceRuntimeException("can't delete a PersistedMessage with no id");
        }
        
        routeQueue = entityManager.merge(routeQueue);
        entityManager.remove(routeQueue);
        if (routeQueue.getPayload() != null) {
            PersistedMessagePayload payload =  entityManager.merge(routeQueue.getPayload());
            entityManager.remove(payload);
        }
        entityManager.flush();
    }

    
    public void save(PersistedMessage routeQueue) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Persisting message " + routeQueue);
        }
        PersistedMessage jpaInstance = entityManager.merge(routeQueue);
        Long routeQueueId = jpaInstance.getRouteQueueId();
        Integer verNo = jpaInstance.getLockVerNbr();
        routeQueue.setRouteQueueId(routeQueueId);
        routeQueue.setLockVerNbr(verNo);

        if (routeQueue.getPayload() != null) {
            routeQueue.getPayload().setRouteQueueId(routeQueueId);
            entityManager.merge(routeQueue.getPayload());
        }
    }

    public EntityManager getEntityManager() {
        return this.entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

}

@@ -4970,32 +4970,34 @@
    %7D%0D%0A        %0D%0A
+//
         routeQue
@@ -5027,32 +5027,34 @@
 e(routeQueue);%0D%0A
+//
         entityMa
@@ -5072,32 +5072,329 @@
 e(routeQueue);%0D%0A
+        // doing it this way to avoid optimistic lock exceptions.  But I'm not happy about it.%0D%0A        entityManager.createQuery(%22delete from %22 + routeQueue.getClass().getSimpleName() + %0D%0A                %22 where routeQueueId = ?1%22).setParameter(1, routeQueue.getRouteQueueId()).executeUpdate();%0D%0A
         if (rout
@@ -5559,24 +5559,24 @@
 (payload);%0D%0A
+
         %7D%0D%0A 
@@ -5578,40 +5578,8 @@
  %7D%0D%0A
-        entityManager.flush();%0D%0A
     

Make KSB unit tests work with JPA