KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > visualeditor > querymodel > BooleanExpressionList


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.db.sql.visualeditor.querymodel;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Collection JavaDoc;
24
25 abstract class BooleanExpressionList implements ExpressionList {
26     protected List JavaDoc _expressions;
27
28     // New: Return the count of criteria
29
public Expression findExpression(String JavaDoc table1, String JavaDoc column1, String JavaDoc table2, String JavaDoc column2) {
30         if (_expressions != null) {
31             for (int index=0; index<_expressions.size(); index++) {
32                 Expression cond = (Expression)_expressions.get(index);
33                 cond = cond.findExpression(table1, column1, table2, column2);
34                 if (cond != null)
35                     return cond;
36             }
37         }
38         return null;
39     }
40
41     // get the columns specified in the condition if any
42
public void getReferencedColumns(Collection JavaDoc comlumns) {
43         if (_expressions != null) {
44             for (int index=0; index<_expressions.size(); index++) {
45                 Expression expr = (Expression)_expressions.get(index);
46                 expr.getReferencedColumns(comlumns);
47             }
48         }
49     }
50
51     public void getQueryItems(Collection JavaDoc items) {
52         if (_expressions != null)
53             items.addAll(_expressions);
54     }
55
56     public int size() {
57         if (_expressions != null)
58             return _expressions.size();
59         return 0;
60     }
61
62     public Expression getExpression(int i) {
63         return (Expression)_expressions.get(i);
64     }
65
66     public void addExpression(Expression expression) {
67         _expressions.add(expression);
68     }
69
70     public void addExpression(int index, Expression expression) {
71         _expressions.add(index, expression);
72     }
73
74     public void replaceExpression(int index, Expression expression) {
75         _expressions.remove(index);
76         _expressions.add(index, expression);
77     }
78
79     public void removeExpression(int index) {
80         _expressions.remove(index);
81     }
82
83     public void removeTable(String JavaDoc tableSpec) {
84         int size = _expressions.size();
85         for (int i = size - 1; i >= 0; i--) {
86             Expression expr = (Expression)_expressions.get(i);
87             if (expr instanceof ExpressionList) {
88                 ExpressionList list = (ExpressionList)expr;
89                 list.removeTable(tableSpec);
90                 if (list.size() == 0) {
91                     // this is odd and needs a bit more thought. It should never happen though.
92
// anyway remove the expressions and run this method again
93
_expressions.remove(i);
94                 }
95             }
96             else {
97                 // for any other kind of expression remove the whole expression if the table is referenced
98
ArrayList JavaDoc column = new ArrayList JavaDoc();
99                 expr.getReferencedColumns(column);
100                 for (int j = 0; j < column.size(); j++) {
101                     Column col = (Column)column.get(j);
102                     if (col.matches(tableSpec)) {
103                         _expressions.remove(i);
104                         break;
105                     }
106                 }
107             }
108         }
109     }
110
111     public boolean isParameterized() {
112         if (_expressions != null) {
113             for (int index=0; index<_expressions.size(); index++) {
114                 Expression expr = (Expression)_expressions.get(index);
115                 if (expr.isParameterized())
116                     return true;
117             }
118         }
119         return false;
120     }
121
122     public void renameTableSpec(String JavaDoc oldTableSpec, String JavaDoc corrName) {
123         if (_expressions != null) {
124             for (int index=0; index<_expressions.size(); index++) {
125                 Expression expr = (Expression)_expressions.get(index);
126                 expr.renameTableSpec(oldTableSpec, corrName);
127             }
128         }
129     }
130
131     static protected void flattenExpression(List JavaDoc expressionsToFlatten, Class JavaDoc typeToLookFor, List JavaDoc expressions) {
132         int size = expressionsToFlatten.size();
133         for (int i = 0; i < size; i++) {
134             Object JavaDoc expr = expressionsToFlatten.get(i);
135             if (expr.getClass() == typeToLookFor) {
136                 flattenExpression(((BooleanExpressionList)expr)._expressions, typeToLookFor, expressions);
137             }
138             else {
139                 expressions.add(expr);
140             }
141         }
142     }
143 }
144
145
146
Popular Tags