KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > field > db > TKContentAttributeTableData


1 package com.teamkonzept.field.db;
2
3 import com.teamkonzept.lib.*;
4 import com.teamkonzept.db.*;
5 import com.teamkonzept.field.*;
6
7 import java.sql.*;
8 import java.text.*;
9
10 public class TKContentAttributeTableData extends TKDBTableData implements TKSortable {
11
12     public static int intType = 1;
13     public static int stringType = 2;
14     public static int dateType = 3;
15
16     public static SimpleDateFormat dateFormat = new SimpleDateFormat ("dd.MM.yyyy HH:mm:ss");
17     public static SimpleDateFormat dateScanFormat = new SimpleDateFormat ("d.M.y H:m:s");
18
19     public int content_id;
20     public int value_id;
21     public int attribute_id;
22     public int type;
23     public String JavaDoc name;
24     public String JavaDoc value;
25     public String JavaDoc classname;
26
27     public TKContentAttributeTableData( )
28     {
29         this.content_id = -1;
30         this.attribute_id = -1;
31         this.type = stringType;
32         this.value = null;
33
34         this.value_id = -1;
35         this.name = null;
36         this.classname = null;
37     }
38
39     public TKContentAttributeTableData(
40         int content_id, int attribute_id, String JavaDoc value )
41     {
42         this.content_id = content_id;
43         this.attribute_id = attribute_id;
44         this.type = stringType;
45         this.value = value;
46
47         this.value_id = -1;
48         this.name = null;
49         this.classname = null;
50         
51         TKContentAttributeOption option = TKContentAttributeOption.getOptionById (attribute_id);
52         if (option != null) {
53
54             this.name = option.name;
55             this.type = option.type;
56             this.classname = option.classname;
57         }
58     }
59
60     public TKContentAttributeTableData (int content_id)
61     {
62         this.content_id = content_id;
63         this.attribute_id = -1;
64         this.type = stringType;
65         this.value = null;
66
67         this.value_id = -1;
68         this.name = null;
69         this.classname = null;
70     }
71
72     public TKContentAttributeTableData ( ResultSet r )
73         throws SQLException
74     {
75         this.content_id = r.getInt("CONTENT_ID");
76         this.value_id = r.getInt("VALUE_ID");
77         this.attribute_id = r.getInt("ATTRIBUTE_ID");
78         this.type = r.getInt("TYPE");
79         this.name = r.getString("NAME");
80         this.classname = r.getString("CLASSNAME");
81         
82         if (this.type == intType) this.value = Integer.toString (r.getInt("INT_VALUE"));
83         else if (this.type == stringType) this.value = r.getString("STR_VALUE");
84         else if (this.type == dateType) this.value = dateFormat.format(r.getTimestamp("DATE_VALUE"));
85         else this.value = null;
86     }
87     
88     public static String JavaDoc scanValue (String JavaDoc value, int attribute_id) throws Exception JavaDoc
89     {
90         TKContentAttributeOption option = TKContentAttributeOption.getOptionById (attribute_id);
91         if (option == null) throw new Exception JavaDoc ("attribute_id "+attribute_id+" invalid");
92
93         if (option.type == stringType) return value;
94         else if (option.type == intType) return Integer.toString (Integer.parseInt (value));
95         else if (option.type != dateType) throw new Exception JavaDoc ("attribute_id "+attribute_id+" invalid");
96
97         return dateFormat.format(
98             value == null || value.length() == 0 ? new java.util.Date JavaDoc() :
99                 dateScanFormat.parse(value, new ParsePosition(0)));
100     }
101     
102     public void updatePrimary (TKDBVectorData dbData) {
103     
104         TKContentDBData cdata = (TKContentDBData) dbData;
105         content_id = cdata.content_id;
106     }
107
108     public void insertIntoQuery(TKQuery query)
109         throws SQLException
110     {
111         query.setQueryParams("CONTENT_ID", new Integer JavaDoc(content_id));
112         query.setQueryParams("ATTRIBUTE_ID", new Integer JavaDoc(attribute_id));
113
114         if (this.type == intType) {
115             query.setQueryParams("STR_VALUE", TKNull.NULL);
116             query.setQueryParams("INT_VALUE", new Integer JavaDoc(value));
117             query.setQueryParams("DATE_VALUE", TKNull.NULL);
118         } else if (this.type == stringType) {
119             query.setQueryParams("STR_VALUE", value);
120             query.setQueryParams("INT_VALUE", TKNull.NULL);
121             query.setQueryParams("DATE_VALUE", TKNull.NULL);
122         } else if (this.type == dateType) {
123             query.setQueryParams("STR_VALUE", TKNull.NULL);
124             query.setQueryParams("INT_VALUE", TKNull.NULL);
125
126             java.util.Date JavaDoc dt = value == null || value.length() == 0 ? new java.util.Date JavaDoc() :
127                 dateScanFormat.parse(value, new ParsePosition(0));
128
129             query.setQueryParams("DATE_VALUE", new java.sql.Timestamp JavaDoc(dt.getTime()));
130         } else {
131             query.setQueryParams("STR_VALUE", TKNull.NULL);
132             query.setQueryParams("INT_VALUE", TKNull.NULL);
133             query.setQueryParams("DATE_VALUE", TKNull.NULL);
134         }
135         query.setQueryParams("VALUE_ID", new Integer JavaDoc(value_id));
136     }
137     
138     public TKDBTableData newFromResultSet( ResultSet r )
139         throws SQLException
140     {
141         return new TKContentAttributeTableData( r );
142     }
143     
144     public String JavaDoc toString()
145     {
146         return "( CONTENT_ID="+String.valueOf( content_id )
147             + ", ATTRIBUTE_ID="+String.valueOf( attribute_id )
148             + ", VALUE_ID="+String.valueOf( value_id )
149             + ", TYPE="+String.valueOf( type )
150             + ", NAME="+String.valueOf( name )
151             + ", VALUE="+String.valueOf( value )
152             + ")<BR>";
153     }
154
155     public static String JavaDoc typeInfo (int type) {
156
157         if (type == TKContentAttributeTableData.intType) return "Integer";
158         else if (type == TKContentAttributeTableData.stringType) return "String";
159         else if (type == TKContentAttributeTableData.dateType) return "Date";
160         else return "*** unknown ***";
161     }
162
163     public int cmp (TKSortable other) {
164
165         if (!(other instanceof TKContentAttributeTableData))
166             return toString().compareTo (other.toString());
167
168         TKContentAttributeTableData otherData = (TKContentAttributeTableData) other;
169         
170         if ((name == null) && (otherData.name != null)) return -1;
171         else if ((name != null) && (otherData.name == null)) return 1;
172         else if ((name == null) && (otherData.name == null)) return 0;
173         else return name.compareTo (otherData.name);
174     };
175     //{{DECLARE_CONTROLS
176
//}}
177
}
178
Popular Tags