]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
DRYD-1200, DRYD-1160: Add ability to automatically de-urn report fields. (#312)
authorRay Lee <ray.lee@lyrasis.org>
Fri, 17 Feb 2023 18:46:38 +0000 (13:46 -0500)
committerGitHub <noreply@github.com>
Fri, 17 Feb 2023 18:46:38 +0000 (13:46 -0500)
* 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.

services/report/3rdparty/jasper-cs-report/src/main/resources/Group_List_Basic.jrxml
services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/DefaultReportScriptlet.java [new file with mode: 0644]
services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/ReportDocumentModelHandler.java

index 2f5d53db324d49f68c5a89fcdb64182cf85c406c..530263a61b8fc281f3ee75dbb23f59cee9cb0de8 100644 (file)
@@ -50,6 +50,9 @@
                : "WHERE hierarchy.name = '" + ($P{csid} ? $P{csid}.replace(/'/g, "''") : '') + "'"
        )]]></defaultValueExpression>
        </parameter>
+       <parameter name="deurnfields" class="java.lang.String" isForPrompting="false">
+               <defaultValueExpression><![CDATA["objectname,objectproductionorganization,objectproductionorganizationrole,objectproductionperson,objectproductionpersonrole,objectproductionpeople,computedcurrentlocation"]]></defaultValueExpression>
+       </parameter>
        <queryString>
                <![CDATA[select
                                                coc.objectnumber,
                        </textField>
                        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                                <reportElement style="Detail" stretchType="RelativeToTallestObject" x="365" y="0" width="91" height="51" uuid="cd2614be-d303-42ec-b1ae-17986df423dd"/>
-                               <textFieldExpression><![CDATA[(
-  (it) => (it && it.startsWith("urn:"))
-    ? it.substring(it.indexOf("'") + 1, it.length - 1)
-    : it
-).call(null, $F{objectproductionorganization})]]></textFieldExpression>
+                               <textFieldExpression><![CDATA[$F{objectproductionorganization}]]></textFieldExpression>
                        </textField>
                        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                                <reportElement style="Detail" stretchType="RelativeToTallestObject" x="456" y="0" width="91" height="51" uuid="f9b9b475-9f9e-48a7-8016-330cbcfb0fae"/>
                        </textField>
                        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                                <reportElement style="Detail" stretchType="RelativeToTallestObject" x="547" y="0" width="91" height="51" uuid="2c3a2c4b-9124-43c8-8be3-69c482f4e5ad"/>
-                               <textFieldExpression><![CDATA[(
-  (it) => (it && it.startsWith("urn:"))
-    ? it.substring(it.indexOf("'") + 1, it.length - 1)
-    : it
-).call(null, $F{objectproductionperson})]]></textFieldExpression>
+                               <textFieldExpression><![CDATA[$F{objectproductionperson}]]></textFieldExpression>
                        </textField>
                        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                                <reportElement style="Detail" stretchType="RelativeToTallestObject" x="638" y="0" width="91" height="51" uuid="baa2cb50-03d1-45e0-9e32-dac575891da9"/>
                        </textField>
                        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                                <reportElement style="Detail" stretchType="RelativeToTallestObject" x="729" y="0" width="91" height="51" uuid="00f2e1c6-6a9d-445a-9bb0-8792754d5f66"/>
-                               <textFieldExpression><![CDATA[(
-  (it) => (it && it.startsWith("urn:"))
-    ? it.substring(it.indexOf("'") + 1, it.length - 1)
-    : it
-).call(null, $F{objectproductionpeople})]]></textFieldExpression>
+                               <textFieldExpression><![CDATA[$F{objectproductionpeople}]]></textFieldExpression>
                        </textField>
                        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                                <reportElement style="Detail" stretchType="RelativeToTallestObject" x="820" y="0" width="91" height="51" uuid="f28c35cd-bc86-49a7-95ff-cebf40b6434f"/>
                                <reportElement style="Detail" stretchType="RelativeToTallestObject" x="1093" y="0" width="91" height="51" uuid="569e8c92-bc33-49c8-bad7-a80b144d5406">
                                        <property name="com.jaspersoft.studio.unit.x" value="pixel"/>
                                </reportElement>
-                               <textFieldExpression><![CDATA[(
-  (it) => (it && it.startsWith("urn:"))
-    ? it.substring(it.indexOf("'") + 1, it.length - 1)
-    : it
-).call(null, $F{computedcurrentlocation})]]></textFieldExpression>
+                               <textFieldExpression><![CDATA[$F{computedcurrentlocation}]]></textFieldExpression>
                        </textField>
                </band>
        </detail>
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 (file)
index 0000000..76c6188
--- /dev/null
@@ -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<String> deurnFieldNames = null;
+
+       @Override
+       public void afterReportInit() throws JRScriptletException {
+               String deurnFieldsSpec = (String) this.getParameterValue(DEURN_FIELDS_PARAM, false);
+
+               if (deurnFieldsSpec != null) {
+                       this.deurnFieldNames = new HashSet<String>();
+
+                       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");
+       }
+}
index b3244829a98534515fa87e15d2489bd22493d219..3d88c372ab3c2878e43704555974912835a4ad37 100644 (file)
@@ -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<Report
                                                                        reportCSID, sourceFilePath);
                                        throw new RuntimeException("Report is missing the specified source file!");
                                }
-               logger.info("Report for csid={} is not compiled. Compiling first, and saving to: {}",
-                               reportCSID, compiledFilePath);
-               JasperCompileManager.compileReportToFile(sourceFilePath, compiledFilePath);
+
+                               logger.info("Report for csid={} is not compiled. Compiling first, and saving to: {}",
+                                       reportCSID, compiledFilePath);
+
+                               JasperDesign design = JRXmlLoader.load(sourceFilePath);
+
+                               design.setScriptletClass("org.collectionspace.services.report.nuxeo.DefaultReportScriptlet");
+
+                               JasperCompileManager.compileReportToFile(design, compiledFilePath);
                        }
 
                        conn = getConnection();