1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
40
41
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
52
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
142
143
144
145 protected Collection<Table> getColumnTables()
146 {
147 return getColumnTables(null);
148 }
149
150
151
152
153
154
155
156 protected Collection<Table> getColumnTables(Collection<Table> columnTables)
157 {
158 if(columnTables == null) {
159 columnTables = new HashSet<Table>();
160 }
161
162 for(Column column : _columns) {
163 columnTables.add(column.getTable());
164 }
165 return columnTables;
166 }
167
168 }