1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.core.enumerationmanagement.dao.impl; |
17 | |
|
18 | |
import java.util.Date; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import javax.persistence.EntityManager; |
22 | |
import javax.persistence.PersistenceContext; |
23 | |
import javax.persistence.Query; |
24 | |
|
25 | |
import org.kuali.student.core.dao.impl.AbstractSearchableCrudDaoImpl; |
26 | |
import org.kuali.student.core.enumerationmanagement.EnumerationException; |
27 | |
import org.kuali.student.core.enumerationmanagement.dao.EnumerationManagementDAO; |
28 | |
import org.kuali.student.core.enumerationmanagement.entity.EnumeratedValue; |
29 | |
import org.kuali.student.core.enumerationmanagement.entity.Enumeration; |
30 | |
import org.slf4j.Logger; |
31 | |
import org.slf4j.LoggerFactory; |
32 | |
import org.springframework.stereotype.Repository; |
33 | |
|
34 | |
@Repository |
35 | 16 | public class EnumerationManagementDAOImpl extends AbstractSearchableCrudDaoImpl implements EnumerationManagementDAO { |
36 | |
|
37 | 2 | final static Logger logger = LoggerFactory.getLogger(EnumerationManagementDAOImpl.class); |
38 | |
|
39 | |
@PersistenceContext(unitName = "EnumerationManagement") |
40 | |
@Override |
41 | |
public void setEm(EntityManager em) { |
42 | 16 | super.setEm(em); |
43 | 16 | } |
44 | |
|
45 | |
public List<Enumeration> findEnumerations() { |
46 | |
List<Enumeration> metas; |
47 | |
try{ |
48 | 3 | Query query = em.createQuery("SELECT e FROM Enumeration e"); |
49 | |
@SuppressWarnings("unchecked") |
50 | 3 | List<Enumeration> resultList = (List<Enumeration>) query.getResultList(); |
51 | 3 | metas = resultList; |
52 | |
} |
53 | 0 | catch(Exception e){ |
54 | 0 | logger.error("findEnumerations query failed.", e); |
55 | 0 | throw new EnumerationException("findEnumerations query failed.", e); |
56 | 3 | } |
57 | 3 | return metas; |
58 | |
} |
59 | |
|
60 | |
public Enumeration addEnumeration(Enumeration entity){ |
61 | |
try{ |
62 | 8 | em.persist(entity); |
63 | 8 | entity = em.find(Enumeration.class, entity.getId()); |
64 | |
} |
65 | 0 | catch(Exception e){ |
66 | 0 | throw new EnumerationException("addEnumerationMeta query failed.", e); |
67 | 8 | } |
68 | 8 | return entity; |
69 | |
} |
70 | |
|
71 | |
public boolean removeEnumeration(String enumerationKey){ |
72 | 1 | boolean removed = false; |
73 | |
try{ |
74 | 1 | Enumeration meta = this.fetch(Enumeration.class, enumerationKey); |
75 | 1 | if(meta != null){ |
76 | 1 | em.remove(meta); |
77 | 1 | removed = true; |
78 | |
} |
79 | |
} |
80 | 0 | catch(Exception e){ |
81 | 0 | logger.error("removeEnumeration query failed.", e); |
82 | 0 | throw new EnumerationException("removeEnumeration query failed.", e); |
83 | 1 | } |
84 | |
|
85 | 1 | return removed; |
86 | |
} |
87 | |
|
88 | |
public EnumeratedValue addEnumeratedValue(String enumerationKey, EnumeratedValue value) { |
89 | |
try{ |
90 | 38 | em.persist(value); |
91 | |
} |
92 | 0 | catch(Exception e){ |
93 | 0 | logger.error("addEnumeratedValue query failed.", e); |
94 | 0 | throw new EnumerationException("addEnumeratedValue query failed.", e); |
95 | 38 | } |
96 | |
|
97 | 38 | return value; |
98 | |
} |
99 | |
|
100 | |
|
101 | |
public EnumeratedValue updateEnumeratedValue(Enumeration enumeration, String code, EnumeratedValue enumeratedValue) { |
102 | |
|
103 | 2 | EnumeratedValue returnValue = null; |
104 | |
try{ |
105 | |
|
106 | 2 | List<EnumeratedValue> list = this.fetchEnumeratedValues(enumeration.getId()); |
107 | 2 | for(EnumeratedValue e: list){ |
108 | 2 | if(e.getCode().equals(code)){ |
109 | 2 | e.setCode(enumeratedValue.getCode()); |
110 | 2 | e.setEffectiveDate(enumeratedValue.getEffectiveDate()); |
111 | 2 | e.setExpirationDate(enumeratedValue.getExpirationDate()); |
112 | 2 | e.setEnumeration(enumeration); |
113 | 2 | e.setSortKey(enumeratedValue.getSortKey()); |
114 | 2 | e.setValue(enumeratedValue.getValue()); |
115 | 2 | e.setAbbrevValue(enumeratedValue.getAbbrevValue()); |
116 | 2 | e.setContextEntityList(enumeratedValue.getContextEntityList()); |
117 | 2 | em.merge(e); |
118 | 2 | returnValue = e; |
119 | 2 | break; |
120 | |
} |
121 | |
} |
122 | |
} |
123 | 0 | catch(Exception e){ |
124 | 0 | throw new EnumerationException("updateEnumeratedValue query failed.", e); |
125 | 2 | } |
126 | |
|
127 | 2 | return returnValue; |
128 | |
} |
129 | |
|
130 | |
public boolean removeEnumeratedValue(String enumerationKey, String code) { |
131 | 2 | boolean removed = false; |
132 | |
try{ |
133 | 2 | List<EnumeratedValue> list = this.fetchEnumeratedValues(enumerationKey); |
134 | 2 | for(EnumeratedValue e: list){ |
135 | 2 | if(e.getCode().equals(code)){ |
136 | 2 | em.remove(e); |
137 | 2 | removed = true; |
138 | 2 | break; |
139 | |
} |
140 | |
} |
141 | |
} |
142 | 0 | catch(Exception e){ |
143 | 0 | logger.error("removeEnumeratedValue query failed.", e); |
144 | 0 | throw new EnumerationException("removeEnumeratedValue query failed.", e); |
145 | 2 | } |
146 | 2 | return removed; |
147 | |
} |
148 | |
|
149 | |
|
150 | |
public List<EnumeratedValue> fetchEnumeratedValues(String enumerationKey) { |
151 | |
|
152 | |
List<EnumeratedValue> list ; |
153 | |
try{ |
154 | 8 | Query query = em.createQuery( |
155 | |
"select e from EnumeratedValue e " + |
156 | |
"where e.enumeration.id = :enumerationKey "); |
157 | 8 | query.setParameter("enumerationKey", enumerationKey); |
158 | |
@SuppressWarnings("unchecked") |
159 | 8 | List<EnumeratedValue> resultList = (List<EnumeratedValue>)query.getResultList(); |
160 | 8 | list = resultList; |
161 | |
} |
162 | 0 | catch(Exception e){ |
163 | 0 | logger.error("fetchEnumeration query failed.", e); |
164 | 0 | throw new EnumerationException("fetchEnumeration query failed.", e); |
165 | 8 | } |
166 | |
|
167 | 8 | return list; |
168 | |
} |
169 | |
|
170 | |
public List<EnumeratedValue> fetchEnumeratedValuesWithDate(String enumerationKey, Date contextDate) { |
171 | |
|
172 | |
List<EnumeratedValue> list; |
173 | |
try{ |
174 | 4 | Query query = em.createQuery( |
175 | |
"select e from EnumeratedValue e " + |
176 | |
"where e.effectiveDate <= :contextDate and " + |
177 | |
"(e.expirationDate is null or e.expirationDate >= :contextDate) and " + |
178 | |
"e.enumeration.id = :enumerationKey "); |
179 | 4 | query.setParameter("enumerationKey", enumerationKey); |
180 | 4 | query.setParameter("contextDate", contextDate); |
181 | |
@SuppressWarnings("unchecked") |
182 | 4 | List<EnumeratedValue> resultList = (List<EnumeratedValue>)query.getResultList(); |
183 | 4 | list = resultList; |
184 | |
} |
185 | 0 | catch(Exception e){ |
186 | 0 | logger.error("fetchEnumerationWithDate query failed.", e); |
187 | 0 | throw new EnumerationException("fetchEnumerationWithDate query failed.", e); |
188 | 4 | } |
189 | 4 | return list; |
190 | |
} |
191 | |
public List<EnumeratedValue> fetchEnumeratedValuesWithContext(String enumerationKey, String enumContextKey, String contextValue) { |
192 | |
|
193 | |
List<EnumeratedValue> list; |
194 | |
try{ |
195 | 4 | Query query = em.createQuery( |
196 | |
"select e from EnumeratedValue e JOIN e.contextEntityList c " + |
197 | |
"where c.contextValue = :contextValue and " + |
198 | |
"c.contextKey = :enumContextKey and " + |
199 | |
"e.enumeration.id = :enumerationKey "); |
200 | 4 | query.setParameter("enumerationKey", enumerationKey); |
201 | 4 | query.setParameter("enumContextKey", enumContextKey); |
202 | 4 | query.setParameter("contextValue", contextValue); |
203 | |
|
204 | |
@SuppressWarnings("unchecked") |
205 | 4 | List<EnumeratedValue> resultList = (List<EnumeratedValue>)query.getResultList(); |
206 | 4 | list = resultList; |
207 | |
} |
208 | 0 | catch(Exception e){ |
209 | 0 | logger.error("fetchEnumerationWithContext query failed.", e); |
210 | 0 | throw new EnumerationException("fetchEnumerationWithContext query failed.", e); |
211 | 4 | } |
212 | 4 | return list; |
213 | |
} |
214 | |
|
215 | |
public List<EnumeratedValue> fetchEnumeratedValuesWithContextAndDate(String enumerationKey, String enumContextKey, String contextValue, |
216 | |
Date contextDate) { |
217 | |
|
218 | |
List<EnumeratedValue> list; |
219 | |
|
220 | |
try{ |
221 | 13 | Query query = em.createQuery( |
222 | |
"select e from EnumeratedValue e, IN(e.contextEntityList) c " |
223 | |
+ "where e.effectiveDate <= :contextDate and " + |
224 | |
"(e.expirationDate is null or e.expirationDate >= :contextDate) and " + |
225 | |
"c.contextValue = :contextValue and " + |
226 | |
"c.contextKey = :enumContextKey and " + |
227 | |
"e.enumeration.id = :enumKey "); |
228 | 13 | query.setParameter("contextDate", contextDate); |
229 | 13 | query.setParameter("contextValue", contextValue); |
230 | 13 | query.setParameter("enumContextKey", enumContextKey); |
231 | 13 | query.setParameter("enumKey", enumerationKey); |
232 | |
@SuppressWarnings("unchecked") |
233 | 13 | List<EnumeratedValue> resultList = (List<EnumeratedValue>)query.getResultList(); |
234 | 13 | list = resultList; |
235 | |
} |
236 | 0 | catch(Exception e){ |
237 | 0 | logger.error("fetchEnumerationWithContextAndDate query failed.", e); |
238 | 0 | throw new EnumerationException("fetchEnumerationWithContextAndDate query failed.", e); |
239 | 13 | } |
240 | |
|
241 | 13 | return list; |
242 | |
} |
243 | |
} |