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();
        }
Exemple #2
0
        public void Handle(CopyActivityAndValues command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

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

            // do not do anything if the activity already has a copy
            if (originalActivity.WorkCopy != null)
            {
                command.CreatedActivity = originalActivity.WorkCopy;
                return;
            }

            // copy activity entity
            var copyActivity = new CopyActivity(command.Principal)
            {
                ActivityId = command.ActivityId,
                Mode       = originalActivity.Mode,
                NoCommit   = true,
            };

            _copyActivity.Handle(copyActivity);
            var copiedActivity = copyActivity.CreatedActivity;

            // copy activityvalues entity
            var modeText           = originalActivity.Mode.AsSentenceFragment();
            var originalValues     = originalActivity.Values.First(x => x.ModeText == modeText);
            var copyActivityValues = new CopyActivityValues(command.Principal)
            {
                CopyToActivity   = copiedActivity,
                ActivityValuesId = originalValues.RevisionId,
                Mode             = copiedActivity.Mode,
                NoCommit         = true,
            };

            _copyActivityValues.Handle(copyActivityValues);
            command.CreatedActivity = copiedActivity;

            _entities.SaveChanges();

            // fix image paths now that there is a pk
            _moveActivityDocuments.Handle(new MoveActivityDocuments(command.CreatedActivity.RevisionId));
        }
Exemple #3
0
        public void Handle(ReplaceActivity command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var workCopyActivity = _entities.Get <Activity>()
                                   .EagerLoad(_entities, new Expression <Func <Activity, object> >[]
            {
                x => x.Values,
            })
                                   .ById(command.ActivityWorkCopyId, false);
            var workCopyValues = workCopyActivity.Values.Single(x => x.Mode == workCopyActivity.Mode);

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

            var originalValues = originalActivity.Values.SingleOrDefault(x => x.Mode == command.Mode);

            if (originalValues == null)
            {
                var copyValuesCommand = new CopyActivityValues(command.Principal)
                {
                    ActivityValuesId = workCopyValues.RevisionId,
                    Mode             = command.Mode,
                    CopyToActivity   = originalActivity,
                    NoCommit         = true,
                };
                _copyActivityValues.Handle(copyValuesCommand);
                originalValues = copyValuesCommand.CreatedActivityValues;
            }
            else
            {
                originalValues.UpdatedByPrincipal = command.Principal.Identity.Name;
                originalValues.UpdatedOnUtc       = DateTime.UtcNow;
            }

            originalActivity.Mode = command.Mode;
            originalActivity.UpdatedByPrincipal = workCopyActivity.UpdatedByPrincipal;
            originalActivity.UpdatedOnUtc       = DateTime.UtcNow;
            originalValues.Title               = workCopyValues.Title;
            originalValues.Content             = workCopyValues.Content;
            originalValues.StartsFormat        = workCopyValues.StartsFormat;
            originalValues.EndsFormat          = workCopyValues.EndsFormat;
            originalValues.StartsOn            = workCopyValues.StartsOn;
            originalValues.EndsOn              = workCopyValues.EndsOn;
            originalValues.OnGoing             = workCopyValues.OnGoing;
            originalValues.WasExternallyFunded = workCopyValues.WasExternallyFunded;
            originalValues.WasInternallyFunded = workCopyValues.WasInternallyFunded;

            // delete places
            var placesToDelete = originalValues.Locations
                                 .Where(x => !workCopyValues.Locations.Select(y => y.PlaceId).Contains(x.PlaceId)).ToArray();

            foreach (var placeToDelete in placesToDelete)
            {
                _entities.Purge(placeToDelete);
            }

            // add places
            var placesToAdd = workCopyValues.Locations
                              .Where(x => !originalValues.Locations.Select(y => y.PlaceId).Contains(x.PlaceId)).ToArray();

            foreach (var placeToAdd in placesToAdd)
            {
                placeToAdd.ActivityValues = originalValues;
            }

            // delete types
            var typesToDelete = originalValues.Types
                                .Where(x => !workCopyValues.Types.Select(y => y.TypeId).Contains(x.TypeId)).ToArray();

            foreach (var typeToDelete in typesToDelete)
            {
                _entities.Purge(typeToDelete);
            }

            // add types
            var typesToAdd = workCopyValues.Types
                             .Where(x => !originalValues.Types.Select(y => y.TypeId).Contains(x.TypeId)).ToArray();

            foreach (var typeToAdd in typesToAdd)
            {
                typeToAdd.ActivityValues = originalValues;
            }

            // delete tags
            var tagsToDelete = originalValues.Tags.ToArray();

            foreach (var tagToDelete in tagsToDelete)
            {
                if (!workCopyValues.Tags.Any(x => x.Text == tagToDelete.Text && x.DomainTypeText == tagToDelete.DomainTypeText && x.DomainKey == tagToDelete.DomainKey))
                {
                    _entities.Purge(tagToDelete);
                }
            }

            // add tags
            var tagsToAdd = workCopyValues.Tags.ToArray();

            foreach (var tagToAdd in tagsToAdd)
            {
                if (!originalValues.Tags.Any(x => x.Text == tagToAdd.Text && x.DomainTypeText == tagToAdd.DomainTypeText && x.DomainKey == tagToAdd.DomainKey))
                {
                    tagToAdd.ActivityValues = originalValues;
                }
            }

            // delete documents
            var documentsToDelete = originalValues.Documents
                                    .Where(x => !workCopyValues.Documents.Select(y => y.EntityId).Contains(x.EntityId)).ToArray();

            foreach (var documentToDelete in documentsToDelete)
            {
                _purgeDocument.Handle(new PurgeActivityDocument(command.Principal, documentToDelete.RevisionId)
                {
                    NoCommit = true,
                });
            }

            // add documents
            var documentsToAdd = workCopyValues.Documents
                                 .Where(x => !originalValues.Documents.Select(y => y.EntityId).Contains(x.EntityId)).ToArray();

            foreach (var documentToAdd in documentsToAdd)
            {
                documentToAdd.ActivityValues = originalValues;
            }

            // update documents
            var documentsToUpdate = originalValues.Documents
                                    .Where(x => workCopyValues.Documents.Select(y => y.EntityId).Contains(x.EntityId)).ToArray();

            foreach (var documentToUpdate in documentsToUpdate)
            {
                var documentToSource = workCopyValues.Documents.Single(x => x.EntityId == documentToUpdate.EntityId);
                if (documentToUpdate.Title != documentToSource.Title)
                {
                    documentToUpdate.Title = documentToSource.Title;
                    documentToUpdate.UpdatedByPrincipal = command.Principal.Identity.Name;
                    documentToUpdate.UpdatedOnUtc       = DateTime.UtcNow;
                }
                _purgeDocument.Handle(new PurgeActivityDocument(command.Principal, documentToSource.RevisionId)
                {
                    NoCommit = true,
                });
            }

            _entities.Purge(workCopyActivity);
            _entities.SaveChanges();
            _moveDocuments.Handle(new MoveActivityDocuments(originalActivity.RevisionId));
        }