001: package org.apache.torque.engine.database.model;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.ArrayList;
023: import java.util.Collections;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: import org.apache.commons.collections.map.ListOrderedMap;
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: import org.apache.torque.engine.EngineException;
033:
034: import org.xml.sax.Attributes;
035:
036: /**
037: * Information about indices of a table.
038: *
039: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
040: * @author <a href="mailto:dlr@finemaltcoding.com>Daniel Rall</a>
041: * @author <a href="mailto:monroe@dukece.com>Greg Monroe</a>
042: * @version $Id: Index.java 473814 2006-11-11 22:30:30Z tv $
043: */
044: public class Index {
045: /** Logging class from commons.logging */
046: private static Log log = LogFactory.getLog(Index.class);
047: /** name of the index */
048: private String indexName;
049: /** table */
050: private Table parentTable;
051: /** columns */
052: private List indexColumns;
053: /** The XML Options specified for this index */
054: private Map options;
055:
056: /**
057: * Creates a new instance with default characteristics (no name or
058: * parent table, small column list size allocation, non-unique).
059: */
060: public Index() {
061: indexColumns = new ArrayList(3);
062: options = Collections.synchronizedMap(new ListOrderedMap());
063: }
064:
065: /**
066: * Creates a new instance for the list of columns composing an
067: * index. Otherwise performs as {@link #Index()}.
068: *
069: * @param table The table this index is associated with.
070: * @param indexColumns The list of {@link
071: * org.apache.torque.engine.database.model.Column} objects which
072: * make up this index. Cannot be empty.
073: * @exception EngineException Error generating name.
074: * @see #Index()
075: */
076: protected Index(Table table, List indexColumns)
077: throws EngineException {
078: this ();
079: setTable(table);
080: if (!indexColumns.isEmpty()) {
081: this .indexColumns = indexColumns;
082:
083: if (log.isDebugEnabled()) {
084: log.debug("Created Index named " + getName() + " with "
085: + indexColumns.size() + " columns");
086: }
087: } else {
088: throw new EngineException(
089: "Cannot create a new Index using an "
090: + "empty list Column object");
091: }
092: }
093:
094: /**
095: * Imports index from an XML specification
096: *
097: * @param attrib the xml attributes
098: */
099: public void loadFromXML(Attributes attrib) {
100: indexName = attrib.getValue("name");
101: }
102:
103: /**
104: * Returns the uniqueness of this index.
105: *
106: * @return the uniqueness of this index
107: */
108: public boolean isUnique() {
109: return false;
110: }
111:
112: /**
113: * Gets the name of this index.
114: *
115: * @return the name of this index
116: */
117: public String getName() {
118: return indexName;
119: }
120:
121: /**
122: * Set the name of this index.
123: *
124: * @param name the name of this index
125: */
126: public void setName(String name) {
127: this .indexName = name;
128: }
129:
130: /**
131: * Set the parent Table of the index
132: *
133: * @param parent the table
134: */
135: public void setTable(Table parent) {
136: parentTable = parent;
137: }
138:
139: /**
140: * Get the parent Table of the index
141: *
142: * @return the table
143: */
144: public Table getTable() {
145: return parentTable;
146: }
147:
148: /**
149: * Returns the Name of the table the index is in
150: *
151: * @return the name of the table
152: */
153: public String getTableName() {
154: return parentTable.getName();
155: }
156:
157: /**
158: * Adds a new column to an index.
159: *
160: * @param attrib xml attributes for the column
161: */
162: public void addColumn(Attributes attrib) {
163: indexColumns.add(attrib.getValue("name"));
164: }
165:
166: /**
167: * Return a comma delimited string of the columns which compose this index.
168: *
169: * @return a list of column names
170: */
171: public String getColumnList() {
172: return Column.makeList(getColumns());
173: }
174:
175: /**
176: * Return the list of local columns. You should not edit this list.
177: *
178: * @return a list of columns
179: */
180: public List getColumns() {
181: return indexColumns;
182: }
183:
184: /**
185: * Returns the list of names of the columns referenced by this
186: * index. Slightly over-allocates the list's buffer (just in case
187: * more elements are going to be added, such as when a name is
188: * being generated). Feel free to modify this list.
189: *
190: * @return a list of column names
191: */
192: protected List getColumnNames() {
193: List names = new ArrayList(indexColumns.size() + 2);
194: Iterator i = getColumns().iterator();
195: while (i.hasNext()) {
196: Column c = (Column) i.next();
197: names.add(c.getName());
198: }
199: return names;
200: }
201:
202: /**
203: * String representation of the index. This is an xml representation.
204: *
205: * @return a xml representation
206: */
207: public String toString() {
208: StringBuffer result = new StringBuffer();
209: result.append(" <index name=\"").append(getName()).append("\"");
210:
211: result.append(">\n");
212:
213: for (int i = 0; i < indexColumns.size(); i++) {
214: result.append(" <index-column name=\"").append(
215: indexColumns.get(i)).append("\"/>\n");
216: }
217: result.append(" </index>\n");
218: return result.toString();
219: }
220:
221: /**
222: * Add an XML Specified option key/value pair to this element's option set.
223: *
224: * @param key the key of the option.
225: * @param value the value of the option.
226: */
227: public void addOption(String key, String value) {
228: options.put(key, value);
229: }
230:
231: /**
232: * Get the value that was associated with this key in an XML option
233: * element.
234: *
235: * @param key the key of the option.
236: * @return The value for the key or a null.
237: */
238: public String getOption(String key) {
239: return (String) options.get(key);
240: }
241:
242: /**
243: * Gets the full ordered hashtable array of items specified by XML option
244: * statements under this element.<p>
245: *
246: * Note, this is not thread save but since it's only used for
247: * generation which is single threaded, there should be minimum
248: * danger using this in Velocity.
249: *
250: * @return An Map of all options. Will not be null but may be empty.
251: */
252: public Map getOptions() {
253: return options;
254: }
255: }
|