/*
 * 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 OSASession {
    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 HttpSession setSession (HttpSession sess, boolean isPreRelease, String progID,String user, String community) {
    	 	sess.setAttribute("osalogin", Boolean.TRUE); 
        	
            if (!OSASchema.isProprietary(community)) {
            sess.setAttribute("osauser", user.toLowerCase().trim());
            }
            else {
                sess.setAttribute("osauser", user.trim());
            }
            sess.setAttribute("osacommunity", community.toUpperCase());
            if (OSASchema.isProprietary(community)){
                sess.setAttribute(OSASchema.SESSIONPROPRIETARYCOMMUNITY, Boolean.TRUE);
            }
            sess.setAttribute("osaprogrammeID",progID);
    	return sess;
    }
    
    public static String getDBUser(HttpSession sess) {
        String community=getCommunity(sess);
        if (getLoginBoolean(sess) && !OSASchema.isProprietary(community) && !community.equalsIgnoreCase("prerelease") && !community.equalsIgnoreCase("world")) {
            return OSASchema.UKIDSSLOGIN;
        }
        if (getLoginBoolean(sess) && community.equalsIgnoreCase("prerelease")) {
            return WSASchema.UKIDSSLOGIN;
        }
        if (getLoginBoolean(sess) && OSASchema.isProprietary(community)) {
            int iProgID=Integer.parseInt(getProgID(sess));
            if (iProgID > 0 && iProgID < 1000) {
                return OSASchema.getShortSurveyName(iProgID).toLowerCase()+"ro";
            }
            else {
            return getUser(sess).toLowerCase()+"ro";
            }
        }
        else {
            return WSASchema.WORLDLOGIN;
        }  
    }
    
 
    
    public static int getSessionClass(HttpSession sess) {
        String community=OSASession.getCommunity(sess);
        if (OSASession.getLoginBoolean(sess)) {
            if (community.equalsIgnoreCase("nonsurvey") || community.equalsIgnoreCase(OSASchema.PROPRIETARYCOMMUNITY) || OSASchema.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("osaprogrammeID") != null) {
        return ((String) sess.getAttribute("osaprogrammeID")).trim();
        }
        else {
            return "-999";
        }
    }
    public static String getCommunity(HttpSession sess) {
        if (getLoginBoolean(sess) && sess.getAttribute("osacommunity") != null) {
            String community = ((String) sess.getAttribute("osacommunity")).toLowerCase().trim();
            if (community != null) {
                return community;
            } else {
                return "world";
            }
        } else {
            return "world";
        }
    }
    
    
    public static String getMaskedCommunity(HttpSession sess) {
        if (getLoginBoolean(sess) && sess.getAttribute("osacommunity") != null) {
            String community = ((String) sess.getAttribute("osacommunity")).toLowerCase().trim().replaceFirst(OSASchema.OSACOMMUNITYPREFIX.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("osareleasedDBs") != null)  {    
            nsDBs =(String [])sess.getAttribute("osareleasedDBs");
        }
        else {
            if (sess.getAttribute("osadbName") != null) {
                
                if (!((String) sess.getAttribute("osadbName")).toLowerCase().trim().equalsIgnoreCase("na")) {
                    
                    String [] dbName={OSASession.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("osauser") != null) {
                   return ((String)sess.getAttribute("osauser")).toLowerCase().trim();
               }
               else {
                   return "public";  
               }
           }
           else {
               int iProgID=Integer.parseInt(getProgID(sess));
               if (iProgID > 0 && iProgID < 1000 && OSASchema.getShortSurveyName(iProgID)!=null) {
                   return OSASchema.getShortSurveyName(iProgID);
               }
               else {
                   return ((String)sess.getAttribute("osauser")).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 = OSASession.getSessionClass(sess);
        String[] DB;
        switch (sessClass) {
        case WORLDCLASS:
            DB = OSASchema.getDBs(progID,true);
            if (includeMoreDB) {
                DB=OSASchema.getDBsPlusExtraDB(DB,WSASchema.moreDB);
            }
            break;
        case UKIDSSCLASS:
            if (OSASchema.accessToRollingDB && doRollingDB && progID!=OSASchema.ALLPROGRAMMEID) {
             DB=OSASchema.getDBsPlusRollingDB(OSASchema.getDBs(progID,false));
             //DB = WSASchema.UKIDSSDB;
            } else {
             DB = OSASchema.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=OSASession.getNonSurveyDBName(sess);
            if (OSASchema.accessToRollingDB && doRollingDB) {
            if (OSASession.getNonSurveyDBName(sess) != null ){
               DB= OSASchema.getDBsPlusRollingDB(OSASession.getNonSurveyDBName(sess));    
            }
            else {
            DB = new String[1];
            DB[0] = OSASchema.rollingDB;
            }
            }
            else {
                if (OSASession.getNonSurveyDBName(sess) != null ){
                DB = OSASession.getNonSurveyDBName(sess);
                }
                else {
                    DB = new String[1];
                    DB[0] = OSASchema.noAccessDB;
                }
                
            }
            if (includeExtraDB) {
                
                DB=OSASchema.getDBsPlusExtraDB(DB,OSASchema.extraDB);
            }
            break;
        case VISTATESTERCLASS:
            if (OSASchema.accessToRollingDB && doRollingDB) {
             DB=OSASchema.getDBsPlusRollingDB(OSASchema.getDBs(progID,false));
             //DB = WSASchema.UKIDSSDB;
            } else {
             DB = OSASchema.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=OSASchema.getDBsPlusRollingDB(OSASchema.getDBs(progID,false)); 
                //DB = WSASchema.UKIDSSTESTERDB;
            } else {
                DB = OSASchema.getDBs(progID,false);
            }
            if (WSASchema.doExtraDB && includeExtraDB) {
                DB=WSASchema.getDBsPlusExtraDB(DB,WSASchema.extraDB);
            }
            break;
        case PRERELEASECLASS:
            DB = OSASchema.PRERELEASEDB;
            break;
        default:
            DB = OSASchema.getDBs(progID,true);
            break;
        }
        return DB;
    }
     
     public static int[] getProgs(HttpSession sess) {
         int sessClass = OSASession.getSessionClass(sess);
         int [] progs;
         switch (sessClass) {
         case WORLDCLASS:
             progs = OSASchema.OMEGAPROGS;
             break;
         case UKIDSSCLASS:
        	 progs = OSASchema.OMEGAPROGS;
             break;
         case NONSURVEYCLASS:
             progs = new int[0];             
             break;
         case UKIDSSTESTERCLASS:
             progs = OSASchema.OMEGAPROGS;
             break;
         case VISTATESTERCLASS:            
             progs= OSASchema.OMEGATESTERPROGS;
             break;
         case PRERELEASECLASS:
             progs = OSASchema.OMEGATESTERPROGS;
             break;
         default:
             progs = OSASchema.OMEGAPROGS;
             break;
         }
         return progs;
     }
			        
			        
		
     
    public static String getSchemaVersion(HttpSession sess) {
        if (sess.getAttribute("osaschemaVersion") != null) {
        String schemaVersion=((String)sess.getAttribute("osaschemaVersion")).toLowerCase().trim();
        if (schemaVersion != null) {
        return  schemaVersion;
        }
        else {
            return "null";
        }
        }
        else {
            return "null";
        }
    }
    
    public static boolean getProprietaryBoolean(HttpSession sess) {
        if (sess.getAttribute(OSASchema.SESSIONPROPRIETARYCOMMUNITY)!= null) {
        return ((Boolean)sess.getAttribute(OSASchema.SESSIONPROPRIETARYCOMMUNITY)).booleanValue();
        }
        else {
            return false;
    }
    }
    public static boolean getSourceBoolean(HttpSession sess) {
        if (sess.getAttribute("osasource")!= null) {
        return ((Boolean)sess.getAttribute("osasource")).booleanValue();
        }
        else {
            return false;
    }
    }
    
    public static int [] getReqFilters(HttpSession sess) {
        try {
        return (int [])sess.getAttribute("osareqFilters");
        }
        catch (Exception e) {
            return null;
        }
    }
 
    
    public static boolean getLoginBoolean(HttpSession sess) {
        if (sess.getAttribute("osalogin") !=null){
            try {
                return ((Boolean)sess.getAttribute("osalogin")).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,OSASession.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,OSASession.getSchemaVersion(sess)); // database 
            }
            else {
                if (getLoginBoolean(sess)) {
                    return WSASchema.getDefaultList(WSASchema.getReqFilters(progID),table,database);
                }
                else {
                    return WSASchema.getDefaultList(WSASchema.getReqFilters(progID),table,database);
                }
            }
        }
       
    }
    

}
