package org.spbu.benchmark.app;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.NClob;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;

import org.spbu.benchmark.Benchmark;
import org.spbu.benchmark.BenchmarkException;
import org.spbu.benchmark.BenchmarkResult;
import org.spbu.benchmark.DefaultBenchmark;
import org.spbu.benchmark.LoggingSupport;
import org.spbu.benchmark.Scenario;
import org.spbu.benchmark.ThreadingSettings;
import org.spbu.benchmark.web.HttpScenario;

import com.progress.open4gl.InputResultSet;
import com.progress.open4gl.Open4GLException;
import com.progress.open4gl.ProResultSetMetaData;
import com.progress.open4gl.ProResultSetMetaDataImpl;
import com.progress.open4gl.RunTimeProperties;
import com.progress.open4gl.javaproxy.Connection;
import com.progress.open4gl.javaproxy.OpenAppObject;
import com.progress.open4gl.javaproxy.ParamArray;
import com.progress.open4gl.javaproxy.ParamArrayMode;

public class Test extends LoggingSupport {

	public void run() {
		String login = "";
		String password = "";
		List<Scenario> sList = new ArrayList<Scenario>();

		ProcInvocation p;
		// AdminStaff\login.p ON DBhandle(FILL-IN-Name, FILL-IN-Password, OUTPUT
		// msg)
		// 
		/*  try{ 
		 * ParamArray params = new ParamArray(3); 
		 * params.addCharacter(0, "admin", ParamArrayMode.INPUT); 
		 * params.addCharacter(1, "admin", ParamArrayMode.INPUT); 
		 * params.addCharacter(2, null, ParamArrayMode.OUTPUT); 
		 * AppAction a1 = new AppAction(new ProcInvocation("AdminStaff\\login.p",params));
		 * s.addAction(a1);
		 * }catch(Open4GLException oe){ log.error("Param error", oe); }
		 */

		//
		/*
		try{ 
			ParamArray	params = new ParamArray(1); 
			ProResultSetMetaDataImpl meta = new ProResultSetMetaDataImpl(6);
			meta.setFieldMetaData(1, "audit_id", 0, Parameter.PRO_INTEGER);
			meta.setFieldMetaData(2, "audit_date", 0, Parameter.PRO_DATETIME);
			meta.setFieldMetaData(3, "DATE", 0, Parameter.PRO_DATE);
			meta.setFieldMetaData(4, "audit_message", 0, Parameter.PRO_CHARACTER);
			meta.setFieldMetaData(5, "user_id", 0, Parameter.PRO_INTEGER);
			meta.setFieldMetaData(6, "login", 0, Parameter.PRO_CHARACTER);

			params.addTable(0, null, ParamArrayMode.OUTPUT, meta);
			AppAction a2 = new AppAction(new ProcInvocation("AdminStaff\\init-Audit.p",params)); 
			s.addAction(a2);
		}catch(Exception oe){ log.error("Param error", oe); }*/
		List<String> users = new ArrayList<String>();
		users.add("a.alekseev                      ");
		users.add("y.nenahova                      ");
		users.add("e.kraev                         ");
		users.add("n.lupakova                      ");
		users.add("v.lupakova                      ");
		users.add("o.butova                        ");
		/*users.add("p.shesterikova                  ");
		users.add("n.emelyanova                    ");
		users.add("a.stepina                       ");
		users.add("g.avdeeva                       ");
		users.add("e.terenteva                     ");
		users.add("i.juzhakova                     ");
		users.add("m.eropolov                      ");
		users.add("k.osipovskaya                   ");
		users.add("m.dmitrieva                     ");
		users.add("k.andreev                       ");
		users.add("v.aniskovich                    ");
		users.add("i.kapichnikov                   ");
		users.add("k.ionov                         ");
		users.add("e.popova                        ");
		users.add("support                         ");
		users.add("a.shchichko                     ");
		users.add("i.zhmajlo                       ");
		users.add("m.demelkhanova                  ");
		users.add("v.belova                        ");
		users.add("e.soldatenkova                  ");
		users.add("o.slobodchukova                 ");
		users.add("a.bykova                        ");
		users.add("d.garayshina                    ");
		users.add("o.shamrikov                     ");
		users.add("o.utkina                        ");
		users.add("m.bondarev                      ");
		users.add("o.slobod                        ");
		users.add("v.lesnaya                       ");
		users.add("a.kolcov                        ");
		users.add("d.yanchev                       ");
		users.add("a.vahrusheva                    ");
		users.add("j.tagaeva                       ");
		users.add("t.pryadkina                     ");
		users.add("s.kurysheva                     ");*/
		for(String user : users)
		try{ 
			AppScenario s = new AppScenario("AppServer://localhost:5162/as_staff",
					login, password, "as_staff");
			//s.setSessionModel(1);
			p = new ProcInvocation();
			p.setProcName("Manager\\employeeLogin.p");
			p.getParameter().add(new Parameter(0,"IN",SimpleParameter.CHARACTER(),user.trim()));
			p.getParameter().add(new Parameter(1,"IN",SimpleParameter.CHARACTER(),"123"));
			p.getParameter().add(new Parameter(2,"OUT",SimpleParameter.INTEGER(),"${divId}"));
			p.getParameter().add(new Parameter(3,"OUT",SimpleParameter.INTEGER(),null));
			p.getParameter().add(new Parameter(4,"OUT",SimpleParameter.INTEGER(),null));
			p.getParameter().add(new Parameter(5,"OUT",SimpleParameter.LOGICAL(),"${ok}"));
			//p.setReturnType(SimpleParameter.LOGICAL());
			s.addAction(new AppAction(p));
			/*p = new ProcInvocation();
			p.setProcName("test-report.p");
			p.getParameter().add(new Parameter(0,"IN",SimpleParameter.INTEGER(),"${divId}"));
			s.addAction(new AppAction(p));*/
			//PersistentAppAction a = new PersistentAppAction(new ProcInvocation("Manager\\functions.p", new ParamArray(0)));
			//a.getFunctions().add(p);
			//s.addAction(a);
			p = new ProcInvocation();
			/*p.setProcName("manager\\init-salary.p");
			p.getParameter().add(new Parameter(0,"IN",SimpleParameter.INTEGER(),"${divId}"));
			p.getParameter().add(new Parameter(1,"IN",SimpleParameter.CHARACTER(),"-1"));
			p.getParameter().add(new Parameter(2,"IN",SimpleParameter.INTEGER(),"1"));
			p.getParameter().add(new Parameter(3,"IN",SimpleParameter.INTEGER(),"1"));
			TableParameter table = new TableParameter();
		    table.addMeta(new TableMetaData(1,"subdivision_id"       , SimpleParameter.INTEGER()));
		    table.addMeta(new TableMetaData(2,"subdivision_name"     , SimpleParameter.CHARACTER()));
		    table.addMeta(new TableMetaData(3 ,"employee_id"          , SimpleParameter.INTEGER()));
		    table.addMeta(new TableMetaData(4 ,"FIO"                , SimpleParameter.CHARACTER()));
		    table.addMeta(new TableMetaData(5 ,"nightduration"         , SimpleParameter.DECIMAL()));
		    table.addMeta(new TableMetaData(6 ,"dayduration"         , SimpleParameter.DECIMAL()));
		    table.addMeta(new TableMetaData(7 ,"daydurationObtainedNew"         , SimpleParameter.DECIMAL()));
		    table.addMeta(new TableMetaData(8 ,"DayShiftCount"         , SimpleParameter.INTEGER()));
		    table.addMeta(new TableMetaData(9 ,"DayShiftCountObtainedNew"         , SimpleParameter.INTEGER()));
		    table.addMeta(new TableMetaData(10 ,"NightShiftCount"         , SimpleParameter.INTEGER()));
		    table.addMeta(new TableMetaData(11 ,"ShiftDuration"           , SimpleParameter.INTEGER()));
		    table.addMeta(new TableMetaData(12 ,"Norma" , SimpleParameter.DECIMAL()));
		    table.addMeta(new TableMetaData(13 ,"PAYROLL_TYPE_ID"    , SimpleParameter.INTEGER()));
		    table.addMeta(new TableMetaData(14 ,"PAYROLL_TYPE_Name"    , SimpleParameter.CHARACTER()));
		    table.addMeta(new TableMetaData(15 ,"daysalary"               , SimpleParameter.DECIMAL()));
		    table.addMeta(new TableMetaData(16 ,"FormulaDay"    , SimpleParameter.CHARACTER()));
		    table.addMeta(new TableMetaData(17 ,"FormulaNight"    , SimpleParameter.CHARACTER()));
		    table.addMeta(new TableMetaData(18 ,"BenefitCoeff"    , SimpleParameter.DECIMAL()));
		    table.addMeta(new TableMetaData(19 ,"obtained"      , SimpleParameter.INTEGER()));
		    table.addMeta(new TableMetaData(20 ,"obtainedNew"      , SimpleParameter.INTEGER()));
		    table.addMeta(new TableMetaData(21 ,"ExpensesSum"   , SimpleParameter.DECIMAL()));
		    table.addMeta(new TableMetaData(22 ,"position_id" , SimpleParameter.INTEGER()));
		    p.getParameter().add(new Parameter(4,"OUT",table,"${t_salary}"));
		    s.addAction(new AppAction(p));*/

			/*p = new ProcInvocation();
			p.setProcName("Manager\\initialization24CurrentEmployeeList.p");
			p.getParameter().add(new Parameter(0,"IN",new SimpleParameter("INTEGER"),"${divId}"));
			p.getParameter().add(new Parameter(1,"OUT",new SimpleParameter("CHARACTER"),null));
			s.addAction(new AppAction(p));*/
			TableParameter emp_table = new TableParameter();
			emp_table.addMeta(new TableMetaData(1,"employee_id",SimpleParameter.INTEGER()));
			emp_table.addMeta(new TableMetaData(2,"last_name"            , SimpleParameter.CHARACTER()));
			emp_table.addMeta(new TableMetaData(3,"first_name"           , SimpleParameter.CHARACTER()));
			emp_table.addMeta(new TableMetaData(4,"middle_name"          , SimpleParameter.CHARACTER()));
			emp_table.addMeta(new TableMetaData(5,"phone_email"          , SimpleParameter.CHARACTER()));
			emp_table.addMeta(new TableMetaData(6,"subdivision_id"       , SimpleParameter.INTEGER())); 
			emp_table.addMeta(new TableMetaData(7,"subdivision_name"     , SimpleParameter.CHARACTER()));
			emp_table.addMeta(new TableMetaData(8,"position_id"          , SimpleParameter.INTEGER()));
			emp_table.addMeta(new TableMetaData(9,"position_name"        , SimpleParameter.CHARACTER()));
			emp_table.addMeta(new TableMetaData(10,"payroll_type_id"      , SimpleParameter.INTEGER()));
			emp_table.addMeta(new TableMetaData(11,"payroll_type_name"    , SimpleParameter.CHARACTER()));
			emp_table.addMeta(new TableMetaData(12,"isShift"    , SimpleParameter.LOGICAL()));   
			emp_table.addMeta(new TableMetaData(13,"birthday"             , SimpleParameter.DATE()));
			emp_table.addMeta(new TableMetaData(14,"entry_date"           , SimpleParameter.DATE()));
			emp_table.addMeta(new TableMetaData(15,"dismissal_date"       , SimpleParameter.DATE()));
			emp_table.addMeta(new TableMetaData(16,"dismissal_reason"     , SimpleParameter.CHARACTER()));
			emp_table.addMeta(new TableMetaData(17,"CODE"                 , SimpleParameter.CHARACTER()));
			emp_table.addMeta(new TableMetaData(18,"enrol_date"           , SimpleParameter.DATE()));
			emp_table.addMeta(new TableMetaData(19,"salary"               , SimpleParameter.DECIMAL()));
			emp_table.addMeta(new TableMetaData(20,"SALARY_FROM_DATE"           , SimpleParameter.DATE()));
			emp_table.addMeta(new TableMetaData(21,"WorkingHours"         , SimpleParameter.DECIMAL()));
			emp_table.addMeta(new TableMetaData(22,"working_percent"      , SimpleParameter.INTEGER()));
			emp_table.addMeta(new TableMetaData(23,"sex"         , SimpleParameter.CHARACTER()));
			emp_table.addMeta(new TableMetaData(24,"SIZE"      , SimpleParameter.CHARACTER()));  
			p = new ProcInvocation();
			p.setProcName("Manager\\InitializationEmployee.p");
			p.getParameter().add(new Parameter(0, "OUT", emp_table, "${t_employee}"));
			p.getParameter().add(new Parameter(1, "IN", SimpleParameter.INTEGER(), "-1"));
			p.getParameter().add(new Parameter(2, "IN", SimpleParameter.CHARACTER(), "-1"));
			p.getParameter().add(new Parameter(3, "IN", SimpleParameter.LOGICAL(), "true"));
			p.getParameter().add(new Parameter(4, "IN", SimpleParameter.LOGICAL(), "false"));
			p.getParameter().add(new Parameter(5, "IN", SimpleParameter.LOGICAL(), "true"));
			p.getParameter().add(new Parameter(6, "IN", SimpleParameter.DATE(),"1/1/2006"));
			p.getParameter().add(new Parameter(7, "IN", SimpleParameter.DATE(),"1/1/2009"));
			p.getParameter().add(new Parameter(8, "IN", SimpleParameter.DATE(),null));
			p.getParameter().add(new Parameter(9, "IN", SimpleParameter.DATE(),null));
			p.getParameter().add(new Parameter(10, "IN", SimpleParameter.CHARACTER(), ""));
			p.getParameter().add(new Parameter(11, "IN", SimpleParameter.CHARACTER(), ""));
			p.getParameter().add(new Parameter(12, "IN", SimpleParameter.LOGICAL(), "false"));
			p.getParameter().add(new Parameter(13, "IN", SimpleParameter.INTEGER(), "1"));
			p.getParameter().add(new Parameter(14, "IN", SimpleParameter.INTEGER(), "-1"));
			p.getParameter().add(new Parameter(15, "OUT", SimpleParameter.INTEGER(), "${records}"));
			s.addAction(new AppAction(p));
			p = new ProcInvocation();
			p.setProcName("Manager\\initializationtotalworkinghours.p");
			p.getParameter().add(new Parameter(0, "IN",emp_table,"${t_employee}"));
			p.getParameter().add(new Parameter(1,"IN",SimpleParameter.INTEGER(),"2009"));//year
			p.getParameter().add(new Parameter(2,"IN",SimpleParameter.INTEGER(),"1"));//month
			p.getParameter().add(new Parameter(3,"IN",SimpleParameter.INTEGER(),"${divId}"));//subdivision_id
			TableParameter table = new TableParameter();
			table.addMeta(new TableMetaData(1,"employee_id",SimpleParameter.INTEGER()));
			table.addMeta(new TableMetaData(2,"DAY"            , SimpleParameter.INTEGER()));
			table.addMeta(new TableMetaData(3,"minutes"           , SimpleParameter.INTEGER()));
			table.addMeta(new TableMetaData(4,"hourduration"          , SimpleParameter.DECIMAL()));
			table.addMeta(new TableMetaData(5,"nighthourduration"          , SimpleParameter.DECIMAL()));
			table.addMeta(new TableMetaData(6,"shift"          , SimpleParameter.CHARACTER()));
			table.addMeta(new TableMetaData(7,"addtional"          , SimpleParameter.CHARACTER()));
			table.addMeta(new TableMetaData(8,"subdivision_name"          , SimpleParameter.CHARACTER()));
			p.getParameter().add(new Parameter(4, "OUT",table,"${t_workinghours}"));
			table = new TableParameter();
			table.addMeta(new TableMetaData(1,"employee_id",SimpleParameter.INTEGER()));
			table.addMeta(new TableMetaData(2,"shift_count",SimpleParameter.CHARACTER()));
			p.getParameter().add(new Parameter(5, "OUT",table,"${t_shiftcount}"));
			s.addAction(new AppAction(p));
			sList.add(s);
			/*AppScenario s_sal = new AppScenario("AppServer://localhost:5162/as_sal_staff",
					login, password, "as_sal_staff");
			p = new ProcInvocation();
			p.setProcName("Manager\\initializationemployee_hide.p");
			p.getParameter().add(new Parameter(0,"IN-OUT",emp_table,"${t_employee}"));
			s_sal.addAction(new AppAction(p));
			s_sal.setContext(s.getContext());
			sList.add(s_sal);*/
		}catch(Exception oe){ log.error("Param error", oe); }

		/*try{ 
			Scenario s = new AppScenario("AppServer://localhost:5162/as_staff",
					login, password, "as_staff");
			ProcInvocation pp = new ProcInvocation();
			pp.setProcName("manager\\init-tt.p");
			pp.getParameter().add(new Parameter(0, "IN", SimpleParameter.INTEGER(), "1092"));
			TableParameter tt = new TableParameter();
			tt.addMeta(new TableMetaData(1,"employee_id",SimpleParameter.INTEGER()));
			tt.addMeta(new TableMetaData(2,"shift_count",SimpleParameter.CHARACTER()));
			pp.getParameter().add(new Parameter(1, "OUT",tt,"${test}"));
			s.addAction(new AppAction(pp));
			pp = new ProcInvocation();
			pp.setProcName("manager\\test-tt.p");
			pp.getParameter().add(new Parameter(0,"IN", tt,"${test}"));
			s.addAction(new AppAction(pp));
			sList.add(s);
		}catch(Exception oe){ log.error("Param error", oe); }*/
		/*try{
			OpenAppObject oa = new OpenAppObject("AppServer://localhost:5162/as_staff",
					login, password, "as_staff");
			ParamArray ppp = new ParamArray(2);
			ProResultSetMetaDataImpl m = new ProResultSetMetaDataImpl(2);
			m.setFieldMetaData(1, "employee_id", 0, com.progress.open4gl.Parameter.PRO_INTEGER);
			m.setFieldMetaData(2, "shift_count", 0, com.progress.open4gl.Parameter.PRO_CHARACTER);
			ppp.addInteger(0, 1092, ParamArrayMode.INPUT);
			ppp.addTable(1, null, ParamArrayMode.OUTPUT, m);
			oa.runProc("manager\\init-tt.p", ppp);
			ProxyResultSet res = new ProxyResultSet((ResultSet)ppp.getOutputParameter(1));
			ParamArray pa = new ParamArray(1);
			
			pa.addTable(0, res, ParamArrayMode.INPUT, m);
			
			oa.runProc("manager\\test-tt.p", pa);

			oa._release();
		}catch(Exception oe){ log.error("Param error", oe); }*/
		/*try{
			Scenario s3 = new AppScenario("AppServer://localhost:5162/as_staff",
					login, password, "as_staff");
			p = new ProcInvocation();
			p.setProcName("Manager\\initializationCurrentEmployeeList.p");
			p.getParameter().add(new Parameter(0,"IN",SimpleParameter.INTEGER(),"230"));//subdivision_id
			TableParameter table = new TableParameter();
			table.addMeta(new TableMetaData(1,"working_time_id",SimpleParameter.INTEGER()));
			table.addMeta(new TableMetaData(2,"employee_id",SimpleParameter.INTEGER()));
			table.addMeta(new TableMetaData(3,"employee_name"           , SimpleParameter.CHARACTER()));
			table.addMeta(new TableMetaData(4,"subdivision_name"          , SimpleParameter.CHARACTER()));
			table.addMeta(new TableMetaData(5,"work_start_time"          , SimpleParameter.CHARACTER()));
			p.getParameter().add(new Parameter(1, "OUT",table,null));
			s3.addAction(new AppAction(p));
			sList.add(s3);
		}catch(Exception oe){ log.error("Param error", oe); }	*/

	
		/*ThreadingSettings settings = new ThreadingSettings();
		settings.setLaunchInterval(200);
		settings.setTotalThreads(15);
		Benchmark b = new DefaultBenchmark(sList, settings);



		try {
			BenchmarkResult bresult = b.start();
			NumberFormat f = new DecimalFormat("00.00000");
			results.info("Benchmark success\n" + bresult.succeeded()
					+ " scenarios succeeded, " + bresult.failed()
					+ " scenarios failed\n" + "Response time: "
					+ f.format((float) bresult.avgResponse() / 1000)
					+ " average, "
					+ f.format((float) bresult.maxResponse() / 1000) + " max, "
					+ f.format((float) bresult.minResponse() / 1000) + " min\n"
					+ "Average throughput: " + f.format(bresult.throughput())
					+ " requests/second");
		} catch (BenchmarkException be) {
			log.error("Error:", be);
		}*/
	}
	public void logResultSet(ResultSet source){
		try{
		ResultSetMetaData meta = source.getMetaData();
		while(source.next()){
			String s = "";
			for(int i = 0;i<meta.getColumnCount();i++) s+=" "+source.getObject(i+1);
			log.debug(s);
		}
		}catch(SQLException sqe){log.error("ResultSet error",sqe);}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Test t = new Test();
		t.run();

	}
}
