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

            var role = command.Role ??
                       _entities.Get <Role>()
                       .EagerLoad(_entities, new Expression <Func <Role, object> >[]
            {
                r => r.Grants,
            })
                       .By(command.RoleGuid);

            var grant = role.Grants.ByUser(command.UserGuid);

            if (grant != null)
            {
                return;
            }

            var user = _entities.Get <User>().By(command.UserGuid);

            grant = new RoleGrant
            {
                Role = role,
                User = user,
            };
            _entities.Create(grant);
            command.IsNewlyGranted = true;
        }
예제 #2
0
        public void Handle(UpdateAffiliation command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var entity = !command.PersonId.HasValue
                ? _entities.Get <Affiliation>().Single(x => x.EstablishmentId == command.EstablishmentId &&
                                                       x.Person.User != null &&
                                                       x.Person.User.Name.Equals(command.Principal.Identity.Name, StringComparison.OrdinalIgnoreCase))
                : _entities.Get <Affiliation>().Single(x => x.EstablishmentId == command.EstablishmentId &&
                                                       x.PersonId == command.PersonId.Value);

            entity.JobTitles     = command.JobTitles;
            entity.FacultyRankId = command.FacultyRankId;

            // can update the establishment as long as it is not the default
            if (!entity.IsDefault && command.NewEstablishmentId.HasValue && command.NewEstablishmentId.Value != command.EstablishmentId)
            {
                entity.EstablishmentId = command.NewEstablishmentId.Value;
            }

            _entities.SaveChanges();
        }
예제 #3
0
        public void Handle(SendCreatePasswordMessageCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // get the establishment
            var establishment = _entities.Get <Establishment>()
                                .EagerLoad(_entities, new Expression <Func <Establishment, object> >[]
            {
                e => e.Type.Category,
            })
                                .ByEmail(command.EmailAddress);

            // get the person
            var person = _entities.Get <Person>()
                         .EagerLoad(_entities, new Expression <Func <Person, object> >[]
            {
                p => p.Emails,
                p => p.Affiliations,
            })
                         .ByEmail(command.EmailAddress);

            // create the person if they don't yet exist
            if (person == null)
            {
                var createPersonHandler = new CreatePersonHandler(_entities);
                var createPersonCommand = new CreatePersonCommand
                {
                    DisplayName = command.EmailAddress,
                };
                createPersonHandler.Handle(createPersonCommand);
                person = createPersonCommand.CreatedPerson;
            }

            // affiliate person with establishment
            person.AffiliateWith(establishment);

            // add email address if necessary
            if (person.GetEmail(command.EmailAddress) == null)
            {
                person.AddEmail(command.EmailAddress);
            }

            // save changes so nested command can find the correct data
            _unitOfWork.SaveChanges();

            // send the message
            var sendCommand = new SendConfirmEmailMessageCommand
            {
                EmailAddress = command.EmailAddress,
                Intent       = EmailConfirmationIntent.CreatePassword,
                SendFromUrl  = command.SendFromUrl,
            };

            _sendHandler.Handle(sendCommand);
            command.ConfirmationToken = sendCommand.ConfirmationToken;
        }
예제 #4
0
        public void Handle(UpdateAgreement command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var entity = _entities.Get <Agreement>().Single(x => x.Id == command.AgreementId);

            // umbrella entity reference is needed for domain to update the hierarchy nodes
            var umbrella = command.UmbrellaId.HasValue
                ? _entities.Get <Agreement>().Single(x => x.Id == command.UmbrellaId.Value) : null;

            // log audit
            var audit = new CommandEvent
            {
                RaisedBy = command.Principal.Identity.Name,
                Name     = command.GetType().FullName,
                Value    = JsonConvert.SerializeObject(new
                {
                    command.AgreementId,
                    command.UmbrellaId,
                    command.Type,
                    command.Name,
                    command.Content,
                    command.Notes,
                    command.StartsOn,
                    command.ExpiresOn,
                    command.IsExpirationEstimated,
                    command.IsAutoRenew,
                    command.Status,
                    command.Visibility,
                }),
                PreviousState = entity.ToJsonAudit(),
            };

            entity.Umbrella              = umbrella;
            entity.UmbrellaId            = umbrella != null ? umbrella.Id : (int?)null;
            entity.Type                  = command.Type;
            entity.Name                  = command.Name;
            entity.Content               = command.Content;
            entity.Notes                 = command.Notes;
            entity.StartsOn              = command.StartsOn;
            entity.ExpiresOn             = command.ExpiresOn;
            entity.IsExpirationEstimated = command.IsExpirationEstimated;
            entity.IsAutoRenew           = command.IsAutoRenew;
            entity.Status                = command.Status;
            entity.Visibility            = command.Visibility.AsEnum <AgreementVisibility>();
            entity.UpdatedByPrincipal    = command.Principal.Identity.Name;
            entity.UpdatedOnUtc          = DateTime.UtcNow;

            audit.NewState = entity.ToJsonAudit();
            _entities.Create(audit);
            _entities.Update(entity);
            _hierarchyHandler.Handle(new UpdateAgreementHierarchy(entity));
            _unitOfWork.SaveChanges();
        }
예제 #5
0
        public void Handle(UpdatePlace command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var entity = _entities.Get <Place>().Single(x => x.RevisionId == command.PlaceId);

            //var audit = new CommandEvent
            //{
            //    RaisedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name,
            //    Name = command.GetType().FullName,
            //    Value = JsonConvert.SerializeObject(new
            //    {
            //        command.PlaceId,
            //        command.IsRegion,
            //    }),
            //    PreviousState = place.ToJsonAudit(),
            //};

            var lastParentId = entity.ParentId; // do not change parent when command.ParentId is zero

            if (command.ParentId.HasValue && command.ParentId.Value != 0)
            {
                var parent = _entities.Get <Place>().Single(x => x.RevisionId == command.ParentId.Value);
                entity.Parent   = parent;
                entity.ParentId = parent.RevisionId;
            }
            else if (!command.ParentId.HasValue)
            {
                entity.Parent   = null;
                entity.ParentId = null;
            }

            if (command.IsRegion.HasValue)
            {
                entity.IsRegion = command.IsRegion.Value;
            }
            if (command.IsWater.HasValue)
            {
                entity.IsWater = command.IsWater.Value;
            }

            //audit.NewState = place.ToJsonAudit();
            //_entities.Create(audit);

            if (lastParentId != entity.ParentId)
            {
                _hierarchy.Handle(new EnsurePlaceHierarchy(entity));
            }

            _entities.SaveChanges();
        }
예제 #6
0
 private void PurgeCurrentAgreements()
 {
     _entities.Get <Agreement>().ToList().ForEach(a =>
     {
         _entities.Get <Agreement>().ToList().ForEach(agreement =>
                                                      agreement.Offspring.ToList().ForEach(_entities.Purge)
                                                      );
         _entities.Purge(a);
         _unitOfWork.SaveChanges();
     });
 }
예제 #7
0
        public void Handle(AddParticipantToAgreementCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var agreement = command.Agreement ??
                            _entities.Get <InstitutionalAgreement>()
                            .EagerLoad(_entities, new Expression <Func <InstitutionalAgreement, object> >[]
            {
                r => r.Participants,
            })
                            .By(command.AgreementGuid);

            var participant = agreement.Participants.SingleOrDefault(
                x => x.Establishment.EntityId == command.EstablishmentGuid);

            if (participant != null)
            {
                return;
            }

            var establishment = _entities.Get <Establishment>()
                                .EagerLoad(_entities, new Expression <Func <Establishment, object> >[]
            {
                e => e.Affiliates.Select(a => a.Person.User),
                e => e.Names.Select(n => n.TranslationToLanguage),
                e => e.Ancestors.Select(h => h.Ancestor.Affiliates.Select(a => a.Person.User)),
                e => e.Ancestors.Select(h => h.Ancestor.Names.Select(n => n.TranslationToLanguage))
            })
                                .By(command.EstablishmentGuid);


            participant = new InstitutionalAgreementParticipant
            {
                Establishment = establishment,
                Agreement     = agreement,
            };

            // derive ownership (todo, this should be a separate query)
            Expression <Func <Affiliation, bool> > principalDefaultAffiliation = affiliation =>
                                                                                 affiliation.IsDefault &&
                                                                                 affiliation.Person.User != null &&
                                                                                 affiliation.Person.User.Name.Equals(command.Principal.Identity.Name, StringComparison.OrdinalIgnoreCase);

            participant.IsOwner = establishment.Affiliates.AsQueryable().Any(principalDefaultAffiliation) ||
                                  establishment.Ancestors.Any(n => n.Ancestor.Affiliates.AsQueryable().Any(principalDefaultAffiliation));

            _entities.Create(participant);
            command.IsNewlyAdded = true;
        }
예제 #8
0
        public void Handle(UpdateMyPreference command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var preferences = _entities.Get <Preference>();

            if (command.HasPrincipal)
            {
                preferences = preferences.ByPrincipal(command.Principal);
            }
            else if (command.IsAnonymous)
            {
                preferences = preferences.ByAnonymousId(command.AnonymousId);
            }

            var preference = preferences
                             .ByCategory(command.Category)
                             .ByKey(command.Key)
                             .SingleOrDefault();

            if (preference == null)
            {
                if (command.HasPrincipal)
                {
                    preference = new Preference(_entities.Get <User>().ByPrincipal(command.Principal));
                }

                else if (command.IsAnonymous)
                {
                    preference = new Preference(command.AnonymousId);
                }

                else
                {
                    return;
                }

                preference.Category = command.Category.ToString();
                preference.Key      = command.Key.ToString();
                preference.Value    = command.Value;
                _entities.Create(preference);
            }
            else
            {
                preference.Value = command.Value;
                _entities.Update(preference);
            }
        }
예제 #9
0
        public void Handle(AttachFileToAgreementCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var agreement = command.Agreement ??
                            _entities.Get <InstitutionalAgreement>()
                            .EagerLoad(_entities, new Expression <Func <InstitutionalAgreement, object> >[]
            {
                r => r.Files,
            })
                            .By(command.AgreementGuid);

            var file = agreement.Files.SingleOrDefault(g => g.EntityId == command.FileGuid);

            if (file != null)
            {
                return;
            }

            var looseFile = _entities.Get <LooseFile>().By(command.FileGuid);

            if (looseFile == null)
            {
                return;
            }

            // also store in binary data
            var path = string.Format(InstitutionalAgreementFile.PathFormat, agreement.RevisionId, Guid.NewGuid());

            _binaryData.Put(path, looseFile.Content);

            file = new InstitutionalAgreementFile
            {
                Agreement = agreement,
                //Content = looseFile.Content,
                Length   = looseFile.Length,
                MimeType = looseFile.MimeType,
                Name     = looseFile.Name,
                Path     = path,
            };

            _entities.Create(file);
            _entities.Purge(looseFile);
            command.IsNewlyAttached = true;
        }
        public void Handle(UpdateLanguageExpertise command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

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

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

            /* Update fields */
            target.LanguageId           = command.LanguageId;
            target.Other                = command.Other;
            target.Dialect              = command.Dialect;
            target.SpeakingProficiency  = command.SpeakingProficiency;
            target.ListeningProficiency = command.ListeningProficiency;
            target.ReadingProficiency   = command.ReadingProficiency;
            target.WritingProficiency   = command.WritingProficiency;
            target.UpdatedOnUtc         = command.UpdatedOn.ToUniversalTime();
            target.UpdatedByPrincipal   = command.Principal.Identity.Name;

            _entities.Update(target);

            if (!command.NoCommit)
            {
                _unitOfWork.SaveChanges();
            }
        }
예제 #11
0
        public void Handle(UpdateActivity command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            if (command.OnGoing.HasValue && command.OnGoing.Value)
            {
                command.EndsOn = null;
            }

            var activity = _entities.Get <Activity>()
                           .EagerLoad(_entities, new Expression <Func <Activity, object> >[]
            {
                x => x.Values,
            })
                           .ById(command.ActivityId, false);

            if (command.Mode.HasValue && command.Mode.Value != activity.Mode)
            {
                activity.Mode = command.Mode.Value;
            }
            activity.UpdatedByPrincipal = command.Impersonator == null
                ? command.Principal.Identity.Name
                : command.Impersonator.Identity.Name;
            activity.UpdatedOnUtc = DateTime.UtcNow;

            var values = activity.Values.SingleOrDefault(x => x.Mode == activity.Mode);

            if (values == null)
            {
                var copyValuesCommand = new CopyActivityValues(command.Principal)
                {
                    ActivityValuesId = activity.Values.Single(x => x.Mode != activity.Mode).RevisionId,
                    Mode             = activity.Mode,
                    CopyToActivity   = activity,
                    NoCommit         = true,
                };
                _copyActivityValues.Handle(copyValuesCommand);
                values = copyValuesCommand.CreatedActivityValues;
            }
            else
            {
                values.UpdatedByPrincipal = command.Principal.Identity.Name;
                values.UpdatedOnUtc       = DateTime.UtcNow;
            }

            values.Title               = command.Title;
            values.Content             = command.Content;
            values.StartsOn            = command.StartsOn;
            values.EndsOn              = command.EndsOn;
            values.StartsFormat        = command.StartsOn.HasValue ? command.StartsFormat ?? "M/d/yyyy" : null;
            values.EndsFormat          = command.EndsOn.HasValue ? command.EndsFormat ?? "M/d/yyyy" : null;
            values.OnGoing             = command.OnGoing;
            values.WasExternallyFunded = command.WasExternallyFunded;
            values.WasInternallyFunded = command.WasInternallyFunded;

            _entities.SaveChanges();
        }
예제 #12
0
        public void Handle(DetachFileFromAgreementCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // todo: this should be FindByPrimaryKey
            var entity = _entities.Get <InstitutionalAgreementFile>()
                         .SingleOrDefault(x =>
                                          x.EntityId == command.FileGuid &&
                                          x.Agreement.EntityId == command.AgreementGuid
                                          );

            if (entity == null)
            {
                return;
            }

            if (!string.IsNullOrWhiteSpace(entity.Path))
            {
                _binaryData.Delete(entity.Path);
            }

            _entities.Purge(entity);
            command.IsNewlyDetached = true;
        }
        public void Handle(PurgeActivityDocument command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var entity = _entities.Get <ActivityDocument>()
                         .EagerLoad(_entities, new Expression <Func <ActivityDocument, object> >[]
            {
                x => x.ActivityValues.Activity,
            })
                         .SingleOrDefault(x => x.RevisionId == command.DocumentId)
            ;

            if (entity == null)
            {
                return;                 // delete idempotently
            }
            entity.ActivityValues.Activity.UpdatedOnUtc       = DateTime.UtcNow;
            entity.ActivityValues.Activity.UpdatedByPrincipal = command.Impersonator == null
                    ? command.Principal.Identity.Name
                    : command.Impersonator.Identity.Name;

            _entities.Purge(entity);
            _binaryData.Delete(entity.Path);

            if (!command.NoCommit)
            {
                _entities.SaveChanges();
            }
        }
예제 #14
0
        public void Handle(CreateMyNewActivityCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            //var person = _queryProcessor.Execute(
            //    new GetMyPersonQuery(command.Principal));
            var person = _entities.Get <Person>()
                         .ByUserName(command.Principal.Identity.Name);

            var otherActivities = _entities.Query <Activity>()
                                  .WithPersonId(person.RevisionId)
            ;

            var activity = new Activity
            {
                Person   = person,
                PersonId = person.RevisionId,
                Number   = otherActivities.NextNumber(),
            };

            _entities.Create(activity);
            command.CreatedActivity = activity;
        }
예제 #15
0
        public void Handle(CreateActivity command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var person = _entities.Get <Person>()
                         .EagerLoad(_entities, new Expression <Func <Person, object> >[]
            {
                x => x.User,
            })
                         .ByUserName(command.Principal.Identity.Name, false);

            var activity = new Activity
            {
                Person             = person,
                PersonId           = person.RevisionId,
                Mode               = command.Mode,
                CreatedByPrincipal = command.Principal.Identity.Name,
                CreatedOnUtc       = DateTime.UtcNow
            };

            _entities.Create(activity);

            command.CreatedActivity = activity;

            if (!command.NoCommit)
            {
                _entities.SaveChanges();
            }
        }
예제 #16
0
        public void Handle(CreateAffiliationCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // get the person
            var person = _entities.Get <Person>()
                         .EagerLoad(_entities, new Expression <Func <Person, object> >[]
            {
                p => p.Affiliations,
            })
                         .By(command.PersonId);

            // construct the affiliation
            var affiliation = new Affiliation
            {
                EstablishmentId    = command.EstablishmentId,
                IsClaimingStudent  = command.IsClaimingStudent,
                IsClaimingEmployee = command.IsClaimingEmployee,
                IsDefault          = !person.Affiliations.Any(a => a.IsDefault),
            };

            person.Affiliations.Add(affiliation);

            // store
            _entities.Create(affiliation);
        }
예제 #17
0
        public void Handle(CopyActivity command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var createActivity = new CreateActivity(command.Principal)
            {
                Mode     = command.Mode,
                NoCommit = true,
            };

            _createActivity.Handle(createActivity);
            command.CreatedActivity = createActivity.CreatedActivity;

            var originalActivity = _entities.Get <Activity>().ById(command.ActivityId, false);

            command.CreatedActivity.Original = originalActivity;
            originalActivity.WorkCopy        = command.CreatedActivity;

            if (!command.NoCommit)
            {
                _entities.SaveChanges();
            }
        }
예제 #18
0
        public void Handle(CreatePasswordCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // get the confirmation
            var confirmation = _entities.Get <EmailConfirmation>()
                               .EagerLoad(_entities, new Expression <Func <EmailConfirmation, object> >[]
            {
                c => c.EmailAddress.Person.User.EduPersonScopedAffiliations,
                c => c.EmailAddress.Person.User.SubjectNameIdentifiers,
            })
                               .ByToken(command.Token);

            // set up user accounts
            var person = confirmation.EmailAddress.Person;

            person.User                     = person.User ?? new User();
            person.User.Name                = person.User.Name ?? confirmation.EmailAddress.Value;
            person.User.IsRegistered        = true;
            person.User.EduPersonTargetedId = null;
            person.User.EduPersonScopedAffiliations.Clear();
            person.User.SubjectNameIdentifiers.Clear();

            confirmation.RetiredOnUtc = DateTime.UtcNow;
            confirmation.SecretCode   = null;
            confirmation.Ticket       = null;
            _entities.Update(confirmation);

            _passwords.Create(confirmation.EmailAddress.Person.User.Name, command.Password);
        }
예제 #19
0
        public void Handle(DeleteUser command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var user = _entities.Get <User>()
                       .EagerLoad(_entities, new Expression <Func <User, object> >[]
            {
                x => x.Person.Affiliations,
            })
                       .Single(a => a.RevisionId == command.Id);

            // when deleting a user, we also want to disassociate their person from tenancy
            var person = user.Person;

            if (person != null)
            {
                person.IsActive = false;
                if (person.DefaultAffiliation != null)
                {
                    person.DefaultAffiliation.IsDefault = false;
                }
            }

            _entities.Purge(user);
            _unitOfWork.SaveChanges();
        }
        public Preference[] Handle(MyPreferencesByCategory query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            if (!query.HasPrincipal && !query.IsAnonymous)
            {
                return(new Preference[0]);
            }

            var results = _entities.Get <Preference>()
                          .EagerLoad(_entities, query.EagerLoad)
            ;

            if (query.HasPrincipal)
            {
                results = results.ByPrincipal(query.Principal);
            }
            else if (query.IsAnonymous)
            {
                results = results.ByAnonymousId(query.AnonymousId);
            }

            results = results
                      .ByCategory(query.Category)
                      .OrderBy(query.OrderBy)
            ;

            return(results.ToArray());
        }
예제 #21
0
        public void Handle(RevokeRoleFromUser command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var grant = _entities.Get <RoleGrant>().SingleOrDefault(g =>
                                                                    g.Role.RevisionId == command.RoleId &&
                                                                    g.User.RevisionId == command.UserId);

            if (grant == null)
            {
                return;
            }

            // log audit
            var audit = new CommandEvent
            {
                RaisedBy = command.Principal.Identity.Name,
                Name     = command.GetType().FullName,
                Value    = JsonConvert.SerializeObject(new
                {
                    command.UserId,
                    command.RoleId,
                }),
                PreviousState = grant.ToJsonAudit(),
            };

            _entities.Create(audit);
            _entities.Purge(grant);
            _unitOfWork.SaveChanges();
        }
예제 #22
0
        public void Handle(RedeemEmailConfirmation command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // get the confirmation
            var confirmation = _entities.Get <EmailConfirmation>()
                               .EagerLoad(_entities, new Expression <Func <EmailConfirmation, object> >[]
            {
                c => c.EmailAddress
            })
                               .ByToken(command.Token);

            // redeem
            if (!confirmation.RedeemedOnUtc.HasValue)
            {
                confirmation.EmailAddress.IsConfirmed = true;
                confirmation.RedeemedOnUtc            = DateTime.UtcNow;
                confirmation.Ticket = HandleGenerateRandomSecretQuery.Handle(
                    new GenerateRandomSecret(256));
                _entities.Update(confirmation);
            }

            command.Ticket = confirmation.Ticket;
        }
        public void Handle(DeleteInternationalAffiliationLocation command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var activity = _entities.Get <InternationalAffiliationLocation>().SingleOrDefault(x => x.RevisionId == command.Id);

            if (activity == null)
            {
                return;
            }

            _entities.Purge(activity);

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

            // TBD
            // log audit
            //var audit = new CommandEvent
            //{
            //    RaisedBy = User.Name,
            //    Name = command.GetType().FullName,
            //    Value = JsonConvert.SerializeObject(new { command.Id }),
            //    PreviousState = activityDocument.ToJsonAudit(),
            //};
            //_entities.Create(audit);

            //_eventProcessor.Raise(new EstablishmentChanged());
        }
예제 #24
0
        public void Handle(PurgeContactPhone command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var entity = _entities.Get <AgreementContactPhone>()
                         .Single(x => x.Id == command.PhoneId && x.OwnerId == command.ContactId && x.Owner.AgreementId == command.AgreementId);

            // log audit
            var audit = new CommandEvent
            {
                RaisedBy = command.Principal.Identity.Name,
                Name     = command.GetType().FullName,
                Value    = JsonConvert.SerializeObject(new
                {
                    command.AgreementId,
                    command.ContactId,
                    command.PhoneId,
                }),
                PreviousState = entity.ToJsonAudit(),
            };

            _entities.Create(audit);
            _entities.Purge(entity);
            _unitOfWork.SaveChanges();
        }
예제 #25
0
        public void Handle(UpdateEstablishmentHierarchiesCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // get all root-level establishments with children
            var establishments = _entities.Get <Establishment>()
                                 .EagerLoad(_entities, new Expression <Func <Establishment, object> >[]
            {
                e => e.Offspring.Select(o => o.Ancestor.Parent),
                e => e.Offspring.Select(o => o.Offspring.Parent),
                e => e.Offspring.Select(o => o.Ancestor.Children),
                e => e.Offspring.Select(o => o.Offspring.Children),
                e => e.Children.Select(c => c.Children.Select(g => g.Children)),
                e => e.Children.Select(c => c.Ancestors.Select(a => a.Ancestor))
            })
                                 .IsRoot()
                                 .WithAnyChildren()
                                 .ToArray()
            ;

            // derive nodes for each parent
            foreach (var parent in establishments)
            {
                _hierarchyHandler.Handle(new UpdateEstablishmentHierarchyCommand(parent));
            }
        }
예제 #26
0
        public void Handle(UpdateMyEmailValueCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            command.ChangedState = false;

            // get the email address
            var email = _entities.Get <EmailAddress>()
                        .ByUserNameAndNumber(command.Principal.Identity.Name, command.Number);

            // only process matching email
            if (email == null)
            {
                return;
            }

            // only update the value if it was respelled
            if (email.Value == command.NewValue)
            {
                return;
            }

            email.Value = command.NewValue;
            _entities.Update(email);
            command.ChangedState = true;
        }
예제 #27
0
        public void Handle(UpdateActivityDocument command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var entity = _entities.Get <ActivityDocument>()
                         .EagerLoad(_entities, new Expression <Func <ActivityDocument, object> >[]
            {
                x => x.ActivityValues.Activity,
            })
                         .Single(x => x.RevisionId == command.DocumentId);

            if (entity.Title == command.Title)
            {
                return;
            }

            entity.Title              = command.Title;
            entity.UpdatedOnUtc       = DateTime.UtcNow;
            entity.UpdatedByPrincipal = command.Impersonator == null
                    ? command.Principal.Identity.Name
                    : command.Impersonator.Identity.Name;
            entity.ActivityValues.Activity.UpdatedOnUtc       = entity.UpdatedOnUtc;
            entity.ActivityValues.Activity.UpdatedByPrincipal = entity.UpdatedByPrincipal;

            _entities.SaveChanges();
        }
예제 #28
0
        public void Handle(CreateActivityType command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var activity = _entities.Get <Activity>()
                           .EagerLoad(_entities, new Expression <Func <Activity, object> >[]
            {
                x => x.Values.Select(y => y.Types),
            })
                           .ById(command.ActivityId, false);
            var values = activity.Values.Single(x => x.Mode == activity.Mode);

            values.Types.Add(new ActivityType
            {
                TypeId = command.ActivityTypeId,
            });
            activity.UpdatedOnUtc       = DateTime.UtcNow;
            activity.UpdatedByPrincipal = command.Impersonator == null
                ? command.Principal.Identity.Name
                : command.Impersonator.Identity.Name;

            _entities.SaveChanges();
        }
예제 #29
0
        public void Handle(PurgeActivityPlace command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var activity = _entities.Get <Activity>()
                           .EagerLoad(_entities, new Expression <Func <Activity, object> >[]
            {
                x => x.Values.Select(y => y.Locations),
            })
                           .ById(command.ActivityId, false);
            var values = activity.Values.Single(x => x.Mode == activity.Mode);

            if (values.Locations.All(x => x.PlaceId != command.PlaceId))
            {
                return;
            }

            var locations = values.Locations.Where(x => x.PlaceId == command.PlaceId).ToArray();

            foreach (var location in locations)
            {
                _entities.Purge(location);
            }

            activity.UpdatedOnUtc       = DateTime.UtcNow;
            activity.UpdatedByPrincipal = command.Impersonator == null
                ? command.Principal.Identity.Name
                : command.Impersonator.Identity.Name;
            _entities.SaveChanges();
        }
예제 #30
0
        public void Handle(PurgeActivityTag command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var activity = _entities.Get <Activity>()
                           .EagerLoad(_entities, new Expression <Func <Activity, object> >[]
            {
                x => x.Values.Select(y => y.Tags),
            })
                           .ById(command.ActivityId, false);
            var values = activity.Values.Single(x => x.Mode == activity.Mode);

            if (values.Tags.All(x => !x.Text.Equals(command.ActivityTagText, StringComparison.OrdinalIgnoreCase)))
            {
                return;
            }

            var tags = values.Tags.Where(x => x.Text.Equals(command.ActivityTagText, StringComparison.OrdinalIgnoreCase)).ToArray();

            foreach (var tag in tags)
            {
                _entities.Purge(tag);
            }

            activity.UpdatedOnUtc       = DateTime.UtcNow;
            activity.UpdatedByPrincipal = command.Impersonator == null
                ? command.Principal.Identity.Name
                : command.Impersonator.Identity.Name;
            _entities.SaveChanges();
        }
예제 #31
0
        internal static PlaceName ToEntity(this GeoNamesAlternateName geoNamesAlternateName, ICommandEntities entities)
        {
            if (geoNamesAlternateName == null) return null;

            var placeName = Mapper.Map<PlaceName>(geoNamesAlternateName);

            if (!string.IsNullOrWhiteSpace(placeName.TranslationToHint))
            {
                placeName.TranslationToLanguage = entities.Get<Language>().ByIsoCode(placeName.TranslationToHint);
            }

            return placeName;
        }
예제 #32
0
        //internal static PlaceName ToEntity(this GeoNamesAlternateName geoNamesAlternateName, LanguageFinder languages)
        //{
        //    if (geoNamesAlternateName == null) return null;

        //    var placeName = Mapper.Map<PlaceName>(geoNamesAlternateName);

        //    if (!string.IsNullOrWhiteSpace(placeName.TranslationToHint))
        //    {
        //        placeName.TranslationToLanguage = languages.FindOne(LanguageBy.IsoCode(placeName.TranslationToHint)
        //            .ForInsertOrUpdate());
        //    }

        //    return placeName;
        //}

        internal static PlaceName ToEntity(this GeoNamesAlternateName geoNamesAlternateName, ICommandEntities entities)
        {
            if (geoNamesAlternateName == null) return null;

            var placeName = Mapper.Map<PlaceName>(geoNamesAlternateName);

            if (!string.IsNullOrWhiteSpace(placeName.TranslationToHint))
            {
                //placeName.TranslationToLanguage = queryProcessor.Execute(
                //    new GetLanguageByIsoCodeQuery
                //    {
                //        IsoCode = placeName.TranslationToHint,
                //    }
                //);
                placeName.TranslationToLanguage = entities.Get<Language>().ByIsoCode(placeName.TranslationToHint);
            }

            return placeName;
        }