001: package org.jsqltool.conn;
002:
003: import java.io.*;
004: import java.util.*;
005: import javax.swing.*;
006: import java.sql.*;
007: import org.jsqltool.gui.tableplugins.datatable.filter.FilterModel;
008: import org.jsqltool.utils.Options;
009:
010: /**
011: * <p>Title: JSqlTool Project</p>
012: * <p>Description: Class for load/save a connection profile.
013: * </p>
014: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
015: *
016: * <p> This file is part of JSqlTool project.
017: * This library is free software; you can redistribute it and/or
018: * modify it under the terms of the (LGPL) Lesser General Public
019: * License as published by the Free Software Foundation;
020: *
021: * GNU LESSER GENERAL PUBLIC LICENSE
022: * Version 2.1, February 1999
023: *
024: * This library is distributed in the hope that it will be useful,
025: * but WITHOUT ANY WARRANTY; without even the implied warranty of
026: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
027: * Library General Public License for more details.
028: *
029: * You should have received a copy of the GNU Library General Public
030: * License along with this library; if not, write to the Free
031: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
032: *
033: * The author may be contacted at:
034: * maurocarniel@tin.it</p>
035: *
036: * @author Mauro Carniel
037: * @version 1.0
038: */
039: public class ConnectionProfile {
040:
041: /** max mumber of old queries to store in the connection profile file */
042: private static final int MAX_OLD_QUERIES = 20;
043:
044: /**
045: * Load connection profile file (profile/filename.ini)
046: */
047: public void loadProfile(JInternalFrame parent, File file,
048: ArrayList conns, Vector connNames) {
049: try {
050: BufferedReader br = new BufferedReader(
051: new InputStreamReader(new FileInputStream(file)));
052: String line = br.readLine();
053: if (line.equals("VERSION 1.0.7")) {
054: loadProfileV107(parent, file, conns, connNames);
055: } else if (line.equals("VERSION 1.0.3")) {
056: loadProfileV103(parent, file, conns, connNames);
057: } else
058: loadProfileV102(parent, file, conns, connNames);
059: br.close();
060: } catch (Exception ex) {
061: ex.printStackTrace();
062: JOptionPane
063: .showMessageDialog(
064: parent,
065: Options
066: .getInstance()
067: .getResource(
068: "error on loading connections profile files.")
069: + ":\n" + ex.getMessage(),
070: Options.getInstance().getResource("error"),
071: JOptionPane.ERROR_MESSAGE);
072: }
073:
074: }
075:
076: /**
077: * Load connection profile file (profile/filename.ini) in JSQLTool <=1.0.2 version.
078: */
079: private void loadProfileV102(JInternalFrame parent, File file,
080: ArrayList conns, Vector connNames) {
081: try {
082: // load .ini file...
083: String line = null;
084: int dbType;
085: String name = null;
086: String driver = null;
087: String url = null;
088: String username = null;
089: String password = null;
090: boolean autoCommit;
091: int isolationLevel;
092: boolean readOnly;
093: String catalog = null;
094: String tableName = null;
095: FilterModel fm = null;
096: String whereC = null;
097: Hashtable filters = null;
098: ArrayList oldQueries = null;
099: BufferedReader br = new BufferedReader(
100: new InputStreamReader(new FileInputStream(file)));
101:
102: // read connection properties...
103: dbType = Integer.parseInt(br.readLine());
104: name = br.readLine();
105: driver = br.readLine();
106: url = br.readLine();
107: username = br.readLine();
108: password = Options.getInstance().decode(br.readLine());
109: autoCommit = br.readLine().toLowerCase().equals("true");
110: isolationLevel = Integer.parseInt(br.readLine());
111: readOnly = br.readLine().toLowerCase().equals("true");
112: catalog = br.readLine();
113:
114: // skip one row...
115: br.readLine();
116:
117: // read filters/orderers...
118: filters = new Hashtable();
119: while (!(line = br.readLine()).equals("")) {
120: tableName = line;
121: fm = new FilterModel();
122: fm.setOrderClause(br.readLine());
123: whereC = br.readLine();
124: if (whereC.length() > 0)
125: whereC = whereC.substring(7);
126: fm.setWhereClause(whereC);
127: filters.put(tableName, fm);
128: }
129:
130: // read old queries...
131: oldQueries = new ArrayList();
132: while ((line = br.readLine()) != null) {
133: oldQueries.add(line);
134: }
135: br.close();
136:
137: conns.add(new DbConnection(dbType, name, driver, url,
138: username, password, autoCommit, isolationLevel,
139: readOnly, catalog, filters, oldQueries, false));
140: connNames.add(name);
141:
142: } catch (Exception ex) {
143: ex.printStackTrace();
144: JOptionPane
145: .showMessageDialog(
146: parent,
147: Options
148: .getInstance()
149: .getResource(
150: "error on loading connections profile files.")
151: + ":\n" + ex.getMessage(),
152: Options.getInstance().getResource("error"),
153: JOptionPane.ERROR_MESSAGE);
154: }
155: }
156:
157: /**
158: * Load connection profile file (profile/filename.ini) in JSQLTool >=1.0.3 version.
159: */
160: private void loadProfileV103(JInternalFrame parent, File file,
161: ArrayList conns, Vector connNames) {
162: try {
163: // load .ini file...
164: String line = null;
165: int dbType;
166: String name = null;
167: String driver = null;
168: String url = null;
169: String username = null;
170: boolean autoCommit;
171: int isolationLevel;
172: boolean readOnly;
173: String catalog = null;
174: String tableName = null;
175: FilterModel fm = null;
176: String whereC = null;
177: Hashtable filters = null;
178: ArrayList oldQueries = null;
179: BufferedReader br = new BufferedReader(
180: new InputStreamReader(new FileInputStream(file)));
181:
182: // read connection properties...
183: String iniVersion = br.readLine();
184: dbType = Integer.parseInt(br.readLine());
185: name = br.readLine();
186: driver = br.readLine();
187: url = br.readLine();
188: username = br.readLine();
189: autoCommit = br.readLine().toLowerCase().equals("true");
190: isolationLevel = Integer.parseInt(br.readLine());
191: readOnly = br.readLine().toLowerCase().equals("true");
192: catalog = br.readLine();
193:
194: // skip one row...
195: br.readLine();
196:
197: // read filters/orderers...
198: filters = new Hashtable();
199: while (!(line = br.readLine()).equals("")) {
200: tableName = line;
201: fm = new FilterModel();
202: fm.setOrderClause(br.readLine());
203: whereC = br.readLine();
204: if (whereC.length() > 0)
205: whereC = whereC.substring(7);
206: fm.setWhereClause(whereC);
207: filters.put(tableName, fm);
208: }
209:
210: // read old queries...
211: oldQueries = new ArrayList();
212: while ((line = br.readLine()) != null) {
213: oldQueries.add(line);
214: }
215: br.close();
216:
217: File passwdFile = new File(file.getAbsolutePath()
218: .substring(0, file.getAbsolutePath().length() - 4)
219: + ".pwd");
220: FileInputStream in = new FileInputStream(passwdFile);
221: byte[] bb = new byte[(int) passwdFile.length()];
222: in.read(bb);
223: String password = Options.getInstance().decodeFromBytes(bb);
224: in.close();
225:
226: conns.add(new DbConnection(dbType, name, driver, url,
227: username, password, autoCommit, isolationLevel,
228: readOnly, catalog, filters, oldQueries, false));
229: connNames.add(name);
230:
231: } catch (Exception ex) {
232: ex.printStackTrace();
233: JOptionPane
234: .showMessageDialog(
235: parent,
236: Options
237: .getInstance()
238: .getResource(
239: "error on loading connections profile files.")
240: + ":\n" + ex.getMessage(),
241: Options.getInstance().getResource("error"),
242: JOptionPane.ERROR_MESSAGE);
243: }
244: }
245:
246: /**
247: * Load connection profile file (profile/filename.ini) in JSQLTool >=1.0.7 version.
248: */
249: private void loadProfileV107(JInternalFrame parent, File file,
250: ArrayList conns, Vector connNames) {
251: try {
252: // load .ini file...
253: String line = null;
254: int dbType;
255: String name = null;
256: String driver = null;
257: String url = null;
258: String username = null;
259: boolean autoCommit;
260: int isolationLevel;
261: boolean readOnly;
262: boolean quotes;
263: String catalog = null;
264: String tableName = null;
265: FilterModel fm = null;
266: String whereC = null;
267: Hashtable filters = null;
268: ArrayList oldQueries = null;
269: BufferedReader br = new BufferedReader(
270: new InputStreamReader(new FileInputStream(file)));
271:
272: // read connection properties...
273: String iniVersion = br.readLine();
274: dbType = Integer.parseInt(br.readLine());
275: name = br.readLine();
276: driver = br.readLine();
277: url = br.readLine();
278: username = br.readLine();
279: autoCommit = br.readLine().toLowerCase().equals("true");
280: isolationLevel = Integer.parseInt(br.readLine());
281: readOnly = br.readLine().toLowerCase().equals("true");
282: catalog = br.readLine();
283: quotes = br.readLine().toLowerCase().equals("true");
284:
285: // skip one row...
286: br.readLine();
287:
288: // read filters/orderers...
289: filters = new Hashtable();
290: while (!(line = br.readLine()).equals("")) {
291: tableName = line;
292: fm = new FilterModel();
293: fm.setOrderClause(br.readLine());
294: whereC = br.readLine();
295: if (whereC.length() > 0)
296: whereC = whereC.substring(7);
297: fm.setWhereClause(whereC);
298: filters.put(tableName, fm);
299: }
300:
301: // read old queries...
302: oldQueries = new ArrayList();
303: while ((line = br.readLine()) != null) {
304: oldQueries.add(line);
305: }
306: br.close();
307:
308: File passwdFile = new File(file.getAbsolutePath()
309: .substring(0, file.getAbsolutePath().length() - 4)
310: + ".pwd");
311: FileInputStream in = new FileInputStream(passwdFile);
312: byte[] bb = new byte[(int) passwdFile.length()];
313: in.read(bb);
314: String password = Options.getInstance().decodeFromBytes(bb);
315: in.close();
316:
317: conns.add(new DbConnection(dbType, name, driver, url,
318: username, password, autoCommit, isolationLevel,
319: readOnly, catalog, filters, oldQueries, quotes));
320: connNames.add(name);
321:
322: } catch (Exception ex) {
323: ex.printStackTrace();
324: JOptionPane
325: .showMessageDialog(
326: parent,
327: Options
328: .getInstance()
329: .getResource(
330: "error on loading connections profile files.")
331: + ":\n" + ex.getMessage(),
332: Options.getInstance().getResource("error"),
333: JOptionPane.ERROR_MESSAGE);
334: }
335: }
336:
337: public void saveProfile(JFrame parent, DbConnection c,
338: boolean isEdit) {
339: try {
340: PrintWriter pw = new PrintWriter(new FileOutputStream(
341: new File("profile/" + c.getName().replace(' ', '_')
342: + ".ini")));
343:
344: // save connection properties...
345: pw.println("VERSION 1.0.7");
346: pw.println(c.getDbType());
347: pw.println(c.getName());
348: pw.println(c.getClassName());
349: pw.println(c.getUrl());
350: pw.println(c.getUsername());
351: pw.println(c.isAutoCommit());
352: pw.println(c.getIsolationLevel());
353: pw.println(c.isReadOnly());
354: pw.println(c.getCatalog());
355: pw.println(c.isQuotes());
356:
357: // save one empty row...
358: pw.println("");
359:
360: // save filters/orderers...
361: Enumeration en = c.getFilters().keys();
362: FilterModel fm = null;
363: String tableName = null;
364: while (en.hasMoreElements()) {
365: tableName = en.nextElement().toString();
366: pw.println(tableName);
367: fm = (FilterModel) c.getFilters().get(tableName);
368: pw.println(fm.getOrderClause());
369: pw.println(fm.getWhereClause());
370: }
371:
372: // save one empty row...
373: pw.println("");
374:
375: // save old queries...
376: while (c.getOldQueries().size() > MAX_OLD_QUERIES)
377: c.getOldQueries().remove(0);
378: for (int i = 0; i < c.getOldQueries().size(); i++)
379: pw.println(c.getOldQueries().get(i));
380:
381: pw.close();
382:
383: File passwdFile = new File("profile/"
384: + c.getName().replace(' ', '_') + ".pwd");
385:
386: FileOutputStream out = new FileOutputStream(passwdFile);
387: out.write(Options.getInstance().encodeToBytes(
388: c.getPassword()));
389: out.close();
390:
391: } catch (Exception ex) {
392: ex.printStackTrace();
393: JOptionPane
394: .showMessageDialog(
395: parent,
396: Options
397: .getInstance()
398: .getResource(
399: "error on saving connections profile files.")
400: + ":\n" + ex.getMessage(),
401: Options.getInstance().getResource("error"),
402: JOptionPane.ERROR_MESSAGE);
403: }
404: }
405:
406: }
|