private bool TryGetMapping(
            string entityName,
            MappingUpdatedEvent updatedEvent,
            out EntityWithETag <MdmId> mapping)
        {
            mapping = mappingService.GetMapping(entityName, updatedEvent.EntityId, updatedEvent.MappingId);
            if (mapping.Object == null)
            {
                this.eventAggregator.Publish(new ErrorEvent("Unable to retrieve original mapping"));
                return(false);
            }

            return(true);
        }
        private void MappingUpdated(MappingUpdatedEvent updatedEvent)
        {
            if (updatedEvent.Cancelled)
            {
                return;
            }

            EntityWithETag <MdmId> entityWithETag;

            if (!TryGetMapping("Broker", updatedEvent, out entityWithETag))
            {
                return;
            }

            if (entityWithETag.Object.EndDate <= updatedEvent.StartDate)
            {
                var message = string.Format(
                    "The start date of the new mapping must be before {0}",
                    entityWithETag.Object.EndDate);
                this.eventAggregator.Publish(new ErrorEvent(message));
                return;
            }

            var newMapping = NewMapping(entityWithETag, updatedEvent);

            if (!TryCreateMapping("Broker", newMapping, updatedEvent))
            {
                return;
            }

            if (!TryGetMapping("Broker", updatedEvent, out entityWithETag))
            {
                return;
            }

            entityWithETag.Object.EndDate = updatedEvent.StartDate.AddSeconds(-1);

            if (!TryUpdateMapping("Broker", entityWithETag, updatedEvent))
            {
                return;
            }

            this.LoadBrokerFromService(updatedEvent.EntityId, updatedEvent.StartDate, true);
            this.eventAggregator.Publish(new StatusEvent(Message.MappingUpdated));
        }
        private bool TryUpdateMapping(
            string entityName,
            EntityWithETag <MdmId> entityWithETag,
            MappingUpdatedEvent updatedEvent)
        {
            var response = mappingService.UpdateMapping(
                entityName,
                updatedEvent.MappingId,
                updatedEvent.EntityId,
                entityWithETag);

            if (!response.IsValid)
            {
                this.eventAggregator.Publish(
                    new ErrorEvent(response.Fault != null ? response.Fault.Message : "Unknown Error"));
                return(false);
            }

            return(true);
        }
 public void OnCancel()
 {
     this.eventAggregator.Publish(MappingUpdatedEvent.ForCancellation());
 }