]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5845: First minor changes toward storing collectionspace_core:refName values...
authorAron Roberts <aron@socrates.berkeley.edu>
Fri, 1 Feb 2013 23:03:32 +0000 (15:03 -0800)
committerAron Roberts <aron@socrates.berkeley.edu>
Sat, 9 Mar 2013 01:27:18 +0000 (17:27 -0800)
CSPACE-5845: Created initial (failing) test of refName value in collectionspace_core.

CSPACE-5845: Placeholder method added for inserting refName values into collectionspace_core on import.

CSPACE-5845: Plain old id-style refNames now generated by Imports service and added to collectionspace_core:refName.

CSPACE-5845: Added (as yet untested) code to insert a provided refName, if any, into collectionspace_core:refName, and to add a display name to the ID form of refNames if the relevant service supports hierarchy.

CSPACE-5845: Generation of display name for refNames for record types with hierarchy is now working, with its own unit test.

CSPACE-5845: Add null checks. Add tests for extracting refName value from an authority record and adding that value to collectionspace_core:refName.

CSPACE-5845: Remove currently-irrelevant comments.

CSPACE-5845: Removed call in (partially-implemented) service test class that was triggering dependency-related issues.

CSPACE-5845: Added code (untested) to generate short identifier-based refNames for authority records, and to skip generating refNames for authority item records (rather than defaulting to giving them id-based refNames).

CSPACE-5845: Added test for generated authority record refName.

12 files changed:
services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/import-collectionobject-varexpansion.xml [new file with mode: 0644]
services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/import-locationauthority-varexpansion.xml [new file with mode: 0644]
services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/imports.xml
services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/res/import-collectionobject-varexpansion.res.xml [new file with mode: 0644]
services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/res/import-collectionobject.res.xml [new file with mode: 0644]
services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/res/import-locationauthority-varexpansion.res.xml [new file with mode: 0644]
services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/res/import-locationauthority.res.xml [new file with mode: 0644]
services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/res/import-objectexit-dollarsign.res.xml
services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/res/import-objectexit-varexpansion.res.xml
services/imports/service/src/main/java/org/collectionspace/services/imports/TemplateExpander.java
services/imports/service/src/main/resources/templates/service-document.xml
services/imports/service/src/test/java/org/collectionspace/services/test/ImportsServiceTest.java

diff --git a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/import-collectionobject-varexpansion.xml b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/import-collectionobject-varexpansion.xml
new file mode 100644 (file)
index 0000000..93db3c4
--- /dev/null
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<imports>
+    <import service="CollectionObjects" type="CollectionObject" CSID="${recordCSID}">
+        <schema xmlns:collectionobjects_common="http://collectionspace.org/services/collectionobjects" 
+                name="collectionobjects_common">
+            <objectNumber>${objectNumberValue}</objectNumber>
+            <briefDescriptions>
+                <briefDescription>${briefDescriptionValue}</briefDescription>
+            </briefDescriptions>
+        </schema>
+    </import>
+</imports>
diff --git a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/import-locationauthority-varexpansion.xml b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/import-locationauthority-varexpansion.xml
new file mode 100644 (file)
index 0000000..bedeb44
--- /dev/null
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<imports>
+    <import service="Locationauthorities" type="Locationauthority" CSID="${recordCSID}">
+        <schema xmlns:locationauthorities_common="http://collectionspace.org/services/location" 
+                name="locationauthorities_common">
+            <displayName>${displayNameValue}</displayName>
+            <shortIdentifier>${shortIdentifierValue}</shortIdentifier>
+            <refName>${refNameValue}</refName>
+        </schema>
+    </import>
+</imports>
index ba529c290cd48e4fb3cefe88c704773b0d5defeb..716a6961d73a87dbdff7e5293e92afcd3c0b6ee5 100644 (file)
@@ -5,8 +5,8 @@
         <auth ID="admin@core.collectionspace.org">YWRtaW5AY29yZS5jb2xsZWN0aW9uc3BhY2Uub3JnOkFkbWluaXN0cmF0b3I=</auth>
     </auths>
     
-   <!-- ================================================================================ -->
-    
+    <!-- ================================================================================ -->
+        
     <testGroup ID="importsTestGroup" autoDeletePOSTS="false">
 
         <!-- Import a single ObjectExit record -->
@@ -81,9 +81,9 @@
             * Insert the value of that CSID into a field via expansion
               of the ${docID} variable.
             * Verify that variable has been expanded in the imported record.
-            * Verify that a server-side variable has also been expanded in the
+            * Verify that server-side variables have also been expanded in the
               wrapper template for imported records; specifically, that an
-              expected value is is present in the collectionspace_core part.
+              expected values are present in the collectionspace_core part.
             (See "Variables supported in expansion of request" in
             http://wiki.collectionspace.org/display/collectionspace/Imports+Service+Home)
         -->
                         <label>objectexit_common</label>
                     </part>
                 </parts>
+                <vars>
+                    <var ID="uriValue">/objectexit/${importObjectExitWithVarExpansion.recordCSID}</var>
+                    <var ID="refNameValue">urn:cspace:core.collectionspace.org:objectexit:id(${importObjectExitWithVarExpansion.recordCSID})</var>
+                </vars>
             </response>
         </test>
         <test ID="deleteObjectExitWithVarExpansion">
             <uri>/cspace-services/objectexit/${importObjectExitWithVarExpansion.recordCSID}</uri>
         </test>
         
+                
+        <!-- Verify that the refName generated for an object or procedural record -->
+        <!-- in a service supporting hierarchy includes a display name, derived -->
+        <!-- from the value of a specific, designated field in that record. -->
+        <!-- Verify that this value is present, as well, in the collectionspace_core part. --> 
+        <test ID="importCollectionObjectWithVarExpansion">
+            <expectedCodes>200</expectedCodes>
+            <method>POST</method>
+            <uri>/cspace-services/imports</uri>
+            <filename>imports/import-collectionobject-varexpansion.xml</filename>
+            <vars>
+                <var ID="recordCSID">715dc8f1-9846-4da7-90ab-b4224936e9e2</var>
+                <var ID="objectNumberValue">COLLECTIONOBJECT-IMPORT-TEST-1999.7</var>
+                <var ID="briefDescriptionValue">Photograph of a Kordofan Giraffe near an Acacia shrub.</var>
+            </vars>
+            <response>
+                <expected level="TEXT" />
+                <filename>imports/res/import-collectionobject.res.xml</filename>
+            </response>
+        </test>
+        <test ID="verifyCollectionObjectWithVarExpansion">
+            <expectedCodes>200</expectedCodes>
+            <method>GET</method>
+            <uri>/cspace-services/collectionobjects/${importCollectionObjectWithVarExpansion.recordCSID}</uri>
+            <response>
+                <filename>imports/res/import-collectionobject-varexpansion.res.xml</filename>
+                <expected level="ADDOK" />
+                <parts>
+                    <part>
+                        <label>collectionspace_core</label>
+                    </part>
+                    <part>
+                        <label>collectionobjects_common</label>
+                    </part>
+                </parts>
+                <vars>
+                    <var ID="uriValue">/collectionobjects/${importCollectionObjectWithVarExpansion.recordCSID}</var>
+                    <!-- Note: This test may fail if the RefnameDisplayNameField for -->
+                    <!-- CollectionObject records, in the core tenant, changes to a -->
+                    <!-- field other than objectNumber. -->
+                    <var ID="refNameValue">urn:cspace:core.collectionspace.org:collectionobjects:id(${importCollectionObjectWithVarExpansion.recordCSID})'${importCollectionObjectWithVarExpansion.objectNumberValue}'</var>
+                </vars>
+            </response>
+        </test>
+        <test ID="deleteCollectionObjectWithVarExpansion">
+            <expectedCodes>200</expectedCodes>
+            <method>DELETE</method>
+            <uri>/cspace-services/collectionobjects/${importCollectionObjectWithVarExpansion.recordCSID}</uri>
+        </test>
+        
+                
+        <!-- Verify that a refName value provided in the body of an authority record -->
+        <!-- and is present, as well, in the collectionspace_core part's refName field. -->
+        <test ID="importLocationAuthorityWithVarExpansion">
+            <expectedCodes>200</expectedCodes>
+            <method>POST</method>
+            <uri>/cspace-services/imports</uri>
+            <filename>imports/import-locationauthority-varexpansion.xml</filename>
+            <vars>
+                <var ID="recordCSID">88b7753c-f63b-4254-80a2-4dfcb3342c63</var>
+                <var ID="displayNameValue">Outbuilding Locations</var>
+                <var ID="shortIdentifierValue">outbuildinglocations</var>
+                <var ID="refNameValue">urn:cspace:core.collectionspace.org:locationauthorities:name(outbuildinglocations)'Outbuilding Locations'</var>
+            </vars>
+            <response>
+                <expected level="TEXT" />
+                <filename>imports/res/import-locationauthority.res.xml</filename>
+            </response>
+        </test>
+        <test ID="verifyLocationAuthorityWithVarExpansion">
+            <expectedCodes>200</expectedCodes>
+            <method>GET</method>
+            <uri>/cspace-services/locationauthorities/${importLocationAuthorityWithVarExpansion.recordCSID}</uri>
+            <response>
+                <filename>imports/res/import-locationauthority-varexpansion.res.xml</filename>
+                <expected level="ADDOK" />
+                <parts>
+                    <part>
+                        <label>collectionspace_core</label>
+                    </part>
+                    <part>
+                        <label>locationauthorities_common</label>
+                    </part>
+                </parts>
+                <vars>
+                    <var ID="displayNameValue">${importLocationAuthorityWithVarExpansion.displayNameValue}</var>
+                    <var ID="shortIdentifierValue">${importLocationAuthorityWithVarExpansion.shortIdentifierValue}</var>
+                    <var ID="uriValue">/locationauthorities/${importLocationAuthorityWithVarExpansion.recordCSID}</var>
+                    <var ID="refNameValue">${importLocationAuthorityWithVarExpansion.refNameValue}</var>
+                </vars>
+            </response>
+        </test>
+        <test ID="deleteLocationAuthorityWithVarExpansion">
+            <expectedCodes>200</expectedCodes>
+            <method>DELETE</method>
+            <uri>/cspace-services/locationauthorities/${importLocationAuthorityWithVarExpansion.recordCSID}</uri>
+        </test>
+        
+        <!-- Verify that a refName value is correctly generated for an authority -->
+        <!-- record, when that value is not provided in the imported record, and -->
+        <!-- that generated value is present, as well, in the collectionspace_core -->
+        <!-- part's refName field. -->
+        <test ID="importLocationAuthorityWithGeneratedRefName">
+            <expectedCodes>200</expectedCodes>
+            <method>POST</method>
+            <uri>/cspace-services/imports</uri>
+            <filename>imports/import-locationauthority-varexpansion.xml</filename>
+            <vars>
+                <var ID="recordCSID">c0589d54-64cb-4c9a-9fa2-6ef1e7ab75db</var>
+                <var ID="displayNameValue">Hill Locations</var>
+                <var ID="shortIdentifierValue">hilllocations</var>
+                <!-- An empty value to clear apparently-cached value from previous test -->
+                <var ID="refNameValue"></var>
+            </vars>
+            <response>
+                <expected level="TEXT" />
+                <filename>imports/res/import-locationauthority.res.xml</filename>
+            </response>
+        </test>
+        <test ID="verifyLocationAuthorityWithGeneratedRefName">
+            <expectedCodes>200</expectedCodes>
+            <method>GET</method>
+            <uri>/cspace-services/locationauthorities/${importLocationAuthorityWithGeneratedRefName.recordCSID}</uri>
+            <response>
+                <filename>imports/res/import-locationauthority-varexpansion.res.xml</filename>
+                <expected level="ADDOK" />
+                <parts>
+                    <part>
+                        <label>collectionspace_core</label>
+                    </part>
+                    <part>
+                        <label>locationauthorities_common</label>
+                    </part>
+                </parts>
+                <vars>
+                    <var ID="displayNameValue">${importLocationAuthorityWithGeneratedRefName.displayNameValue}</var>
+                    <var ID="shortIdentifierValue">${importLocationAuthorityWithGeneratedRefName.shortIdentifierValue}</var>
+                    <var ID="uriValue">/locationauthorities/${importLocationAuthorityWithGeneratedRefName.recordCSID}</var>
+                    <var ID="refNameValue">urn:cspace:core.collectionspace.org:locationauthorities:name(hilllocations)'Hill Locations'</var>
+                </vars>
+            </response>
+        </test>
+        <test ID="deleteLocationAuthorityWithGeneratedRefName">
+            <expectedCodes>200</expectedCodes>
+            <method>DELETE</method>
+            <uri>/cspace-services/locationauthorities/${importLocationAuthorityWithGeneratedRefName.recordCSID}</uri>
+        </test>
+                
         <!--
             Import a record containing dollar sign and backslash character(s).
             (See CSPACE-3917.) 
diff --git a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/res/import-collectionobject-varexpansion.res.xml b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/res/import-collectionobject-varexpansion.res.xml
new file mode 100644 (file)
index 0000000..1a427f2
--- /dev/null
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<document name="collectionobjects">
+    <ns2:collectionobjects_common
+        xmlns:ns2="http://collectionspace.org/services/collectionobject">
+        <objectNumber>${importCollectionObjectWithVarExpansion.objectNumberValue}</objectNumber>
+        <briefDescriptions>
+            <briefDescription>${importCollectionObjectWithVarExpansion.briefDescriptionValue}</briefDescription>
+        </briefDescriptions>
+    </ns2:collectionobjects_common>
+    <ns2:collectionspace_core xmlns:ns2="http://collectionspace.org/collectionspace_core/"
+                              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+        <!-- Note: This test may fail if the identifier for the core tenant changes. -->
+        <tenantId>1</tenantId>
+        <uri>${uriValue}</uri>
+        <!-- Note: This test may fail if the RefnameDisplayNameField for -->
+        <!-- CollectionObject records, in the core tenant, changes to a -->
+        <!-- field other than objectNumber. -->
+        <refName>${refNameValue}</refName>
+    </ns2:collectionspace_core>
+</document>
+
diff --git a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/res/import-collectionobject.res.xml b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/res/import-collectionobject.res.xml
new file mode 100644 (file)
index 0000000..73808bb
--- /dev/null
@@ -0,0 +1,9 @@
+<?xml version="1.0"?>
+<import>
+    <totalRecordsImported>1</totalRecordsImported>
+    <numRecordsImported>
+        <docType>CollectionObject</docType>
+        <numRecords>1</numRecords>
+    </numRecordsImported>
+</import>
+
diff --git a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/res/import-locationauthority-varexpansion.res.xml b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/res/import-locationauthority-varexpansion.res.xml
new file mode 100644 (file)
index 0000000..7d3407e
--- /dev/null
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<document name="locationauthorities">
+    <ns2:locationauthorities_common
+        xmlns:ns2="http://collectionspace.org/services/location">
+        <displayName>${displayNameValue}</displayName>
+        <shortIdentifier>${shortIdentifierValue}</shortIdentifier>
+    </ns2:locationauthorities_common>
+    <ns2:collectionspace_core xmlns:ns2="http://collectionspace.org/collectionspace_core/"
+                              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+        <!-- Note: This test may fail if the identifier for the core tenant changes. -->
+        <tenantId>1</tenantId>
+        <uri>${uriValue}</uri>
+        <refName>${refNameValue}</refName>
+    </ns2:collectionspace_core>
+</document>
+
diff --git a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/res/import-locationauthority.res.xml b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/imports/res/import-locationauthority.res.xml
new file mode 100644 (file)
index 0000000..fa8c261
--- /dev/null
@@ -0,0 +1,9 @@
+<?xml version="1.0"?>
+<import>
+    <totalRecordsImported>1</totalRecordsImported>
+    <numRecordsImported>
+        <docType>LocationAuthority</docType>
+        <numRecords>1</numRecords>
+    </numRecordsImported>
+</import>
+
index 30dbe662e828145a0eb2ba9c2eb975be7f15aea0..aea8fa96095bc82d06bc4b619b6783dbb8a6c719 100644 (file)
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <document name="objectexit">
-    <ns2:objectexit_common xmlns:ns2="http://collectionspace.org/services/objectexit" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+    <ns2:objectexit_common xmlns:ns2="http://collectionspace.org/services/objectexit"
+                           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <exitNote>A dollar sign followed by '29.00': $29.00  And a backslash: \</exitNote>        
         <exitNumber>OE-IMPORT-TEST-1999.10</exitNumber>
     </ns2:objectexit_common>
index d007a8b73496129fcc04d5b923ab1bfe366574a5..1d4966913b428388be391e9fea31214208c1412e 100644 (file)
@@ -8,6 +8,7 @@
                               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <!-- Note: This test may fail if the identifier for the core tenant changes. -->
         <tenantId>1</tenantId>
+        <uri>${uriValue}</uri>
+        <refName>${refNameValue}</refName>
     </ns2:collectionspace_core>
-</document>
-
+</document>
\ No newline at end of file
index 36a8dcd591539b4668187b9600082072eef10aaf..d172814d23ee639aa0115d5e3dc1dc7cfc169912 100644 (file)
@@ -40,7 +40,14 @@ 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.GregorianCalendarDateTimeUtils;
+import org.collectionspace.services.common.api.RefName;
+import org.collectionspace.services.common.api.RefNameUtils;
 import org.collectionspace.services.common.api.Tools;
+import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
+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.config.tenant.TenantBindingType;
 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
 import org.dom4j.Attribute;
 import org.dom4j.Document;
@@ -69,12 +76,20 @@ public class TemplateExpander {
     // non-namespace-qualified elements.
     private static final String IN_AUTHORITY_NAMESPACE_XPATH = "//*[local-name()='inAuthority']";
     private static final String IN_AUTHORITY_NO_NAMESPACE_XPATH = "//inAuthority";
+    private static final String REFNAME_NAMESPACE_XPATH = "//*[local-name()='refName']";
+    private static final String REFNAME_NO_NAMESPACE_XPATH = "//refName";
+    private static final String SHORT_IDENTIFIER_NAMESPACE_XPATH = "//*[local-name()='shortIdentifier']";
+    private static final String SHORT_IDENTIFIER_NO_NAMESPACE_XPATH = "//shortIdentifier";
+    private static final String DISPLAYNAME_NAMESPACE_XPATH = "//*[local-name()='displayName']";
+    private static final String DISPLAYNAME_NO_NAMESPACE_XPATH = "//displayName";
     private static final String SERVICE_ATTRIBUTE = "service";
     private static final String TYPE_ATTRIBUTE = "type";
     private static final String CREATED_AT_ATTRIBUTE = "createdAt";
     private static final String CREATED_BY_ATTRIBUTE = "createdBy";
     private static final String UPDATED_AT_ATTRIBUTE = "updatedAt";
     private static final String UPDATED_BY_ATTRIBUTE = "updatedBy";
+    private static TenantBindingConfigReaderImpl tReader =
+            ServiceMain.getInstance().getTenantBindingConfigReader();
 
     protected static String var(String theVar) {
         return "\\$\\{" + theVar + "\\}";
@@ -160,6 +175,9 @@ public class TemplateExpander {
                 getAttributeValue(perRecordAttributes, UPDATED_BY_ATTRIBUTE));
         wrapperTmpl = Tools.searchAndReplace(wrapperTmpl, var("uri"),
                 getDocUri(tenantId, SERVICE_TYPE, docID, partTmpl));
+        wrapperTmpl = Tools.searchAndReplace(wrapperTmpl, var("refName"),
+                getRefName(tenantId, SERVICE_TYPE, docID, partTmpl));
+
 
         String serviceDir = outDir + '/' + docID;
         FileTools.saveFile(serviceDir, "document.xml", wrapperTmpl, FileTools.FORCE_CREATE_PARENT_DIRS);
@@ -257,16 +275,40 @@ public class TemplateExpander {
     // their uniqueness against those already present in a running system.
     // - ADR 2012-05-24
     private static String getInAuthorityValue(String xmlFragment) {
-        String inAuthorityValue = "";
-        // Check in two ways for the inAuthority value: one intended for records with
-        // namespace-qualified elements, the second for unqualified elements.
+        return extractNamespacedOrNonNamespacedValue(xmlFragment,
+                IN_AUTHORITY_NAMESPACE_XPATH, IN_AUTHORITY_NO_NAMESPACE_XPATH);
+    }
+
+    private static String getRefNameValue(String xmlFragment) {
+        return extractNamespacedOrNonNamespacedValue(xmlFragment,
+                REFNAME_NAMESPACE_XPATH, REFNAME_NO_NAMESPACE_XPATH);
+    }
+
+    private static String getShortIdentifierValue(String xmlFragment) {
+        return extractNamespacedOrNonNamespacedValue(xmlFragment,
+                SHORT_IDENTIFIER_NAMESPACE_XPATH, SHORT_IDENTIFIER_NO_NAMESPACE_XPATH);
+    }
+
+    private static String getDisplayNameValue(String xmlFragment) {
+        return extractNamespacedOrNonNamespacedValue(xmlFragment,
+                DISPLAYNAME_NAMESPACE_XPATH, DISPLAYNAME_NO_NAMESPACE_XPATH);
+    }
+
+    private static String extractNamespacedOrNonNamespacedValue(String xmlFragment,
+            String namespacedXpath, String nonNamespacedXpath) {
+        String value = "";
+        if (Tools.isBlank(namespacedXpath) || Tools.isBlank(nonNamespacedXpath)) {
+            return value;
+        }
+        // Extract the value using two XPath expressions: one intended for records
+        // with namespace-qualified elements, the second for unqualified elements.
         // (There may be a more elegant way to do this with a single XPath expression,
         // via an OR operator or the like.)
-        inAuthorityValue = extractValueFromXmlFragment(IN_AUTHORITY_NAMESPACE_XPATH, xmlFragment);
-        if (Tools.isBlank(inAuthorityValue)) {
-            inAuthorityValue = extractValueFromXmlFragment(IN_AUTHORITY_NO_NAMESPACE_XPATH, xmlFragment);
+        value = extractValueFromXmlFragment(namespacedXpath, xmlFragment);
+        if (Tools.isBlank(value)) {
+            value = extractValueFromXmlFragment(nonNamespacedXpath, xmlFragment);
         }
-        return inAuthorityValue;
+        return value;
     }
 
     // FIXME: Need to handle cases here where the xmlFragment may contain more
@@ -276,8 +318,11 @@ public class TemplateExpander {
     private static String extractValueFromXmlFragment(String xpathExpr, String xmlFragment) {
         String value = "";
         try {
-            // FIXME: Cruelly ugly hack; at this point for imported records
-            // with more than one <schema> child, we have a non-well-formed fragment.
+            // FIXME: This '<root>' wrapper is a crudely ugly hack. This hack is
+            // used because, at this point, when handling imported records that
+            // contain more than one <schema> child, we have only a non-well-formed
+            // fragment to work with, and thus need we to wrap that fragment in a
+            // container element.
             String xmlFragmentWrapped = "<root>" + xmlFragment + "</root>";
             InputSource input = new InputSource(new StringReader(xmlFragmentWrapped));
             value = xpath.evaluate(xpathExpr, input);
@@ -296,6 +341,115 @@ public class TemplateExpander {
         return attributeVal;
     }
 
+    private static String getRefName(String tenantId, String serviceType, String docID, String partTmpl) {
+        String refName = "";
+        String refNameDisplayName = "";
+        String shortIdentifier = "";
+
+        // If a valid refName value is provided within the imported record,
+        // use that value
+        refName = getRefNameValue(partTmpl);
+        if (Tools.notBlank(refName)) {
+            // Validate the provided refName by attempting to parse it   
+            if (RefNameUtils.parseAuthorityInfo(refName) != null
+                    || RefNameUtils.parseAuthorityTermInfo(refName) != null) {
+                return refName;
+            } else {
+                // If the provided refName wasn't valid, reset the refName value
+                refName = "";
+            }
+        }
+
+        shortIdentifier = getShortIdentifierValue(partTmpl);
+        if (Tools.notBlank(shortIdentifier)) {
+            // By inference, if a record contains values for both shortIdentifier
+            // and inAuthority, it is an authority item (term) record
+            if (Tools.notBlank(getInAuthorityValue(partTmpl))) {
+                // At present, we're not attempting to automatically generate
+                // refNames for authority item records. Their 'parent' authority
+                // records don't necessarily exist at the time the item records are
+                // being imported, and thus we might not have a good way of identifying
+                // parents and obtaining their data for building part of this refName.
+                //
+                // Until and unless this is changed, imported authority item records
+                // should include refName values.
+                //
+                // FIXME: We could conceivably do a CSID-based lookup for an
+                // authority item's parent here, using the inAuthority value,
+                // and might then obtain the values necessary for building the
+                // refName, if that parent does exist.
+                return refName;
+                // Otherwise, by inference, if a record contains a value for
+                // only a shortIdentifier, it is an authority record
+            } else {
+                refNameDisplayName = getDisplayNameValue(partTmpl);
+                RefName.RefNameInterface authorityRefName =
+                        RefName.Authority.buildAuthority(getTenantName(tenantId),
+                        getServiceName(tenantId, serviceType), null, shortIdentifier, refNameDisplayName);
+                return authorityRefName.toString();
+            }
+        }
+
+        // If this service supports hierarchy, get the refName display name from
+        // the appropriate field in the record, so it can be added to the refName
+        if (serviceSupportsHierarchy(tenantId, serviceType)) {
+            String displayNameFieldXpath = getRefNameDisplayNameXpath(tenantId, serviceType);
+            refNameDisplayName = extractValueFromXmlFragment(displayNameFieldXpath, partTmpl);
+        }
+        // Generate an 'ID' form of refName
+        RefName.RefNameInterface refname =
+                RefName.Authority.buildAuthority(getTenantName(tenantId),
+                getServiceName(tenantId, serviceType), docID, null, refNameDisplayName);
+        refName = refname.toString();
+
+        return refName;
+    }
+
+    private static String getTenantName(String tenantId) {
+        TenantBindingType tenantBinding = tReader.getTenantBinding(tenantId);
+        return tenantBinding.getName();
+    }
+
+    private static String getServiceName(String tenantId, String serviceType) {
+        ServiceBindingType serviceBinding = tReader.getServiceBindingForDocType(tenantId, serviceType);
+        return serviceBinding.getName();
+    }
+
+    private static boolean serviceSupportsHierarchy(String tenantId, String serviceType) {
+        ServiceBindingType serviceBinding = tReader.getServiceBindingForDocType(tenantId, serviceType);
+        DocHandlerParams params = serviceBinding.getDocHandlerParams();
+        if (params == null || params.getParams() == null || params.getParams().isSupportsHierarchy() == null) {
+            return false;
+        } else {
+            return params.getParams().isSupportsHierarchy();
+        }
+    }
+
+    private static String getRefNameDisplayNameXpath(String tenantId, String serviceType) {
+        String displayNameXPath = "";
+        ServiceBindingType serviceBinding = tReader.getServiceBindingForDocType(tenantId, serviceType);
+        DocHandlerParams params = serviceBinding.getDocHandlerParams();
+        if (params == null || params.getParams() == null) {
+            return displayNameXPath;
+        }
+        ListResultField displayNameField = params.getParams().getRefnameDisplayNameField();
+        if (displayNameField == null) {
+            return displayNameXPath;
+
+        }
+        // Based on the two sample Xpath values for the RefnameDisplayNameField
+        // value, it appears these values require prefacing with the 'double slash'
+        // abbreviation, signifying the axis that selects all matching descendants
+        // of the document root. If that assumption proves incorrect in the future,
+        // we will need to revise this line. - ADR 2013-03-07
+        if (Tools.isBlank(displayNameField.getXpath())) {
+            return displayNameXPath;
+        } else {
+            displayNameXPath = "//" + displayNameField.getXpath();
+        }
+        return displayNameXPath;
+    }
+
     /**
      * This inner class is the callback target for calls to XmlSaxFragmenter,
      * for example: FragmentHandlerImpl callback = new FragmentHandlerImpl();
index 1941651a8bfc1d0b78e3e20fb5715a5dc71f29fb..5c64196817513be0823bc1df73eeeb41f1f367bb 100644 (file)
@@ -42,6 +42,7 @@
     <collectionspace_core:updatedBy>${updatedBy}</collectionspace_core:updatedBy>\r
     <collectionspace_core:tenantId>${tenantID}</collectionspace_core:tenantId>\r
     <collectionspace_core:uri>${uri}</collectionspace_core:uri>\r
+    <collectionspace_core:refName>${refName}</collectionspace_core:refName>\r
   </schema>\r
   ${Schema}\r
 </document>\r
index ec9832010c1e72b8cae93b28583ea86da58499ae..de421fd6c7a4fb58de704f4ae8eac049acdec1e5 100644 (file)
@@ -59,8 +59,20 @@ public class ImportsServiceTest {
 
         String xmlPayload = FileTools.readFile(REQUESTS_DIR,"authority-request.xml");
         InputSource inputSource = ImportsResource.payloadToInputSource(xmlPayload);
-        ImportsResource.expandXmlPayloadToDir(BOGUS_TENANT_ID, inputSource, TEMPLATE_DIR, outputDir);
+        
+        // Following changes made to TemplateExpander in CSPACE-5845 to read from
+        // Services tenant bindings via an instance of a tenant binding reader
+        // obtained from ServiceMain, the following call will yield dependency-
+        // related error.  Some significant re-working, to pass in a tenant reader
+        // obtained by directly reading a tenant bindings file, in the manner of
+        // org.collectionspace.services.authorization.importer.AuthorizationGen.initialize(),
+        // may be needed to re-enable this call.
+        
+        // ImportsResource.expandXmlPayloadToDir(BOGUS_TENANT_ID, inputSource, TEMPLATE_DIR, outputDir);
 
         //TODO: inspect dir, then *cleanup*!!
+        
+        // This test class is incomplete; it does not (yet) have any asserts,
+        // as per an examination by Richard and Aron on 2013-03-08.
     }
 }