diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/MethodUtils.java b/common/source/java/ch/systemsx/cisd/common/utilities/MethodUtils.java index c33e10dee26480c9d62558938f9059f0289a7ee8..6d9330e8c650f6b416b2f445597520704f7789cd 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/MethodUtils.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/MethodUtils.java @@ -17,6 +17,7 @@ package ch.systemsx.cisd.common.utilities; import java.lang.reflect.Method; +import java.util.Arrays; import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel; @@ -66,8 +67,8 @@ public final class MethodUtils * Returns the <code>Method</code> on the stack of <var>level</var>. * <p> * <code>level=0</code> is this method itself, <code>level=1</code> is the method that - * called it and so forth. This method internally uses {@link Class#getMethods()} to retrieve - * the <code>Method</code> (meaning that <code>private</code> methods will not be found). + * called it and so forth. This method internally uses {@link Class#getDeclaredMethods()} to + * retrieve the <code>Method</code>. * </p> * <p> * IMPORTANT NOTE: You should carefully use this method in a class having more than one method @@ -76,20 +77,25 @@ public final class MethodUtils * </p> * * @see StackTraceElement#getMethodName() - * @return <code>null</code> if none could be found. + * @throw {@link IndexOutOfBoundsException} if given <var>level</var> smaller or equal to + * number of stack trace elements. */ public final static Method getMethodOnStack(final int level) { + assert level > -1 : "Level must be positive."; + final StackTraceElement[] elements = new Throwable().getStackTrace(); if (elements.length <= level) { - return null; + throw new IndexOutOfBoundsException(String.format("Not enough elements: %d <= %d", + elements.length, level)); } final StackTraceElement element = elements[level]; final String methodName = element.getMethodName(); + Method[] methods = null; try { - final Method[] methods = Class.forName(element.getClassName()).getMethods(); + methods = Class.forName(element.getClassName()).getDeclaredMethods(); for (final Method method : methods) { if (method.getName().equals(methodName)) @@ -102,7 +108,9 @@ public final class MethodUtils { throw CheckedExceptionTunnel.wrapIfNecessary(ex); } - return null; + throw new IllegalArgumentException(String.format( + "Method name '%s' could not be found in methods '%s'.", methodName, Arrays + .toString(methods))); } /** diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AbstractTypeDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AbstractTypeDAO.java index a299e683cf9450e9552aa7f1963f3f4d4adafd41..cf2fdcf0a06abecdcf0f4364e479ee1a73c626ab 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AbstractTypeDAO.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AbstractTypeDAO.java @@ -18,14 +18,16 @@ package ch.systemsx.cisd.openbis.generic.server.dataaccess.db; import java.util.List; -import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.hibernate.SessionFactory; +import org.hibernate.criterion.DetachedCriteria; +import org.hibernate.criterion.Restrictions; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.support.JdbcAccessor; import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogFactory; +import ch.systemsx.cisd.common.utilities.MethodUtils; import ch.systemsx.cisd.openbis.generic.shared.dto.AbstractTypePE; import ch.systemsx.cisd.openbis.generic.shared.dto.CodeConverter; import ch.systemsx.cisd.openbis.generic.shared.dto.DatabaseInstancePE; @@ -64,44 +66,26 @@ abstract class AbstractTypeDAO<T extends AbstractTypePE> extends AbstractDAO throws DataAccessException { assert code != null : "Unspecified code"; - final List<T> list; - if (appendDatabaseInstance) - { - list = - cast(getHibernateTemplate() - .find( - String.format("select st from %s st where st.code = ? " - + "and st.databaseInstance.id = ?", getEntityClass() - .getSimpleName()), - new Object[] - { CodeConverter.tryToDatabase(code), - getDatabaseInstance().getId() })); - } else + final DetachedCriteria criteria = DetachedCriteria.forClass(getEntityClass()); + criteria.add(Restrictions.eq("code", CodeConverter.tryToDatabase(code))); + if (appendDatabaseInstance) { - list = - cast(getHibernateTemplate().find( - String.format("select st from %s st where st.code = ? ", - getEntityClass().getSimpleName()), new Object[] - { CodeConverter.tryToDatabase(code) })); + criteria.add(Restrictions.eq("databaseInstance", getDatabaseInstance())); } + final List<T> list = cast(getHibernateTemplate().findByCriteria(criteria)); final T entity = tryFindEntity(list, "type"); if (operationLog.isDebugEnabled()) { - operationLog.debug("tryFind" + getTypeDescription() + "ByCode(" + code + "): '" - + entity + "'"); + operationLog.debug(String.format("%s(%s,%s): Entity type '%s' has been found.", + MethodUtils.describeMethod(MethodUtils.getCurrentMethod()), code, + appendDatabaseInstance, entity, list.size())); } return entity; } abstract Class<?> getEntityClass(); - final String getTypeDescription() - { - final String className = getEntityClass().getSimpleName(); - return StringUtils.substring(className, 0, className.length() - 2); - } - final List<T> listTypes() throws DataAccessException { return listTypes(true); @@ -109,26 +93,17 @@ abstract class AbstractTypeDAO<T extends AbstractTypePE> extends AbstractDAO final List<T> listTypes(final boolean appendDatabaseInstance) throws DataAccessException { - final List<T> list; + final DetachedCriteria criteria = DetachedCriteria.forClass(getEntityClass()); if (appendDatabaseInstance) { - list = - cast(getHibernateTemplate().find( - String.format("from %s st where st.databaseInstance = ?", - getEntityClass().getSimpleName()), new Object[] - { getDatabaseInstance() })); - - } else - { - list = - cast(getHibernateTemplate().find( - String.format("from %s", getEntityClass().getSimpleName()))); - + criteria.add(Restrictions.eq("databaseInstance", getDatabaseInstance())); } + final List<T> list = cast(getHibernateTemplate().findByCriteria(criteria)); if (operationLog.isDebugEnabled()) { - operationLog.debug("list" + getTypeDescription() + "s: " + list.size() - + " type(s) have been found."); + operationLog.debug(String.format("%s(%s): %d entity type(s) have been found.", + MethodUtils.describeMethod(MethodUtils.getCurrentMethod()), + appendDatabaseInstance, list.size())); } return list; } diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleTypeDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleTypeDAO.java index 79a80e2c8baa89f6efb3b36b54032576e1aabd19..1ba0968c9215f15c875a2d49ece2d0c9d4387dfe 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleTypeDAO.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleTypeDAO.java @@ -81,8 +81,9 @@ final class SampleTypeDAO extends AbstractTypeDAO<SampleTypePE> implements ISamp final List<SampleTypePE> list = cast(getHibernateTemplate().findByCriteria(criteria)); if (operationLog.isDebugEnabled()) { - operationLog.debug(String.format("%s(%s): %d type(s) have been found.", MethodUtils - .describeMethod(MethodUtils.getCurrentMethod()), onlyListable, list.size())); + operationLog.debug(String.format("%s(%s): %d sample type(s) have been found.", + MethodUtils.describeMethod(MethodUtils.getCurrentMethod()), onlyListable, list + .size())); } return list; }