KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cmsinstaller > DatabaseCommander


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cmsinstaller;
25
26 import java.text.SimpleDateFormat JavaDoc;
27 import java.util.*;
28 import java.util.Date JavaDoc;
29 import java.io.*;
30 import java.sql.*;
31
32 public abstract class DatabaseCommander
33 {
34     public String JavaDoc databaseName = null;
35     public String JavaDoc driverClass = null;
36     public String JavaDoc url = null;
37     public String JavaDoc userName = null;
38     public String JavaDoc password = null;
39     public String JavaDoc databaseHostName = null;
40     public String JavaDoc databasePortNumber= null;
41     private String JavaDoc databaseInstance = null;
42     public String JavaDoc infoglueDatabaseUserName = null;
43     public String JavaDoc infoglueDatabasePassword = null;
44     public String JavaDoc databaseSuffix = null;
45     public String JavaDoc hostName = null;
46     public boolean createDatabase = false;
47     public boolean createInitialData= false;
48
49     public boolean createAntSeekHome= false;
50     public boolean createOfficeStand= false;
51     public boolean createOfficeStand2= false;
52     
53     public DatabaseCommander(String JavaDoc driverClass, String JavaDoc databaseHostName, String JavaDoc databasePortNumber, String JavaDoc databaseInstance, String JavaDoc url, String JavaDoc userName, String JavaDoc password, String JavaDoc infoglueDatabaseUserName, String JavaDoc infoglueDatabasePassword, String JavaDoc databaseName, String JavaDoc databaseSuffix, String JavaDoc hostName, boolean createDatabase, boolean createInitialData, boolean createAntSeekHome, boolean createOfficeStand, boolean createOfficeStand2)
54     {
55         this.driverClass = driverClass;
56         this.url = url;
57         this.userName = userName;
58         this.password = password ;
59         this.databaseHostName = databaseHostName;
60         this.databasePortNumber = databasePortNumber;
61         this.databaseInstance = databaseInstance;
62         this.infoglueDatabaseUserName = infoglueDatabaseUserName;
63         this.infoglueDatabasePassword = infoglueDatabasePassword;
64         this.databaseName = databaseName;
65         this.databaseSuffix = databaseSuffix;
66         this.hostName = hostName;
67         this.createDatabase = createDatabase;
68         this.createInitialData = createInitialData;
69         this.createAntSeekHome = createAntSeekHome;
70         this.createOfficeStand = createOfficeStand;
71         this.createOfficeStand2 = createOfficeStand2;
72     }
73     
74     public Connection getConnection() throws Exception JavaDoc
75     {
76         Connection conn = null;
77
78         Logger.logInfo("*****************************************");
79         Logger.logInfo("* The connect phaze starts *");
80         Logger.logInfo("*****************************************");
81         
82         // Load the JDBC driver
83
Logger.logInfo("Loading JDBC driver " + this.driverClass + "\n");
84         Class.forName(this.driverClass).newInstance();
85         
86         // Connect to the databse
87
Logger.logInfo("Connecting to database on " + url + " whith driver " + this.driverClass + " and " + this.userName + " and " + this.password);
88         conn = DriverManager.getConnection(this.url, userName, password);
89         Logger.logInfo("Connected...");
90         
91         return conn;
92     }
93     
94     public Connection getConnection(String JavaDoc url, String JavaDoc userName, String JavaDoc password) throws Exception JavaDoc
95     {
96         Connection conn = null;
97     
98         Logger.logInfo("*****************************************");
99         Logger.logInfo("* The connect phaze starts *");
100         Logger.logInfo("*****************************************");
101         
102         // Load the JDBC driver
103
Logger.logInfo("Loading JDBC driver " + this.driverClass + "\n");
104         Class.forName(this.driverClass).newInstance();
105         
106         // Connect to the databse
107
Logger.logInfo("Connecting to database on " + url);
108         conn = DriverManager.getConnection(url, userName, password);
109         Logger.logInfo("Connected...");
110         
111         return conn;
112     }
113     
114     public String JavaDoc getHostAddress()
115     {
116         String JavaDoc address = null;
117         
118         try
119         {
120             address = java.net.InetAddress.getLocalHost().getHostAddress();
121         }
122         catch(Exception JavaDoc e)
123         {
124             e.printStackTrace();
125         }
126         
127         return address;
128     }
129     
130     
131     /**
132      * This method issues special command to the db. I had to build my own adoption of sql to make this feature.
133      */

134     
135     protected void issueSpecialCommand(Connection conn, String JavaDoc sql)
136     {
137         Logger.logInfo("Command:" + sql);
138                 
139         try
140         {
141             String JavaDoc tableName = null;
142             String JavaDoc columnName = null;
143             String JavaDoc image = null;
144             String JavaDoc idColumn = null;
145             String JavaDoc idValue = null;
146             
147             StringTokenizer st = new StringTokenizer(sql, " ");
148             int i = 0;
149             while (st.hasMoreTokens())
150             {
151                 String JavaDoc part = st.nextToken();
152                 //Logger.logInfo("Part: " + part);
153
if(i == 2)
154                     tableName = part;
155                 if(i == 4)
156                     columnName = part;
157                 if(i == 6)
158                     image = part;
159                 if(i == 8)
160                     idColumn = part;
161                 if(i == 10)
162                     idValue = part;
163                 
164                 i++;
165             }
166             
167             File file = new File(image);
168             FileInputStream fis = new FileInputStream(file);
169             byte[] imageByteArray = new byte[(int)file.length()];
170             fis.read(imageByteArray);
171             
172                         
173             sql = "UPDATE " + tableName + " SET " + columnName + " = ? WHERE " + idColumn + " = " + idValue + "";
174             //Logger.logInfo("newSQL:" + newSQL);
175
PreparedStatement ps = conn.prepareStatement(sql);
176             ps.setBytes(1, imageByteArray);
177             ps.executeUpdate();
178         }
179         catch(Exception JavaDoc ex)
180         {
181             Logger.logInfo("Command failed: " + ex.getMessage());
182             Logger.logInfo("SQL: " + sql);
183             System.err.println("SQLException: " + ex.getMessage());
184         }
185     }
186     
187     public void setupExamples() throws Exception JavaDoc
188     {
189         /*
190         if(this.createAntSeekHome)
191         {
192             File file = new File("assets/www.antseekhome.com.xml");
193             InitialDataInstaller initialDataInstaller = new InitialDataInstaller();
194             initialDataInstaller.importRepository(file);
195         }
196         if(this.createOfficeStand)
197         {
198             File file = new File("assets/www.officestand.com.xml");
199             InitialDataInstaller initialDataInstaller = new InitialDataInstaller();
200             initialDataInstaller.importRepository(file);
201         }
202         */

203         if(this.createOfficeStand2)
204         {
205             File file = new File("assets/www.officestand2.com.xml");
206             InitialDataInstaller initialDataInstaller = new InitialDataInstaller();
207             initialDataInstaller.importRepository(file);
208         }
209     }
210     
211     /**
212      * This method issues special blob-inserts command to the db.
213      * I had to build my own adoption of sql to make this feature.
214      */

215
216     protected abstract void issueSpecialBlobCommand(Connection conn, String JavaDoc sql);
217     
218     
219     protected List parseColumns(String JavaDoc columnDefinition)
220     {
221         List columns = new ArrayList();
222         
223         //System.out.println("columnDefinition:" + columnDefinition);
224
columnDefinition = columnDefinition.substring(1, columnDefinition.length() - 1);
225         //System.out.println("columnDefinition:" + columnDefinition);
226

227         StringTokenizer st = new StringTokenizer(columnDefinition, ",");
228         while (st.hasMoreTokens())
229         {
230             String JavaDoc part = st.nextToken();
231             //Logger.logInfo("Part: " + part);
232
columns.add(part);
233         }
234         
235         return columns;
236     }
237     
238     public Date JavaDoc parseDate(String JavaDoc dateString, String JavaDoc pattern)
239     {
240         if(dateString == null)
241             return new Date JavaDoc();
242     
243         Date JavaDoc date = new Date JavaDoc();
244     
245         try
246         {
247             SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(pattern);
248             date = formatter.parse(dateString);
249         }
250         catch(Exception JavaDoc e)
251         {
252             Logger.logInfo("Error parsing date:" + dateString);
253         }
254     
255         return date;
256     }
257     
258     protected List parseValues(String JavaDoc values)
259     {
260         List valueList = new ArrayList();
261         
262         //System.out.println("values:" + values);
263
values = values.substring(1, values.length() - 2);
264         //System.out.println("values:" + values);
265

266         int offset = 0;
267         int index = values.indexOf("[,]", offset);
268         //StringTokenizer st = new StringTokenizer(values, "[,]");
269
while (index > -1)
270         {
271             String JavaDoc part = values.substring(offset, index);
272             //String part = st.nextToken();
273
//Logger.logInfo("Part: " + part);
274
valueList.add(part);
275             offset = index + 3;
276             index = values.indexOf("[,]", offset);
277         }
278         
279         String JavaDoc part = values.substring(offset);
280         //Logger.logInfo("Part: " + part);
281
valueList.add(part);
282         
283         return valueList;
284     }
285     
286     public String JavaDoc getDatabaseInstance()
287     {
288         return databaseInstance;
289     }
290     public void setDatabaseInstance(String JavaDoc databaseInstance)
291     {
292         this.databaseInstance = databaseInstance;
293     }
294
295     public abstract String JavaDoc getDriver();
296
297     public abstract String JavaDoc getUrl(String JavaDoc hostName, String JavaDoc databasePortNumber, String JavaDoc database);
298
299     public abstract String JavaDoc getUnicodeUrl(String JavaDoc hostName, String JavaDoc databasePortNumber, String JavaDoc database);
300
301     public abstract void setupDatabase() throws Exception JavaDoc;
302     
303     public abstract void upgradeTo1_3(Connection conn) throws Exception JavaDoc;
304
305     public abstract void upgradeTo1_3_2(Connection conn) throws Exception JavaDoc;
306
307     public abstract void upgradeTo2_0(Connection conn) throws Exception JavaDoc;
308
309     public abstract void upgradeTo2_1(Connection conn) throws Exception JavaDoc;
310
311     public abstract void upgradeTo2_3(Connection conn) throws Exception JavaDoc;
312
313     public abstract String JavaDoc getDatabaseVendor() throws Exception JavaDoc;
314
315     public abstract void createCastorFile() throws Exception JavaDoc;
316     
317     public abstract void createCastorRootFile() throws Exception JavaDoc;
318
319     //public abstract void createOSWorkflowFile() throws Exception;
320

321     public abstract void createOSPropertiesFile() throws Exception JavaDoc;
322
323     public abstract void testSetupDummyDatabase() throws Exception JavaDoc;
324     
325     public abstract void testConnectDatabase() throws Exception JavaDoc;
326         
327     }
Popular Tags