예제 #1
0
        public void Handle(DeleteEstablishmentName command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // load target
            var establishmentName = _entities.Get <EstablishmentName>()
                                    .EagerLoad(_entities, new Expression <Func <EstablishmentName, object> >[]
            {
                x => x.ForEstablishment,
                x => x.TranslationToLanguage,
            })
                                    .SingleOrDefault(x => x.RevisionId == command.Id)
            ;

            if (establishmentName == null)
            {
                return;                            // delete idempotently
            }
            // log audit
            var audit = new CommandEvent
            {
                RaisedBy      = command.Principal.Identity.Name,
                Name          = command.GetType().FullName,
                Value         = JsonConvert.SerializeObject(new { command.Id }),
                PreviousState = establishmentName.ToJsonAudit(),
            };

            _entities.Create(audit);
            _entities.Purge(establishmentName);
            _unitOfWork.SaveChanges();
            _eventTrigger.Raise(new EstablishmentChanged());
        }
예제 #2
0
        public void Handle(CreateUser command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // determine which establishment the user should be affiliated with
            var emailDomain = command.Name.GetEmailDomain();
            var establishmentToAffiliate =
                _entities.Get <Establishment>()
                .Single(x => x.EmailDomains.Any(y => y.Value.Equals(emailDomain, StringComparison.OrdinalIgnoreCase)));

            // default person to the one provided by another internal command
            var person = command.Person;

            if (person == null && command.PersonId.HasValue)
            {
                // get person by id when provided by command
                person = _entities.Get <Person>().Single(x => x.RevisionId == command.PersonId.Value);
            }
            else if (person == null)
            {
                // otherwise, must create a new person
                var createPersonCommand = new CreatePerson
                {
                    DisplayName = command.Name,
                    NoCommit    = true,
                };
                _createPerson.Handle(createPersonCommand);
                person = createPersonCommand.CreatedPerson;
            }

            if (!person.Emails.Any(x => x.Value.Equals(command.Name, StringComparison.OrdinalIgnoreCase)))
            {
                var createEmailCommand = new CreateEmailAddress(command.Name, person)
                {
                    NoCommit    = true,
                    IsConfirmed = true,
                    IsFromSaml  = false,
                    IsDefault   = true,
                };
                _createEmailAddress.Handle(createEmailCommand);
            }

            var affiliation = new Affiliation
            {
                IsDefault       = true,
                Person          = person,
                EstablishmentId = establishmentToAffiliate.RevisionId
            };

            person.Affiliations.Add(affiliation);

            var user = new User
            {
                Name         = command.Name,
                IsRegistered = command.IsRegistered,
                TenantId     = establishmentToAffiliate.RevisionId,
                Person       = person,
            };

            // log audit
            var audit = new CommandEvent
            {
                RaisedBy = command.Principal.Identity.Name,
                Name     = command.GetType().FullName,
                Value    = JsonConvert.SerializeObject(new
                {
                    command.Name,
                    command.PersonId,
                    command.IsRegistered,
                }),
                NewState = user.ToJsonAudit(),
            };

            _entities.Create(audit);
            _entities.Create(user);
            _entities.SaveChanges();

            command.CreatedUserId = user.RevisionId;

            var userCreated = new UserCreated(command.Principal)
            {
                UserId   = user.RevisionId,
                PersonId = person.RevisionId,
                TenantId = affiliation.EstablishmentId,
            };

            _eventTrigger.Raise(userCreated);
        }
예제 #3
0
        public void Handle(UpdateEstablishment command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // load target
            var entity = _entities.Get <Establishment>()
                         .EagerLoad(_entities, new Expression <Func <Establishment, object> >[]
            {
                x => x.Type.Category,
                x => x.Parent,
            })
                         .Single(x => x.RevisionId == command.Id);

            // only mutate when state is modified
            var parentChanged = (command.ParentId.HasValue && entity.Parent == null) ||
                                (!command.ParentId.HasValue && entity.Parent != null) ||
                                (command.ParentId.HasValue && entity.Parent != null && command.ParentId != entity.Parent.RevisionId);

            if (command.TypeId == entity.Type.RevisionId &&
                command.CeebCode == entity.CollegeBoardDesignatedIndicator &&
                command.UCosmicCode == entity.UCosmicCode &&
                !parentChanged
                )
            {
                return;
            }

            // log audit
            var audit = new CommandEvent
            {
                RaisedBy = command.Principal.Identity.Name,
                Name     = command.GetType().FullName,
                Value    = JsonConvert.SerializeObject(new
                {
                    command.Id,
                    command.ParentId,
                    command.TypeId,
                    command.CeebCode,
                    command.UCosmicCode,
                    //command.ExternalId
                }),
                PreviousState = entity.ToJsonAudit(),
            };

            // update scalars
            if (entity.Type.RevisionId != command.TypeId)
            {
                var establishmentType = _entities.Get <EstablishmentType>()
                                        .Single(x => x.RevisionId == command.TypeId);
                entity.Type = establishmentType;
            }
            entity.CollegeBoardDesignatedIndicator = command.CeebCode;
            entity.UCosmicCode = command.UCosmicCode;
            //entity.ExternalId = command.ExternalId;

            // update parent
            if (parentChanged)
            {
                entity.Parent = command.ParentId.HasValue
                    ? _entities.Get <Establishment>().Single(x => x.RevisionId == command.ParentId)
                    : null;
            }

            audit.NewState = entity.ToJsonAudit();
            _entities.Create(audit);
            _entities.Update(entity);

            // update hierarchy
            _updateHierarchy.Handle(new UpdateEstablishmentHierarchy(entity));

            if (command.NoCommit)
            {
                return;
            }
            _entities.SaveChanges();
            _eventTrigger.Raise(new EstablishmentChanged());
        }
예제 #4
0
        public void Handle(CreateEstablishmentUrl command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // load owner
            var establishment = command.Owner ?? _entities.Get <Establishment>()
                                .EagerLoad(_entities, new Expression <Func <Establishment, object> >[]
            {
                x => x.Urls,
            })
                                .Single(x => x.RevisionId == command.OwnerId.Value)
            ;

            // update previous official URL and owner when changing official URL
            if (command.IsOfficialUrl)
            {
                var officialUrl = establishment.Urls.SingleOrDefault(x => x.IsOfficialUrl);
                if (officialUrl != null)
                {
                    var changeCommand = new UpdateEstablishmentUrl(command.Principal)
                    {
                        Id            = officialUrl.RevisionId,
                        Value         = officialUrl.Value,
                        IsFormerUrl   = officialUrl.IsFormerUrl,
                        IsOfficialUrl = false,
                        NoCommit      = true,
                    };
                    _updateHandler.Handle(changeCommand);
                }
                establishment.WebsiteUrl = command.Value;
            }

            // create new establishment URL
            var establishmentUrl = new EstablishmentUrl
            {
                Value         = command.Value,
                IsOfficialUrl = command.IsOfficialUrl,
                IsFormerUrl   = command.IsFormerUrl,
            };

            establishment.Urls.Add(establishmentUrl);
            establishmentUrl.ForEstablishment = establishment;

            // log audit
            var audit = new CommandEvent
            {
                RaisedBy = command.Principal.Identity.Name,
                Name     = command.GetType().FullName,
                Value    = JsonConvert.SerializeObject(new
                {
                    OwnerId = command.Owner != null ? command.Owner.RevisionId : command.OwnerId,
                    command.Value,
                    command.IsFormerUrl,
                    command.IsOfficialUrl,
                }),
                NewState = establishmentUrl.ToJsonAudit(),
            };

            _entities.Create(audit);
            if (establishment.RevisionId != default(int))
            {
                _entities.Update(establishment);
            }
            command.CreatedEntity = establishmentUrl;
            if (command.NoCommit)
            {
                return;
            }

            _entities.SaveChanges();
            command.Id = establishmentUrl.RevisionId;
            _eventTrigger.Raise(new EstablishmentChanged());
        }
        public void Handle(UpdateEstablishmentName command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // load target
            var establishmentName = _entities.Get <EstablishmentName>()
                                    .EagerLoad(_entities, new Expression <Func <EstablishmentName, object> >[]
            {
                x => x.ForEstablishment.Names.Select(y => y.TranslationToLanguage), // parent & siblings
                x => x.TranslationToLanguage,                                       // language
            })
                                    .Single(x => x.RevisionId == command.Id);

            // only mutate when state is modified
            if (command.Text == establishmentName.Text &&
                command.IsFormerName == establishmentName.IsFormerName &&
                command.IsOfficialName == establishmentName.IsOfficialName &&
                (!string.IsNullOrWhiteSpace(command.LanguageCode)
                    ? establishmentName.TranslationToLanguage != null &&
                 command.LanguageCode.Equals(establishmentName.TranslationToLanguage.TwoLetterIsoCode)
                    : establishmentName.TranslationToLanguage == null))
            {
                return;
            }

            // update previous official name and owner when changing official name
            if (!establishmentName.IsOfficialName && command.IsOfficialName)
            {
                var establishment = establishmentName.ForEstablishment;
                var officialName  = establishment.Names.Single(x => x.IsOfficialName);
                var changeCommand = new UpdateEstablishmentName(command.Principal)
                {
                    Id             = officialName.RevisionId,
                    Text           = officialName.Text,
                    IsFormerName   = officialName.IsFormerName,
                    IsOfficialName = false,
                    LanguageCode   = (officialName.TranslationToLanguage != null)
                        ? officialName.TranslationToLanguage.TwoLetterIsoCode : null,
                    NoCommit = true,
                };
                Handle(changeCommand);
                establishment.OfficialName = command.Text;
                _entities.Update(establishment);
            }

            // get new language
            var language = _entities.Get <Language>()
                           .SingleOrDefault(x => x.TwoLetterIsoCode.Equals(command.LanguageCode, StringComparison.OrdinalIgnoreCase));

            // log audit
            var audit = new CommandEvent
            {
                RaisedBy = command.Principal.Identity.Name,
                Name     = command.GetType().FullName,
                Value    = JsonConvert.SerializeObject(new
                {
                    command.Id,
                    command.Text,
                    command.IsFormerName,
                    command.IsOfficialName,
                    command.LanguageCode,
                }),
                PreviousState = establishmentName.ToJsonAudit(),
            };

            // update scalars
            establishmentName.Text                  = command.Text;
            establishmentName.IsFormerName          = command.IsFormerName;
            establishmentName.IsOfficialName        = command.IsOfficialName;
            establishmentName.TranslationToLanguage = language;

            audit.NewState = establishmentName.ToJsonAudit();
            _entities.Create(audit);
            _entities.Update(establishmentName);

            if (command.NoCommit)
            {
                return;
            }
            _entities.SaveChanges();
            _eventTrigger.Raise(new EstablishmentChanged());
        }
예제 #6
0
        public void Handle(CreateEstablishment command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var hasOfficialUrl = command.OfficialUrl != null && !string.IsNullOrWhiteSpace(command.OfficialUrl.Value);
            var hasLocation    = command.Location != null;

            var establishmentParent = command.Parent
                                      ?? (command.ParentId.HasValue
                    ? _entities.Get <Establishment>().Single(x => x.RevisionId == command.ParentId)
                    : null);

            // create initial establishment
            var establishment = new Establishment
            {
                Parent       = establishmentParent,
                TypeId       = command.TypeId,
                OfficialName = command.OfficialName.Text,
                WebsiteUrl   = hasOfficialUrl ? command.OfficialUrl.Value : null,
                Location     = new EstablishmentLocation(),
                //CollegeBoardDesignatedIndicator = command.CeebCode,
                //UCosmicCode = command.UCosmicCode,
                //ExternalId = command.ExternalId,
                VerticalRank = command.Rank
            };

            if (command.Principal.IsInRole(RoleName.EstablishmentAdministrator))
            {
                establishment.CollegeBoardDesignatedIndicator = command.CeebCode;
                establishment.UCosmicCode = command.UCosmicCode;
            }
            else
            {
                establishment.IsUnverified = true;
            }

            // log audit
            var audit = new CommandEvent
            {
                RaisedBy = command.Principal.Identity.Name,
                Name     = command.GetType().FullName,
                Value    = JsonConvert.SerializeObject(new
                {
                    command.ParentId,
                    command.TypeId,
                    command.CeebCode,
                    command.UCosmicCode,
                    //command.ExternalId,
                    OfficialName             = command.OfficialName.Text,
                    OfficialNameLanguageCode = command.OfficialName.LanguageCode,
                    OfficialUrl           = hasOfficialUrl ? command.OfficialUrl.Value : null,
                    CenterLatitude        = command.Location != null ? command.Location.CenterLatitude : null,
                    CenterLongitude       = command.Location != null ? command.Location.CenterLongitude : null,
                    BoxNorthEastLatitude  = command.Location != null ? command.Location.BoxNorthEastLatitude : null,
                    BoxNorthEastLongitude = command.Location != null ? command.Location.BoxNorthEastLongitude : null,
                    BoxSouthWestLatitude  = command.Location != null ? command.Location.BoxSouthWestLatitude : null,
                    BoxSouthWestLongitude = command.Location != null ? command.Location.BoxSouthWestLongitude : null,
                    PlaceId = command.Location != null ? command.Location.PlaceId : null,
                }),
                NewState = establishment.ToJsonAudit(),
            };

            _entities.Create(audit);
            _entities.Create(establishment);
            command.Created = establishment;

            // create official name
            command.OfficialName.OwnerId  = null;
            command.OfficialName.Owner    = establishment;
            command.OfficialName.NoCommit = true;
            _createName.Handle(command.OfficialName);

            // create official URL
            if (hasOfficialUrl)
            {
                command.OfficialUrl.OwnerId  = null;
                command.OfficialUrl.Owner    = establishment;
                command.OfficialUrl.NoCommit = true;
                _createUrl.Handle(command.OfficialUrl);
            }

            // update location
            if (hasLocation)
            {
                command.Location.Id       = null;
                command.Location.Entity   = establishment.Location;
                command.Location.NoCommit = true;
                _updateLocation.Handle(command.Location);
            }

            // update hierarchy
            if (!command.NoHierarchy)
            {
                _updateHierarchy.Handle(new UpdateEstablishmentHierarchy(establishment));
            }

            // commit
            if (command.NoCommit)
            {
                return;
            }

            _entities.SaveChanges();
            command.OfficialName.Id = command.OfficialName.CreatedEntity.RevisionId;
            if (hasOfficialUrl)
            {
                command.OfficialUrl.Id = command.OfficialUrl.CreatedEntity.RevisionId;
            }
            _eventTrigger.Raise(new EstablishmentChanged());
        }
예제 #7
0
        public void Handle(UpdateEstablishmentUrl command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // load target
            var establishmentUrl = _entities.Get <EstablishmentUrl>()
                                   .EagerLoad(_entities, new Expression <Func <EstablishmentUrl, object> >[]
            {
                x => x.ForEstablishment.Urls,
            })
                                   .Single(x => x.RevisionId == command.Id);

            // only mutate when state is modified
            if (command.Value == establishmentUrl.Value &&
                command.IsFormerUrl == establishmentUrl.IsFormerUrl &&
                command.IsOfficialUrl == establishmentUrl.IsOfficialUrl)
            {
                return;
            }

            // update previous official URL and owner when changing official URL
            if (!establishmentUrl.IsOfficialUrl && command.IsOfficialUrl)
            {
                var establishment = establishmentUrl.ForEstablishment;
                var officialUrl   = establishment.Urls.Single(x => x.IsOfficialUrl);
                var changeCommand = new UpdateEstablishmentUrl(command.Principal)
                {
                    Id            = officialUrl.RevisionId,
                    Value         = officialUrl.Value,
                    IsFormerUrl   = officialUrl.IsFormerUrl,
                    IsOfficialUrl = false,
                    NoCommit      = true,
                };
                Handle(changeCommand);
                establishment.WebsiteUrl = command.Value;
                _entities.Update(establishment);
            }

            // log audit
            var audit = new CommandEvent
            {
                RaisedBy = command.Principal.Identity.Name,
                Name     = command.GetType().FullName,
                Value    = JsonConvert.SerializeObject(new
                {
                    command.Id,
                    command.Value,
                    command.IsFormerUrl,
                    command.IsOfficialUrl,
                }),
                PreviousState = establishmentUrl.ToJsonAudit(),
            };

            // update scalars
            establishmentUrl.Value         = command.Value;
            establishmentUrl.IsFormerUrl   = command.IsFormerUrl;
            establishmentUrl.IsOfficialUrl = command.IsOfficialUrl;

            audit.NewState = establishmentUrl.ToJsonAudit();
            _entities.Create(audit);
            _entities.Update(establishmentUrl);

            if (!command.NoCommit)
            {
                _unitOfWork.SaveChanges();
                _eventTrigger.Raise(new EstablishmentChanged());
            }
        }
예제 #8
0
        public void Handle(CreateEstablishmentName command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // load owner
            var establishment = command.Owner ?? _entities.Get <Establishment>()
                                .EagerLoad(_entities, new Expression <Func <Establishment, object> >[]
            {
                x => x.Names.Select(y => y.TranslationToLanguage),
            })
                                .Single(x => x.RevisionId == command.OwnerId.Value)
            ;

            // update previous official name and owner when changing official name
            if (command.IsOfficialName)
            {
                var officialName = establishment.Names.SingleOrDefault(x => x.IsOfficialName);
                if (officialName != null)
                {
                    var changeCommand = new UpdateEstablishmentName(command.Principal)
                    {
                        Id             = officialName.RevisionId,
                        Text           = officialName.Text,
                        IsFormerName   = officialName.IsFormerName,
                        IsOfficialName = false,
                        LanguageCode   = (officialName.TranslationToLanguage != null)
                            ? officialName.TranslationToLanguage.TwoLetterIsoCode : null,
                        NoCommit = true,
                    };
                    _updateHandler.Handle(changeCommand);
                }
                establishment.OfficialName = command.Text;
            }

            // get new language
            var language = _entities.Get <Language>()
                           .SingleOrDefault(x => x.TwoLetterIsoCode.Equals(command.LanguageCode, StringComparison.OrdinalIgnoreCase));

            // create new establishment name
            var establishmentName = new EstablishmentName
            {
                Text = command.Text,
                TranslationToLanguage = language,
                IsOfficialName        = command.IsOfficialName,
                IsFormerName          = command.IsFormerName,
                IsContextName         = command.IsContextName,
            };

            establishment.Names.Add(establishmentName);
            establishmentName.ForEstablishment = establishment;

            // log audit
            var audit = new CommandEvent
            {
                RaisedBy = command.Principal.Identity.Name,
                Name     = command.GetType().FullName,
                Value    = JsonConvert.SerializeObject(new
                {
                    OwnerId = command.Owner != null ? command.Owner.RevisionId : command.OwnerId,
                    command.Text,
                    command.IsFormerName,
                    command.IsOfficialName,
                    command.IsContextName,
                    command.LanguageCode,
                }),
                NewState = establishmentName.ToJsonAudit(),
            };

            _entities.Create(audit);
            if (establishment.RevisionId != default(int))
            {
                _entities.Update(establishment);
            }
            command.CreatedEntity = establishmentName;
            if (command.NoCommit)
            {
                return;
            }

            _entities.SaveChanges();
            command.Id = establishmentName.RevisionId;
            _eventTrigger.Raise(new EstablishmentChanged());
        }
        public void Handle(UpdateEstablishmentLocation command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // load target
            var entity = command.Entity ?? _entities.Get <EstablishmentLocation>()
                         .EagerLoad(_entities, new Expression <Func <EstablishmentLocation, object> >[]
            {
                x => x.Places,
            })
                         .Single(x => x.RevisionId == command.Id.Value);

            // load place
            var commandPlaces = new List <Place>();

            if (command.PlaceId.HasValue)
            {
                var commandPlace = _entities.Get <Place>()
                                   .EagerLoad(_entities, new Expression <Func <Place, object> >[]
                {
                    x => x.Ancestors.Select(y => y.Ancestor),
                })
                                   .Single(x => x.RevisionId == command.PlaceId.Value);
                commandPlaces = commandPlace.Ancestors.OrderByDescending(x => x.Separation)
                                .Select(x => x.Ancestor).ToList();
                commandPlaces.Add(commandPlace);
            }

            // only mutate when state is modified
            if (command.CenterLatitude == entity.Center.Latitude &&
                command.CenterLongitude == entity.Center.Longitude &&
                command.BoxNorthEastLatitude == entity.BoundingBox.Northeast.Latitude &&
                command.BoxNorthEastLongitude == entity.BoundingBox.Northeast.Longitude &&
                command.BoxSouthWestLatitude == entity.BoundingBox.Southwest.Latitude &&
                command.BoxSouthWestLongitude == entity.BoundingBox.Southwest.Longitude &&
                commandPlaces.Select(x => x.RevisionId).SequenceEqual(entity.Places.Select(x => x.RevisionId)) &&
                command.GoogleMapZoomLevel == entity.GoogleMapZoomLevel
                )
            {
                return;
            }

            // log audit
            var audit = new CommandEvent
            {
                RaisedBy = command.Principal.Identity.Name,
                Name     = command.GetType().FullName,
                Value    = JsonConvert.SerializeObject(new
                {
                    Id = command.Entity != null ? command.Entity.RevisionId : command.Id,
                    command.CenterLatitude,
                    command.CenterLongitude,
                    command.BoxNorthEastLatitude,
                    command.BoxNorthEastLongitude,
                    command.BoxSouthWestLatitude,
                    command.BoxSouthWestLongitude,
                    command.PlaceId
                }),
                PreviousState = entity.ToJsonAudit(),
            };

            // update scalars
            entity.Center      = new Coordinates(command.CenterLatitude, command.CenterLongitude);
            entity.BoundingBox = new BoundingBox(command.BoxNorthEastLatitude, command.BoxNorthEastLongitude,
                                                 command.BoxSouthWestLatitude, command.BoxSouthWestLongitude);
            entity.GoogleMapZoomLevel = command.GoogleMapZoomLevel;

            // update places
            if (command.PlaceId.HasValue)
            {
                entity.Places = commandPlaces;
            }
            else
            {
                entity.Places = new Collection <Place>();
            }

            audit.NewState = entity.ToJsonAudit();
            _entities.Create(audit);
            if (entity.RevisionId != default(int))
            {
                _entities.Update(entity);
            }

            if (command.NoCommit)
            {
                return;
            }
            _entities.SaveChanges();
            _eventTrigger.Raise(new EstablishmentChanged());
        }