/// <summary>
        /// 
        /// </summary>
        /// <param name="labServer"></param>
        /// <param name="client"></param>
        /// <param name="startExecution"></param>
        /// <param name="duration"></param>
        /// <param name="userTZ"></param>
        /// <param name="userID"></param>
        /// <param name="groupID"></param>
        /// <param name="groupName"></param>
        /// <returns>The redirect url where the user should be redirected, with the coupon appended to it</returns>
        public string ExecuteExperimentExecutionRecipe(Coupon coupon, ProcessAgentInfo labServer, LabClient client,
            DateTime startExecution, long duration, int userTZ, int userID, int groupID, string groupName)
        {
            int essId = 0;
            ProcessAgentInfo essAgent = null;

            long ticketDuration = 7200; //Default to 2 hours
            //   Add a 10 minutes to ESS ticket duration ( in seconds ) to extend beyond experiment expiration
            if (duration != -1)
            {
                //ticketDuration = duration + 60; // For testing only add a minute
                ticketDuration = duration + 600; // Add 10 minutes beyond the experiment end
            }
            else
            {
                ticketDuration = -1;
            }

            // Authorization wrapper
            AuthorizationWrapperClass wrapper = new AuthorizationWrapperClass();

            // create ticket issuer and payload factory
            BrokerDB brokerDB = new BrokerDB();
            TicketLoadFactory factory = TicketLoadFactory.Instance();

            if (client.needsESS)
            {
                essId = brokerDB.FindProcessAgentIdForClient(client.clientID, ProcessAgentType.EXPERIMENT_STORAGE_SERVER);

            }

               // 1. Coupon creation moved to launch client

            //
            // 2. create ServiceBroker experiment record and get corresponding experiment id
            // This checks authorization.
            long experimentID = wrapper.CreateExperimentWrapper(StorageStatus.INITIALIZED,
                userID, groupID, labServer.agentId, client.clientID,
                essId, startExecution, duration);

            // Store a record of the Experiment Collection Coupon
            DataStorageAPI.InsertExperimentCoupon(experimentID, coupon.couponId);
             string essWebAddress = null;

                 // If a ESS is specified Create the ESS Tickets, this should only happen if a resource is mapped
                 if (essId > 0)
                 {
                     //3.A create ESS administer experiment ticket, Add 10 minutes to duration
                     // This must be created before the ESS experiment records may be created
                     essAgent = brokerDB.GetProcessAgentInfo(essId);
                     if ((essAgent != null) && !essAgent.retired)
                     {
                         brokerDB.AddTicket(coupon,
                                TicketTypes.ADMINISTER_EXPERIMENT, essAgent.AgentGuid, brokerDB.GetIssuerGuid(), ticketDuration, factory.createAdministerExperimentPayload(experimentID, essAgent.webServiceUrl));

                         //3.B create store record ticket
                         brokerDB.AddTicket(coupon,
                                TicketTypes.STORE_RECORDS, essAgent.agentGuid, labServer.agentGuid, ticketDuration, factory.StoreRecordsPayload(true, experimentID, essAgent.webServiceUrl));

                         //3.C create retrieve experiment ticket, retrieve Experiment Records never expires, unless experiment deleted
                         //    This should be changed to a long but finite period once ReadExisting Expermint is in place.
                         brokerDB.AddTicket(coupon,
                                TicketTypes.RETRIEVE_RECORDS, essAgent.agentGuid, brokerDB.GetIssuerGuid(), -1, factory.RetrieveRecordsPayload(experimentID, essAgent.webServiceUrl));

                         // 3.D Create the ESS Experiment Records
                         ExperimentStorageProxy ess = new ExperimentStorageProxy();
                         ess.AgentAuthHeaderValue = new AgentAuthHeader();
                         ess.AgentAuthHeaderValue.coupon = essAgent.identOut;
                         ess.AgentAuthHeaderValue.agentGuid = ProcessAgentDB.ServiceGuid;
                         ess.Url = essAgent.webServiceUrl;
                         essWebAddress = essAgent.webServiceUrl;

                         // Call the ESS to create the ESS Records and open the experiment
                         StorageStatus status = ess.CreateExperiment(experimentID, ticketDuration, userID,groupID,client.clientID);
                         if (status != null)
                             DataStorageAPI.UpdateExperimentStatus(status);
                     }
                 }

            //
            // 4. create the execution ticket for the experiment
            //

            // 4.A create payload
            string payload = factory.createExecuteExperimentPayload(essWebAddress, startExecution, duration,
                userTZ, userID, groupID, groupName, brokerDB.GetIssuerGuid(), experimentID);

            // 4.B create experiment execution ticket.
            brokerDB.AddTicket(coupon,
                      TicketTypes.EXECUTE_EXPERIMENT, labServer.agentGuid, labServer.agentGuid, ticketDuration, payload);

            // 5. construct the redirect query. Note this is now done in the myClient page

            string url = client.loaderScript.Trim();
            return url;
        }
        public ClientSubmissionReport Submit(string labServerID, string experimentSpecification, int priorityHint, bool emailNotification)
        {
            // Default to 24 hours duration
            long duration = TimeSpan.TicksPerDay/TimeSpan.TicksPerSecond;
            int seqNo = 0;
            ClientSubmissionReport clientSReport = null;
            try{

                // Checking if user has permission to use the lab server. The method will set headers for lab server calls
                //if authorization is successful.
                CheckAndSetLSAuthorization(labServerID);

                //Retrieve variables from session
                int userID = Convert.ToInt32(Session["UserID"]);
                int effectiveGroupID = Convert.ToInt32(Session["GroupID"]);
                int clientID = 0;
                if (Session["ClientID"] != null )
                    clientID = Convert.ToInt32(Session["ClientID"]);
                string effectiveGroup = Session["GroupName"].ToString();
                ProcessAgentInfo infoLS = brokerDB.GetProcessAgentInfo(labServerID);

                //if (infoLS.retired)
                //{
                //    throw new Exception("The Batch Lab Server is retired");
                //}
                //// get qualifier ID of labServer
                //int qualifierID = AuthorizationAPI.GetQualifierID(infoLS.agentId, Qualifier.labServerQualifierTypeID);

                ///* End collecting information */

                //// Checking if user has permission to use the lab server
                //if (!AuthorizationAPI.CheckAuthorization(effectiveGroupID, Function.useLabServerFunctionType, qualifierID))
                //{
                //    // check fails

                //    throw new AccessDeniedException("Access denied using labServer '" + infoLS.agentName + "'.");
                //}
                //else
                //{
                    int[] groupIDs = new int[1];
                    groupIDs[0] = effectiveGroupID;

                    SubmissionReport sReport = new SubmissionReport();

                    clientSReport = new ClientSubmissionReport();
                    clientSReport.vReport = new ValidationReport();
                    clientSReport.wait = new WaitEstimate();

                    // 1. Create Coupon for ExperimentCollection
                    Coupon coupon = brokerDB.CreateCoupon();

                    int essID = brokerDB.FindProcessAgentIdForClient(clientID, ProcessAgentType.EXPERIMENT_STORAGE_SERVER);
                    //
                    // 2. create ServiceBroker experiment record and get corresponding experiment id
                    // This checks authorization.
                    long experimentID = wrapper.CreateExperimentWrapper(StorageStatus.INITIALIZED, userID, effectiveGroupID, infoLS.agentId, clientID,
                        essID, DateTime.UtcNow, duration);

                    // Store a record of the Experiment Collection Coupon
                    DataStorageAPI.InsertExperimentCoupon(experimentID, coupon.couponId);

                    //3.A create ESS administer experiment ticket, Add 10 minutes to duration
                    // This must be created before the ESS experiment records may be created
                    ProcessAgentInfo essAgent = brokerDB.GetProcessAgentInfo(essID);
                    if (essAgent.retired)
                    {
                        throw new Exception("The Batch Lab Server is retired");
                    }
                    TicketLoadFactory factory = TicketLoadFactory.Instance();

                    brokerDB.AddTicket(coupon,
                           TicketTypes.ADMINISTER_EXPERIMENT, essAgent.AgentGuid, ProcessAgentDB.ServiceGuid, duration, factory.createAdministerExperimentPayload(experimentID, essAgent.webServiceUrl));

                    //3.B create store record ticket, in the MergedSB the records are all saved via the serviceBroker
                    brokerDB.AddTicket(coupon,
                           TicketTypes.STORE_RECORDS, essAgent.agentGuid, ProcessAgentDB.ServiceGuid, duration, factory.StoreRecordsPayload(true, experimentID, essAgent.webServiceUrl));

                    //3.C create retrieve experiment ticket, retrieve Experiment Records never expires, unless experiment deleted
                    //    This should be changed to a long but finite period once eadExisting Expermint is in place.
                    brokerDB.AddTicket(coupon,
                           TicketTypes.RETRIEVE_RECORDS, essAgent.agentGuid, ProcessAgentDB.ServiceGuid, -1, factory.RetrieveRecordsPayload(experimentID, essAgent.webServiceUrl));

                    // 3.D Create the ESS Experiment Records
                    ExperimentStorageProxy ess = new ExperimentStorageProxy();
                    ess.AgentAuthHeaderValue = new AgentAuthHeader();
                    ess.AgentAuthHeaderValue.coupon = essAgent.identOut;
                    ess.AgentAuthHeaderValue.agentGuid = ProcessAgentDB.ServiceGuid;
                    ess.OperationAuthHeaderValue = new OperationAuthHeader();
                    ess.OperationAuthHeaderValue.coupon = coupon;
                    ess.Url = essAgent.webServiceUrl;

                    // Call the ESS to create the ESS Records and open the experiment
                    StorageStatus status = ess.CreateExperiment(experimentID, duration,userID,effectiveGroupID,clientID);
                    if (status != null)
                        DataStorageAPI.UpdateExperimentStatus(status);

                    seqNo = ess.AddRecord(experimentID, ProcessAgentDB.ServiceGuid,
                              BatchRecordType.SPECIFICATION, true, experimentSpecification, null);

                    // save lab configuration
                    string labConfiguration = batchLS_Proxy.GetLabConfiguration(effectiveGroup);
                    seqNo = ess.AddRecord(experimentID, labServerID,
                        BatchRecordType.LAB_CONFIGURATION, true, labConfiguration, null);

                    // call labServer submit
                    sReport = batchLS_Proxy.Submit(Convert.ToInt32(experimentID), experimentSpecification, effectiveGroup, priorityHint);

                    // save submission report
                    //wrapper.SaveSubmissionReportWrapper(experimentID, sReport);
                    if (sReport.vReport != null)
                        if ((sReport.vReport.errorMessage != null) && (sReport.vReport.errorMessage.CompareTo("") != 0))
                        {
                            seqNo = ess.AddRecord(experimentID, labServerID, BatchRecordType.VALIDATION_ERROR, false, sReport.vReport.errorMessage, null);

                        }

                    if (sReport.vReport.warningMessages != null)
                        foreach (string s in sReport.vReport.warningMessages)
                        {
                            if ((s != null) && (s.CompareTo("") != 0))
                                seqNo = ess.AddRecord(experimentID, labServerID, BatchRecordType.VALIDATION_WARNING, false, s, null);
                        }

                    // return clientSubmissionReport
                    if (sReport.vReport != null)
                    {
                        clientSReport.vReport.accepted = sReport.vReport.accepted;
                        clientSReport.vReport.errorMessage = sReport.vReport.errorMessage;
                        // if error exists then change status to "an experiment with a problem"
                        if ((sReport.vReport.errorMessage != null) && (!sReport.vReport.errorMessage.Equals("")))
                        {
                            StorageStatus sStatus = new StorageStatus();
                            sStatus.experimentId = experimentID;
                            //sStatus.estRuntime=sReport.vReport.estRuntime;
                            sStatus.status = StorageStatus.BATCH_TERMINATED_ERROR;
                            DataStorageAPI.UpdateExperimentStatus(sStatus);
                        }
                        clientSReport.vReport.estRuntime = sReport.vReport.estRuntime;
                        clientSReport.vReport.warningMessages = sReport.vReport.warningMessages;
                    }
                    clientSReport.experimentID = Convert.ToInt32(experimentID);
                    clientSReport.minTimeToLive = sReport.minTimetoLive;
                    if (sReport.wait != null)
                    {
                        clientSReport.wait.effectiveQueueLength = sReport.wait.effectiveQueueLength;
                        clientSReport.wait.estWait = sReport.wait.estWait;
                    }
                //}
                return clientSReport;
            }

            catch
            {
                throw;
            }
        }