KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 import org.apache.cayenne.dba.TypesMapping;
25 import org.apache.cayenne.util.CayenneMapEntry;
26 import org.apache.cayenne.util.Util;
27 import org.apache.cayenne.util.XMLEncoder;
28 import org.apache.cayenne.util.XMLSerializable;
29
30 /**
31  * A descriptor for the Procedure parameter.
32  *
33  * @author Andrus Adamchik
34  */

35 public class ProcedureParameter implements CayenneMapEntry, XMLSerializable, Serializable JavaDoc {
36
37     public static final int IN_OUT_PARAMETER = 3;
38     public static final int IN_PARAMETER = 1;
39     public static final int OUT_PARAMETER = 2;
40
41     protected String JavaDoc name;
42     protected Procedure procedure;
43
44     protected int direction = -1;
45
46     // The length of CHAR or VARCHAR or max num of digits for DECIMAL.
47
protected int maxLength = -1;
48
49     // The number of digits after period for DECIMAL.
50
protected int precision = -1;
51     protected int type = TypesMapping.NOT_DEFINED;
52
53     /**
54      * Creates unnamed ProcedureParameter.
55      */

56     public ProcedureParameter() {
57     }
58
59     public ProcedureParameter(String JavaDoc name) {
60         setName(name);
61     }
62
63     public ProcedureParameter(String JavaDoc name, int type, int direction) {
64         this(name);
65         setType(type);
66         setDirection(direction);
67     }
68
69     public String JavaDoc getName() {
70         return name;
71     }
72
73     public void setName(String JavaDoc name) {
74         this.name = name;
75     }
76
77     public Object JavaDoc getParent() {
78         return getProcedure();
79     }
80
81     public void setParent(Object JavaDoc parent) {
82         if (parent != null && !(parent instanceof Procedure)) {
83             throw new IllegalArgumentException JavaDoc("Expected null or Procedure, got: "
84                     + parent);
85         }
86
87         setProcedure((Procedure) parent);
88     }
89
90     /**
91      * Prints itself as XML to the provided PrintWriter.
92      *
93      * @since 1.1
94      */

95     public void encodeAsXML(XMLEncoder encoder) {
96         encoder.print("<procedure-parameter name=\""
97                 + Util.encodeXmlAttribute(getName())
98                 + '\"');
99
100         String JavaDoc type = TypesMapping.getSqlNameByType(getType());
101         if (type != null) {
102             encoder.print(" type=\"" + type + '\"');
103         }
104
105         if (getMaxLength() > 0) {
106             encoder.print(" length=\"");
107             encoder.print(getMaxLength());
108             encoder.print('\"');
109         }
110
111         if (getPrecision() > 0) {
112             encoder.print(" precision=\"");
113             encoder.print(getPrecision());
114             encoder.print('\"');
115         }
116
117         int direction = getDirection();
118         if (direction == ProcedureParameter.IN_PARAMETER) {
119             encoder.print(" direction=\"in\"");
120         }
121         else if (direction == ProcedureParameter.IN_OUT_PARAMETER) {
122             encoder.print(" direction=\"in_out\"");
123         }
124         else if (direction == ProcedureParameter.OUT_PARAMETER) {
125             encoder.print(" direction=\"out\"");
126         }
127
128         encoder.println("/>");
129     }
130
131     /**
132      * Returns the direction of this parameter. Possible values can be IN_PARAMETER,
133      * OUT_PARAMETER, IN_OUT_PARAMETER, VOID_PARAMETER.
134      */

135     public int getDirection() {
136         return direction;
137     }
138
139     public int getMaxLength() {
140         return maxLength;
141     }
142
143     public int getPrecision() {
144         return precision;
145     }
146
147     public int getType() {
148         return type;
149     }
150
151     /**
152      * @return <code>true</code> if this is IN or INOUT parameter.
153      */

154     public boolean isInParameter() {
155         return direction == IN_PARAMETER || direction == IN_OUT_PARAMETER;
156     }
157
158     /**
159      * @return <code>true</code> if this is OUT or INOUT parameter.
160      */

161     public boolean isOutParam() {
162         return direction == OUT_PARAMETER || direction == IN_OUT_PARAMETER;
163     }
164
165     /**
166      * Sets the direction of this parameter. Acceptable values of direction are defined as
167      * int constants in ProcedureParam class. If an attempt is made to set an invalid
168      * attribute's direction, an IllegalArgumentException is thrown by this method.
169      */

170     public void setDirection(int direction) {
171         if (direction != IN_PARAMETER
172                 && direction != OUT_PARAMETER
173                 && direction != IN_OUT_PARAMETER) {
174             throw new IllegalArgumentException JavaDoc("Unknown parameter type: " + direction);
175         }
176
177         this.direction = direction;
178     }
179
180     public void setMaxLength(int i) {
181         maxLength = i;
182     }
183
184     public void setPrecision(int i) {
185         precision = i;
186     }
187
188     public void setType(int i) {
189         type = i;
190     }
191
192     /**
193      * Returns the procedure that holds this parameter.
194      */

195     public Procedure getProcedure() {
196         return procedure;
197     }
198
199     /**
200      * Sets the procedure that holds this parameter.
201      */

202     public void setProcedure(Procedure procedure) {
203         this.procedure = procedure;
204     }
205 }
206
Popular Tags