Coverage Report - org.kuali.rice.core.impl.persistence.dao.GenericDaoOjb
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericDaoOjb
0%
0/89
0%
0/24
1.583
 
 1  
 /*
 2  
  * Copyright 2006-2011 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  
 
 17  
 package org.kuali.rice.core.impl.persistence.dao;
 18  
 
 19  
 import org.apache.log4j.Logger;
 20  
 import org.apache.ojb.broker.query.Criteria;
 21  
 import org.apache.ojb.broker.query.Query;
 22  
 import org.apache.ojb.broker.query.QueryByCriteria;
 23  
 import org.apache.ojb.broker.query.QueryFactory;
 24  
 import org.kuali.rice.core.api.config.property.Config;
 25  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 26  
 import org.kuali.rice.core.framework.persistence.platform.DatabasePlatform;
 27  
 import org.kuali.rice.core.framework.persistence.dao.GenericDao;
 28  
 import org.kuali.rice.core.framework.persistence.ojb.SuffixableQueryByCriteria;
 29  
 import org.kuali.rice.core.util.RiceConstants;
 30  
 import org.springframework.dao.DataAccessException;
 31  
 import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
 32  
 
 33  
 import java.util.Collection;
 34  
 import java.util.Iterator;
 35  
 import java.util.List;
 36  
 import java.util.Map;
 37  
 
 38  
 /**
 39  
  * This class is the OJB implementation of the GenericDao interface. This
 40  
  * class was adapted from the Kuali Nervous System
 41  
  * (org.kuali.rice.kns.dao.impl.GenericDaoOjb).
 42  
  * 
 43  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 44  
  */
 45  0
 public class GenericDaoOjb extends PersistenceBrokerDaoSupport implements GenericDao {
 46  0
     private static final Logger LOG = Logger.getLogger(GenericDaoOjb.class);
 47  
 
 48  0
     private boolean useSelectForUpdate = true;
 49  
 
 50  
     /**
 51  
      * @param selectForUpdate whether to use select for update to implement pessimistic locking (testing/debugging purposes only)
 52  
      */
 53  
     public void setUseSelectForUpdate(boolean useSelectForUpdate) {
 54  0
         this.useSelectForUpdate = useSelectForUpdate;
 55  0
     }
 56  
 
 57  
     /**
 58  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#findById(Class, Object)
 59  
      */
 60  
     public Object findById(Class clazz, Object id) {
 61  0
         return getPersistenceBrokerTemplate().getObjectById(clazz, id);
 62  
     }
 63  
 
 64  
     /**
 65  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#findByPrimaryKey(java.lang.Class, java.util.Map)
 66  
      */
 67  
     public Object findByPrimaryKey(Class clazz, Map primaryKeys) {
 68  0
         Criteria criteria = buildCriteria(primaryKeys);
 69  
 
 70  0
         return getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(clazz, criteria));
 71  
     }
 72  
 
 73  
     /**
 74  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#findByUniqueKey(java.lang.Class, java.util.Map)
 75  
      */
 76  
     public Object findByUniqueKey(Class clazz, Map uniqueKeys) {
 77  0
         Criteria criteria = buildCriteria(uniqueKeys);
 78  
 
 79  0
         return getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(clazz, criteria));
 80  
     }
 81  
 
 82  
     /**
 83  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#findAll(java.lang.Class)
 84  
      */
 85  
     public Collection findAll(Class clazz) {
 86  0
         return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, (Criteria) null));
 87  
     }
 88  
 
 89  
     /**
 90  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#findAllOrderBy(java.lang.Class, java.lang.String, boolean)
 91  
      */
 92  
     public Collection findAllOrderBy(Class clazz, String sortField,
 93  
             boolean sortAscending) {
 94  0
         QueryByCriteria queryByCriteria = new QueryByCriteria(clazz, (Criteria) null);
 95  
 
 96  0
         if (sortAscending) {
 97  0
             queryByCriteria.addOrderByAscending(sortField);
 98  
         } else {
 99  0
             queryByCriteria.addOrderByDescending(sortField);
 100  
         }
 101  
 
 102  0
         return getPersistenceBrokerTemplate().getCollectionByQuery(
 103  
                 queryByCriteria);
 104  
     }
 105  
 
 106  
     /**
 107  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#findMatching(java.lang.Class, java.util.Map)
 108  
      */
 109  
     public Collection findMatching(Class clazz, Map fieldValues) {
 110  0
         Criteria criteria = buildCriteria(fieldValues);
 111  
 
 112  0
         return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(clazz, criteria));
 113  
     }
 114  
 
 115  
     /**
 116  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#countMatching(java.lang.Class, java.util.Map)
 117  
      */
 118  
     public int countMatching(Class clazz, Map fieldValues) {
 119  0
         Criteria criteria = buildCriteria(fieldValues);
 120  
 
 121  0
         return getPersistenceBrokerTemplate().getCount(QueryFactory.newQuery(clazz, criteria));
 122  
     }
 123  
 
 124  
     /**
 125  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#countMatching(java.lang.Class, java.util.Map, java.util.Map)
 126  
      */
 127  
     public int countMatching(Class clazz, Map positiveFieldValues,
 128  
             Map negativeFieldValues) {
 129  0
         Criteria criteria = buildCriteria(positiveFieldValues);
 130  0
         Criteria negativeCriteria = buildNegativeCriteria(negativeFieldValues);
 131  0
         criteria.addAndCriteria(negativeCriteria);
 132  0
         return getPersistenceBrokerTemplate().getCount(QueryFactory.newQuery(clazz, criteria));
 133  
     }
 134  
 
 135  
     /**
 136  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#findMatchingOrderBy(java.lang.Class, java.util.Map, java.lang.String, boolean)
 137  
      */
 138  
     public Collection findMatchingOrderBy(Class clazz, Map fieldValues,
 139  
             String sortField, boolean sortAscending) {
 140  0
         Criteria criteria = buildCriteria(fieldValues);
 141  0
         QueryByCriteria queryByCriteria = new QueryByCriteria(clazz, criteria);
 142  
 
 143  0
         if (sortAscending) {
 144  0
             queryByCriteria.addOrderByAscending(sortField);
 145  
         } else {
 146  0
             queryByCriteria.addOrderByDescending(sortField);
 147  
         }
 148  
 
 149  0
         return getPersistenceBrokerTemplate().getCollectionByQuery(queryByCriteria);
 150  
     }
 151  
 
 152  
     /**
 153  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#save(java.lang.Object)
 154  
      */
 155  
     public void save(Object bo) throws DataAccessException {
 156  0
         getPersistenceBrokerTemplate().store(bo);
 157  0
     }
 158  
 
 159  
     /**
 160  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#save(java.util.List)
 161  
      */
 162  
     public void save(List businessObjects) throws DataAccessException {
 163  0
         for (Iterator i = businessObjects.iterator(); i.hasNext();) {
 164  0
             Object bo = i.next();
 165  0
             getPersistenceBrokerTemplate().store(bo);
 166  0
         }
 167  0
     }
 168  
 
 169  
     /**
 170  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#delete(java.lang.Object)
 171  
      */
 172  
     public void delete(Object bo) {
 173  0
         getPersistenceBrokerTemplate().delete(bo);
 174  0
     }
 175  
 
 176  
     /**
 177  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#delete(java.util.List)
 178  
      */
 179  
     public void delete(List<Object> boList) {
 180  0
         for (Object bo : boList) {
 181  0
             getPersistenceBrokerTemplate().delete(bo);
 182  
         }
 183  0
     }
 184  
 
 185  
     /**
 186  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#deleteMatching(java.lang.Class, java.util.Map)
 187  
      */
 188  
     public void deleteMatching(Class clazz, Map fieldValues) {
 189  0
         Criteria criteria = buildCriteria(fieldValues);
 190  
 
 191  0
         getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(clazz, criteria));
 192  
 
 193  
         // An ojb delete by query doesn't update the cache so we need to clear
 194  
         // the cache for everything to work property.
 195  
         // don't believe me? Read the source code to OJB
 196  0
         getPersistenceBrokerTemplate().clearCache();
 197  0
     }
 198  
 
 199  
     /**
 200  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#retrieve(java.lang.Object)
 201  
      */
 202  
     public Object retrieve(Object object) {
 203  0
         return getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQueryByIdentity(object));
 204  
     }
 205  
 
 206  
     /**
 207  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#findMatchingByExample(java.lang.Object)
 208  
      */
 209  
     public Collection findMatchingByExample(Object object) {
 210  0
         return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQueryByExample(object));
 211  
     }
 212  
 
 213  
     /**
 214  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#findMatching(java.lang.Class, org.apache.ojb.broker.query.Criteria)
 215  
      */
 216  
     public Collection findMatching(Class clazz, Criteria criteria) {
 217  0
         return findMatching(clazz, criteria, false, RiceConstants.NO_WAIT);
 218  
         /*return getPersistenceBrokerTemplate().getCollectionByQuery(
 219  
         QueryFactory.newQuery(clazz, criteria));*/
 220  
     }
 221  
 
 222  
     /**
 223  
      * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#findMatching(Class, Criteria, boolean)
 224  
      */
 225  
     public Collection findMatching(Class clazz, Criteria criteria, boolean selectForUpdate, long wait) {
 226  
         Query query;
 227  0
         if (selectForUpdate && !useSelectForUpdate) {
 228  0
             LOG.warn("Pessimistic locking was requested but select for update is disabled");
 229  
         }
 230  0
         if (selectForUpdate && useSelectForUpdate) {
 231  0
             SuffixableQueryByCriteria q = new SuffixableQueryByCriteria(clazz, criteria);
 232  
             // XXX: hax
 233  0
             Config config = ConfigContext.getCurrentContextConfig();
 234  0
             DatabasePlatform platform = null;
 235  
                         try {
 236  0
                                 platform = (DatabasePlatform) Class.forName(config.getProperty(Config.DATASOURCE_PLATFORM)).newInstance();
 237  0
                         } catch (Exception e) {
 238  0
                                 throw new RuntimeException(e.getMessage(), e);
 239  0
                         }
 240  0
             q.setQuerySuffix(" " + platform.getSelectForUpdateSuffix(wait));
 241  0
             query = q;            
 242  0
         } else {
 243  0
             query = QueryFactory.newQuery(clazz, criteria);
 244  
         }
 245  0
         return getPersistenceBrokerTemplate().getCollectionByQuery(query);
 246  
 
 247  
     }
 248  
 
 249  
     /**
 250  
      * This method will build out criteria in the key-value paradigm
 251  
      * (attribute-value).
 252  
      * 
 253  
      * @param fieldValues
 254  
      * @return
 255  
      */
 256  
     private Criteria buildCriteria(Map fieldValues) {
 257  0
         Criteria criteria = new Criteria();
 258  0
         for (Iterator i = fieldValues.entrySet().iterator(); i.hasNext();) {
 259  0
             Map.Entry e = (Map.Entry) i.next();
 260  
 
 261  0
             String key = (String) e.getKey();
 262  0
             Object value = e.getValue();
 263  0
             if (value instanceof Collection) {
 264  0
                 criteria.addIn(key, (Collection) value);
 265  
             } else {
 266  0
                 criteria.addEqualTo(key, value);
 267  
             }
 268  0
         }
 269  
 
 270  0
         return criteria;
 271  
     }
 272  
 
 273  
     /**
 274  
      * This method will build out criteria in the key-value paradigm
 275  
      * (attribute-value).
 276  
      * 
 277  
      * @param fieldValues
 278  
      * @return
 279  
      */
 280  
     private Criteria buildNegativeCriteria(Map negativeFieldValues) {
 281  0
         Criteria criteria = new Criteria();
 282  0
         for (Iterator i = negativeFieldValues.entrySet().iterator(); i.hasNext();) {
 283  0
             Map.Entry e = (Map.Entry) i.next();
 284  
 
 285  0
             String key = (String) e.getKey();
 286  0
             Object value = e.getValue();
 287  0
             if (value instanceof Collection) {
 288  0
                 criteria.addNotIn(key, (Collection) value);
 289  
             } else {
 290  0
                 criteria.addNotEqualTo(key, value);
 291  
             }
 292  0
         }
 293  
 
 294  0
         return criteria;
 295  
     }
 296  
 
 297  
         /**
 298  
          * This overridden method ...
 299  
          * 
 300  
          * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#findMatching(java.lang.Class, org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria)
 301  
          */
 302  
         @Override
 303  
         public Collection findMatching(Class clazz,
 304  
                         org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria criteria) {
 305  
                 // TODO g1zhang - THIS METHOD NEEDS JAVADOCS
 306  0
                 return null;
 307  
         }
 308  
 
 309  
         /**
 310  
          * This overridden method ...
 311  
          * 
 312  
          * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#findMatching(java.lang.Class, org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria, boolean, long)
 313  
          */
 314  
         @Override
 315  
         public Collection findMatching(Class clazz,
 316  
                         org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria criteria,
 317  
                         boolean selectForUpdate, long wait) {
 318  
                 // TODO g1zhang - THIS METHOD NEEDS JAVADOCS
 319  0
                 return null;
 320  
         }
 321  
         
 322  
 
 323  
         /**
 324  
          * This overridden method ...
 325  
          * 
 326  
          * @see org.kuali.rice.core.framework.persistence.dao.GenericDao#findMatching(java.lang.Class, java.util.Map, boolean, long)
 327  
          */
 328  
         @Override
 329  
         public Collection findMatching(Class clazz, Map criteria,
 330  
                         boolean selectForUpdate, long wait) {
 331  
                 
 332  0
                 LOG.info("*******************************calling GenericDaoOjb.findMatching(Class clazz, Map criteria, boolean selectForUpdate, long wait)");
 333  0
                 Criteria c = buildCriteria(criteria);
 334  0
                 return findMatching(clazz, c, selectForUpdate, wait);
 335  
         }
 336  
 }