From: Richard Millet Date: Sat, 13 Nov 2010 00:38:28 +0000 (+0000) Subject: CSPACE-2953: A request to create a duplicate role now returns: "POST failed: 23000... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=3e9a202fc2c6552ac0095bd9daa24f9d5018b96c;p=tmp%2Fjakarta-migration.git CSPACE-2953: A request to create a duplicate role now returns: "POST failed: 23000 : Duplicate entry 'ROLE_1_REM-1' for key 'rolename'" --- diff --git a/services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/client/test/RoleServiceTest.java b/services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/client/test/RoleServiceTest.java index 77651823b..d29ce75a9 100644 --- a/services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/client/test/RoleServiceTest.java +++ b/services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/client/test/RoleServiceTest.java @@ -174,7 +174,10 @@ public class RoleServiceTest extends AbstractServiceTestImpl { int statusCode = res.getStatus(); if (logger.isDebugEnabled()) { + logger.debug(testName + ": Role with name \"" + + knownRoleName + "\" should already exist, so this request should fail."); logger.debug(testName + ": status = " + statusCode); + logger.debug(testName + ": " + res); } Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); diff --git a/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/RoleResource.java b/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/RoleResource.java index 1663f5cd3..9e33df598 100644 --- a/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/RoleResource.java +++ b/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/RoleResource.java @@ -165,8 +165,8 @@ public class RoleResource logger.error(ServiceMessages.UNKNOWN_ERROR_MSG, e); Response response = Response.status( Response.Status.INTERNAL_SERVER_ERROR).entity(ServiceMessages.POST_FAILED - + ServiceMessages.UNKNOWN_ERROR_MSG).type("text/plain").build(); - throw new WebApplicationException(response); + + e.getMessage()).type("text/plain").build(); + throw new WebApplicationException(e, response); } } diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/DocumentException.java b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentException.java index de7271163..953641911 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/document/DocumentException.java +++ b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentException.java @@ -18,6 +18,10 @@ package org.collectionspace.services.common.document; +import java.sql.BatchUpdateException; + +import javax.persistence.RollbackException; + import org.collectionspace.services.common.ServiceException; /** @@ -26,6 +30,25 @@ import org.collectionspace.services.common.ServiceException; */ public class DocumentException extends ServiceException { + static public DocumentException createDocumentException(Throwable ex) { + DocumentException result = new DocumentException(ex); + + if (RollbackException.class.isInstance(ex) == true) { + Throwable jpaProviderCause = ex.getCause(); + if (jpaProviderCause != null) { + Throwable cause = jpaProviderCause.getCause(); + if (cause != null && BatchUpdateException.class.isInstance(cause) == true) { + BatchUpdateException bue = (BatchUpdateException)cause; + String sqlState = bue.getSQLState(); + result = new DocumentException(bue.getSQLState() + + " : " + + bue.getMessage()); + } + } + } + + return result; + } /** * Creates a new instance of DocumentException without detail message. diff --git a/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClientImpl.java b/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClientImpl.java index db943c72d..ff609dce0 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClientImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClientImpl.java @@ -19,6 +19,8 @@ package org.collectionspace.services.common.storage.jpa; import java.util.Date; import java.util.List; +import javax.persistence.RollbackException; +import java.sql.BatchUpdateException; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; @@ -89,8 +91,9 @@ public class JpaStorageClientImpl implements StorageClient { * Instantiates a new jpa storage client. */ public JpaStorageClientImpl() { + //intentionally empty } - + /* (non-Javadoc) * @see org.collectionspace.services.common.storage.StorageClient#create(org.collectionspace.services.common.context.ServiceContext, org.collectionspace.services.common.document.DocumentHandler) */ @@ -98,7 +101,7 @@ public class JpaStorageClientImpl implements StorageClient { public String create(ServiceContext ctx, DocumentHandler handler) throws BadRequestException, DocumentException { - + boolean rollbackTransaction = false; if (ctx == null) { throw new IllegalArgumentException( "create: ctx is missing"); @@ -117,31 +120,32 @@ public class JpaStorageClientImpl implements StorageClient { JaxbUtils.setValue(entity, "setCreatedAtItem", Date.class, new Date()); emf = JpaStorageUtils.getEntityManagerFactory(); em = emf.createEntityManager(); - em.getTransaction().begin(); - em.persist(entity); + em.getTransaction().begin(); { //begin of transaction block + em.persist(entity); + } em.getTransaction().commit(); handler.complete(Action.CREATE, wrapDoc); return (String) JaxbUtils.getValue(entity, "getCsid"); } catch (BadRequestException bre) { - if (em != null && em.getTransaction().isActive()) { - em.getTransaction().rollback(); - } + rollbackTransaction = true; throw bre; } catch (DocumentException de) { - if (em != null && em.getTransaction().isActive()) { - em.getTransaction().rollback(); - } + rollbackTransaction = true; throw de; } catch (Exception e) { - if (em != null && em.getTransaction().isActive()) { - em.getTransaction().rollback(); - } + rollbackTransaction = true; if (logger.isDebugEnabled()) { logger.debug("Caught exception ", e); } - throw new DocumentException(e); + throw DocumentException.createDocumentException(e); } finally { if (em != null) { + if (rollbackTransaction == true) { + if (em.getTransaction().isActive() == true) { + em.getTransaction().rollback(); + } + } + // Don't call this unless "em" is not null -hence the check above. JpaStorageUtils.releaseEntityManagerFactory(emf); } }