View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.rule.dao.impl;
18  
19  import java.util.List;
20  
21  import javax.persistence.EntityManager;
22  import javax.persistence.PersistenceContext;
23  
24  import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
25  import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
26  import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
27  import org.kuali.rice.kew.rule.bo.RuleAttribute;
28  import org.kuali.rice.kew.rule.dao.RuleAttributeDAO;
29  
30  
31  public class RuleAttributeDAOJpaImpl implements RuleAttributeDAO {
32  
33      @PersistenceContext
34      private EntityManager entityManager;
35  
36      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleAttributeDAOJpaImpl.class);
37     
38      /**
39  	 * @return the entityManager
40  	 */
41  	public EntityManager getEntityManager() {
42  		return this.entityManager;
43  	}
44  
45  	/**
46  	 * @param entityManager the entityManager to set
47  	 */
48  	public void setEntityManager(EntityManager entityManager) {
49  		this.entityManager = entityManager;
50  	}
51  
52  	public void save(RuleAttribute ruleAttribute) {
53          if (ruleAttribute.getRuleAttributeId() == null) {
54              entityManager.persist(ruleAttribute);
55          } else {
56              OrmUtils.merge(entityManager, ruleAttribute);
57          }
58      }
59  
60      public void delete(Long ruleAttributeId) {
61          entityManager.remove(findByRuleAttributeId(ruleAttributeId));
62      }
63  
64      public RuleAttribute findByRuleAttributeId(Long ruleAttributeId) {
65          return (RuleAttribute) entityManager.createNamedQuery("RuleAttribute.FindById").setParameter("ruleAttributeId", ruleAttributeId).getSingleResult();
66      }
67  
68      public List findByRuleAttribute(RuleAttribute ruleAttribute) {
69          Criteria crit = new Criteria("RuleAttribute", "ra");
70  
71          if (ruleAttribute.getName() != null) {
72              crit.rawJpql("UPPER(RULE_ATTRIB_NM) like '" + ruleAttribute.getName().toUpperCase() + "'");
73          }
74  
75          if (ruleAttribute.getClassName() != null) {
76              crit.rawJpql("UPPER(RULE_ATTRIB_CLS_NM) like '" + ruleAttribute.getClassName().toUpperCase() + "'");
77          }
78          if (ruleAttribute.getType() != null) {
79              crit.rawJpql("UPPER(RULE_ATTRIB_TYP) like '" + ruleAttribute.getType().toUpperCase() + "'");
80          }
81          return new QueryByCriteria(entityManager, crit).toQuery().getResultList();
82  
83      }
84  
85      public List getAllRuleAttributes() {
86          return  entityManager.createNamedQuery("RuleAttribute.GetAllRuleAttributes").getResultList();
87      }
88  
89      public RuleAttribute findByName(String name) {
90          LOG.debug("findByName name=" + name);
91          return (RuleAttribute) entityManager.createNamedQuery("RuleAttribute.FindByName").setParameter("name", name).getSingleResult();
92      }
93  
94      public RuleAttribute findByClassName(String classname) {
95          LOG.debug("findByClassName classname=" + classname);
96  
97          //FIXME: This query is returning multiple rows, which one should it return
98          List<RuleAttribute> ruleAttributes = entityManager.createNamedQuery("RuleAttribute.FindByClassName").setParameter("className", classname).getResultList();
99  
100         return (ruleAttributes.size() > 0 ? ruleAttributes.get(0) : null); 
101     }
102 
103 }