| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kns.dao.impl; |
| 17 | |
|
| 18 | |
import java.util.Collection; |
| 19 | |
import java.util.Iterator; |
| 20 | |
import java.util.List; |
| 21 | |
import java.util.Map; |
| 22 | |
|
| 23 | |
import javax.persistence.EntityManager; |
| 24 | |
import javax.persistence.PersistenceContext; |
| 25 | |
import javax.persistence.PersistenceException; |
| 26 | |
|
| 27 | |
import org.kuali.rice.core.jpa.criteria.QueryByCriteria.QueryByCriteriaType; |
| 28 | |
import org.kuali.rice.core.jpa.metadata.MetadataManager; |
| 29 | |
import org.kuali.rice.core.util.OrmUtils; |
| 30 | |
import org.kuali.rice.kns.bo.PersistableBusinessObject; |
| 31 | |
import org.kuali.rice.kns.bo.PersistableBusinessObjectExtension; |
| 32 | |
import org.kuali.rice.kns.dao.BusinessObjectDao; |
| 33 | |
import org.kuali.rice.kns.service.PersistenceStructureService; |
| 34 | |
import org.kuali.rice.kns.util.KNSPropertyConstants; |
| 35 | |
import org.kuali.rice.kns.util.OjbCollectionHelper; |
| 36 | |
import org.springframework.dao.DataAccessException; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public class BusinessObjectDaoJpa implements BusinessObjectDao { |
| 42 | 0 | private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BusinessObjectDaoJpa.class); |
| 43 | |
|
| 44 | |
@PersistenceContext |
| 45 | |
private EntityManager entityManager; |
| 46 | |
|
| 47 | |
private PersistenceStructureService persistenceStructureService; |
| 48 | |
|
| 49 | |
private OjbCollectionHelper ojbCollectionHelper; |
| 50 | |
|
| 51 | 0 | public BusinessObjectDaoJpa(EntityManager entityManager, PersistenceStructureService persistenceStructureService) { |
| 52 | 0 | this.entityManager = entityManager; |
| 53 | 0 | this.persistenceStructureService = persistenceStructureService; |
| 54 | 0 | } |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
public PersistableBusinessObject findBySinglePrimaryKey(Class clazz, Object primaryKey) { |
| 60 | 0 | return (PersistableBusinessObject) entityManager.find(clazz, primaryKey); |
| 61 | |
} |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public PersistableBusinessObject findByPrimaryKey(Class clazz, Map primaryKeys) { |
| 68 | 0 | PersistableBusinessObject bo = null; |
| 69 | |
try { |
| 70 | 0 | bo = (PersistableBusinessObject) new org.kuali.rice.core.jpa.criteria.QueryByCriteria(entityManager, buildJpaCriteria(clazz, primaryKeys)).toQuery().getSingleResult(); |
| 71 | 0 | } catch (PersistenceException e) {} |
| 72 | 0 | return bo; |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
public Collection findAll(Class clazz) { |
| 85 | 0 | return new org.kuali.rice.core.jpa.criteria.QueryByCriteria(entityManager, new org.kuali.rice.core.jpa.criteria.Criteria(clazz.getName())).toQuery().getResultList(); |
| 86 | |
} |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
public Collection findAllOrderBy(Class clazz, String sortField, boolean sortAscending) { |
| 93 | 0 | org.kuali.rice.core.jpa.criteria.Criteria criteria = new org.kuali.rice.core.jpa.criteria.Criteria(clazz.getName()); |
| 94 | 0 | criteria.orderBy(sortField, sortAscending); |
| 95 | 0 | return new org.kuali.rice.core.jpa.criteria.QueryByCriteria(entityManager, criteria).toQuery().getResultList(); |
| 96 | |
} |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public Collection findMatching(Class clazz, Map fieldValues) { |
| 105 | 0 | return new org.kuali.rice.core.jpa.criteria.QueryByCriteria(entityManager, buildJpaCriteria(clazz, fieldValues)).toQuery().getResultList(); |
| 106 | |
} |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public Collection findAllActive(Class clazz) { |
| 112 | 0 | return new org.kuali.rice.core.jpa.criteria.QueryByCriteria(entityManager, buildActiveJpaCriteria(clazz)).toQuery().getResultList(); |
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
public Collection findAllInactive(Class clazz) { |
| 119 | 0 | return new org.kuali.rice.core.jpa.criteria.QueryByCriteria(entityManager, buildInactiveJpaCriteria(clazz)).toQuery().getResultList(); |
| 120 | |
} |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
public Collection findAllActiveOrderBy(Class clazz, String sortField, boolean sortAscending) { |
| 127 | 0 | org.kuali.rice.core.jpa.criteria.Criteria criteria = buildActiveJpaCriteria(clazz); |
| 128 | 0 | criteria.orderBy(sortField, sortAscending); |
| 129 | 0 | return new org.kuali.rice.core.jpa.criteria.QueryByCriteria(entityManager, criteria).toQuery().getResultList(); |
| 130 | |
} |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
public Collection findMatchingActive(Class clazz, Map fieldValues) { |
| 137 | 0 | org.kuali.rice.core.jpa.criteria.Criteria criteria = buildJpaCriteria(clazz, fieldValues); |
| 138 | 0 | criteria.and(buildActiveJpaCriteria(clazz)); |
| 139 | 0 | return new org.kuali.rice.core.jpa.criteria.QueryByCriteria(entityManager, criteria).toQuery().getResultList(); |
| 140 | |
} |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
public int countMatching(Class clazz, Map fieldValues) { |
| 149 | 0 | return ((Long) new org.kuali.rice.core.jpa.criteria.QueryByCriteria(entityManager, buildJpaCriteria(clazz, fieldValues)).toCountQuery().getSingleResult()).intValue(); |
| 150 | |
} |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
public int countMatching(Class clazz, Map positiveFieldValues, Map negativeFieldValues) { |
| 159 | 0 | org.kuali.rice.core.jpa.criteria.Criteria criteria = buildJpaCriteria(clazz, positiveFieldValues); |
| 160 | 0 | criteria.and(buildNegativeJpaCriteria(clazz, negativeFieldValues)); |
| 161 | 0 | return ((Long) new org.kuali.rice.core.jpa.criteria.QueryByCriteria(entityManager, criteria).toCountQuery().getSingleResult()).intValue(); |
| 162 | |
} |
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
public Collection findMatchingOrderBy(Class clazz, Map fieldValues, String sortField, boolean sortAscending) { |
| 171 | 0 | org.kuali.rice.core.jpa.criteria.Criteria criteria = buildJpaCriteria(clazz, fieldValues); |
| 172 | 0 | criteria.orderBy(sortField, sortAscending); |
| 173 | 0 | return new org.kuali.rice.core.jpa.criteria.QueryByCriteria(entityManager, criteria).toQuery().getResultList(); |
| 174 | |
} |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
public void save(PersistableBusinessObject bo) throws DataAccessException { |
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | 0 | reattachAndSave(bo); |
| 199 | 0 | } |
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
public void save(List businessObjects) throws DataAccessException { |
| 209 | 0 | for (Iterator i = businessObjects.iterator(); i.hasNext();) { |
| 210 | 0 | Object bo = i.next(); |
| 211 | 0 | reattachAndSave((PersistableBusinessObject) bo); |
| 212 | 0 | } |
| 213 | 0 | } |
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
public void delete(PersistableBusinessObject bo) { |
| 223 | |
|
| 224 | 0 | entityManager.remove(entityManager.merge(bo)); |
| 225 | 0 | } |
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
public void delete(List<? extends PersistableBusinessObject> boList) { |
| 231 | 0 | for (PersistableBusinessObject bo : boList) { |
| 232 | |
|
| 233 | |
|
| 234 | 0 | entityManager.remove(entityManager.merge(bo)); |
| 235 | |
} |
| 236 | 0 | } |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
public void deleteMatching(Class clazz, Map fieldValues) { |
| 243 | |
|
| 244 | 0 | new org.kuali.rice.core.jpa.criteria.QueryByCriteria(entityManager, buildJpaCriteria(clazz, fieldValues), QueryByCriteriaType.DELETE).toQuery().executeUpdate(); |
| 245 | 0 | } |
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
public PersistableBusinessObject retrieve(PersistableBusinessObject object) { |
| 251 | 0 | PersistableBusinessObject pbo = null; |
| 252 | 0 | Map primaryKeys = MetadataManager.getPersistableBusinessObjectPrimaryKeyValuePairs(object); |
| 253 | 0 | pbo = (PersistableBusinessObject) new org.kuali.rice.core.jpa.criteria.QueryByCriteria(entityManager, buildJpaCriteria(object.getClass(), primaryKeys)).toQuery().getSingleResult(); |
| 254 | 0 | if (pbo != null && pbo.getExtension() != null) { |
| 255 | 0 | pbo.setExtension((PersistableBusinessObjectExtension) findByPrimaryKey(pbo.getExtension().getClass(), MetadataManager.getPersistableBusinessObjectPrimaryKeyValuePairs(pbo))); |
| 256 | |
} |
| 257 | 0 | return pbo; |
| 258 | |
} |
| 259 | |
|
| 260 | |
private org.kuali.rice.core.jpa.criteria.Criteria buildJpaCriteria(Class clazz, Map fieldValues) { |
| 261 | 0 | org.kuali.rice.core.jpa.criteria.Criteria criteria = new org.kuali.rice.core.jpa.criteria.Criteria(clazz.getName()); |
| 262 | 0 | for (Iterator i = fieldValues.entrySet().iterator(); i.hasNext();) { |
| 263 | 0 | Map.Entry e = (Map.Entry) i.next(); |
| 264 | |
|
| 265 | 0 | String key = (String) e.getKey(); |
| 266 | 0 | Object value = e.getValue(); |
| 267 | 0 | if (value instanceof Collection) { |
| 268 | 0 | criteria.in(key, (List) value); |
| 269 | |
} else { |
| 270 | 0 | criteria.eq(key, value); |
| 271 | |
} |
| 272 | 0 | } |
| 273 | 0 | return criteria; |
| 274 | |
} |
| 275 | |
|
| 276 | |
private org.kuali.rice.core.jpa.criteria.Criteria buildActiveJpaCriteria(Class clazz) { |
| 277 | 0 | org.kuali.rice.core.jpa.criteria.Criteria criteria = new org.kuali.rice.core.jpa.criteria.Criteria(clazz.getName()); |
| 278 | 0 | criteria.eq(KNSPropertyConstants.ACTIVE, true); |
| 279 | 0 | return criteria; |
| 280 | |
} |
| 281 | |
|
| 282 | |
private org.kuali.rice.core.jpa.criteria.Criteria buildInactiveJpaCriteria(Class clazz) { |
| 283 | 0 | org.kuali.rice.core.jpa.criteria.Criteria criteria = new org.kuali.rice.core.jpa.criteria.Criteria(clazz.getName()); |
| 284 | 0 | criteria.eq(KNSPropertyConstants.ACTIVE, false); |
| 285 | 0 | return criteria; |
| 286 | |
} |
| 287 | |
|
| 288 | |
private org.kuali.rice.core.jpa.criteria.Criteria buildNegativeJpaCriteria(Class clazz, Map negativeFieldValues) { |
| 289 | 0 | org.kuali.rice.core.jpa.criteria.Criteria criteria = new org.kuali.rice.core.jpa.criteria.Criteria(clazz.getName()); |
| 290 | 0 | for (Iterator i = negativeFieldValues.entrySet().iterator(); i.hasNext();) { |
| 291 | 0 | Map.Entry e = (Map.Entry) i.next(); |
| 292 | |
|
| 293 | 0 | String key = (String) e.getKey(); |
| 294 | 0 | Object value = e.getValue(); |
| 295 | 0 | if (value instanceof Collection) { |
| 296 | 0 | criteria.notIn(key, (List) value); |
| 297 | |
} else { |
| 298 | 0 | criteria.ne(key, value); |
| 299 | |
} |
| 300 | 0 | } |
| 301 | |
|
| 302 | 0 | return criteria; |
| 303 | |
} |
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
protected PersistenceStructureService getPersistenceStructureService() { |
| 311 | 0 | return persistenceStructureService; |
| 312 | |
} |
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) { |
| 321 | 0 | this.persistenceStructureService = persistenceStructureService; |
| 322 | 0 | } |
| 323 | |
|
| 324 | |
private void reattachAndSave(PersistableBusinessObject bo) { |
| 325 | 0 | PersistableBusinessObject attachedBo = findByPrimaryKey(bo.getClass(), MetadataManager.getPersistableBusinessObjectPrimaryKeyValuePairs(bo)); |
| 326 | 0 | if (attachedBo == null) { |
| 327 | 0 | entityManager.merge(bo); |
| 328 | 0 | if (bo.getExtension() != null) { |
| 329 | 0 | entityManager.merge(bo.getExtension()); |
| 330 | |
} |
| 331 | |
} else { |
| 332 | 0 | if (bo.getExtension() != null) { |
| 333 | 0 | PersistableBusinessObject attachedBoe = findByPrimaryKey(bo.getExtension().getClass(), MetadataManager.getPersistableBusinessObjectPrimaryKeyValuePairs(bo.getExtension())); |
| 334 | 0 | OrmUtils.reattach(attachedBoe, bo.getExtension()); |
| 335 | 0 | attachedBo.setExtension((PersistableBusinessObjectExtension) attachedBoe); |
| 336 | 0 | entityManager.merge(attachedBoe); |
| 337 | |
} |
| 338 | 0 | OrmUtils.reattach(attachedBo, bo); |
| 339 | 0 | entityManager.merge(attachedBo); |
| 340 | |
} |
| 341 | 0 | } |
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
public EntityManager getEntityManager() { |
| 347 | 0 | return this.entityManager; |
| 348 | |
} |
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
public void setEntityManager(EntityManager entityManager) { |
| 354 | 0 | this.entityManager = entityManager; |
| 355 | 0 | } |
| 356 | |
|
| 357 | |
} |