001: package com.jat.integration.db;
002:
003: import com.jat.core.config.Config;
004: import com.jat.core.log.LogManager;
005: import com.jat.integration.GenericFieldDefinition;
006: import com.jat.integration.IntegrationException;
007: import com.jat.integration.db.plugin.ConverterPlugin;
008:
009: /**
010: * <p>Title: JAT</p>
011: * <p>Description: </p>
012: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
013: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
014: * @author stf
015: * @version 1.0
016: * @since 1.2
017: */
018:
019: public class FieldDefinition extends GenericFieldDefinition {
020:
021: public static final String CONFIG_TYPE = ".type";
022: public static final String CONFIG_PLUGIN = ".plugin";
023: public static final String CONFIG_OUTPUT = ".output";
024:
025: public final static int STRING_ARRAY = 10000;
026: public final static int INT_ARRAY = 11000;
027:
028: protected void init(String section, String key, String queryName)
029: throws IntegrationException {
030: super .init(section, key, queryName);
031: try {
032: // set type
033: String ft = null;
034: try {
035: ft = Config.getCurrent().getValue(section,
036: key + CONFIG_TYPE);
037: } catch (Exception ignored) {
038: ft = "-1";
039: }
040: this .setType(ft);
041: LogManager.sendDebug(this .getClass().getName() + "::init: "
042: + section + "/" + queryName + "/Type = "
043: + this .getType());
044: // set plugin
045: String fieldPlugin = null;
046: try {
047: fieldPlugin = Config.getCurrent().getValue(section,
048: key + CONFIG_PLUGIN);
049: } catch (Exception ignored) {
050: }
051: LogManager.sendDebug(this .getClass().getName() + "::init: "
052: + section + "/" + queryName + "/Plugin = "
053: + fieldPlugin);
054: try {
055: if (fieldPlugin != null)
056: this .setPlugin(fieldPlugin);
057: } catch (Exception pex) {
058: throw new IntegrationException(this .getClass()
059: .getName()
060: + "::init: wrong plugin '"
061: + fieldPlugin
062: + "' for field '"
063: + this .getName()
064: + "' for query '"
065: + section
066: + "/"
067: + queryName
068: + "': " + pex);
069: }
070: // set output
071: boolean output = false;
072: try {
073: output = (Config.getCurrent().getValue(section, key
074: + CONFIG_OUTPUT)).equalsIgnoreCase("true");
075: } catch (Exception ignored) {
076: }
077: this .setOutput(output);
078: LogManager.sendDebug(this .getClass().getName() + "::init: "
079: + section + "/" + queryName + "/Output = "
080: + this .isOutput());
081: } catch (Exception ex) {
082: throw new IntegrationException(this .getClass().getName()
083: + "::init: " + ex);
084: }
085: }
086:
087: public FieldDefinition() {
088: super ();
089: this .setOutput(false);
090: try {
091: this .setType("-1");
092: } catch (Exception ignored) {
093: }
094: }
095:
096: public boolean isArrayType() {
097: return (this .getType() == STRING_ARRAY || this .getType() == INT_ARRAY);
098: }
099:
100: public Object convertValue(Object value)
101: throws IntegrationException {
102: if (this .getPlugin() != null) {
103: try {
104: return ((ConverterPlugin) Class.forName(
105: this .getPlugin()).newInstance()).convert(value);
106: } catch (Exception ex) {
107: throw new IntegrationException(this .getClass()
108: + ":: convertValue: exception: " + ex);
109: }
110: }
111: return value;
112: }
113:
114: public int getType() {
115: try {
116: return Integer.parseInt(this .getProperties().getProperty(
117: "type"));
118: } catch (Exception ex) {
119: return -1;
120: }
121: }
122:
123: public void setType(String type) throws IntegrationException {
124: try {
125: if (type != null) {
126: int t = Integer.parseInt(type);
127: this .getProperties().setProperty("type", type);
128: }
129: } catch (Exception intEx) {
130: throw new IntegrationException(
131: "wrong field type for field '" + this .getName()
132: + "': " + type);
133: }
134: }
135:
136: public String getPlugin() {
137: return this .getProperties().getProperty("plugin");
138: }
139:
140: public void setPlugin(String plugin) throws ClassNotFoundException {
141: Class c = Class.forName(plugin);
142: this .getProperties().setProperty("plugin", plugin);
143: }
144:
145: public boolean isOutput() {
146: try {
147: return this .getProperties().getProperty("output")
148: .equalsIgnoreCase("true");
149: } catch (Exception ex) {
150: return false;
151: }
152: }
153:
154: public void setOutput(boolean output) {
155: this .getProperties().setProperty("output", "" + output);
156: }
157:
158: public String toString() {
159: return "[" + this .getName() + "|" + this .getType() + "|"
160: + this .getPlugin() + "|" + this .isOutput() + "]";
161: }
162:
163: }
|