All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sakaiproject.genericdao.base.BaseGeneralGenericDao Maven / Gradle / Ivy

Go to download

Generic Dao is a Java package which allows a developer to skip writing DAOs for their persistence objects when they are using Spring and/or Hibernate. The package was originally created by Aaron Zeckoski for the Evaluation System project but was repackaged to make it distributable by request. It is used in the RSF framework (http://www2.caret.cam.ac.uk/rsfwiki/). Note about the BeanUtils provided dependency: BeanUtils is not required if you are not using it in your project. Note about the Hibernate provided dependency: Hibernate is not required if you are not using it in your project.

The newest version!
/**
 * $Id$
 * $URL$
 * BaseGeneralGenericDao.java - genericdao - May 14, 2008 9:31:51 PM - azeckoski
 **************************************************************************
 * Copyright (c) 2008 Aaron Zeckoski
 * Licensed under the Apache License, Version 2.0
 * 
 * A copy of the Apache License has been included in this 
 * distribution and is available at: http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 * Aaron Zeckoski ([email protected]) ([email protected]) ([email protected])
 */

package org.sakaiproject.genericdao.base;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.sakaiproject.genericdao.api.GeneralGenericDao;

/**
 * This is the simple base implementation which includes shared methods and should be extended,
 * this handles the caching checks and the interceptors on the basic methods,
 * NOTE that findAll is not cached since it would put the entire set into the cache
 * 
 * @author Aaron Zeckoski ([email protected])
 */
public abstract class BaseGeneralGenericDao extends BaseBasicGenericDao implements GeneralGenericDao {

   /**
    * MUST override this method
    */
   protected  int baseCountAll(Class type) {
       throw new UnsupportedOperationException("Not Implemented");
   }

   /**
    * MUST override this method
    */
   protected  int baseSaveSet(Class type, Set entities) {
       throw new UnsupportedOperationException("Not Implemented");
   }

   /**
    * MUST override this method
    */
   protected  int baseDeleteSet(Class type, Serializable[] ids) {
       throw new UnsupportedOperationException("Not Implemented");
   }

   // COMMON CODE

   public  List findAll(Class type) {
      return findAll(type, 0, 0);
   }

   public  int countAll(Class type) {
      checkClass(type);
      int count = 0;

      // check the cache first
      boolean usedCache = false;
      String searchCacheName = getSearchCacheName(type);
      String cacheKey = "countAll::" + type.getName();
      if (getCacheProvider().exists(searchCacheName, cacheKey)) {
         Integer iCount = (Integer) getCacheProvider().get(searchCacheName, cacheKey);
         if (iCount != null) {
            count = iCount.intValue();
            usedCache = true;
         }
      }

      if (! usedCache) {
         count = baseCountAll(type);

         // cache the id results for the search
         getCacheProvider().put(searchCacheName, cacheKey, Integer.valueOf(count));
      }
      return count;
   }

   public  void deleteSet(Class type, Serializable[] ids) {
      checkClass(type);
      if (ids.length > 0) {
         String operation = "deleteSet";
         beforeWrite(operation, type, ids, null);
   
         int changes = baseDeleteSet(type, ids);
   
         afterWrite(operation, type, ids, null, changes);
   
         // clear all removed items from the cache
         String cacheName = getCacheName(type);
         for (int i = 0; i < ids.length; i++) {
            if (ids[i] != null) {
               String key = ids[i].toString();
               getCacheProvider().remove(cacheName, key);
            }
         }
         // clear the search caches
         getCacheProvider().clear(getSearchCacheName(type));
      }
   }

   public  void saveSet(Set entities) {
      if (entities == null || entities.isEmpty()) {
         System.out.println("WARN: Empty list of entities for saveSet, nothing to do...");
      } else {
         Class type = checkEntitySet(entities);
   
         String operation = "saveSet";
         beforeWrite(operation, type, null, entities.toArray());
   
         int changes = baseSaveSet(type, entities);
   
         afterWrite(operation, type, null, entities.toArray(), changes);
   
         // clear all saved items from the cache
         String cacheName = getCacheName(type);
         for (T t : entities) {
            Object id = baseGetIdValue(t);
            if (id != null) {
               String key = id.toString();
               getCacheProvider().remove(cacheName, key);
            }
         }
         // clear the search caches
         getCacheProvider().clear(getSearchCacheName(type));
      }
   }

   public  void deleteSet(Set entities) {
      if (entities.size() > 0) {
         Class type = checkEntitySet(entities);
         List ids = new ArrayList();
         for (T t : entities) {
            Object id = baseGetIdValue(t);
            if (id != null) {
               ids.add(id);
            }
         }
         deleteSet(type, ids.toArray(new Serializable[ids.size()]));
      }
   }

   @SuppressWarnings("unchecked")
   public void saveMixedSet(Set[] entitySets) {
      for (int i=0; i checkEntitySet(Set entities) {
      Class entityClass = null;
      Iterator it = entities.iterator();
      while(it.hasNext()) {
         Object entity = it.next();
         if (entityClass == null) {
            entityClass = (Class) findClass(entity);
         }
         if (! checkClass(entityClass).isInstance(entity)) {
            throw new IllegalArgumentException("Entity set item " +
                  entity.toString() + " is not of type: " + entityClass +
                  ", the type is: " + entity.getClass() +
            " (All items must be of consistent persistent type)");
         }
      }
      return entityClass;
   }

}