From f340095ca913e6d7ec9e9a54d862a23e640cb56a Mon Sep 17 00:00:00 2001 From: Ray Lee Date: Thu, 23 Jul 2020 02:06:01 -0400 Subject: [PATCH] DRYD-889: Add name attribute to invocation context field elements. CsvExportWriter will use as column name if specified. --- .../services/export/CsvExportWriter.java | 42 +++++++++++-------- .../services/export/ExportResource.java | 19 +++++---- .../src/main/resources/invocationContext.xsd | 12 +++++- 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/services/export/service/src/main/java/org/collectionspace/services/export/CsvExportWriter.java b/services/export/service/src/main/java/org/collectionspace/services/export/CsvExportWriter.java index fb404d543..52993c903 100644 --- a/services/export/service/src/main/java/org/collectionspace/services/export/CsvExportWriter.java +++ b/services/export/service/src/main/java/org/collectionspace/services/export/CsvExportWriter.java @@ -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 fields = (includeFields != null ? includeFields.getField() : new ArrayList()); + List fields = (includeFields != null ? includeFields.getField() : new ArrayList()); 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 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 fields = includeFields.getField(); + List fields = includeFields.getField(); List 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 matches = element.createXPath(fieldName).selectNodes(element); + List matches = element.selectNodes(fieldName); if (matches.size() > 0) { List values = new ArrayList<>(); diff --git a/services/export/service/src/main/java/org/collectionspace/services/export/ExportResource.java b/services/export/service/src/main/java/org/collectionspace/services/export/ExportResource.java index b1e76cb6d..a4c9cdeb8 100644 --- a/services/export/service/src/main/java/org/collectionspace/services/export/ExportResource.java +++ b/services/export/service/src/main/java/org/collectionspace/services/export/ExportResource.java @@ -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 fields = excludeFields.getField(); + List 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 matches = (List) partElement.createXPath(xpath).selectNodes(partElement); + List matches = (List) partElement.selectNodes(xpath); for (Node excludeNode : matches) { if (excludeNode.getNodeType() == Node.ELEMENT_NODE) { @@ -211,10 +213,11 @@ public class ExportResource extends AbstractCollectionSpaceResourceImpl fields = includeFields.getField(); + List 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 matches = (List) partElement.createXPath(xpath).selectNodes(partElement); + List matches = (List) partElement.selectNodes(xpath); for (Node includeNode : matches) { if (includeNode.getNodeType() == Node.ELEMENT_NODE) { diff --git a/services/jaxb/src/main/resources/invocationContext.xsd b/services/jaxb/src/main/resources/invocationContext.xsd index 5c970359d..39624e993 100644 --- a/services/jaxb/src/main/resources/invocationContext.xsd +++ b/services/jaxb/src/main/resources/invocationContext.xsd @@ -49,14 +49,14 @@ - + - + @@ -77,4 +77,12 @@ + + + + + + + + -- 2.47.3