/// <summary> /// Returns a valid opened and connected ISession instance /// for the given connection alias. /// </summary> /// <param name="alias"></param> /// <returns></returns> public ISession OpenSession(String alias) { if (alias == null) { throw new ArgumentNullException("alias"); } ITransaction transaction = ObtainCurrentTransaction(); SessionDelegate wrapped = sessionStore.FindCompatibleSession(alias); ISession session; if (wrapped == null) { session = CreateSession(alias); wrapped = WrapSession(transaction != null, session); EnlistIfNecessary(true, transaction, wrapped); sessionStore.Store(alias, wrapped); } else { EnlistIfNecessary(false, transaction, wrapped); wrapped = WrapSession(true, wrapped.InnerSession); } return(wrapped); }
public ISession BeginTransaction() { if (sessionStore.CurrentSession != null) { throw new DataMapperException("SqlMap could not invoke BeginTransaction(). A Transaction is already started. Call CommitTransaction() or RollbackTransaction first."); } ISession session = this.CreateSession(); sessionStore.Store(session); session.BeginTransaction(); return(session); }
public void FindCompatibleSessionWithTwoThreads() { ISessionStore store = container.Resolve <ISessionStore>(); ISessionFactory factory = container.Resolve <ISessionFactory>(); ISession session = factory.OpenSession(); SessionDelegate sessDelegate = new SessionDelegate(true, session, store); store.Store(Constants.DefaultAlias, sessDelegate); ISession session2 = store.FindCompatibleSession(Constants.DefaultAlias); Assert.IsNotNull(session2); Assert.AreSame(sessDelegate, session2); Thread newThread = new Thread(FindCompatibleSessionOnOtherThread); newThread.Start(); arEvent.WaitOne(); sessDelegate.Dispose(); Assert.IsTrue(store.IsCurrentActivityEmptyFor(Constants.DefaultAlias)); }
/// <summary> /// Open a <c>ISession</c> on the given connection /// </summary> /// <param name="connection">The connection.</param> /// <param name="autoClose">if set to <c>true</c> [auto close].</param> /// <returns>A session</returns> /// <remarks> /// The <c>ISession</c> is save in the <see cref="ISessionStore"/> /// </remarks> public ISession OpenSession(IDbConnection connection, bool autoClose) { ISession session = new DataMapperSession(connection, this, transactionManager, autoClose); sessionStore.Store(session); return(session); }
/// <summary> /// Open a connection. /// </summary> /// <returns>A IDalSession.</returns> public IDalSession OpenConnection() { if (_daoSessionHandler == null) { throw new DataAccessException("DaoManager could not get DaoSession. DaoSessionPool was null (possibly not configured)."); } if (_sessionStore.LocalSession != null) { throw new DataAccessException("DaoManager could not invoke OpenConnection(). A connection is already started. Call CloseConnection first."); } IDalSession session = _daoSessionHandler.GetDaoSession(this); _sessionStore.Store(session); session.OpenConnection(); return(session); }
public async Task<QueryReserveCancel.Result> CancelReserveAsync(ISessionStore session) { if (session == null) { throw new ArgumentNullException(nameof(session)); } var login = session.Retrieve<Session>("login"); var reserve = session.Retrieve<Parser.Structs.Layer5705>("reserve"); if (reserve == null) { throw new ArgumentNullException(nameof(reserve)); } var requestData = new Parser.Structs.Layer5769.Request { OrderId = reserve.SaleOrderId }; var response = await _parser.CancelReserveAsync(login, requestData); reserve.Canceled = true; session.Store("reserve", reserve); return new QueryReserveCancel.Result { OrderId = reserve.SaleOrderId, Code = response.Result, Status = response.Status }; }
public void FindCompatibleSession() { ISessionStore store = container.Resolve <ISessionStore>(); ISessionFactory factory = container.Resolve <ISessionFactory>(); ISession session = store.FindCompatibleSession(Constants.DefaultAlias); Assert.IsNull(session); session = factory.OpenSession(); SessionDelegate sessDelegate = new SessionDelegate(true, session, store); store.Store(Constants.DefaultAlias, sessDelegate); Assert.IsNotNull(sessDelegate.SessionStoreCookie); ISession session2 = store.FindCompatibleSession("something in the way she moves"); Assert.IsNull(session2); session2 = store.FindCompatibleSession(Constants.DefaultAlias); Assert.IsNotNull(session2); Assert.AreSame(sessDelegate, session2); session.Dispose(); store.Remove(sessDelegate); session = store.FindCompatibleSession(Constants.DefaultAlias); Assert.IsNull(session); Assert.IsTrue(store.IsCurrentActivityEmptyFor(Constants.DefaultAlias)); }
public async Task <QueryTrains.Result> QueryTrainsAsync(ISessionStore session, QueryTrains.Request request) { if (session == null) { throw new ArgumentNullException(nameof(session)); } if (request == null) { throw new ArgumentNullException(nameof(request)); } var credentials = _config; // TODO: credentials provider, issue ref #42 var parserSession = await _parser.CreateSession(credentials.Username, credentials.Password); session.Store("login", parserSession); var result = await _parser.GetTrainsAsync( parserSession, new Parser.Structs.Layer5827.Request(request.From, request.To, request.DepartDate) ); int optionRef = 0; if (result.Tp != null && result.Tp.Length == 1) { request.DepartDate = request.DepartDate.ToLocalTime(); var options = new Internal.TrainOptions { Request = request, Options = (from t in result.Tp[0].List let tt = t.FlMsk == 3 || t.FlMsk == 2 ? TimeType.MOSCOW : TimeType.LOCAL let durationParts = t.TimeInWay.Split(':').Select(x => int.Parse(x)) select new Internal.TrainOptions.Option { OptionRef = ++optionRef, Number = t.Number, DisplayNumber = t.Number2, //Type = t.Type, Brand = t.Brand, BEntire = t.BEntire, IsFirm = t.BFirm, HasElectronicRegistration = t.ElReg, HasDynamicPricing = t.VarPrice, Depart = new TripEvent(t.Date0, t.Time0, tt, t.Station0, result.Tp[0].FromCode), Arrive = new TripEvent(t.Date1, t.Time1, tt, t.Station1, result.Tp[0].WhereCode), TripDuration = new TimeSpan(durationParts.ElementAt(0), durationParts.ElementAt(1), 00), RouteStart = new TripEvent(t.TrDate0, t.TrTime0, tt, t.Route0, null), RouteEndStation = t.Route1 }) }; session.Store("train_options", options); } optionRef = 0; return(new QueryTrains.Result { Origin = result.Tp[0].From, OriginCode = result.Tp[0].FromCode, Destination = result.Tp[0].Where, DestinationCode = result.Tp[0].WhereCode, DepartureDate = DateTime.Parse(result.Tp[0].Date), TimeType = result.Tp[0].DefShowTime.Equals("msk") ? TimeType.MOSCOW : TimeType.GMT, NoFreeTickets = result.Tp[0].NoSeats, Trains = from t in result.Tp[0].List let tt = t.FlMsk == 3 || t.FlMsk == 2 ? TimeType.MOSCOW : TimeType.LOCAL let durationParts = t.TimeInWay.Split(':').Select(x => int.Parse(x)) select new QueryTrains.Result.Train { OptionRef = ++optionRef, Name = t.TrainName, Number = t.Number, DisplayNumber = t.Number2, Depart = new TripEvent(t.Date0, t.Time0, tt, t.Station0, result.Tp[0].FromCode), Arrive = new TripEvent(t.Date1, t.Time1, tt, t.Station1, result.Tp[0].WhereCode), ArriveLocal = new TripEvent(t.LocalDate1, t.LocalTime1, TimeType.LOCAL, t.Station1, result.Tp[0].WhereCode), RouteStart = new TripEvent(t.TrDate0, t.TrTime0, tt, t.Route0, null), RouteEndStation = t.Route1, TimezoneDifference = t.TimeDeltaString1, TripDuration = new TimeSpan(durationParts.ElementAt(0), durationParts.ElementAt(1), 00), //TripDistance = null, Carrier = t.Carrier, Brand = t.Brand, IsFirm = t.BFirm, HasElectronicRegistration = t.ElReg, HasDynamicPricing = t.VarPrice, IsComponent = t.CarMods, Cars = from c in t.Cars select new QueryTrains.Result.Car { Type = c.TypeLoc, ServiceClass = c.ServCls, FreeSeats = c.FreeSeats, MinPrice = c.Tariff, BonusPoints = c.Pt } } }); }
public async Task <QueryReserveCreate.Result> CreateReserveAsync(ISessionStore session, QueryReserveCreate.Request request) { if (session == null) { throw new ArgumentNullException(nameof(session)); } if (request == null) { throw new ArgumentNullException(nameof(request)); } var login = session.Retrieve <Session>("login"); var trains = session.Retrieve <Internal.TrainOptions>("train_options"); var train = trains.Options.First(x => x.OptionRef == request.Option.TrainOptionRef); var cars = session.Retrieve <Internal.CarOptions>("car_options"); var car = cars.Options.First(x => x.OptionRef == request.Option.CarOptionRef); var requestData = new Parser.Structs.Layer5705.Request { Passengers = request.Passengers.Select(p => _passengerConverter.ToLayer5705(p, train.Depart.DateAndTime, cars.AgeLimits)).ToArray(), Orders = new Parser.Structs.Layer5705.RequestOrder[] { new Parser.Structs.Layer5705.RequestOrder { Range0 = request.Option.Range.From, Range1 = request.Option.Range.To, PlUpdown = $"{request.Option.Range.LowerCount.ToString()}{request.Option.Range.UpperCount.ToString()}", PlBedding = request.Option.Bedding, PlComp = request.Option.Location, Dir = 1, Code0 = Int32.Parse(train.Depart.Station.Code), Code1 = Int32.Parse(train.Arrive.Station.Code), Route0 = train.RouteStart.Station.Name, Route1 = train.RouteEndStation, Number = train.Number, Number2 = train.DisplayNumber, Brand = train.Brand, Letter = Char.IsLetter(train.DisplayNumber.Last()) ? train.DisplayNumber.Last().ToString() : "", Ctype = car.Type.Id, Cnumber = car.Number, ClsType = car.ServiceClass, ElReg = car.HasElectronicRegistration, Ferry = false, SeatType = null, TicketPriceInPoints = 0, TrainType = train.Type, ConferenceRoomFlag = false, CarrierGroupId = 1, Datetime0 = train.Depart.GetDateTimeString(), Teema = 0, CarVipFlag = 0 } } }; var result = await _parser.ReserveTicketAsync(login, requestData); session.Store("reserve", result); var price = _priceConverter.ToDecimal(result.TotalSum); var fixedExtraCharge = new FixedFeeType(200).ToCharge(price); var priceWithCharges = new Price(price, new Charges { Extra = new List <Charge> { fixedExtraCharge } }); session.Store("price", priceWithCharges); return(new QueryReserveCreate.Result { SaleOrderId = result.SaleOrderId, Price = priceWithCharges, Orders = from o in result.Orders let durationParts = o.TimeInWay.Split(':').Select(x => int.Parse(x)) select new QueryReserveCreate.Result.ResultOrder { OrderId = o.OrderId, Cost = o.Cost, TotalCostPt = o.TotalCostPt, Created = o.Created, SeatNums = o.SeatNums, DirName = o.DirName, Agent = o.Agent, //DeferredPayment = o.DeferredPayment, Train = new TrainInfo { Number = o.Number, DisplayNumber = o.Number2, Depart = new TripEvent(o.Date0, o.Time0, o.Msk0 ? TimeType.MOSCOW : TimeType.LOCAL, o.Station0, o.Code0), Arrive = new TripEvent(o.Date1, o.Time1, o.Msk1 ? TimeType.MOSCOW : TimeType.LOCAL, o.Station1, o.Code1), RouteStart = new TripEvent(o.TrDate0, o.TrTime0, o.Msk0 ? TimeType.MOSCOW : TimeType.LOCAL, o.Route0, null), ArriveLocal = new TripEvent(o.LocalDate1, o.LocalTime1, TimeType.LOCAL, o.Station1, o.Code1), RouteEndStation = o.Route1, TimezoneDifference = o.TimeDeltaString1, TripDuration = new TimeSpan(durationParts.ElementAt(0), durationParts.ElementAt(1), 00), //TripDistance = train.TripDistance, Carrier = o.Carrier, Brand = train.Brand, IsFirm = train.IsFirm, HasElectronicRegistration = train.HasElectronicRegistration, HasDynamicPricing = train.HasDynamicPricing }, Car = new QueryReserveCreate.Result.Car { Number = o.CNumber, Type = _carTypeConverter.ByCTypeI(o.Ctype), // WARNING: it's cTypeI here, not cType ServiceClass = o.ClsType, AdditionalInfo = o.TimeInfo }, Tickets = o.Tickets.Select(t => new QueryReserveCreate.Result.Ticket { TicketId = t.TicketId, Cost = t.Cost, Seats = t.Seats, SeatsType = t.SeatsType, Tariff = new QueryReserveCreate.Result.Tariff(t.Tariff, t.TariffName), Teema = t.Teema, Passengers = t.Pass.Select(p => _passengerConverter.ToPassenger(Array.IndexOf(t.Pass, p) + 1, p)) }) }, PaymentSystems = result.PaymentSystems.Select(p => new QueryReserveCreate.Result.PaymentSystem { Id = p.Id, Code = p.Code, Name = p.Name, Tip = p.Tip }) }); }
public async Task <QueryCars.Result> QueryCarsAsync(ISessionStore session, QueryCars.Request request) { if (session == null) { throw new ArgumentNullException(nameof(session)); } if (request == null) { throw new ArgumentNullException(nameof(request)); } var login = session.Retrieve <Session>("login"); var trains = session.Retrieve <Internal.TrainOptions>("train_options"); var selected = trains.Options.First(x => x.OptionRef == request.OptionRef); var result = await _parser.GetCarsAsync(login, new Parser.Structs.Layer5764.Request( 0, trains.Request.From, trains.Request.To, trains.Request.DepartDateString, selected.DisplayNumber, selected.BEntire )); var optionRef = 0; var carsQuery = from c in result.Lst[0].Cars select new QueryCars.Result.Car { OptionRef = ++optionRef, Number = c.CNumber, Type = _carTypeConverter.ByCType(c.CType), ServiceClass = c.ClsType, ServiceClassInternational = c.IntServiceClass, Letter = c.Letter, Categories = c.AddSigns, SchemeId = c.SchemeId.ToString(), FreePlaceNumbers = Internal.Converters.FreePlaceNumbersConverter.Convert(c.Places).ToArray(), SpecialSeatTypes = c.SpecialSeatTypes?.Split(' '), FreeSeats = (from s in c.Seats let price = _priceConverter.ToPrice(s.Tariff) select new QueryCars.Result.SeatGroup { Type = s.Type, Label = s.Label?.Replace(" ", " "), Price = price, Places = Internal.Converters.FreePlacesConverter.Convert(s.Places, price).ToArray(), Count = s.Free }).ToArray(), Services = c.Services.Select(s => new QueryCars.Result.CarService { Name = s.Name, Description = s.Description }).ToArray(), ServicesDescription = c.ClsName, Price = _priceConverter.ToPriceRange(c.Tariff, c.Tariff2), Carrier = c.Carrier, Owner = c.Owner, HasElectronicRegistration = c.ElReg, HasDynamicPricing = c.VarPrice, IsNoSmoking = c.NoSmok, CanAddBedding = c.Bedding, HasBeddingIncluded = c.ForcedBedding, IsTwoStorey = c.BDeck2, IsWebSalesForbidden = c.InetSaleOff }; var cars = carsQuery.ToList(); var schemePatterns = result.Schemes.Select(x => Internal.Converters.CarSchemeConverter.Convert(x.Id, x.Html)); var ageLimits = new QueryCars.Result.AgeRestrictions { ChildWithPlace = result.ChildrenAge, InfantWithoutPlace = result.MotherAndChildAge }; var options = new Internal.CarOptions { Options = cars, Schemes = schemePatterns, AgeLimits = ageLimits }; session.Store("car_options", options); var insuranceProviders = result.InsuranceCompany.Select(x => new InsuranceProvider { Id = x.Id, FullName = x.ShortName, ShortName = x.ShortName, OfferUrl = x.OfferUrl, InsuranceCost = x.InsuranceCost, InsuranceBenefit = x.InsuranceBenefit }); return(new QueryCars.Result { // TODO: add train info Cars = cars, AgeLimits = ageLimits, InsuranceProviders = insuranceProviders }); }