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.text.MessageFormat;
045: import java.util.Enumeration;
046: import java.util.Map;
047: import java.util.Vector;
048:
049: import org.openide.util.NbBundle;
050:
051: import org.netbeans.lib.ddl.Argument;
052: import org.netbeans.lib.ddl.DDLException;
053: import org.netbeans.lib.ddl.ProcedureDescriptor;
054:
055: /**
056: * Interface of database action command. Instances should remember connection
057: * information of DatabaseSpecification and use it in execute() method. This is a base interface
058: * used heavily for sub-interfacing (it is not subclassing :)
059: *
060: * @author Slavek Psenicka
061: */
062:
063: public class CreateProcedure extends AbstractCommand implements
064: ProcedureDescriptor {
065: /** Catalog */
066: private String cat;
067:
068: /** Body of the procedure */
069: private String body;
070:
071: /** Arguments */
072: private Vector args;
073:
074: static final long serialVersionUID = 1316633286943440734L;
075:
076: public CreateProcedure() {
077: args = new Vector();
078: setNewObject(true);
079: }
080:
081: /** Returns catalog */
082: public String getCatalog() {
083: return cat;
084: }
085:
086: /** Sets catalog */
087: public void setCatalog(String cname) {
088: cat = cname;
089: }
090:
091: /** Returns text of procedure */
092: public String getText() {
093: return body;
094: }
095:
096: /** Sets name of table */
097: public void setText(String text) {
098: body = text;
099: }
100:
101: /** Returns arguments */
102: public Vector getArguments() {
103: return args;
104: }
105:
106: public Argument getArgument(int index) {
107: return (Argument) args.get(index);
108: }
109:
110: /** Sets argument array */
111: public void setArguments(Vector argarr) {
112: args = argarr;
113: }
114:
115: public void setArgument(int index, Argument arg) {
116: args.set(index, arg);
117: }
118:
119: public Argument createArgument(String name, int type, int datatype)
120: throws DDLException {
121: try {
122: Map gprops = (Map) getSpecification().getProperties();
123: Map props = (Map) getSpecification().getCommandProperties(
124: Specification.CREATE_PROCEDURE);
125: Map bindmap = (Map) props.get("Binding"); // NOI18N
126: String tname = (String) bindmap.get("ARGUMENT"); // NOI18N
127: if (tname != null) {
128: Map typemap = (Map) gprops.get(tname);
129: if (typemap == null)
130: throw new InstantiationException(
131: MessageFormat
132: .format(
133: NbBundle
134: .getBundle(
135: "org.netbeans.lib.ddl.resources.Bundle")
136: .getString(
137: "EXC_UnableLocateObject"), // NOI18N
138: new String[] { tname }));
139: Class typeclass = Class.forName((String) typemap
140: .get("Class")); // NOI18N
141: String format = (String) typemap.get("Format"); // NOI18N
142: ProcedureArgument arg = (ProcedureArgument) typeclass
143: .newInstance();
144: arg.setName(name);
145: arg.setType(type);
146: arg.setDataType(datatype);
147: arg.setFormat(format);
148: return (Argument) arg;
149: } else
150: throw new InstantiationException(
151: MessageFormat
152: .format(
153: NbBundle
154: .getBundle(
155: "org.netbeans.lib.ddl.resources.Bundle")
156: .getString(
157: "EXC_UnableLocateType"), // NOI18N
158: new String[] {
159: String.valueOf(type),
160: bindmap.toString() }));
161: } catch (Exception e) {
162: throw new DDLException(e.getMessage());
163: }
164: }
165:
166: public void addArgument(String name, int type, int datatype)
167: throws DDLException {
168: Argument arg = createArgument(name, type, datatype);
169: if (arg != null)
170: args.add(arg);
171: }
172:
173: public Map getCommandProperties() throws DDLException {
174: Map props = (Map) getSpecification().getProperties();
175: String cols = "", argdelim = (String) props
176: .get("ArgumentListDelimiter"); // NOI18N
177: Map cmdprops = super .getCommandProperties();
178:
179: Enumeration col_e = args.elements();
180: while (col_e.hasMoreElements()) {
181: ProcedureArgument arg = (ProcedureArgument) col_e
182: .nextElement();
183: boolean inscomma = col_e.hasMoreElements();
184: cols = cols + arg.getCommand(this )
185: + (inscomma ? argdelim : "");
186: }
187:
188: cmdprops.put("arguments", cols); // NOI18N
189: cmdprops.put("body", body); // NOI18N
190: return cmdprops;
191: }
192: }
|