001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013:
014: package org.pentaho.plugin.jfreereport.components;
015:
016: import java.util.Iterator;
017: import java.util.List;
018: import java.util.Map;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.dom4j.Node;
023: import org.jfree.base.config.ModifiableConfiguration;
024: import org.jfree.report.JFreeReport;
025: import org.pentaho.commons.connection.IPentahoMetaData;
026: import org.pentaho.commons.connection.IPentahoResultSet;
027: import org.pentaho.core.util.XmlHelper;
028: import org.pentaho.messages.Messages;
029: import org.pentaho.plugin.jfreereport.AbstractJFreeReportComponent;
030:
031: public class JFreeReportConfigParameterComponent extends
032: AbstractJFreeReportComponent {
033:
034: private static final long serialVersionUID = 6599442849458920502L;
035:
036: private static final String REPORT_CONFIG_INPUT_PARAM = "config_parameters"; //$NON-NLS-1$
037:
038: private static final String REPORT_CONFIG_COMPDEFN = "report_configuration_parameters"; //$NON-NLS-1$
039:
040: public JFreeReportConfigParameterComponent() {
041: // TODO Auto-generated constructor stub
042: super ();
043: }
044:
045: public void done() {
046: // Nothing to do here...
047: }
048:
049: private boolean initReportConfigParameters() {
050: boolean result = true;
051: if (isDefinedInput(DATACOMPONENT_REPORTTEMP_OBJINPUT)) {
052: Object maybeReport = getInputValue(DATACOMPONENT_REPORTTEMP_OBJINPUT);
053: if (maybeReport instanceof JFreeReport) {
054: JFreeReport report = (JFreeReport) maybeReport;
055: // We should have our report object at this point.
056: if (isDefinedInput(REPORT_CONFIG_INPUT_PARAM)) {
057: // It's coming in as an input parameter
058: Object reportConfigParams = this
059: .getInputValue(REPORT_CONFIG_INPUT_PARAM);
060: if (reportConfigParams instanceof IPentahoResultSet) {
061: setReportConfigParameters(report,
062: (IPentahoResultSet) reportConfigParams);
063: } else if (reportConfigParams instanceof Map) {
064: setReportConfigParameters(report,
065: (Map) reportConfigParams);
066: } else {
067: error(Messages
068: .getErrorString("JFreeReport.ERROR_0026_UNKNOWN_REPORT_CONFIGURATION_PARAMETERS")); //$NON-NLS-1$
069: result = false;
070: ;
071: }
072:
073: } else {
074: Node compDef = getComponentDefinition();
075: List configNodes = compDef
076: .selectNodes(REPORT_CONFIG_COMPDEFN + "/*"); //$NON-NLS-1$
077: if ((configNodes != null)
078: && (configNodes.size() > 0)) {
079: setReportConfigParameters(report, configNodes);
080: }
081: }
082: }
083: }
084: return result;
085: }
086:
087: protected boolean executeAction() throws Throwable {
088: return initReportConfigParameters();
089: }
090:
091: public boolean init() {
092: return true;
093: }
094:
095: private void setReportConfigParameters(JFreeReport report,
096: List configNodes) {
097: // We have some configuration parameters in the component definition
098: String parmName = null;
099: String parmValue = null;
100: for (int i = 0; i < configNodes.size(); i++) {
101: Node aNode = (Node) configNodes.get(i);
102: parmName = XmlHelper.getNodeText("@name", aNode, null); //$NON-NLS-1$
103: if ((parmName == null) || (parmName.length() == 0)) {
104: // Ignore configuration settings without name=
105: error(Messages
106: .getErrorString("JFreeReport.ERROR_0027_REPORT_CONFIGURATION_PARAMETER_IGNORED")); //$NON-NLS-1$
107: continue;
108: }
109: parmValue = aNode.getText();
110: if (parmValue != null) {
111: parmValue = parmValue.trim();
112: if (parmValue.length() > 0) {
113: report.getReportConfiguration().setConfigProperty(
114: parmName, applyInputsToFormat(parmValue));
115: } else {
116: error(Messages
117: .getErrorString("JFreeReport.ERROR_0027_REPORT_CONFIGURATION_PARAMETER_IGNORED")); //$NON-NLS-1$
118: }
119: } else {
120: error(Messages
121: .getErrorString("JFreeReport.ERROR_0027_REPORT_CONFIGURATION_PARAMETER_IGNORED")); //$NON-NLS-1$
122: }
123: }
124:
125: }
126:
127: private void setReportConfigParameters(JFreeReport report,
128: Map values) {
129: Map.Entry ent;
130: ModifiableConfiguration config = report
131: .getReportConfiguration();
132: Iterator it = values.entrySet().iterator();
133: while (it.hasNext()) {
134: ent = (Map.Entry) it.next();
135: if ((ent.getKey() != null) && (ent.getValue() != null)) {
136: config.setConfigProperty(ent.getKey().toString(),
137: applyInputsToFormat(ent.getValue().toString()));
138: }
139: }
140: }
141:
142: private void setReportConfigParameters(JFreeReport report,
143: IPentahoResultSet values) {
144: int rowCount = values.getRowCount();
145: int colCount = values.getColumnCount();
146: ModifiableConfiguration config = report
147: .getReportConfiguration();
148: if (colCount >= 2) {
149: IPentahoMetaData md = values.getMetaData();
150: int nameIdx = md.getColumnIndex("name");//$NON-NLS-1$
151: int valIdx = md.getColumnIndex("value");//$NON-NLS-1$
152: if (nameIdx < 0) {
153: nameIdx = 0;
154: }
155: if (valIdx < 0) {
156: valIdx = 1;
157: }
158: for (int i = 0; i < rowCount; i++) {
159: Object[] aRow = values.getDataRow(i);
160: if ((aRow[nameIdx] != null) && (aRow[valIdx] != null)) {
161: config
162: .setConfigProperty(
163: aRow[nameIdx].toString(),
164: applyInputsToFormat(aRow[valIdx]
165: .toString()));
166: }
167: }
168: } else {
169: error(Messages
170: .getErrorString("JFreeReport.ERROR_0025_INVALID_REPORT_CONFIGURATION_PARAMETERS")); //$NON-NLS-1$
171: }
172: }
173:
174: protected boolean validateAction() {
175: return true;
176: }
177:
178: protected boolean validateSystemSettings() {
179: return true;
180: }
181:
182: public Log getLogger() {
183: return LogFactory
184: .getLog(JFreeReportConfigParameterComponent.class);
185: }
186:
187: }
|