1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.mobility.emergencyinfo.dao;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.persistence.EntityManager;
22 import javax.persistence.NoResultException;
23 import javax.persistence.OptimisticLockException;
24 import javax.persistence.PersistenceContext;
25 import javax.persistence.Query;
26
27 import org.apache.log4j.Logger;
28 import org.kuali.mobility.emergencyinfo.entity.EmergencyInfo;
29
30 import org.kuali.mobility.emergencyinfo.entity.EmergencyInfoJPAImpl;
31 import org.springframework.stereotype.Repository;
32 import org.springframework.transaction.annotation.Transactional;
33
34 @Repository
35 public class EmergencyInfoDaoImpl implements EmergencyInfoDao {
36
37 private static final Logger LOG = Logger.getLogger(EmergencyInfoDaoImpl.class);
38
39 @PersistenceContext
40 private EntityManager entityManager;
41
42 @SuppressWarnings("unchecked")
43 public List<EmergencyInfoJPAImpl> findAllEmergencyInfo() {
44 List<EmergencyInfoJPAImpl> contacts = new ArrayList<EmergencyInfoJPAImpl>();
45 Query query = entityManager.createNamedQuery("EmergencyInfo.findAll");
46 try {
47 contacts = query.getResultList();
48 } catch (NoResultException e) {
49 LOG.error("No results found in Emergency Info table.", e);
50 }
51 return contacts;
52 }
53
54 public EmergencyInfo findEmergencyInfoById(Long id) {
55 EmergencyInfo contact = null;
56 Query query = entityManager.createNamedQuery("EmergencyInfo.findById");
57 query.setParameter("id", id);
58 try {
59 contact = (EmergencyInfoJPAImpl) query.getSingleResult();
60 } catch (NoResultException e) {
61 LOG.error("No emergency info found for id=" + id, e);
62 }
63 return contact;
64 }
65
66 @SuppressWarnings("unchecked")
67 public List<? extends EmergencyInfo> findAllEmergencyInfoByCampus(String campus) {
68 List<? extends EmergencyInfo> contacts = new ArrayList<EmergencyInfoJPAImpl>();
69 Query query = entityManager.createNamedQuery("EmergencyInfo.findByCampus");
70 query.setParameter("campus", campus);
71 try {
72 contacts = query.getResultList();
73 } catch (NoResultException e) {
74 LOG.error("No results found in Emergency Info table for campus=" + campus, e);
75 }
76 return contacts;
77 }
78
79 @Transactional
80 public Long saveEmergencyInfo(EmergencyInfo emergencyInfo) {
81 Long id = null;
82 if (emergencyInfo != null) {
83 LOG.debug("BEGIN: Save emergency info for " + emergencyInfo.getTitle());
84 try {
85 if (emergencyInfo.getEmergencyInfoId() == null) {
86 entityManager.persist(emergencyInfo);
87 } else {
88 entityManager.merge(emergencyInfo);
89 }
90 id = emergencyInfo.getEmergencyInfoId();
91 } catch (OptimisticLockException oe) {
92 LOG.error("Unable to save emergency info for " + emergencyInfo.getTitle(), oe);
93 id = null;
94 }
95 LOG.debug("END: Save emergency info for " + emergencyInfo.getTitle());
96 }
97 return id;
98 }
99
100 @Transactional
101 public void deleteEmergencyInfoById(Long id) {
102 if (null != id) {
103 Query query = entityManager.createNamedQuery("EmergencyInfo.deleteById");
104 query.setParameter("id", id);
105 query.executeUpdate();
106 }
107 }
108
109 @Transactional
110 public void reorder(Long id, boolean up) {
111 List<? extends EmergencyInfo> list = findAllEmergencyInfo();
112 EmergencyInfo last = null;
113 boolean flag = false;
114 int index = -1;
115 int count = list.get(0).getOrder();
116 for (EmergencyInfo emergencyInfo : list) {
117 index++;
118 if (emergencyInfo.getEmergencyInfoId().equals(id)) {
119 if (up && last != null) {
120 swap(last, emergencyInfo);
121 count = last.getOrder() + 1;
122 continue;
123 } else if (!up) {
124 EmergencyInfo next = list.get(index + 1);
125 swap(emergencyInfo, next);
126 count = next.getOrder() + 1;
127 continue;
128 }
129 flag = true;
130 }
131 if (flag) {
132 emergencyInfo.setOrder(count);
133 entityManager.merge(emergencyInfo);
134 }
135 count++;
136 last = emergencyInfo;
137 }
138 }
139
140 @Transactional
141 private void swap(EmergencyInfo one, EmergencyInfo two) {
142 int temp = one.getOrder();
143 one.setOrder(two.getOrder());
144 two.setOrder(temp);
145 entityManager.merge(one);
146 entityManager.merge(two);
147 }
148
149 public EntityManager getEntityManager() {
150 return entityManager;
151 }
152
153 public void setEntityManager(EntityManager entityManager) {
154 this.entityManager = entityManager;
155 }
156
157 }