001: package org.julp;
002:
003: /**
004: * This object is holding original values from database.
005: * The values are using to build UPDATE and WHERE clauses
006: * fields are "1 - based" like java.sql.ResultSet.
007: */
008:
009: public class DataHolder implements java.io.Serializable, Cloneable {
010:
011: public DataHolder() {
012: }
013:
014: public DataHolder(int fieldCount) {
015: this .fieldCount = fieldCount;
016: data = new Object[fieldCount];
017: }
018:
019: protected int fieldCount = 0;
020: protected Object[] data = null;
021: protected String[] fieldName = null;
022: protected boolean ignoreUndefinedFieldName = false;
023:
024: public int getFieldCount() {
025: return fieldCount;
026: }
027:
028: public void setFieldCount(int fieldCount) {
029: this .fieldCount = fieldCount;
030: }
031:
032: public void setObject(int fieldIndex, Object value) {
033: this .data[fieldIndex - 1] = value;
034: }
035:
036: public Object getObject(int fieldIndex) {
037: return this .data[fieldIndex - 1];
038: }
039:
040: public void setFieldNameAndValue(int fieldIndex, String name,
041: Object value) {
042: this .data[fieldIndex - 1] = value;
043: if (this .fieldName == null) {
044: this .fieldName = new String[this .fieldCount];
045: }
046: this .fieldName[fieldIndex - 1] = name;
047: }
048:
049: public void setObject(String name, Object value) {
050: int fieldIndex = findFieldIndex(name);
051: if (fieldIndex < 0) {
052: if (this .fieldName == null) {
053: if (!ignoreUndefinedFieldName) {
054: throw new IllegalArgumentException(
055: "Field Name not set");
056: }
057: System.out.println("Field Name not set");
058: } else {
059: if (!ignoreUndefinedFieldName) {
060: throw new IllegalArgumentException(
061: "No such field: " + name);
062: }
063: }
064: }
065: this .data[fieldIndex - 1] = value;
066: }
067:
068: public Object getObject(String name) {
069: if (name == null) {
070: return null;
071: }
072: int fieldIndex = findFieldIndex(name);
073: if (fieldIndex < 0) {
074: if (this .fieldName == null) {
075: if (!ignoreUndefinedFieldName) {
076: throw new IllegalArgumentException(
077: "Field Name not set");
078: }
079: } else {
080: if (!ignoreUndefinedFieldName) {
081: throw new IllegalArgumentException(
082: "No such field: " + name);
083: }
084: }
085: return null;
086: }
087: return this .data[fieldIndex - 1];
088: }
089:
090: public void setFieldName(int fieldIndex, String name) {
091: if (this .fieldName == null) {
092: this .fieldName = new String[this .fieldCount];
093: }
094: this .fieldName[fieldIndex - 1] = name;
095: }
096:
097: public String getFieldName(int fieldIndex) {
098: if (this .fieldName == null || this .fieldName.length == 0) {
099: return null;
100: }
101: return this .fieldName[fieldIndex - 1];
102: }
103:
104: public int findFieldIndex(String name) {
105: int fieldIndex = -1;
106: if (this .fieldName == null || name == null) {
107: return fieldIndex;
108: }
109: for (int i = 0; i < this .fieldCount; i++) {
110: if (fieldName[i] == null) {
111: continue;
112: }
113: if (fieldName[i].equals(name)) {
114: fieldIndex = i + 1;
115: break;
116: }
117: }
118: return fieldIndex;
119: }
120:
121: public String toString() {
122: StringBuffer sb = new StringBuffer();
123: if (this .fieldName != null
124: && this .fieldName.length == fieldCount) {
125: for (int i = 0; i < fieldCount; i++) {
126: sb.append(this .fieldName[i]).append("=");
127: if (this .data[i] == null) {
128: } else {
129: sb.append(this .data[i]);
130: }
131: sb.append("&");
132: }
133: sb.deleteCharAt(sb.length() - 1);
134: } else {
135: for (int i = 0; i < fieldCount; i++) {
136: if (this .data[i] == null) {
137: sb.append("null");
138: } else {
139: sb.append(this .data[i]);
140: }
141: sb.append(", ");
142: }
143: sb.deleteCharAt(sb.length() - 2);
144: }
145: return sb.toString();
146: }
147:
148: public java.lang.Object[] getData() {
149: return this .data;
150: }
151:
152: public void setData(java.lang.Object[] data) {
153: this .data = data;
154: }
155:
156: public boolean isIgnoreUndefinedFieldName() {
157: return ignoreUndefinedFieldName;
158: }
159:
160: public void setIgnoreUndefinedFieldName(
161: boolean ignoreUndefinedFieldName) {
162: this.ignoreUndefinedFieldName = ignoreUndefinedFieldName;
163: }
164:
165: }
|