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