001: package net.xoetrope.optional.data.sql;
002:
003: import net.xoetrope.xui.data.XModel;
004:
005: /**
006: * <p>Provides a model for table rows. The attributes are equivalent to the row
007: * fields. The row will have no children. As a Java object the fields and rows
008: * indices are zero based in contrast to the JDBC indexing setup.</p>
009: * <p>Copyright (c) Xoetrope Ltd. 2001-2003</p>
010: * $Revision: 1.1 $
011: */
012: public class DatabaseRowModel extends XModel {
013: protected int rowIdx;
014: protected DatabaseTableModel xtable;
015:
016: /**
017: * Default constructor, no table or row is referenced
018: */
019: public DatabaseRowModel() {
020: }
021:
022: /**
023: * Construct a model node for a table
024: * @param table the table to reference
025: * @param row the row to reference
026: */
027: public DatabaseRowModel(DatabaseTableModel table, int row) {
028: xtable = table;
029: rowIdx = row;
030: }
031:
032: /**
033: * Get the referenced table
034: * @return
035: */
036: public DatabaseTableModel getTable() {
037: return xtable;
038: }
039:
040: /**
041: * Set the row to reference
042: * @param table the table to reference
043: * @param row the row to reference
044: */
045: public void setRowReference(DatabaseTableModel table, int row) {
046: xtable = table;
047: rowIdx = row;
048: }
049:
050: /**
051: * Get a field as a double value
052: * @param elementName the field name
053: * @return
054: */
055: public double getValueAsDouble(String elementName) {
056: /**This method is inappropriate for this node type*/
057: throw new java.lang.UnsupportedOperationException(
058: "Method getValueAsDouble() not yet implemented.");
059: }
060:
061: /**
062: * Get a field value
063: * @param attribName the field name
064: * @return
065: */
066: public Object get(String attribName) {
067: return new DatabaseFieldModel(xtable, rowIdx, xtable
068: .getAttribute(attribName));
069: }
070:
071: /**
072: * Gets the model element tag name, e.g. 'Component' from the XML fragment
073: * <Component ....
074: * @return the model element name
075: */
076: public String getTagName() {
077: return "";
078: }
079:
080: /**
081: * Append a node
082: * @param childNode the child node
083: */
084: public void append(XModel newObject) {
085: /**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
086: }
087:
088: /**
089: * Get a field value as an int
090: * @param attribName the fieldName
091: * @return the value
092: */
093: public int getInt(String attribName) {
094: return new Integer(xtable.getFieldValue(rowIdx, xtable
095: .getAttribute(attribName))).intValue();
096: }
097:
098: /**
099: * Get a field value as an double
100: * @param attribName the fieldName
101: * @return the value
102: */
103: public double getDouble(String attribName) {
104: return new Double(xtable.getFieldValue(rowIdx, xtable
105: .getAttribute(attribName))).doubleValue();
106: }
107:
108: /**
109: * Get a field value as an String
110: * @param attribName the fieldName
111: * @return the value
112: */
113: public String getString(String attribName) {
114: return xtable.getFieldValue(rowIdx, xtable
115: .getAttribute(attribName));
116: }
117:
118: /**
119: * Get the value of the first field in this row
120: * @return
121: */
122: public String getValue() {
123: return xtable.getFieldValue(rowIdx, 0);
124: }
125:
126: /**
127: * Gets an individual field value
128: * @param fieldIdx the field index
129: * @return the value
130: */
131: public String getFieldValue(int fieldIdx) {
132: return xtable.getFieldValue(rowIdx, fieldIdx);
133: }
134:
135: /**
136: * Get the value of an attribute
137: * @param i
138: * @return
139: */
140: public Object getAttribValue(int i) {
141: return xtable.getAttribValue(i);
142: }
143:
144: public String getAttribValueAsString(int i) {
145: return xtable.getAttribValueAsString(i);
146: }
147:
148: /**
149: * Gets the value attribute of the specified node as a string.
150: * @param elementName
151: * @return the value as a string
152: */
153: public String getValueAsString(String elementName) {
154: return xtable.getAttribValueAsString(xtable
155: .getAttribute(elementName));
156: }
157:
158: /**
159: * Get the ID of this row
160: * @return
161: */
162: public String getId() {
163: return new Integer(rowIdx).toString();
164: }
165:
166: public String getAttribName(int i) {
167: return xtable.getAttribName(i);
168: }
169:
170: public int getAttribute(String attribName) {
171: return xtable.getAttribute(attribName);
172: }
173:
174: public void set(String attribName, Object newObject) {
175: /**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
176: }
177:
178: public void set(Object s) {
179: /**@todo Implement this net.xoetrope.xui.data.XModel abstract method*/
180: fireModelUpdated();
181: }
182:
183: public XModel get(int i) {
184: return new DatabaseFieldModel(xtable, rowIdx, i);
185: }
186:
187: public Object get() {
188: return new DatabaseFieldModel(xtable, rowIdx, 0);
189: }
190:
191: public Object append(String id) {
192: return null;
193: }
194:
195: public int getValueAsInt(String elementName) {
196: /**This method is inappropriate for this node type*/
197: throw new java.lang.UnsupportedOperationException(
198: "Method getValueAsInt() not yet implemented.");
199: }
200:
201: /**
202: * Get the value of a field as a model node
203: * @param i the field index
204: * @return the new field model node
205: */
206: public XModel getValue(int i) {
207: return new DatabaseFieldModel(xtable, rowIdx, i);
208: }
209:
210: public void setAttribValue(int i, Object value) {
211: xtable.setFieldValue(i, value.toString());
212: }
213:
214: public void setAttribValue(int i, String attribName, Object value) {
215: xtable.setFieldValue(i, value.toString());
216: }
217:
218: /**
219: * Set the value of a field
220: * @param colIdx the field index
221: * @param newValue the new field value
222: */
223: public void setFieldValue(int colIdx, String newValue) {
224: xtable.setFieldValue(colIdx, newValue);
225: }
226:
227: /**
228: * Get the hashcode for this row
229: * @return the row id
230: */
231: public int hashCode() {
232: return rowIdx;
233: }
234:
235: /**
236: * Gets the number of fields for this row
237: * @return the number of fields
238: */
239: public int getNumChildren() {
240: return xtable.xtable.numFields;
241: }
242:
243: /**
244: * @param i The index of the attributeValues array whose value we want
245: * @return The double value of the attributeValues array at position i
246: */
247: public double getAttribValueAsDouble(int i) {
248: return new Double(xtable.getAttribValueAsString(i))
249: .doubleValue();
250: }
251:
252: /**
253: * @param i The index of the attributeValues array whose value we want
254: * @return The int value of the attributeValues array at position i
255: */
256: public int getAttribValueAsInt(int i) {
257: return new Integer(xtable.getAttribValueAsString(i)).intValue();
258: }
259:
260: /**
261: * Remove a node
262: * @param childnode node to be removed
263: */
264: public void remove(XModel node) {
265: /**@todo Implement this abstract method*/
266: throw new java.lang.UnsupportedOperationException(
267: "Method remove() not yet implemented.");
268: }
269:
270: }
|