/// <summary>
        /// Retrive records from the experiment. It is assummed that read permissons have been checked and passed.
        /// A new coupon is created for this request.
        /// </summary>
        /// <param name="experimentID"></param>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public ExperimentRecord[] RetrieveExperimentRecords(long experimentID, Criterion[] criteria)
        {
            ExperimentRecord[] records = null;
            ProcessAgentInfo ess = GetExperimentESS(experimentID);
            if (ess != null)
            {
                ExperimentStorageProxy essProxy = new ExperimentStorageProxy();
                Coupon opCoupon = CreateCoupon();

                string payload = TicketLoadFactory.Instance().RetrieveRecordsPayload(experimentID, ess.webServiceUrl);
                opCoupon = CreateTicket(TicketTypes.RETRIEVE_RECORDS, ess.agentGuid, ProcessAgentDB.ServiceGuid,
                     60, payload);

                essProxy.OperationAuthHeaderValue = new OperationAuthHeader();
                essProxy.OperationAuthHeaderValue.coupon = opCoupon;
                essProxy.Url = ess.webServiceUrl;
                records = essProxy.GetRecords(experimentID, criteria);
            }
            return records;
        }
        public Experiment RetrieveExperiment(long experimentID, int userID, int groupID)
        {
            int roles = 0;
            Experiment experiment = null;
            AuthorizationWrapperClass wrapper = new AuthorizationWrapperClass();
            roles = wrapper.GetExperimentAuthorizationWrapper(experimentID, userID, groupID);

            if ((roles | ExperimentAccess.READ) == ExperimentAccess.READ)
            {
                experiment = new Experiment();
                experiment.experimentId = experimentID;
                experiment.issuerGuid = ProcessAgentDB.ServiceGuid;
                ProcessAgentInfo ess = GetExperimentESS(experimentID);
                if (ess != null)
                {
                    ExperimentStorageProxy essProxy = new ExperimentStorageProxy();
                    Coupon opCoupon = GetEssOpCoupon(experimentID, TicketTypes.RETRIEVE_RECORDS, 60, ess.agentGuid);
                    if (opCoupon == null)
                    {
                        string payload = TicketLoadFactory.Instance().RetrieveRecordsPayload(experimentID, ess.webServiceUrl);
                        opCoupon = CreateTicket(TicketTypes.RETRIEVE_RECORDS, ess.agentGuid, ProcessAgentDB.ServiceGuid,
                            60, payload);
                    }
                    essProxy.OperationAuthHeaderValue = new OperationAuthHeader();
                    essProxy.OperationAuthHeaderValue.coupon = opCoupon;
                    essProxy.Url = ess.webServiceUrl;
                    experiment.records = essProxy.GetRecords(experimentID, null);
                }

            }
            else
            {
                throw new AccessDeniedException("You do not have permission to read this experiment");
            }

            return experiment;
        }
        /// <summary>
        /// Retrieves an experiment's ResultReport from the ESS
        /// </summary>
        /// <param name="experimentID"></param>
        /// <param name="roles"></param>
        /// <returns>THe ResultStatus or an empty report with status set, when the experiment has not terminated that have not</returns>
        public static ResultReport GetResultReport(int experimentID)
        {
            ResultReport report = null;
            BrokerDB brokerDB = new BrokerDB();
            try
            {
                ExperimentAdminInfo expInfo = InternalDataDB.SelectExperimentAdminInfo(experimentID);
                if (expInfo == null || expInfo.experimentID <= 0)
                {
                    //experiment does not exist
                    throw new SoapException("Invalid experiment ID. ", SoapException.ServerFaultCode);
                }
                else
                {
                    ProcessAgentInfo ess = brokerDB.GetProcessAgentInfo(expInfo.essID);
                    if(ess.retired){
                        throw new Exception("The requested ESS has been retired");
                    }
                    ExperimentStorageProxy essProxy = new ExperimentStorageProxy();
                    essProxy.AgentAuthHeaderValue = new AgentAuthHeader();
                    essProxy.AgentAuthHeaderValue.agentGuid = ProcessAgentDB.ServiceGuid;
                    essProxy.AgentAuthHeaderValue.coupon = ess.identOut;
                    essProxy.Url = ess.webServiceUrl;

                    Coupon opCoupon = brokerDB.GetEssOpCoupon(expInfo.experimentID, TicketTypes.RETRIEVE_RECORDS, 60, ess.agentGuid);
                    OperationAuthHeader opHeader = new OperationAuthHeader();
                    opHeader.coupon = opCoupon;
                    essProxy.OperationAuthHeaderValue = opHeader;
                    if ((expInfo.status & StorageStatus.CLOSED) == 0)
                    {
                        ProcessAgentInfo lsInfo = brokerDB.GetProcessAgentInfo(expInfo.agentID);
                        if (lsInfo != null)
                        {
                            if(lsInfo.retired){
                                throw new Exception("The requested batch LabServer has ben retired.");
                            }
                            BatchLSProxy batchLS_Proxy = new BatchLSProxy();
                            batchLS_Proxy.AuthHeaderValue = new AuthHeader();
                            batchLS_Proxy.AuthHeaderValue.identifier = ProcessAgentDB.ServiceGuid;
                            batchLS_Proxy.AuthHeaderValue.passKey = lsInfo.identOut.passkey;
                            batchLS_Proxy.Url = lsInfo.webServiceUrl;
                            // retrieve resultReport from labServer

                            LabExperimentStatus expStatus = batchLS_Proxy.GetExperimentStatus(experimentID);
                            if (expStatus != null)
                            {
                                if ((expStatus.statusReport.statusCode >= 3) && (expStatus.statusReport.statusCode != 6))
                                {
                                    report = batchLS_Proxy.RetrieveResult(experimentID);
                                        if (report != null)
                                        {
                                            ExperimentRecord theRecord = null;
                                            List<ExperimentRecord> recordList = new List<ExperimentRecord>();
                                            if (report.experimentResults != null && report.experimentResults.Length > 0)
                                            {
                                                theRecord = new ExperimentRecord();
                                                theRecord.submitter = lsInfo.agentGuid;
                                                theRecord.type = BatchRecordType.RESULT;
                                                theRecord.contents = report.experimentResults;
                                                theRecord.xmlSearchable = false;
                                                recordList.Add(theRecord);
                                            }
                                            if (report.errorMessage != null && report.errorMessage.Length > 0)
                                            {
                                                theRecord = new ExperimentRecord();
                                                theRecord.submitter = lsInfo.agentGuid;
                                                theRecord.type = BatchRecordType.EXECUTION_ERROR;
                                                theRecord.contents = report.errorMessage;
                                                theRecord.xmlSearchable = false;
                                                recordList.Add(theRecord);
                                            }
                                            if (report.warningMessages != null && report.warningMessages.Length > 0)
                                            {
                                                foreach (string s in report.warningMessages)
                                                {
                                                    if (s.Length > 0)
                                                    {
                                                        theRecord = new ExperimentRecord();
                                                        theRecord.submitter = lsInfo.agentGuid;
                                                        theRecord.type = BatchRecordType.EXECUTION_WARNING;
                                                        theRecord.contents = s;
                                                        theRecord.xmlSearchable = false;
                                                        recordList.Add(theRecord);
                                                    }
                                                }
                                            }
                                            if (report.xmlResultExtension != null && report.xmlResultExtension.Length > 0)
                                            {
                                                theRecord = new ExperimentRecord();
                                                theRecord.submitter = lsInfo.agentGuid;
                                                theRecord.type = BatchRecordType.RESULT_EXTENSION;
                                                theRecord.contents = report.xmlResultExtension;
                                                theRecord.xmlSearchable = true;
                                                recordList.Add(theRecord);
                                            }
                                            if (report.xmlBlobExtension != null && report.xmlBlobExtension.Length > 0)
                                            {
                                                theRecord = new ExperimentRecord();
                                                theRecord.submitter = lsInfo.agentGuid;
                                                theRecord.type = BatchRecordType.BLOB_EXTENSION;
                                                theRecord.contents = report.xmlBlobExtension;
                                                theRecord.xmlSearchable = true;
                                                recordList.Add(theRecord);
                                            }
                                            if (recordList.Count > 0)
                                            {
                                                essProxy.AddRecords(experimentID, recordList.ToArray());
                                            }
                                            StorageStatus sStatus = essProxy.SetExperimentStatus(experimentID, report.statusCode | StorageStatus.CLOSED);
                                            DataStorageAPI.UpdateExperimentStatus(sStatus);
                                        }
                                    }
                                }

                        }
                    }
                    else
                    {
                        report = new ResultReport();
                        ExperimentRecord[] records = essProxy.GetRecords(experimentID, null);
                        if (records != null)
                        {
                            List<String> execWarnings = new List<String>();
                            foreach (ExperimentRecord rec in records)
                            {
                                if (rec.type.CompareTo(BatchRecordType.EXECUTION_ERROR) == 0)
                                {
                                    report.errorMessage = rec.contents;
                                }

                                else if (rec.type.CompareTo(BatchRecordType.BLOB_EXTENSION) == 0)
                                {
                                    report.xmlBlobExtension = rec.contents;
                                }
                                else if (rec.type.CompareTo(BatchRecordType.RESULT_EXTENSION) == 0)
                                {
                                    report.xmlResultExtension = rec.contents;
                                }
                                else if (rec.type.CompareTo(BatchRecordType.EXECUTION_WARNING) == 0)
                                {
                                    execWarnings.Add(rec.contents);
                                }
                                else if (rec.type.CompareTo(BatchRecordType.RESULT) == 0)
                                {
                                    report.experimentResults = rec.contents;
                                }

                            }
                            if (execWarnings.Count > 0)
                            {
                                report.warningMessages = execWarnings.ToArray();
                            }
                        }
                        report.statusCode = expInfo.status & StorageStatus.BATCH_MASK;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new SoapException(ex.Message + ". " + ex.GetBaseException(), SoapException.ServerFaultCode, ex);
            }
            return report;
        }
        protected void displayRecords(long experimentId, string essGuid)
        {
            BrokerDB ticketIssuer = new BrokerDB();
            ProcessAgentInfo ess = ticketIssuer.GetProcessAgentInfo(essGuid);
            if (ess == null || ess.retired)
            {
                throw new Exception("The ESS is not registered or is retired");
            }
            Coupon opCoupon = ticketIssuer.GetEssOpCoupon(experimentId, TicketTypes.RETRIEVE_RECORDS, 60, ess.agentGuid);
            if (opCoupon != null)
            {

                ExperimentStorageProxy essProxy = new ExperimentStorageProxy();
                OperationAuthHeader header = new OperationAuthHeader();
                header.coupon = opCoupon;
                essProxy.Url = ess.webServiceUrl;
                essProxy.OperationAuthHeaderValue = header;

                ExperimentRecord[] records = essProxy.GetRecords(experimentId,null);
                if (records != null)
                {

                    StringBuilder buf = null;
                    if (cbxContents.Checked)
                    {
                        buf = new StringBuilder();
                        foreach (ExperimentRecord rec in records)
                        {
                            buf.AppendLine(rec.contents);
                        }
                        txtExperimentRecords.Text = buf.ToString();
                        txtExperimentRecords.Visible = true;
                        grvExperimentRecords.Visible = false;
                    }
                    else
                    {
                        dtRecords = new DataTable();
                        dtRecords.Columns.Add("Seq_Num", typeof(System.Int32));
                        dtRecords.Columns.Add("Record Type", typeof(System.String));
                        dtRecords.Columns.Add("Contents", typeof(System.String));
                        foreach (ExperimentRecord rec in records)
                        {
                            DataRow recTmp = dtRecords.NewRow();
                            recTmp["Seq_Num"] = rec.sequenceNum;
                            recTmp["Record Type"] = rec.type;
                            recTmp["Contents"] = rec.contents;
                            dtRecords.Rows.InsertAt(recTmp, dtRecords.Rows.Count);
                        }
                        grvExperimentRecords.DataSource = dtRecords;

                        grvExperimentRecords.DataBind();
                        grvExperimentRecords.Visible = true;
                        txtExperimentRecords.Visible = false;

                    }

                    divRecords.Visible = true;
                }
                Blob[] blobs = essProxy.GetBlobs(experimentId);
                if (blobs != null)
                {

                    dtBlobs = new DataTable();
                    dtBlobs.Columns.Add("Blob_ID", typeof(System.Int64));
                    dtBlobs.Columns.Add("Seq_Num", typeof(System.Int32));
                    dtBlobs.Columns.Add("MimeType", typeof(System.String));
                    dtBlobs.Columns.Add("Description", typeof(System.String));
                    foreach (Blob b in blobs)
                    {
                        DataRow blobTmp = dtBlobs.NewRow();
                        blobTmp["Blob_ID"] = b.blobId;
                        blobTmp["Seq_Num"] = b.recordNumber;
                        blobTmp["MimeType"] = b.mimeType;
                        blobTmp["Description"] = b.description;
                        dtBlobs.Rows.InsertAt(blobTmp, dtBlobs.Rows.Count);
                    }
                    grvBlobs.DataSource = dtBlobs;
                    grvBlobs.DataBind();
                    divBlobs.Visible = true;

                }
            }
        }
        public Experiment RetrieveExperiment(long experimentID)
        {
            Experiment experiment = null;
            BrokerDB brokerDB = new BrokerDB();
            int roles = 0;
            int userID = 0;
            int groupID = 0;
            //long[] expIDs = null;
            Ticket expTicket = brokerDB.RetrieveTicket(opHeader.coupon, TicketTypes.REDEEM_SESSION);
            if (expTicket != null && !expTicket.IsExpired())
            {
                //Parse payload, only get what is needed

                XmlQueryDoc expDoc = new XmlQueryDoc(expTicket.payload);
                //long expID = -1;

                string userStr = expDoc.Query("RedeemSessionPayload/userID");
                if ((userStr != null) && (userStr.Length > 0))
                    userID = Convert.ToInt32(userStr);
                string groupStr = expDoc.Query("RedeemSessionPayload/groupID");
                if ((groupStr != null) && (groupStr.Length > 0))
                    groupID = Convert.ToInt32(groupStr);

                if (userID > 0)
                {

                    AuthorizationWrapperClass wrapper = new AuthorizationWrapperClass();
                    roles = wrapper.GetExperimentAuthorizationWrapper(experimentID, userID, groupID);
                }
                if ((roles | ExperimentAccess.READ) == ExperimentAccess.READ)
                {
                    experiment = new Experiment();
                    experiment.experimentId = experimentID;
                    experiment.issuerGuid = ProcessAgentDB.ServiceGuid;
                    ProcessAgentInfo ess = brokerDB.GetExperimentESS(experimentID);
                    if (ess != null)
                    {
                        ExperimentStorageProxy essProxy = new ExperimentStorageProxy();
                        Coupon opCoupon = brokerDB.GetEssOpCoupon(experimentID, TicketTypes.RETRIEVE_RECORDS, 60, ess.agentGuid);
                        if (opCoupon == null)
                        {
                            string payload = TicketLoadFactory.Instance().RetrieveRecordsPayload(experimentID, ess.webServiceUrl);
                            opCoupon = brokerDB.CreateTicket(TicketTypes.RETRIEVE_RECORDS, ess.agentGuid, ProcessAgentDB.ServiceGuid,
                                60, payload);
                        }
                        essProxy.OperationAuthHeaderValue = new OperationAuthHeader();
                        essProxy.OperationAuthHeaderValue.coupon = opCoupon;
                        essProxy.Url = ess.webServiceUrl;
                        essProxy.GetRecords(experimentID, null);
                    }

                }
                else
                {
                    throw new AccessDeniedException("You do not have permission to read this experiment");
                }
            }
            return experiment;
        }