KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > source > Column


1 package jimm.datavision.source;
2 import jimm.datavision.*;
3 import jimm.datavision.source.sql.SQLQuery;
4 import jimm.util.XMLWriter;
5 import java.sql.Types JavaDoc;
6
7 /**
8  * Represents a data column. Not all data sources' columns will be
9  * contained within tables. For those that don't, their columns'
10  * <code>getTable</code> method will return <code>null</code>.
11  *
12  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
13  * @see Table
14  * @see DataSource
15  */

16 public class Column
17     implements Identity, Nameable, Selectable, Draggable, Writeable
18 {
19
20 public static final String JavaDoc DEFAULT_DATE_PARSE_FORMAT = "yyyy-MM-dd";
21
22 protected Object JavaDoc id;
23 protected String JavaDoc name;
24 protected int type;
25 protected String JavaDoc dateParseFormat;
26
27 public static int typeFromString(String JavaDoc str) {
28     if (str == null || str.length() == 0)
29     return Types.VARCHAR;
30
31     str = str.toLowerCase();
32     if (str.equals("number"))
33     return Types.NUMERIC;
34     if (str.equals("date"))
35     return Types.DATE;
36     return Types.VARCHAR;
37 }
38
39 public static String JavaDoc typeToString(int type) {
40     switch (type) {
41     case Types.BIGINT:
42     case Types.BIT:
43     case Types.DECIMAL:
44     case Types.DOUBLE:
45     case Types.INTEGER:
46     case Types.NUMERIC:
47     case Types.REAL:
48     case Types.SMALLINT:
49     case Types.TINYINT:
50     return "number";
51     case Types.DATE:
52     case Types.TIME:
53     case Types.TIMESTAMP:
54     return "date";
55     default:
56     return "string";
57     }
58 }
59
60 public Column(Object JavaDoc id, String JavaDoc name, int type) {
61     this.id = id;
62     this.name = name;
63     this.type = type;
64 }
65
66 /**
67  * Returns <code>true</code> if the other object is a column with the
68  * same id.
69  *
70  * @param obj any <code>Object</code>
71  * @return <code>true</code> if the other object is a column with the
72  * same id.
73  */

74 public boolean equals(Object JavaDoc obj) {
75     if (obj == null || !(obj instanceof Column)) return false;
76     if (this == obj) return true;
77     return id.equals(((Column)obj).getId());
78 }
79
80 public int hashCode() {
81     return id.hashCode();
82 }
83
84 /**
85  * Returns the table id. This is a string of the form
86  * "table_name.column_name".
87  *
88  * @return the id string
89  */

90 public Object JavaDoc getId() { return id; }
91
92 /**
93  * Returns the column's name. The name may not be unique. To retrieve
94  * a unique name (for example, "table.column"), use <code>fullName</code>.
95  *
96  * @return the column's name
97  * @see #fullName
98  */

99 public String JavaDoc getName() { return name; }
100
101 /** A column's name is immutable. */
102 public void setName(String JavaDoc name) { }
103
104 /**
105  * Returns the date parse format, useful for data sources that read text
106  * strings and convert them into date objects. If no format has been
107  * defined, returns <code>DEFAULT_DATE_PARSE_FORMAT</code>
108  *
109  * @return the date parse format string (if not defined, returns
110  * <code>DEFAULT_DATE_PARSE_FORMAT</code>)
111  */

112 public String JavaDoc getDateParseFormat() {
113     return dateParseFormat == null
114     ? DEFAULT_DATE_PARSE_FORMAT : dateParseFormat;
115 }
116
117 /**
118  * Sets the date parse format, useful for data sources that read text
119  * strings and convert them into date objects. Called from
120  * <code>ReportReader.column</code>, for example.
121  *
122  * @param format the date parse format string
123  */

124 public void setDateParseFormat(String JavaDoc format) { dateParseFormat = format; }
125
126 /**
127  * Returns the table to which this column belongs, if any.
128  *
129  * @return the table, or <code>null</code> if there is none
130  */

131 public Table getTable() { return null; }
132
133 /**
134  * Returns the full named of this column: the id as a string. The column's
135  * name may not be unique, but the full name should be unique.
136  * <p>
137  * For SQL columns, this is a string of the form "table_name.column_name".
138  * To retrieve just the column name, use <code>getName</code>.
139  *
140  * @return the full name; this is the same as the id as a string
141  * @see #getName
142  */

143 public String JavaDoc fullName() {
144     return id.toString();
145 }
146
147 /**
148  * Returns the type constant (a <code>java.sql.Types</code> value).
149  *
150  * @return a <code>java.sql.Types</code> value
151  * @see java.sql.Types
152  */

153 public int getType() { return type; }
154
155 public Object JavaDoc getValue(Report report) {
156     return report.columnValue(this);
157 }
158
159 public String JavaDoc fieldTypeString() { return "column"; }
160
161 public String JavaDoc getSelectString(SQLQuery query) {
162     return query.quoted(fullName());
163 }
164
165 public String JavaDoc getSortString(SQLQuery query) {
166     return getSelectString(query);
167 }
168
169 public String JavaDoc dragString() {
170     return "column:" + fullName();
171 }
172
173 public String JavaDoc getDisplayName() { return fullName(); }
174
175 public Selectable reloadInstance(DataSource dataSource) {
176     return dataSource.findColumn(getId());
177 }
178
179 /**
180  * Returns <code>true</code> if this column is some numeric type
181  * (double, int, etc.)
182  *
183  * @return <code>true</code> if this column is some numeric type
184  * (double, int, etc.)
185  */

186 public boolean isNumeric() {
187     return type == Types.BIGINT
188     || type == Types.BIT
189     || type == Types.DECIMAL
190     || type == Types.DOUBLE
191     || type == Types.FLOAT
192     || type == Types.INTEGER
193     || type == Types.NUMERIC
194     || type == Types.REAL
195     || type == Types.SMALLINT
196     || type == Types.TINYINT;
197 }
198
199 /**
200  * Returns <code>true</code> if this column is some date type.
201  *
202  * @return <code>true</code> if this column is some date type
203  */

204 public boolean isDate() {
205     return type == Types.DATE
206     || type == Types.TIME
207     || type == Types.TIMESTAMP;
208 }
209
210 /**
211  * Returns <code>true</code> if this column is some character type.
212  *
213  * @return <code>true</code> if this column is some character type
214  */

215 public boolean isString() {
216     return !isNumeric() && !isDate();
217 }
218
219 /**
220  * Returns a string representation of this column. Calls
221  * <code>fullName</code>.
222  *
223  * @see #fullName
224  */

225 public String JavaDoc toString() {
226     return fullName();
227 }
228
229 public void writeXML(XMLWriter out) {
230     out.startElement("column");
231     out.attr("name", getName());
232     out.attr("type", typeToString(type));
233     if (dateParseFormat != null)
234     out.attr("date-format", dateParseFormat);
235     out.endElement();
236 }
237
238 }
239
Popular Tags