From: Ray Lee Date: Fri, 17 Feb 2023 18:46:38 +0000 (-0500) Subject: DRYD-1200, DRYD-1160: Add ability to automatically de-urn report fields. (#312) X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=856519c3daa2670d212bcadff94ed9b55895b0fd;p=tmp%2Fjakarta-migration.git DRYD-1200, DRYD-1160: Add ability to automatically de-urn report fields. (#312) * Add scriptlet to automatically de-urn specified report fields. * Update Group_List_Basic report to use deurnfields parameter. This removes JavaScript de-urning expressions from fields that had them, and adds those field names to the deurnfields parameter, so that they will be conditonally de-urned on the server. * Add additional fields to be de-urned. --- diff --git a/services/report/3rdparty/jasper-cs-report/src/main/resources/Group_List_Basic.jrxml b/services/report/3rdparty/jasper-cs-report/src/main/resources/Group_List_Basic.jrxml index 2f5d53db3..530263a61 100644 --- a/services/report/3rdparty/jasper-cs-report/src/main/resources/Group_List_Basic.jrxml +++ b/services/report/3rdparty/jasper-cs-report/src/main/resources/Group_List_Basic.jrxml @@ -50,6 +50,9 @@ : "WHERE hierarchy.name = '" + ($P{csid} ? $P{csid}.replace(/'/g, "''") : '') + "'" )]]> + + + - (it && it.startsWith("urn:")) - ? it.substring(it.indexOf("'") + 1, it.length - 1) - : it -).call(null, $F{objectproductionorganization})]]> + @@ -194,11 +193,7 @@ - (it && it.startsWith("urn:")) - ? it.substring(it.indexOf("'") + 1, it.length - 1) - : it -).call(null, $F{objectproductionperson})]]> + @@ -206,11 +201,7 @@ - (it && it.startsWith("urn:")) - ? it.substring(it.indexOf("'") + 1, it.length - 1) - : it -).call(null, $F{objectproductionpeople})]]> + @@ -228,11 +219,7 @@ - (it && it.startsWith("urn:")) - ? it.substring(it.indexOf("'") + 1, it.length - 1) - : it -).call(null, $F{computedcurrentlocation})]]> + diff --git a/services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/DefaultReportScriptlet.java b/services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/DefaultReportScriptlet.java new file mode 100644 index 000000000..76c618817 --- /dev/null +++ b/services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/DefaultReportScriptlet.java @@ -0,0 +1,111 @@ +package org.collectionspace.services.report.nuxeo; + +import java.util.HashSet; +import java.util.Set; + +import net.sf.jasperreports.engine.JRDefaultScriptlet; +import net.sf.jasperreports.engine.JRScriptletException; +import net.sf.jasperreports.engine.JasperReport; +import net.sf.jasperreports.engine.fill.JRFillField; + +import org.collectionspace.services.common.api.RefNameUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A JasperReports scriptlet to apply by default to all CollectionSpace reports. This handles the + * formatting of refname values as display names (aka de-urning) in specified fields. The names of + * fields to de-urn are supplied in the deurnfields parameter, as a comma-delimited list. If "*" is + * specified, all string-typed fields are de-urned. + */ +public class DefaultReportScriptlet extends JRDefaultScriptlet { + private final Logger logger = LoggerFactory.getLogger(DefaultReportScriptlet.class); + + private static String DEURN_FIELDS_PARAM = "deurnfields"; + + protected boolean isDeurnAll = false; + protected Set deurnFieldNames = null; + + @Override + public void afterReportInit() throws JRScriptletException { + String deurnFieldsSpec = (String) this.getParameterValue(DEURN_FIELDS_PARAM, false); + + if (deurnFieldsSpec != null) { + this.deurnFieldNames = new HashSet(); + + for (String fieldSpec : deurnFieldsSpec.split(",")) { + String trimmedFieldSpec = fieldSpec.trim(); + + if (trimmedFieldSpec.equals("*")) { + this.isDeurnAll = true; + } else { + this.deurnFieldNames.add(trimmedFieldSpec); + } + } + } + } + + @Override + public void beforeDetailEval() throws JRScriptletException { + if (this.isDeurnAll) { + deurnAllFields(); + } else { + deurnSpecifiedFields(); + } + } + + private void deurnAllFields() { + if (this.fieldsMap != null) { + for (JRFillField field : this.fieldsMap.values()) { + if (field.getValueClass().equals(String.class)) { + deurnField(field); + } + } + } + } + + private void deurnSpecifiedFields() { + if (this.fieldsMap != null && this.deurnFieldNames != null) { + for (String fieldName : this.deurnFieldNames) { + JRFillField field = this.fieldsMap.get(fieldName); + + if (field == null) { + logger.warn("{}: deurn field not found: {}", getReportName(), fieldName); + + continue; + } + + if (!field.getValueClass().equals(String.class)) { + logger.warn("{}: deurn field is not a string: {}", getReportName(), fieldName); + + continue; + } + + deurnField(field); + } + } + } + + private void deurnField(JRFillField field) { + String value = (String) field.getValue(); + + if (value != null) { + try { + field.setValue(RefNameUtils.getDisplayName(value)); + } catch (IllegalArgumentException ex) { + // It wasn't a valid refname. Keep the value. + } + } + } + + private String getReportName() { + JasperReport report = null; + + try { + report = (JasperReport) this.getParameterValue("JASPER_REPORT", false); + } + catch (JRScriptletException ex) {} + + return (report != null ? report.getName() : "Unknown report name"); + } +} diff --git a/services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/ReportDocumentModelHandler.java b/services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/ReportDocumentModelHandler.java index b3244829a..3d88c372a 100644 --- a/services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/ReportDocumentModelHandler.java +++ b/services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/ReportDocumentModelHandler.java @@ -47,6 +47,7 @@ import net.sf.jasperreports.engine.JRParameter; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; +import net.sf.jasperreports.engine.design.JasperDesign; import net.sf.jasperreports.engine.export.JRCsvExporter; import net.sf.jasperreports.engine.export.JRCsvExporterParameter; import net.sf.jasperreports.engine.export.JRHtmlExporter; @@ -55,6 +56,7 @@ import net.sf.jasperreports.engine.export.JRXmlExporter; import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter; import net.sf.jasperreports.engine.export.ooxml.JRPptxExporter; import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter; +import net.sf.jasperreports.engine.xml.JRXmlLoader; import org.collectionspace.authentication.AuthN; import org.collectionspace.services.ReportJAXBSchema; @@ -372,9 +374,15 @@ public class ReportDocumentModelHandler extends NuxeoDocumentModelHandler