001: package jimm.datavision;
002:
003: import jimm.datavision.source.DataSource;
004: import jimm.datavision.source.Table;
005: import jimm.datavision.source.sql.SQLQuery;
006: import jimm.util.XMLWriter;
007:
008: /**
009: * A user column is an arbitrary expression inserted into the SQL query
010: * and retrieved as a column value. It may contain database
011: * column values, parameters, special values, strings, or
012: * numbers. It can't contain formulas because their values may be
013: * undefined when the query is run.
014: * <p>
015: * When used by a query, the following substitutions are made withing
016: * the <var>userString</var> of a user column:
017: * <ul>
018: * <li>{<i>table_name.column_name</i>} is replaced by the column name
019: * <i>table_name.column_name</i>.</li>
020: * <li> {%<i>special_value_name</i>} is replaced by a special value
021: * (report title, report run date, page number, or record number).</li>
022: * <li> {?<i>id_number</i>} is replaced by a parameter value (string,
023: * number, or date).</li>
024: * <ul>
025: *
026: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
027: */
028: public class UserColumn extends Expression implements Selectable {
029:
030: public static final int MAX_DISPLAY_NAME_LENGTH = 16;
031:
032: /**
033: * Constructor.
034: *
035: * @param id the unique identifier for the new user column; if
036: * <code>null</code>, generate a new id
037: * @param report the report containing this user column
038: * @param name the user column name
039: */
040: public UserColumn(Long id, Report report, String name) {
041: this (id, report, name, null);
042: }
043:
044: /**
045: * Constructor. If <i>id</i> is <code>null</code>, generates a new id number.
046: * This number is one higher than any previously-seen id number. This does
047: * <em>not</em> guarantee that no later user column will be created manually
048: * with the same id number.
049: *
050: * @param id the unique identifier for the new user column; if
051: * <code>null</code>, generate a new id
052: * @param report the report containing this user column
053: * @param name the user column name
054: * @param evalString the string to evaulate at runtime.
055: */
056: public UserColumn(Long id, Report report, String name,
057: String evalString) {
058: super (id == null ? report.generateNewUserColumnId() : id,
059: report, name, evalString, null);
060: }
061:
062: public Object getValue(Report report) {
063: return report.columnValue(this );
064: }
065:
066: public String fieldTypeString() {
067: return "usercol";
068: }
069:
070: public String getSelectString(SQLQuery query) {
071: String str = getExpression();
072: if (str == null)
073: return null;
074: return query.prepare(str);
075: }
076:
077: public String getSortString(SQLQuery query) {
078: return getSelectString(query);
079: }
080:
081: public Table getTable() {
082: return null;
083: }
084:
085: public String getDisplayName() {
086: return getName();
087: }
088:
089: public String dragString() {
090: return "usercol:" + getId();
091: }
092:
093: public String designLabel() {
094: return "{!" + getName() + "}";
095: }
096:
097: public String formulaString() {
098: return "{!" + getId() + "}";
099: }
100:
101: public Selectable reloadInstance(DataSource dataSource) {
102: return this ;
103: }
104:
105: /**
106: * Returns this user column's SQL text as entered by the user.
107: */
108: public String toString() {
109: return getExpression();
110: }
111:
112: public void writeXML(XMLWriter out) {
113: writeXML(out, "usercol");
114: }
115:
116: }
|