Ejemplo n.º 1
0
        private Tuple <Order, bool, bool> GetOrderInfo(Cache.SecurityData securityData, OrderTypes type, long transactionId, long?orderId, string orderStringId, Func <long, Order> createOrder, out bool isNew, bool newOrderRaised = false)
        {
            if (createOrder == null)
            {
                throw new ArgumentNullException("createOrder");
            }

            if (transactionId == 0 && orderId == null && orderStringId.IsEmpty())
            {
                throw new ArgumentException(LocalizedStrings.Str719);
            }

            var isNew2 = false;
            var orders = securityData.Orders;

            OrderInfo info;

            if (transactionId == 0)
            {
                if (orderStringId.IsEmpty())
                {
                    throw new ArgumentOutOfRangeException("transactionId");
                }

                info = orders.First(p => p.Value.Order.StringId.CompareIgnoreCase(orderStringId)).Value;
            }
            else
            {
                var cancelKey   = CreateOrderKey(type, transactionId, true);
                var registerKey = CreateOrderKey(type, transactionId, false);

                var cancelledOrder = (Order)orders.TryGetValue(cancelKey);

                // проверяем не отмененная ли заявка пришла
                if (cancelledOrder != null && (cancelledOrder.Id == orderId || (!cancelledOrder.StringId.IsEmpty() && cancelledOrder.StringId.CompareIgnoreCase(orderStringId))))
                {
                    isNew = false;
                    return(Tuple.Create(cancelledOrder, true /*, (Order)orders.TryGetValue(registerKey) != null*/, false));
                }

                info = orders.SafeAdd(registerKey, key =>
                {
                    isNew2 = true;

                    var o = createOrder(transactionId);

                    if (o == null)
                    {
                        throw new InvalidOperationException(LocalizedStrings.Str720Params.Put(transactionId));
                    }

                    if (o.ExtensionInfo == null)
                    {
                        o.ExtensionInfo = new Dictionary <object, object>();
                    }

                    _cache.AddOrder(o);

                    // с таким же идентификатором транзакции может быть заявка по другому инструменту
                    _cache.AllOrdersByTransactionId.TryAdd(Tuple.Create(transactionId, type == OrderTypes.Conditional), o);

                    return(new OrderInfo(o));
                });
            }

            var raiseNewOrder = info.RaiseNewOrder;

            if (raiseNewOrder && newOrderRaised)
            {
                info.RaiseNewOrder = false;
            }

            isNew = isNew2;
            return(Tuple.Create((Order)info, false, raiseNewOrder));
        }