KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > core > ResultSetCompareComponent


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * The purpose of this component is to compare a column in one resultset with
14  * a column in another result set.
15  *
16  * Assumptions:
17  * a- It assumes the column number in both result sets are the same. This should be changed.
18  *
19  *
20  * Created Jan 5, 2006
21  * @author mbatchel
22  */

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 JavaDoc RESULT_SET_1 = "result-set-from"; //$NON-NLS-1$
36

37     private static final String JavaDoc RESULT_SET_2 = "result-set-to"; //$NON-NLS-1$
38

39     private static final String JavaDoc OUTPUT_MISMATCHES = "output_mismatches"; //$NON-NLS-1$
40

41     private static final String JavaDoc RSCOLUMN = "compare-column"; //$NON-NLS-1$
42

43     private static final String JavaDoc COMPARE_RESULT_OUTPUT = "compare-result"; //$NON-NLS-1$
44

45     private static final String JavaDoc STOP_ON_ERROR = "stop-on-error"; //$NON-NLS-1$
46

47     private static final String JavaDoc COMPARE_RESULT_OK = "No Mismatches"; //$NON-NLS-1$
48

49     protected boolean validateAction() {
50
51         if (!isDefinedInput(RESULT_SET_1)) {
52             error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0001_INPUT_RS1_UNDEFINED")); //$NON-NLS-1$
53
return false;
54         }
55
56         if (!isDefinedInput(RESULT_SET_2)) {
57             error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0002_INPUT_RS2_UNDEFINED")); //$NON-NLS-1$
58
return false;
59         }
60         if (!isDefinedInput(RSCOLUMN)) {
61             error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0003_COLUMN_UNDEFINED")); //$NON-NLS-1$
62
return false;
63         }
64
65         return true;
66     }
67
68     protected boolean validateSystemSettings() {
69         // No system settings
70
return true;
71     }
72
73     public void done() {
74         // No cleanup necessary
75
}
76
77     protected boolean executeAction() throws Throwable JavaDoc {
78         IPentahoResultSet rs1 = (IPentahoResultSet) getInputValue(RESULT_SET_1);
79         IPentahoResultSet rs2 = (IPentahoResultSet) getInputValue(RESULT_SET_2);
80         String JavaDoc outMismatchStr = getInputStringValue(OUTPUT_MISMATCHES);
81         if (outMismatchStr == null) {
82             outMismatchStr = "false"; //$NON-NLS-1$
83
}
84         boolean outputMismatches = outMismatchStr.equalsIgnoreCase("true"); //$NON-NLS-1$
85

86         String JavaDoc compareResult = getInputStringValue(COMPARE_RESULT_OUTPUT);
87         if (compareResult == null && getOutputNames().contains(COMPARE_RESULT_OUTPUT)) {
88             // Get the compare-result node - This is the preferred method to use
89
compareResult = COMPARE_RESULT_OUTPUT;
90         }
91         boolean stopOnError = getInputBooleanValue(STOP_ON_ERROR, true);
92
93         String JavaDoc col = getInputStringValue(RSCOLUMN);
94         if (col == null) {
95             error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0003_COLUMN_UNDEFINED")); //$NON-NLS-1$
96
return false;
97         }
98         int compareCol = Integer.parseInt(col);
99
100         Object JavaDoc obj = getInputValue(RESULT_SET_1);
101         if (!(obj instanceof IPentahoResultSet)) {
102             error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0004_INPUT_RS1_NOT_RS")); //$NON-NLS-1$
103
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")); //$NON-NLS-1$
108
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 JavaDoc outputVar) {
115         int sourceRowCount = rs1.getRowCount();
116         int sourceColCount = rs1.getColumnCount();
117         int compRowCount = rs2.getRowCount();
118         int compColCount = rs2.getColumnCount();
119         StringBuffer JavaDoc outputBuf = new StringBuffer JavaDoc();
120         if (!outputMismatches) {
121             if (sourceRowCount != compRowCount) {
122                 error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0006_RESULTSETS_ROWCOUNT_WRONG")); //$NON-NLS-1$
123
return false;
124             }
125             if (sourceColCount != compColCount) {
126                 error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0007_RESULTSETS_COLUMNCOUNT_WRONG")); //$NON-NLS-1$
127
return false;
128             }
129         }
130         if (compareCol > sourceColCount) {
131             error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0008_COLUMN_NOT_FOUND") + compareCol); //$NON-NLS-1$
132
return false;
133         }
134         if (compareCol > compColCount) {
135             error(Messages.getErrorString("ResultSetCompareComponent.ERROR_0009_COMPARISON_COLUMN_NOT_FOUND") + compareCol); //$NON-NLS-1$
136
return false;
137         }
138         boolean anyMismatches = false;
139         boolean foundIt;
140         Object JavaDoc srcValue = null, compValue = null;
141         for (int sourceRows = 0; sourceRows < sourceRowCount; sourceRows++) {
142             foundIt = false;
143             srcValue = rs1.getValueAt(sourceRows, compareCol);
144             // n+1 traversal. This accommodates non-ordered input
145
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()); //$NON-NLS-1$
155
} 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())); //$NON-NLS-1$
163
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         // no initialization required
181
return true;
182     }
183
184     public Log getLogger() {
185         return LogFactory.getLog(ResultSetCompareComponent.class);
186     }
187
188 }
189
Popular Tags