View Javadoc

1   /*
2   Copyright (c) 2008 Health Market Science, Inc.
3   
4   This library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License.
8   
9   This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  Lesser General Public License for more details.
13  
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
17  USA
18  
19  You can contact Health Market Science at info@healthmarketscience.com
20  or at the following address:
21  
22  Health Market Science
23  2700 Horizon Drive
24  Suite 200
25  King of Prussia, PA 19406
26  */
27  
28  package com.healthmarketscience.sqlbuilder;
29  
30  import java.util.Collection;
31  import java.util.HashSet;
32  
33  import com.healthmarketscience.common.util.Tuple2;
34  import com.healthmarketscience.sqlbuilder.dbspec.Column;
35  import com.healthmarketscience.sqlbuilder.dbspec.Table;
36  import java.util.ArrayList;
37  
38  /**
39   * Object used to accummulate state during query validation.
40   *
41   * @author james
42   */
43  public class ValidationContext {
44  
45    public static final boolean DEFAULT_LOCAL_ONLY = false;
46    
47    private final ValidationContext _parent;
48  
49    private Collection<Column> _columns;
50    private Collection<Table> _tables;
51    /** whether or not collection/validation should proceed into nested
52        subqueries */
53    private boolean _localOnly;
54    private Collection<Tuple2<ValidationContext,Verifiable>> _verifiables;
55  
56    public ValidationContext() {
57      this(null, null, null, DEFAULT_LOCAL_ONLY);
58    }
59  
60    public ValidationContext(ValidationContext parent) {
61      this(parent, null, null, DEFAULT_LOCAL_ONLY);
62    }
63    
64    public ValidationContext(boolean localOnly) {
65      this(null, null, null, localOnly);
66    }
67    
68    public ValidationContext(Collection<Table> tables,
69                             Collection<Column> columns) {
70      this(null, tables, columns, DEFAULT_LOCAL_ONLY);
71    }
72    
73    public ValidationContext(ValidationContext parent,
74                             Collection<Table> tables,
75                             Collection<Column> columns,
76                             boolean localOnly) {
77      _parent = parent;
78      _tables = ((tables != null) ? tables : new HashSet<Table>());
79      _columns = ((columns != null) ? columns : new HashSet<Column>());
80      _localOnly = localOnly;
81      _verifiables = ((_parent != null) ? _parent._verifiables :
82                      new ArrayList<Tuple2<ValidationContext,Verifiable>>(2));
83    }
84  
85    public ValidationContext getParent() {
86      return _parent;
87    }
88  
89    public Collection<Table> getTables() {
90      return _tables;
91    }
92  
93    public void setTables(Collection<Table> newTables) {
94      _tables = newTables;
95    }
96  
97    public void addTable(Table table) {
98      _tables.add(table);
99    }
100   
101   public Collection<Column> getColumns() {
102     return _columns;
103   }
104 
105   public void setColumns(Collection<Column> newColumns) {
106     _columns = newColumns;
107   }
108   
109   public void addColumn(Column column) {
110     _columns.add(column);
111   }
112 
113   public boolean isLocalOnly() {
114     return _localOnly;
115   }
116 
117   public void setLocalOnly(boolean newLocalOnly) {
118     _localOnly = newLocalOnly;
119   }
120 
121   public void addVerifiable(Verifiable verifiable)
122   {
123     if(verifiable == null) {
124       throw new IllegalArgumentException("verifiable was null");
125     }
126     _verifiables.add(Tuple2.create(this, verifiable));
127   }
128 
129   public void validateAll() throws ValidationException {
130     for(Tuple2<ValidationContext,Verifiable> verifiable : _verifiables) {
131       try {
132         verifiable.get1().validate(verifiable.get0());
133       } catch(ValidationException e) {
134         e.setFailedVerifiable(verifiable);
135         throw e;
136       }
137     }
138   }
139   
140   /**
141    * Retrieves the tables referenced by the column objects.
142    *
143    * @return a new columnTables collection
144    */
145   protected Collection<Table> getColumnTables()
146   {
147     return getColumnTables(null);
148   }
149   
150   /**
151    * Retrieves the tables referenced by the column objects.
152    *
153    * @param columnTables (out) all tables referenced by the given columns
154    * @return the given columnTables collection
155    */
156   protected Collection<Table> getColumnTables(Collection<Table> columnTables)
157   {
158     if(columnTables == null) {
159       columnTables = new HashSet<Table>();
160     }
161     // get the tables from the columns referenced
162     for(Column column : _columns) {
163       columnTables.add(column.getTable());
164     }
165     return columnTables;
166   }
167 
168 }