using System;
using System.Web;
using System.Web.SessionState;
using edu.jhu.Cas.Jobs;
using edu.jhu.Cas.Util;
using System.Data.SqlClient;
using System.Collections;
namespace edu.jhu.Cas.Web
{
	/// <summary>
	/// Some utils for the ASPX pages
	///   $Id: WebUtils.cs 151352 2013-11-04 19:49:48Z dmedv $
	///   $Revision: 151352 $
	///   $Date: 2013-11-04 14:49:48 -0500 (Mon, 04 Nov 2013) $
	/// </summary>
	public class WebUtils
	{
		public  static string GetUniqueTableName(long wsid){
			try{
				string[] existingTables = (new MyDBJobs()).Tables(wsid);
				string baseTable = "MyTable";
				int i = 0;
				string rst = baseTable;
				bool unique = false;
				while(!unique){
					unique = true;
					foreach(string s in existingTables)
						if(s.ToUpper().Trim().CompareTo(rst.ToUpper().Trim())== 0)
							unique = false;
					if(!unique){rst = baseTable+"_"+(i++);}
				}
				return rst;
			}catch{return "MyTable";}
		}

        public static string GetUniqueTableNameEx(long wsid)
        {
            try
            {
                string[] existingTables = (new MyDBJobs()).Tables(wsid);

                ArrayList pendingTables = new ArrayList();
                using (SqlConnection conn = new SqlConnection(SqlFactory.JOBS_STRING))
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "SELECT Created_Table FROM Jobs WHERE (WebServicesID = @wsid) AND (Status <= 2)";
                        cmd.Parameters.AddWithValue("@wsid", wsid);
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read()) 
                            {
                                pendingTables.Add(reader.GetString(0));
                            }
                        }
                    }
                }

                string baseTable = "MyTable";
                int i = 0;
                string rst = baseTable;
                bool unique = false;
                while (!unique)
                {
                    unique = true;
                    foreach (string s in existingTables)
                        if (s.ToUpper().Trim().CompareTo(rst.ToUpper().Trim()) == 0)
                            unique = false;

                    if (unique)
                    {
                        foreach (string s in pendingTables)
                            if (s.ToUpper().Trim().CompareTo(rst.ToUpper().Trim()) == 0)
                                unique = false;
                    }

                    if (!unique) { rst = baseTable + "_" + (i++); }
                }
                return rst;
            }
            catch { return "MyTable"; }
        }

		// check if the user is logged in 
		// if not forward to the login page and when logged in 
		// come back to this page
        public static void CheckLoggedIn(HttpSessionState session, HttpResponse resp, HttpRequest req, bool noTop) {
            CheckLoggedIn(session, resp, req, noTop, "");
        }
		public static void CheckLoggedIn(HttpSessionState session, HttpResponse resp, HttpRequest req,bool noTop, string pref)
		{
			if (session["USER"] == null ) 
			{
				resp.Redirect(pref+"login.aspx?noTop="+noTop.ToString()+"&lmessage=Please Log in - your session may have expired&message=There was a problem&nexturl="+HttpUtility.UrlEncode(req.RawUrl));
			}
		}
		public static void CheckLoggedIn(HttpSessionState session, HttpResponse resp, HttpRequest req)
		{
			CheckLoggedIn(session,resp,req,false);
		}
		public static void CheckLoggedInREMOVE(HttpSessionState session, HttpResponse resp)
		{
			if (session["USER"] == null ) 
			{
				resp.Redirect("login.aspx?lmessage=Please Log in - your session may have expired&message=There was a problem");
			}
		}
		public WebUtils()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		/// 
		/// Revision from CVS
		/// 
		public static string Revision
		{
			get
			{
				return "$Revision: 151352 $";
			}
		}
	}
}
/* Revision History
   $Log$
   Revision 1.5  2007/09/20 03:43:49  nli
   bigplot

   Revision 1.4  2006/09/14 16:34:32  nli
   minor cosmetic changes

   Revision 1.3  2006/09/13 15:47:40  nli
   back from summer
   Revision 1.2  2004/09/07 18:50:30  nli
   --import ripped from sky
   Revision 1.1.1.1  2004/04/27 16:38:31  cvsuser
   import CasJobs to FERMI
   Revision 1.1.1.1  2004/02/18 16:38:28  nli
   new version
   Revision 1.1.1.1  2004/01/25 18:16:58  nli
   first
   Revision 1.1.1.1  2004/01/20 20:59:57  nli
   new
   Revision 1.3  2003/07/28 20:01:26  nli
   -authentication in CasUsers
   -build.build compiles extras from reference
   Revision 1.2  2003/07/01 23:18:19  womullan
   cancel job and re submit
   Revision 1.1  2003/06/30 14:05:48  womullan
    better queues
   
*/
