private void EnsureLineItemsHasShippingMethod()
        {
            OrderForm orderForm = CachedOrderGroup.GetDefaultOrderForm();

            foreach (LineItem lineItem in
                     orderForm.LineItems
                     .OfType <LineItem>()
                     .Where(x => x.ShippingMethodId == Guid.Empty))
            {
                lineItem.ShippingMethodId = Guid.NewGuid();
            }
        }
        protected virtual Shipment GetShipmentFromCachedOrderGroup(string shipmentId)
        {
            OrderForm orderForm = CachedOrderGroup.GetDefaultOrderForm();

            int index = orderForm.Shipments.IndexOf(new Guid(shipmentId));

            if (index < 0)
            {
                throw ItemDoesNotExist(MetadataDefinitions.Shipment.EntityName, shipmentId);
            }

            return(orderForm.Shipments[index]);
        }
        protected override void DeleteRelatedItem(CommerceDeleteRelatedItem deleteRelatedItemOperation)
        {
            string shipmentId = GetSearchModelId(deleteRelatedItemOperation, MetadataDefinitions.Shipment.EntityName, true);

            if (!String.IsNullOrEmpty(shipmentId))
            {
                Shipment deletingShipment = GetShipmentFromCachedOrderGroup(shipmentId);
                CachedOrderGroup.GetDefaultOrderForm().Shipments.Remove(deletingShipment);
            }
            else
            {
                CachedOrderGroup.GetDefaultOrderForm().Shipments.Clear();
            }
        }
        protected override void CreateRelatedItem(CommerceCreateRelatedItem createRelatedItemOperation)
        {
            CommerceEntity shipmentEntity = createRelatedItemOperation.Model;

            var newShipment =
                CommerceServerClassFactory.CreateInstance <Shipment>(
                    shipmentEntity.ModelName, CommerceServerArea.Orders);

            shipmentEntity.Id = newShipment.ShipmentId.ToString("B");

            Translator.ToExternalEntity(shipmentEntity, newShipment);

            OrderForm orderForm = CachedOrderGroup.GetDefaultOrderForm();

            UpdateShippingLineItemsAssociation(newShipment, orderForm);

            orderForm.Shipments.Add(newShipment);
        }
        protected override void CreateRelatedItem(CommerceCreateRelatedItem createRelatedItemOperation)
        {
            CommerceEntity paymentEntity = createRelatedItemOperation.Model;

            var newPayment =
                CommerceServerClassFactory.CreateInstance <Payment>(
                    paymentEntity.ModelName,
                    CommerceServerArea.Orders,
                    new[] { paymentEntity.Properties[MetadataDefinitions.Payment.Properties.BillingAddressId], Guid.Empty, });

            paymentEntity.Id = newPayment.PaymentId.ToString("B");

            Translate(paymentEntity, newPayment);

            OrderForm orderForm = CachedOrderGroup.GetDefaultOrderForm();

            orderForm.Payments.Add(newPayment);
        }
        protected override void UpdateRelatedItem(CommerceUpdateRelatedItem updateRelatedItemOperation)
        {
            OrderForm orderForm = CachedOrderGroup.GetDefaultOrderForm();

            CommerceEntity model     = updateRelatedItemOperation.GetModel(updateRelatedItemOperation.Model.ModelName);
            string         paymentId = GetSearchModelId(updateRelatedItemOperation, MetadataDefinitions.Payment.EntityName, true);

            if (!String.IsNullOrEmpty(paymentId))
            {
                Payment updatingPayment = GetPaymentFromCachedOrderGroup(paymentId);
                Translate(model, updatingPayment);
            }
            else
            {
                foreach (Payment payment in orderForm.Payments)
                {
                    Translate(model, payment);
                }
            }
        }
        protected override void UpdateRelatedItem(CommerceUpdateRelatedItem updateRelatedItemOperation)
        {
            OrderForm orderForm = CachedOrderGroup.GetDefaultOrderForm();

            CommerceEntity model      = updateRelatedItemOperation.GetModel(MetadataDefinitions.Shipment.EntityName);
            string         shipmentId = GetSearchModelId(updateRelatedItemOperation, MetadataDefinitions.Shipment.EntityName, true);

            if (!String.IsNullOrEmpty(shipmentId))
            {
                Shipment updatingShipment = GetShipmentFromCachedOrderGroup(shipmentId);
                Translator.ToExternalEntity(model, updatingShipment);
                UpdateShippingLineItemsAssociation(updatingShipment, orderForm);
            }
            else
            {
                foreach (Shipment shipment in orderForm.Shipments)
                {
                    Translator.ToExternalEntity(model, shipment);
                }
            }
        }