KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Serializable JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.openide.DialogDisplayer;
30 import org.openide.ErrorManager;
31 import org.openide.NotifyDescriptor;
32 import org.openide.util.NbBundle;
33 import org.openide.windows.IOProvider;
34 import org.openide.windows.OutputWriter;
35
36 import org.netbeans.lib.ddl.DatabaseSpecification;
37 import org.netbeans.lib.ddl.DDLCommand;
38 import org.netbeans.lib.ddl.DDLException;
39 import org.netbeans.lib.ddl.util.CommandFormatter;
40 import org.openide.windows.InputOutput;
41
42 /**
43 * Basic implementation of DDLCommand. This class can be used for really simple
44 * commands with format and without arguments. Heavilly subclassed.
45 */

46 public class AbstractCommand implements Serializable JavaDoc, DDLCommand {
47     /** Command owner */
48     private DatabaseSpecification spec;
49
50     /** Execution command with some exception */
51     boolean executionWithException;
52
53     /** Command format */
54     private String JavaDoc format;
55
56     /** Object owner and name */
57     private String JavaDoc owner, name;
58
59     /** Additional properties */
60     private Map JavaDoc addprops;
61
62     static final long serialVersionUID =-560515030304320086L;
63
64     private String JavaDoc quoteStr;
65
66     /** Returns specification (DatabaseSpecification) for this command */
67     public DatabaseSpecification getSpecification() {
68         return spec;
69     }
70
71     /**
72     * Sets specification (DatabaseSpecification) for this command. This method is usually called
73     * in relevant createXXX method.
74     * @param specification New specification object.
75     */

76     public void setSpecification(DatabaseSpecification specification) {
77         spec = specification;
78     }
79
80     /**
81     * Sets format for this command. This method is usually called in relevant createXXX
82     * method.
83     * @param fmt New format.
84     */

85     public void setFormat(String JavaDoc fmt) {
86         format = fmt;
87     }
88
89     /** Returns name of modified object */
90     public String JavaDoc getObjectName() {
91         return name;
92     }
93
94     /** Sets name to be used in command
95     * @param nam New name.
96     */

97     public void setObjectName(String JavaDoc nam) {
98         name = nam;
99     }
100
101     /** Returns name of modified object */
102     public String JavaDoc getObjectOwner() {
103         if (owner != null)
104             if (owner.trim().equals(""))
105                 setObjectOwner(null);
106
107         return owner;
108     }
109
110     /** Sets name to be used in command
111     * @param objectowner New owner.
112     */

113     public void setObjectOwner(String JavaDoc objectowner) {
114         owner = objectowner;
115     }
116
117     /** Returns general property */
118     public Object JavaDoc getProperty(String JavaDoc pname) {
119         return addprops.get(pname);
120     }
121
122     /** Sets general property */
123     public void setProperty(String JavaDoc pname, Object JavaDoc pval) {
124         if (addprops == null)
125             addprops = new HashMap JavaDoc();
126         addprops.put(pname, pval);
127     }
128
129     /**
130     * Returns properties and it's values supported by this object.
131     * object.name Name of the object; use setObjectName()
132     * object.owner Name of the object; use setObjectOwner()
133     * Throws DDLException if object name is not specified.
134     */

135     public Map JavaDoc getCommandProperties() throws DDLException {
136         HashMap JavaDoc args = new HashMap JavaDoc();
137         if (addprops != null)
138             args.putAll(addprops);
139         String JavaDoc oname = getObjectName();
140         if (oname != null)
141             args.put("object.name", quote(getObjectName())); // NOI18N
142
else
143             throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_Unknown")); // NOI18N
144
args.put("object.owner", quote(getObjectOwner())); // NOI18N
145

146         return args;
147     }
148
149     /**
150     * Executes command.
151     * First it calls getCommand() to obtain command text. Then tries to open JDBC
152     * connection and execute it. If connection is already open, uses it and leave
153     * open; otherwise creates new one and closes after use. Throws DDLException if
154     * something wrong occurs.
155     */

156     public void execute() throws DDLException {
157         String JavaDoc fcmd;
158         Connection JavaDoc fcon = null;
159         boolean opened = false;
160         executionWithException = false;
161
162         try {
163             fcmd = getCommand();
164         } catch (Exception JavaDoc e) {
165             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
166             executionWithException = true;
167             DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_UnableToFormat")+"\n" + format + "\n" + e.getMessage(), NotifyDescriptor.ERROR_MESSAGE)); // NOI18N
168
return;
169         }
170
171         //In case of debug mode print command
172
if (spec.getSpecificationFactory().isDebugMode()) {
173
174             try {
175                 InputOutput io = IOProvider.getDefault().getIO(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("LBL_Output_Window"), false);
176                 io.select();
177                 OutputWriter ow = io.getOut(); //NOI18N
178
if (ow != null) {
179                     ow.println(fcmd);
180                     ow.println(" "); //NOI18N
181
} else
182                     throw new Exception JavaDoc();
183
184             } catch (Exception JavaDoc e) {
185                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, e.getMessage() + "\n" + fcmd); //NOI18N
186
}
187         }
188
189         try {
190             fcon = spec.getJDBCConnection();
191             if (fcon == null) {
192                 fcon = spec.openJDBCConnection();
193                 opened = true;
194             }
195
196             Statement JavaDoc stat = fcon.createStatement();
197             stat.execute(fcmd);
198             stat.close();
199         } catch (Exception JavaDoc e) {
200             executionWithException = true;
201             if (opened && fcon != null)
202                 spec.closeJDBCConnection();
203
204             throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_UnableToExecute")+"\n" + fcmd + "\n" + e.getMessage()); // NOI18N
205
}
206
207         if (opened)
208             spec.closeJDBCConnection();
209     }
210
211     /**
212     * Returns full string representation of command. This string needs no
213     * formatting and could be used directly as argument of executeUpdate()
214     * command. Throws DDLException if format is not specified or CommandFormatter
215     * can't format it (it uses MapFormat to process entire lines and can solve []
216     * enclosed expressions as optional.
217     */

218     public String JavaDoc getCommand() throws DDLException {
219         if (format == null)
220             throw new DDLException(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_NoFormatSpec")); // NOI18N
221
try {
222             Map JavaDoc props = getCommandProperties();
223             return CommandFormatter.format(format, props);
224         } catch (Exception JavaDoc e) {
225             throw new DDLException(e.getMessage());
226         }
227     }
228
229     /** information about appearance some exception in the last execute a bunch of commands */
230     public boolean wasException() {
231         return executionWithException;
232     }
233
234     private String JavaDoc getQuoteString() {
235         try {
236             quoteStr = getSpecification().getJDBCConnection().getMetaData().getIdentifierQuoteString();
237             
238             //Firebird patch (commands don't work with quoted names)
239
if (getSpecification().getJDBCConnection().getMetaData().getDatabaseProductName().indexOf("Firebird") != -1) //NOI18N
240
quoteStr = "";
241         } catch (SQLException JavaDoc exc) {
242             //PENDING
243
}
244         if (quoteStr == null)
245             quoteStr = ""; //NOI18N
246
else
247             quoteStr.trim();
248
249         return quoteStr;
250     }
251
252     protected String JavaDoc quote(String JavaDoc name) {
253         if (name == null || name.equals(""))
254             return name;
255
256         if (quoteStr == null)
257             quoteStr = getQuoteString();
258
259         return quoteStr + name + quoteStr;
260     }
261
262     /** Reads object from stream */
263     public void readObject(java.io.ObjectInputStream JavaDoc in) throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
264         format = (String JavaDoc)in.readObject();
265         owner = (String JavaDoc)in.readObject();
266         name = (String JavaDoc)in.readObject();
267         addprops = (Map JavaDoc)in.readObject();
268     }
269
270     /** Writes object to stream */
271     public void writeObject(java.io.ObjectOutputStream JavaDoc out) throws java.io.IOException JavaDoc {
272         //System.out.println("Writing command "+name);
273
out.writeObject(format);
274         out.writeObject(owner);
275         out.writeObject(name);
276         out.writeObject(addprops);
277     }
278 }
279
Popular Tags