1 package org.collectionspace.services.report.nuxeo;
3 import java.util.HashSet;
6 import net.sf.jasperreports.engine.JRDefaultScriptlet;
7 import net.sf.jasperreports.engine.JRScriptletException;
8 import net.sf.jasperreports.engine.JasperReport;
9 import net.sf.jasperreports.engine.fill.JRFillField;
11 import org.collectionspace.services.common.api.RefNameUtils;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
16 * A JasperReports scriptlet to apply by default to all CollectionSpace reports. This handles the
17 * formatting of refname values as display names (aka de-urning) in specified fields. The names of
18 * fields to de-urn are supplied in the deurnfields parameter, as a comma-delimited list. If "*" is
19 * specified, all string-typed fields are de-urned.
21 public class DefaultReportScriptlet extends JRDefaultScriptlet {
22 private final Logger logger = LoggerFactory.getLogger(DefaultReportScriptlet.class);
24 private static String DEURN_FIELDS_PARAM = "deurnfields";
26 protected boolean isDeurnAll = false;
27 protected Set<String> deurnFieldNames = null;
30 public void afterReportInit() throws JRScriptletException {
31 String deurnFieldsSpec = (String) this.getParameterValue(DEURN_FIELDS_PARAM, false);
33 if (deurnFieldsSpec != null) {
34 this.deurnFieldNames = new HashSet<String>();
36 for (String fieldSpec : deurnFieldsSpec.split(",")) {
37 String trimmedFieldSpec = fieldSpec.trim();
39 if (trimmedFieldSpec.equals("*")) {
40 this.isDeurnAll = true;
42 this.deurnFieldNames.add(trimmedFieldSpec);
49 public void beforeDetailEval() throws JRScriptletException {
50 if (this.isDeurnAll) {
53 deurnSpecifiedFields();
57 private void deurnAllFields() {
58 if (this.fieldsMap != null) {
59 for (JRFillField field : this.fieldsMap.values()) {
60 if (field.getValueClass().equals(String.class)) {
67 private void deurnSpecifiedFields() {
68 if (this.fieldsMap != null && this.deurnFieldNames != null) {
69 for (String fieldName : this.deurnFieldNames) {
70 JRFillField field = this.fieldsMap.get(fieldName);
73 logger.warn("{}: deurn field not found: {}", getReportName(), fieldName);
78 if (!field.getValueClass().equals(String.class)) {
79 logger.warn("{}: deurn field is not a string: {}", getReportName(), fieldName);
89 private void deurnField(JRFillField field) {
90 String value = (String) field.getValue();
94 field.setValue(RefNameUtils.getDisplayName(value));
95 } catch (IllegalArgumentException ex) {
96 // It wasn't a valid refname. Keep the value.
101 private String getReportName() {
102 JasperReport report = null;
105 report = (JasperReport) this.getParameterValue("JASPER_REPORT", false);
107 catch (JRScriptletException ex) {}
109 return (report != null ? report.getName() : "Unknown report name");