KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > map > Procedure


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.map;
57
58 import java.util.ArrayList JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import java.util.List JavaDoc;
61
62 import org.objectstyle.cayenne.util.Util;
63 import org.objectstyle.cayenne.util.XMLEncoder;
64
65 /**
66  * A mapping descriptor for a database stored procedure.
67  *
68  * @author Andrei Adamchik
69  */

70 public class Procedure extends MapObject {
71     protected String JavaDoc catalog;
72     protected String JavaDoc schema;
73     protected boolean returningValue;
74     protected List JavaDoc callParameters = new ArrayList JavaDoc();
75
76     /**
77      * Creates an unnamed procedure object.
78      */

79     public Procedure() {
80         super();
81     }
82
83     /**
84      * Creates a named Procedure object.
85      */

86     public Procedure(String JavaDoc name) {
87         super(name);
88     }
89
90     /**
91      * Prints itself as XML to the provided XMLEncoder.
92      *
93      * @since 1.1
94      */

95     public void encodeAsXML(XMLEncoder encoder) {
96         encoder.print("<procedure name=\"");
97         encoder.print(Util.encodeXmlAttribute(getName()));
98         encoder.print('\"');
99
100         if (getSchema() != null && getSchema().trim().length() > 0) {
101             encoder.print(" schema=\"");
102             encoder.print(getSchema().trim());
103             encoder.print('\"');
104         }
105
106         if (getCatalog() != null && getCatalog().trim().length() > 0) {
107             encoder.print(" catalog=\"");
108             encoder.print(getCatalog().trim());
109             encoder.print('\"');
110         }
111
112         if (isReturningValue()) {
113             encoder.print(" returningValue=\"true\"");
114         }
115
116         encoder.println('>');
117
118         encoder.indent(1);
119         encoder.print(getCallParameters());
120         encoder.indent(-1);
121         
122         encoder.println("</procedure>");
123     }
124
125     /**
126      * Returns procedure name including schema, if present.
127      */

128     public String JavaDoc getFullyQualifiedName() {
129         return (schema != null) ? schema + '.' + getName() : getName();
130     }
131
132     /**
133      * @return parent DataMap of this entity.
134      */

135     public DataMap getDataMap() {
136         return (DataMap) getParent();
137     }
138
139     /**
140      * Sets parent DataMap of this entity.
141      */

142     public void setDataMap(DataMap dataMap) {
143         this.setParent(dataMap);
144     }
145
146     public void setCallParameters(List JavaDoc parameters) {
147         clearCallParameters();
148         callParameters.addAll(parameters);
149     }
150
151     /**
152      * Adds new call parameter to the stored procedure. Also sets
153      * <code>param</code>'s parent to be this procedure.
154      */

155     public void addCallParameter(ProcedureParameter param) {
156         if (param.getName() == null) {
157             throw new IllegalArgumentException JavaDoc("Attempt to add unnamed parameter.");
158         }
159
160         if (callParameters.contains(param)) {
161             throw new IllegalArgumentException JavaDoc(
162                 "Attempt to add the same parameter more than once:" + param);
163         }
164
165         param.setProcedure(this);
166         callParameters.add(param);
167     }
168
169     /** Removes a named call parameter. */
170     public void removeCallParameter(String JavaDoc name) {
171         for (int i = 0; i < callParameters.size(); i++) {
172             ProcedureParameter nextParam = (ProcedureParameter) callParameters.get(i);
173             if (name.equals(nextParam.getName())) {
174                 callParameters.remove(i);
175                 break;
176             }
177         }
178     }
179
180     public void clearCallParameters() {
181         callParameters.clear();
182     }
183
184     /**
185      * Returns a list of call parameters.
186      *
187      * @return List
188      */

189     public List JavaDoc getCallParameters() {
190         return callParameters;
191     }
192
193     /**
194      * Returns a list of OUT and INOUT call parameters. If procedure has a
195      * return value, it will also be included as a call parameter.
196      */

197     public List JavaDoc getCallOutParameters() {
198         List JavaDoc outParams = new ArrayList JavaDoc(callParameters.size());
199         Iterator JavaDoc it = callParameters.iterator();
200         while (it.hasNext()) {
201             ProcedureParameter param = (ProcedureParameter) it.next();
202             if (param.isOutParam()) {
203                 outParams.add(param);
204             }
205         }
206
207         return outParams;
208     }
209
210     /**
211      * Returns parameter describing the return value of the StoredProcedure, or
212      * null if procedure does not support return values. If procedure supports return parameters,
213      * its first parameter is always assumed to be a return result.
214      */

215     public ProcedureParameter getResultParam() {
216         // if procedure returns parameters, this must be the first parameter
217
// otherwise, return null
218
return (returningValue && callParameters.size() > 0)
219             ? (ProcedureParameter) callParameters.get(0)
220             : null;
221     }
222
223     /**
224      * Returns <code>true</code> if a stored procedure returns a value.
225      * The first parameter in a list of parameters will be assumed to be
226      * a descriptor of return value.
227      *
228      * @return boolean
229      */

230     public boolean isReturningValue() {
231         return returningValue;
232     }
233
234     public void setReturningValue(boolean returningValue) {
235         this.returningValue = returningValue;
236     }
237
238     public String JavaDoc getCatalog() {
239         return catalog;
240     }
241
242     public String JavaDoc getSchema() {
243         return schema;
244     }
245
246     /**
247      * Sets stored procedure's catalog.
248      */

249     public void setCatalog(String JavaDoc string) {
250         catalog = string;
251     }
252
253     /**
254      * Sets stored procedure's database schema.
255      */

256     public void setSchema(String JavaDoc string) {
257         schema = string;
258     }
259 }
260
Popular Tags