/*
 * Created on 27-Jun-2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package uk.ac.roe.wfau;

import java.io.IOException;
import org.apache.log4j.Logger;
import java.sql.SQLException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author mar
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DatabaseURL {

    private static Logger logger=Logger.getLogger("wsa.simple");  
    
    public static String getDatabaseURL(HttpSession sess){
        return getDatabaseURL(sess,true);
    }
    public static String getDatabaseURL(HttpSession sess, boolean useTDS){
        return getDatabaseURL(sess,useTDS,WSASchema.DEFAULTSQLSERVER);
    }
    
    public static String getDatabaseURL(HttpSession sess, boolean useTDS, String server){
        boolean loginBoolean=false;
        boolean nonSurveyBoolean=false;
        if (sess.getAttribute("login") != null){
        loginBoolean=((Boolean)sess.getAttribute("login")).booleanValue();
        }
        if (sess.getAttribute("nonsurvey") != null){
        nonSurveyBoolean=((Boolean)sess.getAttribute("nonsurvey")).booleanValue();
        }
        String user=String.valueOf(sess.getAttribute("user"));
        if (loginBoolean && nonSurveyBoolean) {
            return getDatabaseURL(server,user,user+"ro",user+"ro"+"pw",useTDS);
        }
        else if (loginBoolean && !nonSurveyBoolean) {
            return getDatabaseURL(server,WSASchema.LATESTUKIDSSDATABASE,WSASchema.UKIDSSLOGIN,WSASchema.UKIDSSLOGIN+"pw",useTDS);
        }
        else {
            return getDatabaseURL(server,WSASchema.LATESTWORLDDATABASE,WSASchema.WORLDLOGIN,WSASchema.WORLDLOGIN+"pw",useTDS);
        }
        
    }
    
    public static String getDatabaseURLWSA(boolean login, String server, String databaseName,boolean useTDS){
        String user=WSASchema.WORLDLOGIN;        
        if (login) {
            user=WSASchema.UKIDSSLOGIN;
            
        }
        String passwd=user+"pw";
        return getDatabaseURL(server,databaseName,user,passwd,useTDS);
        
    }
        
    public static String getDatabaseURL(HttpSession sess, boolean useTDS, String server, String LoggedInDatabaseName, String WorldDatabaseName){
        boolean loginBoolean=false;
        boolean nonSurveyBoolean=false;
        
        logger.info("DBURL "+WSASession.getCommunity(sess)+" "+server);
        if (WSASession.getCommunity(sess).equalsIgnoreCase("preRelease")) {
            server=WSASchema.PRERELEASESERVER;
            LoggedInDatabaseName=WSASchema.PRERELEASEDATABASE;
        }
        if (sess.getAttribute("login") != null){
        loginBoolean=((Boolean)sess.getAttribute("login")).booleanValue();
        logger.info("DBURL loginboolean "+loginBoolean);
        }
        if (sess.getAttribute("nonsurvey") != null){
        nonSurveyBoolean=((Boolean)sess.getAttribute("nonsurvey")).booleanValue();
        }
        String user=String.valueOf(sess.getAttribute("user"));
        if (loginBoolean && nonSurveyBoolean) {
            return getDatabaseURL(server,LoggedInDatabaseName,user+"ro",user+"ro"+"pw",useTDS);
        }
        else if (loginBoolean && !nonSurveyBoolean) {
            return getDatabaseURL(server,LoggedInDatabaseName,WSASchema.UKIDSSLOGIN,WSASchema.UKIDSSLOGIN+"pw",useTDS);
        }
        else {
            return getDatabaseURL(server,WorldDatabaseName,WSASchema.WORLDLOGIN,WSASchema.WORLDLOGIN+"pw",useTDS);
        } 
    }
    
    public static String getDatabaseURL(String server,String database, String user, String passwd, boolean useTDS){
        String durl;
        if (database.trim().equalsIgnoreCase("ukidssdr2") || database.trim().equalsIgnoreCase("ukidssdr2plus") || 
                database.trim().equalsIgnoreCase("ukidssdr1") || database.trim().equalsIgnoreCase("ukidssdr1plus")) {
            server="thutmose";
        }
        if (useTDS){
        durl="jdbc:jtds:sqlserver://"+server+":1433/" + database+";user="+user+";password="+passwd;
        }
        else {
        durl= "jdbc:microsoft:sqlserver://"+server+":1433;databaseName="+database+";user="+user+";password="+passwd+"";
        }
        return durl;
    }
    
    public static String getDatabaseNameFromURL(String url, boolean useTDS){
        String startS=":1433";
        int startSL=startS.length()+1;
        int startI=url.indexOf(startS);
        int endI=url.indexOf(";user=",startI+startSL);
        
        if (startI > 0 && endI >=startI) {
        return url.substring(startI+startSL,endI);
        }
        else {
            return "";
        }
        
    }
}
