View Javadoc

1   package org.apache.ojb.broker.metadata;
2   
3   /* Copyright 2003-2005 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  import java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Iterator;
22  
23  /**
24   * A ProcedureDescriptor contains information that is common for all types
25   * of procedures/functions that are used to handle the persistence operations.
26   * <br>
27   * Note: Be careful when use ProcedureDescriptor variables or caching
28   * ProcedureDescriptor instances, because instances could become invalid
29   * during runtime (see {@link MetadataManager}).
30   *
31   * @author <a href="mailto:rongallagher@bellsouth.net">Ron Gallagher<a>
32   * @version $Id: ProcedureDescriptor.java,v 1.1 2007-08-24 22:17:29 ewestfal Exp $
33   */
34  public abstract class ProcedureDescriptor extends DescriptorBase implements Serializable
35  {
36  	private static final long serialVersionUID = -8228331122289787173L;
37      //---------------------------------------------------------------
38      /**
39       * The the name of the procedure/function to invoke.
40       */
41      private String name;
42  
43      //---------------------------------------------------------------
44      /**
45       * The the field descriptor that will receive the return value from the procedure/function...
46       */
47      private FieldDescriptor returnValueFieldRef;
48  
49      //---------------------------------------------------------------
50      /**
51       * The class descriptor that this object is related to.
52       */
53      private ClassDescriptor classDescriptor;
54  
55      //---------------------------------------------------------------
56      /**
57       * The argument descriptor lists.
58       */
59      private ArrayList arguments = new ArrayList();
60  
61      //---------------------------------------------------------------
62      /**
63       * Constructor declaration
64       */
65      public ProcedureDescriptor(ClassDescriptor classDescriptor, String name)
66      {
67          this.classDescriptor = classDescriptor;
68          this.name = name;
69      }
70  
71      //---------------------------------------------------------------
72      /**
73       * Retrieve the the name of the procedure/function to invoke.
74       *
75       * @return The current value
76       */
77      public final String getName()
78      {
79          return this.name;
80      }
81  
82      //---------------------------------------------------------------
83      /**
84       * Change the field descriptor that will receive the return value
85       * from the procedure/function..
86       *
87       * @param fieldName the name of the field that will receive the
88       * return value from the procedure/function.
89       */
90      public final void setReturnValueFieldRef(String fieldName)
91      {
92          this.returnValueFieldRef = this.getClassDescriptor().getFieldDescriptorByName(fieldName);
93      }
94  
95      //---------------------------------------------------------------
96      /**
97       * Change the the field descriptor that will receive the return
98       * value from the procedure/function...
99       *
100      * @param fieldDescriptor the field descriptor that will receive the
101      * return value from the procedure/function.
102      */
103     public final void setReturnValueFieldRef(FieldDescriptor fieldDescriptor)
104     {
105         this.returnValueFieldRef = fieldDescriptor;
106     }
107 
108     //---------------------------------------------------------------
109     /**
110      * Retrieve the field descriptor that will receive the return value
111      * from the procedure/function...
112      *
113      * @return The current value
114      */
115     public final FieldDescriptor getReturnValueFieldRef()
116     {
117         return this.returnValueFieldRef;
118     }
119 
120     //---------------------------------------------------------------
121     /**
122      * Is there a return value for this procedure?
123      *
124      * @return <code>true</code> if there is a return value for this
125      * procedure.
126      */
127     public final boolean hasReturnValue()
128     {
129         return (this.returnValueFieldRef != null);
130     }
131 
132     //---------------------------------------------------------------
133     /**
134      * Does this procedure return any values to the 'caller'?
135      *
136      * @return <code>true</code> if the procedure returns at least 1
137      * value that is returned to the caller.
138      */
139     public final boolean hasReturnValues()
140     {
141         if (this.hasReturnValue())
142         {
143             return true;
144         }
145         else
146         {
147             // TODO: We may be able to 'pre-calculate' the results
148             // of this loop by just checking arguments as they are added
149             // The only problem is that the 'isReturnedbyProcedure' property
150             // can be modified once the argument is added to this procedure.
151             // If that occurs, then 'pre-calculated' results will be inacccurate.
152             Iterator iter = this.getArguments().iterator();
153             while (iter.hasNext())
154             {
155                 ArgumentDescriptor arg = (ArgumentDescriptor) iter.next();
156                 if (arg.getIsReturnedByProcedure())
157                 {
158                     return true;
159                 }
160             }
161         }
162         return false;
163     }
164 
165     //---------------------------------------------------------------
166     /**
167      * Retrieve the name of the field descriptor that will receive the
168      * return value from the procedure/function...
169      *
170      * @return The current value
171      */
172     public final String getReturnValueFieldRefName()
173     {
174         if (this.returnValueFieldRef == null)
175         {
176             return null;
177         }
178         else
179         {
180             return this.returnValueFieldRef.getAttributeName();
181         }
182     }
183 
184     //---------------------------------------------------------------
185     /**
186      * Retrieve the class descriptor that this object is related to.
187      *
188      * @return The current value
189      */
190     public final ClassDescriptor getClassDescriptor()
191     {
192         return this.classDescriptor;
193     }
194 
195     /*
196      * @see XmlCapable#toXML()
197      */
198     public abstract String toXML();
199 
200     //---------------------------------------------------------------
201     /**
202      * Add an argument
203      */
204     protected void addArgument(ArgumentDescriptor argument)
205     {
206         this.arguments.add(argument);
207     }
208 
209     //---------------------------------------------------------------
210     /**
211      * Set up arguments for each FieldDescriptor in an array.
212      */
213     protected void addArguments(FieldDescriptor field[])
214     {
215         for (int i = 0; i < field.length; i++)
216         {
217             ArgumentDescriptor arg = new ArgumentDescriptor(this);
218             arg.setValue(field[i].getAttributeName(), false);
219             this.addArgument(arg);
220         }
221     }
222 
223     //---------------------------------------------------------------
224     /**
225      * Get the argument descriptors for this procedure.
226      */
227     public final Collection getArguments()
228     {
229         return this.arguments;
230     }
231 
232     //---------------------------------------------------------------
233     /**
234      * Retrieves the number of arguments that are passed to the
235      * procedure that this descriptor represents.
236      * <p>
237      * Note: The value returned by this method does not reflect
238      * the presence of any return value for the procedure
239      */
240     public final int getArgumentCount()
241     {
242         return this.arguments.size();
243     }
244 }