Am migrating from Hibernate 2.0.
here is the previous code
Code:
/**
* Washington Mutual
* (c) 2004, All Rights Reserved
*/
package com.wamu.dashboard.pricing;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import com.wamu.dashboard.util.Constant;
import com.wamu.dashboard.util.command.CommandException;
import com.wamu.dashboard.util.command.CommandParameters;
import com.wamu.dashboard.util.command.CommandResult;
import com.wamu.dashboard.util.command.InvalidParameterException;
import com.wamu.dashboard.util.hibernate.HibernateCommand;
import com.wamu.dashboard.util.hibernate.HibernateUtil;
import com.wamu.dashboard.util.logging.Logger;
import com.wamu.dashboard.util.logging.WebLayerLogHelper;
/**
* <code>PricingCommand</code>
*
*
* Created: Fri Jul 09 15:49:12 2004
*
* @author <a href="mailto:erik.curiel@wamu.net">Erik Curiel</a>
* @version $Id: PricingCommand.java,v 1.12 2005/07/29 17:48:48 lohashi Exp $
*/
public class PricingCommand extends HibernateCommand
{
private final static String CLASSNAME = PricingCommand.class.getName();
// command-names
public static String FETCH_ALL_CATEGORIES = "fetchAllCategories";
public static String FETCH_CATEGORY_BY_ID = "fetchCategoryById";
public static String FETCH_PRODUCT_BY_ID = "fetchProductById";
public static String UPDATE_CATEGORY_BY_ID = "updateCategoryById";
public static String UPDATE_PRODUCT_BY_ID = "updateProductById";
public static String DELETE_CATEGORY_BY_ID = "deleteCategoryById";
public static String DELETE_PRODUCT_BY_ID = "deleteProductById";
public static String CREATE_CATEGORY = "createCategory";
public static String CREATE_PRODUCT = "createProduct";
public PricingCommand(Map params)
{
super(params);
}
public PricingCommand(PricingCommandParameters params)
{
super(params);
}
/**
* <code>validate</code> does nuthin' coz there's, like, nuthin' to
* validate, ya know?
*
* @exception InvalidParameterException
*/
public void validate() throws InvalidParameterException
{
}
public void parametrize(CommandParameters params)
{
//TODO: make this command conform to the new ParametrizedCommand interface
}
/**
* <code>runCommand</code>, oddly enough, runs a command
*
* @return a <code>CommandResult</code> value
* @exception CommandException if an error occurs
*/
protected CommandResult runCommand() throws CommandException
{
PricingCommandParameters params = (PricingCommandParameters) this.getCommandParameters();
Session session = null;
Logger.debug(CLASSNAME, "in runCommand(), with params: |" + params.toString() + "|");
try {
// retrieve session
session = HibernateUtil.getSession();
Logger.debug(CLASSNAME, "got hibernate session: |" + session + "|");
List results = new ArrayList();
String commandName = params.getCommand();
// execute hibernate command, depending on the params
if (FETCH_ALL_CATEGORIES.equals(commandName)) {
results =
new ArrayList(fetchByQuery(session, (FetchPricingCommandParameters) params));
} else if (FETCH_CATEGORY_BY_ID.equals(commandName) ||
FETCH_PRODUCT_BY_ID.equals(commandName)) {
results.add(fetchById(session, (FetchPricingCommandParameters) params));
} else if (UPDATE_CATEGORY_BY_ID.equals(commandName) ||
UPDATE_PRODUCT_BY_ID.equals(commandName)) {
results.add(updateById(session, params));
} else if (DELETE_CATEGORY_BY_ID.equals(commandName) ||
DELETE_PRODUCT_BY_ID.equals(commandName)) {
results.add(deleteById(session, params));
} else if (CREATE_CATEGORY.equals(commandName) || CREATE_PRODUCT.equals(commandName)) {
results.add(create(session, params));
} else {
throw new IllegalArgumentException("runCommand passed illegal params: |" +
params.toString() + "|");
}
Logger.debug(CLASSNAME,
"returning from runCommand(): " + Constant.NEW_LINE.value() +
WebLayerLogHelper.stringify("results", results, 1));
return new CommandResult(commandName, results);
} catch (HibernateException he) {
String msg =
"runCommand() failed for the params:" + Constant.NEW_LINE.value() +
params.toString();
Logger.logError(CLASSNAME, he, msg);
throw new CommandException(msg, he);
}
} // end runCommand()
//
// utility methods
//
/**
* <code>deleteById</code> deletes the given object given from the
* database, returning a copy of the object just deleted
*
* @param session a <code>Session</code> value
* @param params a <code>PricingCommandParameters</code> value
* @return a <code>Object</code> value
* @exception HibernateException if an error occurs
*/
private Object deleteById(Session session, final PricingCommandParameters params)
throws HibernateException
{
Logger.debug(CLASSNAME,
"entering deleteById(), with params: |" + String.valueOf(params) + "|");
String targetType = params.getCommandTargetType();
Integer targetId = params.getCommandTarget();
Class targetClass = null;
Object target = null;
// if we're deleting a product, first remove prod from category
// and update the category so as to reflect the new product list;
// if we're deleting a category, we need first to delete all the
// associated products
ProductCategory category = null;
if (PricingCommandParameters.PRODUCT_PARAM.equals(targetType)) {
targetClass = Product.class;
target = populateById(session, targetClass, targetId);
category = ((Product) target).getCategory();
java.util.SortedSet prods = category.getProducts();
// if category was empty, this may be null
if (prods == null) {
prods =
new org.hibernate.collection.PersistentSortedSet((org.hibernate.engine.SessionImplementor) session,
new TreeSet(new ProductComparator()));
}
prods.remove(target);
Logger.debug(CLASSNAME,
"deleteById(), about to update category after having removed from it product: |" +
((Product) target).getDisplayName() + "| of category: |" + category.getName() +
"|");
update(session, category);
Logger.debug(CLASSNAME, "deleteById(), updated category: |" + category.toString() +
"|");
} else if (PricingCommandParameters.CATEGORY_PARAM.equals(targetType)) {
targetClass = ProductCategory.class;
target = populateById(session, targetClass, targetId);
category = (ProductCategory) target;
java.util.SortedSet prods = category.getProducts();
Logger.debug(CLASSNAME,
"deleteById(), fetched products for about to be deleted category: |" +
category.toString() + "|");
// if category was empty, this may be null
if (prods != null) {
for (Iterator prodIter = prods.iterator(); prodIter.hasNext();) {
// delete
Product prod = (Product) prodIter.next();
Logger.debug(CLASSNAME,
"deleteById(), deleting subproduct: |" + prod.getDisplayName() +
"| of category: |" + category.getName() + "|");
try {
delete(session, prod);
} catch (HibernateException he) {
String msg =
"caught HibernateException trying to deleteById sub-product: |" +
prod.getDisplayName() + "| before deleting category for params:" +
Constant.NEW_LINE.value() + String.valueOf(params);
throw new HibernateException(msg, he);
}
} // end for (Iterator prodIter = prods.iterator(); prodIter.hasNext();)
} // end if (prods != null)
} // end else if (PricingCommandParameters.CATEGORY_PARAM.equals(targetType))
// delete
delete(session, target);
//finally, close the session to make sure all future sessions get
//updated properly from the persistent store
HibernateUtil.closeSession();
//everything's cool
return target;
}
/**
* <code>fetchByQuery</code> returns a list of persistent objects
* materialized from the database/session matching the params
*
* @param session a <code>Session</code> value
* @param params a <code>PricingCommandParameters</code> value
* @return a <code>List</code> value
* @exception HibernateException if an error occurs
*/
private List fetchByQuery(final Session session, final FetchPricingCommandParameters params)
throws HibernateException
{
Logger.debug(CLASSNAME,
"entering fetchByQuery(), with params: |" + String.valueOf(params) + "|");
return session.createQuery(params.getQuery());
}
/**
* <code>fetchById</code> returns the persistent object materialized
* from the database/session matching the given primary-key
*
* @param session a <code>Session</code> value
* @param params a <code>PricingCommandParameters</code> value
* @return an <code>Object</code> value
* @exception HibernateException if an error occurs
*/
private Object fetchById(Session session, final FetchPricingCommandParameters params)
throws HibernateException
{
Logger.debug(CLASSNAME,
"entering fetchById(), with params: |" + String.valueOf(params) + "|");
return session.createQuery(params.getQuery());
}
/**
* <code>populateById</code> returns a persistent object, based on
* the given class, materialized from the database/session with the
* given primary-key
*
* @param session a <code>Session</code> value
* @param targetClass a <code>Class</code> value
* @param targetId an <code>Integer</code> value
* @return an <code>Object</code> value
* @exception HibernateException if an error occurs
*/
private Object populateById(Session session, final Class targetClass, final Integer targetId)
throws HibernateException
{
Logger.debug(CLASSNAME,
"entering populateById(), with class/id: |" + String.valueOf(targetClass) + "/" +
String.valueOf(targetId) + "|");
Object target = session.load(targetClass, targetId);
Logger.debug(CLASSNAME,
"returning from populateById(), with target: |" + String.valueOf(target) + "|");
return target;
}
/**
* <code>create</code> creates a new persistent object of the given
* type, populating it with the given params, and returns it
*
* @param session a <code>Session</code> value
* @param params a <code>PricingCommandParameters</code> value
* @return an <code>Object</code> value
* @exception HibernateException if an error occurs
*/
private Object create(Session session, final PricingCommandParameters params)
throws HibernateException
{
Logger.debug(CLASSNAME, "entering create(), with params: |" + String.valueOf(params) + "|");
String targetType = params.getCommandTargetType();
Object target = null;
Integer targetId = null;
if (PricingCommandParameters.CATEGORY_PARAM.equals(targetType)) {
target = new ProductCategory(params);
} else if (PricingCommandParameters.PRODUCT_PARAM.equals(targetType)) {
//if we're creating a product, we need the persistent parent
//already in the new object
target = new Product(params);
((Product) target).setCategory((ProductCategory) populateById(session,
ProductCategory.class, params.getCategoryId()));
}
Transaction tx = null;
try {
tx = session.beginTransaction();
targetId = (Integer) session.save(target);
tx.commit();
} catch (HibernateException he) {
String msg =
"caught HibernateException trying to create for params:" +
Constant.NEW_LINE.value() + String.valueOf(params);
try {
tx.rollback();
} catch (HibernateException nhe) {
HibernateUtil.closeSession();
throw new HibernateException(msg + Constant.NEW_LINE.value() +
"PricingCommand.create: caught HibernateException (next in chain) trying to roll-back a transaction after having caught this HibernateException:" +
Constant.NEW_LINE.value() + he.getMessage() + Constant.NEW_LINE.value() +
WebLayerLogHelper.stringify(he), nhe);
}
throw new HibernateException(msg, he);
}
target = populateById(session, target.getClass(), targetId);
// if we're creating a product, update the category to reflect
// the new product list
if (PricingCommandParameters.PRODUCT_PARAM.equals(targetType)) {
ProductCategory category = ((Product) target).getCategory();
java.util.SortedSet prods = category.getProducts();
// if category was empty, this may be null
if (prods == null) {
prods =
new org.hibernate.collection.PersistentSortedSet((org.hibernate.engine.SessionImplementor) session,
new TreeSet(new ProductComparator()));
}
prods.add(target);
update(session, category);
}
//finally, close the session to make sure all future sessions get
//updated properly from the persistent store
HibernateUtil.closeSession();
// we're cool
return target;
} // end create ()
/**
* <code>updateById</code> updates a persistent object of the given type,
* populating it with the params, returning the new object
*
* @param session a <code>Session</code> value
* @param params a <code>PricingCommandParameters</code> value
* @return an <code>Object</code> value
* @exception HibernateException if an error occurs
*/
private Object updateById(Session session, final PricingCommandParameters params)
throws HibernateException
{
Logger.debug(CLASSNAME, "entering update(), with params: |" + String.valueOf(params) + "|");
String targetType = params.getCommandTargetType();
Integer targetId = params.getCommandTarget();
Class targetClass = null;
if (PricingCommandParameters.CATEGORY_PARAM.equals(targetType)) {
targetClass = ProductCategory.class;
} else if (PricingCommandParameters.PRODUCT_PARAM.equals(targetType)) {
targetClass = Product.class;
}
Object target = populateById(session, targetClass, targetId);
if (targetClass == ProductCategory.class) {
ProductCategory category = (ProductCategory) target;
category.setName(params.getCategoryName());
} else if (targetClass == Product.class) {
Product product = (Product) target;
product.setProductCode(params.getProductCode());
product.setDisplayName(params.getProductDisplayName());
product.setOptionArm(params.getProductOptionArm());
}
target = update(session, target);
// if we're updating a product, update the category to reflect the
// new product list
if (PricingCommandParameters.PRODUCT_PARAM.equals(targetType)) {
ProductCategory category = ((Product) target).getCategory();
java.util.SortedSet prods = category.getProducts();
// if category was empty, this may be null
if (prods == null) {
prods =
new org.hibernate.collection.PersistentSortedSet((org.hibernate.engine.SessionImplementor) session,
new TreeSet(new ProductComparator()));
}
for (Iterator prodIter = prods.iterator(); prodIter.hasNext();) {
Product prod = (Product) prodIter.next();
if (prod.getProductId().equals(targetId)) {
prods.remove(prod);
break;
}
}
prods.add(target);
update(session, category);
}
//finally, close the session to make sure all future sessions get
//updated properly from the persistent store
HibernateUtil.closeSession();
return target;
}
//
// utlity methods used only by other private utility methods
//
/**
* <code>delete</code>
*
* @param session a <code>Session</code> value
* @param target an <code>Object</code> value
* @exception HibernateException if an error occurs
*/
private void delete(final Session session, final Object target)
throws HibernateException
{
Transaction tx = null;
try {
Logger.debug(CLASSNAME, "in delete(), about to delete: |" + String.valueOf(target) +
"|");
tx = session.beginTransaction();
session.delete(target);
Logger.debug(CLASSNAME, "in delete(), successfully deleted, about to commit");
tx.commit();
} catch (HibernateException he) {
try {
tx.rollback();
} catch (HibernateException nhe) {
HibernateUtil.closeSession();
throw new HibernateException(Constant.NEW_LINE.value() +
"PricingCommand.delete: caught HibernateException (next in chain) trying to roll-back a transaction after having caught previous HibernateException:" +
Constant.NEW_LINE.value() + he.getMessage() + Constant.NEW_LINE.value() +
WebLayerLogHelper.stringify(he), nhe);
}
throw he;
}
}
/**
* <code>update</code>
*
* @param session a <code>Session</code> value
* @param target an <code>Object</code> value
* @return an <code>Object</code> value
* @exception HibernateException if an error occurs
*/
private Object update(Session session, final Object target)
throws HibernateException
{
Transaction tx = null;
try {
tx = session.beginTransaction();
session.update(target);
tx.commit();
} catch (HibernateException he) {
String msg =
"caught HibernateException trying to update target:" + Constant.NEW_LINE.value() +
String.valueOf(target);
try {
tx.rollback();
} catch (HibernateException nhe) {
HibernateUtil.closeSession();
throw new HibernateException(msg + Constant.NEW_LINE.value() +
"PricingCommand.update: caught HibernateException (next in chain) trying to roll-back a transaction after having caught this HibernateException:" +
Constant.NEW_LINE.value() + he.getMessage() + Constant.NEW_LINE.value() +
WebLayerLogHelper.stringify(he), nhe);
}
throw new HibernateException(msg, he);
}
return target;
}
//
// over-ridden methods
//
/**
* Retrieve a String representation of this object
* preceded by <code>tabbing</code> tabs
*
* @return a <code>String</code> representation of this object.
*/
public String toString(final int tabbing)
{
String tabs = "";
for (int i = 0; i < tabbing; i++)
tabs += "\t";
return tabs + CLASSNAME + Constant.NEW_LINE.value() +
WebLayerLogHelper.stringify("parameters", this.parameters, tabbing + 1) +
Constant.NEW_LINE.value() + this.getCommandParameters().toString(tabbing + 1);
}
/**
* Retrieve a String representation of this object
*
* @return a <code>String</code> representation of this object.
* @see Object#toString()
*/
public String toString()
{
return this.toString(0);
}
} // end class PricingCommand