using System;
using System.Data;
using System.Threading;
using edu.jhu.Cas.Jobs;
using edu.jhu.Cas.Users;
using edu.jhu.Cas.Util;
using edu.jhu.Cas.Servers;
using edu.jhu.Cas.MyDB;

/**
  $Id: Test.cs,v 1.2 2004/08/16 20:46:29 nli Exp $
  $Revision: 1.2 $
  $Date: 2004/08/16 20:46:29 $
  */

namespace edu.jhu.Cas.Test {
	public class Test {
		public const string VERSION = "$Revision: 1.2 $";

		public static string GoodQuery = "select * from customers";
		public static string BadSyntaxQuery= "select asdf* from customers";
		public static string parserTestQry =@" Select p.objid, p.ra, p.dec, z.z 
	     FROM PhotoObj as p, PhotoObj as z
	     WHERE z.objID=p.objID
	     ";

		public static string LongAssQuery = @"

SELECT count(*)
from PhotoSecondary as s, PhotoPrimary as p, Neighbors as n, phototype
as t
where p.objID = n.objid and
      s.objID = n.neighborObjID and
      p.mode = 1 and s.mode = 2 and
      p.run != s.run and
        n.distance < 1./60. and
        ((s.flags | p.flags) & dbo.fPhotoFlags('saturated')) = 0 and
        abs(p.modelMag_g-s.modelMag_g) >

10*sqrt(p.modelMagerr_g*p.modelMagerr_g+s.modelMagerr_g*s.modelMagerr_g)
and
        s.probpsf = 1 and
        p.probpsf = 1 and
        s.type = t.value and
    s.modelMag_u <> -9999 and
        s.modelMag_g <> -9999 and
        s.modelMag_r <> -9999 and
        s.modelMag_i <> -9999 and
        s.modelMag_z <> -9999 and
        p.modelMag_u <> -9999 and
        p.modelMag_g <> -9999 and
        p.modelMag_r <> -9999 and
        p.modelMag_i <> -9999 and
        p.modelMag_z <> -9999
	    
	    ";

		public static void makeLotsOfJobs() {
			CasJobs jobs = new CasJobs();
			Random rand = new Random();
			for(int x=0; x<=10000;x++) {
				int estimate = rand.Next(0,65);
				jobs.SubmitJob(12345678,GoodQuery,"BatchTest","name_"+x.ToString(),estimate, 0);
				jobs.SubmitJob(12345678,BadSyntaxQuery,"BatchTest","name_"+x.ToString(),estimate, 0);
			}
		}

		public static void Main() {
			long wsID = 10000001;
			long wsID2 = 12345678;
			wsID = wsID;
			wsID2 = wsID2;
			CasJobs jobs = new CasJobs();
			Console.WriteLine(jobs.SubmitShortJob(193424723,parserTestQry,"BESTDR1","TESTME",1));
			/**
			jobs.CancelJob(wsID, 21);
			Console.WriteLine("=====(0)>"+ jobs.SubmitJob(wsID,"sql suckage", "hustlaz",69,69));
			Console.WriteLine("=====(1)>"+jobs.StatusJob(wsID, 21));
			jobs.FindJob(wsID, "");
			*/
			// Job j = jobs.FindJob(264635970,"")[0];
			//Console.WriteLine(j.WebServicesID+" "+j.Query+"**"+j.JobID);
			DateTime then;
			DataSet rst;
			Thread.CurrentThread.Name = "\tM";
			//	    (new CasJobs()).ListServers(2123423);
			//   (new ExtractTable()).GetDataSet(1579551075,"foo");
			/**
			for(;;){
			rst = null;
			then = DateTime.Now;
			try{
				rst = (new CasJobs()).SubmitShortJob(881861168,"select * from photoObj","skyserverv3","mydbtest",1,0);
				//   if(rst != null)Console.WriteLine("\tthis query has results?");
			}catch(Exception e){Console.WriteLine("\t***"+e.Message);}
			TimeSpan diff = DateTime.Now - then;
			Console.WriteLine("\t\t"+diff.Minutes+"m "+diff.Seconds+"s passed");
			try{
				Thread.Sleep(1000);
			}catch(Exception e){e = e;}
			}
			CasJobsAdmin ja = new CasJobsAdmin();
			/**
			ja.StartJob(wsID,21,"hosted.here","ouput_loc_here");
			ja.FailJob(wsID, 21, "smegma");
			ja.FinishedJob(wsID,20,"the job be finished",99999999);
			ja.ResubmitJob(wsID,36);
			*/
			//Console.WriteLine("==>"+ ja.IsInLowestQueue(1,"SkyServerV3"));
			CasServers cServers = new CasServers();
			//int i = cServers.ClosestQueue(12345678,"BEST", 10000);

			CasUsers users = new CasUsers();
			//Console.WriteLine(users.HasPrivilege(12345678, "admin"));
			//users.FindUserDataSet(123223, "fdasf");
			/**
			*/
			//Console.WriteLine(users.CreateAccount(1020938, "captain2", "crunch","ccc@kellogs.com","secret","yeah right", 2));
			//  Console.WriteLine(users.GetWebServiceId("jay", "secret"));
			//Console.WriteLine(users.ForgotPassword(wsID2,"bob"));
			//Console.WriteLine(users.CreateMyDB(12345678));
			//	    Console.WriteLine(users.HasMyDB(12345678));
			//	    Console.WriteLine(users.HasMyDB(142345678));
			User user = new User();
			user.Name = "Darth";
			user.UserID = "Vader";
			user.Email = "death@star.org";
			user.Password = "lightSucks";
			user.Privileges = "Admin,Query";
			user.EmailLevel = 2;

			//Console.WriteLine(users.UpdateAccount(10000001,user));
			//User[] us = users.FindUser(12345678,"webservicesid = 12345678");
			//Console.WriteLine(us.Length);
			//Console.WriteLine(users.GetUniqueUserId("Vader"));

			/**
			  makeLotsOfJobs();
			  */
			//	    ServiceThread action = new ServiceThread();
	    
			// long jId =  jobs.SubmitJob(12345678,LongAssQuery,"BEST", "long ass query",15,1);
			//jobs.SubmitShortJob(12345678,LongAssQuery+"sp_","BEST", "long ass query",15,1);
			// Thread thread = new Thread(new ThreadStart(action.start));
			// thread.Start();
			//Thread.Sleep(20*1000);
			//jobs.CancelJob(12345678,jId);

			//Console.WriteLine("==>"+(new CasUsers()).MyDBConnection(12345678));



			FilterFactory ff = new FilterFactory(); 
			//	   ff.CheckMyDb(12345678, "mydb.tableName asdlkjfalksinternain
			//Console.WriteLine(ff.FilterQuery(12345678, "mydb mydb", "SkyServerV3", 1000));
			//Console.WriteLine(ff.EnforceLinkedServerRules(parserTestQry,"server","catalog","owner"));
	    
			//	    Console.WriteLine(
			//	    ff.SwitchMyDb("Initial Catalog=bestdr1; Data Source=sdssdr1; User ID=test; Password=riuzg54;","someName"));
			CasGroups groups = new CasGroups();
			//groups.CreateGroup(1463505287,"Red Army","ph33r th15 j00");
			// groups.InviteUser(1463505287,12345678,1854088402);
			// groups.RemoveUser(1463505287,12345678,1854088402);
			//   groups.AcceptInvite(12345678, 1854088402);
			MyDBJobs mydb = new MyDBJobs();
			foreach(string s in mydb.Tables(193424723))
				Console.WriteLine(s);

		}
	}
}
/**
 * $Log: Test.cs,v $
 * Revision 1.2  2004/08/16 20:46:29  nli
 * updated casjobscl page
 *
 * 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.41  2003/10/29 19:09:39  nli
 * test
 *
 * Revision 1.40  2003/10/24 20:22:25  nli
 * bug
 *
 * Revision 1.39  2003/10/24 17:31:22  nli
 * dbo check in linked substitution
 *
 * Revision 1.38  2003/10/24 17:20:31  nli
 * some bugfixes in linked server parser
 *
 * Revision 1.37  2003/10/21 17:13:51  nli
 * virtualhost implemented, all targets are virtual
 *
 * Revision 1.36  2003/09/03 17:27:26  nli
 * grammar, all mydbs changed to users
 *
 * Revision 1.35  2003/08/29 18:08:01  nli
 *
 * asdfCVS: ----------------------------------------------------------------------
 *
 * Revision 1.34  2003/08/29 17:17:17  nli
 * spoonman
 *
 * Revision 1.33  2003/08/27 18:41:35  nli
 * pretty rows
 *
 * Revision 1.32  2003/08/26 18:41:34  nli
 * linked mydbs
 *
 * Revision 1.31  2003/08/05 21:45:10  nli
 * allyourbase
 *
 * Revision 1.30  2003/07/31 21:33:10  nli
 * it broke
 *
 * Revision 1.29  2003/07/30 20:42:30  nli
 * short job timeouts should actually be working correctly now.  empty timeouts are in there but untested
 *
 * Revision 1.28  2003/07/29 19:08:09  nli
 * -groups backend all set up and ready to go
 *
 * Revision 1.27  2003/07/28 20:17:55  nli
 * Privileges simplified.  Not bool, just throws exception
 * -added priv to casjobs
 *
 * Revision 1.26  2003/07/28 20:01:26  nli
 * -authentication in CasUsers
 * -build.build compiles extras from reference
 *
 * Revision 1.25  2003/07/28 17:01:27  nli
 * timed out queries are failed, not compelted
 *
 * Revision 1.24  2003/07/28 16:42:29  nli
 * cleaning
 *
 * Revision 1.23  2003/07/28 16:22:22  nli
 * queue times be in minutes
 *
 * Revision 1.22  2003/07/24 17:26:55  nli
 * typo
 *
 * Revision 1.21  2003/07/24 17:15:43  nli
 * various fields should be filled out correctly by outputcore
 *
 * Revision 1.20  2003/07/23 21:11:06  nli
 * -outputloc arg removed from CasJobs.StartJob
 *     this is set only by the output proceesor
 * -hostip is correctly set in submitshortjob and queuethreadaction
 * -various changes in other stuff to make the above 2 thigns work
 *
 * Revision 1.19  2003/07/21 20:39:51  nli
 * OutputCore tested working, csv not implemented
 *
 * Revision 1.18  2003/07/21 19:57:58  nli
 * untested outputcore
 *
 * Revision 1.17  2003/07/17 19:57:47  nli
 * mydb target support
 *
 * Revision 1.16  2003/07/16 16:30:08  nli
 * pCore.getBestQueue now just uses CasServers.ClosestQueue
 *
 * Revision 1.15  2003/07/16 16:14:55  nli
 * somework on pCore.getBestQueue
 *
 * Revision 1.14  2003/07/15 19:42:37  nli
 * short jobs timeout too
 *
 * Revision 1.13  2003/07/15 18:10:12  nli
 * cancel looks like its working
 *
 * Revision 1.12  2003/07/14 21:55:49  nli
 * --cancel query seems to be working.  Minimal testing.
 *
 * Revision 1.11  2003/07/14 18:08:51  nli
 * -new method in jobsadmin -- CompleteCancel
 * -Core will cancel jobs that have not started yet (still will not timeout)
 *
 * Revision 1.10  2003/07/10 17:55:41  nli
 * filter system should work now
 *
 * Revision 1.9  2003/07/09 20:24:27  nli
 * qry filtering is done; except for find nearest queue bit.
 *
 * Revision 1.8  2003/07/02 20:29:53  nli
 * --full lazy creation mydb support
 * --changed some *.aspx so non ms cronies can run it
 * --failed submitJob should set failed status
 *
 * Revision 1.7  2003/07/01 19:31:40  nli
 * --though incomplete, mydb might work now
 *
 * Revision 1.6  2003/06/27 17:38:05  nli
 * -fixed bug in HasMyDB(wsid) method
 *
 * Revision 1.5  2003/06/26 21:36:45  nli
 * added mydb creation support.   Core methods are in casusers
 *
 * Revision 1.4  2003/06/26 20:31:08  nli
 * IsInLowestQueueStatement made more robust
 *
 * Revision 1.3  2003/06/26 19:13:06  nli
 * new webmethod in CasUsers called HasMyDB(wsid)
 *
 * Revision 1.2  2003/06/25 19:05:37  nli
 * procrat working, kinda
 *
 * Revision 1.1  2003/06/24 18:52:30  nli
 * test moved to test/
 *
 * Revision 1.3  2003/06/23 16:35:25  nli
 * generated wsids restricted to positive, added procrastinate base
 *
 * Revision 1.2  2003/06/23 16:04:08  nli
 * informative exceptions in users.GetWebServiceId
 *
 * Revision 1.1  2003/06/20 16:02:25  nli
 * users done and tested (somewhat)
 *
 * Revision 1.4  2003/06/19 19:37:19  womullan
 *  users added
 *
 * Revision 1.3  2003/06/19 18:52:08  nli
 *
 * blah blah cvs
 *
 */
