1 13 package org.pentaho.plugin.jfreereport.components; 14 15 import java.io.ByteArrayOutputStream ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.util.Iterator ; 19 import java.util.Set ; 20 import java.util.zip.ZipEntry ; 21 import java.util.zip.ZipInputStream ; 22 23 import org.apache.commons.logging.Log; 24 import org.pentaho.core.admin.datasources.DataSourceInfo; 25 import org.pentaho.core.admin.datasources.jboss.JBossDatasourceAdmin; 26 import org.pentaho.core.connection.IPentahoConnection; 27 import org.pentaho.core.connection.IPentahoResultSet; 28 import org.pentaho.core.repository.ISolutionRepository; 29 import org.pentaho.core.runtime.IActionParameter; 30 import org.pentaho.core.solution.IActionResource; 31 import org.pentaho.core.system.PentahoSystem; 32 import org.pentaho.data.PentahoConnectionFactory; 33 import org.pentaho.jfreereport.castormodel.reportspec.ReportSpec; 34 import org.pentaho.jfreereport.wizard.utility.CastorUtility; 35 import org.pentaho.jfreereport.wizard.utility.ReportGenerationUtility; 36 import org.pentaho.jfreereport.wizard.utility.connection.ConnectionUtility; 37 import org.pentaho.plugin.jfreereport.AbstractJFreeReportComponent; 38 39 45 public class JFreeReportGenerateDefinitionComponent extends 46 AbstractJFreeReportComponent { 47 private static final long serialVersionUID = -6364796568478754207L; 48 49 50 public JFreeReportGenerateDefinitionComponent() { 51 } 52 53 protected boolean validateAction() { 54 if (isDefinedResource(REPORTGENERATEDEFN_REPORTSPECINPUT) == false) { 55 return false; 56 } 57 return true; 58 } 59 60 protected boolean validateSystemSettings() { 61 return true; 62 } 63 64 public void done() { 65 66 } 67 68 76 private ZipEntry findReportSpec(ZipInputStream zStrm) throws IOException { 77 ZipEntry reportSpecEntry = null; 78 for (reportSpecEntry = zStrm.getNextEntry(); null != reportSpecEntry 80 && !reportSpecEntry.getName().endsWith(".xreportspec"); reportSpecEntry = zStrm 81 .getNextEntry()) 82 ; 83 84 return reportSpecEntry; 85 } 86 87 private ReportSpec loadFromZip(IActionResource resource) { 88 final ISolutionRepository solutionRepository = PentahoSystem 89 .getSolutionRepository(getSession()); 90 InputStream reportSpecInputStream = solutionRepository 91 .getResourceInputStream(resource); 92 93 try { 94 ZipInputStream zis = new ZipInputStream (reportSpecInputStream); 95 ZipEntry reportSpecEntry = findReportSpec( zis ); 96 97 if (reportSpecEntry == null) { 98 return null; 99 } 100 101 return (ReportSpec) CastorUtility.getInstance().readCastorObject(zis, 104 ReportSpec.class); 105 } catch (Exception e) { 106 return null; 107 } 108 } 109 110 protected boolean executeAction() { 111 if (isDefinedResource(REPORTGENERATEDEFN_REPORTSPECINPUT) == false) { 112 return false; 113 } 114 115 IActionResource resource = getResource(REPORTGENERATEDEFN_REPORTSPECINPUT); 116 ReportSpec reportSpec = loadFromZip(resource); 117 if (reportSpec == null) { 118 InputStream reportSpecInputStream = PentahoSystem.getSolutionRepository( 119 getSession()).getResourceInputStream(resource); 120 reportSpec = (ReportSpec) CastorUtility.getInstance().readCastorObject( 121 reportSpecInputStream, ReportSpec.class); 122 } 123 if (reportSpec == null) { 124 return false; 125 } 126 127 ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); 128 ReportGenerationUtility.createJFreeReportXML(reportSpec, outputStream, 0, 0, false, 129 "", 0, 0); String reportDefinition = new String (outputStream.toByteArray()); 131 addTempParameterObject(REPORTGENERATEDEFN_REPORTDEFN, reportDefinition); 132 133 if (isDefinedInput(REPORTGENERATEDEFN_REPORTTEMP_PERFQRY)) { 135 if ("true".equals(getInputParameter(REPORTGENERATEDEFN_REPORTTEMP_PERFQRY)) == false) { return true; 137 } 138 } 139 140 JBossDatasourceAdmin admin = new JBossDatasourceAdmin(); 141 if (reportSpec.getReportSpecChoice().getXqueryUrl() != null) { 142 } else { 144 try { 145 DataSourceInfo info = admin.getDataSourceInfo(reportSpec 146 .getReportSpecChoice().getJndiSource()); 147 IPentahoConnection connection = null; 148 if (reportSpec.getIsMDX()) { 149 connection = PentahoConnectionFactory.getConnection( 150 PentahoConnectionFactory.MDX_DATASOURCE, info.getDriver(), info 151 .getUrl(), info.getUserId(), info.getPassword(), null); 152 } else { 153 connection = PentahoConnectionFactory.getConnection( 154 PentahoConnectionFactory.SQL_DATASOURCE, info.getDriver(), info 155 .getUrl(), info.getUserId(), info.getPassword(), null); 156 } 157 String query = ConnectionUtility.setupParametersForActionSequence(reportSpec 158 .getQuery()); 159 query = setupQueryParameters(query); 160 IPentahoResultSet res = connection.executeQuery(query); 161 addTempParameterObject(DATACOMPONENT_DATAINPUT, res); 162 } catch (Exception e) { 163 return false; 164 } 165 } 166 167 return true; 168 } 169 170 public boolean init() { 171 return true; 172 } 173 174 public Log getLogger() { 175 return null; 176 } 177 178 public String setupQueryParameters(String query) { 179 Set inputNames = getInputNames(); 180 Iterator iter = inputNames.iterator(); 181 while (iter.hasNext()) { 182 String inputName = (String ) iter.next(); 183 final IActionParameter inputParameter = getInputParameter(inputName); 184 final Object value = inputParameter.getValue(); 185 if (value instanceof String == false) { 186 continue; 187 } 188 String paramValue = (String ) value; 189 String param = "\\{" + inputName + "\\}"; query = query.replaceAll(param, paramValue); 191 } 192 return query; 193 } 194 195 } 196 | Popular Tags |