import java.io.*;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;


import org.apache.log4j.Logger;


import uk.ac.roe.wfau.DatabaseConnection;
import uk.ac.roe.wfau.DatabaseURL;
import uk.ac.roe.wfau.FormatLines;
import uk.ac.roe.wfau.VDFSSchema;
import uk.ac.roe.wfau.VDFSSession;


public class HTTPSQL extends HttpServlet {
    private static String [] VSASERVERS=null;
    private static String [] WSASERVERS=null;
    private static String [] VSACUSERVERS=null;
    private static String [] WSACUSERVERS=null;
    private static String [] WSANSSERVERS=null;
    Logger logger=Logger.getLogger("wsa.simple");
    
    public void init() throws ServletException {
        //super.init(conf);
        logger=Logger.getLogger("wsa.simple");
        logger.debug("ImageList,initailizing Imagelist servlet");
        // read in global properties
        Properties properties = new Properties();
        String path = getServletContext().getRealPath("/");
        try {
            
            properties.load(new FileInputStream(path
                    + "WEB-INF/WSA.properties"));

        } catch (IOException e) {
            logger.error("ImageList,Can't find properties file ");
            return;
        }

        
        WSASERVERS=FormatLines.commaSeparatedStringToStringArray(properties.getProperty("WSASERVERS").trim());
        VSASERVERS=FormatLines.commaSeparatedStringToStringArray(properties.getProperty("VSASERVERS").trim());
        WSACUSERVERS=FormatLines.commaSeparatedStringToStringArray(properties.getProperty("WSACUSERVERS").trim());
        VSACUSERVERS=FormatLines.commaSeparatedStringToStringArray(properties.getProperty("VSACUSERVERS").trim());
        WSANSSERVERS=FormatLines.commaSeparatedStringToStringArray(properties.getProperty("WSANSSERVERS").trim());
    }
  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
    res.setContentType("text/plain");
    HttpSession sess=req.getSession();
    PrintWriter out = res.getWriter();
    String server=req.getParameter("server");
    String database=req.getParameter("database");
 
    String sqlStmt=req.getParameter("sqlStmt");
    String archive=req.getParameter("archive");
    if (archive==null) {
        archive="SSA";
    }
    String dburl;
    String [] servers;
    int archiveID=VDFSSchema.getArchiveID(archive);
    if (server!= null) {
        servers=new String [] {server};
    }
    else {
        if (archive.equalsIgnoreCase("SSA")){
            servers=new String [] {"ramses7","ramses8"};
        }
        else {
        servers = VDFSSession.getServers(VDFSSession.getCommunity(sess,archiveID),archive,WSASERVERS,WSACUSERVERS,VSASERVERS,VSACUSERVERS,WSANSSERVERS);
        }
    }
    Connection con=null;
    try {
        con=DatabaseConnection.getConnection(servers,database,archive,sess);
        DatabaseMetaData dbmd;
        dbmd = con.getMetaData();
        con=DatabaseConnection.getConnection(servers,database,archive,sess);
        out.println(dbmd.getURL());
        con.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally {
        if (con!=null) {
            try {
                con.close();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
            }
            finally {
                	con=null;   
            }
        }
    }
   
  }

  protected void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    // Data received by an HTTP POST is handled the same way
    // as data sent with a HTTP GET request... simply pass on to above...
    doGet(req, res);
  }; // END of doPost()

}
