001: package net.sourceforge.squirrel_sql.client.session.properties;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: /**
022: * @author gwg
023: * Object to save, manage and restore lists of specific columns
024: * to use in the WHERE clause when doing editing of cells in tables as
025: * in the ContentsTab.
026: */
027:
028: import java.util.HashMap;
029: import java.util.ArrayList;
030: import java.util.Iterator;
031:
032: import net.sourceforge.squirrel_sql.client.IApplication;
033: import net.sourceforge.squirrel_sql.client.preferences.PreferenceType;
034:
035: public class EditWhereCols {
036:
037: /**
038: * The version of the data suitible for loading/unloading from/to files.
039: * The internal representation is a HashMap containing HashMaps containing strings.
040: * The XMLReader/Writer Beans in fw.xml do not handle the general case of HashMaps,
041: * so rather than trying to handle that there, we just convert the data internally into a form
042: * that those classes can handle, i.e. an array of strings.
043: * Each string consists of the name of the table, a space, then a comma-separated list of
044: * all of the columns that SHOULD be used in the WHERE clause when doing editing
045: * operations.
046: * This is used in an instance of this class created during load/unload.
047: */
048: private String[] dataArray = new String[0];
049:
050: /**
051: * The mapping from table name to object (also a HashMap)
052: * containing the list of names of columns to use in WHERE clause.
053: * There is only one copy of this table for all instances of this class.
054: */
055: private static HashMap<String, HashMap<String, String>> _tables = new HashMap<String, HashMap<String, String>>();
056:
057: /**
058: * Is used to persist the _tables map each time it changes is the user
059: * so wishes
060: */
061: private IApplication _app = null;
062:
063: /**
064: * ctor
065: */
066: public EditWhereCols() {
067: }
068:
069: /**
070: * get data in form that can be used to output to file.
071: * This is called from an instance of this class.
072: */
073: public String[] getDataArray() {
074: // first convert internal data into the string array
075: dataArray = new String[_tables.size()];
076: Iterator<String> keys = _tables.keySet().iterator();
077: int index = 0;
078:
079: // get each table's info
080: while (keys.hasNext()) {
081: String tableName = keys.next();
082: HashMap<String, String> h = _tables.get(tableName);
083: Iterator<String> columnNames = h.keySet().iterator();
084: String outData = tableName + " ";
085: while (columnNames.hasNext()) {
086: String colName = columnNames.next();
087: outData += colName;
088: if (columnNames.hasNext())
089: outData += ",";
090: }
091:
092: // put this into the data array
093: dataArray[index++] = outData;
094: }
095:
096: return dataArray;
097: }
098:
099: /**
100: * Data in the external form (array of strings) is passed in and must be converted
101: * to the internal form.
102: * This is called on an instance of this class.
103: * @param inData array of strings in form "tableName col,col,col..."
104: */
105: public void setDataArray(String[] inData) {
106: // make sure we are starting clean
107: _tables = new HashMap<String, HashMap<String, String>>();
108:
109: // convert each string into key+HashMap and fill it into the data
110: for (int i = 0; i < inData.length; i++) {
111: int endIndex = inData[i].indexOf(" ");
112: String tableName = inData[i].substring(0, endIndex);
113:
114: int startIndex;
115: ArrayList<String> colList = new ArrayList<String>();
116: while (true) {
117: startIndex = endIndex + 1;
118: endIndex = inData[i].indexOf(',', startIndex);
119: if (endIndex == -1) {
120: // we are at the last one in the list
121: colList.add(inData[i].substring(startIndex));
122: break;
123: }
124: colList.add(inData[i].substring(startIndex, endIndex));
125: }
126:
127: // create a hashmap containing the column names.
128: // by convention, the value and key is the same for each column name
129: HashMap<String, String> h = new HashMap<String, String>(
130: colList.size());
131: for (int j = 0; j < colList.size(); j++)
132: h.put(colList.get(j), colList.get(j));
133:
134: // put the map into the tables db with the table name as the key
135: _tables.put(tableName, h);
136: }
137: }
138:
139: /**
140: * add or replace a table-name/hashmap-of-column-names mapping.
141: * If map is null, remove the entry from the tables.
142: */
143: public void put(String tableName, HashMap<String, String> colNames) {
144: if (_app == null) {
145: throw new IllegalStateException(
146: "application has not been set");
147: }
148: if (colNames == null) {
149: _tables.remove(tableName);
150: } else {
151: _tables.put(tableName, colNames);
152: }
153: _app.savePreferences(PreferenceType.EDITWHERECOL_PREFERENCES);
154: return;
155: }
156:
157: /**
158: * get the HashMap of column names for the given table name.
159: * it will be null if the table does not have any limitation on the columns to use.
160: */
161: public static HashMap<String, String> get(String tableName) {
162: return _tables.get(tableName);
163: }
164:
165: /**
166: * Used to inject the IApplication which is used to persist this data.
167: *
168: * @param application the IApplication to set
169: */
170: public void setApplication(IApplication application) {
171: if (application == null) {
172: throw new IllegalArgumentException(
173: "application cannot be null");
174: }
175: this._app = application;
176: }
177:
178: }
|