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: * Created Nov 17, 2005
014: * @author wseyler
015: */
016:
017: package org.pentaho.ui.component.charting;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.apache.commons.logging.LogFactory;
025: import org.dom4j.Document;
026: import org.dom4j.Node;
027: import org.jfree.data.general.Dataset;
028: import org.pentaho.core.session.IPentahoSession;
029: import org.pentaho.core.solution.ActionResource;
030: import org.pentaho.core.solution.IActionResource;
031: import org.pentaho.core.system.PentahoSystem;
032: import org.pentaho.core.ui.IPentahoUrlFactory;
033: import org.pentaho.core.util.XmlHelper;
034: import org.pentaho.messages.Messages;
035:
036: public abstract class AbstractJFreeChartComponent extends
037: AbstractChartComponent {
038:
039: protected int chartType;
040:
041: protected Dataset dataDefinition;
042:
043: public AbstractJFreeChartComponent(int chartType,
044: String definitionPath, int width, int height,
045: IPentahoUrlFactory urlFactory, List messages) {
046: this (urlFactory, messages);
047: this .chartType = chartType;
048: this .definitionPath = definitionPath;
049: this .width = width;
050: this .height = height;
051: PentahoSystem.ActionInfo info = PentahoSystem
052: .parseActionString(definitionPath);
053: if (info != null) {
054: setSourcePath(info.getSolutionName() + File.separator
055: + info.getPath());
056: }
057: }
058:
059: /**
060: * @param definitionPath
061: * @param urlFactory
062: * @param messages
063: */
064: public AbstractJFreeChartComponent(String definitionPath,
065: IPentahoUrlFactory urlFactory, ArrayList messages) {
066: this (urlFactory, messages);
067: this .definitionPath = definitionPath;
068: PentahoSystem.ActionInfo info = PentahoSystem
069: .parseActionString(definitionPath);
070: if (info != null) {
071: setSourcePath(info.getSolutionName() + File.separator
072: + info.getPath());
073: }
074: }
075:
076: public AbstractJFreeChartComponent(IPentahoUrlFactory urlFactory,
077: List messages) {
078: super (urlFactory, messages);
079: logger = LogFactory.getLog(this .getClass());
080: }
081:
082: /**
083: * Creates a Dataset object (actaully one of it's subclasses from the XML
084: * doc
085: *
086: * @param doc
087: * XML document that describes the chart
088: * @return the Dataset Implementation
089: */
090: public abstract Dataset createChart(Document doc);
091:
092: /**
093: * @return Returns the dataSet.
094: */
095: public Dataset getDataDefinitiont() {
096: return dataDefinition;
097: }
098:
099: /**
100: * @param dataSet
101: * The dataSet to set.
102: */
103: public void setDataDefinition(Dataset dataSet) {
104: this .dataDefinition = dataSet;
105: }
106:
107: /**
108: * @return Returns the chartType.
109: */
110: public int getChartType() {
111: return chartType;
112: }
113:
114: /**
115: * @param chartType
116: * The chartType to set.
117: */
118: public void setChartType(int chartType) {
119: this .chartType = chartType;
120: }
121:
122: public boolean setDataAction(String chartDefinition) {
123: ActionResource resource = new ActionResource(
124: "", IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$ //$NON-NLS-2$
125: chartDefinition);
126: try {
127: Document dataActionDocument = getResourceAsDocument(
128: getSession(), resource);
129: if (dataActionDocument == null) {
130: return false;
131: }
132:
133: Node dataNode = dataActionDocument
134: .selectSingleNode("chart/data"); //$NON-NLS-1$
135:
136: if (dataNode == null) {
137: // No data here
138: return false;
139: }
140: chartType = (int) XmlHelper.getNodeText(
141: "chart-type", dataNode, -1); //$NON-NLS-1$
142: solution = XmlHelper.getNodeText("data-solution", dataNode); //$NON-NLS-1$
143: actionPath = XmlHelper.getNodeText("data-path", dataNode); //$NON-NLS-1$
144: actionName = XmlHelper.getNodeText("data-action", dataNode); //$NON-NLS-1$
145: actionOutput = XmlHelper.getNodeText(
146: "data-output", dataNode); //$NON-NLS-1$
147: byRow = XmlHelper
148: .getNodeText("data-orientation", dataNode, "rows").equals("rows"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
149: if (width == 0) {
150: width = (int) XmlHelper.getNodeText(
151: "chart/width", dataActionDocument, 125); //$NON-NLS-1$
152: }
153: if (height == 0) {
154: height = (int) XmlHelper.getNodeText(
155: "chart/height", dataActionDocument, 125); //$NON-NLS-1$
156: }
157: } catch (Exception e) {
158: error(
159: Messages
160: .getString(
161: "CategoryDatasetChartComponent.ERROR_0001_INVALID_CHART_DEFINITION", chartDefinition), e); //$NON-NLS-1$
162: return false;
163: }
164: return true;
165: }
166:
167: // HACK for BISERVER-171:
168: // This is a temporary fix for SolutionRepository.getResourceAsDocument() returning the wrong solution file -
169: // chart definition files should not get a "true" parameter for check for localized file. We need to move
170: // this code into the Solution Repository after 1.6 RC1.
171:
172: public static Document getResourceAsDocument(
173: IPentahoSession userSession, IActionResource actionResource)
174: throws IOException {
175: // TODO support locales here
176: String xml = new String(PentahoSystem.getSolutionRepository(
177: userSession).getResourceAsBytes(actionResource, false));
178: if (xml == null) {
179: return null;
180: }
181: Document document = XmlHelper.getDocFromString(xml);
182: return document;
183: }
184: }
|