KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > ddl > impl > CreateProcedure


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.ddl.impl;
21
22 import java.text.MessageFormat JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import org.openide.util.NbBundle;
28
29 import org.netbeans.lib.ddl.Argument;
30 import org.netbeans.lib.ddl.DDLException;
31 import org.netbeans.lib.ddl.ProcedureDescriptor;
32
33 /**
34 * Interface of database action command. Instances should remember connection
35 * information of DatabaseSpecification and use it in execute() method. This is a base interface
36 * used heavily for sub-interfacing (it is not subclassing :)
37 *
38 * @author Slavek Psenicka
39 */

40
41 public class CreateProcedure extends AbstractCommand implements ProcedureDescriptor
42 {
43     /** Catalog */
44     private String JavaDoc cat;
45
46     /** Body of the procedure */
47     private String JavaDoc body;
48
49     /** Arguments */
50     private Vector JavaDoc args;
51
52     static final long serialVersionUID =1316633286943440734L;
53     public CreateProcedure()
54     {
55         args = new Vector JavaDoc();
56     }
57
58     /** Returns catalog */
59     public String JavaDoc getCatalog()
60     {
61         return cat;
62     }
63
64     /** Sets catalog */
65     public void setCatalog(String JavaDoc cname)
66     {
67         cat = cname;
68     }
69
70     /** Returns text of procedure */
71     public String JavaDoc getText()
72     {
73         return body;
74     }
75
76     /** Sets name of table */
77     public void setText(String JavaDoc text)
78     {
79         body = text;
80     }
81
82     /** Returns arguments */
83     public Vector JavaDoc getArguments()
84     {
85         return args;
86     }
87
88     public Argument getArgument(int index)
89     {
90         return (Argument)args.get(index);
91     }
92
93     /** Sets argument array */
94     public void setArguments(Vector JavaDoc argarr)
95     {
96         args = argarr;
97     }
98
99     public void setArgument(int index, Argument arg)
100     {
101         args.set(index, arg);
102     }
103
104     public Argument createArgument(String JavaDoc name, int type, int datatype)
105     throws DDLException
106     {
107         try {
108             Map JavaDoc gprops = (Map JavaDoc)getSpecification().getProperties();
109             Map JavaDoc props = (Map JavaDoc)getSpecification().getCommandProperties(Specification.CREATE_PROCEDURE);
110             Map JavaDoc bindmap = (Map JavaDoc)props.get("Binding"); // NOI18N
111
String JavaDoc tname = (String JavaDoc)bindmap.get("ARGUMENT"); // NOI18N
112
if (tname != null) {
113                 Map JavaDoc typemap = (Map JavaDoc)gprops.get(tname);
114                 if (typemap == null) throw new InstantiationException JavaDoc(
115                     MessageFormat.format(
116                         NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_UnableLocateObject"), // NOI18N
117
new String JavaDoc[] {tname}));
118                 Class JavaDoc typeclass = Class.forName((String JavaDoc)typemap.get("Class")); // NOI18N
119
String JavaDoc format = (String JavaDoc)typemap.get("Format"); // NOI18N
120
ProcedureArgument arg = (ProcedureArgument)typeclass.newInstance();
121                 arg.setName(name);
122                 arg.setType(type);
123                 arg.setDataType(datatype);
124                 arg.setFormat(format);
125                 return (Argument)arg;
126             } else throw new InstantiationException JavaDoc(
127                         MessageFormat.format(
128                             NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_UnableLocateType"), // NOI18N
129
new String JavaDoc[] {String.valueOf(type), bindmap.toString() }));
130         } catch (Exception JavaDoc e) {
131             throw new DDLException(e.getMessage());
132         }
133     }
134
135     public void addArgument(String JavaDoc name, int type, int datatype)
136     throws DDLException
137     {
138         Argument arg = createArgument(name, type, datatype);
139         if (arg != null) args.add(arg);
140     }
141
142     public Map JavaDoc getCommandProperties()
143     throws DDLException
144     {
145         Map JavaDoc props = (Map JavaDoc)getSpecification().getProperties();
146         String JavaDoc cols = "", argdelim = (String JavaDoc)props.get("ArgumentListDelimiter"); // NOI18N
147
Map JavaDoc cmdprops = super.getCommandProperties();
148
149         Enumeration JavaDoc col_e = args.elements();
150         while (col_e.hasMoreElements()) {
151             ProcedureArgument arg = (ProcedureArgument)col_e.nextElement();
152             boolean inscomma = col_e.hasMoreElements();
153             cols = cols + arg.getCommand(this)+(inscomma ? argdelim : "");
154         }
155
156         cmdprops.put("arguments", cols); // NOI18N
157
cmdprops.put("body", body); // NOI18N
158
return cmdprops;
159     }
160 }
161
Popular Tags