Пример #1
0
        public void Handle(PurgeFile command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var entity = _entities.Get <AgreementFile>().Single(x => x.Id == command.FileId);
            var path   = entity.Path;

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

            _entities.Create(audit);
            _entities.Purge(entity);
            _unitOfWork.SaveChanges();
            _binaryData.Delete(path);
        }
Пример #2
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();
            }
        }
Пример #4
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();
        }
Пример #5
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();
            }
        }
Пример #6
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);
            }
        }
Пример #7
0
        public void Handle(PurgeUpload command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var entity = _entities.Get <Upload>()
                         .SingleOrDefault(x => command.Guid.Equals(x.Guid));

            if (entity == null)
            {
                return;
            }

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

            if (!command.NoCommit)
            {
                _unitOfWork.SaveChanges();
            }
        }
Пример #8
0
        public void Handle(CreateFile command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // create the initial entity
            var entity = new AgreementFile
            {
                AgreementId = command.AgreementId,
                Visibility  = command.Visibility.AsEnum <AgreementVisibility>(),
                Path        = string.Format(AgreementFile.PathFormat, command.AgreementId, Guid.NewGuid()),
            };

            _entities.Create(entity);

            // will we be moving an upload or creating a new file from scratch?
            var upload = command.UploadGuid.HasValue
                ? _entities.Get <Upload>().Single(x => x.Guid.Equals(command.UploadGuid.Value)) : null;

            // populate other entity properties and store binary data
            if (upload != null)
            {
                entity.FileName = upload.FileName;
                entity.Length   = (int)upload.Length;
                entity.MimeType = upload.MimeType;
                entity.Name     = GetExtensionedCustomName(command.CustomName, upload.FileName);
                _binaryData.Move(upload.Path, entity.Path);
                _purgeUpload.Handle(new PurgeUpload(upload.Guid)
                {
                    NoCommit = true
                });
            }
            else
            {
                entity.FileName = command.FileData.FileName;
                entity.Length   = command.FileData.Content.Length;
                entity.MimeType = command.FileData.MimeType;
                entity.Name     = GetExtensionedCustomName(command.CustomName, command.FileData.FileName);
                _binaryData.Put(entity.Path, command.FileData.Content, true);
            }

            // log audit
            var audit = new CommandEvent
            {
                RaisedBy = command.Principal.Identity.Name,
                Name     = command.GetType().FullName,
                Value    = JsonConvert.SerializeObject(new
                {
                    command.AgreementId,
                    command.CustomName,
                    command.Visibility,
                    command.UploadGuid,
                    FileData = command.FileData == null ? null : new
                    {
                        command.FileData.FileName,
                        ContentType   = command.FileData.MimeType,
                        ContentLength = command.FileData.Content.Length,
                    }
                }),
                NewState = entity.ToJsonAudit(),
            };

            _entities.Create(audit);

            try
            {
                _unitOfWork.SaveChanges();
                command.CreatedFileId = entity.Id;
            }
            catch
            {
                // restore binary data state when the db save fails
                if (_binaryData.Exists(entity.Path))
                {
                    if (upload != null)
                    {
                        _binaryData.Move(entity.Path, upload.Path);
                    }
                    else
                    {
                        _binaryData.Delete(entity.Path);
                    }
                }
            }
        }
Пример #9
0
        public void Handle(CreateActivityDocument command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var values = command.ActivityValues;

            if (values == null)
            {
                if (command.Mode.HasValue)
                {
                    var modeText = command.Mode.Value.AsSentenceFragment();
                    values = _entities.Get <ActivityValues>()
                             .Single(x => x.ActivityId == command.ActivityId && x.ModeText == modeText);
                }
                else
                {
                    values = _entities.Get <ActivityValues>()
                             .Single(x => x.ActivityId == command.ActivityId && x.ModeText == x.Activity.ModeText);
                }
            }

            var path = command.Path;

            if (string.IsNullOrWhiteSpace(path))
            {
                path = string.Format(ActivityDocument.PathFormat, values.ActivityId, Guid.NewGuid());
                _binaryData.Put(path, command.Content, true);
            }

            var activityDocument = new ActivityDocument
            {
                ActivityValues     = values,
                Title              = command.Title,
                FileName           = command.FileName,
                MimeType           = command.MimeType,
                Path               = path,
                Length             = command.Length.HasValue ? (int)command.Length.Value : command.Content.Length,
                CreatedByPrincipal = command.Impersonator == null
                    ? command.Principal.Identity.Name
                    : command.Impersonator.Identity.Name,
            };

            if (command.EntityId.HasValue && command.EntityId != Guid.Empty)
            {
                activityDocument.EntityId = command.EntityId.Value;
            }
            values.Documents.Add(activityDocument);

            // do NOT update activity here as it throws concurrency exceptions
            // when users upload multiple documents at once

            _entities.Create(activityDocument);

            if (!command.NoCommit)
            {
                try
                {
                    _entities.SaveChanges();
                    command.CreatedActivityDocument = _detachedEntities.Query <ActivityDocument>()
                                                      .Single(x => x.RevisionId == activityDocument.RevisionId);
                }
                catch
                {
                    _binaryData.Delete(activityDocument.Path);
                    throw;
                }
            }
            else
            {
                command.CreatedActivityDocument = activityDocument;
            }
        }
Пример #10
0
        public void Handle(PurgeActivity command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // load the activity along with its documents & alternate copies
            var activity = _entities.Get <Activity>()
                           .EagerLoad(_entities, new Expression <Func <Activity, object> >[]
            {
                x => x.Values.Select(y => y.Documents),
                x => x.WorkCopy,
                x => x.Original,
            })
                           .SingleOrDefault(x => x.RevisionId == command.ActivityId);

            if (activity == null)
            {
                return;
            }

            // deleting activity will cascade delete documents,
            // so they must be removed from the binary store
            command.DeletedDocuments = new Dictionary <string, byte[]>();
            foreach (var path in activity.Values.SelectMany(x => x.Documents.Select(y => y.Path)))
            {
                if (_binaryData.Exists(path))
                {
                    command.DeletedDocuments.Add(path, _binaryData.Get(path));
                    _binaryData.Delete(path);
                }
            }

            // if this activity is a work copy, also delete the original if it is empty
            PurgeActivity deleteOriginal = null;

            // if a work copy exists, delete it too
            PurgeActivity deleteWorkCopy = null;

            if (activity.Original != null && activity.Original.RevisionId != command.OuterActivityId && activity.Original.IsEmpty())
            {
                deleteOriginal = new PurgeActivity(command.Principal, activity.Original.RevisionId)
                {
                    NoCommit        = true,
                    OuterActivityId = command.ActivityId,
                };
                Handle(deleteOriginal);
            }
            else if (activity.WorkCopy != null && activity.WorkCopy.RevisionId != command.OuterActivityId)
            {
                deleteWorkCopy = new PurgeActivity(command.Principal, activity.WorkCopy.RevisionId)
                {
                    NoCommit        = true,
                    OuterActivityId = command.ActivityId,
                };
                Handle(deleteWorkCopy);
            }

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

            _entities.Create(audit);
            _entities.Purge(activity);

            try
            {
                // wrap removal in try block
                if (!command.NoCommit)
                {
                    _entities.SaveChanges();
                }
            }
            catch
            {
                // restore binary data when savechanges fails
                foreach (var path in command.DeletedDocuments)
                {
                    _binaryData.Put(path.Key, path.Value, true);
                }

                if (deleteOriginal != null)
                {
                    foreach (var path in deleteOriginal.DeletedDocuments)
                    {
                        _binaryData.Put(path.Key, path.Value, true);
                    }
                }

                if (deleteWorkCopy != null)
                {
                    foreach (var path in deleteWorkCopy.DeletedDocuments)
                    {
                        _binaryData.Put(path.Key, path.Value, true);
                    }
                }
            }
        }