public int RevokeReservation(string serviceBrokerGuid, string groupName,
            string labServerGuid, string labClientGuid, DateTime startTime, DateTime endTime, string message)
        {
            bool status = false;
            bool fromISB = false;
            int count = 0;
            Coupon opCoupon = new Coupon();
            opCoupon.couponId = opHeader.coupon.couponId;
            opCoupon.passkey = opHeader.coupon.passkey;
            opCoupon.issuerGuid = opHeader.coupon.issuerGuid;
            UserSchedulingDB dbManager = new UserSchedulingDB();
            try
            {
                Ticket retrievedTicket = dbManager.RetrieveAndVerify(opCoupon, TicketTypes.REVOKE_RESERVATION);
                if (retrievedTicket.payload != null && retrievedTicket.payload.Length > 0)
                {
                    XmlQueryDoc revokeDoc = new XmlQueryDoc(retrievedTicket.payload);
                    string sourceStr = revokeDoc.Query("RevokeReservationPayload/source");
                    if (sourceStr != null && sourceStr.CompareTo("ISB") == 0)
                    {
                        fromISB = true;
                    }
                }
                DateTime targetStart = new DateTime(startTime.Year, startTime.Month, startTime.Day,
                    startTime.Hour, startTime.Minute, 0, startTime.Kind);
                if (targetStart.Kind != DateTimeKind.Utc)
                    targetStart = targetStart.ToUniversalTime();
                DateTime targetEnd = new DateTime(endTime.Year, endTime.Month, endTime.Day,
                    endTime.Hour, endTime.Minute, 0, endTime.Kind);
                if (targetEnd.Kind != DateTimeKind.Utc)
                    targetEnd = targetEnd.ToUniversalTime();
                if (fromISB)
                { // Need to forward to LSS
                    string lssUrl = dbManager.ListLssUrlByExperiment(labClientGuid, labServerGuid);
                    if (lssUrl != null && lssUrl.Length > 0)
                    {
                        LabSchedulingProxy lssProxy = new LabSchedulingProxy();
                        lssProxy.OperationAuthHeaderValue = new OperationAuthHeader();
                        lssProxy.OperationAuthHeaderValue.coupon = opCoupon;
                        lssProxy.Url = lssUrl;
                        int rCount = lssProxy.RemoveReservation(serviceBrokerGuid, groupName, ProcessAgentDB.ServiceGuid,
                            labServerGuid, labClientGuid, targetStart, targetEnd);
                    }
                }

                ReservationData[] ris = dbManager.GetReservations(serviceBrokerGuid, null, groupName,
                    labServerGuid, labClientGuid, targetStart, targetEnd);

                if (ris != null && ris.Length > 0)
                {

                    InteractiveSBProxy sbProxy = new InteractiveSBProxy();
                    ProcessAgentInfo sbInfo = dbManager.GetProcessAgentInfo(ProcessAgentDB.ServiceAgent.domainGuid);
                    AgentAuthHeader header = new AgentAuthHeader();
                    header.coupon = sbInfo.identOut;
                    header.agentGuid = ProcessAgentDB.ServiceGuid;
                    sbProxy.AgentAuthHeaderValue = header;
                    sbProxy.Url = sbInfo.webServiceUrl;
                    foreach (ReservationData rd in ris)
                    {

                        status = dbManager.RevokeReservation(rd.sbGuid, rd.groupName, rd.lsGuid, rd.clientGuid,
                              rd.startTime, rd.endTime, message);
                        if (status)
                        {
                            count++;
                            status = sbProxy.RevokeReservation(rd.sbGuid, rd.userName, rd.groupName, rd.lsGuid, rd.clientGuid,
                              rd.startTime, rd.endTime, message);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception("USS: RevokeReservation -> ",e);
            }
            return count;
        }
        public TimePeriod[] RetrieveAvailableTimePeriods(string serviceBrokerGuid, string groupName,
            string labServerGuid, string clientGuid, DateTime startTime, DateTime endTime)
        {
            Coupon opCoupon = new Coupon();
            opCoupon.couponId = opHeader.coupon.couponId;
            opCoupon.passkey = opHeader.coupon.passkey;
            opCoupon.issuerGuid = opHeader.coupon.issuerGuid;
            try
            {
                UserSchedulingDB dbManager = new UserSchedulingDB();
                Ticket ssTicket = dbManager.RetrieveAndVerify(opCoupon, TicketTypes.SCHEDULE_SESSION);

                string lssGuid = dbManager.ListLssIdByExperiment(clientGuid, labServerGuid);
                LSSInfo  lssInfo = dbManager.GetLSSInfo(lssGuid);
                LabSchedulingProxy lssProxy = new LabSchedulingProxy();
                lssProxy.OperationAuthHeaderValue = new OperationAuthHeader();
                lssProxy.OperationAuthHeaderValue.coupon = opCoupon;
                lssProxy.Url = lssInfo.lssUrl;
                TimePeriod[] array = lssProxy.RetrieveAvailableTimePeriods( serviceBrokerGuid, groupName, ProcessAgentDB.ServiceGuid,
                    labServerGuid, clientGuid, startTime, endTime);
                return array;
            }
            catch
            {
                throw;
            }
        }
        public Reservation[] ListReservations(string serviceBrokerGuid, string userName, 
            string labServerGuid, string labClientGuid, DateTime startTime, DateTime endTime)
        {
            UserSchedulingDB dbManager = new UserSchedulingDB();
            Ticket retrievedTicket = dbManager.RetrieveAndVerify(opHeader.coupon, TicketTypes.REDEEM_RESERVATION);
            if (retrievedTicket.IsExpired())
            {
                throw new AccessDeniedException("The reservation ticket has expired, Please re-login.");
            }
            //used to check ticketPayload
            string user = null;
            string group = null;
            string labGuid = null;
            string clientGuid = null;
            string sbGuid = null;

            //Resolved target arguments
            string userTarget = null;
            string labGuidTarget = null;
            string clientGuidTarget = null;
            string lssUrl = null;
            string lssGuid = null;
            XmlDocument payload = new XmlDocument();
            payload.LoadXml(retrievedTicket.payload);

            sbGuid = payload.GetElementsByTagName("sbGuid")[0].InnerText;
            user = payload.GetElementsByTagName("userName")[0].InnerText;
            group = payload.GetElementsByTagName("groupName")[0].InnerText;
            clientGuid = payload.GetElementsByTagName("clientGuid")[0].InnerText;
            labGuid = payload.GetElementsByTagName("labServerGuid")[0].InnerText;

            lssUrl = dbManager.ListLssUrlByExperiment(clientGuid, labServerGuid);
            lssGuid = dbManager.ListLssIdByExperiment(clientGuid, labServerGuid);

            userTarget = Utilities.ResolveArguments(userName, user, true);
            clientGuidTarget = Utilities.ResolveArguments(labClientGuid, clientGuid, false);
            labGuidTarget = Utilities.ResolveArguments(labServerGuid, labGuid, false);

            DateTime targetStart = new DateTime(startTime.Year, startTime.Month, startTime.Day,
               startTime.Hour, startTime.Minute, 0, startTime.Kind);
            if (targetStart.Kind != DateTimeKind.Utc)
                targetStart = targetStart.ToUniversalTime();
            DateTime targetEnd = new DateTime(endTime.Year, endTime.Month, endTime.Day,
                    endTime.Hour, endTime.Minute, 0, endTime.Kind);
            if (targetEnd.Kind != DateTimeKind.Utc)
                targetEnd = targetEnd.ToUniversalTime();
            ReservationInfo[] resInfos = dbManager.GetReservationInfos(serviceBrokerGuid, userTarget, group,
                labGuidTarget, clientGuidTarget, targetStart, targetEnd);
            if (resInfos != null && resInfos.Length > 0)
            {
                Reservation[] reservations = new Reservation[resInfos.Length];

                for (int i = 0; i < resInfos.Length; i++)
                {
                    reservations[i] = new Reservation(resInfos[i].startTime, resInfos[i].endTime);
                    reservations[i].userName = resInfos[i].userName;
                }
                return reservations;
            }
            else
                return null;
        }
 public Reservation RedeemReservation(string serviceBrokerGuid, String userName,  
     String labServerGuid, string clientGuid)
 {
     UserSchedulingDB dbManager = new UserSchedulingDB();
     Coupon opCoupon = new Coupon();
     opCoupon.couponId = opHeader.coupon.couponId;
     opCoupon.passkey = opHeader.coupon.passkey;
     opCoupon.issuerGuid = opHeader.coupon.issuerGuid;
     string type = TicketTypes.REDEEM_RESERVATION;
     try
     {
         Ticket retrievedTicket = dbManager.RetrieveAndVerify(opCoupon, type);
         ReservationInfo res = dbManager.RedeemReservation(userName, serviceBrokerGuid, clientGuid, labServerGuid);
         if (res != null)
         {
             Reservation reservation = new Reservation(res.startTime, res.endTime);
             reservation.userName = res.userName;
             return reservation;
         }
         else
         {
             return null;
         }
     }
     catch
     {
         throw;
     }
 }
        public string AddReservation(string serviceBrokerGuid, string userName, string groupName,
            string labServerGuid, string labClientGuid, DateTime startTime, DateTime endTime)
        {
            string message = null;
             Coupon opCoupon = new Coupon();
             UserSchedulingDB dbManager = new UserSchedulingDB();
            opCoupon.couponId = opHeader.coupon.couponId;
            opCoupon.passkey = opHeader.coupon.passkey;
            opCoupon.issuerGuid = opHeader.coupon.issuerGuid;
            string type = TicketTypes.SCHEDULE_SESSION;
            try
            {
                Ticket retrievedTicket = dbManager.RetrieveAndVerify(opCoupon, type);
                if (retrievedTicket.IsExpired())
                {
                    throw new AccessDeniedException("The reservation ticket has expired, Please re-login.");
                }
                //used to check ticketPayload
                string user = null;
                string group = null;
                string labGuid = null;
                string clientGuid = null;
                string sbGuid = null;

                //Resolved target arguments
                string userTarget = null;
                string groupTarget = null;
                string labGuidTarget = null;
                string clientGuidTarget = null;

                XmlDocument payload = new XmlDocument();
                payload.LoadXml(retrievedTicket.payload);

                sbGuid = payload.GetElementsByTagName("sbGuid")[0].InnerText;
                user = payload.GetElementsByTagName("userName")[0].InnerText;
                group = payload.GetElementsByTagName("groupName")[0].InnerText;
                clientGuid = payload.GetElementsByTagName("clientGuid")[0].InnerText;
                labGuid = payload.GetElementsByTagName("labServerGuid")[0].InnerText;

                userTarget = Utilities.ResolveArguments(userName, user, true);
                groupTarget = Utilities.ResolveArguments(groupName, group, true);
                clientGuidTarget = Utilities.ResolveArguments(labClientGuid, clientGuid, false);
                labGuidTarget = Utilities.ResolveArguments(labServerGuid, labGuid, false);

                string lssGuid = dbManager.ListLssIdByExperiment(labClientGuid, labServerGuid);
                LSSInfo lssInfo = dbManager.GetLSSInfo(lssGuid);
                DateTime targetStart = new DateTime(startTime.Year, startTime.Month, startTime.Day,
               startTime.Hour, startTime.Minute, 0, startTime.Kind);
                if (targetStart.Kind != DateTimeKind.Utc)
                    targetStart = targetStart.ToUniversalTime();
                DateTime targetEnd = new DateTime(endTime.Year, endTime.Month, endTime.Day,
                    endTime.Hour, endTime.Minute, 0, endTime.Kind);
                if (targetEnd.Kind != DateTimeKind.Utc)
                    targetEnd = targetEnd.ToUniversalTime();

                LabSchedulingProxy lssProxy = new LabSchedulingProxy();
                lssProxy.OperationAuthHeaderValue = new OperationAuthHeader();
                lssProxy.OperationAuthHeaderValue.coupon = opCoupon;
                lssProxy.Url = lssInfo.lssUrl;
                message = lssProxy.ConfirmReservation( serviceBrokerGuid, groupName, ProcessAgentDB.ServiceGuid,
                    labServerGuid, labClientGuid, targetStart, targetEnd);
                if(message.ToLower().Contains("success")){
                    int infoID = dbManager.ListExperimentInfoIDByExperiment(labServerGuid, labClientGuid);
                    dbManager.AddReservation(userName, serviceBrokerGuid,groupName,infoID,targetStart,targetEnd);
                }
                return message;
            }

            catch (Exception e)
            {
                throw new Exception("USS: AddReservation -> ", e);
            }
            return message;
        }