001: package org.romaframework.aspect.reporting.jr;
002:
003: import java.util.Collection;
004: import java.util.HashMap;
005: import java.util.Map;
006:
007: import net.sf.jasperreports.engine.JRException;
008: import net.sf.jasperreports.engine.JasperCompileManager;
009: import net.sf.jasperreports.engine.JasperReport;
010: import net.sf.jasperreports.engine.design.JasperDesign;
011:
012: import org.romaframework.aspect.reporting.ReportingException;
013: import org.romaframework.aspect.reporting.jr.element.DesignElement;
014: import org.romaframework.aspect.reporting.jr.template.TemplateManager;
015: import org.romaframework.core.flow.ObjectContext;
016: import org.romaframework.core.schema.SchemaClassDefinition;
017: import org.romaframework.core.schema.SchemaField;
018:
019: public class DesignJr {
020: protected static TemplateManager templateManager = ObjectContext
021: .getInstance().getComponent(TemplateManager.class);
022: protected JasperDesign design;
023: private Map<SchemaField, DesignElement> subreports = new HashMap<SchemaField, DesignElement>();
024: private JasperReport report;
025: protected SchemaClassDefinition rootDesignClass;
026: protected String componentId;
027: protected boolean customTemplate = false;
028:
029: public DesignJr() {
030: }
031:
032: public DesignJr(SchemaClassDefinition iRootDesignClass,
033: String iComponentId) {
034: rootDesignClass = iRootDesignClass;
035: componentId = iComponentId;
036: design = getBaseDesign();
037: }
038:
039: public JasperDesign getDesign() {
040: return design;
041: }
042:
043: /**
044: * Return a defined subreport for the field
045: *
046: * @param iField
047: * @return
048: */
049: public DesignElement getSubreport(SchemaField iField) {
050: return subreports.get(iField);
051: }
052:
053: /**
054: * Add a subreport to the design
055: *
056: * @param iField
057: * @param subreport
058: */
059: public void addSubReport(SchemaField iField, DesignElement subreport) {
060: subreports.put(iField, subreport);
061: }
062:
063: /**
064: * Return the list of subreport in the design
065: *
066: * @return
067: */
068: public Collection<DesignElement> getSubReports() {
069: return subreports.values();
070: }
071:
072: /**
073: * Get the compiled JReport
074: *
075: * @return
076: * @throws JRException
077: */
078: public JasperReport getCompiledReport() throws JRException {
079: if (report == null) {
080: report = JasperCompileManager
081: .compileReport(getBaseDesign());
082: }
083: return report;
084: }
085:
086: /**
087: * compiled JReport
088: *
089: * @return
090: * @throws JRException
091: */
092: public void compile(JasperDesign iDesign) throws JRException {
093: design = iDesign;
094: report = JasperCompileManager.compileReport(iDesign);
095: }
096:
097: /**
098: * Get the base JR design to use for the rendering.
099: *
100: * If no template is defined in the package of the application return the
101: * template schema in {application.package}.reporting if
102: *
103: * @return
104: */
105: protected JasperDesign getBaseDesign() {
106: final JasperDesign custom = templateManager.getCustomSRFile(
107: rootDesignClass, componentId);
108: if (custom != null) {
109: customTemplate = true;
110: return custom;
111: }
112: if (design == null) {
113: return templateManager.getBaseDesign(componentId,
114: rootDesignClass);
115: } else {
116: return design;
117: }
118: }
119:
120: /**
121: * Return true if a template is defined in the pakage of the class
122: *
123: * @return
124: */
125: public boolean isCustomTemplate() {
126: return customTemplate;
127: }
128:
129: public void saveTemplate() throws ReportingException {
130: try {
131: templateManager.saveTemplate(design, rootDesignClass);
132: } catch (final Exception e) {
133: throw new ReportingException(e);
134: }
135: for (final DesignElement subReport : getSubReports()) {
136: subReport.saveTemplate();
137: }
138: }
139:
140: }
|