001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * The purpose of this component is to compare a column in one resultset with
014: * a column in another result set.
015: *
016: * Assumptions:
017: * a- It assumes the column number in both result sets are the same. This should be changed.
018: *
019: *
020: * Created Jan 5, 2006
021: * @author mbatchel
022: */
023: package org.pentaho.plugin.core;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.pentaho.actionsequence.dom.ActionOutput;
028: import org.pentaho.actionsequence.dom.IActionInputValueProvider;
029: import org.pentaho.actionsequence.dom.actions.ResultSetCompareAction;
030: import org.pentaho.commons.connection.IPentahoResultSet;
031: import org.pentaho.messages.Messages;
032: import org.pentaho.plugin.ComponentBase;
033:
034: public class ResultSetCompareComponent extends ComponentBase {
035:
036: private static final long serialVersionUID = -1449352563247459588L;
037:
038: private static final String COMPARE_RESULT_OK = "No Mismatches"; //$NON-NLS-1$
039:
040: protected boolean validateAction() {
041:
042: boolean actionValidated = true;
043: ResultSetCompareAction compareAction = null;
044:
045: // get report connection setting
046: if (getActionDefinition() instanceof ResultSetCompareAction) {
047: compareAction = (ResultSetCompareAction) getActionDefinition();
048: if (compareAction.getResultSet1() == IActionInputValueProvider.NULL_INPUT) {
049: actionValidated = false;
050: error(Messages
051: .getErrorString("ResultSetCompareComponent.ERROR_0001_INPUT_RS1_UNDEFINED")); //$NON-NLS-1$
052: }
053:
054: if (actionValidated
055: && compareAction.getResultSet2() == IActionInputValueProvider.NULL_INPUT) {
056: actionValidated = false;
057: error(Messages
058: .getErrorString("ResultSetCompareComponent.ERROR_0002_INPUT_RS2_UNDEFINED")); //$NON-NLS-1$
059: }
060:
061: if (actionValidated
062: && compareAction.getCompareColumnNum() == IActionInputValueProvider.NULL_INPUT) {
063: actionValidated = false;
064: error(Messages
065: .getErrorString("ResultSetCompareComponent.ERROR_0003_COLUMN_UNDEFINED")); //$NON-NLS-1$
066: }
067: } else {
068: actionValidated = false;
069: error(Messages
070: .getErrorString(
071: "ComponentBase.ERROR_0001_UNKNOWN_ACTION_TYPE", getActionDefinition().getElement().asXML())); //$NON-NLS-1$
072: }
073:
074: return actionValidated;
075: }
076:
077: protected boolean validateSystemSettings() {
078: // No system settings
079: return true;
080: }
081:
082: public void done() {
083: // No cleanup necessary
084: }
085:
086: protected boolean executeAction() throws Throwable {
087: ResultSetCompareAction compareAction = (ResultSetCompareAction) getActionDefinition();
088:
089: Object obj1 = compareAction.getResultSet1().getValue();
090: if (!(obj1 instanceof IPentahoResultSet)) {
091: error(Messages
092: .getErrorString("ResultSetCompareComponent.ERROR_0004_INPUT_RS1_NOT_RS")); //$NON-NLS-1$
093: return false;
094: }
095:
096: Object obj2 = compareAction.getResultSet2().getValue();
097: if (!(obj2 instanceof IPentahoResultSet)) {
098: error(Messages
099: .getErrorString("ResultSetCompareComponent.ERROR_0005_INPUT_RS2_NOT_RS")); //$NON-NLS-1$
100: return false;
101: }
102:
103: IPentahoResultSet rs1 = (IPentahoResultSet) compareAction
104: .getResultSet1().getValue();
105: IPentahoResultSet rs2 = (IPentahoResultSet) compareAction
106: .getResultSet2().getValue();
107:
108: String tempOutputMismatches = compareAction
109: .getOutputMismatches().getStringValue();
110: boolean outputMismatches = false;
111: if (tempOutputMismatches != null
112: && tempOutputMismatches.trim().toLowerCase().equals(
113: "true")) {
114: outputMismatches = true;
115: }
116:
117: boolean stopOnError = false;
118: String tempStopOnError = compareAction.getStopOnError()
119: .getStringValue();
120: if (tempStopOnError != null
121: && tempStopOnError.trim().toLowerCase().equals("true")) {
122: stopOnError = true;
123: }
124:
125: int compareCol = Integer.parseInt(compareAction
126: .getCompareColumnNum().getStringValue());
127:
128: return compareEquals(rs1, rs2, compareCol, outputMismatches,
129: stopOnError);
130: }
131:
132: private boolean compareEquals(IPentahoResultSet rs1,
133: IPentahoResultSet rs2, int compareCol,
134: boolean outputMismatches, boolean stopOnError) {
135: int sourceRowCount = rs1.getRowCount();
136: int sourceColCount = rs1.getColumnCount();
137: int compRowCount = rs2.getRowCount();
138: int compColCount = rs2.getColumnCount();
139: StringBuffer outputBuf = new StringBuffer();
140: if (!outputMismatches) {
141: if (sourceRowCount != compRowCount) {
142: error(Messages
143: .getErrorString("ResultSetCompareComponent.ERROR_0006_RESULTSETS_ROWCOUNT_WRONG")); //$NON-NLS-1$
144: return false;
145: }
146: if (sourceColCount != compColCount) {
147: error(Messages
148: .getErrorString("ResultSetCompareComponent.ERROR_0007_RESULTSETS_COLUMNCOUNT_WRONG")); //$NON-NLS-1$
149: return false;
150: }
151: }
152: if (compareCol > sourceColCount) {
153: error(Messages
154: .getErrorString("ResultSetCompareComponent.ERROR_0008_COLUMN_NOT_FOUND") + compareCol); //$NON-NLS-1$
155: return false;
156: }
157: if (compareCol > compColCount) {
158: error(Messages
159: .getErrorString("ResultSetCompareComponent.ERROR_0009_COMPARISON_COLUMN_NOT_FOUND") + compareCol); //$NON-NLS-1$
160: return false;
161: }
162: boolean anyMismatches = false;
163: boolean foundIt;
164: Object srcValue = null, compValue = null;
165: ResultSetCompareAction compareAction = (ResultSetCompareAction) getActionDefinition();
166: ActionOutput output = compareAction.getOutputCompareResult();
167: for (int sourceRows = 0; sourceRows < sourceRowCount; sourceRows++) {
168: foundIt = false;
169: srcValue = rs1.getValueAt(sourceRows, compareCol);
170: // n+1 traversal. This accommodates non-ordered input
171: for (int compRows = 0; compRows < compRowCount; compRows++) {
172: compValue = rs2.getValueAt(compRows, compareCol);
173: if (compValue.equals(srcValue)) {
174: foundIt = true;
175: break;
176: }
177: }
178: if (!foundIt) {
179: if (outputBuf.length() > 0) {
180: outputBuf
181: .append(",").append(srcValue.toString().trim()); //$NON-NLS-1$
182: } else {
183: outputBuf.append(srcValue.toString().trim());
184: }
185: if (output != null) {
186: output.setValue(outputBuf.toString());
187: }
188: if (outputMismatches) {
189: error(Messages
190: .getErrorString(
191: "ResultSetCompareComponent.ERROR_0010_MISMATCH_OUTPUT", srcValue.toString())); //$NON-NLS-1$
192: anyMismatches = true;
193: } else {
194: if (stopOnError) {
195: return false;
196: }
197: }
198: }
199: }
200: if (!anyMismatches) {
201: if (output != null) {
202: output.setValue(COMPARE_RESULT_OK);
203: }
204: }
205: return stopOnError ? !anyMismatches : true;
206: }
207:
208: public boolean init() {
209: // no initialization required
210: return true;
211: }
212:
213: public Log getLogger() {
214: return LogFactory.getLog(ResultSetCompareComponent.class);
215: }
216:
217: }
|