protected EmployeeModuleSettings Seed(CreateEmployeeModuleSettings command) { // make sure entity does not already exist var employeeModuleSettings = _queryProcessor.Execute( new EmployeeModuleSettingsByEstablishmentId(command.EstablishmentId, true)); if (employeeModuleSettings != null) { return(employeeModuleSettings); } var iconsBinaryPath = string.Format("{0}/{1}/", EmployeeConsts.SettingsBinaryStoreBasePath, EmployeeConsts.IconsBinaryStorePath); foreach (var icon in UsfEmployeeModuleSettingsSeeder.UsfEmployeeModuleSettingIcons) { var iconPath = Path.Combine(iconsBinaryPath, icon.Value.Third.ToString()); if (!_binaryStore.Exists(iconPath)) { var filePath = string.Format("{0}{1}{2}", AppDomain.CurrentDomain.BaseDirectory, @"..\UCosmic.Infrastructure\SeedData\SeedMediaFiles\", icon.Key); using (var fileStream = File.OpenRead(filePath)) _binaryStore.Put(iconPath, fileStream.ReadFully()); } if (string.IsNullOrEmpty(command.GlobalViewIconPath) && icon.Value.First == "Global") { command.GlobalViewIconFileName = icon.Value.Third.ToString(); command.GlobalViewIconLength = _binaryStore.Get(iconPath).Length; command.GlobalViewIconMimeType = "image/png"; command.GlobalViewIconName = icon.Value.Second; command.GlobalViewIconPath = iconsBinaryPath; } if (string.IsNullOrEmpty(command.FindAnExpertIconPath) && icon.Value.First == "Expert") { command.FindAnExpertIconFileName = icon.Value.Third.ToString(); command.FindAnExpertIconLength = _binaryStore.Get(iconPath).Length; command.FindAnExpertIconMimeType = "image/svg+xml"; command.FindAnExpertIconName = icon.Value.Second; command.FindAnExpertIconPath = iconsBinaryPath; } } _createEmployeeModuleSettings.Handle(command); _unitOfWork.SaveChanges(); return(command.CreatedEmployeeModuleSettings); }
public void Seed() { var externalFiles = _entities.Query <ExternalFile>().ToArray(); foreach (var externalFile in externalFiles.Where(x => !_binaryData.Exists(x.Path))) { CopyFile(externalFile.Name, externalFile.Path); } var agreementFiles = _entities.Query <AgreementFile>().ToArray(); foreach (var agreementFile in agreementFiles.Where(x => !_binaryData.Exists(x.Path))) { CopyFile(agreementFile.FileName, agreementFile.Path); } var activityDocuments = _entities.Query <ActivityDocument>().ToArray(); foreach (var activityDocument in activityDocuments.Where(x => !_binaryData.Exists(x.Path))) { CopyFile(activityDocument.FileName, activityDocument.Path); } var employeeModuleSettings = _entities.Query <EmployeeModuleSettings>() .ToArray(); foreach (var employeeModuleSetting in employeeModuleSettings) { var globalIconPath = string.Format("{0}{1}", employeeModuleSetting.GlobalViewIconPath, employeeModuleSetting.GlobalViewIconFileName); if (!string.IsNullOrWhiteSpace(globalIconPath) && !_binaryData.Exists(globalIconPath)) { var localPath = UsfEmployeeModuleSettingsSeeder.UsfEmployeeModuleSettingIcons .Single(x => x.Value.Second == employeeModuleSetting.GlobalViewIconName); CopyFile(localPath.Key, globalIconPath); } var expertIconPath = string.Format("{0}{1}", employeeModuleSetting.FindAnExpertIconPath, employeeModuleSetting.FindAnExpertIconFileName); if (!string.IsNullOrWhiteSpace(expertIconPath) && !_binaryData.Exists(expertIconPath)) { var localPath = UsfEmployeeModuleSettingsSeeder.UsfEmployeeModuleSettingIcons .Single(x => x.Value.Second == employeeModuleSetting.FindAnExpertIconName); CopyFile(localPath.Key, expertIconPath); } foreach (var activityType in employeeModuleSetting.ActivityTypes) { var iconPath = string.Format("{0}{1}", activityType.IconPath, activityType.IconFileName); if (!string.IsNullOrWhiteSpace(iconPath) && !_binaryData.Exists(iconPath)) { var localPath = UsfEmployeeModuleSettingsSeeder.UsfActivityTypeIcons .Single(x => x.Value.Second == activityType.IconName); CopyFile(localPath.Key, iconPath); } } } }
public void Handle(MoveActivityDocuments command) { if (command == null) { throw new ArgumentNullException("command"); } var queryable = _entities.Get <ActivityDocument>() .EagerLoad(_entities, new Expression <Func <ActivityDocument, object> >[] { x => x.ActivityValues, }) ; if (command.ActivityId.HasValue) { queryable = queryable.Where(x => x.ActivityValues.ActivityId == command.ActivityId); } var entities = queryable.ToArray(); foreach (var entity in entities) { if (entity.Path.StartsWith(string.Format(ActivityDocument.PathFormat, entity.ActivityValues.ActivityId, ""))) { continue; } var oldPath = entity.Path; var newPath = string.Format(ActivityDocument.PathFormat, entity.ActivityValues.ActivityId, Guid.NewGuid()); try { _binaryData.Move(oldPath, newPath); entity.Path = newPath; _entities.SaveChanges(); } catch { if (_binaryData.Exists(newPath)) { _binaryData.Move(newPath, oldPath); } } } }
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); } } } }
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); } } } }