1
2
3
4
5
6
7
8
9
10
11
12
13
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
30
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 }