]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-2953: A request to create a duplicate role now returns: "POST failed: 23000...
authorRichard Millet <richard.millet@berkeley.edu>
Sat, 13 Nov 2010 00:38:28 +0000 (00:38 +0000)
committerRichard Millet <richard.millet@berkeley.edu>
Sat, 13 Nov 2010 00:38:28 +0000 (00:38 +0000)
services/authorization-mgt/client/src/test/java/org/collectionspace/services/authorization/client/test/RoleServiceTest.java
services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/RoleResource.java
services/common/src/main/java/org/collectionspace/services/common/document/DocumentException.java
services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClientImpl.java

index 77651823bf79ec6a4c55e0f58f57c1a4952b7977..d29ce75a98ebc25f53728febbdb3901b63a9116c 100644 (file)
@@ -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));
index 1663f5cd3e6470a4a14d3980827c944f115d24dd..9e33df598ca466d46e00a64da62fe0787144971f 100644 (file)
@@ -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);
         }
     }
 
index de727116343d4beda2cda560aa4e1cb959ba9361..953641911a13b334e1c8fa92a3dc78afa0425148 100644 (file)
 
 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 <code>DocumentException</code> without detail message.
index db943c72d8be7e8ebaaf200bd2df657e5d6ccc61..ff609dce083c4d3098dac2533e1683873ea54901 100644 (file)
@@ -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);
             }
         }