001    /*
002     * Copyright 2007-2008 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.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/ecl2.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 org.kuali.rice.ksb.messaging.bam.dao.impl;
017    
018    import java.util.List;
019    
020    import javax.persistence.EntityManager;
021    import javax.persistence.PersistenceContext;
022    import javax.xml.namespace.QName;
023    
024    import org.kuali.rice.core.reflect.ObjectDefinition;
025    import org.kuali.rice.ksb.messaging.bam.BAMTargetEntry;
026    import org.kuali.rice.ksb.messaging.bam.dao.BAMDAO;
027    
028    
029    public class BAMDaoJpaImpl implements BAMDAO {
030    
031        private EntityManager entityManager;
032    
033        @PersistenceContext
034        public void setEntityManager(EntityManager entityManager) {
035            this.entityManager = entityManager;
036        }
037    
038        public void clearBAMTables() {
039            entityManager.createQuery("delete bte from BAMTargetEntry bte").executeUpdate();
040            entityManager.createQuery("delete bp from BAMParam bp").executeUpdate();        
041        }
042    
043        public List<BAMTargetEntry> getCallsForService(QName serviceName, String methodName) {       
044            return (List<BAMTargetEntry>) entityManager.createQuery("select bte from BAMTargetEntry bte where bte.serviceName = :serviceName and bte.methodName = :methodName").setParameter("serviceName", serviceName.toString()).setParameter("methodName", methodName).getResultList();
045        }
046    
047        public void save(BAMTargetEntry bamEntry) {
048            if(bamEntry.getBamId() == null) {
049                entityManager.persist(bamEntry);
050            }
051            else {
052                entityManager.merge(bamEntry);
053            }
054        }
055    
056        public List<BAMTargetEntry> getCallsForService(QName serviceName) {
057            return (List<BAMTargetEntry>) entityManager.createQuery("select bte from BAMTargetEntry bte where bte.serviceName = :serviceName").setParameter("serviceName", serviceName.toString()).getResultList();
058        }
059    
060        public List<BAMTargetEntry> getCallsForRemotedClasses(ObjectDefinition objDef) {
061            QName qname = new QName(objDef.getServiceNamespace(), objDef.getClassName());
062            return (List<BAMTargetEntry>) entityManager.createQuery("select bte from BAMTargetEntry bte where bte.serviceName like :serviceName%").setParameter("serviceName", qname.toString()).getResultList();
063        }
064    
065        public List<BAMTargetEntry> getCallsForRemotedClasses(ObjectDefinition objDef, String methodName) {
066            QName qname = new QName(objDef.getServiceNamespace(), objDef.getClassName());
067            return (List<BAMTargetEntry>) entityManager.createQuery("select bte from BAMTargetEntry bte where bte.serviceName like :serviceName% and bte.methodName = :methodName").setParameter("serviceName", qname.toString()).setParameter("methodName", methodName).getResultList();
068        }
069    
070    }