001: package com.jat.integration.db;
002:
003: import com.jat.business.BusinessObjectProperties;
004: import com.jat.core.config.Config;
005: import com.jat.core.log.LogManager;
006: import com.jat.integration.GenericFieldDefinition;
007: import com.jat.integration.GenericOperationDefinition;
008: import com.jat.integration.IntegrationException;
009: import com.jat.integration.db.plugin.CheckReturnPlugin;
010:
011: /**
012: * <p>Title: JAT</p>
013: * <p>Description: </p>
014: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
015: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
016: * @author stf
017: * @version 1.0
018: * @since 1.2
019: */
020:
021: public class QueryDefinition extends GenericOperationDefinition {
022:
023: public static final String SELECT_TYPE = "select";
024: public static final String UPDATE_TYPE = "update";
025: public static final String INSERT_TYPE = "insert";
026: public static final String PROCEDURE_TYPE = "procedure";
027:
028: public static final String CONFIG_OPERATION = "query";
029: public static final String CONFIG_TYPE = ".type";
030: public static final String CONFIG_RESPONSE_PLUGIN = ".response_plugin";
031:
032: public void init(String section, String key)
033: throws IntegrationException {
034: super .init(section, key);
035: // set type
036: try {
037: this .setType(Config.getCurrent().getValue(section,
038: key + CONFIG_TYPE));
039: } catch (Exception ex) {
040: this .setType(this .getDefaultType());
041: LogManager.sendDebug(this .getClass().getName()
042: + "::init: no type found for query '" + section
043: + "'. Using default...");
044: }
045: LogManager.sendDebug(this .getClass().getName() + "::init: "
046: + section + "/Type = " + this .getType());
047: // set return plugin
048: String returnPlugin = null;
049: try {
050: returnPlugin = Config.getCurrent().getValue(section,
051: key + CONFIG_RESPONSE_PLUGIN);
052: } catch (Exception ignored) {
053: }
054: LogManager.sendDebug(this .getClass().getName() + "::init: "
055: + section + "/response_plugin = " + returnPlugin);
056: try {
057: if (returnPlugin != null) {
058: this .setCheckReturnPlugin(returnPlugin);
059: }
060: } catch (Exception pex) {
061: throw new IntegrationException(this .getClass().getName()
062: + "::init: wrong plugin '" + returnPlugin
063: + "' for query '" + section + "/" + this .getName()
064: + "': " + pex);
065: }
066: }
067:
068: public String getConfigKey() {
069: return CONFIG_OPERATION;
070: }
071:
072: public GenericFieldDefinition getEmptyFieldDefinition() {
073: return new FieldDefinition();
074: }
075:
076: public void setValue(String value) {
077: super .setValue(value);
078: this .setType(this .getDefaultType());
079: }
080:
081: public String getValue(BusinessObjectProperties parameters)
082: throws IntegrationException {
083: String theQuery = new String(this .getValue());
084: int count = 0;
085: for (int i = 0; i < getFields().size(); i++) {
086: FieldDefinition field = (FieldDefinition) this
087: .getFieldAt(i);
088: try {
089: if (field.isArrayType()) {
090: String pattern = "(";
091: if (field.getType() == field.INT_ARRAY) {
092: Integer[] str = (Integer[]) parameters
093: .get(field.getName());
094: for (int j = 0; j < str.length; j++) {
095: pattern += "'" + str[j] + "'";
096: if (j < str.length - 1)
097: pattern += ",";
098: }
099: } else if (field.getType() == field.STRING_ARRAY) {
100: String[] str = (String[]) parameters.get(field
101: .getName());
102: for (int j = 0; j < str.length; j++) {
103: pattern += "'" + str[j] + "'";
104: if (j < str.length - 1)
105: pattern += ",";
106: }
107: } else
108: throw new Exception("Not a valid array type: "
109: + field.getType());
110: pattern += ")";
111: theQuery = com.jat.util.StringUtil.replace(
112: theQuery, "?", pattern, i + 1 - (count++));
113: }
114: } catch (Exception ex) {
115: throw new IntegrationException(this .getClass()
116: .getName()
117: + "::getValue: exception for field '"
118: + field.getName() + "': " + ex);
119: }
120: }
121: LogManager.sendDebug(this .getClass().getName()
122: + "::getValue: query '" + this .getName()
123: + "' substitution: " + theQuery);
124: return theQuery;
125: }
126:
127: public String getReturnPlugin() {
128: return this .getProperties().getProperty("response plugin");
129: }
130:
131: public void setCheckReturnPlugin(String plugin)
132: throws ClassNotFoundException {
133: Class.forName(plugin);
134: this .getProperties().setProperty("response plugin", plugin);
135: }
136:
137: public String getType() {
138: String type = this .getProperties().getProperty("type");
139: if (type == null)
140: type = this .getDefaultType();
141: this .getProperties().setProperty("type", type);
142: return type;
143: }
144:
145: public boolean isType(String type) {
146: return this .getType().equalsIgnoreCase(type);
147: }
148:
149: public void setType(String type) {
150: this .getProperties().setProperty("type", type);
151: }
152:
153: public String toString() {
154: String ret = "[NAME="
155: + this .getName()
156: + "]"
157: + " [VALUE="
158: + this .getValue()
159: + "]"
160: + " [TYPE="
161: + this .getType()
162: + "]"
163: + " [RESPONSE_PLUGIN="
164: + (this .getReturnPlugin() != null ? this
165: .getReturnPlugin() : "null") + "]"
166: + " [FIELDS={";
167: if (this .getFields() != null) {
168: for (int i = 0; i < this .getFields().size(); i++) {
169: ret += this .getFields().elementAt(i).toString();
170: if (i < this .getFields().size() - 1)
171: ret += ";";
172: }
173: }
174: ret += "}]";
175: return ret;
176: }
177:
178: public BusinessObjectProperties checkResponse(
179: BusinessObjectProperties properties)
180: throws IntegrationException {
181: try {
182: if (this .getReturnPlugin() != null) {
183: return ((CheckReturnPlugin) Class.forName(
184: this .getReturnPlugin()).newInstance())
185: .check(properties);
186: }
187: } catch (Exception ex) {
188: throw new IntegrationException(this .getClass()
189: + ":: checkResponse: exception: " + ex);
190: }
191: return properties;
192: }
193:
194: public String getDefaultType() {
195: String type = null;
196: String q = new String(this .getValue().toLowerCase());
197: if (q.startsWith(SELECT_TYPE))
198: type = SELECT_TYPE;
199: else if (q.startsWith(UPDATE_TYPE))
200: type = UPDATE_TYPE;
201: else if (q.startsWith(INSERT_TYPE))
202: type = INSERT_TYPE;
203: else
204: type = PROCEDURE_TYPE;
205: LogManager.sendDebug(this .getClass().getName()
206: + "::setDefaultType: default type for query '"
207: + this .getValue() + " is '" + type + "'");
208: return type;
209: }
210: }
|