/// <summary> /// Downloads the configuration database. /// </summary> private void DownloadBase() { transferControl.ThrowIfCancellationRequested(); transferControl.WriteLine(); transferControl.WriteMessage(AdminPhrases.DownloadBase); GetTempFileName(out string tempFileName, out string extractDir); agentClient.DownloadConfig(tempFileName, TopFolder.Base); ExtractArchive(tempFileName, extractDir, false); foreach (IBaseTable baseTable in project.ConfigDatabase.AllTables) { transferControl.ThrowIfCancellationRequested(); transferControl.WriteMessage(string.Format(ExtensionPhrases.ImportTable, baseTable.Name)); string datFileName = Path.Combine(extractDir, "BaseDAT", baseTable.FileNameDat); if (File.Exists(datFileName)) { BaseTableAdapter baseAdapter = new() { FileName = datFileName }; IBaseTable srcTable = BaseTableFactory.GetBaseTable(baseTable); baseAdapter.Fill(srcTable); baseTable.Modified = true; foreach (object item in srcTable.EnumerateItems()) { baseTable.AddObject(item); } } } progressTracker.TaskIndex++; }
/// <summary> /// Exports the configuration database table to the file. /// </summary> public void ExportBaseTable(string destFileName, BaseTableFormat format, IBaseTable baseTable, int startID, int endID) { if (destFileName == null) { throw new ArgumentNullException("destFileName"); } if (baseTable == null) { throw new ArgumentNullException("baseTable"); } IBaseTable destTable; if (0 < startID || endID < int.MaxValue) { // filter data destTable = BaseTableFactory.GetBaseTable(baseTable); if (startID <= endID) { foreach (object item in baseTable.EnumerateItems()) { int itemID = baseTable.GetPkValue(item); if (startID <= itemID && itemID <= endID) { destTable.AddObject(item); } else if (itemID > endID) { break; } } } } else { destTable = baseTable; } switch (format) { case BaseTableFormat.DAT: new BaseAdapter() { FileName = destFileName }.Update(destTable); break; case BaseTableFormat.XML: destTable.Save(destFileName); break; default: // BaseTableFormat.CSV new CsvConverter(destFileName).ConvertToCsv(destTable); break; } }
/// <summary> /// Imports the table. /// </summary> private bool ImportTable(string fileName, IBaseTable baseTable, BaseTableFormat format, int srcStartID, int srcEndID, int destStartID) { if (!File.Exists(fileName)) { ScadaUiUtils.ShowError(CommonPhrases.FileNotFound); return(false); } try { // open source table IBaseTable srcTable = BaseTableFactory.GetBaseTable(baseTable); switch (format) { case BaseTableFormat.DAT: new BaseTableAdapter { FileName = fileName }.Fill(srcTable); break; case BaseTableFormat.XML: srcTable.Load(fileName); break; case BaseTableFormat.CSV: using (StreamReader reader = new(fileName)) { using CsvReader csvReader = new(reader, Locale.Culture); foreach (object record in csvReader.GetRecords(srcTable.ItemType)) { srcTable.AddObject(record); } } break; } // copy data from source table to destination ExtensionUtils.NormalizeIdRange(0, ConfigDatabase.MaxID, ref srcStartID, ref srcEndID, destStartID, out int idOffset); int affectedRows = 0; foreach (object item in srcTable.EnumerateItems()) { int itemID = srcTable.GetPkValue(item); if (itemID < srcStartID) { continue; } else if (itemID > srcEndID) { break; } else { if (idOffset != 0) { srcTable.SetPkValue(item, itemID + idOffset); } baseTable.AddObject(item); affectedRows++; } } if (affectedRows > 0) { baseTable.Modified = true; } ScadaUiUtils.ShowInfo(string.Format(ExtensionPhrases.ImportTableCompleted, affectedRows)); return(true); } catch (Exception ex) { log.HandleError(ex, ExtensionPhrases.ImportTableError); return(false); } }
/// <summary> /// Exports the configuration database table to the file. /// </summary> public void ImportBaseTable(string srcFileName, BaseTableFormat format, IBaseTable baseTable, int srcStartID, int srcEndID, int destStartID, out int affectedRows) { if (srcFileName == null) { throw new ArgumentNullException("destFileName"); } if (baseTable == null) { throw new ArgumentNullException("baseTable"); } affectedRows = 0; if (srcStartID > srcEndID) { return; } // open the source table IBaseTable srcTable = BaseTableFactory.GetBaseTable(baseTable); switch (format) { case BaseTableFormat.DAT: new BaseAdapter() { FileName = srcFileName }.Fill(srcTable, true); break; case BaseTableFormat.XML: srcTable.Load(srcFileName); break; default: // BaseTableFormat.CSV throw new ScadaException("Format is not supported."); } // copy data from the source table to the destination int shiftID = destStartID - srcStartID; foreach (object item in srcTable.EnumerateItems()) { int itemID = srcTable.GetPkValue(item); if (srcStartID <= itemID && itemID <= srcEndID) { if (shiftID == 0) { baseTable.AddObject(item); affectedRows++; } else { int newItemID = itemID + shiftID; if (1 <= newItemID && newItemID <= AdminUtils.MaxCnlNum) { srcTable.SetPkValue(item, newItemID); baseTable.AddObject(item); affectedRows++; } } } else if (itemID > srcEndID) { break; } } if (affectedRows > 0) { baseTable.Modified = true; } }
/// <summary> /// Exports the table. /// </summary> private bool ExportTable(string fileName, IBaseTable baseTable, BaseTableFormat format, int startID, int endID) { try { // filter table IBaseTable filteredTable; if (0 < startID || endID < ConfigDatabase.MaxID) { filteredTable = BaseTableFactory.GetBaseTable(baseTable); if (startID <= endID) { foreach (object item in baseTable.EnumerateItems()) { int itemID = baseTable.GetPkValue(item); if (startID <= itemID && itemID <= endID) { filteredTable.AddObject(item); } else if (itemID > endID) { break; } } } } else { filteredTable = baseTable; } // save table switch (format) { case BaseTableFormat.DAT: new BaseTableAdapter { FileName = fileName }.Update(filteredTable); break; case BaseTableFormat.XML: filteredTable.Save(fileName); break; case BaseTableFormat.CSV: using (StreamWriter writer = new(fileName)) { using CsvWriter csvWriter = new(writer, Locale.Culture); csvWriter.WriteRecords(filteredTable.EnumerateItems()); } break; } return(true); } catch (Exception ex) { log.HandleError(ex, ExtensionPhrases.ExportTableError); return(false); } }