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.ui.view.conditionbuilder;
042:
043: import java.util.ArrayList;
044: import java.util.Iterator;
045: import java.util.List;
046:
047: import org.netbeans.modules.sql.framework.model.RuntimeDatabaseModel;
048: import org.netbeans.modules.sql.framework.model.RuntimeInput;
049: import org.netbeans.modules.sql.framework.model.SQLCondition;
050: import org.netbeans.modules.sql.framework.model.SQLDefinition;
051: import org.netbeans.modules.sql.framework.model.SQLGroupBy;
052: import org.netbeans.modules.sql.framework.model.SQLJoinOperator;
053: import org.netbeans.modules.sql.framework.model.SQLModelObjectFactory;
054: import org.netbeans.modules.sql.framework.model.SQLObject;
055: import org.netbeans.modules.sql.framework.model.SQLWhen;
056: import org.netbeans.modules.sql.framework.model.SourceTable;
057: import org.netbeans.modules.sql.framework.model.TargetTable;
058: import org.netbeans.modules.sql.framework.model.impl.SQLCustomOperatorImpl;
059: import org.netbeans.modules.sql.framework.model.impl.ValidationInfoImpl;
060: import org.netbeans.modules.sql.framework.model.utils.SQLObjectUtil;
061: import org.netbeans.modules.sql.framework.ui.graph.IGraphView;
062: import org.netbeans.modules.sql.framework.ui.graph.IOperatorXmlInfoModel;
063: import org.netbeans.modules.sql.framework.ui.model.CollabSQLUIModel;
064: import org.netbeans.modules.sql.framework.ui.model.SQLUIModel;
065: import org.netbeans.modules.sql.framework.ui.view.IGraphViewContainer;
066:
067: import com.sun.sql.framework.exception.BaseException;
068: import org.netbeans.modules.sql.framework.model.SQLJoinView;
069: import org.netbeans.modules.sql.framework.model.ValidationInfo;
070:
071: /**
072: * Utility class for generating ConditionBuilderView instances for various situations.
073: *
074: * @author Ritesh Adval
075: * @version $Revision$
076: */
077: public class ConditionBuilderUtil {
078:
079: public static ConditionBuilderView getConditionBuilderView(
080: SourceTable sTable, IGraphViewContainer gContainer) {
081: ArrayList tables = new ArrayList(1);
082: tables.add(sTable);
083: addRuntimeInput(sTable, tables);
084: ConditionBuilderView cView = new ConditionBuilderView(
085: gContainer, tables, sTable.getExtractionCondition(),
086: IOperatorXmlInfoModel.CATEGORY_FILTER);
087: return cView;
088: }
089:
090: public static ConditionBuilderView getConditionBuilderView(
091: SQLJoinOperator join, IGraphViewContainer gContainer) {
092: ArrayList tables = new ArrayList(1);
093: tables.addAll(join.getAllSourceTables());
094: addRuntimeInput(join, tables);
095: ConditionBuilderView cView = new ConditionBuilderView(
096: gContainer, tables, join.getJoinCondition(),
097: IOperatorXmlInfoModel.CATEGORY_FILTER);
098: return cView;
099: }
100:
101: public static ConditionBuilderView getHavingConditionBuilderView(
102: SQLObject obj, IGraphViewContainer gContainer) {
103: ArrayList tables = new ArrayList(1);
104: SQLGroupBy groupBy = null;
105: if (obj instanceof SourceTable) {
106: tables.add((SourceTable) obj);
107: groupBy = ((SourceTable) obj).getSQLGroupBy();
108: } else if (obj instanceof TargetTable) {
109: try {
110: tables.addAll(((TargetTable) obj).getSourceTableList());
111: } catch (BaseException be) {
112: throw new IllegalStateException("Unable to get "
113: + "Source tables list for the target table: "
114: + ((TargetTable) obj).getDisplayName());
115: }
116: groupBy = ((TargetTable) obj).getSQLGroupBy();
117: } else if (obj instanceof SQLJoinView) {
118: tables.addAll(((SQLJoinView) obj).getSourceTables());
119: groupBy = ((SQLJoinView) obj).getSQLGroupBy();
120: }
121:
122: SQLCondition havingByCondition = groupBy.getHavingCondition();
123: if (havingByCondition == null) {
124: havingByCondition = SQLModelObjectFactory.getInstance()
125: .createSQLCondition("Having Condition");
126: }
127: ConditionBuilderView cView = new ConditionBuilderView(
128: gContainer, tables, havingByCondition,
129: IOperatorXmlInfoModel.CATEGORY_FILTER);
130: return cView;
131: }
132:
133: public static ConditionBuilderView getJoinConditionBuilderView(
134: TargetTable tTable, IGraphViewContainer gContainer) {
135: ArrayList tables = new ArrayList(1);
136: tables.add(tTable);
137:
138: try {
139: tables.addAll(tTable.getSourceTableList());
140: } catch (BaseException be) {
141: throw new IllegalStateException(
142: "Unable to get Source tables list for the target table: "
143: + tTable.getDisplayName());
144: }
145:
146: addRuntimeInput(tTable, tables);
147: ConditionBuilderView cView = new ConditionBuilderView(
148: gContainer, tables, tTable.getJoinCondition(),
149: IOperatorXmlInfoModel.CATEGORY_FILTER);
150: return cView;
151: }
152:
153: public static ConditionBuilderView getFilterConditionBuilderView(
154: TargetTable tTable, IGraphViewContainer gContainer) {
155: ArrayList tables = new ArrayList(1);
156: tables.add(tTable);
157:
158: try {
159: tables.addAll(tTable.getSourceTableList());
160: } catch (BaseException be) {
161: throw new IllegalStateException(
162: "Unable to get Source tables list for the target table: "
163: + tTable.getDisplayName());
164: }
165:
166: addRuntimeInput(tTable, tables);
167: ConditionBuilderView cView = new ConditionBuilderView(
168: gContainer, tables, tTable.getFilterCondition(),
169: IOperatorXmlInfoModel.CATEGORY_FILTER);
170: return cView;
171: }
172:
173: public static ConditionBuilderView getValidationConditionBuilderView(
174: SourceTable sTable, IGraphViewContainer gContainer) {
175: ArrayList tables = new ArrayList(1);
176: tables.add(sTable);
177: addRuntimeInput(sTable, tables);
178: ConditionBuilderView cView = new ConditionBuilderView(
179: gContainer, tables,
180: sTable.getDataValidationCondition(),
181: IOperatorXmlInfoModel.CATEGORY_VALIDATION);
182: return cView;
183: }
184:
185: public static ConditionBuilderView getConditionBuilderView(
186: SQLWhen whenCondition, IGraphViewContainer gContainer) {
187: IGraphView view = gContainer.getGraphView();
188: if (view == null) {
189: throw new IllegalStateException(
190: "No IGraphView instance associated with given IGraphViewContainer.");
191: }
192:
193: SQLUIModel model = (SQLUIModel) view.getGraphModel();
194: if (!(model instanceof CollabSQLUIModel)) {
195: throw new IllegalStateException(
196: "No CollabSQLUIModel instance associated with IGraphView.");
197: }
198:
199: SQLDefinition sqlDef = ((CollabSQLUIModel) model)
200: .getSQLDefinition();
201: List tables = new ArrayList(sqlDef.getSourceTables());
202:
203: RuntimeInput runtimeInput = getRuntimeInput(sqlDef);
204: if (runtimeInput != null) {
205: tables.add(runtimeInput);
206: }
207:
208: SQLCondition condition = whenCondition.getCondition();
209: ConditionBuilderView cView = new ConditionBuilderView(
210: gContainer, tables, condition,
211: IOperatorXmlInfoModel.CATEGORY_FILTER);
212: return cView;
213: }
214:
215: private static void addRuntimeInput(SQLObject obj, List tables) {
216: RuntimeInput rInput = getRuntimeInput(obj);
217: if (rInput != null) {
218: tables.add(rInput);
219: }
220: }
221:
222: private static RuntimeInput getRuntimeInput(SQLObject obj) {
223: return getRuntimeInput(SQLObjectUtil
224: .getAncestralSQLDefinition(obj));
225: }
226:
227: private static RuntimeInput getRuntimeInput(
228: SQLDefinition sqlDefinition) {
229: if (sqlDefinition != null) {
230: RuntimeDatabaseModel runModel = sqlDefinition
231: .getRuntimeDbModel();
232: if (runModel != null) {
233: RuntimeInput rInput = runModel.getRuntimeInput();
234: return rInput;
235: }
236: }
237: return null;
238: }
239:
240: /**
241: * This method examines and filters the validation errors(?) for
242: * CustomOperator. Reasoning behind this is that custom operators
243: * are defined by the user and does not have a template to validate
244: * at the condition builder level. It can be effectively be validated
245: * only at the Test colloboration level where the sql gets executed
246: * at the host database of the custom function. Hence ignoring the
247: * validation against the dummy userFx template at the condition builder
248: * level.
249: * @param validationList collection of validationInfoImpl objects
250: * generated by the parser.
251: * @return
252: */
253: public static List<ValidationInfo> filterValidations(
254: List validationList) {
255: Iterator iter = validationList.iterator();
256: List<ValidationInfo> newList = new ArrayList<ValidationInfo>();
257: while (iter.hasNext()) {
258: ValidationInfo valInfoImpl = (ValidationInfoImpl) iter
259: .next();
260: Object valObj = valInfoImpl.getValidatedObject();
261: if (!(valObj instanceof SQLCustomOperatorImpl)) {
262: newList.add(valInfoImpl);
263: }
264: }
265: return newList;
266: }
267: }
|