001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.dataset;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import org.dbunit.DatabaseUnitRuntimeException;
028: import org.dbunit.dataset.datatype.DataType;
029:
030: import java.util.Arrays;
031: import java.util.Comparator;
032:
033: /**
034: * This is a ITable decorator that provide a sorted view of the decorated table.
035: * This implementation does not keep a separate copy of the decorated table data.
036: *
037: * @author Manuel Laflamme
038: * @version $Revision: 554 $
039: * @since Feb 19, 2003
040: */
041: public class SortedTable extends AbstractTable {
042:
043: /**
044: * Logger for this class
045: */
046: private static final Logger logger = LoggerFactory
047: .getLogger(SortedTable.class);
048:
049: private final ITable _table;
050: private final Column[] _columns;
051: private Integer[] _indexes;
052:
053: /**
054: * Sort the decorated table by specified columns order.
055: */
056: public SortedTable(ITable table, Column[] columns) {
057: _table = table;
058: _columns = columns;
059: }
060:
061: /**
062: * Sort the decorated table by specified columns order.
063: */
064: public SortedTable(ITable table, String[] columnNames)
065: throws DataSetException {
066: _table = table;
067: _columns = new Column[columnNames.length];
068:
069: Column[] columns = table.getTableMetaData().getColumns();
070: for (int i = 0; i < columnNames.length; i++) {
071: String columnName = columnNames[i];
072: _columns[i] = DataSetUtils.getColumn(columnName, columns);
073: }
074: }
075:
076: /**
077: * Sort the decorated table by specified metadata columns order. All
078: * metadata columns will be used.
079: */
080: public SortedTable(ITable table, ITableMetaData metaData)
081: throws DataSetException {
082: this (table, metaData.getColumns());
083: }
084:
085: /**
086: * Sort the decorated table by its own columns order. All
087: * table columns will be used.
088: */
089: public SortedTable(ITable table) throws DataSetException {
090: this (table, table.getTableMetaData());
091: }
092:
093: private int getOriginalRowIndex(int row) throws DataSetException {
094: logger.debug("getOriginalRowIndex(row=" + row + ") - start");
095:
096: if (_indexes == null) {
097: Integer[] indexes = new Integer[getRowCount()];
098: for (int i = 0; i < indexes.length; i++) {
099: indexes[i] = new Integer(i);
100: }
101:
102: try {
103: Arrays.sort(indexes, new RowComparator());
104: } catch (DatabaseUnitRuntimeException e) {
105: logger.error("getOriginalRowIndex()", e);
106:
107: throw (DataSetException) e.getException();
108: }
109:
110: _indexes = indexes;
111: }
112:
113: return _indexes[row].intValue();
114: }
115:
116: ////////////////////////////////////////////////////////////////////////////
117: // ITable interface
118:
119: public ITableMetaData getTableMetaData() {
120: logger.debug("getTableMetaData() - start");
121:
122: return _table.getTableMetaData();
123: }
124:
125: public int getRowCount() {
126: logger.debug("getRowCount() - start");
127:
128: return _table.getRowCount();
129: }
130:
131: public Object getValue(int row, String column)
132: throws DataSetException {
133: logger.debug("getValue(row=" + row + ", column=" + column
134: + ") - start");
135:
136: assertValidRowIndex(row);
137:
138: return _table.getValue(getOriginalRowIndex(row), column);
139: }
140:
141: ////////////////////////////////////////////////////////////////////////////
142: // Comparator interface
143:
144: private class RowComparator implements Comparator {
145:
146: /**
147: * Logger for this class
148: */
149: private final Logger logger = LoggerFactory
150: .getLogger(RowComparator.class);
151:
152: public int compare(Object o1, Object o2) {
153: logger.debug("compare(o1=" + o1 + ", o2=" + o2
154: + ") - start");
155:
156: Integer i1 = (Integer) o1;
157: Integer i2 = (Integer) o2;
158:
159: try {
160: for (int i = 0; i < _columns.length; i++) {
161: String columnName = _columns[i].getColumnName();
162: Object value1 = _table.getValue(i1.intValue(),
163: columnName);
164: Object value2 = _table.getValue(i2.intValue(),
165: columnName);
166:
167: if (value1 == null && value2 == null) {
168: continue;
169: }
170:
171: if (value1 == null && value2 != null) {
172: return -1;
173: }
174:
175: if (value1 != null && value2 == null) {
176: return 1;
177: }
178:
179: String stringValue1 = DataType.asString(value1);
180: String stringValue2 = DataType.asString(value2);
181: int result = stringValue1.compareTo(stringValue2);
182: if (result != 0) {
183: return result;
184: }
185: }
186: } catch (DataSetException e) {
187: logger.error("compare()", e);
188:
189: throw new DatabaseUnitRuntimeException(e);
190: }
191:
192: return 0;
193: }
194: }
195: }
|