From e08fec457a59afbc9f816ea7f51bc8da283dc1ef Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Tue, 18 Sep 2012 21:52:10 -0700 Subject: [PATCH] CSPACE-5504: Adding code that will update relations_common table with updated/changed refNames --- .../AuthorityItemDocumentModelHandler.java | 2 +- .../services/client/IRelationsManager.java | 2 + .../services/common/ResourceBase.java | 8 +- .../common/relation/IRelationsManager.java | 112 ------------------ .../common/relation/RelationJAXBSchema.java | 10 +- .../common/relation/RelationUtils.java | 39 ++++++ .../vocabulary/RefNameServiceUtils.java | 24 +++- .../java/RemoteDocumentModelHandlerImpl.java | 21 ++-- 8 files changed, 90 insertions(+), 128 deletions(-) delete mode 100644 services/common/src/main/java/org/collectionspace/services/common/relation/IRelationsManager.java create mode 100644 services/common/src/main/java/org/collectionspace/services/common/relation/RelationUtils.java diff --git a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java index 905a6794e..4c0894de0 100644 --- a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java +++ b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java @@ -140,7 +140,7 @@ public abstract class AuthorityItemDocumentModelHandler refname = RefName.buildAuthorityItem(parentsRefName, shortIdentifier, displayName); // Now update the document model with the refname value String refNameStr = refname.toString(); - docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.REF_NAME, refNameStr); + docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.REF_NAME, refNameStr); // REM - This field is deprecated now that the refName is part of the collection_space core schema } catch (Exception e) { logger.error(e.getMessage(), e); diff --git a/services/client/src/main/java/org/collectionspace/services/client/IRelationsManager.java b/services/client/src/main/java/org/collectionspace/services/client/IRelationsManager.java index a4c73960b..408198cca 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/IRelationsManager.java +++ b/services/client/src/main/java/org/collectionspace/services/client/IRelationsManager.java @@ -20,6 +20,7 @@ public interface IRelationsManager { /** The Constant SUBJECT. */ static public final String SUBJECT = "subjectCsid"; + static public final String SUBJECT_REFNAME = "subjectRefName"; static public final String SUBJECT_QP = "sbj"; static public final String SUBJECT_TYPE = "subjectType"; static public final String SUBJECT_TYPE_QP = SUBJECT_QP + "Type"; @@ -30,6 +31,7 @@ public interface IRelationsManager { /** The Constant OBJECT. */ static public final String OBJECT = "objectCsid"; + static public final String OBJECT_REFNAME = "objectRefName"; static public final String OBJECT_QP = "obj"; static public final String OBJECT_TYPE = "objectType"; static public final String OBJECT_TYPE_QP = OBJECT_QP + "Type"; diff --git a/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java b/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java index d9e420e7b..7034abfd8 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java +++ b/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java @@ -28,10 +28,12 @@ import java.util.List; import java.util.Map; import javax.ws.rs.*; import javax.ws.rs.core.*; + import org.collectionspace.services.client.IClientQueryParams; import org.collectionspace.services.client.IQueryManager; import org.collectionspace.services.client.PoxPayloadIn; import org.collectionspace.services.client.PoxPayloadOut; + import org.collectionspace.services.common.api.RefName; import org.collectionspace.services.common.api.Tools; import org.collectionspace.services.common.authorityref.AuthorityRefList; @@ -42,20 +44,22 @@ import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.DocumentFilter; 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.query.QueryManager; import org.collectionspace.services.common.vocabulary.RefNameServiceUtils; import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.AuthRefConfigInfo; + import org.collectionspace.services.config.ClientType; import org.collectionspace.services.config.service.DocHandlerParams; import org.collectionspace.services.config.service.ListResultField; import org.collectionspace.services.config.service.ServiceBindingType; + import org.collectionspace.services.jaxb.AbstractCommonList; import org.collectionspace.services.nuxeo.client.java.DocumentModelHandler; -import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl; import org.collectionspace.services.nuxeo.util.NuxeoUtils; + import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; import org.jboss.resteasy.util.HttpResponseCodes; + import org.nuxeo.ecm.core.api.DocumentModel; import org.nuxeo.ecm.core.api.DocumentModelList; import org.nuxeo.ecm.core.api.repository.RepositoryInstance; diff --git a/services/common/src/main/java/org/collectionspace/services/common/relation/IRelationsManager.java b/services/common/src/main/java/org/collectionspace/services/common/relation/IRelationsManager.java deleted file mode 100644 index ed7e17b12..000000000 --- a/services/common/src/main/java/org/collectionspace/services/common/relation/IRelationsManager.java +++ /dev/null @@ -1,112 +0,0 @@ -/** - * IRelationsManager.java - * - * {Purpose of This Class} - * - * {Other Notes Relating to This Class (Optional)} - * - * $LastChangedBy: $ - * $LastChangedRevision: $ - * $LastChangedDate: $ - * - * This document is a part of the source code and related artifacts - * for CollectionSpace, an open source collections management system - * for museums and related institutions: - * - * http://www.collectionspace.org - * http://wiki.collectionspace.org - * - * Copyright (c) 2009 {Contributing Institution} - * - * Licensed under the Educational Community License (ECL), Version 2.0. - * You may not use this file except in compliance with this License. - * - * You may obtain a copy of the ECL 2.0 License at - * https://source.collectionspace.org/collection-space/LICENSE.txt - */ -package org.collectionspace.services.common.relation; - -import java.util.List; - -import org.collectionspace.services.common.document.DocumentException; -import org.collectionspace.services.relation.RelationsCommon; -import org.nuxeo.ecm.core.api.DocumentModel; - -/** - * The Interface IRelationsManager. - */ -public interface IRelationsManager { - - /** - * Gets the relationships for the entity corresponding to the CSID=csid. - * The csid refers to either the subject *OR* the object. - * - * @param nuxeoRepoSession the nuxeo repo session - * @param csid the csid - * - * @return the relationships - * - * @throws DocumentException the document exception - */ - public List getRelationships(Object nuxeoRepoSession, String csid) - throws DocumentException; - - /** - * Gets the relationships. - * - * @param repoSession the repo session - * @param subjectCsid the subject csid - * @param relationType the relation type - * @param objectCsid the object csid - * - * @return the relationships - * - * @throws DocumentException the document exception - */ - public List getRelationships(Object repoSession, - String subjectCsid, - String relationType, - String objectCsid) - throws DocumentException; - - /** - * Creates the relationship. - * - * @param repoSession the repo session - * @param subjectCsid the subject csid - * @param predicate the predicate - * @param objectCsid the object csid - * - * @return the relation - * - * @throws DocumentException the document exception - */ - public RelationsCommon createRelationship(Object repoSession, String subjectCsid, - String predicate, String objectCsid) throws DocumentException; - - /** - * Gets the q property name. - * - * @param propertyName the property name - * - * @return the q property name - */ - public String getQPropertyName(String propertyName); - - /** - * Checks if is query match. - * - * @param documentModel the document model - * @param subjectCsid the subject csid - * @param predicate the predicate - * @param objectCsid the object csid - * - * @return true, if is query match - * - * @throws DocumentException the document exception - */ - public boolean isQueryMatch(DocumentModel documentModel, - String subjectCsid, - String predicate, - String objectCsid) throws DocumentException; -} diff --git a/services/common/src/main/java/org/collectionspace/services/common/relation/RelationJAXBSchema.java b/services/common/src/main/java/org/collectionspace/services/common/relation/RelationJAXBSchema.java index c28d367c5..2237211d0 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/relation/RelationJAXBSchema.java +++ b/services/common/src/main/java/org/collectionspace/services/common/relation/RelationJAXBSchema.java @@ -26,6 +26,8 @@ */ package org.collectionspace.services.common.relation; +import org.collectionspace.services.client.IRelationsManager; + /** * The Interface RelationJAXBSchema. */ @@ -46,13 +48,13 @@ public interface RelationJAXBSchema { final static String RELATIONSHIP_TYPE_DISPLAYNAME = "predicateDisplayName"; final static String SUBJECT_URI = "subjectUri"; - final static String SUBJECT_CSID = "subjectCsid"; - final static String SUBJECT_REFNAME = "subjectRefName"; + final static String SUBJECT_CSID = IRelationsManager.SUBJECT; + final static String SUBJECT_REFNAME = IRelationsManager.SUBJECT_REFNAME; final static String SUBJECT_DOCTYPE = "subjectDocumentType"; final static String OBJECT_URI = "objectUri"; - final static String OBJECT_CSID = "objectCsid"; - final static String OBJECT_REFNAME = "objectRefName"; + final static String OBJECT_CSID = IRelationsManager.OBJECT; + final static String OBJECT_REFNAME = IRelationsManager.OBJECT_REFNAME; final static String OBJECT_DOCTYPE = "objectDocumentType"; } diff --git a/services/common/src/main/java/org/collectionspace/services/common/relation/RelationUtils.java b/services/common/src/main/java/org/collectionspace/services/common/relation/RelationUtils.java new file mode 100644 index 000000000..b19a685a5 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/relation/RelationUtils.java @@ -0,0 +1,39 @@ +package org.collectionspace.services.common.relation; + +import org.collectionspace.services.client.PoxPayloadIn; +import org.collectionspace.services.client.PoxPayloadOut; +import org.collectionspace.services.common.api.Tools; +import org.collectionspace.services.common.context.ServiceContext; +import org.collectionspace.services.common.document.DocumentFilter; +import org.collectionspace.services.common.query.QueryContext; +import org.collectionspace.services.common.repository.RepositoryClient; +import org.collectionspace.services.nuxeo.util.NuxeoUtils; +import org.nuxeo.ecm.core.api.DocumentModelList; +import org.nuxeo.ecm.core.api.repository.RepositoryInstance; + +public class RelationUtils { + + private static void updateRefNamesInRelations( + ServiceContext ctx, + RepositoryClient repoClient, + RepositoryInstance repoSession, + String targetField, + String oldRefName, + String newRefName) { + + DocumentFilter filter = new DocumentFilter(); + String oldOrderBy = filter.getOrderByClause(); + if (Tools.isEmpty(oldOrderBy) == true){ + filter.setOrderByClause(DocumentFilter.ORDER_BY_LAST_UPDATED); + } + QueryContext queryContext = new QueryContext(ctx, handler); + + RepositoryInstance repoSession = null; + + DocumentModelList docList = null; + String query = NuxeoUtils.buildNXQLQuery(ctx, queryContext); + docList = repoSession.query(query); + } + + +} diff --git a/services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameServiceUtils.java b/services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameServiceUtils.java index afc573659..3b004d7da 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameServiceUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameServiceUtils.java @@ -39,6 +39,7 @@ import org.nuxeo.ecm.core.api.repository.RepositoryInstance; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.collectionspace.services.client.IRelationsManager; import org.collectionspace.services.client.PoxPayloadIn; import org.collectionspace.services.client.PoxPayloadOut; import org.collectionspace.services.common.ServiceMain; @@ -204,9 +205,29 @@ public class RefNameServiceUtils { this.property = prop; } } + private static final Logger logger = LoggerFactory.getLogger(RefNameServiceUtils.class); private static ArrayList refNameServiceTypes = null; + + public static void updateRefNamesInRelations( + ServiceContext ctx, + RepositoryClient repoClient, + RepositoryInstance repoSession, + String oldRefName, + String newRefName) { + // + // First, look for and update all the places where the refName is the "subject" of the relationship + // + updateRefNamesInRelations(ctx, repoClient, repoSession, IRelationsManager.SUBJECT_REFNAME, oldRefName, newRefName); + + // + // Next, look for and update all the places where the refName is the "object" of the relationship + // + updateRefNamesInRelations(ctx, repoClient, repoSession, IRelationsManager.OBJECT_REFNAME, oldRefName, newRefName); + + } + public static List getConfiguredAuthorityRefs(ServiceContext ctx) { List authRefFields = ((AbstractServiceContextImpl) ctx).getAllPartsPropertyValues( @@ -355,6 +376,7 @@ public class RefNameServiceUtils { } return refNameServiceTypes; } + // Seems like a good value - no real data to set this well. // Note: can set this value lower during debugging; e.g. to 3 - ADR 2012-07-10 private static final int N_OBJS_TO_UPDATE_PER_LOOP = 100; @@ -379,7 +401,7 @@ public class RefNameServiceUtils { if (!(repoClient instanceof RepositoryJavaClientImpl)) { throw new InternalError("updateAuthorityRefDocs() called with unknown repoClient type!"); } - try { // REM - How can we deal with transaction and timeout issues here. + try { // REM - How can we deal with transaction and timeout issues here? final int pageSize = N_OBJS_TO_UPDATE_PER_LOOP; DocumentModelList docList; boolean morePages = true; diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandlerImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandlerImpl.java index 013c9daa2..5a19e398d 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandlerImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RemoteDocumentModelHandlerImpl.java @@ -1394,17 +1394,22 @@ public abstract class RemoteDocumentModelHandlerImpl if (hasRefNameUpdate() == true) { // We have work to do. if (logger.isDebugEnabled()) { - String eol = System.getProperty("line.separator"); - logger.debug("Need to find and update references to Item." + eol - + " Old refName" + oldRefNameOnUpdate + eol + final String EOL = System.getProperty("line.separator"); + logger.debug("Need to find and update references to Item." + EOL + + " Old refName" + oldRefNameOnUpdate + EOL + " New refName" + newRefNameOnUpdate); } ServiceContext ctx = getServiceContext(); - RepositoryClient repoClient = getRepositoryClient(ctx); - String refNameProp = getRefPropName(); - - int nUpdated = RefNameServiceUtils.updateAuthorityRefDocs(ctx, repoClient, this.getRepositorySession(), - oldRefNameOnUpdate, newRefNameOnUpdate, refNameProp); + RepositoryClient repoClient = getRepositoryClient(ctx); + + RepositoryInstance repoSession = this.getRepositorySession(); + RefNameServiceUtils.updateRefNamesInRelations(ctx, repoClient, repoSession, + oldRefNameOnUpdate, newRefNameOnUpdate); + + int nUpdated = RefNameServiceUtils.updateAuthorityRefDocs(ctx, repoClient, repoSession, + oldRefNameOnUpdate, newRefNameOnUpdate, getRefPropName()); + + // Finished so log a message. if (logger.isDebugEnabled()) { logger.debug("Updated " + nUpdated + " instances of oldRefName to newRefName"); } -- 2.47.3