Beispiel #1
0
        public void Handle(CreateDeepInternationalAffiliation command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var person = _entities.Get <Person>().SingleOrDefault(p => p.User.Name == command.Principal.Identity.Name);

            if (person == null)
            {
                string message = string.Format("Person {0} not found.", command.Principal.Identity.Name);
                throw new Exception(message);
            }

            var createExpertiseCommand = new CreateInternationalAffiliation(command.Principal)
            {
                From        = command.From,
                To          = command.To,
                OnGoing     = command.OnGoing,
                Institution = command.Institution,
                Position    = command.Position
            };

            _createInternationalAffiliation.Handle(createExpertiseCommand);

            foreach (var placeId in command.PlaceIds)
            {
                var createExpertiseLocationCommand = new CreateInternationalAffiliationLocation(
                    command.Principal,
                    createExpertiseCommand.CreatedInternationalAffiliation.RevisionId,
                    placeId);

                _createInternationalAffiliationLocation.Handle(createExpertiseLocationCommand);
            }

            if (!command.NoCommit)
            {
                _unitOfWork.SaveChanges();
            }

            command.CreatedInternationalAffiliation   = createExpertiseCommand.CreatedInternationalAffiliation;
            command.CreatedInternationalAffiliationId = command.CreatedInternationalAffiliation.RevisionId;
        }
        public void Handle(UpdateInternationalAffiliation command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            /* Retrieve the expertise to update. */
            var target = _entities.Get <InternationalAffiliation>().SingleOrDefault(a => a.RevisionId == command.Id);

            if (target == null)
            {
                string message = String.Format("Affiliation Id {0} not found.", command.Id);
                throw new Exception(message);
            }

            var updated = new InternationalAffiliation
            {
                From        = command.From,
                To          = command.To,
                OnGoing     = command.OnGoing,
                Institution = command.Institution,
                Position    = command.Position,
                Locations   = command.Locations
            };

            /* If target fields equal new field values, we do not proceed. */
            if (target.Equals(updated))
            {
                return;
            }

            /* Run through all new locations and attempt to find same in target.  If not found, create.*/
            foreach (var location in command.Locations.ToList())
            {
                var targetLocation = target.Locations.SingleOrDefault(x => x.PlaceId == location.PlaceId);
                if (targetLocation == null)
                {
                    var createAffiliationLocation = new CreateInternationalAffiliationLocation(
                        command.Principal, target.RevisionId, location.PlaceId)
                    {
                        NoCommit = true
                    };

                    _createAffiliationLocation.Handle(createAffiliationLocation);
                }
            }

            /* Delete activity locations. Run through the targets list of locations and try to find
             *  a matching one in the updated list.  If not found, it must have been deleted. */
            foreach (var location in target.Locations.ToList())
            {
                var updateLocation = command.Locations.SingleOrDefault(x => x.PlaceId == location.PlaceId);
                if (updateLocation == null)
                {
                    var deleteAffiliationLocationCommand = new DeleteInternationalAffiliationLocation(
                        command.Principal, location.RevisionId)
                    {
                        NoCommit = true
                    };

                    _deleteAffiliationLocation.Handle(deleteAffiliationLocationCommand);
                }
            }

            /* Update fields */
            target.From               = command.From;
            target.To                 = command.To;
            target.OnGoing            = command.OnGoing;
            target.Institution        = command.Institution;
            target.Position           = command.Position;
            target.Locations          = command.Locations;
            target.UpdatedOnUtc       = command.UpdatedOn.ToUniversalTime();
            target.UpdatedByPrincipal = command.Principal.Identity.Name;

            _entities.Update(target);

            if (!command.NoCommit)
            {
                _unitOfWork.SaveChanges();
            }
        }