Esempio n. 1
0
        private void ClearNodesRecursive(InstitutionalAgreement umbrella)
        {
            // ensure that the offspring and children properties are not null
            umbrella.Offspring = umbrella.Offspring ?? new List <InstitutionalAgreementNode>();
            umbrella.Children  = umbrella.Children ?? new List <InstitutionalAgreement>();

            // delete all of this umbrella's offspring nodes
            while (umbrella.Offspring.FirstOrDefault() != null)
            {
                _entities.Purge(umbrella.Offspring.First());
            }

            // operate recursively over children
            foreach (var child in umbrella.Children)
            {
                // ensure that the child's ancestor nodes are not null
                child.Ancestors = child.Ancestors ?? new List <InstitutionalAgreementNode>();

                // delete each of the child's ancestor nodes
                while (child.Ancestors.FirstOrDefault() != null)
                {
                    _entities.Purge(child.Ancestors.First());
                }

                // run this method again on the child
                ClearNodesRecursive(child);
            }
        }
Esempio n. 2
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();
        }
Esempio n. 3
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();
        }
Esempio n. 4
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();
        }
Esempio n. 5
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();
            }
        }
Esempio n. 7
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());
        }
        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();
        }
Esempio n. 9
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();
        }
Esempio n. 10
0
        public void Handle(PurgeInstitutionalAgreement command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // find agreement
            var agreement = _queryProcessor.Execute(
                new GetMyInstitutionalAgreementByGuidQuery(command.Principal, command.AgreementId));

            if (agreement == null)
            {
                return;
            }

            agreement = _entities.Get <InstitutionalAgreement>().Single(x => x.EntityId == command.AgreementId);

            if (agreement.Files != null && agreement.Files.Any())
            {
                foreach (var file in agreement.Files.Where(x => !string.IsNullOrWhiteSpace(x.Path)))
                {
                    _binaryData.Delete(file.Path);
                }
            }

            _entities.Purge(agreement);
            _unitOfWork.SaveChanges();
        }
        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());
        }
Esempio n. 12
0
        public void Handle(DeleteMyPhoto command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

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

            // if command has filenames, only delete photo if it matches one of them
            if (command.FileNames != null && !command.FileNames.Contains(person.Photo.Name))
            {
                return;
            }

            // only delete if there is a photo present
            var photo = person.Photo;

            if (photo == null)
            {
                return;
            }

            // unlink the photo before deleting
            person.Photo = null;

            // delete the photo from binary storage (if it exists there)
            if (!string.IsNullOrWhiteSpace(photo.Path))
            {
                _binaryData.Delete(photo.Path);
            }

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

            // push to database
            _entities.Update(person);
            _entities.Purge(photo);
            _entities.Create(audit);
            if (!command.NoCommit)
            {
                _unitOfWork.SaveChanges();
            }
        }
Esempio n. 13
0
 private void PurgeCurrentSettings()
 {
     _entities.Get <AgreementSettings>().ToList().ForEach(x =>
     {
         _entities.Purge(x);
         _unitOfWork.SaveChanges();
     });
 }
 private void PurgeCurrentSettings()
 {
     _entities.Get <InstitutionalAgreementConfiguration>().ToList().ForEach(x =>
     {
         _entities.Purge(x);
         _unitOfWork.SaveChanges();
     });
 }
Esempio n. 15
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();
     });
 }
Esempio n. 16
0
        private void ClearNodesRecursive(Establishment parent)
        {
            // delete all of this parent's offspring nodes
            while (parent.Offspring.FirstOrDefault() != null)
            {
                _entities.Purge(parent.Offspring.First());
            }

            // operate recursively over children
            foreach (var child in parent.Children)
            {
                // delete each of the child's ancestor nodes
                while (child.Ancestors.FirstOrDefault() != null)
                {
                    _entities.Purge(child.Ancestors.First());
                }

                // run this method again on the child
                ClearNodesRecursive(child);
            }
        }
Esempio n. 17
0
        public void Handle(DeleteExternalUrl command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var entity = _entities.Get <ExternalUrl>().Single(x => x.Id == command.UrlId);

            _entities.Purge(entity);
            _entities.SaveChanges();
        }
        public void Perform(DeleteRandomUsfActivities job)
        {
            var stopwatch = new Stopwatch();

            var draftText  = ActivityMode.Draft.AsSentenceFragment(); // delete drafts and work copies
            var activities = _entities.Get <Activity>().Where(x => x.ModeText == draftText || x.Original != null);

            foreach (var activity in activities)
            {
                _entities.Purge(activity);
            }
            _entities.SaveChanges();

            activities = _entities.Get <Activity>()
                         .Where(x => x.Person.Affiliations.Any(y => y.IsDefault && y.EstablishmentId == 3306))
            ;

            var random = new Random();

            while (activities.Count() > 250)
            {
                var total      = activities.Count();
                var idToDelete = random.Next(activities.Min(x => x.RevisionId), activities.Max(x => x.RevisionId));
                if (!IdsToKeep.Contains(idToDelete))
                {
                    var activityToDelete = _entities.Get <Activity>().ById(idToDelete);
                    if (activityToDelete != null)
                    {
                        _entities.Purge(activityToDelete);
                        _entities.SaveChanges();
                    }
                }

                if (stopwatch.Elapsed.TotalMinutes > 1)
                {
                    break;
                }
            }
        }
Esempio n. 19
0
        public void Handle(DeleteEmailAddress command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var emailAddress = _entities.Get <EmailAddress>()
                               .Single(x => x.PersonId == command.PersonId && x.Number == command.EmailAddressNumber);

            _entities.Purge(emailAddress);
            _entities.SaveChanges();
        }
Esempio n. 20
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;
        }
Esempio n. 21
0
        public void Handle(PurgeAffiliation 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);

            _entities.Purge(entity);
            _entities.SaveChanges();
        }
Esempio n. 22
0
        public void Handle(PurgeMyActivityCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var activity = _entities.Get <Activity>()
                           .EagerLoad(_entities, new Expression <Func <Activity, object> >[]
            {
                t => t.Tags,
                t => t.DraftedTags,
            })
                           .ByUserNameAndNumber(command.Principal.Identity.Name, command.Number);

            _entities.Purge(activity);
        }
Esempio n. 23
0
        private void DeriveNodes(Place entity)
        {
            entity.Ancestors.ToList().ForEach(node =>
                                              _entities.Purge(node));

            var separation = 1;
            var parent     = entity.Parent;

            while (parent != null)
            {
                entity.Ancestors.Add(new PlaceNode
                {
                    Ancestor   = parent,
                    Separation = separation++,
                });
                parent = parent.Parent;
            }
        }
Esempio n. 24
0
        private void DeriveNodes(GeoNamesToponym toponym)
        {
            toponym.Ancestors.ToList().ForEach(node =>
                                               _entities.Purge(node));

            var separation = 1;
            var parent     = toponym.Parent;

            while (parent != null)
            {
                toponym.Ancestors.Add(new GeoNamesToponymNode
                {
                    Ancestor   = parent,
                    Separation = separation++,
                });
                parent = parent.Parent;
            }
        }
        private void DeriveNodes(GeoPlanetPlace place)
        {
            place.Ancestors.ToList().ForEach(node =>
                                             _entities.Purge(node));

            var separation = 1;
            var parent     = place.Parent;

            while (parent != null)
            {
                place.Ancestors.Add(new GeoPlanetPlaceNode
                {
                    Ancestor   = parent,
                    Separation = separation++,
                });
                parent = parent.Parent;
            }
        }
Esempio n. 26
0
        public void Handle(RevokeRoleFromUserCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var grant = _entities.Get <RoleGrant>().SingleOrDefault(g =>
                                                                    g.Role.EntityId == command.RoleGuid &&
                                                                    g.User.EntityId == command.UserGuid);

            if (grant == null)
            {
                return;
            }
            _entities.Purge(grant);
            command.IsNewlyRevoked = true;
        }
Esempio n. 27
0
        public void Handle(PurgeLooseFileCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var entity = _entities.Get <LooseFile>()
                         .By(command.Guid);

            if (entity == null)
            {
                return;
            }

            _entities.Purge(entity);
            _unitOfWork.SaveChanges();
        }
Esempio n. 28
0
        public void Handle(PurgeAgreement command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // find agreement
            var entity = _queryProcessor.Execute(
                new AgreementById(command.Principal, command.AgreementId));

            if (entity == null)
            {
                return;
            }

            entity = _entities.Get <Agreement>().ById(command.AgreementId);
            var filePaths = entity.Files.Where(x => !string.IsNullOrEmpty(x.Path))
                            .Select(x => x.Path).ToArray();

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

            _entities.Create(audit);

            _entities.Purge(entity);
            _unitOfWork.SaveChanges();

            if (!filePaths.Any())
            {
                return;
            }
            foreach (var filePath in filePaths)
            {
                _binaryData.Delete(filePath);
            }
        }
        public void Handle(RemoveContactFromAgreementCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // todo: this should be FindByPrimaryKey
            var entity = _entities.Get <InstitutionalAgreementContact>()
                         .SingleOrDefault(x =>
                                          x.EntityId == command.ContactGuid
                                          );

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

            command.Place.Ancestors.ToList().ForEach(node =>
                                                     _entities.Purge(node));

            var separation = 1;
            var parent     = command.Place.Parent;

            while (parent != null)
            {
                command.Place.Ancestors.Add(new PlaceNode
                {
                    Ancestor   = parent,
                    Separation = separation++,
                });
                parent = parent.Parent;
            }
        }