public void ImportSetParts() { NotifyBeginStep("Importing set parts"); string tmpDownloadDir = null; try { //if (!string.IsNullOrEmpty(InventoriesCsvFile) && !File.Exists(InventoriesCsvFile)) // InventoriesCsvFile = string.Empty; //if (!string.IsNullOrEmpty(InventoryPartsCsvFile) && !File.Exists(InventoryPartsCsvFile)) // InventoryPartsCsvFile = string.Empty; //if (string.IsNullOrEmpty(InventoriesCsvFile) || string.IsNullOrEmpty(InventoryPartsCsvFile)) //{ // NotifyIndefiniteProgress("Downloading rebrickable csv files..."); // tmpDownloadDir = FileHelper.GetTempDirectory(); // Directory.CreateDirectory(tmpDownloadDir); // DownloadRebrickableCsvFiles(RbDatabaseFile.Inventories | RbDatabaseFile.InventoryParts, tmpDownloadDir); // InventoriesCsvFile = GetDatabaseFileName(RbDatabaseFile.Inventories, tmpDownloadDir); // InventoryPartsCsvFile = GetDatabaseFileName(RbDatabaseFile.InventoryParts, tmpDownloadDir); //} //if (!File.Exists(InventoriesCsvFile) || !File.Exists(InventoryPartsCsvFile)) // return; var inventoryCsv = GetRebrickableCsvFile(RbDatabaseFile.Inventories); inventoryCsv.Rows[0].IsHeader = true; var partsCsv = GetRebrickableCsvFile(RbDatabaseFile.InventoryParts); partsCsv.Rows[0].IsHeader = true; var existingSetIDs = new List <string>(); using (var db = new PaletteDbContext(Connection)) existingSetIDs.AddRange(db.RbSets.Select(x => x.SetID)); var invIdToSetNum = new Dictionary <string, string>(); var setNumToSetId = new Dictionary <string, string>(); foreach (var row in inventoryCsv.Rows.Where(x => !x.IsHeader)) { if (existingSetIDs.Contains(row[2]) && !setNumToSetId.ContainsKey(row[2])) { setNumToSetId.Add(row[2], row[0]); invIdToSetNum.Add(row[0], row[2]); } } using (var trans = Connection.BeginTransaction()) using (var cmd = Connection.CreateCommand()) { DbHelper.InitializeInsertCommand <RbSetPart>(cmd, x => new { x.SetID, x.PartID, //x.ElementID, x.ColorID, x.Quantity, x.IsSpare }); var validRows = partsCsv.Rows.Where(x => !x.IsHeader && invIdToSetNum.ContainsKey(x[0])).ToList(); int totalToProcess = validRows.Count; int totalProcessed = 0; foreach (var partRow in validRows) { string invID = partRow[0]; if (!invIdToSetNum.TryGetValue(invID, out string setNum)) { continue; } int?colorID = null; if (int.TryParse(partRow[2], out int tmp)) { colorID = tmp; } if (!int.TryParse(partRow[3], out int quantity)) { continue; } DbHelper.InsertWithParameters(cmd, setNum, partRow[1], colorID, quantity, partRow[4].Trim() != "f"); ReportProgress(++totalProcessed, totalToProcess); } trans.Commit(); } } finally { if (!string.IsNullOrEmpty(tmpDownloadDir)) { Task.Factory.StartNew(() => FileHelper.DeleteFileOrFolder(tmpDownloadDir, true, true)); } } }
public void ImportLddElements() { NotifyIndefiniteProgress("Loading LDD palette file..."); var lddPaletteFile = GetLddPaletteFile(Environment); if (lddPaletteFile == null) { return; } var lddPalette = lddPaletteFile.Palettes.First(); var duplicatedElements = lddPalette.Items.GroupBy(x => x.ElementID).Where(g => g.Count() > 1).Select(g => g.Key).ToList(); var manualLddElements = new List <LddElement>(); using (var dbContext = new PaletteDbContext(Connection)) { manualLddElements.AddRange( dbContext.LddElements.Where(x => x.Flag != 1) ); } var addedElements = new HashSet <string>(); using (var trans = Connection.BeginTransaction()) using (var cmd = Connection.CreateCommand()) { cmd.CommandText = $"DELETE FROM {DbHelper.GetTableName<ElementDecoration>()}"; cmd.ExecuteNonQuery(); cmd.CommandText = $"DELETE FROM {DbHelper.GetTableName<ElementMaterial>()}"; cmd.ExecuteNonQuery(); cmd.CommandText = $"DELETE FROM {DbHelper.GetTableName<ElementPart>()}"; cmd.ExecuteNonQuery(); cmd.CommandText = $"DELETE FROM {DbHelper.GetTableName<LddElement>()}"; cmd.ExecuteNonQuery(); cmd.CommandText = $"DELETE FROM sqlite_sequence WHERE name='{DbHelper.GetTableName<LddElement>()}'"; cmd.ExecuteNonQuery(); cmd.CommandText = $"DELETE FROM sqlite_sequence WHERE name='{DbHelper.GetTableName<ElementPart>()}'"; cmd.ExecuteNonQuery(); cmd.CommandText = $"DELETE FROM sqlite_sequence WHERE name='{DbHelper.GetTableName<ElementMaterial>()}'"; cmd.ExecuteNonQuery(); cmd.CommandText = $"DELETE FROM sqlite_sequence WHERE name='{DbHelper.GetTableName<ElementDecoration>()}'"; cmd.ExecuteNonQuery(); trans.Commit(); } int totalToProcess = lddPalette.Items.Count + manualLddElements.Count; NotifyTaskStart("Importing LDD elements...", totalToProcess); using (var trans = Connection.BeginTransaction()) using (var elemCmd = Connection.CreateCommand()) using (var partCmd = Connection.CreateCommand()) using (var decCmd = Connection.CreateCommand()) using (var matCmd = Connection.CreateCommand()) using (var rowidCmd = Connection.CreateCommand()) { DbHelper.InitializeInsertCommand <LddElement>(elemCmd, x => new { x.ElementID, x.DesignID, x.IsAssembly, x.Flag }); //elemCmd.Parameters[$"${nameof(LddElement.Flag)}"].Value = 1; DbHelper.InitializeInsertCommand <ElementPart>(partCmd, x => new { x.ElementID, x.PartID, x.MaterialID }); DbHelper.InitializeInsertCommand <ElementDecoration>(decCmd, x => new { x.ElementPartID, x.SurfaceID, x.DecorationID }); DbHelper.InitializeInsertCommand <ElementMaterial>(matCmd, x => new { x.ElementPartID, x.SurfaceID, x.MaterialID }); rowidCmd.CommandText = "select last_insert_rowid()"; int totalProcessed = 0; foreach (var item in lddPalette.Items) { if (IsCancellationRequested) { break; } if (string.IsNullOrEmpty(item.ElementID) || duplicatedElements.Contains(item.ElementID)) { ReportProgress(++totalProcessed, totalToProcess); continue; } bool isAssembly = item is LDDModder.LDD.Palettes.Assembly; DbHelper.InsertWithParameters(elemCmd, item.ElementID, item.DesignID.ToString(), isAssembly, 1); addedElements.Add(item.ElementID); var partConfigs = new List <ElementPart>(); if (item is LDDModder.LDD.Palettes.Assembly assy) { foreach (var part in assy.Parts) { partConfigs.Add(AssemblyPartToConfig(part)); } } else if (item is LDDModder.LDD.Palettes.Brick brick) { partConfigs.Add(PaletteBrickToConfig(brick)); } foreach (var part in partConfigs) { int partID = DbHelper.InsertWithParameters(partCmd, rowidCmd, item.ElementID, part.PartID, part.MaterialID); foreach (var dec in part.Decorations) { DbHelper.InsertWithParameters(decCmd, partID, dec.SurfaceID, dec.DecorationID); } foreach (var mat in part.SubMaterials) { DbHelper.InsertWithParameters(matCmd, partID, mat.SurfaceID, mat.MaterialID); } } ReportProgress(++totalProcessed, totalToProcess); } foreach (var oldElem in manualLddElements) { if (addedElements.Contains(oldElem.ElementID)) { continue; } DbHelper.InsertWithParameters(elemCmd, oldElem.ElementID, oldElem.DesignID, oldElem.IsAssembly, oldElem.Flag); foreach (var part in oldElem.Parts) { int partID = DbHelper.InsertWithParameters(partCmd, rowidCmd, oldElem.ElementID, part.PartID, part.MaterialID); foreach (var dec in part.Decorations) { DbHelper.InsertWithParameters(decCmd, partID, dec.SurfaceID, dec.DecorationID); } foreach (var mat in part.SubMaterials) { DbHelper.InsertWithParameters(matCmd, partID, mat.SurfaceID, mat.MaterialID); } } ReportProgress(++totalProcessed, totalToProcess); } trans.Commit(); } }