/*
 * 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 GESSession {
    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) && !GESSchema.isProprietary(community) && !community.equalsIgnoreCase("prerelease") && !community.equalsIgnoreCase("world")) {
            return GESSchema.UKIDSSLOGIN;
        }
        if (getLoginBoolean(sess) && community.equalsIgnoreCase(GESSchema.PRERELEASECOMMUNITY)) {
            return WSASchema.UKIDSSLOGIN;
        }
        if (getLoginBoolean(sess) && GESSchema.isProprietary(community)) {
            int iProgID=Integer.parseInt(getProgID(sess));
            if (iProgID > 0 && iProgID < 1000) {
                return GESSchema.getShortSurveyName(iProgID).toLowerCase()+"ro";
            }
            else {
            return getUser(sess).toLowerCase()+"ro";
            }
        }
        else {
            return WSASchema.WORLDLOGIN;
        }  
    }
    
 
    
    public static int getSessionClass(HttpSession sess) {
        String community=GESSession.getCommunity(sess);
        if (GESSession.getLoginBoolean(sess)) {
            if (community.equalsIgnoreCase("nonsurvey") || community.equalsIgnoreCase(GESSchema.PROPRIETARYCOMMUNITY) || GESSchema.isProprietary(community)) {
                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("gesprogrammeID") != null) {
        return ((String) sess.getAttribute("gesprogrammeID")).trim();
        }
        else {
            return "-999";
        }
    }
    public static String getCommunity(HttpSession sess) {
        if (getLoginBoolean(sess) && sess.getAttribute("gescommunity") != null) {
            String community = ((String) sess.getAttribute("gescommunity")).toLowerCase().trim();
            if (community != null) {
                return community;
            } else {
                return "world";
            }
        } else {
            return "world";
        }
    }
    
    public static String getMaskedCommunity(HttpSession sess) {
        if (getLoginBoolean(sess) && sess.getAttribute("gescommunity") != null) {
            String community = ((String) sess.getAttribute("gescommunity")).toLowerCase().trim().replaceFirst(GESSchema.GESCOMMUNITYPREFIX.toLowerCase(), "");
            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("gesreleasedDBs") != null)  {    
            nsDBs =(String [])sess.getAttribute("gesreleasedDBs");
        }
        else {
            if (sess.getAttribute("gesdbName") != null) {
                
                if (!((String) sess.getAttribute("gesdbName")).toLowerCase().trim().equalsIgnoreCase("na")) {
                    
                    String [] dbName={GESSession.getUser(sess)};
                    nsDBs=dbName;
                }
            }
        }
            return nsDBs;
        
    }
    
    
    public static String getUser(HttpSession sess) {
        return getUser(sess,false);
    }
    public static String getUser(HttpSession sess, boolean generic) {
             if (getLoginBoolean(sess)) {
           if (!generic) {
               if (sess.getAttribute("gesuser") != null) {
                   return ((String)sess.getAttribute("gesuser")).toLowerCase().trim();
               }
               else {
                   return "public";  
               }
           }
           else {
               int iProgID=Integer.parseInt(getProgID(sess));
               if (iProgID > 0 && iProgID < 1000 && GESSchema.getShortSurveyName(iProgID)!=null) {
                   return GESSchema.getShortSurveyName(iProgID);
               }
               else {
                   return ((String)sess.getAttribute("gesuser")).toLowerCase().trim();
               }
           }
       }
       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 = GESSession.getSessionClass(sess);
        System.out.println(sessClass);
        String[] DB;
        switch (sessClass) {
        case WORLDCLASS:
            DB = GESSchema.getDBs(progID,true);
            //System.out.println(DB[0]);
            if (includeMoreDB) {
                DB=GESSchema.getDBsPlusExtraDB(DB,WSASchema.moreDB);
            }
            break;
        case NONSURVEYCLASS:
            String [] nsdbs=GESSession.getNonSurveyDBName(sess);
            if (GESSchema.accessToRollingDB && doRollingDB) {
            if (GESSession.getNonSurveyDBName(sess) != null ){
               DB= GESSchema.getDBsPlusRollingDB(GESSession.getNonSurveyDBName(sess));    
            }
            else {
            DB = new String[1];
            DB[0] = GESSchema.rollingDB;
            }
            }
            else {
                if (GESSession.getNonSurveyDBName(sess) != null ){
                DB = GESSession.getNonSurveyDBName(sess);
                }
                else {
                    DB = new String[1];
                    DB[0] = GESSchema.noAccessDB;
                }
                
            }
            if (includeExtraDB) {
                
                DB=GESSchema.getDBsPlusExtraDB(DB,GESSchema.extraDB);
            }
            break;

        case PRERELEASECLASS:
            DB = GESSchema.PRERELEASEDB;
            break;
        default:
            DB = GESSchema.getDBs(progID,true);
            break;
        }
        return DB;
    }
     
     public static int[] getProgs(HttpSession sess) {
         int sessClass = GESSession.getSessionClass(sess);
         int [] progs;
         switch (sessClass) {
         case WORLDCLASS:
             progs = GESSchema.GESPROGS;
             break;
         case UKIDSSCLASS:
             progs = GESSchema.GESPROGS;
             break;
         case NONSURVEYCLASS:
             progs = new int[0];             
             break;
         case PRERELEASECLASS:
             progs = GESSchema.GESTESTERPROGS;
             break;
         default:
             progs = GESSchema.GESPROGS;
             break;
         }
         return progs;
     }
			        
			        
		
     
    public static String getSchemaVersion(HttpSession sess) {
        if (sess.getAttribute("gesschemaVersion") != null) {
        String schemaVersion=((String)sess.getAttribute("gesschemaVersion")).toLowerCase().trim();
        if (schemaVersion != null) {
        return  schemaVersion;
        }
        else {
            return "null";
        }
        }
        else {
            return "null";
        }
    }
    
    public static boolean getProprietaryBoolean(HttpSession sess) {
        if (sess.getAttribute(GESSchema.SESSIONPROPRIETARYCOMMUNITY)!= null) {
        return ((Boolean)sess.getAttribute(GESSchema.SESSIONPROPRIETARYCOMMUNITY)).booleanValue();
        }
        else {
            return false;
    }
    }
    public static boolean getSourceBoolean(HttpSession sess) {
        if (sess.getAttribute("gessource")!= null) {
        return ((Boolean)sess.getAttribute("gessource")).booleanValue();
        }
        else {
            return false;
    }
    }
    
    public static int [] getReqFilters(HttpSession sess) {
        try {
        return (int [])sess.getAttribute("gesreqFilters");
        }
        catch (Exception e) {
            return null;
        }
    }
 
    
    public static boolean getLoginBoolean(HttpSession sess) {
        if (sess.getAttribute("geslogin") !=null){
            try {
                return ((Boolean)sess.getAttribute("geslogin")).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,GESSession.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,GESSession.getSchemaVersion(sess)); // database 
            }
            else {
                if (getLoginBoolean(sess)) {
                    return WSASchema.getDefaultList(WSASchema.getReqFilters(progID),table,database);
                }
                else {
                    return WSASchema.getDefaultList(WSASchema.getReqFilters(progID),table,database);
                }
            }
        }
       
    }
    

}
