001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.shared.search;
034:
035: import com.flexive.shared.content.FxPK;
036: import com.flexive.shared.exceptions.FxInvalidParameterException;
037: import com.flexive.shared.exceptions.FxNotFoundException;
038: import com.flexive.shared.value.*;
039:
040: import java.util.Arrays;
041: import java.util.List;
042: import java.util.Date;
043:
044: /**
045: * Provides a thin wrapper for a result row of a SQL search
046: * result set.
047: *
048: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
049: * @version $Rev: 181 $
050: */
051: public class FxResultRow {
052: private final FxResultSet resultSet;
053: private final int index;
054:
055: public FxResultRow(FxResultSet resultSet, int index) {
056: if (index < 0 || index > resultSet.getRows().size()) {
057: throw new FxInvalidParameterException("INDEX",
058: "ex.sqlSearch.resultRow.index", index, resultSet
059: .getRows().size()).asRuntimeException();
060: }
061: this .resultSet = resultSet;
062: this .index = index;
063: }
064:
065: public Object[] getData() {
066: return resultSet.getRows().get(index);
067: }
068:
069: public String[] getColumnNames() {
070: return resultSet.getColumnNames();
071: }
072:
073: public int getColumnIndex(String columnName) {
074: return resultSet.getColumnIndex(columnName);
075: }
076:
077: public Object getValue(int column) {
078: return getData()[column - 1];
079: }
080:
081: public FxPK getPk(int column) {
082: return (FxPK) getValue(column);
083: }
084:
085: public FxValue getFxValue(int column) {
086: return (FxValue) getValue(column);
087: }
088:
089: public List<FxPaths.Path> getPaths(int column) {
090: return ((FxPaths) getValue(column)).getPaths();
091: }
092:
093: public List<FxPaths.Path> getPaths(String columnName) {
094: return getPaths(getColumnIndex(columnName));
095: }
096:
097: public Object getValue(String columnName) {
098: final int columnIndex = getColumnIndex(columnName);
099: if (columnIndex == -1) {
100: throw new FxNotFoundException(
101: "ex.sqlSearch.resultRow.column.notfound",
102: columnName, Arrays.asList(resultSet
103: .getColumnNames())).asRuntimeException();
104: }
105: return getData()[columnIndex - 1];
106: }
107:
108: public FxPK getPk(String columnName) {
109: return (FxPK) getValue(columnName);
110: }
111:
112: public FxValue getFxValue(String columnName) {
113: return (FxValue) getValue(columnName);
114: }
115:
116: public long getLong(int column) {
117: final Object value = getValue(column);
118: if (value instanceof Number) {
119: return ((Number) value).longValue();
120: } else if (value instanceof FxNumber) {
121: return ((FxNumber) value).getBestTranslation();
122: } else if (value instanceof FxLargeNumber) {
123: return ((FxLargeNumber) value).getBestTranslation();
124: }
125: throw new FxInvalidParameterException("column",
126: "ex.sqlSearch.resultRow.invalidType", column,
127: getColumnNames()[column - 1], "Long", value.getClass()
128: .getName()).asRuntimeException();
129: }
130:
131: public long getLong(String columnName) {
132: return getLong(getColumnIndex(columnName));
133: }
134:
135: public int getInt(int column) {
136: final Object value = getValue(column);
137: if (value instanceof Number) {
138: return ((Number) value).intValue();
139: } else if (value instanceof FxNumber) {
140: return ((FxNumber) value).getBestTranslation();
141: }
142: throw new FxInvalidParameterException("column",
143: "ex.sqlSearch.resultRow.invalidType", column,
144: getColumnNames()[column - 1], "Integer", value
145: .getClass().getName()).asRuntimeException();
146: }
147:
148: public int getInt(String columnName) {
149: return getInt(getColumnIndex(columnName));
150: }
151:
152: public String getString(int column) {
153: final Object value = getValue(column);
154: if (value instanceof FxString) {
155: return ((FxString) value).getBestTranslation();
156: } else if (value instanceof String) {
157: return (String) value;
158: } else {
159: return value != null ? value.toString() : null;
160: }
161: }
162:
163: public String getString(String columnName) {
164: return getString(getColumnIndex(columnName));
165: }
166:
167: public Date getDate(int column) {
168: final Object value = getValue(column);
169: if (value instanceof FxDate) {
170: return ((FxDate) value).getBestTranslation();
171: } else if (value instanceof FxDateTime) {
172: return ((FxDateTime) value).getBestTranslation();
173: } else if (value instanceof Date) {
174: return (Date) value;
175: }
176: throw new FxInvalidParameterException("column",
177: "ex.sqlSearch.resultRow.invalidType", column,
178: getColumnNames()[column - 1], "Date", value.getClass()
179: .getName()).asRuntimeException();
180: }
181:
182: public Date getDate(String columnName) {
183: return getDate(getColumnIndex(columnName));
184: }
185: }
|