Index: /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/DatabaseFactory.java
===================================================================
--- /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/DatabaseFactory.java	(revision 15081)
+++ /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/DatabaseFactory.java	(revision 15081)
@@ -0,0 +1,93 @@
+package org.greenstone.gsdl3.sql;
+
+/**
+ * Returns a sql server according to the user's selection
+ *
+ */
+
+import org.apache.log4j.*;
+
+import java.util.HashMap;
+
+public class DatabaseFactory{
+    private static HashMap serverMap = new HashMap();
+    private static HashMap wrapperMap = new HashMap();
+    private static Logger logger = Logger.getLogger(org.greenstone.gsdl3.sql.DatabaseFactory.class.getName());
+
+
+    //upcase the first letter
+    public static String properDBName(String name_in){
+	String name_out = "";
+	name_in = name_in.toLowerCase();
+	if (name_in.length() >= 1){
+	    String firstLetter = name_in.substring(0,1).toUpperCase();
+	    name_out = firstLetter +  name_in.substring(1);
+	}
+	return name_out;
+    }
+
+
+    public static final SQLServer getDatabaseServer(String dbname){
+	dbname = properDBName(dbname);
+	if (serverMap.containsKey(dbname)) return (SQLServer) serverMap.get(dbname);
+	String fullName = "org.greenstone.gsdl3.sql." + dbname.toLowerCase()+ "." + dbname +"SQLServer";
+	try {
+	    SQLServer server = (SQLServer)Class.forName(fullName).newInstance();
+	    serverMap.put(dbname, server);
+	    return server;
+	} catch (Exception e) {
+	    logger.info("Couldn't load the database server "+ fullName);
+	}
+	
+	return null; 
+    }
+
+    public static final MetadataDBWrapper getMetadataDBWrapper(String dbname, String sqlstate){
+	dbname = properDBName(dbname);
+	if (wrapperMap.containsKey(dbname + ":" + sqlstate)) return (MetadataDBWrapper)wrapperMap.get(dbname + ":" + sqlstate);	
+
+	String fullName = "org.greenstone.gsdl3.sql." + dbname.toLowerCase()+ "." + dbname +"DBWrapper";
+	try {
+	    MetadataDBWrapper wrapper = (MetadataDBWrapper)Class.forName(fullName).newInstance();
+	    if (wrapper != null){
+		//load SQLServer class
+		SQLServer server = getDatabaseServer(dbname);
+		if (server != null){
+		    wrapper.setSQLServer(server);
+		}
+
+		String fullsqlstate = "org.greenstone.gsdl3.sql." + sqlstate;
+		//load SQLStatements class
+		try{
+		   SQLStatements state = (SQLStatements)Class.forName(fullsqlstate).newInstance(); 
+		   if (state != null){
+		       wrapper.setSQLStatements(state); 
+		       wrapperMap.put(dbname + ":" + sqlstate, wrapper);
+		       return wrapper;
+		   }
+		}
+		catch (Exception e) {
+		    // if falied use sqlstate as a full path
+		    try{
+			SQLStatements state = (SQLStatements)Class.forName(sqlstate).newInstance(); 
+			if (state != null){
+			    wrapper.setSQLStatements(state);
+			    wrapperMap.put(dbname + ":" + sqlstate,wrapper);
+			    return wrapper;
+			}
+		    }
+		    catch (Exception e2) {
+			// failed again, give up
+			logger.info("Couldn't load the sql statement  "+ sqlstate);
+		    }
+		}
+	    }
+	} catch (Exception e) {
+	    logger.info("Couldn't load the database wrapper "+ fullName);
+	}
+	
+	return null; 
+
+    }
+
+}
Index: /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/MetadataDBWrapper.java
===================================================================
--- /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/MetadataDBWrapper.java	(revision 15081)
+++ /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/MetadataDBWrapper.java	(revision 15081)
@@ -0,0 +1,22 @@
+package org.greenstone.gsdl3.sql;
+
+import java.util.ArrayList;
+import org.greenstone.gsdl3.util.DBInfo;
+import org.greenstone.gsdl3.sql.SQLServer;
+
+public interface MetadataDBWrapper{
+
+    public DBInfo getInfo(String id);
+
+    public void setSQLServer(SQLServer server);
+
+    public void setSQLStatements(SQLStatements sqlstate);
+
+    public boolean openConnection(String databasepath);
+
+     //return a list of rows
+    public ArrayList executeQuery(String query_statement);
+
+    public void closeConnection(String databasepath);
+   
+}
Index: /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/SQLServer.java
===================================================================
--- /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/SQLServer.java	(revision 15081)
+++ /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/SQLServer.java	(revision 15081)
@@ -0,0 +1,21 @@
+package org.greenstone.gsdl3.sql;
+
+import java.sql.Connection;
+
+/*
+ * this class provide an interface for connecting and querying a database system. In order to not tie Greenstone to a particalur database system 
+ * every database action should go through this interface 
+ * 
+ */
+public interface SQLServer{
+ 
+    /** connect to a database without creating the database if it doesn't exist */
+    public Connection connect(String databasePath);
+
+    /** connect to a database and create the database if it doesn't exist */
+    public Connection connectAndCreate(String databasePath); 
+
+    public boolean disconnect(String databasePath);
+
+   	
+ }
Index: /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/SQLStatements.java
===================================================================
--- /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/SQLStatements.java	(revision 15081)
+++ /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/SQLStatements.java	(revision 15081)
@@ -0,0 +1,7 @@
+package org.greenstone.gsdl3.sql;
+
+public interface SQLStatements{
+
+    public String getDBInfoStatement(String node_id);
+    public String undoDBSafe(Object text);
+}
Index: /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/derby/DerbyDBWrapper.java
===================================================================
--- /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/derby/DerbyDBWrapper.java	(revision 15081)
+++ /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/derby/DerbyDBWrapper.java	(revision 15081)
@@ -0,0 +1,123 @@
+package org.greenstone.gsdl3.sql.derby;
+
+import  org.greenstone.gsdl3.sql.*;
+
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.SQLWarning;
+import java.sql.Statement;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+
+import org.greenstone.gsdl3.util.DBInfo;
+
+import org.apache.log4j.*;
+
+public class DerbyDBWrapper implements MetadataDBWrapper{
+    protected SQLServer sqlServer = null;
+    protected SQLStatements sqlState = null;
+    protected Connection connection = null; 
+    protected Statement statement = null;
+    protected static Logger logger = Logger.getLogger(org.greenstone.gsdl3.sql.derby.DerbyDBWrapper.class.getName());
+
+
+    public DBInfo getInfo(String id){
+	if(sqlState == null) return null;
+        String dbInfoState = sqlState.getDBInfoStatement(id);
+        ArrayList result = executeQuery(dbInfoState);
+	if (result.size() == 0) return null;
+	DBInfo info = new DBInfo();
+	for(int i=0;  i<result.size();i++){
+	    HashMap arow = (HashMap)result.get(i);
+	    Iterator ite = arow.keySet().iterator();
+	    while(ite.hasNext()){
+		String key = (String)ite.next();
+		Object value = arow.get(key);
+		logger.info(key + "=>" +value);
+		if (value == null || value.equals("")) continue;
+		info.addInfo(key.toLowerCase(),sqlState.undoDBSafe(value));
+	    }
+	}
+
+	return info;
+    }
+
+    public void setSQLServer(SQLServer server){
+	sqlServer = server;
+    }
+
+    public void setSQLStatements(SQLStatements state){
+	sqlState = state;
+    }
+
+    public boolean openConnection(String databasepath){
+	connection = sqlServer.connect(databasepath);
+	if (connection == null) return false;
+
+	try{
+	    statement = connection.createStatement();
+	}
+	catch(SQLException sqle){
+	    logger.error("Database Error occured when creating a statement object",sqle);
+	    return false;
+	}
+	return true;
+    }
+
+    //return a list of rows array->hashmap
+    public ArrayList executeQuery(String query_statement){
+	//the database hasn't been correct yet
+	if (statement == null){
+	    logger.info("the database hasn't been correct yet");
+	    return new ArrayList();
+	}
+	
+	ArrayList results = new ArrayList();
+
+	try{
+	   ResultSet rs = statement.executeQuery(query_statement);
+	   ResultSetMetaData rsmd = rs.getMetaData();
+	   int numOfColumns = rsmd.getColumnCount();
+	   while(rs.next()){
+	       HashMap arow = new HashMap();
+	       for(int i = 1; i < numOfColumns ; i++){
+		   arow.put(rsmd.getColumnName(i), rs.getObject(i));
+	       }
+	       results.add(arow);  
+	   }
+	}
+	catch(SQLException sqle){
+	    logger.error("Database Error occured when executeQuery " + query_statement,sqle);
+	    return results;
+	}
+	return results;
+    }
+    
+     /** check for the existence of a table**/
+    public boolean check4Table (String check_table) {
+         try {
+	  statement.execute(check_table);
+      }  catch (SQLException sqle) {
+	  String theError = (sqle).getSQLState();
+	  if (theError.equals("42X05"))   // Table does not exist
+	      { 
+		  return false;
+	      }   
+	  else{
+	      return true;
+	  }
+      }
+      //  System.out.println("Just got the warning - table exists OK ");
+      return true;
+    }   
+
+    public void closeConnection(String databasepath){
+	sqlServer.disconnect(databasepath);
+    }
+  
+ }
Index: /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/derby/DerbySQLServer.java
===================================================================
--- /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/derby/DerbySQLServer.java	(revision 15081)
+++ /greenstone3/trunk/src/java/org/greenstone/gsdl3/sql/derby/DerbySQLServer.java	(revision 15081)
@@ -0,0 +1,70 @@
+package org.greenstone.gsdl3.sql.derby;
+
+
+import org.apache.log4j.*;
+import org.greenstone.gsdl3.sql.*;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+public class DerbySQLServer implements SQLServer{
+    static final String PROTOCOL = "jdbc:derby:";
+    static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
+    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.sql.derby.DerbySQLServer.class.getName());     
+
+    public DerbySQLServer(){
+	try{
+	    Class.forName(DRIVER).newInstance();
+	}
+        catch(Exception e){
+	    logger.error("Couldn't find derby driver: "+DRIVER,e);
+	}
+    }
+
+    public Connection connect(String databasePath){
+	try{
+	    String  protocol_str = PROTOCOL + databasePath; 
+	    Connection connection = DriverManager.getConnection(protocol_str);
+	    return connection;
+	}
+        catch(Exception e){
+	    logger.info("Connect to database "+databasePath + " failed!");
+	}
+
+	return null;
+	
+    }
+
+    public Connection connectAndCreate(String databasePath){
+	try{
+	    String  protocol_str = PROTOCOL + databasePath + ";create=true"; 
+	    Connection connection = DriverManager.getConnection(protocol_str);
+	    return connection;
+	}
+	catch(Exception e){
+	    logger.error("Connect to database "+databasePath + " failed!",e);
+	
+	}
+	return null;
+    } 
+
+    public boolean disconnect(String databasePath){
+	try{
+	    String protocol_str = PROTOCOL + databasePath + ";shutdown=true"; 
+	    DriverManager.getConnection(protocol_str);
+	}catch (SQLException se){
+	    String theError = (se).getSQLState();
+	    if (!theError.equals("08006")){
+		logger.error("Database "+databasePath + " couldn't be shut down properly!",se);
+	    }
+	    else{
+		return true;
+	    }
+	}
+	
+	return false;
+    }
+
+
+   
+}
