001: package com.jat.integration;
002:
003: import java.util.Enumeration;
004: import java.util.Vector;
005:
006: import com.jat.business.BusinessObjectProperties;
007: import com.jat.core.config.Config;
008: import com.jat.core.log.LogManager;
009: import java.util.Properties;
010:
011: /**
012: * <p>Title: JAT</p>
013: * <p>Description: This class represents a configured operation of your Data Source.
014: * It is composed of<ul>
015: * <li>the operation name,</li>
016: * <li>the operation value,</li>
017: * <li>a set of fields
018: * (see {@link com.jat.integration.GenericFieldDefinition})</li>
019: * </ul>
020: * </p>
021: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
022: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
023: * @author stf
024: * @version 1.0
025: * @since 1.2
026: * @see com.jat.integration.GenericDataSource
027: * @see com.jat.integration.GenericFieldDefinition
028: */
029:
030: public abstract class GenericOperationDefinition {
031:
032: /**
033: * Get further properties of the operation
034: * @return properties
035: */
036: public final Properties getProperties() {
037: return properties;
038: }
039:
040: public final void setProperties(Properties properties) {
041: this .properties = properties;
042: }
043:
044: /**
045: * Use this method to get your (@link com.jat.integration.GenericOperationDefinition) instance
046: * @return a (@link com.jat.integration.GenericOperationDefinition) instance
047: */
048: public abstract GenericFieldDefinition getEmptyFieldDefinition();
049:
050: /**
051: * The confiured operation key
052: */
053: public static String CONFIG_OPERATION = "operation";
054:
055: /**
056: * The configured operation name key
057: */
058: public static final String CONFIG_NAME = ".name";
059:
060: /**
061: * The configured operation name value
062: */
063: public static final String CONFIG_VALUE = ".value";
064:
065: /**
066: * The pattern used in value substitution
067: */
068: public static final String PATTERN_SUBSTITUTION = "?";
069:
070: /**
071: * Return the key of the configured operation of the Data Source
072: * @return the operation configuration key
073: */
074: public String getConfigKey() {
075: return CONFIG_OPERATION;
076: }
077:
078: /**
079: * This method initialize the configured operation
080: * @param section the section into config file
081: * @param key the key into config file used to load operation
082: * @throws IntegrationException if any exception occours
083: */
084: public void init(String section, String key)
085: throws IntegrationException {
086: try {
087: this .setName(Config.getCurrent().getValue(section,
088: key + CONFIG_NAME));
089: LogManager.sendDebug(this .getClass().getName() + "::init: "
090: + section + "/Name = " + this .name);
091: this .setValue(Config.getCurrent().getValue(section,
092: key + CONFIG_VALUE));
093: LogManager.sendDebug(this .getClass().getName() + "::init: "
094: + section + "/Value = " + this .value);
095: this .initFields(section, key);
096: } catch (Exception ex) {
097: throw new IntegrationException(ex);
098: }
099: }
100:
101: /**
102: * This methos initalize fields of this operation
103: * @param section the section into config file
104: * @param key the key into config file used to load fields
105: * @throws IntegrationException if any exception occours
106: */
107: public void initFields(String section, String key)
108: throws IntegrationException {
109: try {
110: fields = new Vector();
111: for (Enumeration e = Config.getCurrent().getSubKeys(
112: section, key + GenericFieldDefinition.CONFIG_FIELD)
113: .elements(); e.hasMoreElements();) {
114: GenericFieldDefinition fd = getEmptyFieldDefinition();
115: fd.init(section, (String) e.nextElement(), this .name);
116: this .addField(fd);
117: }
118: } catch (Exception ex) {
119: throw new IntegrationException(this .getClass().getName()
120: + "::initFileds: exception: " + ex);
121: }
122: }
123:
124: /**
125: * Get the name of the operation
126: * @return the operation name
127: */
128: public String getName() {
129: return this .name;
130: }
131:
132: public void setName(String name) {
133: this .name = name;
134: }
135:
136: /**
137: * Get the value of the operation
138: * @return the operation value
139: */
140: public String getValue() {
141: return this .value;
142: }
143:
144: public void setValue(String value) {
145: this .value = value;
146: }
147:
148: /**
149: * Get the value of operation applying the pattern substitution of fields from parameters
150: * @param parameters the parameters for pattern substitution
151: * @return the operation value with pattern substitution
152: * @throws IntegrationException if any exception occours
153: */
154: public String getValue(BusinessObjectProperties parameters)
155: throws IntegrationException {
156: String operation = new String(this .getValue());
157: Vector fields = this .getFields();
158: if (fields == null || fields.size() == 0)
159: return operation;
160: for (Enumeration e = fields.elements(); e.hasMoreElements();) {
161: GenericFieldDefinition field = (GenericFieldDefinition) e
162: .nextElement();
163: if (operation.indexOf(getPatternSubstitution()) < 0)
164: throw new IntegrationException(
165: "Not all parameters bound in query '" + name
166: + "'");
167: String value = "" + parameters.get(field.getName());
168: if (value == null || value.equalsIgnoreCase("null"))
169: throw new IntegrationException("Field parameter '"
170: + field + "' cannot be null");
171: operation = com.jat.util.StringUtil.replace(operation,
172: getPatternSubstitution(), value);
173: }
174: LogManager.sendDebug(this .getClass().getName()
175: + "::getValue: query '" + name + "': " + operation);
176: return operation;
177: }
178:
179: /**
180: * Get the pattern for value's substitution
181: * @return the pattern for substituion
182: */
183: public String getPatternSubstitution() {
184: return PATTERN_SUBSTITUTION;
185: }
186:
187: /**
188: * Get fields ({@link com.jat.integration.GenericFieldDefinition})
189: * @return the iterator of fields
190: */
191: public Enumeration fields() {
192: return this .fields.elements();
193: }
194:
195: public int getFieldsSize() {
196: return this .fields.size();
197: }
198:
199: /**
200: * Get fields ({@link com.jat.integration.GenericFieldDefinition})
201: * @return the list of fields
202: */
203: public Vector getFields() {
204: return this .fields;
205: }
206:
207: public void setFields(Vector fields) {
208: this .fields = fields;
209: }
210:
211: /**
212: * Add a field to the list
213: * @param field the field
214: */
215: public void addField(GenericFieldDefinition field) {
216: this .fields.addElement(field);
217: }
218:
219: public boolean removeField(GenericFieldDefinition field) {
220: return this .fields.remove(field);
221: }
222:
223: /**
224: * Get a field ({@link com.jat.integration.GenericFieldDefinition}) by name
225: * @param name the configured field name
226: * @return the field if exists, null otherwise
227: */
228: public GenericFieldDefinition getField(String name) {
229: for (Enumeration e = this .getFields().elements(); e
230: .hasMoreElements();) {
231: GenericFieldDefinition field = (GenericFieldDefinition) e
232: .nextElement();
233: if (field.getName().equalsIgnoreCase(name))
234: return field;
235: }
236: return null;
237: }
238:
239: /**
240: * Get a field ({@link com.jat.integration.GenericFieldDefinition}) by position
241: * @param index the position
242: * @return the field
243: */
244: public GenericFieldDefinition getFieldAt(int index) {
245: return (GenericFieldDefinition) this .getFields().elementAt(
246: index);
247: }
248:
249: /**
250: * Get the position of a field into the field list
251: * @param field the configured field name
252: * @return the position of the field
253: */
254: public int indexOfField(GenericFieldDefinition field) {
255: return this .getFields().indexOf(field);
256: }
257:
258: public String toString() {
259: String ret = "[NAME=" + this .getName() + "]" + " [VALUE="
260: + this .getValue() + "]" + " [FIELDS={";
261: if (this .getFields() != null) {
262: for (int i = 0; i < this .getFields().size(); i++) {
263: ret += this .getFields().elementAt(i);
264: if (i < this .getFields().size() - 1)
265: ret += ";";
266: }
267: }
268: ret += "}]";
269: return ret;
270: }
271:
272: private Properties properties = new Properties();
273: private Vector fields = new Vector();
274: private String name;
275: private String value;
276:
277: /** @link dependency
278: * @stereotype use*/
279: /*# GenericFieldDefinition lnkGenericFieldDefinition; */
280: }
|