001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.ddl.impl;
043:
044: import java.util.HashMap;
045: import java.util.Map;
046: import org.openide.util.NbBundle;
047:
048: import org.netbeans.lib.ddl.Argument;
049: import org.netbeans.lib.ddl.DatabaseSpecification;
050: import org.netbeans.lib.ddl.DDLException;
051: import org.netbeans.lib.ddl.util.CommandFormatter;
052:
053: /**
054: * Argument of procedure. Encapsulates name, type (in/out) and datatype.
055: */
056: public class ProcedureArgument implements Argument {
057: /** Argument name */
058: private String name;
059:
060: /** Argument type */
061: private int type;
062:
063: /** Argument datatype */
064: private int dtype;
065:
066: /** Format */
067: private String format;
068:
069: /** Additional properties */
070: private Map addprops;
071:
072: public static String getArgumentTypeName(int type) {
073: String typename = null;
074: switch (type) {
075: case java.sql.DatabaseMetaData.procedureColumnIn:
076: typename = "IN";
077: break; // NOI18N
078: case java.sql.DatabaseMetaData.procedureColumnOut:
079: typename = "OUT";
080: break; // NOI18N
081: case java.sql.DatabaseMetaData.procedureColumnInOut:
082: typename = "INOUT";
083: break; // NOI18N
084: }
085:
086: return typename;
087: }
088:
089: /** Returns name */
090: public String getName() {
091: return name;
092: }
093:
094: /** Sets name */
095: public void setName(String aname) {
096: name = aname;
097: }
098:
099: /** Returns name of column */
100: public String getFormat() {
101: return format;
102: }
103:
104: /** Sets name of column */
105: public void setFormat(String fmt) {
106: format = fmt;
107: }
108:
109: /** Returns general property */
110: public Object getProperty(String pname) {
111: return addprops.get(pname);
112: }
113:
114: /** Sets general property */
115: public void setProperty(String pname, Object pval) {
116: if (addprops == null)
117: addprops = new HashMap();
118: addprops.put(pname, pval);
119: }
120:
121: /** Describes type of argument: in, out, in/out or return value
122: * of procedure. Particular values you can find in DatabaseMetadata;
123: */
124: public int getType() {
125: return type;
126: }
127:
128: /** Translates numeric representation of type into IN/OUT/INOUT strings.
129: */
130: public String getTypeName() {
131: return getArgumentTypeName(type);
132: }
133:
134: /** Sets type of argument */
135: public void setType(int atype) {
136: type = atype;
137: }
138:
139: /** Returns datatype of argument */
140: public int getDataType() {
141: return dtype;
142: }
143:
144: /** Sets datatype of argument */
145: public void setDataType(int atype) {
146: dtype = atype;
147: }
148:
149: /**
150: * Returns properties and it's values supported by this object.
151: * argument.name Name of argument
152: * argument.type Type of argument
153: * argument.datatype Datatype of argument
154: * Throws DDLException if object name is not specified.
155: */
156: public Map getColumnProperties(AbstractCommand cmd)
157: throws DDLException {
158: HashMap args = new HashMap();
159: DatabaseSpecification spec = cmd.getSpecification();
160: Map typemap = (Map) spec.getProperties().get(
161: "ProcedureArgumentMap"); // NOI18N
162: String typename = (String) typemap
163: .get(getArgumentTypeName(type));
164: args.put("argument.name", cmd.quote(name)); // NOI18N
165: args.put("argument.type", typename); // NOI18N
166: args.put("argument.datatype", spec.getType(dtype)); // NOI18N
167: return args;
168: }
169:
170: /**
171: * Returns full string representation of argument.
172: */
173: public String getCommand(CreateProcedure cmd) throws DDLException {
174: Map cprops;
175: if (format == null)
176: throw new DDLException(NbBundle.getBundle(
177: "org.netbeans.lib.ddl.resources.Bundle").getString(
178: "EXC_NoFormatSpec")); // NOI18N
179: try {
180: cprops = getColumnProperties(cmd);
181: return CommandFormatter.format(format, cprops);
182: } catch (Exception e) {
183: throw new DDLException(e.getMessage());
184: }
185: }
186: }
|