Coverage Report - org.kuali.rice.kns.dao.impl.BusinessObjectDaoOjb
 
Classes in this File Line Coverage Branch Coverage Complexity
BusinessObjectDaoOjb
0%
0/97
0%
0/28
1.64
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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  
  * This class is the OJB implementation of the BusinessObjectDao interface and should be used for generic business object unit
 38  
  * tests.
 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  
          * This constructs a {@link BusinessObjectDaoOjb}
 47  
          */
 48  0
         public BusinessObjectDaoOjb(PersistenceStructureService persistenceStructureService) {
 49  0
                 this.persistenceStructureService = persistenceStructureService;
 50  0
         }
 51  
 
 52  
     /**
 53  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#findBySinglePrimaryKey(java.lang.Class, java.lang.Object)
 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  
                     // it doesn't exist, just return null
 60  0
                         return null;
 61  
                 }
 62  
         }
 63  
 
 64  
     /**
 65  
      * @see org.kuali.rice.kns.dao.BusinessObjectDao#findByPrimaryKey(java.lang.Class, java.util.Map)
 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  
      * Retrieves all of the records for a given class name.
 75  
      *
 76  
      * @param clazz - the name of the object being used, either KualiCodeBase or a subclass
 77  
      * @return Collection
 78  
      * @see org.kuali.rice.kns.dao.BusinessObjectDao#findAll(java.lang.Class)
 79  
      */
 80  
     public Collection findAll(Class clazz) {
 81  0
         return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, (Criteria) null));
 82  
     }
 83  
 
 84  
     /**
 85  
      * @see org.kuali.rice.kns.dao.BusinessObjectDao#findAllOrderBy(java.lang.Class, java.lang.String, boolean)
 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  
      * This is the default impl that comes with Kuali - uses OJB.
 102  
      *
 103  
      * @see org.kuali.rice.kns.dao.BusinessObjectDao#findMatching(java.lang.Class, java.util.Map)
 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  
      * @see org.kuali.rice.kns.dao.BusinessObjectDao#findAllActive(java.lang.Class)
 114  
      */
 115  
     public Collection findAllActive(Class clazz) {
 116  0
         return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, buildActiveCriteria()));
 117  
     }
 118  
 
 119  
     /**
 120  
      * @see org.kuali.rice.kns.dao.BusinessObjectDao#findAllActive(java.lang.Class)
 121  
      */
 122  
     public Collection findAllInactive(Class clazz) {
 123  0
         return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, buildInactiveCriteria()));
 124  
     }
 125  
 
 126  
     /**
 127  
      * @see org.kuali.rice.kns.dao.BusinessObjectDao#findAllActiveOrderBy(java.lang.Class, java.lang.String, boolean)
 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  
      * @see org.kuali.rice.kns.dao.BusinessObjectDao#findMatchingActive(java.lang.Class, java.util.Map)
 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  
      * This is the default impl that comes with Kuali - uses OJB.
 154  
      *
 155  
      * @see org.kuali.rice.kns.dao.BusinessObjectDao#countMatching(java.lang.Class, java.util.Map)
 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  
      * This is the default impl that comes with Kuali - uses OJB.
 165  
      *
 166  
      * @see org.kuali.rice.kns.dao.BusinessObjectDao#countMatching(java.lang.Class, java.util.Map, java.util.Map)
 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  
      * This is the default impl that comes with Kuali - uses OJB.
 178  
      *
 179  
      * @see org.kuali.rice.kns.dao.BusinessObjectDao#findMatching(java.lang.Class, java.util.Map)
 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  
          * Saves a business object.
 197  
          *
 198  
          * @see org.kuali.rice.kns.dao.BusinessObjectDao#save(org.kuali.rice.kns.bo.PersistableBusinessObject)
 199  
          */
 200  
         public void save(PersistableBusinessObject bo) throws DataAccessException {
 201  
                 // if collections exist on the BO, create a copy and use to process the
 202  
                 // collections to ensure
 203  
                 // that removed elements are deleted from the database
 204  0
                 Set<String> boCollections = getPersistenceStructureService().listCollectionObjectTypes(bo.getClass()).keySet();
 205  0
                 PersistableBusinessObject savedBo = null;
 206  0
                 if (!boCollections.isEmpty()) {
 207  
                         // refresh bo to get db copy of collections
 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  
      * Saves a business object.
 222  
      *
 223  
      * @see org.kuali.rice.kns.dao.BusinessObjectDao#save(org.kuali.rice.kns.bo.PersistableBusinessObject)
 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  
      * Deletes the business object passed in.
 241  
      *
 242  
      * @param bo
 243  
      * @throws DataAccessException
 244  
      * @see org.kuali.rice.kns.dao.BusinessObjectDao#delete(org.kuali.rice.kns.bo.PersistableBusinessObject)
 245  
      */
 246  
     public void delete(PersistableBusinessObject bo) {
 247  0
         getPersistenceBrokerTemplate().delete(bo);
 248  0
     }
 249  
 
 250  
     /**
 251  
      * @see org.kuali.rice.kns.dao.BusinessObjectDao#delete(java.util.List)
 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  
      * @see org.kuali.rice.kns.dao.BusinessObjectDao#deleteMatching(java.lang.Class, java.util.Map)
 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  
         // An ojb delete by query doesn't update the cache so we need to clear the cache for everything to work property.
 269  
         // don't believe me? Read the source code to OJB
 270  0
         getPersistenceBrokerTemplate().clearCache();
 271  0
     }
 272  
 
 273  
     /**
 274  
      * @see org.kuali.rice.kns.dao.BusinessObjectDao#retrieve(org.kuali.rice.kns.bo.PersistableBusinessObject)
 275  
      */
 276  
     public PersistableBusinessObject retrieve(PersistableBusinessObject object) {
 277  0
         return (PersistableBusinessObject) getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQueryByIdentity(object));
 278  
     }
 279  
 
 280  
     /**
 281  
      * This method will build out criteria in the key-value paradigm (attribute-value).
 282  
      *
 283  
      * @param fieldValues
 284  
      * @return
 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  
      * Builds a Criteria object for active field set to true
 306  
      * @return Criteria
 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  
      * Builds a Criteria object for active field set to true
 317  
      * @return Criteria
 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  
      * This method will build out criteria in the key-value paradigm (attribute-value).
 328  
      *
 329  
      * @param negativeFieldValues
 330  
      * @return
 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  
      * Gets the persistenceStructureService attribute.
 352  
      * @return Returns the persistenceStructureService.
 353  
      */
 354  
     protected PersistenceStructureService getPersistenceStructureService() {
 355  0
         return persistenceStructureService;
 356  
     }
 357  
 
 358  
     /**
 359  
      * Sets the persistenceStructureService attribute value.
 360  
      * @param persistenceStructureService The persistenceStructureService to set.
 361  
      */
 362  
     public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) {
 363  0
         this.persistenceStructureService = persistenceStructureService;
 364  0
     }
 365  
 
 366  
 }