001: package com.teamkonzept.field.db;
002:
003: import com.teamkonzept.lib.*;
004: import com.teamkonzept.db.*;
005: import com.teamkonzept.field.*;
006:
007: import java.sql.*;
008: import java.text.*;
009:
010: public class TKContentAttributeTableData extends TKDBTableData
011: implements TKSortable {
012:
013: public static int intType = 1;
014: public static int stringType = 2;
015: public static int dateType = 3;
016:
017: public static SimpleDateFormat dateFormat = new SimpleDateFormat(
018: "dd.MM.yyyy HH:mm:ss");
019: public static SimpleDateFormat dateScanFormat = new SimpleDateFormat(
020: "d.M.y H:m:s");
021:
022: public int content_id;
023: public int value_id;
024: public int attribute_id;
025: public int type;
026: public String name;
027: public String value;
028: public String classname;
029:
030: public TKContentAttributeTableData() {
031: this .content_id = -1;
032: this .attribute_id = -1;
033: this .type = stringType;
034: this .value = null;
035:
036: this .value_id = -1;
037: this .name = null;
038: this .classname = null;
039: }
040:
041: public TKContentAttributeTableData(int content_id,
042: int attribute_id, String value) {
043: this .content_id = content_id;
044: this .attribute_id = attribute_id;
045: this .type = stringType;
046: this .value = value;
047:
048: this .value_id = -1;
049: this .name = null;
050: this .classname = null;
051:
052: TKContentAttributeOption option = TKContentAttributeOption
053: .getOptionById(attribute_id);
054: if (option != null) {
055:
056: this .name = option.name;
057: this .type = option.type;
058: this .classname = option.classname;
059: }
060: }
061:
062: public TKContentAttributeTableData(int content_id) {
063: this .content_id = content_id;
064: this .attribute_id = -1;
065: this .type = stringType;
066: this .value = null;
067:
068: this .value_id = -1;
069: this .name = null;
070: this .classname = null;
071: }
072:
073: public TKContentAttributeTableData(ResultSet r) throws SQLException {
074: this .content_id = r.getInt("CONTENT_ID");
075: this .value_id = r.getInt("VALUE_ID");
076: this .attribute_id = r.getInt("ATTRIBUTE_ID");
077: this .type = r.getInt("TYPE");
078: this .name = r.getString("NAME");
079: this .classname = r.getString("CLASSNAME");
080:
081: if (this .type == intType)
082: this .value = Integer.toString(r.getInt("INT_VALUE"));
083: else if (this .type == stringType)
084: this .value = r.getString("STR_VALUE");
085: else if (this .type == dateType)
086: this .value = dateFormat
087: .format(r.getTimestamp("DATE_VALUE"));
088: else
089: this .value = null;
090: }
091:
092: public static String scanValue(String value, int attribute_id)
093: throws Exception {
094: TKContentAttributeOption option = TKContentAttributeOption
095: .getOptionById(attribute_id);
096: if (option == null)
097: throw new Exception("attribute_id " + attribute_id
098: + " invalid");
099:
100: if (option.type == stringType)
101: return value;
102: else if (option.type == intType)
103: return Integer.toString(Integer.parseInt(value));
104: else if (option.type != dateType)
105: throw new Exception("attribute_id " + attribute_id
106: + " invalid");
107:
108: return dateFormat
109: .format(value == null || value.length() == 0 ? new java.util.Date()
110: : dateScanFormat.parse(value,
111: new ParsePosition(0)));
112: }
113:
114: public void updatePrimary(TKDBVectorData dbData) {
115:
116: TKContentDBData cdata = (TKContentDBData) dbData;
117: content_id = cdata.content_id;
118: }
119:
120: public void insertIntoQuery(TKQuery query) throws SQLException {
121: query.setQueryParams("CONTENT_ID", new Integer(content_id));
122: query.setQueryParams("ATTRIBUTE_ID", new Integer(attribute_id));
123:
124: if (this .type == intType) {
125: query.setQueryParams("STR_VALUE", TKNull.NULL);
126: query.setQueryParams("INT_VALUE", new Integer(value));
127: query.setQueryParams("DATE_VALUE", TKNull.NULL);
128: } else if (this .type == stringType) {
129: query.setQueryParams("STR_VALUE", value);
130: query.setQueryParams("INT_VALUE", TKNull.NULL);
131: query.setQueryParams("DATE_VALUE", TKNull.NULL);
132: } else if (this .type == dateType) {
133: query.setQueryParams("STR_VALUE", TKNull.NULL);
134: query.setQueryParams("INT_VALUE", TKNull.NULL);
135:
136: java.util.Date dt = value == null || value.length() == 0 ? new java.util.Date()
137: : dateScanFormat.parse(value, new ParsePosition(0));
138:
139: query.setQueryParams("DATE_VALUE", new java.sql.Timestamp(
140: dt.getTime()));
141: } else {
142: query.setQueryParams("STR_VALUE", TKNull.NULL);
143: query.setQueryParams("INT_VALUE", TKNull.NULL);
144: query.setQueryParams("DATE_VALUE", TKNull.NULL);
145: }
146: query.setQueryParams("VALUE_ID", new Integer(value_id));
147: }
148:
149: public TKDBTableData newFromResultSet(ResultSet r)
150: throws SQLException {
151: return new TKContentAttributeTableData(r);
152: }
153:
154: public String toString() {
155: return "( CONTENT_ID=" + String.valueOf(content_id)
156: + ", ATTRIBUTE_ID=" + String.valueOf(attribute_id)
157: + ", VALUE_ID=" + String.valueOf(value_id) + ", TYPE="
158: + String.valueOf(type) + ", NAME="
159: + String.valueOf(name) + ", VALUE="
160: + String.valueOf(value) + ")<BR>";
161: }
162:
163: public static String typeInfo(int type) {
164:
165: if (type == TKContentAttributeTableData.intType)
166: return "Integer";
167: else if (type == TKContentAttributeTableData.stringType)
168: return "String";
169: else if (type == TKContentAttributeTableData.dateType)
170: return "Date";
171: else
172: return "*** unknown ***";
173: }
174:
175: public int cmp(TKSortable other) {
176:
177: if (!(other instanceof TKContentAttributeTableData))
178: return toString().compareTo(other.toString());
179:
180: TKContentAttributeTableData otherData = (TKContentAttributeTableData) other;
181:
182: if ((name == null) && (otherData.name != null))
183: return -1;
184: else if ((name != null) && (otherData.name == null))
185: return 1;
186: else if ((name == null) && (otherData.name == null))
187: return 0;
188: else
189: return name.compareTo(otherData.name);
190: };
191: //{{DECLARE_CONTROLS
192: //}}
193: }
|