Exemplo n.º 1
0
        /// <summary>
        /// Compare two seats collections. 
        /// If there is one or more seat included in both collection the result is true.
        /// </summary>
        /// <param name="group1"></param>
        /// <param name="group2"></param>
        /// <returns></returns>
        public static bool CompareSeates(SeatIndex[] group1, SeatIndex[] group2)
        {
            if (group1 == null || group1.Length == 0)
            {
                if (group2 == null || group2.Length == 0)
                    return true;
                else
                    return false;                
            }

            if (group2 == null || group2.Length == 0)
            {
                if (group1 == null || group1.Length == 0)
                    return true;
                else
                    return false;
            }

            if (group1.Length != group2.Length)
                return false;

          
            foreach (var item in group1)
            {
                if (!group2.Contains(item))
                    return false;
            }

            return true;
        }
        public void OrderTicket(Order newOrder, SeatIndex[] seats, Guid callID)
        {
            try
            {
                callback_prox = CallBackChannelFactory.GetProxy(false);
                Guid result;

                // Do the job and call back the ticketing bridge
                result = generalTicketing.OrderTicket(newOrder, seats);

                //Call back the bridge
                callback_prox.IDArrived(result, callID);
            }
            catch (TicketingException tex)
            {
                LoggingManager.Logger.Log(LoggingCategory.Error, StringsResource.TicketingFailed + " " + tex.Message);
                throw new TicketingException(StringsResource.TicketingFailed + " " + tex.Message, tex);
            }
            catch (Exception ex)
            {
                LoggingManager.Logger.Log(LoggingCategory.Error, StringsResource.FailedToContactTicketingBridge + " " + ex.Message);
                throw new TicketingException(StringsResource.TicketingFailed + " " + ex.Message, ex);
            }
            finally
            {
                if ((callback_prox != null) && ((callback_prox as ICommunicationObject).State == CommunicationState.Opened))
                    (callback_prox as ICommunicationObject).Close();
            }
        }
        public Guid OrderTicket(Order newOrder, SeatIndex[] seats)
        {
            ITicketingServiceOneWay prox = null;
            Guid callId = Guid.NewGuid();
            try
            {
                // TODO: Ex2 - Add code to call the one-way service
                // Find a proxy to the ticketing service (one-way)
                prox = TicketingServiceOneWayProxyFactory.GetProxy(false);
                AutoResetEvent arrived = new AutoResetEvent(false);

                // Create a ResultPackage with a wait handle to wait on until a response arrives (on another channel)
                ResultPackage pack = new ResultPackage() { ResultArrived = arrived };
                ResultsCache.Current.SetPackage(callId, pack);

                // Call the pricing service on MSMQ channel on another thread.
                Action<ITicketingServiceOneWay> del =
                    (p => p.OrderTicket(newOrder, seats, callId));
                del.BeginInvoke(prox, null, null);
                //Wait until result arrives
                arrived.WaitOne(timeout);
                Guid result = (Guid)ResultsCache.Current.GetResult(callId);
                ResultsCache.Current.ClearResult(callId);
                return result;

            }
            catch (Exception ex)
            {
                LoggingManager.Logger.Log(LoggingCategory.Error, StringsResource.FailedToContactTicketing + " " + ex.Message);
                throw new TicketingException(StringsResource.TicketingFailed + " " + ex.Message, ex);
            }
            finally
            {
                if ((prox != null) && ((prox as ICommunicationObject).State == CommunicationState.Opened))
                    (prox as ICommunicationObject).Close();
            }

        }
        private void ShowAvailableSeats(SeatIndex[] reservedSeats)
        {
            seatsLayout.Children.Clear();
            seatsLayout.RowDefinitions.Clear();
            seatsLayout.ColumnDefinitions.Clear();
            _seats.Clear();
            if (reservedSeats != null)
            {
                for (int i = 0; i < ROWS_IN_HALL; i++)
                {
                    seatsLayout.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
                    for (int j = 0; j < SEATS_IN_ROW; j++)
                    {
                        seatsLayout.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
                        SeatMarker seatMarker = new SeatMarker();
                        if (reservedSeats.FirstOrDefault(s => s.Row == i + 1 && s.Seat == j + 1) != null)
                        {
                            seatMarker.Availability = SeatAvailability.Reserved;
                        }
                        else
                        {
                            seatMarker.Availability = SeatAvailability.Available;
                        }
                        seatsLayout.Children.Add(seatMarker);
                        seatMarker.ReservationChanged += new SeatMarker.ReservationChangedEventHandler(seatMarker_ReservationChanged);

                        Grid.SetRow(seatMarker, i);
                        Grid.SetColumn(seatMarker, j);
                    }
                }
            }
        }
        public Guid OrderTicket(Contracts.Order newOrder, SeatIndex[] seats)
        {
            //1. validate the order is valid
            //validate newOrder.EventInfo and newOrder.CustomerInfo
            ICrmBase crmProx = null;
            IShowsService showsProx = null;
            IPricingService pricingProx = null;
            IReservationService reservationsProx = null;
            Guid reservationID;

            if (newOrder == null)
                throw new NullReferenceException(StringsResource.NullOrder);
            if (newOrder.CustomerInfo == null)
                throw new NullReferenceException(StringsResource.NullCustomer);
            if (newOrder.EventInfo == null)
                throw new NullReferenceException(StringsResource.NullEvent);
            if ((seats == null) || (seats.Count() == 0))
                throw new ArgumentException(StringsResource.NoSeatsInOrder);

            #region Validate Customer
            //Contact the Crm service to ensure the customer exist.
            lock (this)
            {
                if (crmChf == null)
                    crmChf = new ChannelFactory<ICrmBase>("CrmCertEP");
            }

            try
            {
                crmProx = crmChf.CreateChannel();
                var customer = crmProx.GetCustomerByID(newOrder.CustomerInfo.ID);
                if (customer == null)
                    throw new TicketingException(string.Format(StringsResource.CustomerNotFound, newOrder.CustomerInfo.ID));
                newOrder.CustomerInfo = CustomerMapper.MapToTicketingCustomer(customer);

            }
            catch (Exception ex)
            {
                LoggingManager.Logger.Log(LoggingCategory.Error, ex.Message);
                throw new TicketingException(StringsResource.FailedToContactCrm, ex);
            }
            finally
            {
                var channel = crmProx as ICommunicationObject;
                if ((channel != null) && (channel.State == CommunicationState.Opened))
                    channel.Close();
            }

            #endregion

            #region Validate Event

            ShowsService.Contracts.Event _event;

            //1. contact the shows service to fetch information about the event.
            lock (this)
            {
                if (showsChf == null)
                    showsChf = new ChannelFactory<IShowsService>("ShowEP");
            }

            try
            {
                showsProx = showsChf.CreateChannel();
                _event = showsProx.FindEventByID(newOrder.EventInfo.EventID);
                if (_event == null)
                    throw new TicketingException(string.Format(StringsResource.EventNotFound, newOrder.EventInfo.EventID));
                newOrder.EventInfo = _event;
            }
            catch (Exception ex)
            {
                LoggingManager.Logger.Log(LoggingCategory.Error, ex.Message);
                throw new TicketingException(StringsResource.FailedToContactShows + " " + ex.Message, ex);
            }
            finally
            {
                var channel = showsProx as ICommunicationObject;
                if ((channel != null) && (channel.State == CommunicationState.Opened))
                    channel.Close();
            }

            //2. check the event exist and it is open
            if ((_event == null) || (_event.State != EventState.Opened))
                throw new TicketingException(StringsResource.InvalidOrClosedEvent);
            #endregion

            #region Call Pricing to calculate the total price
            lock (this)
            {
                if (pricingChf == null)
                    pricingChf = new ChannelFactory<IPricingService>("PricingEP");
            }

            try
            {
                pricingProx = pricingChf.CreateChannel();
                var price = pricingProx.CalculatePrice(reductionCode: newOrder.CustomerInfo.ReductionCode.Value,
                                                            policyName: newOrder.EventInfo.PricingPolicy,
                                                            listPrice: (int)newOrder.EventInfo.ListPrice,
                                                            numberOfTickets: seats.Count(), currency: null);
                newOrder.TotalPrice = price * seats.Count();

            }
            catch (Exception ex)
            {
                LoggingManager.Logger.Log(LoggingCategory.Error, ex.Message);
                throw new TicketingException(StringsResource.FailedToContactPricing + " " + ex.Message, ex);
            }
            finally
            {
                var channel = pricingProx as ICommunicationObject;
                if ((channel != null) && (channel.State == CommunicationState.Opened))
                    channel.Close();
            }
            #endregion

            #region Call the resrevation service and reserve seats
            //Contact the HallState service to perform the reservation.
            lock (this)
            {
                if (reservationsChf == null)
                    reservationsChf = new ChannelFactory<IReservationService>("ReservationsEP");
            }

            try
            {
                reservationsProx = reservationsChf.CreateChannel();
                reservationID = reservationsProx.CreateResevation(new Reservation()
                {
                    ID = Guid.NewGuid(),
                    CustomerID = newOrder.CustomerInfo.ID,
                    EventID = newOrder.EventInfo.EventID,
                    Seats = seats.ToList(),
                    Remarks = newOrder.Remarks
                });

            }
            catch (Exception ex)
            {
                LoggingManager.Logger.Log(LoggingCategory.Error, ex.Message);
                throw new TicketingException(StringsResource.FailedToContactHallState + " " + ex.Message, ex);
            }
            finally
            {
                var channel = reservationsProx as ICommunicationObject;
                if ((channel != null) && (channel.State == CommunicationState.Opened))
                    channel.Close();
            }

            #endregion


            newOrder.ID = reservationID;
            newOrder.State = OrderState.Created;
            manager.OrderTicket(newOrder);

            #region ClientNotifications
            //ClientNotifications
            string userName = string.Empty;
            if ((OperationContext.Current != null) && (OperationContext.Current.ServiceSecurityContext != null) && (OperationContext.Current.ServiceSecurityContext.PrimaryIdentity != null))
                userName = OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name;

            var clientRegistrations = MemoryRepository.Current.GetRegistrations(NotificationTypes.TicketingNotification, userName);
            if (clientRegistrations.Length > 0)
                ClientNotificationsManager.SendNotifications(new OrderMessage()
                {
                    Content = "Order Created",
                    OrderID = reservationID
                }, clientRegistrations);

            #endregion

            return newOrder.ID;
        }