blob: f7c06c42514c6b6b32bd2d5ae2f6047c75a4e9ed [file] [log] [blame]
/*******************************************************************************
* Copyright (C) 2007 The University of Manchester
*
* Modifications to the initial code base are copyright of their
* respective authors, or their employers as appropriate.
*
* This program 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 program 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 program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
******************************************************************************/
package net.sf.taverna.t2.reference.impl;
import net.sf.taverna.t2.reference.DaoException;
import net.sf.taverna.t2.reference.ReferenceSet;
import net.sf.taverna.t2.reference.ReferenceSetDao;
import net.sf.taverna.t2.reference.T2Reference;
import net.sf.taverna.t2.reference.T2ReferenceType;
import net.sf.taverna.t2.reference.annotations.GetIdentifiedOperation;
import net.sf.taverna.t2.reference.annotations.PutIdentifiedOperation;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* An implementation of ReferenceSetDao based on Spring's HibernateDaoSupport.
* To use this in spring inject a property 'sessionFactory' with either a
* {@link org.springframework.orm.hibernate3.LocalSessionFactoryBean LocalSessionFactoryBean}
* or the equivalent class from the T2Platform module to add SPI based
* implementation discovery and mapping. To use outside of Spring ensure you
* call the setSessionFactory(..) method before using this (but really, use it
* from Spring, so much easier).
*
* @author Tom Oinn
*
*/
public class HibernateReferenceSetDao extends HibernateDaoSupport implements
ReferenceSetDao {
/**
* Store the specified new reference set
*
* @param rs
* a reference set, must not already exist in the database.
* @throws DaoException
* if the entry already exists in the database, if the supplied
* reference set isn't an instance of ReferenceSetImpl or if
* something else goes wrong connecting to the database
*/
@PutIdentifiedOperation
public void store(ReferenceSet rs) throws DaoException {
if (rs.getId() == null) {
throw new DaoException(
"Supplied reference set has a null ID, allocate "
+ "an ID before calling the store method in the dao.");
} else if (rs.getId().getReferenceType().equals(
T2ReferenceType.ReferenceSet) == false) {
throw new DaoException(
"Strangely the reference set ID doesn't have type "
+ "T2ReferenceType.ReferenceSet, something has probably "
+ "gone badly wrong somewhere earlier!");
}
if (rs instanceof ReferenceSetImpl) {
try {
getHibernateTemplate().save(rs);
} catch (Exception ex) {
throw new DaoException(ex);
}
} else {
throw new DaoException(
"Supplied reference set not an instance of ReferenceSetImpl");
}
}
/**
* Update a pre-existing entry in the database
*
* @param rs
* the reference set to update. This must already exist in the
* database
* @throws DaoException
*/
@PutIdentifiedOperation
public void update(ReferenceSet rs) throws DaoException {
if (rs.getId() == null) {
throw new DaoException(
"Supplied reference set has a null ID, allocate "
+ "an ID before calling the store method in the dao.");
} else if (rs.getId().getReferenceType().equals(
T2ReferenceType.ReferenceSet) == false) {
throw new DaoException(
"Strangely the reference set ID doesn't have type "
+ "T2ReferenceType.ReferenceSet, something has probably "
+ "gone badly wrong somewhere earlier!");
}
if (rs instanceof ReferenceSetImpl) {
try {
getHibernateTemplate().update(rs);
} catch (Exception ex) {
throw new DaoException(ex);
}
} else {
throw new DaoException(
"Supplied reference set not an instance of ReferenceSetImpl");
}
}
/**
* Fetch a reference set by id
*
* @param ref
* the ReferenceSetT2ReferenceImpl to fetch
* @return a retrieved ReferenceSetImpl
* @throws DaoException
* if the supplied reference is of the wrong type or if
* something goes wrong fetching the data or connecting to the
* database
*/
@GetIdentifiedOperation
public ReferenceSetImpl get(T2Reference ref) throws DaoException {
if (ref == null) {
throw new DaoException(
"Supplied reference is null, can't retrieve.");
} else if (ref.getReferenceType().equals(T2ReferenceType.ReferenceSet) == false) {
throw new DaoException(
"This dao can only retrieve reference of type T2Reference.ReferenceSet");
}
if (ref instanceof T2ReferenceImpl) {
try {
return (ReferenceSetImpl) getHibernateTemplate().get(
ReferenceSetImpl.class,
((T2ReferenceImpl) ref).getCompactForm());
} catch (Exception ex) {
throw new DaoException(ex);
}
} else {
throw new DaoException(
"Reference must be an instance of T2ReferenceImpl");
}
}
}