From 0286364427d56e7bdf6acca8c4815e94c1c96fe8 Mon Sep 17 00:00:00 2001 From: buczekp <buczekp> Date: Mon, 28 Sep 2009 12:26:32 +0000 Subject: [PATCH] [LMS-1061] fixed 'Bad error message on attempt to enter sample with already existing code' SVN: 12741 --- .../bo/DataAccessExceptionTranslator.java | 2 +- .../generic/server/business/bo/SampleBO.java | 5 +++++ .../server/dataaccess/db/SampleDAO.java | 22 ++++++++++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/DataAccessExceptionTranslator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/DataAccessExceptionTranslator.java index 4bd041ac8fc..02f63f13a9a 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/DataAccessExceptionTranslator.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/DataAccessExceptionTranslator.java @@ -54,7 +54,7 @@ public final class DataAccessExceptionTranslator public final static String DETAILED_FOREIGN_KEY_VIOLATION_FORMAT = FOREIGN_KEY_VIOLATION_FORMAT + " To find out which exactly objects are connected to this object " - + "go to it's Detail view or use Search."; + + "go to its Detail view or use Search."; private DataAccessExceptionTranslator() { diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/SampleBO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/SampleBO.java index 7adc69dbb29..cd8f86dbfa6 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/SampleBO.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/SampleBO.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.Set; import org.springframework.dao.DataAccessException; +import org.springframework.dao.DataIntegrityViolationException; import ch.systemsx.cisd.common.exceptions.UserFailureException; import ch.systemsx.cisd.openbis.generic.server.business.bo.util.SampleOwner; @@ -179,6 +180,10 @@ public final class SampleBO extends AbstractSampleBusinessObject implements ISam try { getSampleDAO().createSample(sample); + } catch (final DataIntegrityViolationException ex) + { + // needed because we throw an exception in DAO instead of relying on DB constraint + throw UserFailureException.fromTemplate(ex.getMessage()); } catch (final DataAccessException ex) { throwException(ex, String.format("Sample '%s'", sample.getSampleIdentifier())); diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAO.java index 81e7bb68785..f1c0553094a 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAO.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAO.java @@ -16,6 +16,7 @@ package ch.systemsx.cisd.openbis.generic.server.dataaccess.db; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -27,11 +28,14 @@ import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Restrictions; import org.hibernate.validator.ClassValidator; import org.springframework.dao.DataAccessException; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.jdbc.UncategorizedSQLException; import org.springframework.jdbc.support.JdbcAccessor; import org.springframework.orm.hibernate3.HibernateTemplate; import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogFactory; +import ch.systemsx.cisd.common.utilities.ExceptionUtils; import ch.systemsx.cisd.openbis.generic.server.dataaccess.ISampleDAO; import ch.systemsx.cisd.openbis.generic.shared.dto.CodeConverter; import ch.systemsx.cisd.openbis.generic.shared.dto.DatabaseInstancePE; @@ -172,7 +176,23 @@ public class SampleDAO extends AbstractGenericEntityDAO<SamplePE> implements ISa internalCreateSample(sample, hibernateTemplate, new ClassValidator<SamplePE>(SamplePE.class), true); - hibernateTemplate.flush(); + try + { + hibernateTemplate.flush(); + } catch (UncategorizedSQLException e) + { + // need to deal with exception thrown by trigger checking code uniqueness + final SQLException sqlExceptionOrNull = + ExceptionUtils.tryGetThrowableOfClass(e, SQLException.class); + if (sqlExceptionOrNull != null && sqlExceptionOrNull.getNextException() != null) + { + throw new DataIntegrityViolationException(sqlExceptionOrNull.getNextException() + .getMessage()); + } else + { + throw e; + } + } } public List<SamplePE> listSamplesWithPropertiesByExperiment(final ExperimentPE experiment) -- GitLab