View Javadoc

1   /**
2    * Copyright 2005-2013 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.edl.impl.dao.impl;
17  
18  import java.util.ArrayList;
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.edl.impl.bo.EDocLiteAssociation;
28  import org.kuali.rice.edl.impl.bo.EDocLiteDefinition;
29  import org.kuali.rice.edl.impl.dao.EDocLiteDAO;
30  
31  /**
32   * JPA implementation of the EDOcLiteDAO
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public class EDocLiteDAOJpaImpl implements EDocLiteDAO {
37      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EDocLiteDAOJpaImpl.class);
38  
39      private static final String ACTIVE_IND_CRITERIA = "activeInd";
40      private static final String NAME_CRITERIA = "name";
41  
42      @PersistenceContext(unitName = "kew-unit")
43      private EntityManager entityManager;
44  
45      /**
46       * Save a EDocLiteDefinition
47       *
48       * @see org.kuali.rice.edl.impl.dao.EDocLiteDAO#saveEDocLiteDefinition(org.kuali.rice.edl.impl.bo.EDocLiteDefinition)
49       */
50      public void saveEDocLiteDefinition(final EDocLiteDefinition edocLiteData) {
51          if (edocLiteData.getEDocLiteDefId() == null) {
52              entityManager.persist(edocLiteData);
53          } else {
54              OrmUtils.merge(entityManager, edocLiteData);
55          }
56      }
57  
58      /**
59       * Save a EDocLiteAssocitaion
60       *
61       * @see org.kuali.rice.edl.impl.dao.EDocLiteDAO#saveEDocLiteAssociation(org.kuali.rice.edl.impl.bo.EDocLiteAssociation)
62       */
63      public void saveEDocLiteAssociation(final EDocLiteAssociation assoc) {
64          if (assoc.getEdocLiteAssocId() == null) {
65              entityManager.persist(assoc);
66          } else {
67              OrmUtils.merge(entityManager, assoc);
68          }
69      }
70  
71      /**
72       * Get a EDocLiteDefinition
73       *
74       * @see org.kuali.rice.edl.impl.dao.EDocLiteDAO#getEDocLiteDefinition(java.lang.String)
75       */
76      public EDocLiteDefinition getEDocLiteDefinition(final String defName) {
77          final Criteria crit = new Criteria(EDocLiteDefinition.class.getName());
78          crit.eq(NAME_CRITERIA, defName);
79          crit.eq(ACTIVE_IND_CRITERIA, Boolean.TRUE);
80          return (EDocLiteDefinition) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
81      }
82  
83      /**
84       * Get a EDocLiteAssociation
85       *
86       * @see org.kuali.rice.edl.impl.dao.EDocLiteDAO#getEDocLiteAssociation(java.lang.String)
87       */
88      public EDocLiteAssociation getEDocLiteAssociation(final String docTypeName) {
89          final Criteria crit = new Criteria(EDocLiteAssociation.class.getName());
90          crit.eq("edlName", docTypeName);
91          crit.eq(ACTIVE_IND_CRITERIA, Boolean.TRUE);
92          return (EDocLiteAssociation) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
93      }
94  
95      /**
96       * Returns the names of all active Definitions
97       *
98       * @see org.kuali.rice.edl.impl.dao.EDocLiteDAO#getEDocLiteDefinitions()
99       */
100     @SuppressWarnings("unchecked")
101     public List<String> getEDocLiteDefinitions() {
102         final Criteria crit = new Criteria(EDocLiteDefinition.class.getName());
103         crit.eq(ACTIVE_IND_CRITERIA, Boolean.TRUE);
104 
105         final List<EDocLiteDefinition> defs = (List<EDocLiteDefinition>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
106         final ArrayList<String> names = new ArrayList<String>(defs.size());
107         for (EDocLiteDefinition def : defs) {
108             names.add(def.getName());
109         }
110         return names;
111     }
112 
113     /**
114      * Returns all active Definitions
115      *
116      * @see org.kuali.rice.edl.impl.dao.EDocLiteDAO#getEDocLiteAssociations()
117      */
118     @SuppressWarnings("unchecked")
119     public List<EDocLiteAssociation> getEDocLiteAssociations() {
120         final Criteria crit = new Criteria(EDocLiteAssociation.class.getName());
121         crit.eq(ACTIVE_IND_CRITERIA, Boolean.TRUE);
122         return (List<EDocLiteAssociation>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
123     }
124 
125     /**
126      * Finds matching Associations
127      *
128      * @see org.kuali.rice.edl.impl.dao.EDocLiteDAO#search(org.kuali.rice.edl.impl.bo.EDocLiteAssociation)
129      */
130     @SuppressWarnings("unchecked")
131     public List<EDocLiteAssociation> search(final EDocLiteAssociation edocLite) {
132         final Criteria crit = new Criteria(EDocLiteAssociation.class.getName());
133         if (edocLite.getActiveInd() != null) {
134             crit.eq(ACTIVE_IND_CRITERIA, edocLite.getActiveInd());
135         }
136         if (edocLite.getDefinition() != null) {
137             crit.like("UPPER(definition)", "%" + edocLite.getDefinition().toUpperCase() + "%");
138         }
139         if (edocLite.getEdlName() != null) {
140             crit.like("UPPER(edlName)", "%" + edocLite.getEdlName().toUpperCase() + "%");
141         }
142         if (edocLite.getStyle() != null) {
143             crit.like("UPPER(style)", "%" + edocLite.getStyle().toUpperCase() + "%");
144         }
145         return (List<EDocLiteAssociation>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
146     }
147 
148     /**
149      * Returns a specific Association
150      *
151      * @see org.kuali.rice.edl.impl.dao.EDocLiteDAO#getEDocLiteAssociation(java.lang.Long)
152      */
153     public EDocLiteAssociation getEDocLiteAssociation(final Long associationId) {
154         final Criteria crit = new Criteria(EDocLiteAssociation.class.getName());
155         crit.eq("edocLiteAssocId", associationId);
156         return (EDocLiteAssociation) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
157     }
158 
159     public EntityManager getEntityManager() {
160         return this.entityManager;
161     }
162 
163     public void setEntityManager(EntityManager entityManager) {
164         this.entityManager = entityManager;
165     }
166 }