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 java.io.Serializable;
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.Iterator;
022:
023: /**
024: * A ProcedureDescriptor contains information that is common for all types
025: * of procedures/functions that are used to handle the persistence operations.
026: * <br>
027: * Note: Be careful when use ProcedureDescriptor variables or caching
028: * ProcedureDescriptor 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: ProcedureDescriptor.java,v 1.7.2.1 2005/12/21 22:26:10 tomdz Exp $
033: */
034: public abstract class ProcedureDescriptor extends DescriptorBase
035: implements Serializable {
036: private static final long serialVersionUID = -8228331122289787173L;
037: //---------------------------------------------------------------
038: /**
039: * The the name of the procedure/function to invoke.
040: */
041: private String name;
042:
043: //---------------------------------------------------------------
044: /**
045: * The the field descriptor that will receive the return value from the procedure/function...
046: */
047: private FieldDescriptor returnValueFieldRef;
048:
049: //---------------------------------------------------------------
050: /**
051: * The class descriptor that this object is related to.
052: */
053: private ClassDescriptor classDescriptor;
054:
055: //---------------------------------------------------------------
056: /**
057: * The argument descriptor lists.
058: */
059: private ArrayList arguments = new ArrayList();
060:
061: //---------------------------------------------------------------
062: /**
063: * Constructor declaration
064: */
065: public ProcedureDescriptor(ClassDescriptor classDescriptor,
066: String name) {
067: this .classDescriptor = classDescriptor;
068: this .name = name;
069: }
070:
071: //---------------------------------------------------------------
072: /**
073: * Retrieve the the name of the procedure/function to invoke.
074: *
075: * @return The current value
076: */
077: public final String getName() {
078: return this .name;
079: }
080:
081: //---------------------------------------------------------------
082: /**
083: * Change the field descriptor that will receive the return value
084: * from the procedure/function..
085: *
086: * @param fieldName the name of the field that will receive the
087: * return value from the procedure/function.
088: */
089: public final void setReturnValueFieldRef(String fieldName) {
090: this .returnValueFieldRef = this .getClassDescriptor()
091: .getFieldDescriptorByName(fieldName);
092: }
093:
094: //---------------------------------------------------------------
095: /**
096: * Change the the field descriptor that will receive the return
097: * value from the procedure/function...
098: *
099: * @param fieldDescriptor the field descriptor that will receive the
100: * return value from the procedure/function.
101: */
102: public final void setReturnValueFieldRef(
103: FieldDescriptor fieldDescriptor) {
104: this .returnValueFieldRef = fieldDescriptor;
105: }
106:
107: //---------------------------------------------------------------
108: /**
109: * Retrieve the field descriptor that will receive the return value
110: * from the procedure/function...
111: *
112: * @return The current value
113: */
114: public final FieldDescriptor getReturnValueFieldRef() {
115: return this .returnValueFieldRef;
116: }
117:
118: //---------------------------------------------------------------
119: /**
120: * Is there a return value for this procedure?
121: *
122: * @return <code>true</code> if there is a return value for this
123: * procedure.
124: */
125: public final boolean hasReturnValue() {
126: return (this .returnValueFieldRef != null);
127: }
128:
129: //---------------------------------------------------------------
130: /**
131: * Does this procedure return any values to the 'caller'?
132: *
133: * @return <code>true</code> if the procedure returns at least 1
134: * value that is returned to the caller.
135: */
136: public final boolean hasReturnValues() {
137: if (this .hasReturnValue()) {
138: return true;
139: } else {
140: // TODO: We may be able to 'pre-calculate' the results
141: // of this loop by just checking arguments as they are added
142: // The only problem is that the 'isReturnedbyProcedure' property
143: // can be modified once the argument is added to this procedure.
144: // If that occurs, then 'pre-calculated' results will be inacccurate.
145: Iterator iter = this .getArguments().iterator();
146: while (iter.hasNext()) {
147: ArgumentDescriptor arg = (ArgumentDescriptor) iter
148: .next();
149: if (arg.getIsReturnedByProcedure()) {
150: return true;
151: }
152: }
153: }
154: return false;
155: }
156:
157: //---------------------------------------------------------------
158: /**
159: * Retrieve the name of the field descriptor that will receive the
160: * return value from the procedure/function...
161: *
162: * @return The current value
163: */
164: public final String getReturnValueFieldRefName() {
165: if (this .returnValueFieldRef == null) {
166: return null;
167: } else {
168: return this .returnValueFieldRef.getAttributeName();
169: }
170: }
171:
172: //---------------------------------------------------------------
173: /**
174: * Retrieve the class descriptor that this object is related to.
175: *
176: * @return The current value
177: */
178: public final ClassDescriptor getClassDescriptor() {
179: return this .classDescriptor;
180: }
181:
182: /*
183: * @see XmlCapable#toXML()
184: */
185: public abstract String toXML();
186:
187: //---------------------------------------------------------------
188: /**
189: * Add an argument
190: */
191: protected void addArgument(ArgumentDescriptor argument) {
192: this .arguments.add(argument);
193: }
194:
195: //---------------------------------------------------------------
196: /**
197: * Set up arguments for each FieldDescriptor in an array.
198: */
199: protected void addArguments(FieldDescriptor field[]) {
200: for (int i = 0; i < field.length; i++) {
201: ArgumentDescriptor arg = new ArgumentDescriptor(this );
202: arg.setValue(field[i].getAttributeName(), false);
203: this .addArgument(arg);
204: }
205: }
206:
207: //---------------------------------------------------------------
208: /**
209: * Get the argument descriptors for this procedure.
210: */
211: public final Collection getArguments() {
212: return this .arguments;
213: }
214:
215: //---------------------------------------------------------------
216: /**
217: * Retrieves the number of arguments that are passed to the
218: * procedure that this descriptor represents.
219: * <p>
220: * Note: The value returned by this method does not reflect
221: * the presence of any return value for the procedure
222: */
223: public final int getArgumentCount() {
224: return this.arguments.size();
225: }
226: }
|