View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15   
16  package org.kuali.mobility.configparams.dao;
17  
18  import java.util.List;
19  
20  import javax.persistence.EntityManager;
21  import javax.persistence.OptimisticLockException;
22  import javax.persistence.PersistenceContext;
23  import javax.persistence.Query;
24  
25  import org.kuali.mobility.configparams.entity.ConfigParam;
26  import org.springframework.stereotype.Repository;
27  
28  /**
29   * DAO to actually perform manipulation of ConfigParam objects
30   * @author Kuali Mobility Team (moblitiy.collab@kuali.org)
31   */
32  @Repository
33  public class ConfigParamDaoImpl implements ConfigParamDao {
34  
35      @PersistenceContext
36      private EntityManager entityManager;
37      
38      public void deleteConfigParamById(Long id) {
39          Query query = entityManager.createQuery("delete from ConfigParam cp where cp.configParamId = :id");
40          query.setParameter("id", id);
41          query.executeUpdate();
42      }
43  
44      @SuppressWarnings("unchecked")
45      public List<ConfigParam> findAllConfigParam() {
46          Query query = entityManager.createQuery("select cp from ConfigParam cp order by cp.name");
47          return query.getResultList();
48      }
49  
50      public ConfigParam findConfigParamById(Long id) {
51          Query query = entityManager.createQuery("select cp from ConfigParam cp where cp.configParamId = :id");
52          query.setParameter("id", id);
53          return (ConfigParam) query.getSingleResult();
54      }
55      
56      public ConfigParam findConfigParamByName(String name) {
57          Query query = entityManager.createQuery("select cp from ConfigParam cp where cp.name like :name");
58          query.setParameter("name", name);
59          return (ConfigParam) query.getSingleResult();
60      }
61  
62      public Long saveConfigParam(ConfigParam configParam) {
63          if (configParam == null) {
64              return null;
65          }
66          if (configParam.getName() != null) {
67              configParam.setName(configParam.getName().trim());
68          }
69          if (configParam.getValue() != null) {
70              configParam.setValue(configParam.getValue().trim());
71          }
72          try {
73  	        if (configParam.getConfigParamId() == null) {
74  	            entityManager.persist(configParam);
75  	        } else {
76  	            entityManager.merge(configParam);
77  	        }
78          } catch (OptimisticLockException oe) {
79              return null;
80          }
81          return configParam.getConfigParamId();
82      }
83      
84      public EntityManager getEntityManager() {
85          return entityManager;
86      }
87  
88      public void setEntityManager(EntityManager entityManager) {
89          this.entityManager = entityManager;
90      }
91      
92  }