/*
 * Created on 04-Nov-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.sql.Types;

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 WSASession {
	public static final int UKIDSSTESTERCLASS=0;
	public static final int UKIDSSCLASS=1;
	public static final int WORLDCLASS=2;
	public static final int PRERELEASECLASS=3;
	public static final int NONSURVEYCLASS=4;
    
    public static String getDBUser(HttpSession sess) {
        String community=getCommunity(sess);
        if (community != null && community.equalsIgnoreCase("roe.ac.uk") && getUser(sess).equalsIgnoreCase("gpsDR8")) {
            return "testro";
        }
        if (getLoginBoolean(sess) && !community.equalsIgnoreCase("nonSurvey") && !community.equalsIgnoreCase("prerelease") && !community.equalsIgnoreCase("world")) {
            return WSASchema.UKIDSSLOGIN;
        }
        if (getLoginBoolean(sess) && community.equalsIgnoreCase("prerelease")) {
            return WSASchema.UKIDSSLOGIN;
        }
        if (getLoginBoolean(sess) && community.equalsIgnoreCase("nonsurvey")) {
            return getUser(sess)+"ro";
        }
        else {
            return WSASchema.WORLDLOGIN;
        } 
    }

    
    public static int getSessionClass(HttpSession sess) {
        String community=WSASession.getCommunity(sess);
        if (WSASession.getLoginBoolean(sess)) {
            if (community.equalsIgnoreCase("nonsurvey")) {
                return NONSURVEYCLASS;
            } else if (community.equalsIgnoreCase(
            "prerelease")) {
                return PRERELEASECLASS;
            } else if  (community.equalsIgnoreCase(
            "ukidsstester")) {
                return UKIDSSTESTERCLASS;
            }
            else {
                return UKIDSSCLASS;
            }
        } else {
            return WORLDCLASS;
        }
    }
    
    
    public static String getProgID(HttpSession sess) {
        if (sess.getAttribute("wsaprogrammeID") != null) {
        return ((String) sess.getAttribute("wsaprogrammeID")).trim();
        }
        else {
            return "-999";
        }
    }
    public static String getCommunity(HttpSession sess) {
        if (sess.getAttribute("community") != null) {
            String community = ((String) sess.getAttribute("community")).toLowerCase().trim();
            if (community != null) {
                return community;
            } else {
                return "world";
            }
        } else {
            return "world";
        }
    }
 /*  
    public static String getNonSurveyDBName(HttpSession sess) {
        if (sess.getAttribute("dbName") != null) {
            return((String) sess.getAttribute("dbName")).toLowerCase().trim();
        } else {
            return null;
        }
    }
    */
    public static String [] getNonSurveyDBName(HttpSession sess) {
        String [] nsDBs= null;
        if (sess.getAttribute("wsareleasedDBs") != null)  {    
            nsDBs =(String [])sess.getAttribute("wsareleasedDBs");
        }
        else {
            if (sess.getAttribute("wsadbName") != null) {
                
                if (!((String) sess.getAttribute("wsadbName")).toLowerCase().trim().equalsIgnoreCase("na")) {
                    
                    String [] dbName={WSASession.getUser(sess)};
                    nsDBs=dbName;
                }
            }
        }
            return nsDBs;
        
    }
    
    public static String getUser(HttpSession sess) {
        if (sess.getAttribute("user") != null) {
        String user=((String)sess.getAttribute("user")).toLowerCase().trim();
        if (user != null) {
        return  user;
        }
        else {
            return "public";
        }
        }
        else {
            return "public";
        }
    }
    public static String[] getDB(HttpSession sess){
        return getDB(sess,false);
    }
    public static String[] getDB(HttpSession sess,boolean doRollingDB){
        return getDB(sess,doRollingDB,true);        
    }
    public static String[] getDB(HttpSession sess, boolean doRollingDB, boolean includeExtraDB) {
        return getDB(sess,doRollingDB,includeExtraDB,false);   
    }
     public static String[] getDB(HttpSession sess, boolean doRollingDB, boolean includeExtraDB,boolean includeMoreDB) {
        int sessClass = WSASession.getSessionClass(sess);
        String[] DB;
        switch (sessClass) {
        case WORLDCLASS:
            DB = WSASchema.WORLDDB;
            DB=WSASchema.getDBsPlusExtraDB(DB,WSASchema.worldCalDBs);
            if (includeMoreDB) {
                DB=WSASchema.getDBsPlusExtraDB(DB,WSASchema.moreDB);
            }
            break;
        case UKIDSSCLASS:
            if (WSASchema.accessToRollingDB && doRollingDB) {
             DB=WSASchema.getDBsPlusRollingDB(WSASchema.UKIDSSDB);
             //DB = WSASchema.UKIDSSDB;
            } else {
             DB = WSASchema.UKIDSSDB;
            }
            DB=WSASchema.getDBsPlusExtraDB(DB,WSASchema.calDBs);
            if (WSASchema.doExtraDB && includeExtraDB) {
                DB=WSASchema.getDBsPlusExtraDB(DB,WSASchema.extraDB);
            }
            if (includeMoreDB) {
                DB=WSASchema.getDBsPlusExtraDB(DB,WSASchema.moreDB);
            }
            break;
        case NONSURVEYCLASS:
            String [] nsdbs=WSASession.getNonSurveyDBName(sess);
            if (WSASchema.accessToRollingDB && doRollingDB) {
            if (WSASession.getNonSurveyDBName(sess) != null ){
               DB= WSASchema.getDBsPlusRollingDB(WSASession.getNonSurveyDBName(sess));    
            }
            else {
            DB = new String[1];
            DB[0] = WSASchema.PRERELEASEDATABASE;
            }
            }
            else {
                if (WSASession.getNonSurveyDBName(sess) != null ){
                DB = WSASession.getNonSurveyDBName(sess);
                }
                else {
                    DB = new String[1];
                    DB[0] = WSASchema.noAccessDB;
                }
                
            }
            break;
        case UKIDSSTESTERCLASS:
            if (WSASchema.accessToRollingDB && doRollingDB) {
                DB=WSASchema.getDBsPlusRollingDB(WSASchema.UKIDSSTESTERDB); 
                //DB = WSASchema.UKIDSSTESTERDB;
            } else {
                DB = WSASchema.UKIDSSTESTERDB;
            }
            DB=WSASchema.getDBsPlusExtraDB(DB,WSASchema.calDBs);
            if (WSASchema.doExtraDB && includeExtraDB) {
                DB=WSASchema.getDBsPlusExtraDB(DB,WSASchema.extraDB);
            }
            break;
        case PRERELEASECLASS:
            DB = WSASchema.PRERELEASEDB;
            break;
        default:
            DB = WSASchema.WORLDDB;
            break;
        }
        return DB;
    }
     
     public static int[] getProgs(HttpSession sess) {
    	 return getProgs(sess,false);
     }
     
     public static int[] getProgs(HttpSession sess, boolean showUHS) {
         int sessClass = WSASession.getSessionClass(sess);
         int [] progs;
         switch (sessClass) {
         case WORLDCLASS:
             progs = WSASchema.WORLDPROGS;
             break;
         case UKIDSSCLASS:
        	 if (showUHS){
        		 progs=WSASchema.UKIDSSUHSPROGS;
        	 }
        	 else {
             progs = WSASchema.UKIDSSPROGS;
        	 }
             break;
         case NONSURVEYCLASS:
             progs = new int[0];             
             break;
         case UKIDSSTESTERCLASS:
             progs = WSASchema.UKIDSSTESTERPROGS;
             break;
         case PRERELEASECLASS:
             progs = WSASchema.PRERELEASEPROGS;
             break;
         default:
             progs = WSASchema.WORLDPROGS;
             break;
         }
         return progs;
     }
			        
			        
		
     
    public static String getSchemaVersion(HttpSession sess) {
        if (sess.getAttribute("schemaVersion") != null) {
        String schemaVersion=((String)sess.getAttribute("schemaVersion")).toLowerCase().trim();
        if (schemaVersion != null) {
        return  schemaVersion;
        }
        else {
            return "null";
        }
        }
        else {
            return "null";
        }
    }
    
    
    public static boolean getSourceBoolean(HttpSession sess) {
        if (sess.getAttribute("wsasource")!= null) {
        return ((Boolean)sess.getAttribute("wsasource")).booleanValue();
        }
        else {
            return false;
    }
    }
    
    public static int [] getReqFilters(HttpSession sess) {
        try {
        return (int [])sess.getAttribute("wsareqFilters");
        }
        catch (Exception e) {
            return null;
        }
    }
    
    public static boolean getLoginBoolean(HttpSession sess) {
        if (sess.getAttribute("login") !=null){
            try {
                return ((Boolean)sess.getAttribute("login")).booleanValue();
                }
                catch (Exception e) {
                    return false;
                }
        }
        else {
            return false;
        }
    }
 
    
    
    
    public static String getDefaultList(HttpSession sess,String table,String database,int progID) {
 
        if (database==null){
            if (getCommunity(sess).equalsIgnoreCase("nonsurvey")){
                return WSASchema.getDefaultList(getReqFilters(sess),table,WSASession.getSchemaVersion(sess)); 
            }
            else {
                if (getLoginBoolean(sess)) {
                    return WSASchema.getDefaultList(WSASchema.getReqFilters(progID),table,WSASchema.LATESTUKIDSSSCHEMA);
                }
                else {
                    return WSASchema.getDefaultList(WSASchema.getReqFilters(progID),table,WSASchema.LATESTWORLDSCHEMA);
                }
            }
            
        }
        else {
            if (getCommunity(sess).equalsIgnoreCase("nonsurvey")){
                return WSASchema.getDefaultList(getReqFilters(sess),table,WSASession.getSchemaVersion(sess)); // database 
            }
            else {
                if (getLoginBoolean(sess)) {
                    return WSASchema.getDefaultList(WSASchema.getReqFilters(progID),table,database);
                }
                else {
                    return WSASchema.getDefaultList(WSASchema.getReqFilters(progID),table,database);
                }
            }
        }
       
    }
    

}
