1 16 package org.pentaho.plugin.jfreereport.outputs; 17 18 import java.io.File ; 19 20 import javax.swing.table.TableModel ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.jfree.io.IOUtils; 25 import org.jfree.report.JFreeReport; 26 import org.jfree.report.ResourceBundleFactory; 27 import org.jfree.report.util.CloseableTableModel; 28 import org.pentaho.messages.Messages; 29 import org.pentaho.plugin.jfreereport.AbstractJFreeReportComponent; 30 import org.pentaho.plugin.jfreereport.helper.PentahoResourceBundleFactory; 31 32 42 public abstract class AbstractGenerateContentComponent extends 43 AbstractJFreeReportComponent { 44 protected AbstractGenerateContentComponent() { 45 } 46 47 protected boolean validateAction() { 48 if (isDefinedInput(DATACOMPONENT_REPORTTEMP_OBJINPUT) == false) { 49 warn(Messages 50 .getString("AbstractGenerateContentComponent.ERROR_0038_NO_REPORT_OBJECT_INPUT")); return false; 52 } 53 54 if (isDefinedInput(DATACOMPONENT_REPORTTEMP_DATAINPUT) == false) { 55 warn(Messages 56 .getString("AbstractGenerateContentComponent.ERROR_0039_NO_REPORT_DATA_PARAMETER")); return false; 58 } 59 60 if (isDefinedInput(REPORTGENERATE_YIELDRATE)) { 61 final Object inputValue = getInputValue(REPORTGENERATE_YIELDRATE); 62 if (inputValue instanceof Number ) { 63 Number n = (Number ) inputValue; 64 if (n.intValue() < 1) { 65 warn(Messages 66 .getString("AbstractGenerateContentComponent.ERROR_0040_YIELD_RATE_POSITIVE")); } 68 } else { 69 warn(Messages 70 .getString("AbstractGenerateContentComponent.ERROR_0041_YIELD_RATE_NUMERIC")); return false; 72 } 73 } 74 if (isDefinedInput(REPORTGENERATE_PRIORITYINPUT)) { 75 final String inputValue = getInputStringValue(REPORTGENERATE_PRIORITYINPUT); 76 if (REPORTGENERATE_PRIORITYNORMAL.equals(inputValue) == false 77 && REPORTGENERATE_PRIORITYLOWER.equals(inputValue) == false 78 && REPORTGENERATE_PRIORITYLOWEST.equals(inputValue) == false) { 79 warn(Messages 80 .getString("AbstractGenerateContentComponent.ERROR_0042_PRIORITY_MUST_BE")); } 82 } 83 return true; 84 } 85 86 protected JFreeReport getReport() { 87 Object maybeJFreeReport = getInputValue(DATACOMPONENT_REPORTTEMP_OBJINPUT); 88 if (maybeJFreeReport instanceof JFreeReport) { 89 return (JFreeReport) maybeJFreeReport; 90 } 91 return null; 92 } 93 94 protected TableModel getData() { 95 if (isDefinedInput(DATACOMPONENT_REPORTTEMP_DATAINPUT)) { 96 Object maybeTableModel = getInputValue(DATACOMPONENT_REPORTTEMP_DATAINPUT); 97 if (maybeTableModel instanceof TableModel ) { 98 return (TableModel ) maybeTableModel; 99 } 100 } 101 return null; 102 } 103 104 protected boolean validateSystemSettings() { 105 return true; 106 } 107 108 protected boolean executeAction() throws Throwable { 109 JFreeReport report = getReport(); 110 if (report == null) { 111 warn(Messages 112 .getString("AbstractGenerateContentComponent.ERROR_0043_NO_REPORT_FOR_ACTION")); return false; 114 } 115 116 applyThreadPriority(); 117 118 final boolean privateCopy = getInputBooleanValue( 119 REPORTPARAMCOMPONENT_PRIVATEREPORT_OUTPUT, false); 120 if (privateCopy && isDefinedOutput(DATACOMPONENT_REPORTTEMP_OBJINPUT)) { 121 report = (JFreeReport) report.clone(); 122 } 123 124 final String baseName = IOUtils.getInstance().stripFileExtension( 126 getActionName()); 127 final String path = getSolutionName() + File.separator + getSolutionPath(); 128 final PentahoResourceBundleFactory bundleFactory = new PentahoResourceBundleFactory( 129 path, baseName, getSession()); 130 report.setResourceBundleFactory(bundleFactory); 131 report.getReportConfiguration().setConfigProperty( 135 ResourceBundleFactory.DEFAULT_RESOURCE_BUNDLE_CONFIG_KEY, baseName); 136 137 TableModel data = getData(); 138 if (data != null) { 139 report.setData(data); 140 } 141 142 return performExport(report); 143 } 144 145 private void applyThreadPriority() { 146 if (isDefinedInput(REPORTGENERATE_PRIORITYINPUT)) 147 { 148 try { 149 final String inputValue = getInputStringValue(REPORTGENERATE_PRIORITYINPUT); 150 if (REPORTGENERATE_PRIORITYLOWER.equals(inputValue)) { 151 final int priority = Math.max( 152 Thread.currentThread().getPriority() - 1, 1); 153 Thread.currentThread().setPriority(priority); 154 } else if (REPORTGENERATE_PRIORITYLOWEST.equals(inputValue)) { 155 Thread.currentThread().setPriority(1); 156 } 157 } catch (Exception e) { 158 warn(Messages 160 .getString("AbstractGenerateContentComponent.ERROR_0044_UNABLE_T0_SET_THREAD_PRIORITY")); } 162 } 163 } 164 165 protected abstract boolean performExport(final JFreeReport report); 166 167 public void done() { 168 TableModel model = getData(); 169 if (model instanceof CloseableTableModel) { 170 CloseableTableModel closeableTableModel = (CloseableTableModel) model; 171 closeableTableModel.close(); 172 } 173 } 174 175 public boolean init() { 176 return true; 177 } 178 179 public Log getLogger() { 180 return LogFactory.getLog(getClass()); 181 } 182 183 protected int getYieldRate() { 184 if (isDefinedInput(REPORTGENERATE_YIELDRATE)) { 185 final Object inputValue = getInputValue(REPORTGENERATE_YIELDRATE); 186 if (inputValue instanceof Number ) { 187 Number n = (Number ) inputValue; 188 if (n.intValue() < 1) { 189 return 0; 190 } 191 return n.intValue(); 192 } 193 } 194 return 0; 195 } 196 197 } 198 | Popular Tags |