001: package net.sourceforge.squirrel_sql.client.gui.db;
002:
003: import java.io.Serializable;
004: import java.util.ArrayList;
005: import java.util.Arrays;
006:
007: public class SQLAliasSchemaProperties implements Serializable {
008: private static final long serialVersionUID = 1L;
009:
010: SQLAliasSchemaDetailProperties[] _schemaDetails = new SQLAliasSchemaDetailProperties[0];
011:
012: public static final int GLOBAL_STATE_LOAD_ALL_CACHE_NONE = 0;
013: public static final int GLOBAL_STATE_LOAD_AND_CACHE_ALL = 1;
014: public static final int GLOBAL_STATE_SPECIFY_SCHEMAS = 2;
015:
016: private int _globalState = GLOBAL_STATE_LOAD_ALL_CACHE_NONE;
017: private boolean _cacheSchemaIndependentMetaData;
018:
019: public SQLAliasSchemaDetailProperties[] getSchemaDetails() {
020: return _schemaDetails;
021: }
022:
023: public void setSchemaDetails(
024: SQLAliasSchemaDetailProperties[] schemaDetails) {
025: _schemaDetails = schemaDetails;
026: }
027:
028: public int getGlobalState() {
029: return _globalState;
030: }
031:
032: public void setGlobalState(int globalState) {
033: this ._globalState = globalState;
034: }
035:
036: public boolean isCacheSchemaIndependentMetaData() {
037: return _cacheSchemaIndependentMetaData;
038: }
039:
040: public void setCacheSchemaIndependentMetaData(boolean b) {
041: _cacheSchemaIndependentMetaData = b;
042: }
043:
044: public boolean loadSchemaIndependentMetaData(
045: SQLAliasSchemaProperties schemaPropsCacheIsBasedOn) {
046: if (null == schemaPropsCacheIsBasedOn) {
047: return true;
048: }
049:
050: return !(schemaPropsCacheIsBasedOn._cacheSchemaIndependentMetaData && _cacheSchemaIndependentMetaData);
051:
052: }
053:
054: /**
055: * @param schemaPropsCacheIsBasedOn null means that cache is not considered
056: */
057: public SchemaLoadInfo[] getSchemaLoadInfos(
058: SQLAliasSchemaProperties schemaPropsCacheIsBasedOn,
059: String[] tableTypes, String[] viewTypes) {
060: if (null == schemaPropsCacheIsBasedOn) {
061: return getSchemasToLoadDefault(tableTypes, viewTypes);
062: }
063:
064: if (GLOBAL_STATE_LOAD_AND_CACHE_ALL == _globalState
065: && GLOBAL_STATE_LOAD_AND_CACHE_ALL == schemaPropsCacheIsBasedOn._globalState) {
066: // See also loadSchemaNames()
067: return new SchemaLoadInfo[0];
068: }
069:
070: if (GLOBAL_STATE_SPECIFY_SCHEMAS == _globalState
071: && GLOBAL_STATE_SPECIFY_SCHEMAS == schemaPropsCacheIsBasedOn._globalState) {
072: ArrayList<SchemaLoadInfo> ret = new ArrayList<SchemaLoadInfo>();
073:
074: for (int i = 0; i < _schemaDetails.length; i++) {
075: SQLAliasSchemaDetailProperties cachedDetailProp = getMatchingDetail(
076: _schemaDetails[i].getSchemaName(),
077: schemaPropsCacheIsBasedOn._schemaDetails);
078:
079: SchemaLoadInfo buf = new SchemaLoadInfo(
080: addStringArrays(tableTypes, viewTypes));
081: buf.schemaName = _schemaDetails[i].getSchemaName();
082:
083: ArrayList<String> tableTypesToLoad = new ArrayList<String>();
084:
085: if (needsLoading(_schemaDetails[i].getTable(),
086: null == cachedDetailProp ? null
087: : cachedDetailProp.getTable())) {
088: tableTypesToLoad.addAll(Arrays.asList(tableTypes));
089: }
090:
091: if (needsLoading(_schemaDetails[i].getView(),
092: null == cachedDetailProp ? null
093: : cachedDetailProp.getView())) {
094: tableTypesToLoad.addAll(Arrays.asList(viewTypes));
095: }
096:
097: buf.loadProcedures = needsLoading(_schemaDetails[i]
098: .getProcedure(),
099: null == cachedDetailProp ? null
100: : cachedDetailProp.getProcedure());
101:
102: if (0 < tableTypesToLoad.size() || buf.loadProcedures) {
103: buf.tableTypes = tableTypesToLoad
104: .toArray(new String[tableTypesToLoad.size()]);
105: ret.add(buf);
106: }
107: }
108:
109: return ret.toArray(new SchemaLoadInfo[ret.size()]);
110: }
111:
112: return getSchemasToLoadDefault(tableTypes, viewTypes);
113: }
114:
115: /**
116: * Returns SchemaLoadInfos as if there was no cache.
117: */
118: private SchemaLoadInfo[] getSchemasToLoadDefault(
119: String[] tableTypes, String[] viewTypes) {
120: if (GLOBAL_STATE_LOAD_ALL_CACHE_NONE == _globalState
121: || GLOBAL_STATE_LOAD_AND_CACHE_ALL == _globalState) {
122: // Means load all Schemas from database.
123: return new SchemaLoadInfo[] { new SchemaLoadInfo(
124: addStringArrays(tableTypes, viewTypes)) };
125:
126: } else if (SQLAliasSchemaProperties.GLOBAL_STATE_SPECIFY_SCHEMAS == _globalState) {
127: ArrayList<SchemaLoadInfo> schemaLoadInfos = new ArrayList<SchemaLoadInfo>();
128:
129: for (int i = 0; i < _schemaDetails.length; i++) {
130: if (SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_DONT_LOAD == _schemaDetails[i]
131: .getTable()
132: && SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_DONT_LOAD == _schemaDetails[i]
133: .getView()
134: && SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_DONT_LOAD == _schemaDetails[i]
135: .getProcedure()) {
136: continue;
137: }
138:
139: SchemaLoadInfo schemaLoadInfo = new SchemaLoadInfo(
140: addStringArrays(tableTypes, viewTypes));
141: schemaLoadInfo.schemaName = _schemaDetails[i]
142: .getSchemaName();
143: schemaLoadInfo.tableTypes = new String[0];
144:
145: if (SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_DONT_LOAD != _schemaDetails[i]
146: .getTable()) {
147: schemaLoadInfo.tableTypes = addStringArrays(
148: schemaLoadInfo.tableTypes, tableTypes);
149: }
150:
151: if (SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_DONT_LOAD != _schemaDetails[i]
152: .getView()) {
153: schemaLoadInfo.tableTypes = addStringArrays(
154: schemaLoadInfo.tableTypes, viewTypes);
155: }
156:
157: if (SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_DONT_LOAD != _schemaDetails[i]
158: .getProcedure()) {
159: schemaLoadInfo.loadProcedures = true;
160: } else {
161: schemaLoadInfo.loadProcedures = false;
162:
163: }
164:
165: schemaLoadInfos.add(schemaLoadInfo);
166: }
167:
168: return schemaLoadInfos
169: .toArray(new SchemaLoadInfo[schemaLoadInfos.size()]);
170: } else {
171: throw new IllegalStateException("Undefined global state "
172: + _globalState);
173: }
174: }
175:
176: private String[] addStringArrays(String[] tableTypes,
177: String[] viewTypes) {
178: ArrayList<String> ret = new ArrayList<String>();
179: ret.addAll(Arrays.asList(tableTypes));
180: ret.addAll(Arrays.asList(viewTypes));
181:
182: return ret.toArray(new String[ret.size()]);
183: }
184:
185: private boolean needsLoading(int loadingID, Integer cachedLoadingID) {
186: if (SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_DONT_LOAD == loadingID) {
187: // current Schema says don't load
188: return false;
189: } else if (SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_LOAD_AND_CACHE == loadingID
190: && null != cachedLoadingID
191: && SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_LOAD_AND_CACHE == cachedLoadingID
192: .intValue()) {
193: return false;
194: }
195:
196: return true;
197: }
198:
199: private SQLAliasSchemaDetailProperties getMatchingDetail(
200: String schemaName,
201: SQLAliasSchemaDetailProperties[] schemaDetails) {
202: for (int i = 0; i < schemaDetails.length; i++) {
203: if (schemaDetails[i].getSchemaName().equals(schemaName)) {
204: return schemaDetails[i];
205: }
206: }
207:
208: return null;
209:
210: }
211:
212: public SchemaTableTypeCombination[] getAllSchemaTableTypeCombinationsNotToBeCached(
213: String[] tableTypes, String[] viewTypes) {
214: ArrayList<SchemaTableTypeCombination> ret = new ArrayList<SchemaTableTypeCombination>();
215:
216: for (int i = 0; i < _schemaDetails.length; i++) {
217: if (SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_LOAD_AND_CACHE != _schemaDetails[i]
218: .getTable()) {
219: SchemaTableTypeCombination buf = new SchemaTableTypeCombination();
220: buf.schemaName = _schemaDetails[i].getSchemaName();
221: buf.types = tableTypes;
222: ret.add(buf);
223: }
224:
225: if (SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_LOAD_AND_CACHE != _schemaDetails[i]
226: .getView()) {
227: SchemaTableTypeCombination buf = new SchemaTableTypeCombination();
228: buf.schemaName = _schemaDetails[i].getSchemaName();
229: buf.types = viewTypes;
230: ret.add(buf);
231: }
232: }
233:
234: return ret.toArray(new SchemaTableTypeCombination[ret.size()]);
235: }
236:
237: public String[] getAllSchemaProceduresNotToBeCached() {
238: ArrayList<String> ret = new ArrayList<String>();
239:
240: for (int i = 0; i < _schemaDetails.length; i++) {
241: if (SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_LOAD_AND_CACHE != _schemaDetails[i]
242: .getProcedure()) {
243: ret.add(_schemaDetails[i].getSchemaName());
244: }
245:
246: }
247:
248: return ret.toArray(new String[ret.size()]);
249: }
250:
251: public boolean getExpectsSomeCachedData() {
252: if (_cacheSchemaIndependentMetaData
253: || GLOBAL_STATE_LOAD_AND_CACHE_ALL == _globalState) {
254: return true;
255: }
256:
257: if (GLOBAL_STATE_SPECIFY_SCHEMAS == _globalState) {
258: // Note: If we are here _cacheSchemaIndependentMetaData must be false
259:
260: for (int i = 0; i < _schemaDetails.length; i++) {
261: if (SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_LOAD_AND_CACHE == _schemaDetails[i]
262: .getTable()
263: || SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_LOAD_AND_CACHE == _schemaDetails[i]
264: .getView()
265: || SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_LOAD_AND_CACHE == _schemaDetails[i]
266: .getProcedure()) {
267: return true;
268: }
269: }
270: }
271:
272: return false;
273: }
274:
275: public boolean loadSchemaNames(
276: SQLAliasSchemaProperties schemaPropsCacheIsBasedOn) {
277: if (GLOBAL_STATE_LOAD_AND_CACHE_ALL == _globalState
278: && GLOBAL_STATE_LOAD_AND_CACHE_ALL == schemaPropsCacheIsBasedOn._globalState) {
279: return true;
280: } else {
281: return false;
282: }
283: }
284:
285: public SchemaNameLoadInfo getSchemaNameLoadInfo(
286: SQLAliasSchemaProperties schemaPropsCacheIsBasedOn) {
287: SchemaNameLoadInfo ret = new SchemaNameLoadInfo();
288:
289: if (GLOBAL_STATE_LOAD_AND_CACHE_ALL == _globalState
290: && null != schemaPropsCacheIsBasedOn
291: && GLOBAL_STATE_LOAD_AND_CACHE_ALL == schemaPropsCacheIsBasedOn._globalState) {
292: ret.state = SchemaNameLoadInfo.STATE_DONT_REFERESH_SCHEMA_NAMES;
293: } else if (GLOBAL_STATE_SPECIFY_SCHEMAS == _globalState) {
294: ArrayList<String> schemaNames = new ArrayList<String>();
295:
296: ret.state = SchemaNameLoadInfo.STATE_USES_PROVIDED_SCHEMA_NAMES;
297:
298: for (int i = 0; i < _schemaDetails.length; i++) {
299: if (SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_DONT_LOAD == _schemaDetails[i]
300: .getTable()
301: && SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_DONT_LOAD == _schemaDetails[i]
302: .getView()
303: && SQLAliasSchemaDetailProperties.SCHEMA_LOADING_ID_DONT_LOAD == _schemaDetails[i]
304: .getProcedure()) {
305: continue;
306: }
307:
308: schemaNames.add(_schemaDetails[i].getSchemaName());
309: }
310:
311: ret.schemaNames = schemaNames
312: .toArray(new String[schemaNames.size()]);
313: } else {
314: ret.state = SchemaNameLoadInfo.STATE_REFERESH_SCHEMA_NAMES_FROM_DB;
315: }
316:
317: return ret;
318: }
319: }
|