Example #1
0
        /// <summary>
        /// to select reservation accorrding to given criterion
        /// </summary>
        public static ReservationInfo[] GetReservations(string userName, int experimentInfoId, int credentialSetId, DateTime start, DateTime end)
        {
            List<ReservationInfo> reservations = new List<ReservationInfo>();
            int action = 0;
            if (experimentInfoId > 0)
                action |= 1;
            if (credentialSetId > 0)
                action |= 2;
            if (userName != null && userName.Length > 0)
                action |= 4;

            // create sql connection
            DbConnection connection = FactoryDB.GetConnection();
            DbCommand cmd = null;
            DbParameter expParam = null;
            DbParameter credParam = null;
            DbParameter userParam = null;
            // create sql command
            switch (action)
            {
                case 0:
                    cmd = FactoryDB.CreateCommand("RetrieveReservations", connection);
                    break;
                case 1:
                    cmd = FactoryDB.CreateCommand("RetrieveReservationsByExperiment", connection);
                    break;
                case 2:
                    cmd = FactoryDB.CreateCommand("RetrieveReservationsByGroup", connection);
                    break;
                case 3:
                    cmd = FactoryDB.CreateCommand("RetrieveReservationsByGroupAndExperiment", connection);
                    break;
                case 4:
                    cmd = FactoryDB.CreateCommand("RetrieveReservationsByUser", connection);
                    break;
                case 5:
                    throw new Exception("Wrong combination of arguments in selectReservations 5");
                    // NOT Implemented cmd = FactoryDB.CreateCommand("RetrieveReservationsByExperiment", connection);
                    break;
                case 6:
                    cmd = FactoryDB.CreateCommand("RetrieveReservationsByUserAndGroup", connection);
                    break;
                case 7:
                    cmd = FactoryDB.CreateCommand("RetrieveReservationsByAll", connection);
                    break;
                default:
                    throw new Exception("Wrong combination of arguments in selectReservations");
                    break;
            }
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(FactoryDB.CreateParameter(cmd,"@start", start, DbType.DateTime));
            cmd.Parameters.Add(FactoryDB.CreateParameter(cmd,"@end", end, DbType.DateTime));
            switch (action)
            {
                case 0:
                    break;
                case 1:
                    cmd.Parameters.Add(FactoryDB.CreateParameter(cmd,"@experInfoID", experimentInfoId, DbType.Int32));
                    break;
                case 2:
                    cmd.Parameters.Add(FactoryDB.CreateParameter(cmd,"@credSetID", credentialSetId, DbType.Int32));
                    break;
                case 3:
                    cmd.Parameters.Add(FactoryDB.CreateParameter(cmd,"@experInfoID", experimentInfoId, DbType.Int32));
                    cmd.Parameters.Add(FactoryDB.CreateParameter(cmd,"@credSetID", credentialSetId, DbType.Int32));
                    break;
                case 4:
                    cmd.Parameters.Add(FactoryDB.CreateParameter(cmd, "@userName", userName, DbType.String, 256));
                    break;
                case 5:
                    break;
                case 6:
                    cmd.Parameters.Add(FactoryDB.CreateParameter(cmd,"@credSetID", credentialSetId,DbType.Int32));
                    cmd.Parameters.Add(FactoryDB.CreateParameter(cmd, "@userName", userName, DbType.String, 256));
                    break;
                case 7:
                    cmd.Parameters.Add(FactoryDB.CreateParameter(cmd,"@experInfoID", experimentInfoId, DbType.Int32));
                    cmd.Parameters.Add(FactoryDB.CreateParameter(cmd,"@credSetID", credentialSetId, DbType.Int32));
                    cmd.Parameters.Add(FactoryDB.CreateParameter(cmd,"@userName", userName, DbType.String, 256));
                    break;
                default:
                    break;
            }
            DbDataReader dataReader = null;
            try
            {
                connection.Open();
                dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    ReservationInfo reservation = new ReservationInfo();
                    reservation.reservationId = dataReader.GetInt32(0); ;
                    if (dataReader[1] != System.DBNull.Value)
                        reservation.userName = dataReader.GetString(1);
                    if (dataReader[2] != System.DBNull.Value)
                        reservation.startTime = DateUtil.SpecifyUTC(dataReader.GetDateTime(2));
                    if (dataReader[3] != System.DBNull.Value)
                        reservation.endTime = DateUtil.SpecifyUTC(dataReader.GetDateTime(3));
                    if (dataReader[4] != System.DBNull.Value)
                        reservation.credentialSetId = (int)dataReader.GetInt32(4);
                    if (dataReader[5] != System.DBNull.Value)
                        reservation.experimentInfoId = (int)dataReader.GetInt32(5);
                    reservations.Add(reservation);
                }
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                connection.Close();
            }
            return reservations.ToArray();
        }
Example #2
0
        /// <summary>
        /// returns an array of the immutable reservation objects that correspond to the supplied reservation IDs
        /// </summary>
        /// <param name="reservationIDs"></param>
        /// <returns></returns>
        public static ReservationInfo[] GetReservations(int[] reservationIDs)
        {
            ReservationInfo[] reservations=new ReservationInfo[reservationIDs.Length];
            for(int i=0; i<reservationIDs.Length;i++)
            {
                reservations[i]=new  ReservationInfo();
            }
            // create sql connection
            DbConnection connection = FactoryDB.GetConnection();

            // create sql command
            // command executes the "RetrieveReservationByID" stored procedure
            DbCommand cmd = FactoryDB.CreateCommand("RetrieveReservationByID", connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(FactoryDB.CreateParameter(cmd,"@reservationID", null, DbType.Int32));
            //execute the command
            try
            {
                connection.Open();
                for(int i=0;i<reservationIDs.Length;i++)
                {
                    // populate the parameters
                    cmd.Parameters["@reservationID"].Value = reservationIDs[i];
                    DbDataReader dataReader = null;
                    dataReader=cmd.ExecuteReader();
                    while(dataReader.Read())
                    {
                        reservations[i].reservationId=reservationIDs[i];
                        if(dataReader[0] != System.DBNull.Value )
                            reservations[i].userName=dataReader.GetString(0);
                        if(dataReader[1] != System.DBNull.Value )
                            reservations[i].startTime= DateUtil.SpecifyUTC(dataReader.GetDateTime(1));
                        if(dataReader[2] != System.DBNull.Value )
                            reservations[i].endTime = DateUtil.SpecifyUTC(dataReader.GetDateTime(2));
                        if(dataReader[3] != System.DBNull.Value )
                            reservations[i].credentialSetId=(int)dataReader.GetInt32(3);
                        if(dataReader[4] != System.DBNull.Value )
                            reservations[i].experimentInfoId=(int)dataReader.GetInt32(4);
                    }
                    dataReader.Close();
                }
            }

            catch (Exception ex)
            {
                throw new Exception("Exception thrown in get reservations",ex);
            }
            finally
            {
                connection.Close();
            }
            return reservations;
        }
        /// <summary>
        /// to select reservation according to given criterion
        /// </summary>
        public ReservationInfo[] GetReservationInfos(string sbGuid, string userName, string groupName,
            string lsGuid, string clientGuid, DateTime start, DateTime end)
        {
            List<ReservationInfo> reservations = new List<ReservationInfo>();
            // create sql connection
            DbConnection connection = FactoryDB.GetConnection();
            DbCommand cmd = null;
            cmd = FactoryDB.CreateCommand("RetrieveReservationInfos", connection);
            cmd.CommandType = CommandType.StoredProcedure;
            if(sbGuid != null && sbGuid .Length > 0)
                cmd.Parameters.Add(FactoryDB.CreateParameter( "@sbGuid", sbGuid, DbType.String, 50));
            if (userName != null && userName.Length > 0)
                cmd.Parameters.Add(FactoryDB.CreateParameter( "@userName", userName, DbType.String, 256));
            if (groupName != null && groupName.Length > 0)
                cmd.Parameters.Add(FactoryDB.CreateParameter( "@groupName", groupName, DbType.String, 256));
            if (lsGuid != null && lsGuid.Length > 0)
                cmd.Parameters.Add(FactoryDB.CreateParameter( "@labServerGuid", lsGuid, DbType.String, 50));
            if (clientGuid != null && clientGuid.Length > 0)
                cmd.Parameters.Add(FactoryDB.CreateParameter( "@clientGuid", clientGuid, DbType.String, 50));
            if(start != null)
                cmd.Parameters.Add(FactoryDB.CreateParameter( "@start", start, DbType.DateTime));
            if(end != null)
                cmd.Parameters.Add(FactoryDB.CreateParameter( "@end", end, DbType.DateTime));

            DbDataReader dataReader = null;
            try
            {
                connection.Open();
                dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    //select r.Reservation_ID, r.Start_Time, r.End_Time,r.Credential_Set_ID, r.Experiment_Info_ID, r.User_Name
                    ReservationInfo reservation = new ReservationInfo();
                    reservation.reservationId = dataReader.GetInt32(0);
                    if (dataReader[1] != System.DBNull.Value)
                        reservation.startTime = DateUtil.SpecifyUTC(dataReader.GetDateTime(1));
                    if (dataReader[2] != System.DBNull.Value)
                        reservation.endTime = DateUtil.SpecifyUTC(dataReader.GetDateTime(2));
                    if (dataReader[3] != System.DBNull.Value)
                        reservation.credentialSetId = (int)dataReader.GetInt32(3);
                    if (dataReader[4] != System.DBNull.Value)
                        reservation.experimentInfoId = (int)dataReader.GetInt32(4);
                    if (dataReader[5] != System.DBNull.Value)
                        reservation.userName = dataReader.GetString(5);

                    reservations.Add(reservation);
                }
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                connection.Close();
            }
            return reservations.ToArray();
        }
        /// <summary>
        /// Return the time span the user should wait till the start time of the reservation
        /// </summary>
        /// <param name="reservationID"></param>the reservation ID to be checked
        /// <returns></returns>
        public TimeSpan GetReservationWaitTime(int reservationID)
        {
            ReservationInfo redeemedRes = new ReservationInfo();
            TimeSpan ts = new TimeSpan();
            try
            {
                redeemedRes = GetReservationInfos(new int[] { reservationID })[0];
                DateTime startTime = redeemedRes.startTime.ToLocalTime();
                ts = startTime.Subtract(DateTime.Now);
            }

            catch (Exception ex)
            {
                throw new Exception("Exception thrown in RedeemReservation", ex);
            }

            return ts;
        }
        private void redeemReservation(ReservationInfo res)
        {
            long duration = (res.endTime.Ticks - res.startTime.Ticks) / TimeSpan.TicksPerSecond;
            TicketLoadFactory factory = TicketLoadFactory.Instance();
            ProcessAgentDB ticketing = new ProcessAgentDB();

            string payload = factory.createAllowExperimentExecutionPayload(
                res.startTime, duration, Session["groupName"].ToString(),clientGuid);
            DateTime tmpTime = res.startTime.AddTicks(duration * TimeSpan.TicksPerSecond);
            DateTime utcNow = DateTime.UtcNow;
            long ticketDuration = (tmpTime.Ticks - utcNow.Ticks) / TimeSpan.TicksPerSecond;

            TicketIssuerProxy ticketIssuer = new TicketIssuerProxy();

            //get the SB web service URL, and set the proxy's URL accordingly
            ProcessAgentInfo sbInfo = ticketing.GetServiceBrokerInfo();
            ticketIssuer.Url = sbInfo.webServiceUrl;

            //Get the agent coupon Coupon, to be embedded in the SOAP header of the web service call to the SB
            Coupon agentOutCoupon = sbInfo.identOut;

            iLabs.DataTypes.SoapHeaderTypes.AgentAuthHeader agentAuthHeader = new iLabs.DataTypes.SoapHeaderTypes.AgentAuthHeader();

            //set the SOAP header (of the proxy class) to the agentCoupon
            agentAuthHeader.coupon = agentOutCoupon;
            agentAuthHeader.agentGuid = ProcessAgentDB.ServiceGuid;
            ticketIssuer.AgentAuthHeaderValue = agentAuthHeader;

            //call the CreateTicket web service method on the SB (ticket issuer)
            Coupon allowExecutionCoupon = ticketIssuer.CreateTicket(TicketTypes.ALLOW_EXPERIMENT_EXECUTION,
                sbInfo.agentGuid, ticketDuration, payload);

            if (allowExecutionCoupon != null)
            {
                string couponId = allowExecutionCoupon.couponId.ToString();
                string passkey = allowExecutionCoupon.passkey;
                string issuerGuid = allowExecutionCoupon.issuerGuid;

                string backToSbUrl = Session["sbUrl"].ToString() +
                    "?coupon_id=" + couponId +
                    "&passkey=" + passkey +
                    "&issuer_guid=" + issuerGuid;

                Response.Redirect(backToSbUrl, false);
            }
            else
            {
                string msg = "Exception: ExperimentExecution is not allowed.";
                lblErrorMessage.Text = Utilities.FormatErrorMessage(msg);
                lblErrorMessage.Visible = true;
            }
        }