KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.map;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.cayenne.util.CayenneMapEntry;
29 import org.apache.cayenne.util.Util;
30 import org.apache.cayenne.util.XMLEncoder;
31 import org.apache.cayenne.util.XMLSerializable;
32
33 /**
34  * A mapping descriptor for a database stored procedure.
35  *
36  * @author Andrus Adamchik
37  */

38 public class Procedure implements CayenneMapEntry, XMLSerializable, Serializable JavaDoc {
39
40     protected String JavaDoc name;
41     protected DataMap dataMap;
42
43     protected String JavaDoc catalog;
44     protected String JavaDoc schema;
45     protected boolean returningValue;
46     protected List JavaDoc callParameters = new ArrayList JavaDoc();
47
48     /**
49      * Creates an unnamed procedure object.
50      */

51     public Procedure() {
52     }
53
54     /**
55      * Creates a named Procedure object.
56      */

57     public Procedure(String JavaDoc name) {
58         setName(name);
59     }
60
61     public String JavaDoc getName() {
62         return name;
63     }
64
65     public void setName(String JavaDoc name) {
66         this.name = name;
67     }
68
69     public Object JavaDoc getParent() {
70         return getDataMap();
71     }
72
73     public void setParent(Object JavaDoc parent) {
74         if (parent != null && !(parent instanceof DataMap)) {
75             throw new IllegalArgumentException JavaDoc("Expected null or DataMap, got: " + parent);
76         }
77
78         setDataMap((DataMap) parent);
79     }
80
81     /**
82      * Prints itself as XML to the provided XMLEncoder.
83      *
84      * @since 1.1
85      */

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

119     public String JavaDoc getFullyQualifiedName() {
120         return (schema != null) ? schema + '.' + getName() : getName();
121     }
122
123     /**
124      * @return parent DataMap of this entity.
125      */

126     public DataMap getDataMap() {
127         return dataMap;
128     }
129
130     /**
131      * Sets parent DataMap of this entity.
132      */

133     public void setDataMap(DataMap dataMap) {
134         this.dataMap = dataMap;
135     }
136
137     public void setCallParameters(List JavaDoc parameters) {
138         clearCallParameters();
139         callParameters.addAll(parameters);
140     }
141
142     /**
143      * Adds new call parameter to the stored procedure. Also sets <code>param</code>'s
144      * parent to be this procedure.
145      */

146     public void addCallParameter(ProcedureParameter param) {
147         if (param.getName() == null) {
148             throw new IllegalArgumentException JavaDoc("Attempt to add unnamed parameter.");
149         }
150
151         if (callParameters.contains(param)) {
152             throw new IllegalArgumentException JavaDoc(
153                     "Attempt to add the same parameter more than once:" + param);
154         }
155
156         param.setProcedure(this);
157         callParameters.add(param);
158     }
159
160     /** Removes a named call parameter. */
161     public void removeCallParameter(String JavaDoc name) {
162         for (int i = 0; i < callParameters.size(); i++) {
163             ProcedureParameter nextParam = (ProcedureParameter) callParameters.get(i);
164             if (name.equals(nextParam.getName())) {
165                 callParameters.remove(i);
166                 break;
167             }
168         }
169     }
170
171     public void clearCallParameters() {
172         callParameters.clear();
173     }
174
175     /**
176      * Returns an unmodifiable list of call parameters.
177      */

178     public List JavaDoc getCallParameters() {
179         return Collections.unmodifiableList(callParameters);
180     }
181
182     /**
183      * Returns a list of OUT and INOUT call parameters. If procedure has a return value,
184      * it will also be included as a call parameter.
185      */

186     public List JavaDoc getCallOutParameters() {
187         List JavaDoc outParams = new ArrayList JavaDoc(callParameters.size());
188         Iterator JavaDoc it = callParameters.iterator();
189         while (it.hasNext()) {
190             ProcedureParameter param = (ProcedureParameter) it.next();
191             if (param.isOutParam()) {
192                 outParams.add(param);
193             }
194         }
195
196         return outParams;
197     }
198
199     /**
200      * Returns parameter describing the return value of the StoredProcedure, or null if
201      * procedure does not support return values. If procedure supports return parameters,
202      * its first parameter is always assumed to be a return result.
203      */

204     public ProcedureParameter getResultParam() {
205         // if procedure returns parameters, this must be the first parameter
206
// otherwise, return null
207
return (returningValue && callParameters.size() > 0)
208                 ? (ProcedureParameter) callParameters.get(0)
209                 : null;
210     }
211
212     /**
213      * Returns <code>true</code> if a stored procedure returns a value. The first
214      * parameter in a list of parameters will be assumed to be a descriptor of return
215      * value.
216      *
217      * @return boolean
218      */

219     public boolean isReturningValue() {
220         return returningValue;
221     }
222
223     public void setReturningValue(boolean returningValue) {
224         this.returningValue = returningValue;
225     }
226
227     public String JavaDoc getCatalog() {
228         return catalog;
229     }
230
231     public String JavaDoc getSchema() {
232         return schema;
233     }
234
235     /**
236      * Sets stored procedure's catalog.
237      */

238     public void setCatalog(String JavaDoc string) {
239         catalog = string;
240     }
241
242     /**
243      * Sets stored procedure's database schema.
244      */

245     public void setSchema(String JavaDoc string) {
246         schema = string;
247     }
248 }
249
Popular Tags