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

net.smartlab.web.history.HistorizedBusinessObjectFactory Maven / Gradle / Ivy

Go to download

SmartWeb is a web application development meta framework based on Jakarta Struts, Hibernate and other open source frameworks and libraries.

The newest version!
/*
 * The SmartWeb Framework
 * Copyright (C) 2004-2006
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * For further informations on the SmartWeb Framework please visit
 *
 *                        http://smartweb.sourceforge.net
 */
package net.smartlab.web.history;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.smartlab.web.BusinessObjectFactory;
import net.smartlab.web.DAOException;
import net.smartlab.web.UndefinedKeyException;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;

/**
 * TODO documentation
 * 
 * @author rlogiacco
 */
public abstract class HistorizedBusinessObjectFactory extends BusinessObjectFactory {

	/**
	 * TODO documentation
	 */
	protected HistorizedBusinessObjectFactory() {
		super();
	}

	public Object searchLast(Serializable key) throws DAOException {
		return this.searchLast(key, null, null);
	}

	public Object searchLast(Serializable key, Date start, Date end) throws DAOException {
		key = this.getBaseFactory().convertKey(key);
		Object actual = this.getBaseFactory().findByKey(key);
		if (actual != null && end == null) {
			/*
			 * Wrapper wrapper = new Wrapper((BusinessObject)actual); return
			 * (HistorizedBusinessObject
			 * )Proxy.newProxyInstance(Thread.currentThread
			 * ().getContextClassLoader(), new Class[] {actual.getClass(),
			 * HistorizedBusinessObject.class}, wrapper);
			 */
			return actual;
		} else {
			Session session = this.current();
			try {
				Criteria criteria = session.createCriteria(this.getMappedClass());
				criteria.add(Expression.idEq(key));
				criteria.add(Expression.between("lastModified", start, end));
				criteria.addOrder(Order.desc("lastModified"));
				HistorizedBusinessObject last = (HistorizedBusinessObject)criteria.uniqueResult();
				// FIX ME
				return last;
			} catch (HibernateException he) {
				logger.debug("findByKey(key = " + key + ") - deserialization failed", he);
				throw new UndefinedKeyException(key, this.getMappedClass(), he);
			}
		}
	}

	public Object searchFirst(Serializable key) throws DAOException {
		return this.searchFirst(key, null, null);
	}

	public Object searchFirst(Serializable key, Date start, Date end) throws DAOException {
		key = this.getBaseFactory().convertKey(key);
		Session session = this.current();
		try {
			Criteria criteria = session.createCriteria(this.getMappedClass());
			criteria.add(Expression.idEq(key));
			criteria.add(Expression.between("lastModified", start, end));
			criteria.addOrder(Order.asc("lastModified"));
			HistorizedBusinessObject first = (HistorizedBusinessObject)criteria.uniqueResult();
			if (first == null) {
				return this.getBaseFactory().findByKey(key);
			} else {
				return first;
			}
		} catch (HibernateException he) {
			logger.debug("findByKey(key = " + key + ") - deserialization failed", he);
			throw new UndefinedKeyException(key, this.getMappedClass(), he);
		}
	}

	public List searchHistory(Serializable key) throws DAOException {
		return this.searchHistory(key, null, null);
	}

	public List searchHistory(Serializable key, Date start, Date end) throws DAOException {
		List history = new ArrayList();
		Session session = this.current();
		try {
			Criteria criteria = session.createCriteria(this.getMappedClass());
			criteria.add(Expression.idEq(key));
			criteria.add(Expression.between("lastModified", start, end));
			criteria.addOrder(Order.asc("lastModified"));
			history.addAll(criteria.list());
			history.add(this.getBaseFactory().findByKey(key));
			return history;
		} catch (HibernateException he) {
			logger.debug("findByKey(key = " + key + ") - deserialization failed", he);
			throw new UndefinedKeyException(key, this.getMappedClass(), he);
		}
	}

	protected abstract BusinessObjectFactory getBaseFactory();
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy