View Javadoc

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