001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.model.visitors;
042:
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.Iterator;
046: import java.util.List;
047:
048: import org.netbeans.modules.sql.framework.model.SQLCaseOperator;
049: import org.netbeans.modules.sql.framework.model.SQLCondition;
050: import org.netbeans.modules.sql.framework.model.SQLConstants;
051: import org.netbeans.modules.sql.framework.model.SQLDefinition;
052: import org.netbeans.modules.sql.framework.model.SQLJoinOperator;
053: import org.netbeans.modules.sql.framework.model.SQLJoinView;
054: import org.netbeans.modules.sql.framework.model.SQLWhen;
055: import org.netbeans.modules.sql.framework.model.SourceTable;
056: import org.netbeans.modules.sql.framework.model.TargetTable;
057: import org.netbeans.modules.sql.framework.model.ValidationInfo;
058: import org.netbeans.modules.sql.framework.model.impl.ValidationInfoImpl;
059: import org.openide.util.NbBundle;
060: import net.java.hulp.i18n.Logger;
061: import org.netbeans.modules.etl.logger.Localizer;
062: import org.netbeans.modules.etl.logger.LogUtil;
063:
064: /**
065: * @author Girish Patil
066: * @version $Revision$
067: */
068: public class SQLOperatorInfoVisitor {
069:
070: private static final String LOG_CATEGORY = SQLOperatorInfoVisitor.class
071: .getName();
072: private boolean foundJavaOperator = false;
073: private boolean foundUserFunction = false;
074: private boolean foundValidationCondition = false;
075: private boolean pipelineForced = false;
076:
077: // TODO Add flag to warn when Hex operator is being used on SQLServer or Sybase.
078: private boolean validate = false;
079: private List<ValidationInfo> validationInfoList = new ArrayList<ValidationInfo>();
080: private static transient final Logger mLogger = LogUtil
081: .getLogger(SQLOperatorInfoVisitor.class.getName());
082: private static transient final Localizer mLoc = Localizer.get();
083:
084: public SQLOperatorInfoVisitor() {
085: }
086:
087: public SQLOperatorInfoVisitor(boolean validate) {
088: this .validate = true;
089: }
090:
091: public List<ValidationInfo> getValidationInfoList() {
092: return this .validationInfoList;
093: }
094:
095: public boolean isJavaOperatorFound() {
096: return this .foundJavaOperator;
097: }
098:
099: public boolean isPipelineForced() {
100: return this .pipelineForced;
101: }
102:
103: public boolean isUserFunctionFound() {
104: return this .foundUserFunction;
105: }
106:
107: public boolean isValidationConditionFound() {
108: return this .foundValidationCondition;
109: }
110:
111: public void visit(SourceTable sourceTable) {
112: SQLCondition eCondition = sourceTable.getExtractionCondition();
113: visit(eCondition);
114:
115: SQLCondition vCondition = sourceTable
116: .getDataValidationCondition();
117: if (vCondition != null && vCondition.isConditionDefined()
118: && vCondition.getRootPredicate() != null) {
119: this .foundValidationCondition = true;
120: visit(vCondition);
121: }
122: }
123:
124: public void visit(SQLCaseOperator caseop) {
125: Iterator whenIter = caseop.getWhenList().iterator();
126: while (whenIter.hasNext()) {
127: SQLWhen when = (SQLWhen) whenIter.next();
128: visit(when);
129: }
130: }
131:
132: public void visit(SQLCondition condition) {
133: if (condition != null && condition.isConditionDefined()) {
134: if (condition.isContainsJavaOperators()) {
135: foundJavaOperator = true;
136: }
137:
138: Collection uFunctions = condition
139: .getObjectsOfType(SQLConstants.CUSTOM_OPERATOR);
140: if ((uFunctions != null) && (uFunctions.size() > 0)) {
141: this .foundUserFunction = true;
142: }
143: }
144: }
145:
146: public void visit(SQLDefinition definition) {
147:
148: if (definition.isContainsJavaOperators()) {
149: this .foundJavaOperator = true;
150: }
151:
152: if (SQLDefinition.EXECUTION_STRATEGY_PIPELINE == definition
153: .getExecutionStrategyCode().intValue()) {
154: this .pipelineForced = true;
155: }
156:
157: Iterator it = definition.getTargetTables().iterator();
158: while (it.hasNext()) {
159: TargetTable targetTable = (TargetTable) it.next();
160: visit(targetTable);
161: }
162:
163: it = definition.getObjectsOfType(SQLConstants.CASE).iterator();
164: while (it.hasNext()) {
165: SQLCaseOperator caseop = (SQLCaseOperator) it.next();
166: visit(caseop);
167: }
168:
169: if (this .validate) {
170: if (this .pipelineForced || this .foundValidationCondition
171: || this .foundJavaOperator) {
172: // Pipeline required
173: if (this .foundUserFunction) {
174: String nbBundle1 = mLoc
175: .t("PRSR001: Can not use User function(s) in pipeline/validation mode.");
176: String desc = Localizer.parse(nbBundle1); // NOI18N
177: ValidationInfoImpl validationInfo = new ValidationInfoImpl(
178: definition, desc,
179: ValidationInfo.VALIDATION_ERROR);
180: validationInfoList.add(validationInfo);
181: }
182: } else {
183: if (this .foundUserFunction) {
184: String nbBundle2 = mLoc
185: .t("PRSR001: One or more User function is used. Make sure the function is valid and available on appropriate source or target database. ");
186: String desc = Localizer.parse(nbBundle2);
187: ValidationInfoImpl validationInfo = new ValidationInfoImpl(
188: definition, desc,
189: ValidationInfo.VALIDATION_WARNING);
190: validationInfoList.add(validationInfo);
191: }
192: }
193: }
194: }
195:
196: public void visit(SQLJoinOperator operator) {
197: SQLCondition condition = operator.getJoinCondition();
198: visit(condition);
199: }
200:
201: public void visit(SQLJoinView joinView) {
202: SQLJoinOperator jOperator = joinView.getRootJoin();
203: if (jOperator != null) {
204: visit(jOperator);
205: }
206: }
207:
208: public void visit(SQLWhen when) {
209: SQLCondition condition = when.getCondition();
210: visit(condition);
211: }
212:
213: public void visit(TargetTable targetTable) {
214: // Visit Target Join Condition
215: SQLCondition joinCondition = targetTable.getJoinCondition();
216: visit(joinCondition);
217:
218: // Visit Target Filter Condition
219: SQLCondition filterCondition = targetTable.getFilterCondition();
220: visit(filterCondition);
221:
222: // Visit view
223: SQLJoinView joinView = targetTable.getJoinView();
224: if (joinView != null) {
225: visit(joinView);
226: }
227:
228: // Visit Source Tables
229: try {
230: Iterator itr = targetTable.getSourceTableList().iterator();
231: while (itr.hasNext()) {
232: SourceTable sourceTable = (SourceTable) itr.next();
233: visit(sourceTable);
234: }
235: } catch (Exception ex) {
236: mLogger
237: .errorNoloc(
238: mLoc
239: .t(
240: "PRSR129: Could not find source tables for this target table{0}",
241: LOG_CATEGORY), ex);
242: }
243: }
244: }
|