001: package org.apache.ojb.broker.metadata;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.commons.lang.builder.ToStringBuilder;
019: import org.apache.commons.lang.builder.ToStringStyle;
020: import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
021: import java.io.Serializable;
022:
023: /**
024: * An ArgumentDescriptor contains information that defines a single argument
025: * that is passed to a procedure/function.
026: * <br>
027: * Note: Be careful when use ArgumentDescriptor variables or caching
028: * ArgumentDescriptor instances, because instances could become invalid
029: * during runtime (see {@link MetadataManager}).
030: *
031: * @author <a href="mailto:rongallagher@bellsouth.net">Ron Gallagher<a>
032: * @version $Id: ArgumentDescriptor.java,v 1.8.2.1 2005/12/21 22:26:10 tomdz Exp $
033: */
034: public final class ArgumentDescriptor extends DescriptorBase implements
035: XmlCapable, Serializable {
036: private static final long serialVersionUID = 5205304260023247711L;
037:
038: private static final int SOURCE_NULL = 0;
039: private static final int SOURCE_FIELD = 1;
040: private static final int SOURCE_VALUE = 2;
041: private int fieldSource = SOURCE_NULL;
042: private String constantValue = null;
043: private String fieldRefName = null;
044: private boolean returnedByProcedure = false;
045:
046: //---------------------------------------------------------------
047: /**
048: * The procedure descriptor that this object is related to.
049: */
050: private ProcedureDescriptor procedureDescriptor;
051:
052: //---------------------------------------------------------------
053: /**
054: * Constructor declaration. By default, this object will be configured
055: * so that the value returned by {@link #fieldSource} will be null. To
056: * change this, call either version of the setValue method.
057: *
058: * @see #setValue()
059: * @see #setValue(String)
060: * @see #setValue(String,boolean)
061: */
062: public ArgumentDescriptor(ProcedureDescriptor procedureDescriptor) {
063: this .procedureDescriptor = procedureDescriptor;
064: this .setValue();
065: }
066:
067: /**
068: * Sets up this object to represent a null value.
069: */
070: public void setValue() {
071: this .fieldSource = SOURCE_NULL;
072: this .fieldRefName = null;
073: this .returnedByProcedure = false;
074: this .constantValue = null;
075: }
076:
077: /**
078: * Sets up this object to represent a value that is derived from a field
079: * in the corresponding class-descriptor.
080: * <p>
081: * If the value of <code>fieldRefName</code> is blank or refers to an
082: * invalid field reference, then the value of the corresponding argument
083: * will be set to null. In this case, {@link #getIsReturnedByProcedure}
084: * will be set to <code>false</code>, regardless of the value of the
085: * <code>returnedByProcedure</code> argument.
086: *
087: * @param fieldRefName the name of the field reference that provides the
088: * value of this argument.
089: * @param returnedByProcedure indicates that the value of the argument
090: * is returned by the procedure that is invoked.
091: */
092: public void setValue(String fieldRefName,
093: boolean returnedByProcedure) {
094: this .fieldSource = SOURCE_FIELD;
095: this .fieldRefName = fieldRefName;
096: this .returnedByProcedure = returnedByProcedure;
097: this .constantValue = null;
098:
099: // If the field reference is not valid, then disregard the value
100: // of the returnedByProcedure argument.
101: if (this .getFieldRef() == null) {
102: this .returnedByProcedure = false;
103: }
104:
105: // If the field reference is not valid, then disregard the value
106: // of the returnedByProcedure argument.
107: if (this .getFieldRef() == null) {
108: this .returnedByProcedure = false;
109: }
110: }
111:
112: /**
113: * Sets up this object to represent an argument that will be set to a
114: * constant value.
115: *
116: * @param constantValue the constant value.
117: */
118: public void setValue(String constantValue) {
119: this .fieldSource = SOURCE_VALUE;
120: this .fieldRefName = null;
121: this .returnedByProcedure = false;
122: this .constantValue = constantValue;
123: }
124:
125: public boolean getIsReturnedByProcedure() {
126: return this .returnedByProcedure;
127: }
128:
129: public Object getValue(Object objekt) {
130: switch (this .fieldSource) {
131: case SOURCE_FIELD:
132: if (objekt == null) {
133: return null;
134: } else {
135: FieldDescriptor fd = this .getFieldRef();
136: Object value = null;
137: FieldConversion conversion = null;
138: if (fd != null) {
139: conversion = fd.getFieldConversion();
140: value = fd.getPersistentField().get(objekt);
141: if (conversion != null) {
142: value = conversion.javaToSql(value);
143: }
144: }
145: return value;
146: }
147: case SOURCE_VALUE:
148: return this .constantValue;
149: case SOURCE_NULL:
150: return null;
151: default:
152: return null;
153: }
154: }
155:
156: public void saveValue(Object objekt, Object value) {
157: if ((this .fieldSource == SOURCE_FIELD)
158: && (this .returnedByProcedure)) {
159: FieldDescriptor fd = this .getFieldRef();
160: FieldConversion conversion = null;
161: if (fd != null) {
162: conversion = fd.getFieldConversion();
163: if (conversion == null) {
164: fd.getPersistentField().set(objekt, value);
165: } else {
166: fd.getPersistentField().set(objekt,
167: conversion.sqlToJava(value));
168: }
169: }
170: }
171: }
172:
173: //---------------------------------------------------------------
174: /**
175: * Retrieve the field descriptor that this argument is related to.
176: * <p>
177: * This reference can only be set via the {@link #setValue(String,boolean)}
178: * method.
179: * @return The current value
180: */
181: public final FieldDescriptor getFieldRef() {
182: if (this .fieldSource == SOURCE_FIELD) {
183: return this .getProcedureDescriptor().getClassDescriptor()
184: .getFieldDescriptorByName(this .fieldRefName);
185: } else {
186: return null;
187: }
188: }
189:
190: /**
191: * Retrieve the jdbc type for the field descriptor that is related
192: * to this argument.
193: */
194: public final int getJdbcType() {
195: switch (this .fieldSource) {
196: case SOURCE_FIELD:
197: return this .getFieldRef().getJdbcType().getType();
198: case SOURCE_NULL:
199: return java.sql.Types.NULL;
200: case SOURCE_VALUE:
201: return java.sql.Types.VARCHAR;
202: default:
203: return java.sql.Types.NULL;
204: }
205: }
206:
207: //---------------------------------------------------------------
208: /**
209: * Retrieve the procedure descriptor that this object is related to.
210: *
211: * @return The current value
212: */
213: public final ProcedureDescriptor getProcedureDescriptor() {
214: return this .procedureDescriptor;
215: }
216:
217: /*
218: * @see XmlCapable#toXML()
219: */
220: public String toXML() {
221: String eol = System.getProperty("line.separator");
222: RepositoryTags tags = RepositoryTags.getInstance();
223:
224: // The result
225: String result = " ";
226:
227: switch (this .fieldSource) {
228: case SOURCE_FIELD:
229: result += " "
230: + tags
231: .getOpeningTagNonClosingById(RUNTIME_ARGUMENT);
232: result += " "
233: + tags.getAttribute(FIELD_REF, this .fieldRefName);
234: result += " "
235: + tags.getAttribute(RETURN, String
236: .valueOf(this .returnedByProcedure));
237: result += "/>";
238: break;
239: case SOURCE_VALUE:
240: result += " "
241: + tags
242: .getOpeningTagNonClosingById(CONSTANT_ARGUMENT);
243: result += " "
244: + tags.getAttribute(VALUE, this .constantValue);
245: result += "/>";
246: break;
247: case SOURCE_NULL:
248: result += " "
249: + tags
250: .getOpeningTagNonClosingById(RUNTIME_ARGUMENT);
251: result += "/>";
252: break;
253: default:
254: break;
255: }
256:
257: // Return the result.
258: return (result + eol);
259: }
260:
261: //---------------------------------------------------------------
262: /**
263: * Provide a string representation of this object
264: *
265: * @return a string representation of this object
266: */
267: public String toString() {
268: ToStringBuilder buf = new ToStringBuilder(this ,
269: ToStringStyle.MULTI_LINE_STYLE);
270: switch (this .fieldSource) {
271: case SOURCE_FIELD: {
272: buf.append("fieldRefName", this .fieldRefName);
273: buf.append("returnedByProcedure", this .returnedByProcedure);
274: break;
275: }
276: case SOURCE_NULL: {
277: break;
278: }
279: case SOURCE_VALUE: {
280: buf.append("constantValue", this.constantValue);
281: break;
282: }
283: }
284: return buf.toString();
285: }
286: }
|