From b787fae5eaae3a5b189394b52754a7afbca6a6ce Mon Sep 17 00:00:00 2001 From: pkupczyk <pkupczyk> Date: Mon, 1 Oct 2012 09:26:34 +0000 Subject: [PATCH] SP-282/BIS-179: Failure of Sample Registration with automatically generated codes - improve error messages SVN: 26863 --- .../SampleDataAccessExceptionTranslator.java | 13 ++--- .../SampleUniqueCodeViolationException.java | 38 +++++++++++++++ ...eUniqueCodeViolationExceptionAbstract.java | 48 +++++++++++++++++++ ...SampleUniqueSubcodeViolationException.java | 40 ++++++++++++++++ .../web/server/GenericClientService.java | 29 +++++++++-- 5 files changed, 156 insertions(+), 12 deletions(-) create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/exception/SampleUniqueCodeViolationException.java create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/exception/SampleUniqueCodeViolationExceptionAbstract.java create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/exception/SampleUniqueSubcodeViolationException.java diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDataAccessExceptionTranslator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDataAccessExceptionTranslator.java index a17a7614199..a94a87bd3ca 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDataAccessExceptionTranslator.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDataAccessExceptionTranslator.java @@ -21,9 +21,10 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.springframework.dao.DataAccessException; -import org.springframework.dao.DataIntegrityViolationException; import ch.systemsx.cisd.common.db.SQLStateUtils; +import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.exception.SampleUniqueCodeViolationException; +import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.exception.SampleUniqueSubcodeViolationException; /** * Extracts information about an actual cause of sample related DataAccessException. @@ -73,10 +74,7 @@ public class SampleDataAccessExceptionTranslator UniqueViolationMessage message = UniqueViolationMessage.get(exception); if (message != null) { - throw new DataIntegrityViolationException( - String.format( - "Insert/Update of Sample (Code: %s) failed because sample with the same code already exists.", - message.getSampleCode())); + throw new SampleUniqueCodeViolationException(message.getSampleCode(), exception); } } @@ -85,10 +83,7 @@ public class SampleDataAccessExceptionTranslator UniqueViolationMessage message = UniqueViolationMessage.get(exception); if (message != null) { - throw new DataIntegrityViolationException( - String.format( - "Insert/Update of Sample (Code: %s) failed because sample of the same type with the same subcode already exists.", - message.getSampleCode())); + throw new SampleUniqueSubcodeViolationException(message.getSampleCode(), exception); } } diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/exception/SampleUniqueCodeViolationException.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/exception/SampleUniqueCodeViolationException.java new file mode 100644 index 00000000000..c4cfa602ae9 --- /dev/null +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/exception/SampleUniqueCodeViolationException.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.generic.server.dataaccess.db.exception; + +/** + * @author pkupczyk + */ +public class SampleUniqueCodeViolationException extends SampleUniqueCodeViolationExceptionAbstract +{ + + private static final long serialVersionUID = 1L; + + public SampleUniqueCodeViolationException(String sampleCode, Throwable cause) + { + super(sampleCode, getMessage(sampleCode), cause); + } + + private static String getMessage(String sampleCode) + { + return String + .format("Insert/Update of sample (code: %s) failed because sample with the same code already exists.", + sampleCode); + } +} diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/exception/SampleUniqueCodeViolationExceptionAbstract.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/exception/SampleUniqueCodeViolationExceptionAbstract.java new file mode 100644 index 00000000000..dc1a2e2285f --- /dev/null +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/exception/SampleUniqueCodeViolationExceptionAbstract.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.generic.server.dataaccess.db.exception; + +import org.springframework.dao.DataIntegrityViolationException; + +/** + * @author pkupczyk + */ +public class SampleUniqueCodeViolationExceptionAbstract extends DataIntegrityViolationException +{ + + private static final long serialVersionUID = 1L; + + private String sampleCode; + + public SampleUniqueCodeViolationExceptionAbstract(String sampleCode, String msg, Throwable cause) + { + super(msg, cause); + + if (sampleCode == null) + { + throw new IllegalArgumentException("Sample code cannot be null"); + } + + this.sampleCode = sampleCode; + } + + public String getSampleCode() + { + return sampleCode; + } + +} diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/exception/SampleUniqueSubcodeViolationException.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/exception/SampleUniqueSubcodeViolationException.java new file mode 100644 index 00000000000..3517e93f988 --- /dev/null +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/exception/SampleUniqueSubcodeViolationException.java @@ -0,0 +1,40 @@ +/* + * Copyright 2012 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.generic.server.dataaccess.db.exception; + +/** + * @author pkupczyk + */ +public class SampleUniqueSubcodeViolationException extends + SampleUniqueCodeViolationExceptionAbstract +{ + + private static final long serialVersionUID = 1L; + + public SampleUniqueSubcodeViolationException(String sampleCode, Throwable cause) + { + super(sampleCode, getMessage(sampleCode), cause); + } + + private static String getMessage(String sampleCode) + { + return String + .format("Insert/Update of sample (code: %s) failed because sample of the same type with the same subcode already exists.", + sampleCode); + } + +} diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/GenericClientService.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/GenericClientService.java index 787f758f851..8216f1db872 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/GenericClientService.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/GenericClientService.java @@ -38,6 +38,7 @@ import ch.systemsx.cisd.openbis.generic.client.web.server.AbstractClientService; import ch.systemsx.cisd.openbis.generic.client.web.server.AttachmentRegistrationHelper; import ch.systemsx.cisd.openbis.generic.client.web.server.UploadedFilesBean; import ch.systemsx.cisd.openbis.generic.client.web.server.translator.UserFailureExceptionTranslator; +import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.exception.SampleUniqueCodeViolationExceptionAbstract; import ch.systemsx.cisd.openbis.generic.shared.IServer; import ch.systemsx.cisd.openbis.generic.shared.basic.TechId; import ch.systemsx.cisd.openbis.generic.shared.basic.dto.BatchOperationKind; @@ -162,11 +163,13 @@ public class GenericClientService extends AbstractClientService implements IGene public final List<BatchRegistrationResult> registerSamples(final SampleType sampleType, final String sessionKey, final String defaultGroupIdentifier, boolean updateExisting) { + boolean isAutogenerateCodes = defaultGroupIdentifier != null; + BatchOperationKind operationKind = updateExisting ? BatchOperationKind.UPDATE : BatchOperationKind.REGISTRATION; BatchSamplesOperation info = - parseSamples(sampleType, sessionKey, defaultGroupIdentifier, - defaultGroupIdentifier != null, true, null, operationKind); + parseSamples(sampleType, sessionKey, defaultGroupIdentifier, isAutogenerateCodes, + true, null, operationKind); try { final String sessionToken = getSessionToken(); @@ -174,7 +177,27 @@ public class GenericClientService extends AbstractClientService implements IGene return info.getResultList(); } catch (final ch.systemsx.cisd.common.exceptions.UserFailureException e) { - throw UserFailureExceptionTranslator.translate(e); + if (e.getCause() instanceof SampleUniqueCodeViolationExceptionAbstract) + { + SampleUniqueCodeViolationExceptionAbstract codeException = + (SampleUniqueCodeViolationExceptionAbstract) e.getCause(); + + if (isAutogenerateCodes) + { + throw new UserFailureException( + String.format( + "Import failed because the autogenerated codes are no longer unique. Somebody has created a sample (code: %s) that matched one of the autogenerated codes. Please run the import once again.", + codeException.getSampleCode())); + } else + { + throw new UserFailureException(String.format( + "Import failed because sample (code: %s) already exists.", + codeException.getSampleCode())); + } + } else + { + throw UserFailureExceptionTranslator.translate(e); + } } } -- GitLab