]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5012: Now appears to correctly add parent authority service and parent vocabul...
authorAron Roberts <aron@socrates.berkeley.edu>
Thu, 24 May 2012 19:58:29 +0000 (12:58 -0700)
committerAron Roberts <aron@socrates.berkeley.edu>
Thu, 24 May 2012 19:58:29 +0000 (12:58 -0700)
services/imports/service/src/main/java/org/collectionspace/services/imports/TemplateExpander.java

index 43d06f389680962e2b4cba2ad46aa3238497e8a6..3f609e7d6dc6a840b8faaa23ff8a503a13ca5465 100644 (file)
 
 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<String,String> docTypeSvcNameRegistry = new HashMap<String,String>();
+    private static Map<String,String> docTypeSvcNameRegistry = new HashMap<String,String>();
+    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: