]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
76c618817f68e197bd4deb4fb67d3b6551a5a881
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.report.nuxeo;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
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;
10
11 import org.collectionspace.services.common.api.RefNameUtils;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 /**
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.
20  */
21 public class DefaultReportScriptlet extends JRDefaultScriptlet {
22         private final Logger logger = LoggerFactory.getLogger(DefaultReportScriptlet.class);
23
24         private static String DEURN_FIELDS_PARAM = "deurnfields";
25
26         protected boolean isDeurnAll = false;
27         protected Set<String> deurnFieldNames = null;
28
29         @Override
30         public void afterReportInit() throws JRScriptletException {
31                 String deurnFieldsSpec = (String) this.getParameterValue(DEURN_FIELDS_PARAM, false);
32
33                 if (deurnFieldsSpec != null) {
34                         this.deurnFieldNames = new HashSet<String>();
35
36                         for (String fieldSpec : deurnFieldsSpec.split(",")) {
37                                 String trimmedFieldSpec = fieldSpec.trim();
38
39                                 if (trimmedFieldSpec.equals("*")) {
40                                         this.isDeurnAll = true;
41                                 } else {
42                                         this.deurnFieldNames.add(trimmedFieldSpec);
43                                 }
44                         }
45                 }
46         }
47
48         @Override
49         public void beforeDetailEval() throws JRScriptletException {
50                 if (this.isDeurnAll) {
51                         deurnAllFields();
52                 } else {
53                         deurnSpecifiedFields();
54                 }
55         }
56
57         private void deurnAllFields() {
58                 if (this.fieldsMap != null) {
59                         for (JRFillField field : this.fieldsMap.values()) {
60                                 if (field.getValueClass().equals(String.class)) {
61                                         deurnField(field);
62                                 }
63                         }
64                 }
65         }
66
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);
71
72                                 if (field == null) {
73                                         logger.warn("{}: deurn field not found: {}", getReportName(), fieldName);
74
75                                         continue;
76                                 }
77
78                                 if (!field.getValueClass().equals(String.class)) {
79                                         logger.warn("{}: deurn field is not a string: {}", getReportName(), fieldName);
80
81                                         continue;
82                                 }
83
84                                 deurnField(field);
85                         }
86                 }
87         }
88
89         private void deurnField(JRFillField field) {
90                 String value = (String) field.getValue();
91
92                 if (value != null) {
93                         try {
94                                 field.setValue(RefNameUtils.getDisplayName(value));
95                         } catch (IllegalArgumentException ex) {
96                                 // It wasn't a valid refname. Keep the value.
97                         }
98                 }
99         }
100
101         private String getReportName() {
102                 JasperReport report = null;
103
104                 try {
105                         report = (JasperReport) this.getParameterValue("JASPER_REPORT", false);
106                 }
107                 catch (JRScriptletException ex) {}
108
109                 return (report != null ? report.getName() : "Unknown report name");
110         }
111 }