/// <summary> /// Gets the file metadata. /// </summary> /// <param name="macAddress">The mac address.</param> /// <param name="computerName">Name of the computer.</param> /// <param name="fileName">Name of the file.</param> /// <returns></returns> public IFileMetadata GetFileMetadata(string macAddress, string computerName, string fileName) { dynamic metadata; IFileMetadata retval = null; try { using (var context = new MetaFSDB()) { if ((metadata = GetMetadata(context, macAddress, computerName, fileName)) != null) { // Metadata found retval = new FileMetadata() { MetadataId = metadata.RowId, Category = metadata.Category, FileName = metadata.FileName, FullPath = metadata.FilePath, TemplateId = metadata.TemplateId, PcId = metadata.PcId, Metadata = ((string)metadata.Metadata).LoadAsXDocument(), SubCategory = metadata.SubCategory, TemplateBody = ((string)metadata.Template).LoadAsXDocument() }; } else // Metadata needs to be created { retval = CreateMetadata(fileName); } } } catch (Exception ex) { Logger.LogError(ex); } return(retval); }
/// <summary> /// Creates the metadata. /// </summary> /// <param name="fileName">Name of the file.</param> /// <returns></returns> private IFileMetadata CreateMetadata(string fileName) { IFileMetadata retval = null; try { using (var context = new MetaFSDB()) { var metadata = context.TemplateCatalogues.FirstOrDefault(x => x.IsActive.Value); if (metadata != null) { retval = new FileMetadata() { PcId = PcIdentifier, FullPath = fileName, TemplateId = metadata.TemplateId, FileName = Path.GetFileName(fileName), TemplateBody = metadata.TemplateBody.LoadAsXDocument() }; } } } catch (Exception ex) { Logger.LogError(ex); } return(retval); }
/// <summary> /// Creates the record. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t">The t.</param> /// <returns></returns> public bool CreateRecord <T>(T t) { var retval = true; try { using (var context = new MetaFSDB()) { if (t is RegisteredPC) { context.RegisteredPCs.Add(t as RegisteredPC); } else if (t is FileCatalogue) { context.FileCatalogues.Add(t as FileCatalogue); } else if (t is Metadata) { context.Metadatas.Add(t as Metadata); } context.SaveChanges(); } } catch (Exception ex) { retval = false; Logger.LogError(ex); } return(retval); }
/// <summary> /// Preloads the ef. /// </summary> /// <param name="args">The arguments.</param> /// <returns>System.Int32.</returns> private static int PreloadEF(string args) { using (var context = new MetaFSDB()) context.Categories.Any(); return(0); }
/// <summary> /// Initializes the database. /// </summary> private void InitializeDb() { try { using (var ts = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted })) { using (var context = new MetaFSDB()) { // Create basic categories if (!context.Categories.Any()) { context.Categories.AddRange(GetBasicCategories()); } // Create basic object types if (!context.ObjectTypes.Any()) { context.ObjectTypes.AddRange(GetBasicObjectTypes()); } // Register PC SelectedMac = NetworkOps.GetMacAddress(); var registeredPc = context.RegisteredPCs.FirstOrDefault(x => x.PCName.Equals(Environment.MachineName, StringComparison.OrdinalIgnoreCase)); if (registeredPc == null) { registeredPc = new RegisteredPC() { MacAddress = SelectedMac, IsEnabled = true, PCName = Environment.MachineName }; context.RegisteredPCs.Add(registeredPc); } else { registeredPc.MacAddress = SelectedMac; context.RegisteredPCs.Attach(registeredPc); var entry = context.Entry(registeredPc); entry.Property(e => e.MacAddress).IsModified = true; } context.SaveChanges(); PcIdentifier = registeredPc.PCId; MachineName = registeredPc.PCName; } ts.Complete(); } } catch (Exception ex) { Logger.LogError(ex); } }
/// <summary> /// Updates the existing record. /// </summary> /// <param name="currentMetadata">The current metadata.</param> /// <param name="context">The context.</param> /// <param name="metadata">The metadata.</param> private void UpdateExistingRecord(IFileMetadata currentMetadata, MetaFSDB context, XDocument metadata) { var existing = context.Metadatas.FirstOrDefault(x => x.MetaId == currentMetadata.MetadataId); existing.Metadata1 = metadata.ToString(); context.Metadatas.Attach(existing); var entry = context.Entry(existing); entry.Property(e => e.Metadata1).IsModified = true; context.SaveChanges(); }
/// <summary> /// Updates the file metadata. /// </summary> /// <param name="currentMetadata">The current metadata.</param> public void UpdateFileMetadata(IFileMetadata currentMetadata) { var metadata = GetMetadataAsXml(Renderer.RehydrateFromUi(), currentMetadata); try { using (var ts = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted })) { using (var context = new MetaFSDB()) { if (currentMetadata.MetadataId != 0) { UpdateExistingRecord(currentMetadata, context, metadata); } else { var newFileEntry = context.FileCatalogues.Add(new FileCatalogue() { Fk_PCId = PcIdentifier, FileName = currentMetadata.FileName, FullPath = currentMetadata.FullPath, Fk_ObjectType = 1 }); context.SaveChanges(); context.Metadatas.Add(new Metadata() { Fk_RowId = newFileEntry.RowId, Fk_TemplateId = currentMetadata.TemplateId, Metadata1 = metadata.ToString() }); context.SaveChanges(); } } ts.Complete(); } } catch (Exception ex) { Logger.LogError(ex); } }
/// <summary> /// Gets the metadata. /// </summary> /// <param name="context">The context.</param> /// <param name="macAddress">The mac address.</param> /// <param name="computerName">Name of the computer.</param> /// <param name="fileName">Name of the file.</param> /// <returns>System.Object.</returns> private object GetMetadata(MetaFSDB context, string macAddress, string computerName, string fileName) { return((from pcs in context.RegisteredPCs join fileCat in context.FileCatalogues on pcs.PCId equals fileCat.Fk_PCId join meta in context.Metadatas on fileCat.RowId equals meta.Fk_RowId join template in context.TemplateCatalogues on meta.Fk_TemplateId equals template.TemplateId where pcs.MacAddress == macAddress && fileCat.FullPath == fileName select new { PcId = pcs.PCId, RowId = meta.MetaId, Metadata = meta.Metadata1, FileName = fileCat.FileName, FilePath = fileCat.FullPath, Category = fileCat.Category, TemplateId = template.TemplateId, Template = template.TemplateBody, SubCategory = fileCat.SubCategory }).FirstOrDefault()); }