1 23 package org.pentaho.plugin.core; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.pentaho.core.connection.IPentahoResultSet; 28 import org.pentaho.messages.Messages; 29 import org.pentaho.plugin.ComponentBase; 30 31 public class ResultSetCompareComponent extends ComponentBase { 32 33 private static final long serialVersionUID = -1449352563247459588L; 34 35 private static final String RESULT_SET_1 = "result-set-from"; 37 private static final String RESULT_SET_2 = "result-set-to"; 39 private static final String OUTPUT_MISMATCHES = "output_mismatches"; 41 private static final String RSCOLUMN = "compare-column"; 43 private static final String COMPARE_RESULT_OUTPUT = "compare-result"; 45 private static final String STOP_ON_ERROR = "stop-on-error"; 47 private static final String COMPARE_RESULT_OK = "No Mismatches"; 49 protected boolean validateAction() { 50 51 if (!isDefinedInput(RESULT_SET_1)) { 52 error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0001_INPUT_RS1_UNDEFINED")); return false; 54 } 55 56 if (!isDefinedInput(RESULT_SET_2)) { 57 error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0002_INPUT_RS2_UNDEFINED")); return false; 59 } 60 if (!isDefinedInput(RSCOLUMN)) { 61 error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0003_COLUMN_UNDEFINED")); return false; 63 } 64 65 return true; 66 } 67 68 protected boolean validateSystemSettings() { 69 return true; 71 } 72 73 public void done() { 74 } 76 77 protected boolean executeAction() throws Throwable { 78 IPentahoResultSet rs1 = (IPentahoResultSet) getInputValue(RESULT_SET_1); 79 IPentahoResultSet rs2 = (IPentahoResultSet) getInputValue(RESULT_SET_2); 80 String outMismatchStr = getInputStringValue(OUTPUT_MISMATCHES); 81 if (outMismatchStr == null) { 82 outMismatchStr = "false"; } 84 boolean outputMismatches = outMismatchStr.equalsIgnoreCase("true"); 86 String compareResult = getInputStringValue(COMPARE_RESULT_OUTPUT); 87 if (compareResult == null && getOutputNames().contains(COMPARE_RESULT_OUTPUT)) { 88 compareResult = COMPARE_RESULT_OUTPUT; 90 } 91 boolean stopOnError = getInputBooleanValue(STOP_ON_ERROR, true); 92 93 String col = getInputStringValue(RSCOLUMN); 94 if (col == null) { 95 error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0003_COLUMN_UNDEFINED")); return false; 97 } 98 int compareCol = Integer.parseInt(col); 99 100 Object obj = getInputValue(RESULT_SET_1); 101 if (!(obj instanceof IPentahoResultSet)) { 102 error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0004_INPUT_RS1_NOT_RS")); return false; 104 } 105 obj = getInputValue(RESULT_SET_2); 106 if (!(obj instanceof IPentahoResultSet)) { 107 error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0005_INPUT_RS2_NOT_RS")); return false; 109 } 110 111 return compareEquals(rs1, rs2, compareCol, outputMismatches, stopOnError, compareResult); 112 } 113 114 private boolean compareEquals(IPentahoResultSet rs1, IPentahoResultSet rs2, int compareCol, boolean outputMismatches, boolean stopOnError, String outputVar) { 115 int sourceRowCount = rs1.getRowCount(); 116 int sourceColCount = rs1.getColumnCount(); 117 int compRowCount = rs2.getRowCount(); 118 int compColCount = rs2.getColumnCount(); 119 StringBuffer outputBuf = new StringBuffer (); 120 if (!outputMismatches) { 121 if (sourceRowCount != compRowCount) { 122 error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0006_RESULTSETS_ROWCOUNT_WRONG")); return false; 124 } 125 if (sourceColCount != compColCount) { 126 error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0007_RESULTSETS_COLUMNCOUNT_WRONG")); return false; 128 } 129 } 130 if (compareCol > sourceColCount) { 131 error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0008_COLUMN_NOT_FOUND") + compareCol); return false; 133 } 134 if (compareCol > compColCount) { 135 error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0009_COMPARISON_COLUMN_NOT_FOUND") + compareCol); return false; 137 } 138 boolean anyMismatches = false; 139 boolean foundIt; 140 Object srcValue = null, compValue = null; 141 for (int sourceRows = 0; sourceRows < sourceRowCount; sourceRows++) { 142 foundIt = false; 143 srcValue = rs1.getValueAt(sourceRows, compareCol); 144 for (int compRows = 0; compRows < compRowCount; compRows++) { 146 compValue = rs2.getValueAt(compRows, compareCol); 147 if (compValue.equals(srcValue)) { 148 foundIt = true; 149 break; 150 } 151 } 152 if (!foundIt) { 153 if (outputBuf.length() > 0) { 154 outputBuf.append(",").append(srcValue.toString().trim()); } else { 156 outputBuf.append(srcValue.toString().trim()); 157 } 158 if (outputVar != null) { 159 setOutputValue(outputVar, outputBuf.toString()); 160 } 161 if (outputMismatches) { 162 error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0010_MISMATCH_OUTPUT", srcValue.toString())); anyMismatches = true; 164 } else { 165 if (stopOnError) { 166 return false; 167 } 168 } 169 } 170 } 171 if (!anyMismatches) { 172 if (outputVar != null) { 173 setOutputValue(outputVar, COMPARE_RESULT_OK); 174 } 175 } 176 return stopOnError ? !anyMismatches : true; 177 } 178 179 public boolean init() { 180 return true; 182 } 183 184 public Log getLogger() { 185 return LogFactory.getLog(ResultSetCompareComponent.class); 186 } 187 188 } 189 | Popular Tags |