001    /**
002     * Copyright 2005-2011 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.kew.rule.dao.impl;
017    
018    import java.util.List;
019    
020    import javax.persistence.EntityManager;
021    import javax.persistence.PersistenceContext;
022    
023    import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
024    import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
025    import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
026    import org.kuali.rice.kew.rule.bo.RuleAttribute;
027    import org.kuali.rice.kew.rule.dao.RuleAttributeDAO;
028    
029    
030    public class RuleAttributeDAOJpaImpl implements RuleAttributeDAO {
031    
032        @PersistenceContext
033        private EntityManager entityManager;
034    
035        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleAttributeDAOJpaImpl.class);
036       
037        /**
038             * @return the entityManager
039             */
040            public EntityManager getEntityManager() {
041                    return this.entityManager;
042            }
043    
044            /**
045             * @param entityManager the entityManager to set
046             */
047            public void setEntityManager(EntityManager entityManager) {
048                    this.entityManager = entityManager;
049            }
050    
051            public void save(RuleAttribute ruleAttribute) {
052            if (ruleAttribute.getId() == null) {
053                entityManager.persist(ruleAttribute);
054            } else {
055                OrmUtils.merge(entityManager, ruleAttribute);
056            }
057        }
058    
059        public void delete(String ruleAttributeId) {
060            entityManager.remove(findByRuleAttributeId(ruleAttributeId));
061        }
062    
063        public RuleAttribute findByRuleAttributeId(String ruleAttributeId) {
064            return (RuleAttribute) entityManager.createNamedQuery("RuleAttribute.FindById").setParameter("ruleAttributeId", ruleAttributeId).getSingleResult();
065        }
066    
067        public List<RuleAttribute> findByRuleAttribute(RuleAttribute ruleAttribute) {
068            Criteria crit = new Criteria("RuleAttribute", "ra");
069    
070            if (ruleAttribute.getName() != null) {
071                crit.rawJpql("UPPER(RULE_ATTRIB_NM) like '" + ruleAttribute.getName().toUpperCase() + "'");
072            }
073    
074            if (ruleAttribute.getResourceDescriptor() != null) {
075                crit.rawJpql("UPPER(RULE_ATTRIB_CLS_NM) like '" + ruleAttribute.getResourceDescriptor().toUpperCase() + "'");
076            }
077            if (ruleAttribute.getType() != null) {
078                crit.rawJpql("UPPER(RULE_ATTRIB_TYP) like '" + ruleAttribute.getType().toUpperCase() + "'");
079            }
080            return new QueryByCriteria(entityManager, crit).toQuery().getResultList();
081    
082        }
083    
084        public List<RuleAttribute> getAllRuleAttributes() {
085            return  entityManager.createNamedQuery("RuleAttribute.GetAllRuleAttributes").getResultList();
086        }
087    
088        public RuleAttribute findByName(String name) {
089            LOG.debug("findByName name=" + name);
090            return (RuleAttribute) entityManager.createNamedQuery("RuleAttribute.FindByName").setParameter("name", name).getSingleResult();
091        }
092    
093        public RuleAttribute findByClassName(String classname) {
094            LOG.debug("findByClassName classname=" + classname);
095    
096            //FIXME: This query is returning multiple rows, which one should it return
097            List<RuleAttribute> ruleAttributes = entityManager.createNamedQuery("RuleAttribute.FindByClassName").setParameter("resourceDescriptor", classname).getResultList();
098    
099            return (ruleAttributes.size() > 0 ? ruleAttributes.get(0) : null); 
100        }
101    
102    }