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.api.reflect.ObjectDefinition;
025    import org.kuali.rice.core.api.reflect.ObjectDefinition;
026    import org.kuali.rice.ksb.messaging.bam.BAMTargetEntry;
027    import org.kuali.rice.ksb.messaging.bam.dao.BAMDAO;
028    
029    
030    public class BAMDaoJpaImpl implements BAMDAO {
031    
032        private EntityManager entityManager;
033    
034        @PersistenceContext
035        public void setEntityManager(EntityManager entityManager) {
036            this.entityManager = entityManager;
037        }
038    
039        public void clearBAMTables() {
040            entityManager.createQuery("delete from BAMTargetEntry").executeUpdate();
041            entityManager.createQuery("delete from BAMParam").executeUpdate();        
042        }
043    
044        public List<BAMTargetEntry> getCallsForService(QName serviceName, String methodName) {       
045            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();
046        }
047    
048        public void save(BAMTargetEntry bamEntry) {
049            if(bamEntry.getBamId() == null) {
050                entityManager.persist(bamEntry);
051            }
052            else {
053                entityManager.merge(bamEntry);
054            }
055        }
056    
057        public List<BAMTargetEntry> getCallsForService(QName serviceName) {
058            return (List<BAMTargetEntry>) entityManager.createQuery("select bte from BAMTargetEntry bte where bte.serviceName = :serviceName").setParameter("serviceName", serviceName.toString()).getResultList();
059        }
060    
061        public List<BAMTargetEntry> getCallsForRemotedClasses(ObjectDefinition objDef) {
062            QName qname = new QName(objDef.getApplicationId(), objDef.getClassName());
063            return (List<BAMTargetEntry>) entityManager.createQuery("select bte from BAMTargetEntry bte where bte.serviceName like :serviceName%").setParameter("serviceName", qname.toString()).getResultList();
064        }
065    
066        public List<BAMTargetEntry> getCallsForRemotedClasses(ObjectDefinition objDef, String methodName) {
067            QName qname = new QName(objDef.getApplicationId(), objDef.getClassName());
068            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();
069        }
070    
071    }