From c49102237ca5b9c438f68b5b7441107a13f9544a Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Thu, 24 May 2012 12:58:29 -0700 Subject: [PATCH] CSPACE-5012: Now appears to correctly add parent authority service and parent vocabulary CSID to collectionspace_core:uri for authority services. --- .../services/imports/TemplateExpander.java | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/services/imports/service/src/main/java/org/collectionspace/services/imports/TemplateExpander.java b/services/imports/service/src/main/java/org/collectionspace/services/imports/TemplateExpander.java index 43d06f389..3f609e7d6 100644 --- a/services/imports/service/src/main/java/org/collectionspace/services/imports/TemplateExpander.java +++ b/services/imports/service/src/main/java/org/collectionspace/services/imports/TemplateExpander.java @@ -24,19 +24,24 @@ package org.collectionspace.services.imports; +import java.io.StringReader; import java.util.HashMap; import java.util.Map; import java.util.UUID; - +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; import org.collectionspace.services.client.AuthorityClient; -import org.collectionspace.services.common.*; +import org.collectionspace.services.common.IFragmentHandler; +import org.collectionspace.services.common.ServiceMain; +import org.collectionspace.services.common.XmlSaxFragmenter; +import org.collectionspace.services.common.XmlTools; import org.collectionspace.services.common.api.FileTools; import org.collectionspace.services.common.api.Tools; import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl; import org.collectionspace.services.common.datetime.GregorianCalendarDateTimeUtils; import org.collectionspace.services.config.service.ServiceBindingType; import org.collectionspace.services.nuxeo.util.NuxeoUtils; - import org.dom4j.Document; import org.dom4j.Element; import org.xml.sax.InputSource; @@ -51,7 +56,8 @@ import org.xml.sax.InputSource; */ public class TemplateExpander { - static Map docTypeSvcNameRegistry = new HashMap(); + private static Map docTypeSvcNameRegistry = new HashMap(); + private static final String IN_AUTHORITY_XPATH = "//inAuthority"; protected static String var(String theVar){ return "\\$\\{"+theVar+"\\}"; @@ -88,7 +94,7 @@ public class TemplateExpander { wrapperTmpl = searchAndReplaceVar(wrapperTmpl, "updatedDate", nowTime); wrapperTmpl = Tools.searchAndReplace(wrapperTmpl, var("uri"), - getDocUri(tenantId, SERVICE_TYPE, docID)); + getDocUri(tenantId, SERVICE_TYPE, docID, partTmpl)); /* TenantBindingConfigReaderImpl tReader = ServiceMain.getInstance().getTenantBindingConfigReader(); @@ -139,7 +145,8 @@ public class TemplateExpander { XmlSaxFragmenter.parse(requestSource, chopPath, callback, false); } - private static String getDocUri(String tenantId, String SERVICE_TYPE, String docID) throws Exception { + private static String getDocUri(String tenantId, String SERVICE_TYPE, String docID, + String partTmpl) throws Exception { // FIXME: Use already-defined constants, likely to be found in another package final String AUTHORITY_TYPE = "authority"; @@ -158,7 +165,7 @@ public class TemplateExpander { return uri; } // FIXME: Get the inAuthority value from the payload or from a new param for this method - String inAuthorityID = "inAuthorityValueHere"; // stub + String inAuthorityID = getInAuthorityValue(partTmpl); uri = getAuthorityUri(authoritySvcName, inAuthorityID, docID); } else if (serviceType.equalsIgnoreCase(OBJECT_TYPE) || serviceType.equalsIgnoreCase(PROCEDURE_TYPE) ) { @@ -202,6 +209,23 @@ public class TemplateExpander { + '/' + docID; } + private static String getInAuthorityValue(String xmlFragment) { + return getXpathValueFromXmlFragment(IN_AUTHORITY_XPATH, xmlFragment); + } + + private static String getXpathValueFromXmlFragment(String xpathExpr, String xmlFragment) { + String xpathValue = ""; + try { + XPath xpath = XPathFactory.newInstance().newXPath(); + InputSource input = new InputSource(new StringReader(xmlFragment)); + xpathValue = xpath.evaluate(xpathExpr, input); + } catch (XPathExpressionException e) { + // Do nothing here. + } + return xpathValue; + + } + // FIXME: Create equivalent getUri-type method(s) for sub-resources, such as contacts /** This inner class is the callback target for calls to XmlSaxFragmenter, for example: -- 2.47.3