1 13 package org.pentaho.plugin.jfreereport.components; 14 15 import java.io.InputStream ; 16 import java.io.StringReader ; 17 import java.net.URL ; 18 import java.util.Iterator ; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.jfree.report.JFreeReport; 23 import org.jfree.report.modules.parser.base.ReportGenerator; 24 import org.pentaho.core.repository.ISolutionRepository; 25 import org.pentaho.core.runtime.IActionParameter; 26 import org.pentaho.core.solution.IActionResource; 27 import org.pentaho.core.system.PentahoSystem; 28 import org.pentaho.messages.Messages; 29 import org.pentaho.plugin.jfreereport.AbstractJFreeReportComponent; 30 import org.pentaho.plugin.jfreereport.helper.ReportUtils; 31 import org.xml.sax.InputSource ; 32 33 39 public class JFreeReportLoadComponent extends AbstractJFreeReportComponent { 40 private static final long serialVersionUID = -2240691437049710246L; 41 42 public JFreeReportLoadComponent() { 43 } 44 45 protected boolean validateAction() { 46 if (isDefinedResource(REPORTGENERATEDEFN_REPORTDEFN)) { 47 return true; 48 } 49 50 if (isDefinedInput(REPORTGENERATEDEFN_REPORTDEFN)) { 51 IActionParameter o = getInputParameter(REPORTGENERATEDEFN_REPORTDEFN); 52 if ((o != null) && (o.getValue() instanceof String )) { 53 return true; 54 } 55 return false; 56 } 57 58 if (isDefinedInput(REPORTLOAD_RESOURCENAME)) { 60 if (isDefinedResource(getInputStringValue(REPORTLOAD_RESOURCENAME))) { 61 return true; 62 } else { 63 error(Messages.getErrorString("JFreeReport.ERROR_0004_REPORT_DEFINITION_UNREADABLE")); return false; 65 } 66 } 67 68 if (isDefinedResource(DATACOMPONENT_JARINPUT)) { 69 if (!isDefinedInput(REPORTLOAD_REPORTLOC)) { 70 error(Messages.getErrorString("JFreeReport.ERROR_0011_REPORT_LOCATION_MISSING")); return false; 72 } 73 74 final IActionResource resource = getResource(DATACOMPONENT_JARINPUT); 75 return validateResource(resource); 76 } 77 return false; 78 } 79 80 private boolean validateResource(IActionResource resource) { 81 final ISolutionRepository solutionRepository = PentahoSystem.getSolutionRepository(getSession()); 82 final InputStream in = solutionRepository.getResourceInputStream(resource); 83 if (in == null) { 84 error(Messages.getErrorString("JFreeReport.ERROR_0010_REPORT_JAR_MISSING", resource.getAddress())); return false; 86 } 87 88 try { 89 if (in.read() == -1) { 91 error(Messages.getErrorString("JFreeReport.ERROR_0009_REPORT_JAR_UNREADABLE")); return false; 93 } 94 } catch (Exception e) { 95 error(Messages.getErrorString("JFreeReport.ERROR_0009_REPORT_JAR_UNREADABLE")); return false; 97 } 98 99 if (!isDefinedInput(REPORTLOAD_REPORTLOC)) { 100 error(Messages.getErrorString("JFreeReport.ERROR_0012_CLASS_LOCATION_MISSING")); return false; 102 } 103 return true; 104 } 105 106 protected boolean validateSystemSettings() { 107 return true; 108 } 109 110 public void done() { 111 112 } 113 114 protected boolean executeAction() throws Throwable { 115 if (isDefinedResource(REPORTGENERATEDEFN_REPORTDEFN)) { 117 final IActionResource resource = getResource(REPORTGENERATEDEFN_REPORTDEFN); 118 JFreeReport report = parseReport(resource); 119 if (report == null) { 120 return false; 121 } 122 addTempParameterObject(DATACOMPONENT_REPORTTEMP_OBJINPUT, report); 123 return true; 124 } 125 126 if (isDefinedInput(REPORTLOAD_RESOURCENAME)) { 128 final String resName = getInputStringValue(REPORTLOAD_RESOURCENAME); 129 if (isDefinedResource(resName)) { 130 final IActionResource resource = getResource(resName); 131 JFreeReport report = parseReport(resource); 132 if (report == null) { 133 return false; 134 } 135 addTempParameterObject(DATACOMPONENT_REPORTTEMP_OBJINPUT, report); 136 return true; 137 } else { 138 error(Messages.getErrorString("JFreeReport.ERROR_0004_REPORT_DEFINITION_UNREADABLE")); return false; 140 } 141 } 142 143 if (isDefinedInput(REPORTGENERATEDEFN_REPORTDEFN)) { 144 IActionParameter o = getInputParameter(REPORTGENERATEDEFN_REPORTDEFN); 145 if (o != null) { 146 String repDef = o.getStringValue(); 147 ReportGenerator generator = ReportGenerator.getInstance(); 148 JFreeReport report = generator.parseReport(new InputSource (new StringReader (repDef)), getDefinedResourceURL(null)); 149 if (report == null) { 150 return false; 151 } 152 addTempParameterObject(DATACOMPONENT_REPORTTEMP_OBJINPUT, report); 153 return true; 154 } 155 } 156 157 final IActionResource resource = getResource(DATACOMPONENT_JARINPUT); 159 final ClassLoader loader = ReportUtils.createJarLoader(getSession(), resource); 160 if (loader == null) { 161 error(Messages.getString("JFreeReportLoadComponent.ERROR_0035_COULD_NOT_CREATE_CLASSLOADER")); return false; 163 } 164 165 String reportLocation = getInputStringValue(REPORTLOAD_REPORTLOC); 166 URL resourceUrl = loader.getResource(reportLocation); 167 if (resourceUrl == null) { 168 error(Messages.getErrorString("JFreeReport.ERROR_0016_REPORT_RESOURCE_INVALID", reportLocation, resource.getAddress())); 170 return false; 171 } 172 173 try { 174 ReportGenerator generator = ReportGenerator.getInstance(); 175 JFreeReport report = generator.parseReport(resourceUrl, getDefinedResourceURL(resourceUrl)); 176 if (report == null) { 177 return false; 178 } 179 addTempParameterObject(DATACOMPONENT_REPORTTEMP_OBJINPUT, report); 180 return true; 181 } catch (Exception ex) { 182 error(Messages.getErrorString("JFreeReport.ERROR_0007_COULD_NOT_PARSE", reportLocation), ex); return false; 184 } 185 } 186 187 private URL getDefinedResourceURL(URL defaultValue) { 188 if (isDefinedInput(REPORTLOAD_RESURL) == false) { 189 return defaultValue; 190 } 191 192 try { 193 final String inputStringValue = getInputStringValue(Messages.getString(REPORTLOAD_RESURL)); 194 return new URL (inputStringValue); 195 } catch (Exception e) { 196 return defaultValue; 197 } 198 } 199 200 private String getBaseServerURL(String pentahoBaseURL) { 201 try { 202 URL url = new URL (pentahoBaseURL); 203 return url.getProtocol() + "://" + url.getHost() + ":" + url.getPort(); 204 } catch (Exception e) { 205 } 206 return pentahoBaseURL; 207 } 208 209 215 private JFreeReport parseReport(IActionResource resource) { 216 try { 217 URL resourceUrl = ReportUtils.getURL(getSession(), resource); 218 219 final ISolutionRepository solutionRepository = PentahoSystem.getSolutionRepository(getSession()); 220 final InputStream in = solutionRepository.getResourceInputStream(resource); 221 222 ReportGenerator generator = ReportGenerator.getInstance(); 223 224 String pentahoBaseURL = PentahoSystem.getApplicationContext().getBaseUrl(); 225 generator.setObject("pentahoBaseURL", PentahoSystem.getApplicationContext().getBaseUrl()); 226 227 generator.setObject("serverBaseURL", getBaseServerURL(pentahoBaseURL)); 229 230 generator.setObject("solutionRoot", PentahoSystem.getApplicationContext().getSolutionPath("")); 231 232 Iterator it = getInputNames().iterator(); 233 while (it.hasNext()) { 234 try { 235 String inputName = (String ) it.next(); 236 String inputValue = getInputStringValue(inputName); 237 generator.setObject(inputName, inputValue); 238 } catch (Exception e) { 239 } 240 } 241 242 return generator.parseReport(new InputSource (in), getDefinedResourceURL(resourceUrl)); 243 } catch (Exception ex) { 244 error(Messages.getErrorString("JFreeReport.ERROR_0007_COULD_NOT_PARSE", resource.getAddress()), ex); return null; 246 } 247 } 248 249 public boolean init() { 250 return true; 251 } 252 253 public Log getLogger() { 254 return LogFactory.getLog(JFreeReportLoadComponent.class); 255 } 256 } 257 | Popular Tags |