Source code: com/vinculum/processeditor/DatabaseWriter.java
1 /* * ** ** BEGIN LICENSE BLOCK * ** **
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is Vinculum Open Source.
15 *
16 * The Initial Developer of the Original Code is
17 * Gerard Toonstra.
18 * Portions created by the Initial Developer are Copyright (C) 2003
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 *
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
34 *
35 * ** ** * END LICENSE BLOCK * ** **
36 */
37
38 /***************************************************************************
39 $RCSfile: DatabaseWriter.java,v $ - description
40 -------------------
41 begin : $Date: 2003/07/08 08:02:06 $
42 copyright : Vinculum (C) 2002
43 author : $Author: chiraz $
44 ***************************************************************************/
45
46 /* $Log: DatabaseWriter.java,v $
47 /* Revision 1.1.1.1 2003/07/08 08:02:06 chiraz
48 /* egg
49 /* */
50
51 package com.vinculum.processeditor;
52
53 import com.vinculum.processeditor.model.ProcessDiagram;
54 import com.vinculum.processeditor.preference.PrefPage;
55
56 import java.io.File;
57 import java.net.URL;
58 import java.sql.Connection;
59 import java.sql.Driver;
60 import java.sql.DriverManager;
61 import java.sql.SQLException;
62 import java.util.HashMap;
63 import java.util.Map;
64 import java.util.Properties;
65
66 import org.eclipse.core.runtime.CoreException;
67 import org.eclipse.core.runtime.Platform;
68 import org.eclipse.jface.preference.IPreferenceStore;
69 import org.eclipse.ui.plugin.AbstractUIPlugin;
70
71 /**
72 * @author chilan
73 */
74
75 /**
76 * Initiates the writing of a process description to a database.
77 *
78 */
79 public class DatabaseWriter
80 {
81 private static DatabaseWriter singleton = null;
82
83 public DatabaseWriter()
84 {
85 super();
86 }
87
88 public DatabaseWriter getInstance()
89 {
90 if ( singleton == null )
91 {
92 singleton = new DatabaseWriter();
93 }
94 return singleton;
95 }
96
97 /**
98 * Saves a process diagram to a database.
99 *
100 * @param diagram The diagram to save.
101 * @return -
102 */
103
104 public static void saveToDatabase(ProcessDiagram diagram)
105 {
106 Connection conn = null;
107 String database = null;
108 String username = null;
109 String password = null;
110 String port = null;
111 String server = null;
112 String driverName = null;
113 String driverURL = null;
114 String connectionString = new String();
115 Driver driver = null;
116
117 AbstractUIPlugin plugin = (AbstractUIPlugin)Platform.getPlugin(ProcesseditorPlugin.PLUGIN_ID);
118 IPreferenceStore store = plugin.getPreferenceStore();
119
120 database = store.getString(PrefPage.P_DATABASE);
121 username = store.getString(PrefPage.P_USERNAME);
122 password = store.getString(PrefPage.P_PASSWORD);
123 port = store.getString(PrefPage.P_DBPORT);
124 server = store.getString(PrefPage.P_SERVERNAME);
125 driverName = store.getString(PrefPage.P_DRIVERNAME);
126 driverURL = store.getString(PrefPage.P_DRIVERURL);
127
128 Map auth = Platform.getAuthorizationInfo(ProcesseditorPlugin.URL, "com.vinculum.processeditor", "Basic");
129
130 if ( auth == null )
131 {
132 auth = new HashMap();
133 }
134
135 connectionString =
136 server + ":" + port + "/" + database;
137
138 auth.put("url", connectionString);
139 auth.put("user", username);
140 auth.put("driver", driverURL);
141 auth.put("password", password);
142
143 Properties info = new Properties();
144 info.setProperty("user", username);
145 info.setProperty("password", password);
146
147 try
148 {
149 Platform.addAuthorizationInfo(ProcesseditorPlugin.URL, "com.vinculum.processeditor", "Basic", auth);
150 }
151 catch (CoreException e)
152 {
153 System.err.println("Oops, error adding auth info");
154 }
155
156 try
157 {
158 jdbcClassLoader loader = new jdbcClassLoader();
159
160 loader.getDriverClassLoader(new URL[] {new File(driverURL).toURL()} );
161
162 driver = loader.getDriver( driverName, connectionString );
163 conn = driver.connect( connectionString, info );
164
165 ProcessDiagramDbSerializer.serialise( diagram, conn );
166 }
167 catch (SQLException sqle)
168 {
169 System.err.println(sqle.getMessage());
170 System.err.println(sqle.toString());
171 }
172 catch (InstantiationException ie)
173 {
174 System.err.println(ie.getMessage());
175 System.err.println(ie.toString());
176 }
177 catch (IllegalAccessException iae)
178 {
179 System.err.println(iae.getMessage());
180 System.err.println(iae.toString());
181 }
182 catch (ClassNotFoundException cnfe)
183 {
184 System.err.println(cnfe.getMessage());
185 System.err.println(cnfe.toString());
186 }
187 catch( Exception e )
188 {
189 System.err.println(e.getMessage());
190 System.err.println(e.toString());
191 }
192 }
193 }