| 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.common.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 | 1 | final static Logger logger = LoggerFactory.getLogger(EnumerationManagementDAOImpl.class); |
| 38 | |
|
| 39 | |
@PersistenceContext(unitName = "EnumerationManagement") |
| 40 | |
@Override |
| 41 | |
public void setEm(EntityManager em) { |
| 42 | 0 | super.setEm(em); |
| 43 | 0 | } |
| 44 | |
|
| 45 | |
public List<Enumeration> findEnumerations() { |
| 46 | |
List<Enumeration> metas; |
| 47 | |
try{ |
| 48 | 0 | Query query = em.createQuery("SELECT e FROM Enumeration e"); |
| 49 | |
@SuppressWarnings("unchecked") |
| 50 | 0 | List<Enumeration> resultList = (List<Enumeration>) query.getResultList(); |
| 51 | 0 | 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 | 0 | } |
| 57 | 0 | return metas; |
| 58 | |
} |
| 59 | |
|
| 60 | |
public Enumeration addEnumeration(Enumeration entity){ |
| 61 | |
try{ |
| 62 | 0 | em.persist(entity); |
| 63 | 0 | entity = em.find(Enumeration.class, entity.getId()); |
| 64 | |
} |
| 65 | 0 | catch(Exception e){ |
| 66 | 0 | throw new EnumerationException("addEnumerationMeta query failed.", e); |
| 67 | 0 | } |
| 68 | 0 | return entity; |
| 69 | |
} |
| 70 | |
|
| 71 | |
public boolean removeEnumeration(String enumerationKey){ |
| 72 | 0 | boolean removed = false; |
| 73 | |
try{ |
| 74 | 0 | Enumeration meta = this.fetch(Enumeration.class, enumerationKey); |
| 75 | 0 | if(meta != null){ |
| 76 | 0 | em.remove(meta); |
| 77 | 0 | 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 | 0 | } |
| 84 | |
|
| 85 | 0 | return removed; |
| 86 | |
} |
| 87 | |
|
| 88 | |
public EnumeratedValue addEnumeratedValue(String enumerationKey, EnumeratedValue value) { |
| 89 | |
try{ |
| 90 | 0 | 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 | 0 | } |
| 96 | |
|
| 97 | 0 | return value; |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
public EnumeratedValue updateEnumeratedValue(Enumeration enumeration, String code, EnumeratedValue enumeratedValue) { |
| 102 | |
|
| 103 | 0 | EnumeratedValue returnValue = null; |
| 104 | |
try{ |
| 105 | |
|
| 106 | 0 | List<EnumeratedValue> list = this.fetchEnumeratedValues(enumeration.getId()); |
| 107 | 0 | for(EnumeratedValue e: list){ |
| 108 | 0 | if(e.getCode().equals(code)){ |
| 109 | 0 | e.setCode(enumeratedValue.getCode()); |
| 110 | 0 | e.setEffectiveDate(enumeratedValue.getEffectiveDate()); |
| 111 | 0 | e.setExpirationDate(enumeratedValue.getExpirationDate()); |
| 112 | 0 | e.setEnumeration(enumeration); |
| 113 | 0 | e.setSortKey(enumeratedValue.getSortKey()); |
| 114 | 0 | e.setValue(enumeratedValue.getValue()); |
| 115 | 0 | e.setAbbrevValue(enumeratedValue.getAbbrevValue()); |
| 116 | 0 | e.setContextEntityList(enumeratedValue.getContextEntityList()); |
| 117 | 0 | em.merge(e); |
| 118 | 0 | returnValue = e; |
| 119 | 0 | break; |
| 120 | |
} |
| 121 | |
} |
| 122 | |
} |
| 123 | 0 | catch(Exception e){ |
| 124 | 0 | throw new EnumerationException("updateEnumeratedValue query failed.", e); |
| 125 | 0 | } |
| 126 | |
|
| 127 | 0 | return returnValue; |
| 128 | |
} |
| 129 | |
|
| 130 | |
public boolean removeEnumeratedValue(String enumerationKey, String code) { |
| 131 | 0 | boolean removed = false; |
| 132 | |
try{ |
| 133 | 0 | List<EnumeratedValue> list = this.fetchEnumeratedValues(enumerationKey); |
| 134 | 0 | for(EnumeratedValue e: list){ |
| 135 | 0 | if(e.getCode().equals(code)){ |
| 136 | 0 | em.remove(e); |
| 137 | 0 | removed = true; |
| 138 | 0 | 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 | 0 | } |
| 146 | 0 | return removed; |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | |
public List<EnumeratedValue> fetchEnumeratedValues(String enumerationKey) { |
| 151 | |
|
| 152 | |
List<EnumeratedValue> list ; |
| 153 | |
try{ |
| 154 | 0 | Query query = em.createQuery( |
| 155 | |
"select e from EnumeratedValue e " + |
| 156 | |
"where e.enumeration.id = :enumerationKey "); |
| 157 | 0 | query.setParameter("enumerationKey", enumerationKey); |
| 158 | |
@SuppressWarnings("unchecked") |
| 159 | 0 | List<EnumeratedValue> resultList = (List<EnumeratedValue>)query.getResultList(); |
| 160 | 0 | 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 | 0 | } |
| 166 | |
|
| 167 | 0 | return list; |
| 168 | |
} |
| 169 | |
|
| 170 | |
public List<EnumeratedValue> fetchEnumeratedValuesWithDate(String enumerationKey, Date contextDate) { |
| 171 | |
|
| 172 | |
List<EnumeratedValue> list; |
| 173 | |
try{ |
| 174 | 0 | 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 | 0 | query.setParameter("enumerationKey", enumerationKey); |
| 180 | 0 | query.setParameter("contextDate", contextDate); |
| 181 | |
@SuppressWarnings("unchecked") |
| 182 | 0 | List<EnumeratedValue> resultList = (List<EnumeratedValue>)query.getResultList(); |
| 183 | 0 | 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 | 0 | } |
| 189 | 0 | return list; |
| 190 | |
} |
| 191 | |
public List<EnumeratedValue> fetchEnumeratedValuesWithContext(String enumerationKey, String enumContextKey, String contextValue) { |
| 192 | |
|
| 193 | |
List<EnumeratedValue> list; |
| 194 | |
try{ |
| 195 | 0 | 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 | 0 | query.setParameter("enumerationKey", enumerationKey); |
| 201 | 0 | query.setParameter("enumContextKey", enumContextKey); |
| 202 | 0 | query.setParameter("contextValue", contextValue); |
| 203 | |
|
| 204 | |
@SuppressWarnings("unchecked") |
| 205 | 0 | List<EnumeratedValue> resultList = (List<EnumeratedValue>)query.getResultList(); |
| 206 | 0 | 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 | 0 | } |
| 212 | 0 | 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 | 0 | 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 | 0 | query.setParameter("contextDate", contextDate); |
| 229 | 0 | query.setParameter("contextValue", contextValue); |
| 230 | 0 | query.setParameter("enumContextKey", enumContextKey); |
| 231 | 0 | query.setParameter("enumKey", enumerationKey); |
| 232 | |
@SuppressWarnings("unchecked") |
| 233 | 0 | List<EnumeratedValue> resultList = (List<EnumeratedValue>)query.getResultList(); |
| 234 | 0 | 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 | 0 | } |
| 240 | |
|
| 241 | 0 | return list; |
| 242 | |
} |
| 243 | |
} |