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.db.sql.visualeditor.querymodel;
042:
043: import java.util.ArrayList;
044: import java.util.List;
045: import java.util.Collection;
046:
047: abstract class BooleanExpressionList implements ExpressionList {
048: protected List _expressions;
049:
050: // New: Return the count of criteria
051: public Expression findExpression(String table1, String column1,
052: String table2, String column2) {
053: if (_expressions != null) {
054: for (int index = 0; index < _expressions.size(); index++) {
055: Expression cond = (Expression) _expressions.get(index);
056: cond = cond.findExpression(table1, column1, table2,
057: column2);
058: if (cond != null)
059: return cond;
060: }
061: }
062: return null;
063: }
064:
065: // get the columns specified in the condition if any
066: public void getReferencedColumns(Collection comlumns) {
067: if (_expressions != null) {
068: for (int index = 0; index < _expressions.size(); index++) {
069: Expression expr = (Expression) _expressions.get(index);
070: expr.getReferencedColumns(comlumns);
071: }
072: }
073: }
074:
075: public int size() {
076: if (_expressions != null)
077: return _expressions.size();
078: return 0;
079: }
080:
081: public Expression getExpression(int i) {
082: return (Expression) _expressions.get(i);
083: }
084:
085: public void addExpression(Expression expression) {
086: _expressions.add(expression);
087: }
088:
089: public void addExpression(int index, Expression expression) {
090: _expressions.add(index, expression);
091: }
092:
093: public void replaceExpression(int index, Expression expression) {
094: _expressions.remove(index);
095: _expressions.add(index, expression);
096: }
097:
098: public void removeExpression(int index) {
099: _expressions.remove(index);
100: }
101:
102: public void removeTable(String tableSpec) {
103: int size = _expressions.size();
104: for (int i = size - 1; i >= 0; i--) {
105: Expression expr = (Expression) _expressions.get(i);
106: if (expr instanceof ExpressionList) {
107: ExpressionList list = (ExpressionList) expr;
108: list.removeTable(tableSpec);
109: if (list.size() == 0) {
110: // this is odd and needs a bit more thought. It should never happen though.
111: // anyway remove the expressions and run this method again
112: _expressions.remove(i);
113: }
114: } else {
115: // for any other kind of expression remove the whole expression if the table is referenced
116: ArrayList column = new ArrayList();
117: expr.getReferencedColumns(column);
118: for (int j = 0; j < column.size(); j++) {
119: Column col = (Column) column.get(j);
120: if (col.matches(tableSpec)) {
121: _expressions.remove(i);
122: break;
123: }
124: }
125: }
126: }
127: }
128:
129: public boolean isParameterized() {
130: if (_expressions != null) {
131: for (int index = 0; index < _expressions.size(); index++) {
132: Expression expr = (Expression) _expressions.get(index);
133: if (expr.isParameterized())
134: return true;
135: }
136: }
137: return false;
138: }
139:
140: public void renameTableSpec(String oldTableSpec, String corrName) {
141: if (_expressions != null) {
142: for (int index = 0; index < _expressions.size(); index++) {
143: Expression expr = (Expression) _expressions.get(index);
144: expr.renameTableSpec(oldTableSpec, corrName);
145: }
146: }
147: }
148:
149: static protected void flattenExpression(List expressionsToFlatten,
150: Class typeToLookFor, List expressions) {
151: int size = expressionsToFlatten.size();
152: for (int i = 0; i < size; i++) {
153: Object expr = expressionsToFlatten.get(i);
154: if (expr.getClass() == typeToLookFor) {
155: flattenExpression(
156: ((BooleanExpressionList) expr)._expressions,
157: typeToLookFor, expressions);
158: } else {
159: expressions.add(expr);
160: }
161: }
162: }
163: }
|