/*
 * 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;

import sun.security.action.GetBooleanAction;

/**
 * @author mar
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class VSASession {
    static final int UKIDSSTESTERCLASS=0;
    static final int UKIDSSCLASS=1;
    static final int WORLDCLASS=2;
    static final int PRERELEASECLASS=3;
    static final int NONSURVEYCLASS=4;
    static final int VISTATESTERCLASS=5;
    
    public static String getDBUser(HttpSession sess) {
        String community=getCommunity(sess);
        if (getLoginBoolean(sess) && !community.equalsIgnoreCase("nonSurvey") && !community.equalsIgnoreCase("prerelease") && !community.equalsIgnoreCase("world")) {
            return VSASchema.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=VSASession.getCommunity(sess);
        if (VSASession.getLoginBoolean(sess)) {
            if (community.equalsIgnoreCase("nonsurvey")) {
                return NONSURVEYCLASS;
            } else if (community.equalsIgnoreCase(
            "prerelease")) {
                return PRERELEASECLASS;
            } else if  (community.equalsIgnoreCase(
            "ukidsstester")) {
                return UKIDSSTESTERCLASS;
            }
            else if  (community.equalsIgnoreCase(
            "vistatester")) {
                return VISTATESTERCLASS;
            }
            else {
                return UKIDSSCLASS;
            }
        } else {
            return WORLDCLASS;
        }
    }
    
    
    public static String getProgID(HttpSession sess) {
        if (sess.getAttribute("programmeID") != null) {
        return ((String) sess.getAttribute("programmeID")).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("releasedDBs") != null)  {    
            nsDBs =(String [])sess.getAttribute("releasedDBs");
        }
        else {
            if (sess.getAttribute("dbName") != null) {
                
                if (!((String) sess.getAttribute("dbName")).toLowerCase().trim().equalsIgnoreCase("na")) {
                    
                    String [] dbName={VSASession.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, int progID){
        return getDB(sess,progID,false);
    }
    public static String[] getDB(HttpSession sess, int progID, boolean doRollingDB){
        return getDB(sess,progID,doRollingDB,true);        
    }
    public static String[] getDB(HttpSession sess, int progID, boolean doRollingDB, boolean includeExtraDB) {
        return getDB(sess,progID,doRollingDB,includeExtraDB,false);   
    }
     public static String[] getDB(HttpSession sess, int progID, boolean doRollingDB, boolean includeExtraDB,boolean includeMoreDB) {
        int sessClass = VSASession.getSessionClass(sess);
        String[] DB;
        switch (sessClass) {
        case WORLDCLASS:
            DB = VSASchema.getDBs(progID,true);
            if (includeMoreDB) {
                DB=VSASchema.getDBsPlusExtraDB(DB,WSASchema.moreDB);
            }
            break;
        case UKIDSSCLASS:
            if (VSASchema.accessToRollingDB && doRollingDB && progID!=VSASchema.ALLPROGRAMMEID) {
             DB=VSASchema.getDBsPlusRollingDB(VSASchema.getDBs(progID,false));
             //DB = WSASchema.UKIDSSDB;
            } else {
             DB = VSASchema.getDBs(progID,false) ;
            }
            if (WSASchema.doExtraDB && includeExtraDB) {
                DB=WSASchema.getDBsPlusExtraDB(DB,WSASchema.extraDB);
            }
            if (includeMoreDB) {
                DB=WSASchema.getDBsPlusExtraDB(DB,WSASchema.moreDB);
            }
            break;
        case NONSURVEYCLASS:
            String [] nsdbs=VSASession.getNonSurveyDBName(sess);
            if (WSASchema.accessToRollingDB && doRollingDB) {
            if (VSASession.getNonSurveyDBName(sess) != null ){
               DB= WSASchema.getDBsPlusRollingDB(VSASession.getNonSurveyDBName(sess));    
            }
            else {
            DB = new String[1];
            DB[0] = WSASchema.PRERELEASEDATABASE;
            }
            }
            else {
                if (VSASession.getNonSurveyDBName(sess) != null ){
                DB = VSASession.getNonSurveyDBName(sess);
                }
                else {
                    DB = new String[1];
                    DB[0] = WSASchema.noAccessDB;
                }
                
            }
            break;
        case VISTATESTERCLASS:
            if (VSASchema.accessToRollingDB && doRollingDB) {
             DB=VSASchema.getDBsPlusRollingDB(VSASchema.getDBs(progID,false));
             //DB = WSASchema.UKIDSSDB;
            } else {
             DB = VSASchema.getDBs(progID,false) ;
            }
            if (WSASchema.doExtraDB && includeExtraDB) {
                DB=WSASchema.getDBsPlusExtraDB(DB,WSASchema.extraDB);
            }
            if (includeMoreDB) {
                DB=WSASchema.getDBsPlusExtraDB(DB,WSASchema.moreDB);
            }
            break;
            
        case UKIDSSTESTERCLASS:
            if (WSASchema.accessToRollingDB && doRollingDB) {
                DB=VSASchema.getDBsPlusRollingDB(VSASchema.getDBs(progID,false)); 
                //DB = WSASchema.UKIDSSTESTERDB;
            } else {
                DB = VSASchema.getDBs(progID,false);
            }
            if (WSASchema.doExtraDB && includeExtraDB) {
                DB=WSASchema.getDBsPlusExtraDB(DB,WSASchema.extraDB);
            }
            break;
        case PRERELEASECLASS:
            DB = VSASchema.PRERELEASEDB;
            break;
        default:
            DB = VSASchema.getDBs(progID,true);
            break;
        }
        return DB;
    }
     
     public static int[] getProgs(HttpSession sess) {
         int sessClass = VSASession.getSessionClass(sess);
         int [] progs;
         switch (sessClass) {
         case WORLDCLASS:
             progs = VSASchema.VISTAPROGS;
             break;
         case UKIDSSCLASS:
             progs = VSASchema.VISTAPROGS;
             break;
         case NONSURVEYCLASS:
             progs = new int[0];             
             break;
         case UKIDSSTESTERCLASS:
             progs = VSASchema.VISTAPROGS;
             break;
         case VISTATESTERCLASS:            
             progs= VSASchema.VISTATESTERPROGS;
             break;
         case PRERELEASECLASS:
             progs = VSASchema.VISTATESTERPROGS;
             break;
         default:
             progs = VSASchema.VISTAPROGS;
             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("source")!= null) {
        return ((Boolean)sess.getAttribute("source")).booleanValue();
        }
        else {
            return false;
    }
    }
    
    public static int [] getReqFilters(HttpSession sess) {
        try {
        return (int [])sess.getAttribute("reqFilters");
        }
        catch (Exception e) {
            return null;
        }
    }
 
    
    public static boolean getLoginBoolean(HttpSession sess) {
        if (sess.getAttribute("vsalogin") !=null){
            try {
                return ((Boolean)sess.getAttribute("vsalogin")).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,VSASession.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,VSASession.getSchemaVersion(sess)); // database 
            }
            else {
                if (getLoginBoolean(sess)) {
                    return WSASchema.getDefaultList(WSASchema.getReqFilters(progID),table,database);
                }
                else {
                    return WSASchema.getDefaultList(WSASchema.getReqFilters(progID),table,database);
                }
            }
        }
       
    }
    

}
