001: package com.salmonllc.jsp.tags;
002:
003: /////////////////////////
004: //$Archive: /SOFIA/SourceCode/com/salmonllc/jsp/tags/DataSourceFieldTag.java $
005: //$Author: Dan $
006: //$Revision: 19 $
007: //$Modtime: 6/11/03 4:58p $
008: /////////////////////////
009: import com.salmonllc.sql.*;
010:
011: /**
012: * Implements the field tag.
013: */
014: public class DataSourceFieldTag extends BaseEmptyTag {
015: String _tableName, _fieldName, _dataType, _primaryKey, _updateable,
016: _format, _sortdir, _aliasName;
017:
018: /**
019: * This method must be implemented by each subclass of this tag. Each tag except NestedTags has an Html or Jsp Component associated with it. This method must be implemented to insure that the correct component gets created.
020: */
021: void addField(DataStoreBuffer buf, DataDictionary dict) {
022: DataStore ds = (DataStore) buf;
023: String name = getName();
024: if (_fieldName != null && _tableName == null) {
025: int pos = _fieldName.lastIndexOf(".");
026: if (pos > -1 && _fieldName.indexOf('(') == -1
027: && _fieldName.indexOf(',') == -1) {
028: _tableName = _fieldName.substring(0, pos);
029: _fieldName = _fieldName.substring(pos + 1);
030: }
031: }
032:
033: try {
034: _aliasName = _tableName;
035: if (_aliasName == null)
036: _aliasName = ds.getDefaultTable();
037:
038: if (_aliasName != null) {
039: for (int i = 0; i < ds.getAliasCount(); i++) {
040: String alias = ds.getAlias(i);
041: if (alias != null && alias.equals(_aliasName)) {
042: _aliasName = ds.getTable(i);
043: break;
044: }
045: }
046: }
047: } catch (Exception e) {
048: }
049:
050: if (_tableName != null && ds.getDefaultTable() != null) {
051: if (_tableName.equalsIgnoreCase(ds.getDefaultTable()))
052: _tableName = null;
053: }
054:
055: if (name == null) {
056: if (_tableName != null)
057: name = _tableName + "." + _fieldName;
058: else
059: name = _fieldName;
060: }
061:
062: ds.addColumn(ds.computeTableName(_tableName), _fieldName,
063: getDataType(dict), getPrimaryKey(dict), BaseTagHelper
064: .stringToBoolean(_updateable, true), name);
065: try {
066: if (_format != null)
067: ds.setFormat(name, _format);
068: } catch (Exception e) {
069: }
070:
071: }
072:
073: public Object clone() {
074: DataSourceFieldTag f = new DataSourceFieldTag();
075: f.setName(getName());
076: f.setDatatype(_dataType);
077: f.setFieldname(_fieldName);
078: f.setFormat(_format);
079: f.setPrimarykey(_primaryKey);
080: f.setSortdir(_sortdir);
081: f.setTablename(_tableName);
082: f.setUpdateable(_updateable);
083: return f;
084: }
085:
086: /**
087: * This method must be implemented by each subclass of this tag. Each tag except NestedTags has an Html or Jsp Component associated with it. This method must be implemented to insure that the correct component gets created.
088: */
089: public com.salmonllc.html.HtmlComponent createComponent() {
090: BaseTagHelper h = getHelper();
091: DataStoreBuffer buf = h.getDataSourceObject();
092: DataSourceNestedTag par = h.getDataSourceNestedTag();
093:
094: try {
095: if (par instanceof DataSourceDefinitionTag) {
096: DataSourceTag t = h.getDataSourceTag();
097: if (t != null)
098: t.addField((DataSourceFieldTag) this .clone());
099: } else if (par instanceof DataSourceOrderTag) {
100: String name = _fieldName;
101: if (name == null)
102: name = getName();
103: if (buf instanceof DataStore)
104: name = ((DataStore) buf)
105: .computeTableAndFieldName(name);
106: ((DataSourceOrderTag) par).addColumn(name, _sortdir);
107: } else if (par instanceof DataSourceGroupTag) {
108: String name = _fieldName;
109: if (name == null)
110: name = getName();
111: if (buf instanceof DataStore)
112: name = ((DataStore) buf)
113: .computeTableAndFieldName(name);
114: ((DataSourceGroupTag) par).addColumn(name);
115: }
116:
117: } catch (Exception e) {
118: }
119:
120: return null;
121: }
122:
123: /**
124: * Returns the DataType
125: */
126: private int getDataType(DataDictionary d) {
127: if (_dataType == null)
128: return d.getDSDataType(_aliasName + "." + _fieldName);
129: else
130: return translateDataType(_dataType);
131: }
132:
133: /**
134: * Gets the fieldname attribute.
135: */
136: public String getFieldname() {
137: return _fieldName;
138: }
139:
140: /**
141: * Gets the format attribute.
142: */
143: public String getFormat() {
144: return _format;
145: }
146:
147: /**
148: * Returns whether or not the field is a primary key
149: */
150: private boolean getPrimaryKey(DataDictionary d) {
151: if (_primaryKey == null)
152: return d.isPkey(_aliasName + "." + _fieldName);
153: else
154: return BaseTagHelper.stringToBoolean(_primaryKey, false);
155:
156: }
157:
158: /**
159: * Returns CONV_DONT_CONVERT
160: */
161: public int getTagConvertType() {
162: return CONV_DONT_CONVERT;
163: }
164:
165: /**
166: * Releases resources used by the tag
167: */
168: public void release() {
169: super .release();
170: _tableName = null;
171: _fieldName = null;
172: _dataType = null;
173: _primaryKey = null;
174: _updateable = null;
175: _format = null;
176: _sortdir = null;
177: _aliasName = null;
178: }
179:
180: /**
181: * Sets the fieldname attribute.
182: */
183: public void setDatatype(String value) {
184: _dataType = value;
185: }
186:
187: /**
188: * Sets the fieldname attribute.
189: */
190: public void setFieldname(String value) {
191: _fieldName = value;
192: }
193:
194: /**
195: * Sets the format attribute.
196: */
197: public void setFormat(String value) {
198: _format = value;
199: }
200:
201: /**
202: * Sets the primary key attribute.
203: */
204: public void setPrimarykey(String value) {
205: _primaryKey = value;
206: }
207:
208: /**
209: * Sets the sortdir attribute.
210: */
211: public void setSortdir(String value) {
212: _sortdir = value;
213: }
214:
215: /**
216: * Sets the tablename attribute.
217: */
218: public void setTablename(String value) {
219: _tableName = value;
220: }
221:
222: /**
223: * Sets the updateable attribute.
224: */
225: public void setUpdateable(String value) {
226: _updateable = value;
227: }
228:
229: /**
230: * Translates a string to a datastore datatype
231: */
232: static int translateDataType(String type) {
233: if (type == null)
234: return DataStore.DATATYPE_STRING;
235: type = type.toUpperCase();
236: if (type.equals("STRING"))
237: return DataStore.DATATYPE_STRING;
238: else if (type.startsWith("INT"))
239: return DataStore.DATATYPE_INT;
240: else if (type.equals("DATETIME"))
241: return DataStore.DATATYPE_DATETIME;
242: else if (type.equals("DOUBLE"))
243: return DataStore.DATATYPE_DOUBLE;
244: else if (type.equals("BYTEARRAY"))
245: return DataStore.DATATYPE_BYTEARRAY;
246: else if (type.equals("SHORT"))
247: return DataStore.DATATYPE_SHORT;
248: else if (type.equals("LONG"))
249: return DataStore.DATATYPE_LONG;
250: else if (type.equals("FLOAT"))
251: return DataStore.DATATYPE_FLOAT;
252: else if (type.equals("DATE"))
253: return DataStore.DATATYPE_DATE;
254: else if (type.equals("TIME"))
255: return DataStore.DATATYPE_TIME;
256: else
257: return DataStore.DATATYPE_STRING;
258: }
259: }
|