This commit is contained in:
524
source/Plugins/AyaNova.PlugIn.ImportExportCSV/Util.cs
Normal file
524
source/Plugins/AyaNova.PlugIn.ImportExportCSV/Util.cs
Normal file
@@ -0,0 +1,524 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using GZTW.AyaNova.BLL;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace AyaNova.PlugIn.ImportExportCSV
|
||||
{
|
||||
public static class Util
|
||||
{
|
||||
public static System.Resources.ResourceManager AyaRes = null;
|
||||
public static bool LicensedMode = false;
|
||||
private static Integration _IECSVIntegrationData = null;
|
||||
private static IECSVData _IEData = null;
|
||||
public static Guid PluginID
|
||||
{
|
||||
get { return new Guid("{46BC31D2-3132-4b43-8789-120604573A08}"); }
|
||||
}
|
||||
|
||||
public static IECSVData IEData
|
||||
{
|
||||
get
|
||||
{
|
||||
#region retrieve / create
|
||||
if (_IECSVIntegrationData == null)
|
||||
{
|
||||
if (Integration.IntegrationExists(PluginID))
|
||||
{
|
||||
_IECSVIntegrationData = Integration.GetItem(PluginID);
|
||||
|
||||
_IEData = new IECSVData();
|
||||
|
||||
if (_IECSVIntegrationData.AIObject == null || _IECSVIntegrationData.AIObject.ToString() == "")
|
||||
{
|
||||
_IECSVIntegrationData.AIObject = _IEData.XMLData;
|
||||
_IECSVIntegrationData = (Integration)_IECSVIntegrationData.Save();
|
||||
}
|
||||
else
|
||||
{
|
||||
//All normal, parse xml and move on
|
||||
_IEData.XMLData = (string)_IECSVIntegrationData.AIObject;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_IECSVIntegrationData = Integration.NewItem(PluginID);
|
||||
_IECSVIntegrationData.Active = true;
|
||||
_IECSVIntegrationData.AppVersion = "6.3+";
|
||||
_IECSVIntegrationData.Name = "AyaNova ImportExportCSV - Plugin integration";
|
||||
_IEData = new IECSVData();
|
||||
_IECSVIntegrationData.AIObject = _IEData.XMLData;
|
||||
_IECSVIntegrationData = (Integration)_IECSVIntegrationData.Save();
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return _IEData;
|
||||
#endregion retrieve create
|
||||
}
|
||||
}
|
||||
|
||||
public static void SaveIntegrationData()
|
||||
{
|
||||
if (!_IEData.IsDirty) return;
|
||||
_IECSVIntegrationData.AIObject = _IEData.XMLData;
|
||||
_IECSVIntegrationData = (Integration)_IECSVIntegrationData.Save();
|
||||
_IEData.IsDirty = false;
|
||||
}
|
||||
|
||||
|
||||
public static void ShowOptionsDialog()
|
||||
{
|
||||
ImportUpdateOptions d = new ImportUpdateOptions();
|
||||
d.ShowDialog();
|
||||
d.Dispose();
|
||||
SaveIntegrationData();
|
||||
}
|
||||
|
||||
#region case 1588 Mapping
|
||||
/// <summary>
|
||||
/// case 1588 map of user choices for what data goes where used for import.
|
||||
/// </summary>
|
||||
/// <param name="grid"></param>
|
||||
/// <returns></returns>
|
||||
public static Dictionary<string, int> BuildGridImportMap(DataGridView grid)
|
||||
{
|
||||
Dictionary<string, int> lsMap = new Dictionary<string, int>(grid.ColumnCount);
|
||||
foreach (DataGridViewColumn cm in grid.Columns)
|
||||
{
|
||||
if (cm.HeaderCell.Value != null && !string.IsNullOrWhiteSpace(cm.HeaderCell.Value.ToString()))
|
||||
lsMap.Add(cm.HeaderCell.Value.ToString(), cm.Index);
|
||||
}
|
||||
|
||||
return lsMap;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the columns to their last used value from database
|
||||
/// </summary>
|
||||
/// <param name="grid"></param>
|
||||
/// <param name="objectType"></param>
|
||||
internal static void LoadGridMappings(DataGridView grid, RootObjectTypes objectType)
|
||||
{
|
||||
int nColumn = 0;
|
||||
//case 1762
|
||||
int GridColumnsCount = grid.Columns.Count;
|
||||
if (GridColumnsCount == 0) return;
|
||||
GridColumnsCount--;//account for zero based index
|
||||
|
||||
string sLastMapping = "";
|
||||
switch (objectType)
|
||||
{
|
||||
case RootObjectTypes.Client:
|
||||
sLastMapping = IEData.ClientLastMapping;
|
||||
break;
|
||||
case RootObjectTypes.Part:
|
||||
sLastMapping = IEData.PartLastMapping;
|
||||
break;
|
||||
case RootObjectTypes.Unit:
|
||||
sLastMapping = IEData.UnitLastMapping;
|
||||
break;
|
||||
case RootObjectTypes.PartAssembly:
|
||||
sLastMapping = IEData.AssemblyLastMapping;
|
||||
break;
|
||||
//case 2072
|
||||
case RootObjectTypes.WorkorderStatus:
|
||||
sLastMapping = IEData.WorkorderStatusLastMapping;
|
||||
break;
|
||||
case RootObjectTypes.WorkorderCategory:
|
||||
sLastMapping = IEData.WorkorderCategoryLastMapping;
|
||||
break;
|
||||
case RootObjectTypes.WorkorderItemType:
|
||||
sLastMapping = IEData.WorkorderItemTypeLastMapping;
|
||||
break;
|
||||
case RootObjectTypes.UnitServiceType:
|
||||
sLastMapping = IEData.UnitServiceTypeLastMapping;
|
||||
break;
|
||||
case RootObjectTypes.Priority:
|
||||
sLastMapping = IEData.PriorityLastMapping;
|
||||
break;
|
||||
case RootObjectTypes.PartCategory:
|
||||
sLastMapping = IEData.PartCategoryLastMapping;
|
||||
break;
|
||||
case RootObjectTypes.Rate:
|
||||
sLastMapping = IEData.RateLastMapping;
|
||||
break;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(sLastMapping))
|
||||
{
|
||||
//grid.SuspendLayout();
|
||||
string[] smap = sLastMapping.Split(',');
|
||||
foreach (string s in smap)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(s))
|
||||
{
|
||||
grid.Columns[nColumn].HeaderCell.Value = s;
|
||||
grid.Columns[nColumn].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
|
||||
}
|
||||
nColumn++;
|
||||
if (nColumn > GridColumnsCount)
|
||||
{
|
||||
// MessageBox.Show("Less columns in source than last import, skipping extra columns");
|
||||
break;
|
||||
}
|
||||
}
|
||||
//grid.PerformLayout();
|
||||
// grid.Invalidate();
|
||||
grid.AutoResizeColumns();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save the last mappings to the database
|
||||
/// </summary>
|
||||
/// <param name="grid"></param>
|
||||
/// <param name="objectType"></param>
|
||||
internal static void SaveGridMappings(DataGridView grid, RootObjectTypes objectType)
|
||||
{
|
||||
|
||||
System.Text.StringBuilder sb = new StringBuilder();
|
||||
foreach (DataGridViewColumn cm in grid.Columns)
|
||||
{
|
||||
if (cm.HeaderCell.Value == null)
|
||||
sb.Append(",");
|
||||
else
|
||||
{
|
||||
sb.Append(cm.HeaderCell.Value.ToString());
|
||||
sb.Append(",");
|
||||
}
|
||||
}
|
||||
|
||||
//removing hanging comma
|
||||
if (sb.Length > 0)
|
||||
sb.Length = sb.Length - 1;
|
||||
|
||||
switch (objectType)
|
||||
{
|
||||
case RootObjectTypes.Client:
|
||||
IEData.ClientLastMapping = sb.ToString();
|
||||
break;
|
||||
case RootObjectTypes.Part:
|
||||
IEData.PartLastMapping = sb.ToString();
|
||||
break;
|
||||
case RootObjectTypes.Unit:
|
||||
IEData.UnitLastMapping = sb.ToString();
|
||||
break;
|
||||
case RootObjectTypes.PartAssembly:
|
||||
IEData.AssemblyLastMapping = sb.ToString();
|
||||
break;
|
||||
//case 2072
|
||||
case RootObjectTypes.WorkorderStatus:
|
||||
IEData.WorkorderStatusLastMapping = sb.ToString();
|
||||
break;
|
||||
case RootObjectTypes.WorkorderCategory:
|
||||
IEData.WorkorderCategoryLastMapping = sb.ToString();
|
||||
break;
|
||||
case RootObjectTypes.WorkorderItemType:
|
||||
IEData.WorkorderItemTypeLastMapping = sb.ToString();
|
||||
break;
|
||||
case RootObjectTypes.UnitServiceType:
|
||||
IEData.UnitServiceTypeLastMapping = sb.ToString();
|
||||
break;
|
||||
case RootObjectTypes.Priority:
|
||||
IEData.PriorityLastMapping = sb.ToString();
|
||||
break;
|
||||
case RootObjectTypes.PartCategory:
|
||||
IEData.PartCategoryLastMapping = sb.ToString();
|
||||
break;
|
||||
case RootObjectTypes.Rate:
|
||||
IEData.RateLastMapping = sb.ToString();
|
||||
break;
|
||||
}
|
||||
SaveIntegrationData();
|
||||
|
||||
}
|
||||
|
||||
|
||||
//abstracts out the common code in all import types
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="grid"></param>
|
||||
/// <param name="dt"></param>
|
||||
/// <param name="obtype"></param>
|
||||
public static void ReadCSV(DataGridView grid, ref System.Data.DataTable dt, RootObjectTypes obtype, string csvPath)
|
||||
{
|
||||
if (dt != null)
|
||||
{
|
||||
dt.Clear();
|
||||
dt = null;
|
||||
}
|
||||
|
||||
|
||||
bool bHasErrors = false;
|
||||
System.Text.StringBuilder sbErrors = new System.Text.StringBuilder();
|
||||
long lRecordsRead = 0;
|
||||
long lRecordsBad = 0;
|
||||
|
||||
Microsoft.VisualBasic.FileIO.TextFieldParser rdr = new Microsoft.VisualBasic.FileIO.TextFieldParser(csvPath);
|
||||
rdr.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
|
||||
rdr.SetDelimiters(",");
|
||||
rdr.TrimWhiteSpace = true;
|
||||
rdr.HasFieldsEnclosedInQuotes = true;
|
||||
|
||||
|
||||
List<string[]> lsRows = new List<string[]>();
|
||||
int nMaxRowFields = 0;
|
||||
|
||||
string[] sCurrentRow;
|
||||
while (!rdr.EndOfData)
|
||||
{
|
||||
try
|
||||
{
|
||||
sCurrentRow = rdr.ReadFields();
|
||||
if (sCurrentRow.Length > nMaxRowFields)
|
||||
nMaxRowFields = sCurrentRow.Length;
|
||||
lsRows.Add(sCurrentRow);
|
||||
|
||||
lRecordsRead++;
|
||||
}
|
||||
catch (Microsoft.VisualBasic.FileIO.MalformedLineException ex)
|
||||
{
|
||||
bHasErrors = true;
|
||||
sbErrors.Append("Line ");
|
||||
sbErrors.Append(ex.Message);
|
||||
sbErrors.Append(" is not valid and will be skipped.\r\n");
|
||||
lRecordsBad++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (bHasErrors)
|
||||
{
|
||||
MessageBox.Show(lRecordsBad.ToString() + " records were malformed and not able to be read:\r\n\r\n" + sbErrors.ToString());
|
||||
}
|
||||
|
||||
|
||||
MessageBox.Show(lRecordsRead.ToString() +
|
||||
" records were successfully read from the .csv file and will now be displayed\r\n" +
|
||||
" for you to set the order of the fields before importing.");
|
||||
|
||||
dt = new System.Data.DataTable("CSVData");
|
||||
for (int x = 0; x < nMaxRowFields; x++)
|
||||
{
|
||||
dt.Columns.Add("Column" + (x + 1).ToString(), typeof(string));
|
||||
}
|
||||
|
||||
foreach (string[] sRow in lsRows)
|
||||
{
|
||||
System.Data.DataRow dr = dt.NewRow();
|
||||
int nField = 0;
|
||||
foreach (string sField in sRow)
|
||||
{
|
||||
dr[nField] = sField;
|
||||
nField++;
|
||||
}
|
||||
dt.Rows.Add(dr);
|
||||
}
|
||||
|
||||
grid.DataSource = dt;
|
||||
|
||||
//Load last used grid mappings
|
||||
Util.LoadGridMappings(grid, obtype);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endregion mapping
|
||||
|
||||
#region case 3606 dupes
|
||||
|
||||
/// <summary>
|
||||
/// checks for all duplicates in AyaNova of relevant objects and
|
||||
/// presents a warning message if found
|
||||
/// </summary>
|
||||
public static void DupeCheck()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
|
||||
{
|
||||
var dupes = ClientPickList.DuplicateNameCheck();
|
||||
if (dupes.Count > 0)
|
||||
{
|
||||
sb.AppendLine("DUPLICATE CLIENT NAMES");
|
||||
sb.AppendLine("======================");
|
||||
sb.AppendLine(String.Join("\r\n", dupes));
|
||||
sb.AppendLine();
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var dupes = VendorPickList.DuplicateNameCheck();
|
||||
if (dupes.Count > 0)
|
||||
{
|
||||
sb.AppendLine("DUPLICATE VENDOR NAMES");
|
||||
sb.AppendLine("======================");
|
||||
sb.AppendLine(String.Join("\r\n", dupes));
|
||||
sb.AppendLine();
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var dupes = UnitPickList.DuplicateSerialCheck();
|
||||
if (dupes.Count > 0)
|
||||
{
|
||||
sb.AppendLine("DUPLICATE UNIT SERIAL NUMBERS");
|
||||
sb.AppendLine("=============================");
|
||||
sb.AppendLine(String.Join("\r\n", dupes));
|
||||
sb.AppendLine();
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var dupes = UnitModelPickList.DuplicateNameCheck();
|
||||
if (dupes.Count > 0)
|
||||
{
|
||||
sb.AppendLine("DUPLICATE UNIT MODEL NAMES");
|
||||
sb.AppendLine("==========================");
|
||||
sb.AppendLine(String.Join("\r\n", dupes));
|
||||
sb.AppendLine();
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var dupes = PartPickList.DuplicatePartNumberCheck();
|
||||
if (dupes.Count > 0)
|
||||
{
|
||||
sb.AppendLine("DUPLICATE PART NUMBERS");
|
||||
sb.AppendLine("======================");
|
||||
sb.AppendLine(String.Join("\r\n", dupes));
|
||||
sb.AppendLine();
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var dupes = PartAssemblies.DuplicateNameCheck();
|
||||
if (dupes.Count > 0)
|
||||
{
|
||||
sb.AppendLine("DUPLICATE PART ASSEMBLY NAMES");
|
||||
sb.AppendLine("=============================");
|
||||
sb.AppendLine(String.Join("\r\n", dupes));
|
||||
sb.AppendLine();
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var dupes = PartCategories.DuplicateNameCheck();
|
||||
if (dupes.Count > 0)
|
||||
{
|
||||
sb.AppendLine("DUPLICATE PART CATEGORY NAMES");
|
||||
sb.AppendLine("=============================");
|
||||
sb.AppendLine(String.Join("\r\n", dupes));
|
||||
sb.AppendLine();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
var dupes = PriorityPickList.DuplicateNameCheck();
|
||||
if (dupes.Count > 0)
|
||||
{
|
||||
sb.AppendLine("DUPLICATE PRIORITY NAMES");
|
||||
sb.AppendLine("========================");
|
||||
sb.AppendLine(String.Join("\r\n", dupes));
|
||||
sb.AppendLine();
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var dupes = RatePickList.DuplicateNameCheck();
|
||||
if (dupes.Count > 0)
|
||||
{
|
||||
sb.AppendLine("DUPLICATE RATE NAMES");
|
||||
sb.AppendLine("====================");
|
||||
sb.AppendLine(String.Join("\r\n", dupes));
|
||||
sb.AppendLine();
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var dupes = UnitServiceTypePickList.DuplicateNameCheck();
|
||||
if (dupes.Count > 0)
|
||||
{
|
||||
sb.AppendLine("DUPLICATE UNIT SERVICE TYPE NAMES");
|
||||
sb.AppendLine("=================================");
|
||||
sb.AppendLine(String.Join("\r\n", dupes));
|
||||
sb.AppendLine();
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var dupes = WorkorderCategories.DuplicateNameCheck();
|
||||
if (dupes.Count > 0)
|
||||
{
|
||||
sb.AppendLine("DUPLICATE WORKORDER CATEGORY NAMES");
|
||||
sb.AppendLine("==================================");
|
||||
sb.AppendLine(String.Join("\r\n", dupes));
|
||||
sb.AppendLine();
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var dupes = WorkorderItemTypePickList.DuplicateNameCheck();
|
||||
if (dupes.Count > 0)
|
||||
{
|
||||
sb.AppendLine("DUPLICATE WORKORDER ITEM TYPE NAMES");
|
||||
sb.AppendLine("===================================");
|
||||
sb.AppendLine(String.Join("\r\n", dupes));
|
||||
sb.AppendLine();
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var dupes = WorkorderStatusPickList.DuplicateNameCheck();
|
||||
if (dupes.Count > 0)
|
||||
{
|
||||
sb.AppendLine("DUPLICATE WORKORDER STATUS NAMES");
|
||||
sb.AppendLine("================================");
|
||||
sb.AppendLine(String.Join("\r\n", dupes));
|
||||
sb.AppendLine();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (sb.Length > 0)
|
||||
{
|
||||
string sDupesWarning =
|
||||
"***** DUPLICATE ITEMS FOUND *****\r\n\r\n" +
|
||||
"The following list of objects in your AyaNova database contain duplicate names.\r\n\r\n" +
|
||||
"In order to protect the integrity of your data, you should either remove or rename\r\n" +
|
||||
"all duplicate named records before using this utility to avoid issues with incorrect matches:\r\n\r\n\r\n";
|
||||
CopyableMessageBox c = new CopyableMessageBox(sDupesWarning + sb.ToString());
|
||||
c.ShowDialog();
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
//case 3606 Dupe checks
|
||||
public static void AddItemWithDupeCheck(string objectType, System.Collections.Hashtable ht, Guid id, string name)
|
||||
{
|
||||
if (!ht.ContainsKey(name))
|
||||
{
|
||||
ht.Add(name, id);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
|
||||
}//EOC
|
||||
}//EONS
|
||||
Reference in New Issue
Block a user