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.modules.db.sql.editor;
043:
044: import java.io.BufferedWriter;
045: import java.io.IOException;
046: import java.io.OutputStream;
047: import java.io.OutputStreamWriter;
048: import java.text.MessageFormat;
049: import org.netbeans.api.db.explorer.DatabaseConnection;
050: import org.netbeans.modules.db.api.sql.execute.SQLExecuteCookie;
051: import org.netbeans.modules.db.spi.sql.editor.SQLEditorProvider;
052: import org.openide.cookies.OpenCookie;
053: import org.openide.filesystems.FileLock;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.filesystems.Repository;
057: import org.openide.loaders.DataObject;
058: import org.openide.loaders.DataObjectNotFoundException;
059: import org.openide.util.Exceptions;
060: import org.openide.util.NbBundle;
061:
062: /**
063: *
064: * @author Andrei Badea
065: */
066: public class SQLEditorProviderImpl implements SQLEditorProvider {
067:
068: // TODO: should ensure that the number of the generated temporary file
069: // is greater than any of the numbers of the existing files
070:
071: private static final String CMD_FOLDER = "Databases/SQLCommands"; // NOI18N
072:
073: public void openSQLEditor(DatabaseConnection dbconn, String sql,
074: boolean execute) {
075: FileObject root = Repository.getDefault()
076: .getDefaultFileSystem().getRoot();
077: FileObject tmpFo = root.getFileObject(CMD_FOLDER);
078: if (tmpFo == null) {
079: try {
080: tmpFo = FileUtil.createFolder(root, CMD_FOLDER);
081: } catch (IOException e) {
082: Exceptions.printStackTrace(e);
083: }
084: }
085:
086: FileObject sqlFo = null;
087:
088: int i = 1;
089: for (;;) {
090: String nameFmt = NbBundle.getMessage(
091: SQLEditorProviderImpl.class,
092: "LBL_SQLCommandFileName");
093: String name = MessageFormat.format(nameFmt,
094: new Object[] { new Integer(i) });
095: try {
096: sqlFo = tmpFo.createData(name);
097: } catch (IOException e) {
098: i++;
099: continue;
100: }
101: break;
102: }
103:
104: try {
105: FileLock lock = sqlFo.lock();
106: try {
107: OutputStream stream = sqlFo.getOutputStream(lock);
108: try {
109: BufferedWriter writer = new BufferedWriter(
110: new OutputStreamWriter(stream, "UTF-8")); // NOI18N
111: try {
112: writer.write(sql);
113: } finally {
114: writer.close();
115: }
116: } finally {
117: stream.close();
118: }
119: } finally {
120: lock.releaseLock();
121: }
122: } catch (IOException e) {
123: Exceptions.printStackTrace(e);
124: }
125:
126: DataObject sqlDo;
127: try {
128: sqlDo = DataObject.find(sqlFo);
129: } catch (DataObjectNotFoundException e) {
130: Exceptions.printStackTrace(e);
131: return;
132: }
133:
134: OpenCookie openCookie = (OpenCookie) sqlDo
135: .getCookie(OpenCookie.class);
136: openCookie.open();
137:
138: SQLExecuteCookie sqlCookie = (SQLExecuteCookie) sqlDo
139: .getCookie(SQLExecuteCookie.class);
140: sqlCookie.setDatabaseConnection(dbconn);
141: if (execute) {
142: sqlCookie.execute();
143: }
144: }
145: }
|