1 16 17 package org.pentaho.ui.component; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.PrintWriter ; 22 import java.io.StringWriter ; 23 import java.util.List ; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.dom4j.Document; 27 import org.dom4j.DocumentHelper; 28 import org.dom4j.Element; 29 import org.pentaho.messages.Messages; 30 import org.pentaho.plugin.jfreechart.DialWidgetDefinition; 31 import org.pentaho.plugin.jfreechart.WidgetDefinition; 32 import org.pentaho.core.solution.ActionResource; 33 import org.pentaho.core.solution.IActionResource; 34 import org.pentaho.core.system.PentahoSystem; 35 import org.pentaho.core.ui.IPentahoUrlFactory; 36 import org.pentaho.plugin.jfreechart.JFreeChartEngine; 37 import org.pentaho.ui.XmlComponent; 38 39 57 public class DashboardWidgetComponent extends XmlComponent { 58 59 62 private static final long serialVersionUID = 3060729271469984040L; 63 64 public static final int TYPE_DIAL = 1; 65 66 public static final int TYPE_THERMOMETER = 2; 67 68 private static final Log logger = LogFactory.getLog(DashboardWidgetComponent.class); 69 70 private int type; 71 72 private double value = TYPE_DIAL; 73 74 private String definitionPath; 75 76 private String title = ""; 78 private String units = ""; 80 private int width; 81 82 private int height; 83 84 103 public DashboardWidgetComponent(int type, String definitionPath, int width, int height, IPentahoUrlFactory urlFactory, List messages) { 104 super(urlFactory, messages, null); 105 this.type = type; 106 this.definitionPath = definitionPath; 107 this.width = width; 108 this.height = height; 109 String info[] = PentahoSystem.parseActionString(definitionPath); 110 if (info != null && info.length == 3) { 111 setSourcePath(info[0] + File.separator + info[1]); 112 } 113 setXsl("text/html", "DialWidget.xsl"); } 116 117 123 public void setValue(double value) { 124 this.value = value; 125 } 126 127 133 public void setTitle(String title) { 134 this.title = title; 135 } 136 137 143 public void setUnits(String units) { 144 this.units = units; 145 } 146 147 152 public Log getLogger() { 153 return logger; 154 } 155 156 159 public boolean validate() { 160 return true; 162 } 163 164 176 public Document getXmlContent() { 177 178 WidgetDefinition widget = null; 179 if (type == TYPE_DIAL) { 180 181 ActionResource resource = new ActionResource(title, IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", definitionPath); 184 Document dialDefinition = null; 185 try { 186 dialDefinition = PentahoSystem.getSolutionRepository(getSession()).getResourceAsDocument(resource); 187 } catch (IOException e) { 188 error(Messages.getErrorString("Widget.ERROR_0002_INVALID_RESOURCE", definitionPath), e); } 190 191 if (dialDefinition == null) { 192 error(Messages.getErrorString("Widget.ERROR_0002_INVALID_RESOURCE", definitionPath) ); return null; 194 } 195 widget = new DialWidgetDefinition(dialDefinition, 0, width, height, getSession()); 197 198 if (widget != null) { 199 widget.setValue(new Double (value)); 201 202 } else { 203 error(Messages.getString("Widget.ERROR_0001_COULD_NOT_CREATE")); return null; 205 } 206 } 207 226 if (widget == null) { 227 error(Messages.getString("Widget.ERROR_0001_COULD_NOT_CREATE")); return null; 229 } 230 StringWriter stringWriter = new StringWriter (); 232 PrintWriter printWriter = new PrintWriter (stringWriter); 233 String solutionDir = "system/tmp/"; String fileNamePrefix = "tmp_pie_"; String extension = ".png"; String fileName = null; 238 String filePathWithoutExtension = null; 239 try { 240 File file = File.createTempFile(fileNamePrefix, extension, new File (PentahoSystem.getApplicationContext().getFileOutputPath(solutionDir))); 241 file.deleteOnExit(); 242 fileName = file.getName(); 243 filePathWithoutExtension = solutionDir + fileName.substring(0, fileName.indexOf('.')); 244 } catch (IOException e) { 245 e.printStackTrace(); 247 } 248 String dialTitle = ""; JFreeChartEngine.saveChart(widget, dialTitle, units, filePathWithoutExtension, width, height, JFreeChartEngine.OUTPUT_PNG, printWriter, this); 250 251 Document result = DocumentHelper.createDocument(); 253 String baseUrl = PentahoSystem.getApplicationContext().getBaseUrl(); 254 setXslProperty("baseUrl", baseUrl); 256 Element root = result.addElement("widget"); root.addElement("title").setText(title); root.addElement("units").setText(units); root.addElement("width").setText(Integer.toString(width)); root.addElement("height").setText(Integer.toString(height)); Element valueNode = root.addElement("value"); valueNode.setText(Double.toString(value)); 263 valueNode.addAttribute("in-image", Boolean.toString(widget.getValueFont() != null)); root.addElement("image").setText(fileName); return result; 266 267 } 268 269 270 271 public void dispose() { 272 273 } 274 275 } 276 | Popular Tags |