001: /*
002: * $Id: Row.java,v 1.5 2005/05/03 17:22:38 spal Exp $
003: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/beans/Row.java,v $
004: * SQLUnit - a test harness for unit testing database stored procedures.
005: * Copyright (C) 2003 The SQLUnit Team
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program 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
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021: package net.sourceforge.sqlunit.beans;
022:
023: import org.apache.log4j.Logger;
024: import org.jdom.Element;
025:
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: /**
030: * Models a row element, (wrapping multiple Col objects) in a database result.
031: * @author Sujit Pal (spal@users.sourceforge.net)
032: * @version $Revision: 1.5 $
033: */
034: public class Row implements Comparable {
035:
036: private static final Logger LOG = Logger.getLogger(Row.class);
037:
038: private String id;
039: private boolean partial = false;
040: private Col[] cols;
041: private Map colMap;
042: private String[] sortColIds;
043:
044: /**
045: * Returns the Id for this row.
046: * @return the id for this row.
047: */
048: public final String getId() {
049: return id;
050: }
051:
052: /**
053: * Set the id for this row.
054: * @param rowId the id for this row.
055: */
056: public final void setId(final String rowId) {
057: this .id = rowId;
058: }
059:
060: /**
061: * Returns true if the row is partially specified.
062: * @return true if the row is partially specified.
063: */
064: public final boolean isPartial() {
065: return partial;
066: }
067:
068: /**
069: * Sets whether the row is partially specified.
070: * @param partial "true" or "false", default is "false".
071: */
072: public final void setPartial(String partial) {
073: this .partial = (partial.equals("true"));
074: }
075:
076: /**
077: * Return an array of Col objects for this row.
078: * @return an array of Col objects for this row.
079: */
080: public final Col[] getCols() {
081: return cols;
082: }
083:
084: /**
085: * Set the Col object array for this row. Also sets an internal Map
086: * of Col objects.
087: * @param columns an array of Col objects.
088: */
089: public final void setCols(final Col[] columns) {
090: this .cols = columns;
091: this .colMap = new HashMap();
092: for (int i = 0; i < cols.length; i++) {
093: this .colMap.put(cols[i].getId(), cols[i]);
094: }
095: }
096:
097: /**
098: * Convenience method to return a Col object by its id.
099: * @param colId the id of the Col object.
100: * @return a Col object for that id.
101: */
102: public final Col getColById(final String colId) {
103: return (Col) colMap.get(colId);
104: }
105:
106: /**
107: * Sets a string array of ids to sort this row by.
108: * @param colIdsToSort a String array of column ids.
109: */
110: public final void setSortCols(final String[] colIdsToSort) {
111: this .sortColIds = colIdsToSort;
112: }
113:
114: /**
115: * Returns a negative, zero or positive value depending on whether
116: * the contents of this column is less than, equal to or greater
117: * than the contents of the passed in Col object.
118: * @param obj an instance of a Col object.
119: * @return a negative, zero or positive number.
120: */
121: public final int compareTo(final Object obj) {
122: if (!(obj instanceof Row)) {
123: return 0;
124: }
125: Row that = (Row) obj;
126: // the sort columns are set up from the resultset level
127: // so we can use either one, we are going to use this object.
128: int compareResult = 0; // they are equal, for now
129: String[] sortCols = sortColIds;
130: for (int i = 0; i < sortCols.length; i++) {
131: // check if prefixed by a negative sign
132: LOG.debug("cols[" + i + "] = " + sortCols[i]);
133: boolean sortAscending = (!(sortCols[i].startsWith("-")));
134: Col this Col = this .getColById((sortAscending ? sortCols[i]
135: : sortCols[i].substring(1)));
136: Col thatCol = that.getColById((sortAscending ? sortCols[i]
137: : sortCols[i].substring(1)));
138: if (sortAscending) {
139: compareResult = this Col.compareTo(thatCol);
140: } else {
141: compareResult = thatCol.compareTo(this Col);
142: }
143: if (compareResult == 0) {
144: continue; // look at the next column to decide
145: } else {
146: break; // we know which is larger, quit compare
147: }
148: }
149: return compareResult;
150: }
151:
152: /**
153: * Returns a JDOM element that represents this Col object.
154: * @return a JDOM element representing this object.
155: */
156: public final Element toElement() {
157: Element elRow = new Element("row");
158: elRow.setAttribute("id", getId());
159: Col[] theCols = getCols();
160: for (int i = 0; i < theCols.length; i++) {
161: Element elCol = theCols[i].toElement();
162: elRow.addContent(elCol);
163: }
164: return elRow;
165: }
166:
167: /**
168: * Returns the string representation of the Row bean.
169: * @return the String representation of this bean.
170: */
171: public final String toString() {
172: StringBuffer buffer = new StringBuffer();
173: Col[] theCols = getCols();
174: for (int i = 0; i < theCols.length; i++) {
175: if (i > 0) {
176: buffer.append(",");
177: }
178: buffer.append(theCols[i].getValue());
179: }
180: return buffer.toString();
181: }
182: }
|