001: package net.sourceforge.squirrel_sql.fw.sql;
002:
003: /*
004: * Copyright (C) 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: import java.io.Serializable;
022: import java.sql.DriverPropertyInfo;
023: import java.util.ArrayList;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Properties;
027: import java.util.TreeMap;
028:
029: /**
030: * A collection of <TT>SQLDriverDriverProperty</TT> objects.
031: *
032: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
033: */
034: public class SQLDriverPropertyCollection implements Serializable {
035: private static final long serialVersionUID = 1L;
036:
037: /**
038: * JavaBean property names for this class.
039: */
040: public interface IPropertyNames {
041: String DRIVER_PROPERTIES = "driverProperties";
042: }
043:
044: /** Collection of <TT></TT> objects keyed by the object name. */
045: private final Map<String, SQLDriverProperty> _objectsIndexMap = new TreeMap<String, SQLDriverProperty>();
046:
047: /** Array of <TT>SQLDriverProperty</TT> objects. */
048: private final List<SQLDriverProperty> _objectsList = new ArrayList<SQLDriverProperty>();
049:
050: /**
051: * Default ctor. Creates an empty collection.
052: */
053: public SQLDriverPropertyCollection() {
054: super ();
055: }
056:
057: /**
058: * Clear all entries from this collection.
059: */
060: public synchronized void clear() {
061: _objectsIndexMap.clear();
062: _objectsList.clear();
063: }
064:
065: /**
066: * Retrieve the number of elements in this collection.
067: *
068: * @return the number of elements in this collection.
069: */
070: public int size() {
071: return _objectsList.size();
072: }
073:
074: public synchronized void applyTo(Properties props) {
075: for (int i = 0, limit = size(); i < limit; ++i) {
076: SQLDriverProperty sdp = getDriverProperty(i);
077: if (sdp.isSpecified()) {
078: final String value = sdp.getValue();
079: if (value != null) {
080: props.put(sdp.getName(), value);
081: }
082: }
083: }
084: }
085:
086: /**
087: * Retrieve an array of the <TT>SQLDriverProperty</TT> objects contained
088: * in this collection.
089: *
090: * @return an array of the <TT>SQLDriverProperty</TT> objects contained
091: * in this collection.
092: */
093: public synchronized SQLDriverProperty[] getDriverProperties() {
094: SQLDriverProperty[] ar = new SQLDriverProperty[_objectsList
095: .size()];
096: return _objectsList.toArray(ar);
097: }
098:
099: public synchronized SQLDriverProperty getDriverProperty(int idx) {
100: return _objectsList.get(idx);
101: }
102:
103: public synchronized void setDriverProperties(
104: SQLDriverProperty[] values) {
105: _objectsIndexMap.clear();
106: _objectsList.clear();
107: for (int i = 0; i < values.length; ++i) {
108: _objectsList.add(values[i]);
109: _objectsIndexMap.put(values[i].getName(), values[i]);
110: }
111: }
112:
113: /**
114: * Warning - should only be used when loading javabean from XML.
115: */
116: public synchronized void setDriverProperty(int idx,
117: SQLDriverProperty value) {
118: _objectsList.add(idx, value);
119: _objectsIndexMap.put(value.getName(), value);
120: }
121:
122: public synchronized void applyDriverPropertynfo(
123: DriverPropertyInfo[] infoAr) {
124: if (infoAr == null || infoAr.length == 0) {
125: infoAr = new DriverPropertyInfo[1];
126: infoAr[0] = new DriverPropertyInfo("remarksReporting",
127: "true");
128: infoAr[0].required = false;
129: infoAr[0].description = "Set to true in order to table/column comments";
130: }
131: for (int i = 0; i < infoAr.length; ++i) {
132: SQLDriverProperty sdp = _objectsIndexMap
133: .get(infoAr[i].name);
134: if (sdp == null) {
135: sdp = new SQLDriverProperty(infoAr[i]);
136: _objectsIndexMap.put(sdp.getName(), sdp);
137: _objectsList.add(sdp);
138: }
139: sdp.setDriverPropertyInfo(infoAr[i]);
140: }
141: }
142: }
|