From 09a31bca52c6d306523248b7ff74d9c1622f00db Mon Sep 17 00:00:00 2001 From: remillet Date: Fri, 8 Dec 2017 22:37:47 -0800 Subject: [PATCH] NOJIRA: Misc transaction handling cleanup and fixes. --- .../common/relation/RelationResource.java | 40 ++++- .../common/repository/RepositoryClient.java | 15 ++ .../client/java/RepositoryClientImpl.java | 166 +++++++++++------- 3 files changed, 152 insertions(+), 69 deletions(-) diff --git a/services/common/src/main/java/org/collectionspace/services/common/relation/RelationResource.java b/services/common/src/main/java/org/collectionspace/services/common/relation/RelationResource.java index 12dedc2c5..6b0947494 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/relation/RelationResource.java +++ b/services/common/src/main/java/org/collectionspace/services/common/relation/RelationResource.java @@ -41,10 +41,13 @@ import org.collectionspace.services.common.relation.nuxeo.RelationsUtils; import org.collectionspace.services.relation.RelationsCommon; import org.collectionspace.services.relation.RelationsCommonList; import org.collectionspace.services.relation.RelationsCommonList.RelationListItem; - +import org.jboss.resteasy.util.HttpResponseCodes; +import org.nuxeo.ecm.core.api.DocumentModel; +import org.nuxeo.ecm.core.api.DocumentModelList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; import java.util.List; import javax.ws.rs.Consumes; @@ -139,13 +142,36 @@ public class RelationResource extends NuxeoBasedResource { @DELETE public Response delete(@Context UriInfo uriInfo) { - Response result = Response.status(200).build(); - - RelationsCommonList relationsList = this.getList(null, uriInfo); - for (RelationListItem relation : relationsList.getRelationListItem()) { - Response deleteResponse = this.delete(relation.getCsid()); - } + Response result = Response.status(HttpResponseCodes.SC_OK).build(); + List csidList = new ArrayList(); + try { + RelationsCommonList relationsList = this.getList(null, uriInfo); + for (RelationListItem relation : relationsList.getRelationListItem()) { + csidList.add(relation.getCsid()); + } + + if (csidList.isEmpty() == false) { + ServiceContext ctx = createServiceContext(); + DocumentHandler handler = createDocumentHandler(ctx); + getRepositoryClient(ctx).delete(ctx, csidList, handler); + } else { + result = Response.status(HttpResponseCodes.SC_NOT_FOUND).build(); + } + } catch (Exception e) { + String separator = ", "; + String payloadDescription = "unknown"; + if (csidList.isEmpty() == false) { + StringBuffer tempStr = new StringBuffer(); + for (String csid : csidList) { + tempStr.append(csid); + tempStr.append(separator); + } + payloadDescription = String.format("{%s}", tempStr.substring(0, tempStr.length() - separator.length())); + } + throw bigReThrow(e, ServiceMessages.DELETE_FAILED, payloadDescription); + } + return result; } diff --git a/services/common/src/main/java/org/collectionspace/services/common/repository/RepositoryClient.java b/services/common/src/main/java/org/collectionspace/services/common/repository/RepositoryClient.java index bf6316622..23f7376f8 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/repository/RepositoryClient.java +++ b/services/common/src/main/java/org/collectionspace/services/common/repository/RepositoryClient.java @@ -31,6 +31,7 @@ import org.collectionspace.services.common.document.DocumentException; import org.collectionspace.services.common.document.DocumentHandler; import org.collectionspace.services.common.document.DocumentNotFoundException; import org.collectionspace.services.common.document.DocumentWrapper; +import org.collectionspace.services.common.document.TransactionException; import org.collectionspace.services.common.storage.StorageClient; import org.collectionspace.services.config.tenant.RepositoryDomainType; import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface; @@ -178,4 +179,18 @@ public interface RepositoryClient extends StorageClient { */ boolean reindex(DocumentHandler handler, String indexid) throws DocumentNotFoundException, DocumentException; + + /** + * Delete a list of objects + * + * @param ctx + * @param idList + * @param handler + * @return + * @throws DocumentNotFoundException + * @throws DocumentException + * @throws TransactionException + */ + boolean delete(ServiceContext ctx, List idList, DocumentHandler handler) + throws DocumentNotFoundException, DocumentException, TransactionException; } diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryClientImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryClientImpl.java index a2950f30b..2293bb966 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryClientImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryClientImpl.java @@ -204,13 +204,15 @@ public class RepositoryClientImpl implements RepositoryClient ctx, CoreSessionInterface repoSession, @@ -1703,7 +1711,7 @@ public class RepositoryClientImpl implements RepositoryClient idList, DocumentHandler handler) throws DocumentNotFoundException, DocumentException, TransactionException { boolean result = true; @@ -1715,41 +1723,46 @@ public class RepositoryClientImpl implements RepositoryClient wrapDoc = null; + try { + DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id); + wrapDoc = new DocumentWrapperImpl(repoSession.getDocument(docRef)); + ((DocumentModelHandler) handler).setRepositorySession(repoSession); + if (handler.handle(Action.DELETE, wrapDoc) == true) { + repoSession.removeDocument(docRef); + if (logger.isDebugEnabled()) { + String msg = String.format("DELETE - User '%s' hard-deleted document CSID=%s of type %s.", + ctx.getUserId(), id, ctx.getDocumentType()); + logger.debug(msg); + } + } else { + String msg = String.format("Could not delete %s resource with csid=%s.", + handler.getServiceContext().getServiceName(), id); + throw new DocumentException(msg); + } + } catch (org.nuxeo.ecm.core.api.DocumentNotFoundException ce) { + String msg = logException(ce, + String.format("Could not find %s resource/record to delete with CSID=%s", ctx.getDocumentType(), id)); + throw new DocumentNotFoundException(msg, ce); + } + repoSession.save(); + handler.complete(Action.DELETE, wrapDoc); } - repoSession.save(); - handler.complete(Action.DELETE, wrapDoc); } catch (DocumentException de) { + rollbackTransaction(repoSession); throw de; - } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Caught exception ", e); - } + } catch (Throwable e) { + rollbackTransaction(repoSession); throw new NuxeoDocumentException(e); } finally { if (repoSession != null) { @@ -1759,6 +1772,25 @@ public class RepositoryClientImpl implements RepositoryClient idList = new ArrayList(); + idList.add(id); + result = delete(ctx, idList, handler); + + return result; + } /* (non-Javadoc) * @see org.collectionspace.services.common.storage.StorageClient#delete(org.collectionspace.services.common.context.ServiceContext, java.lang.String, org.collectionspace.services.common.document.DocumentHandler) @@ -1819,7 +1851,8 @@ public class RepositoryClientImpl implements RepositoryClient