]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
DRYD-889: Add name attribute to invocation context field elements. CsvExportWriter...
authorRay Lee <ray.lee@lyrasis.org>
Thu, 23 Jul 2020 06:06:01 +0000 (02:06 -0400)
committerRay Lee <ray.lee@lyrasis.org>
Thu, 23 Jul 2020 06:06:59 +0000 (02:06 -0400)
services/export/service/src/main/java/org/collectionspace/services/export/CsvExportWriter.java
services/export/service/src/main/java/org/collectionspace/services/export/ExportResource.java
services/jaxb/src/main/resources/invocationContext.xsd

index fb404d543911eb77954e150a790a6ebbe5793bd8..52993c9037c46a727d1e672e509ca14a6c3542ab 100644 (file)
@@ -19,6 +19,7 @@ import org.collectionspace.services.client.PayloadOutputPart;
 import org.collectionspace.services.client.PoxPayloadOut;
 import org.collectionspace.services.common.api.RefNameUtils;
 import org.collectionspace.services.common.context.ServiceBindingUtils;
+import org.collectionspace.services.common.invocable.Field;
 import org.collectionspace.services.common.invocable.InvocationContext;
 import org.collectionspace.services.config.service.ServiceBindingType;
 import org.collectionspace.services.config.service.ServiceObjectType;
@@ -42,7 +43,7 @@ public class CsvExportWriter extends AbstractExportWriter {
                // present.
 
                InvocationContext.IncludeFields includeFields = invocationContext.getIncludeFields();
-               List<String> fields = (includeFields != null ? includeFields.getField() : new ArrayList<String>());
+               List<Field> fields = (includeFields != null ? includeFields.getField() : new ArrayList<Field>());
 
                if (fields.size() == 0) {
                        throw new Exception("For CSV output, the fields to export must be specified using includeFields.");
@@ -50,18 +51,23 @@ public class CsvExportWriter extends AbstractExportWriter {
 
                List<String> headers = new ArrayList<>();
 
-               for (String field : fields) {
-                       Matcher matcher = VALID_FIELD_XPATH_PATTERN.matcher(field);
+               for (Field field : fields) {
+                       String fieldSpec = field.getValue();
+                       Matcher matcher = VALID_FIELD_XPATH_PATTERN.matcher(fieldSpec);
 
                        if (!matcher.matches()) {
-                               throw new Exception("The includeField XPath expression \"" + field + "\" is not valid for CSV output. For CSV output, all included fields must be individually specified without using wildcards.");
+                               throw new Exception("The includeField expression \"" + fieldSpec + "\" is not valid for CSV output. For CSV output, all included fields must be individually specified without using wildcards.");
                        }
 
-                       String fieldName = matcher.group(2);
+                       String fieldName = field.getName();
+
+                       if (fieldName == null) {
+                               fieldName = matcher.group(2);
+                       }
 
                        headers.add(fieldName);
 
-                       if (isFieldWithinAuthItemTermGroup(field)) {
+                       if (isFieldWithinAuthItemTermGroup(fieldSpec)) {
                                headers.add(fieldName + "NonPreferred");
                        }
                }
@@ -79,19 +85,21 @@ public class CsvExportWriter extends AbstractExportWriter {
                        return;
                }
 
-               List<String> fields = includeFields.getField();
+               List<Field> fields = includeFields.getField();
                List<String> csvRecord = new ArrayList<>();
 
-               for (String field : fields) {
-                       if (isFieldWithinAuthItemTermGroup(field)) {
+               for (Field field : fields) {
+                       String fieldSpec = field.getValue();
+
+                       if (isFieldWithinAuthItemTermGroup(fieldSpec)) {
                                // Write a column for values within the preferred (primary) term group.
-                               csvRecord.add(collectValues(document, field.replace(AUTH_ITEM_TERM_GROUP_SUFFIX + "/", AUTH_ITEM_TERM_GROUP_SUFFIX + "[position()=1]/")));
+                               csvRecord.add(collectValues(document, fieldSpec.replace(AUTH_ITEM_TERM_GROUP_SUFFIX + "/", AUTH_ITEM_TERM_GROUP_SUFFIX + "[position()=1]/")));
 
                                // Write a column for values within non-preferred term groups.
-                               csvRecord.add(collectValues(document, field.replace(AUTH_ITEM_TERM_GROUP_SUFFIX + "/", AUTH_ITEM_TERM_GROUP_SUFFIX + "[position()>1]/")));
+                               csvRecord.add(collectValues(document, fieldSpec.replace(AUTH_ITEM_TERM_GROUP_SUFFIX + "/", AUTH_ITEM_TERM_GROUP_SUFFIX + "[position()>1]/")));
                        }
                        else {
-                               csvRecord.add(collectValues(document, field));
+                               csvRecord.add(collectValues(document, fieldSpec));
                        }
                }
 
@@ -105,15 +113,15 @@ public class CsvExportWriter extends AbstractExportWriter {
          csvPrinter.close();
        }
 
-       private boolean isFieldWithinAuthItemTermGroup(String field) {
+       private boolean isFieldWithinAuthItemTermGroup(String fieldSpec) {
                // FIXME: How to know a NonPreferred column is needed without hardcoding "TermGroup"?
 
-               return field.contains(AUTH_ITEM_TERM_GROUP_SUFFIX + "/");
+               return fieldSpec.contains(AUTH_ITEM_TERM_GROUP_SUFFIX + "/");
        }
 
-       private String collectValues(PoxPayloadOut document, String field) {
+       private String collectValues(PoxPayloadOut document, String fieldSpec) {
                String delimitedValues = "";
-               String[] segments = field.split(":", 2);
+               String[] segments = fieldSpec.split(":", 2);
                String partName = segments[0];
                String xpath = segments[1];
 
@@ -130,7 +138,7 @@ public class CsvExportWriter extends AbstractExportWriter {
                String delimitedValues = "";
                String fieldName = path.get(depth);
                String delimiter = (depth / 2 > 0) ? "^^" : ";";
-               List<Node> matches = element.createXPath(fieldName).selectNodes(element);
+               List<Node> matches = element.selectNodes(fieldName);
 
                if (matches.size() > 0) {
                        List<String> values = new ArrayList<>();
index b1e76cb6dab49176d71566f0508161700b0355c4..a4c9cdeb8b3767404760e4292e2950700314e5fd 100644 (file)
@@ -45,6 +45,7 @@ import org.collectionspace.services.common.ServiceMessages;
 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
 import org.collectionspace.services.common.context.ServiceContext;
 import org.collectionspace.services.common.context.ServiceContextFactory;
+import org.collectionspace.services.common.invocable.Field;
 import org.collectionspace.services.common.invocable.Invocable;
 import org.collectionspace.services.common.invocable.InvocationContext;
 import org.dom4j.Node;
@@ -185,10 +186,11 @@ public class ExportResource extends AbstractCollectionSpaceResourceImpl<PoxPaylo
                InvocationContext.ExcludeFields excludeFields = invocationContext.getExcludeFields();
 
                if (excludeFields != null) {
-                       List<String> fields = excludeFields.getField();
+                       List<Field> fields = excludeFields.getField();
 
-                       for (String field : fields) {
-                               String[] segments = field.split(":", 2);
+                       for (Field field : fields) {
+                               String fieldSpec = field.getValue();
+                               String[] segments = fieldSpec.split(":", 2);
 
                                String partName = segments[0];
                                String xpath = segments[1];
@@ -197,7 +199,7 @@ public class ExportResource extends AbstractCollectionSpaceResourceImpl<PoxPaylo
 
                                if (part != null) {
                                        org.dom4j.Element partElement = part.getElementBody();
-                                       List<Node> matches = (List<Node>) partElement.createXPath(xpath).selectNodes(partElement);
+                                       List<Node> matches = (List<Node>) partElement.selectNodes(xpath);
 
                                        for (Node excludeNode : matches) {
                                                if (excludeNode.getNodeType() == Node.ELEMENT_NODE) {
@@ -211,10 +213,11 @@ public class ExportResource extends AbstractCollectionSpaceResourceImpl<PoxPaylo
                InvocationContext.IncludeFields includeFields = invocationContext.getIncludeFields();
 
                if (includeFields != null) {
-                       List<String> fields = includeFields.getField();
+                       List<Field> fields = includeFields.getField();
 
-                       for (String field : fields) {
-                               String[] segments = field.split(":", 2);
+                       for (Field field : fields) {
+                               String fieldSpec = field.getValue();
+                               String[] segments = fieldSpec.split(":", 2);
 
                                String partName = segments[0];
                                String xpath = segments[1];
@@ -223,7 +226,7 @@ public class ExportResource extends AbstractCollectionSpaceResourceImpl<PoxPaylo
 
                                if (part != null) {
                                        org.dom4j.Element partElement = part.getElementBody();
-                                       List<Node> matches = (List<Node>) partElement.createXPath(xpath).selectNodes(partElement);
+                                       List<Node> matches = (List<Node>) partElement.selectNodes(xpath);
 
                                        for (Node includeNode : matches) {
                                                if (includeNode.getNodeType() == Node.ELEMENT_NODE) {
index 5c970359d46fcdf99debdffde2c81250505084b9..39624e99332fab7699129ed429af057670d1a622 100644 (file)
                 <xs:element name="includeFields">
                     <xs:complexType>
                         <xs:sequence>
-                            <xs:element name="field" type="xs:string" minOccurs="0" maxOccurs="unbounded"></xs:element>
+                            <xs:element name="field" type="field" minOccurs="0" maxOccurs="unbounded"></xs:element>
                         </xs:sequence>
                     </xs:complexType>
                 </xs:element>
                 <xs:element name="excludeFields">
                     <xs:complexType>
                         <xs:sequence>
-                            <xs:element name="field" type="xs:string" minOccurs="0" maxOccurs="unbounded"></xs:element>
+                            <xs:element name="field" type="field" minOccurs="0" maxOccurs="unbounded"></xs:element>
                         </xs:sequence>
                     </xs:complexType>
                 </xs:element>
             </xs:sequence>
         </xs:complexType>
     </xs:element>
+
+    <xs:complexType name="field">
+        <xs:simpleContent>
+            <xs:extension base="xs:string">
+                <xs:attribute name="name" type="xs:string" use="optional" />
+            </xs:extension>
+        </xs:simpleContent>
+    </xs:complexType>
 </xs:schema>