001: /*
002: *
003: * Copyright 2007 Giordano Maestro (giordano.maestro@assetdata.it)
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package org.romaframework.aspect.reporting.jr;
018:
019: import java.io.File;
020: import java.util.Iterator;
021:
022: import net.sf.jasperreports.engine.JRAlignment;
023: import net.sf.jasperreports.engine.JRElement;
024: import net.sf.jasperreports.engine.JRException;
025: import net.sf.jasperreports.engine.JRRewindableDataSource;
026: import net.sf.jasperreports.engine.JasperReport;
027: import net.sf.jasperreports.engine.design.JRDesignBand;
028: import net.sf.jasperreports.engine.design.JRDesignExpression;
029: import net.sf.jasperreports.engine.design.JRDesignStaticText;
030: import net.sf.jasperreports.engine.design.JRDesignSubreport;
031: import net.sf.jasperreports.engine.design.JRDesignTextField;
032: import net.sf.jasperreports.engine.design.JasperDesign;
033:
034: import org.romaframework.aspect.reporting.ReportingAspect;
035: import org.romaframework.aspect.reporting.ReportingConstants;
036: import org.romaframework.aspect.reporting.feature.ReportingBaseFeatures;
037: import org.romaframework.aspect.reporting.feature.ReportingFieldFeatures;
038: import org.romaframework.aspect.reporting.jr.component.BaseCollectionComponentJr;
039: import org.romaframework.aspect.reporting.jr.component.BaseComponentJr;
040: import org.romaframework.aspect.reporting.jr.component.CollectionListComponentJr;
041: import org.romaframework.aspect.reporting.jr.component.CollectionTableComponentJr;
042: import org.romaframework.aspect.reporting.jr.component.ExpandedParameterSource;
043: import org.romaframework.aspect.reporting.jr.component.MapTableComponentJr;
044: import org.romaframework.aspect.reporting.jr.component.ObjectEmbeddedJr;
045: import org.romaframework.aspect.reporting.jr.element.DesignElement;
046: import org.romaframework.aspect.reporting.jr.element.ElementJr;
047: import org.romaframework.aspect.reporting.jr.element.ParameterJr;
048: import org.romaframework.aspect.view.ViewAspect;
049: import org.romaframework.aspect.view.feature.ViewBaseFeatures;
050: import org.romaframework.aspect.view.feature.ViewElementFeatures;
051: import org.romaframework.core.config.RomaApplicationContext;
052: import org.romaframework.core.schema.SchemaClassDefinition;
053: import org.romaframework.core.schema.SchemaField;
054:
055: public class JRDesignHelper {
056:
057: private static final String SUBREPORT_SOURCE_KEY = "subreportSource_";
058:
059: private static final String SUBREPORT_DESIGN_KEY = "subreportDesign_";
060:
061: private static final String FIELD_TYPE = "F";
062:
063: public static final int FONT_SIZE = 10;
064: public static final int FONT_BOX = 14;
065: public static final int TITLE_SIZE = 14;
066: public static final int TITLE_BOX = 18;
067: public static final int MAX_EXPAND_LEVEL = 3;
068:
069: public static final String font = "Arial";
070:
071: /**
072: * Create the parameter text field
073: *
074: * @param field
075: * @return
076: */
077: public static JRDesignTextField getField(ElementJr iParameter) {
078: final JRDesignTextField textField = new JRDesignTextField();
079: textField.setHeight(FONT_BOX);
080: textField.setFontSize(FONT_SIZE);
081: textField
082: .setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_LEFT);
083: // Adding the field expression
084: final JRDesignExpression expression = new JRDesignExpression();
085: setSimpleParameter(iParameter.getType(), expression);
086: expression.setText("$P{" + iParameter.getName() + "}");
087: textField.setExpression(expression);
088: return textField;
089: }
090:
091: public static void setSimpleParameter(Class type,
092: JRDesignExpression expression) {
093: if (ReflectionHelper.isPrimitive(type)) {
094: expression.setValueClass(type);
095: } else {
096: expression.setValueClass(String.class);
097: }
098: }
099:
100: public static JRDesignBand getBand() {
101: final JRDesignBand band = new JRDesignBand();
102: band.setHeight(0);
103: return band;
104: }
105:
106: public static JRDesignTextField getFieldExpression(String name,
107: Class type) {
108: return getElementExpression(name, type, FIELD_TYPE);
109: }
110:
111: public static JRDesignTextField getIndirectFieldExpression(
112: String name, Class type) {
113: return getIndirectElementExpression(name, type, FIELD_TYPE);
114: }
115:
116: private static JRDesignTextField getElementExpression(String name,
117: Class type, String elementType) {
118: final JRDesignTextField textField = new JRDesignTextField();
119: textField.setHeight(FONT_BOX);
120: textField.setFontSize(FONT_SIZE);
121: textField
122: .setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_LEFT);
123: textField
124: .setStretchType(net.sf.jasperreports.engine.JRElement.STRETCH_TYPE_NO_STRETCH);
125: textField.setBlankWhenNull(true);
126: // Adding the field expression
127: final JRDesignExpression expression = new JRDesignExpression();
128: expression.setValueClass(type);
129:
130: expression.setText("$" + elementType + "{" + name + "}");
131: textField.setExpression(expression);
132: return textField;
133: }
134:
135: private static JRDesignTextField getIndirectElementExpression(
136: String name, Class type, String elementType) {
137: final JRDesignTextField textField = new JRDesignTextField();
138: textField.setHeight(FONT_BOX);
139: textField.setFontSize(FONT_SIZE);
140: textField
141: .setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_LEFT);
142: textField
143: .setStretchType(net.sf.jasperreports.engine.JRElement.STRETCH_TYPE_NO_STRETCH);
144: textField.setBlankWhenNull(true);
145: // Adding the field expression
146: final JRDesignExpression expression = new JRDesignExpression();
147: expression.setValueClass(type);
148: expression.setText("$" + elementType + "{source}.get(\"" + name
149: + "\")");
150: textField.setExpression(expression);
151: return textField;
152: }
153:
154: /**
155: * Create a label object with the default size and align values
156: *
157: * @param field, the field to lebel
158: * @return
159: */
160: public static JRDesignStaticText getLabel(String field) {
161: final JRDesignStaticText staticText = new JRDesignStaticText();
162: staticText.setHeight(FONT_BOX);
163: staticText
164: .setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_LEFT);
165: staticText.setText(ReflectionHelper.firstToUppercase(field));
166: staticText.setBold(true);
167: staticText.setPdfEmbedded(true);
168: staticText.setPdfFontName("Helvetica-Bold");
169: staticText.setFontSize(FONT_SIZE);
170: return staticText;
171: }
172:
173: public static JRDesignBand getTitleBand(String reportName, int width) {
174: final JRDesignStaticText title = getLabel(reportName);
175: title.setFontSize(TITLE_SIZE);
176: title.setHeight(TITLE_BOX);
177: title.setWidth(width);
178: title
179: .setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER);
180: title.setPdfEmbedded(true);
181: final JRDesignBand titleBand = getBand();
182:
183: titleBand.setHeight(titleBand.getHeight() + title.getHeight());
184: titleBand.addElement(title);
185: return titleBand;
186: }
187:
188: /**
189: * Get a jasper subreport definition.
190: *
191: * @param iDesign
192: * @param id
193: * @return
194: */
195: public static JRDesignSubreport getSubReport(JasperDesign iDesign,
196: String id) {
197: final JRDesignSubreport sub = new JRDesignSubreport(iDesign);
198: // Setting subExspression
199: final JRDesignExpression subExpr = new JRDesignExpression();
200: subExpr.setValueClass(JasperReport.class);
201: subExpr.setText("$F{source}.get(\""
202: + JRDesignHelper.subReportDesignKey(id) + "\")");
203: sub.setExpression(subExpr);
204: final JRDesignExpression subExpr2 = new JRDesignExpression();
205: subExpr2.setValueClass(JRRewindableDataSource.class);
206: subExpr2.setText("$F{source}.get(\""
207: + JRDesignHelper.subReportSourceKey(id) + "\")");
208: sub.setDataSourceExpression(subExpr2);
209: sub.setPositionType(JRElement.POSITION_TYPE_FLOAT);
210: return sub;
211: }
212:
213: public static String subReportDesignKey(String id) {
214: return SUBREPORT_DESIGN_KEY + id;
215: }
216:
217: public static String subReportSourceKey(String id) {
218: return SUBREPORT_SOURCE_KEY + id;
219: }
220:
221: public static File getImage(String name) {
222: final Class toSave = Object.class;
223: String packageFile = RomaApplicationContext.getInstance()
224: .getApplicationPackage()
225: + "." + ReportingAspect.ASPECT_NAME + ".image";
226: packageFile = packageFile.replaceAll("\\.", "/");
227: final File template = new File(toSave.getResource(
228: "/" + packageFile + "/").getFile()
229: + name);
230: return template;
231: }
232:
233: public static String getLayoutField(SchemaField schemaField) {
234: String fieldLayout = (String) schemaField.getFeature(
235: ReportingAspect.ASPECT_NAME,
236: ReportingBaseFeatures.LAYOUT);
237: if (fieldLayout == null) {
238: fieldLayout = (String) schemaField.getFeature(
239: ViewAspect.ASPECT_NAME, ViewBaseFeatures.LAYOUT);
240: }
241: return fieldLayout;
242: }
243:
244: public static Boolean getIsVisibleField(SchemaField schemaField) {
245: Boolean result = (Boolean) schemaField.getFeature(
246: ReportingAspect.ASPECT_NAME,
247: ReportingFieldFeatures.VISIBLE);
248: if (result != null) {
249: return result;
250: } else {
251: result = (Boolean) schemaField
252: .getFeature(ViewAspect.ASPECT_NAME,
253: ViewElementFeatures.VISIBLE);
254: if (result != null) {
255: return result;
256: }
257: }
258: return true;
259: }
260:
261: public static String getRenderField(SchemaField schemaField) {
262: String fieldRender = (String) schemaField.getFeature(
263: ReportingAspect.ASPECT_NAME,
264: ReportingBaseFeatures.RENDER);
265: if (fieldRender == null) {
266: fieldRender = (String) schemaField.getFeature(
267: ViewAspect.ASPECT_NAME, ViewBaseFeatures.RENDER);
268: }
269: return fieldRender;
270: }
271:
272: public static void renderFields(DesignJr design,
273: SchemaClassDefinition designClassToRender,
274: BaseComponentJr componentThis) throws JRException {
275: final Iterator<SchemaField> iterator = designClassToRender
276: .getFieldIterator();
277: while (iterator.hasNext()) {
278: final SchemaField schemaField = iterator.next();
279: if (!getIsVisibleField(schemaField)) {
280: continue;
281: }
282: final Class fieldClass = schemaField.getTypeClass();
283: if (!ReflectionHelper.isCollectionMapSet(fieldClass)) {
284: final String objectEmbedded = getRenderField(schemaField);
285: if (ReportingConstants.LAYOUT_EXPAND
286: .equals(getLayoutField(schemaField))
287: && componentThis.getLevel() < MAX_EXPAND_LEVEL) {
288: final ExpandedParameterSource comp = new ExpandedParameterSource(
289: componentThis, schemaField,
290: designClassToRender);
291: comp.generateDesign();
292: design.addSubReport(schemaField, comp);
293: } else if (ReportingConstants.RENDER_OBJECTEMBEDDED
294: .equals(objectEmbedded)
295: && componentThis.getLevel() < 2) {
296: final ObjectEmbeddedJr comp = new ObjectEmbeddedJr(
297: componentThis, schemaField
298: .getComplexClass(), schemaField,
299: designClassToRender);
300: comp.generateDesign();
301: design.addSubReport(schemaField, comp);
302: } else {
303: final ParameterJr param = new ParameterJr(
304: componentThis.getId(), schemaField);
305: design.addSubReport(schemaField, param);
306: }
307: } else if (ReflectionHelper.isCollection(fieldClass)) {
308: // generate collection subreport
309: final DesignElement comp = JRDesignHelper
310: .generateCollectionSubReport(schemaField,
311: designClassToRender, componentThis);
312: design.addSubReport(schemaField, comp);
313: // end generate collection subreport
314: } else if (ReflectionHelper.isMap(fieldClass)) {
315: final DesignElement comp = JRDesignHelper
316: .generateMapSubReport(schemaField,
317: designClassToRender, componentThis);
318: design.addSubReport(schemaField, comp);
319: }
320: }
321: }
322:
323: private static DesignElement generateMapSubReport(
324: SchemaField schemaField,
325: SchemaClassDefinition designClassToRender,
326: BaseComponentJr componentThis) throws JRException {
327: BaseCollectionComponentJr comp;
328: comp = new MapTableComponentJr(componentThis, schemaField,
329: designClassToRender);
330: comp.generateDesign();
331: return comp;
332: }
333:
334: public static DesignElement generateCollectionSubReport(
335: SchemaField schemaField,
336: SchemaClassDefinition designClassToRender,
337: BaseComponentJr componentThis) throws JRException {
338: final String fieldRender = getRenderField(schemaField);
339: BaseCollectionComponentJr comp;
340: if (ReportingConstants.RENDER_TABLE.equals(fieldRender)) {
341: comp = new CollectionTableComponentJr(componentThis,
342: schemaField, designClassToRender);
343: comp.generateDesign();
344: } else {
345: comp = new CollectionListComponentJr(componentThis,
346: schemaField, designClassToRender);
347: comp.generateDesign();
348: }
349: return comp;
350: }
351: }
|