protected void Page_Load(object sender, EventArgs e)
        {
            LabDB dbManager = new LabDB();
            if (Session["opCouponID"] != null && Session["opIssuer"] != null && Session["opPasscode"] != null)
            {
                opCoupon = new Coupon(Session["opIssuer"].ToString(),
                    Convert.ToInt64(Session["opCouponID"].ToString()),
                    Session["opPasscode"].ToString());
            }
            else
            {
                throw new AccessDeniedException("Missing credentials for BEE Lab graph.");
            }
            if (Request.QueryString["expid"] != null)
                experimentID = Convert.ToInt64(Request.QueryString["expid"]);
            if (Request.QueryString["min"] != null && Request.QueryString["min"].Length > 0)
                minRecord = Convert.ToInt32(Request.QueryString["min"]);
            if (Request.QueryString["max"] != null && Request.QueryString["max"].Length > 0)
                maxRecord = Convert.ToInt32(Request.QueryString["max"]) +1;
            if (Request.QueryString["index"] != null && Request.QueryString["index"].Length > 0)
                indices = Request.QueryString["index"];

            InteractiveSBProxy sbProxy = new InteractiveSBProxy();
            //ProcessAgentInfo sbInfo = dbManager.GetProcessAgentInfo(Session["opIssuer"].ToString());
            ProcessAgentInfo sbInfo = dbManager.GetProcessAgentInfo(ProcessAgentDB.ServiceAgent.domainGuid);
            if (sbInfo != null)
            {
                sbProxy.OperationAuthHeaderValue = new OperationAuthHeader();
                sbProxy.OperationAuthHeaderValue.coupon = opCoupon;
                sbProxy.Url = sbInfo.webServiceUrl;
                List<Criterion> criteria = new List<Criterion>();
                //criteria.Add(new Criterion("experiment_ID", "=", experimentID));
                criteria.Add(new Criterion("Record_Type", "=", "data"));
                if (minRecord > -1)
                    criteria.Add(new Criterion("sequence_no", ">=", minRecord.ToString()));
                if (maxRecord > -1)
                    criteria.Add(new Criterion("sequence_no", "<=", maxRecord.ToString()));
                //ExperimentSummary[] expInfo = sbProxy.RetrieveExperimentSummary(search);
                //if (expInfo.Length > 0)
                //{
                //    theExperiment = expInfo[0];
                //}
                //ExperimentRecord[] profileRecords = getRecords(sbProxy, Convert.ToInt64(hdnExperimentID.Value),
                //  new Criterion[] { new Criterion("Record_Type", "=", "profile") });
                ExperimentRecord[] records = getRecords(sbProxy, experimentID, criteria.ToArray());
                if (records != null)
                {
                    if (records.Length > 0)
                    {

                        //processProfile(profileRecords);
                        StringBuilder buf = processRecords(records, indices);
                        int bufLen = buf.Length * 2;
                        Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
                        //Response.AddHeader("Content-Length",bufLen.ToString());
                        Response.ContentType = "application/octet-stream";
                        Response.Write(buf.ToString());
                        Response.Flush();
                    }
                }
            }
        }
        protected void btnGo_Click(object sender, System.EventArgs e)
        {
            //clearExperimentDisplay();
            List<Criterion> cList = new List<Criterion>();
            LabDB dbManager = new LabDB();
            LabAppInfo clientInfo = dbManager.GetLabApp(hdnAppkey.Value);
            if (clientInfo == null)
            {
                throw new Exception("Client does not exist!");
            }
            if( clientInfo.extraInfo != null &&  clientInfo.extraInfo.Length > 0)
                cList.Add(new Criterion("clientguid", "=", clientInfo.extraInfo));
            if((ddlTimeAttribute.SelectedIndex >0))
            {
                DateTime time1 = FactoryDB.MinDbDate;
                try
                {
                    time1 = DateUtil.ParseUserToUtc(txtTime1.Text,culture,Convert.ToInt32(Session["UserTZ"]));
                }
                catch
                {
                    lblResponse.Text = Utilities.FormatErrorMessage("Please enter a valid date & time in the first time field .");
                    lblResponse.Visible = true;
                    return;
                }
                if (ddlTimeAttribute.SelectedIndex == 1)
                {
                    cList.Add(new Criterion("CreationTime", ">=", time1.ToString()));
                    cList.Add(new Criterion("CreationTime", "<", time1.AddDays(1).ToString()));
                }
                else if (ddlTimeAttribute.SelectedIndex == 2)
                {
                    cList.Add(new Criterion("CreationTime", "<", time1.ToString()));
                }
                else if (ddlTimeAttribute.SelectedIndex == 3)
                {
                    cList.Add(new Criterion("CreationTime", ">=", time1.ToString()));
                }
                if (ddlTimeAttribute.SelectedIndex == 4)
                {
                    DateTime time2 = FactoryDB.MaxDbDate;
                    try
                    {
                        time2 = DateUtil.ParseUserToUtc(txtTime2.Text, culture, Convert.ToInt32(Session["UserTZ"]));
                    }
                    catch
                    {
                        lblResponse.Text = Utilities.FormatErrorMessage("Please enter a valid date & time in the second time field.");
                        lblResponse.Visible = true;
                        return;
                    }
                    cList.Add(new Criterion("CreationTime", ">=", time1.ToString()));
                    cList.Add(new Criterion("CreationTime", "<=", time2.ToString()));
                }
            }

            InteractiveSBProxy sbProxy = new InteractiveSBProxy();
            ProcessAgentInfo sbInfo = dbManager.GetProcessAgentInfo(hdnIssuer.Value);
            if (sbInfo != null)
            {
                sbProxy.OperationAuthHeaderValue = new OperationAuthHeader();
                sbProxy.OperationAuthHeaderValue.coupon
                    = new Coupon(hdnIssuer.Value, Convert.ToInt64(hdnCoupon.Value), hdnPasscode.Value);
                sbProxy.Url = sbInfo.webServiceUrl;

                List<Criterion> cri = new List<Criterion>();
                cri.Add(new Criterion("clientguid", "=", clientInfo.extraInfo));
                ExperimentSummary[] expSum = sbProxy.RetrieveExperimentSummary(cri.ToArray());
                if (expSum.Length == 0)
                {
                    string msg = "No experiment records were found for user '" + Session["UserName"] + "' in group '" + Session["GroupName"] + "'.";
                    lblResponse.Text = Utilities.FormatErrorMessage(msg);
                    lblResponse.Visible = true;
                }
                listSummaries(expSum);

            }
        }
        protected void Page_Load(object sender, System.EventArgs e)
        {
            LabDB dbManager = new LabDB();
            if (!IsPostBack)
            {
                // Query values from the request
                //clearSessionInfo();
                hdnAppkey.Value = Request.QueryString["app"];
                hdnCoupon.Value = Request.QueryString["coupon_id"];
                hdnPasscode.Value = Request.QueryString["passkey"];
                hdnIssuer.Value = Request.QueryString["issuer_guid"];
                hdnSbUrl.Value = Request.QueryString["sb_url"];
                if (hdnSbUrl.Value != null && hdnSbUrl.Value.Length > 0)
                    Session["sbUrl"] = hdnSbUrl.Value;
                else
                    Session.Remove("sbUrl");
                string userName = null;
                string userIdStr = null;

                int tz = 0;
                if (Session["userTZ"] != null)
                    tz = Convert.ToInt32(Session["userTZ"]);

                // this should be the RedeemSession & Experiment Coupon data
                if (!(hdnPasscode.Value != null && hdnPasscode.Value != ""
                    && hdnCoupon.Value != null && hdnCoupon.Value != ""
                    && hdnIssuer.Value != null && hdnIssuer.Value != ""))
                {
                    Logger.WriteLine("BEEanaylsis: " + "AccessDenied missing credentials");
                    Response.Redirect("AccessDenied.aspx?text=missing+credentials.", true);
                }

                Coupon expCoupon = new Coupon(hdnIssuer.Value, Convert.ToInt64(hdnCoupon.Value), hdnPasscode.Value);

                //Check the database for ticket and coupon, if not found Redeem Ticket from
                // issuer and store in database.
                //This ticket should include group, experiment id and be valid for this moment in time??
                Ticket expTicket = dbManager.RetrieveAndVerify(expCoupon, TicketTypes.EXECUTE_EXPERIMENT);

                if (expTicket != null)
                {
                    if (expTicket.IsExpired())
                    {
                        Response.Redirect("AccessDenied.aspx?text=The ExperimentExecution+ticket+has+expired.", true);

                    }
                    Session["exCoupon"] = expCoupon;

                    ////Parse experiment payload, only get what is needed
                    string payload = expTicket.payload;
                    XmlQueryDoc expDoc = new XmlQueryDoc(payload);
                    string expIdStr = expDoc.Query("ExecuteExperimentPayload/experimentID");
                    string tzStr = expDoc.Query("ExecuteExperimentPayload/userTZ");
                    //string userIdStr = expDoc.Query("ExecuteExperimentPayload/userID");
                    string groupName = expDoc.Query("ExecuteExperimentPayload/groupName");
                    Session["groupName"] = groupName;
                    string sbStr = expDoc.Query("ExecuteExperimentPayload/sbGuid");
                    Session["brokerGUID"] = sbStr;

                    if ((tzStr != null) && (tzStr.Length > 0))
                    {
                        Session["userTZ"] = tzStr;
                    }
                    LabAppInfo clientInfo = dbManager.GetLabApp(hdnAppkey.Value);
                    if (clientInfo == null)
                    {
                        throw new Exception("Client does not exist!");
                    }
                    long experimentID = Convert.ToInt64(expIdStr);
                    InteractiveSBProxy sbProxy = new InteractiveSBProxy();
                    ProcessAgentInfo sbInfo = dbManager.GetProcessAgentInfo(hdnIssuer.Value);
                    if (sbInfo != null)
                    {
                        sbProxy.OperationAuthHeaderValue = new OperationAuthHeader();
                        sbProxy.OperationAuthHeaderValue.coupon = expCoupon;
                        sbProxy.Url = sbInfo.webServiceUrl;
                        // Use the extraInfo field to provide the target ClientGUID
                        List<Criterion> cri = new List<Criterion>();
                        if (clientInfo.extraInfo != null && clientInfo.extraInfo.Length > 0)
                            cri.Add(new Criterion("clientguid", "=", clientInfo.extraInfo));

                        ExperimentSummary[] expSum = sbProxy.RetrieveExperimentSummary(cri.ToArray());
                        listSummaries(expSum);
                    }
                }
            }
        }
        protected void lbxSelectExperiment_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            LabDB dbManager = new LabDB();
            //clearSessionInfo();
            //clearExperimentDisplay();
            long experimentID = Int64.Parse (lbxSelectExperiment.Items [lbxSelectExperiment.SelectedIndex ].Value);
            InteractiveSBProxy sbProxy = new InteractiveSBProxy();
            ProcessAgentInfo sbInfo = dbManager.GetProcessAgentInfo(hdnIssuer.Value);
            if (sbInfo != null)
            {
                sbProxy.OperationAuthHeaderValue = new OperationAuthHeader();
                sbProxy.OperationAuthHeaderValue.coupon
                    = new Coupon(hdnIssuer.Value, Convert.ToInt64(hdnCoupon.Value), hdnPasscode.Value);
                sbProxy.Url = sbInfo.webServiceUrl;
                try
                {
                    //ExperimentSummary[] expInfo = wrapper.GetExperimentSummaryWrapper(new long[] { experimentID });
                    Criterion[] search = new Criterion[] { new Criterion("experiment_ID", "=", experimentID.ToString()) };
                    ExperimentSummary[] expInfo = sbProxy.RetrieveExperimentSummary(search);

                    if (expInfo[0] != null)
                    {
                        displayExperimentSummary(expInfo[0]);
                    }
                }
                catch (Exception ex)
                {
                    lblResponse.Text = Utilities.FormatErrorMessage("Error retrieving experiment information. " + ex.Message);
                    lblResponse.Visible = true;
                }
            }
        }
 protected void btnSaveAnnotation_Click(object sender, System.EventArgs e)
 {
     lblResponse.Visible=false;
     LabDB dbManager = new LabDB();
     try
     {
         InteractiveSBProxy sbProxy = new InteractiveSBProxy();
         ProcessAgentInfo sbInfo = dbManager.GetProcessAgentInfo(hdnIssuer.Value);
         if (sbInfo != null)
         {
             sbProxy.OperationAuthHeaderValue = new OperationAuthHeader();
             sbProxy.OperationAuthHeaderValue.coupon
                 = new Coupon(hdnIssuer.Value, Convert.ToInt64(hdnCoupon.Value), hdnPasscode.Value);
             sbProxy.Url = sbInfo.webServiceUrl;
             sbProxy.SetAnnotation(Int32.Parse(txtExperimentID.Text), txtAnnotation.Text);
             Criterion[] search = new Criterion[] { new Criterion("experiment_ID", "=", txtExperimentID.Text) };
             ExperimentSummary[] expInfo = sbProxy.RetrieveExperimentSummary(search);
             if (expInfo.Length > 0)
             {
                 displayExperimentSummary(expInfo[0]);
             }
             lblResponse.Text = Utilities.FormatConfirmationMessage("Annotation saved for experiment ID " + txtExperimentID.Text);
             lblResponse.Visible = true;
         }
     }
     catch (Exception ex)
     {
         lblResponse.Text = Utilities.FormatErrorMessage("Error saving experiment annotation. " + ex.Message);
         lblResponse.Visible = true;
     }
 }
        protected void Page_Load(object sender, System.EventArgs e)
        {
            if(Session["userTZ"] != null)
                tz = Convert.ToInt32(Session["userTZ"]);
            String returnURL = (string)Session["sbUrl"];
            if ((returnURL != null) && (returnURL.Length > 0))
            {
                lnkBackSB.NavigateUrl = returnURL;
                lnkBackSB.Visible = true;
            }
            else{
                lnkBackSB.Visible = false;
            }
            if (Session["opCouponID"] != null && Session["opIssuer"] != null && Session["opPasscode"] != null)
            {
                opCoupon = new Coupon(Session["opIssuer"].ToString(), Convert.ToInt64(Session["opCouponID"].ToString()), Session["opPasscode"].ToString());

            }
            else
            {
                throw new AccessDeniedException("Missing credentials for BEE Lab graph.");
            }
            LabDB dbManager = new LabDB();
            if (!IsPostBack)
            {
                lblResponse.Text = "";
                lblResponse.Visible = false;
                // Query values from the request
                hdnExperimentID.Value = Request.QueryString["expid"];
                if (hdnExperimentID.Value != null && hdnExperimentID.Value.Length > 0)
                    hdnChannelID.Value = ChecksumUtil.ToMD5Hash("BEElab" + hdnExperimentID.Value);
                else
                    hdnChannelID.Value = "experiment-channel";

                if (Session["opCouponID"] != null && Session["opIssuer"] != null && Session["opPasscode"] != null)
                {
                    // Save the coupon data for when the session times out
                    ViewState["opCouponID"] = Session["opCouponID"].ToString();
                    ViewState["opIssuer"] = Session["opIssuer"].ToString();
                    ViewState["opPasscode"] = Session["opPasscode"].ToString();
                }

                InteractiveSBProxy sbProxy = new InteractiveSBProxy();
                ProcessAgentInfo sbInfo = dbManager.GetProcessAgentInfo(Session["opIssuer"].ToString());
                if (sbInfo != null)
                {
                    sbProxy.OperationAuthHeaderValue = new OperationAuthHeader();
                    sbProxy.OperationAuthHeaderValue.coupon = opCoupon;
                    sbProxy.Url = sbInfo.webServiceUrl;

                    //Criterion[] search = new Criterion[] { new Criterion("experiment_ID", "=", hdnExperimentID.Value) };
                    //ExperimentSummary[] expInfo = sbProxy.RetrieveExperimentSummary(search);
                    //if (expInfo.Length > 0)
                    //{
                    //    theExperiment = expInfo[0];
                    //}
                    ExperimentRecord[] profileRecords = getRecords(sbProxy, Convert.ToInt64(hdnExperimentID.Value),
                      new Criterion[] { new Criterion("Record_Type", "=", "profile") });
                    ExperimentRecord[] records = getRecords(sbProxy,Convert.ToInt64(hdnExperimentID.Value),
                       new Criterion[] { new Criterion("Record_Type", "=", "data")});
                    if(hdnChannelID.Value != null)
                        writeChannelID(hdnChannelID.Value);
                    //processProfile(profileRecords);
                    processRecords(records);
                }
            }
        }