| 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 | |
import java.util.Set; |
| 23 | |
|
| 24 | |
import org.apache.ojb.broker.query.Criteria; |
| 25 | |
import org.apache.ojb.broker.query.QueryByCriteria; |
| 26 | |
import org.apache.ojb.broker.query.QueryFactory; |
| 27 | |
import org.kuali.rice.kns.bo.PersistableBusinessObject; |
| 28 | |
import org.kuali.rice.kns.dao.BusinessObjectDao; |
| 29 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
| 30 | |
import org.kuali.rice.kns.service.PersistenceStructureService; |
| 31 | |
import org.kuali.rice.kns.util.KNSPropertyConstants; |
| 32 | |
import org.kuali.rice.kns.util.ObjectUtils; |
| 33 | |
import org.kuali.rice.kns.util.OjbCollectionAware; |
| 34 | |
import org.springframework.dao.DataAccessException; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public class BusinessObjectDaoOjb extends PlatformAwareDaoBaseOjb implements BusinessObjectDao, OjbCollectionAware { |
| 41 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BusinessObjectDaoOjb.class); |
| 42 | |
|
| 43 | |
private PersistenceStructureService persistenceStructureService; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | 0 | public BusinessObjectDaoOjb(PersistenceStructureService persistenceStructureService) { |
| 49 | 0 | this.persistenceStructureService = persistenceStructureService; |
| 50 | 0 | } |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public PersistableBusinessObject findBySinglePrimaryKey(Class clazz, Object primaryKey) { |
| 56 | |
try { |
| 57 | 0 | return (PersistableBusinessObject) getPersistenceBrokerTemplate().getObjectById(clazz, primaryKey); |
| 58 | 0 | } catch ( DataAccessException ex ) { |
| 59 | |
|
| 60 | 0 | return null; |
| 61 | |
} |
| 62 | |
} |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public PersistableBusinessObject findByPrimaryKey(Class clazz, Map primaryKeys) { |
| 68 | 0 | Criteria criteria = buildCriteria(primaryKeys); |
| 69 | |
|
| 70 | 0 | return (PersistableBusinessObject) getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(clazz, criteria)); |
| 71 | |
} |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public Collection findAll(Class clazz) { |
| 81 | 0 | return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, (Criteria) null)); |
| 82 | |
} |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public Collection findAllOrderBy(Class clazz, String sortField, boolean sortAscending) { |
| 88 | 0 | QueryByCriteria queryByCriteria = new QueryByCriteria(clazz, (Criteria) null); |
| 89 | |
|
| 90 | 0 | if (sortAscending) { |
| 91 | 0 | queryByCriteria.addOrderByAscending(sortField); |
| 92 | |
} |
| 93 | |
else { |
| 94 | 0 | queryByCriteria.addOrderByDescending(sortField); |
| 95 | |
} |
| 96 | |
|
| 97 | 0 | return getPersistenceBrokerTemplate().getCollectionByQuery(queryByCriteria); |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public Collection findMatching(Class clazz, Map fieldValues) { |
| 106 | 0 | Criteria criteria = buildCriteria(fieldValues); |
| 107 | |
|
| 108 | 0 | return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, criteria)); |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public Collection findAllActive(Class clazz) { |
| 116 | 0 | return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, buildActiveCriteria())); |
| 117 | |
} |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
public Collection findAllInactive(Class clazz) { |
| 123 | 0 | return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, buildInactiveCriteria())); |
| 124 | |
} |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public Collection findAllActiveOrderBy(Class clazz, String sortField, boolean sortAscending) { |
| 130 | 0 | QueryByCriteria queryByCriteria = new QueryByCriteria(clazz, buildActiveCriteria()); |
| 131 | |
|
| 132 | 0 | if (sortAscending) { |
| 133 | 0 | queryByCriteria.addOrderByAscending(sortField); |
| 134 | |
} |
| 135 | |
else { |
| 136 | 0 | queryByCriteria.addOrderByDescending(sortField); |
| 137 | |
} |
| 138 | |
|
| 139 | 0 | return getPersistenceBrokerTemplate().getCollectionByQuery(queryByCriteria); |
| 140 | |
} |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public Collection findMatchingActive(Class clazz, Map fieldValues) { |
| 146 | 0 | Criteria criteria = buildCriteria(fieldValues); |
| 147 | 0 | criteria.addAndCriteria(buildActiveCriteria()); |
| 148 | |
|
| 149 | 0 | return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, criteria)); |
| 150 | |
} |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
public int countMatching(Class clazz, Map fieldValues) { |
| 158 | 0 | Criteria criteria = buildCriteria(fieldValues); |
| 159 | |
|
| 160 | 0 | return getPersistenceBrokerTemplate().getCount(QueryFactory.newQuery(clazz, criteria)); |
| 161 | |
} |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
public int countMatching(Class clazz, Map positiveFieldValues, Map negativeFieldValues) { |
| 169 | 0 | Criteria criteria = buildCriteria(positiveFieldValues); |
| 170 | 0 | Criteria negativeCriteria = buildNegativeCriteria(negativeFieldValues); |
| 171 | 0 | criteria.addAndCriteria(negativeCriteria); |
| 172 | 0 | return getPersistenceBrokerTemplate().getCount(QueryFactory.newQuery(clazz, criteria)); |
| 173 | |
} |
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
public Collection findMatchingOrderBy(Class clazz, Map fieldValues, String sortField, boolean sortAscending) { |
| 182 | 0 | Criteria criteria = buildCriteria(fieldValues); |
| 183 | 0 | QueryByCriteria queryByCriteria = new QueryByCriteria(clazz, criteria); |
| 184 | |
|
| 185 | 0 | if (sortAscending) { |
| 186 | 0 | queryByCriteria.addOrderByAscending(sortField); |
| 187 | |
} |
| 188 | |
else { |
| 189 | 0 | queryByCriteria.addOrderByDescending(sortField); |
| 190 | |
} |
| 191 | |
|
| 192 | 0 | return getPersistenceBrokerTemplate().getCollectionByQuery(queryByCriteria); |
| 193 | |
} |
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
public void save(PersistableBusinessObject bo) throws DataAccessException { |
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | 0 | Set<String> boCollections = getPersistenceStructureService().listCollectionObjectTypes(bo.getClass()).keySet(); |
| 205 | 0 | PersistableBusinessObject savedBo = null; |
| 206 | 0 | if (!boCollections.isEmpty()) { |
| 207 | |
|
| 208 | 0 | savedBo = (PersistableBusinessObject) ObjectUtils.deepCopy(bo); |
| 209 | 0 | for (String boCollection : boCollections) { |
| 210 | 0 | if (getPersistenceStructureService().isCollectionUpdatable(savedBo.getClass(), boCollection)) { |
| 211 | 0 | savedBo.refreshReferenceObject(boCollection); |
| 212 | |
} |
| 213 | |
} |
| 214 | 0 | KNSServiceLocator.getOjbCollectionHelper().processCollections(this, bo, savedBo); |
| 215 | |
} |
| 216 | |
|
| 217 | 0 | getPersistenceBrokerTemplate().store(bo); |
| 218 | 0 | } |
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
public void save(List businessObjects) throws DataAccessException { |
| 226 | 0 | if ( LOG.isDebugEnabled() ) { |
| 227 | 0 | LOG.debug( "About to persist the following BOs:" ); |
| 228 | 0 | for ( Object bo : businessObjects ) { |
| 229 | 0 | LOG.debug( " --->" + bo ); |
| 230 | |
} |
| 231 | |
} |
| 232 | 0 | for (Iterator i = businessObjects.iterator(); i.hasNext();) { |
| 233 | 0 | Object bo = i.next(); |
| 234 | 0 | getPersistenceBrokerTemplate().store(bo); |
| 235 | 0 | } |
| 236 | 0 | } |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
public void delete(PersistableBusinessObject bo) { |
| 247 | 0 | getPersistenceBrokerTemplate().delete(bo); |
| 248 | 0 | } |
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
public void delete(List<? extends PersistableBusinessObject> boList) { |
| 254 | 0 | for (PersistableBusinessObject bo : boList) { |
| 255 | 0 | getPersistenceBrokerTemplate().delete(bo); |
| 256 | |
} |
| 257 | 0 | } |
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
public void deleteMatching(Class clazz, Map fieldValues) { |
| 264 | 0 | Criteria criteria = buildCriteria(fieldValues); |
| 265 | |
|
| 266 | 0 | getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(clazz, criteria)); |
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | 0 | getPersistenceBrokerTemplate().clearCache(); |
| 271 | 0 | } |
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
public PersistableBusinessObject retrieve(PersistableBusinessObject object) { |
| 277 | 0 | return (PersistableBusinessObject) getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQueryByIdentity(object)); |
| 278 | |
} |
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
private Criteria buildCriteria(Map fieldValues) { |
| 287 | 0 | Criteria criteria = new Criteria(); |
| 288 | 0 | for (Iterator i = fieldValues.entrySet().iterator(); i.hasNext();) { |
| 289 | 0 | Map.Entry e = (Map.Entry) i.next(); |
| 290 | |
|
| 291 | 0 | String key = (String) e.getKey(); |
| 292 | 0 | Object value = e.getValue(); |
| 293 | 0 | if (value instanceof Collection) { |
| 294 | 0 | criteria.addIn(key, (Collection) value); |
| 295 | |
} |
| 296 | |
else { |
| 297 | 0 | criteria.addEqualTo(key, value); |
| 298 | |
} |
| 299 | 0 | } |
| 300 | |
|
| 301 | 0 | return criteria; |
| 302 | |
} |
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
private Criteria buildActiveCriteria(){ |
| 309 | 0 | Criteria criteria = new Criteria(); |
| 310 | 0 | criteria.addEqualTo(KNSPropertyConstants.ACTIVE, true); |
| 311 | |
|
| 312 | 0 | return criteria; |
| 313 | |
} |
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
private Criteria buildInactiveCriteria(){ |
| 320 | 0 | Criteria criteria = new Criteria(); |
| 321 | 0 | criteria.addEqualTo(KNSPropertyConstants.ACTIVE, false); |
| 322 | |
|
| 323 | 0 | return criteria; |
| 324 | |
} |
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
private Criteria buildNegativeCriteria(Map negativeFieldValues) { |
| 333 | 0 | Criteria criteria = new Criteria(); |
| 334 | 0 | for (Iterator i = negativeFieldValues.entrySet().iterator(); i.hasNext();) { |
| 335 | 0 | Map.Entry e = (Map.Entry) i.next(); |
| 336 | |
|
| 337 | 0 | String key = (String) e.getKey(); |
| 338 | 0 | Object value = e.getValue(); |
| 339 | 0 | if (value instanceof Collection) { |
| 340 | 0 | criteria.addNotIn(key, (Collection) value); |
| 341 | |
} |
| 342 | |
else { |
| 343 | 0 | criteria.addNotEqualTo(key, value); |
| 344 | |
} |
| 345 | 0 | } |
| 346 | |
|
| 347 | 0 | return criteria; |
| 348 | |
} |
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
|
| 354 | |
protected PersistenceStructureService getPersistenceStructureService() { |
| 355 | 0 | return persistenceStructureService; |
| 356 | |
} |
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) { |
| 363 | 0 | this.persistenceStructureService = persistenceStructureService; |
| 364 | 0 | } |
| 365 | |
|
| 366 | |
} |