Files
2018-06-29 19:47:36 +00:00

413 lines
13 KiB
C#

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using GZTW.AyaNova.BLL;
using System.Threading;
using CSLA.Security;
using FileHelpers;
using System.Collections.Generic;
using System.IO;
namespace AyaNova.PlugIn.ImportExportCSV
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class ImportWorkorderStatus : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGridView dataGrid1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem mnuOpen;
private System.Windows.Forms.MenuItem mnuImport;
private System.Windows.Forms.OpenFileDialog dlgOpen;
private System.Windows.Forms.MenuItem mnuExport;
private MenuItem mnuImportOptions;
private IContainer components;
public ImportWorkorderStatus()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.Icon = Resource.ImportExportCSV16icon;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.dataGrid1 = new System.Windows.Forms.DataGridView();
this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
this.mnuOpen = new System.Windows.Forms.MenuItem();
this.mnuImport = new System.Windows.Forms.MenuItem();
this.mnuExport = new System.Windows.Forms.MenuItem();
this.mnuImportOptions = new System.Windows.Forms.MenuItem();
this.dlgOpen = new System.Windows.Forms.OpenFileDialog();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.ReadOnly = true;
this.dataGrid1.Size = new System.Drawing.Size(632, 433);
this.dataGrid1.TabIndex = 1;
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuOpen,
this.mnuImport,
this.mnuExport,
this.mnuImportOptions});
//
// mnuOpen
//
this.mnuOpen.Index = 0;
this.mnuOpen.Text = "&Open CSV file";
this.mnuOpen.Click += new System.EventHandler(this.mnuOpen_Click);
//
// mnuImport
//
this.mnuImport.Enabled = false;
this.mnuImport.Index = 1;
this.mnuImport.Text = "&Import data";
this.mnuImport.Click += new System.EventHandler(this.mnuImport_Click);
//
// mnuExport
//
this.mnuExport.Index = 2;
this.mnuExport.Text = "&Export data";
this.mnuExport.Click += new System.EventHandler(this.mnuExport_Click);
//
// mnuImportOptions
//
this.mnuImportOptions.Index = 3;
this.mnuImportOptions.Text = "&Duplicate import options";
this.mnuImportOptions.Visible = false;
//
// dlgOpen
//
this.dlgOpen.DefaultExt = "csv";
this.dlgOpen.Filter = "CSV files|*.csv";
this.dlgOpen.Title = "Comma separated values client file";
//
// ImportWorkorderStatus
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.ClientSize = new System.Drawing.Size(632, 433);
this.Controls.Add(this.dataGrid1);
this.Menu = this.mainMenu1;
this.Name = "ImportWorkorderStatus";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
//-----------------------------------------------------------------------------
//
// _________ __
// \_ ___ \ __ __ _______/ |_ ____ _____
// / \ \/ | | \ / ___/\ __\ / _ \ / \
// \ \____| | / \___ \ | | ( <_> )| Y Y \
// \______ /|____/ /____ > |__| \____/ |__|_| /
// \/ \/ \/
// _________ .___
// \_ ___ \ ____ __| _/ ____
// / \ \/ / _ \ / __ | _/ __ \
// \ \____( <_> )/ /_/ | \ ___/
// \______ / \____/ \____ | \___ >
// \/ \/ \/
#region Form load and login
private void Form1_Load(object sender, System.EventArgs e)
{
{
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
string sVersion = "AyaNova® WorkorderStatus import / export " + AyaBizUtils.DisplayVersion(a.GetName().Version);
System.Diagnostics.FileVersionInfo fileVersion = System.Diagnostics.FileVersionInfo.GetVersionInfo(a.Location);
if (fileVersion.FileBuildPart > 0)
sVersion += " (Patch " + fileVersion.FileBuildPart.ToString() + ")";
this.Text = sVersion;
}
MessageBox.Show(
"Confirm that you have a backup copy of your AyaNova database before proceeding.\r\n\r\n" +
"There is no undo function for importing data other than restoring from a pre-import backup copy.\r\n\r\n" +
"** If you are not 100% certain that you have a *working* backup copy ready, stop now **",
"ACCIDENTS HAPPEN: DO NOT IMPORT WITHOUT A GOOD BACKUP!");
}
#endregion
#region Read import file
private DataTable dtItems;
private FHWorkorderStatus[] records;
private void mnuOpen_Click(object sender, System.EventArgs e)
{
if(dlgOpen.ShowDialog()!=DialogResult.OK) return;
this.mnuImport.Enabled=false;
if(dtItems==null)
{
dtItems=new DataTable("CSVAssemblies");
dtItems.Columns.Add("Name",typeof(string));
dtItems.Columns.Add("Description", typeof(string));
dtItems.Columns.Add("ARGB", typeof(string));
}
else
dtItems.Rows.Clear();
FileHelperEngine engine = new FileHelperEngine(typeof(FHWorkorderStatus));
try
{
records= (FHWorkorderStatus[]) engine.ReadFile(dlgOpen.FileName);
}
catch(Exception ex)
{
MessageBox.Show("Error reading the .csv file:\r\n\r\n" + ex.Message);
return;
}
string sMax="The first 25 records only";
if(records.GetLength(0)<25)
sMax="All records";
MessageBox.Show(records.GetLength(0).ToString() + " records were read in from the .csv file.\r\n\r\n" +
sMax+" will be displayed for you to confirm the fields\r\n" +
"and check that the data is displaying correctly before importing.\r\n\r\n" +
"If any field doesn't appear correctly you must create a new .csv file with the correct\r\n" +
"column order before importing or data will be imported into the wrong place in AyaNova");
//Maximum 25 records displayed
int nMax=0;
foreach(FHWorkorderStatus c in records)
{
dtItems.Rows.Add(
new object[]{c.Name, c.Description, c.ARGB});
nMax++;
if(nMax>24) break;
}
dataGrid1.DataSource=dtItems;
this.mnuImport.Enabled=true;
}
#endregion
#region Import data
private WorkorderStatuses PA = null;
private bool ContainsItem(string name)
{
foreach (WorkorderStatus p in PA)
{
if (p.Name == name)
return true;
}
return false;
}
private void mnuImport_Click(object sender, System.EventArgs e)
{
if(records.GetLength(0)<1)
{
MessageBox.Show("There are no records to import");
return;
}
long lBadCount=0;
long lGoodCount=0;
Waiting w=new Waiting();
w.Show();
w.Ops="Importing items";
PA = WorkorderStatuses.GetItems();
foreach(FHWorkorderStatus f in records)
{
//If client has a name and doesn't already exist in the db then import it
if(f.Name!=null && f.Name!="" && (!ContainsItem(t(255,f.Name))))
{
w.Step=t(255,f.Name);
WorkorderStatus p=PA.Add();
p.Active = true;
p.Name = t(255, f.Name);
p.Description = t(255, f.Description);
string sArgb=t(255,f.ARGB);
////try to set ARGB, if it's unparseable or empty then the
////default will take effect based on workorderStatus constructor (-1 white)
//if (!string.IsNullOrEmpty(sArgb))
//{
// int n = 0;
// if (int.TryParse(sArgb, out n))
// {
// p.ARGB = n;
// }
//}
//try to set ARGB in HEX FORMAT, if it's unparseable or empty then the
//default will take effect based on Priority constructor (-1 white)
if (!string.IsNullOrEmpty(sArgb))
{
p.ARGB = argbFromHexString(sArgb);
}
lGoodCount++;
}
else
lBadCount++;
}
if (lGoodCount > 0)
{
w.Step = "Saving...";
PA.Save();
}
w.Close();
if(lBadCount>0)
MessageBox.Show(lGoodCount.ToString() + " items were imported sucessfully.\r\n" +
lBadCount.ToString() + " items were not imported because an item with the same name already exists.");
else
MessageBox.Show(lGoodCount.ToString() + " items were imported sucessfully");
}
private static string t(int nLength, string s)
{
if(s==null || s=="") return "";
if(s.Length<=nLength) return s;
else
return s.Substring(0,nLength);
}
//Note returns white value if anything
//doesn't parse
private static int argbFromHexString(string sHex)
{
if (!string.IsNullOrEmpty(sHex))
{
sHex = sHex.Trim();
//leading hash important for translator
if (!sHex.StartsWith("#"))
sHex = "#" + sHex;
return System.Drawing.ColorTranslator.FromHtml(sHex).ToArgb();
}
//empty return white
return System.Drawing.Color.White.ToArgb();
}
private static string ArgbToHex(int intArgb)
{
return ColorTranslator.ToHtml(Color.FromArgb(intArgb));
}
#endregion
#region Export data
/// <summary>
/// Exports all existing items to CSV format identical
/// to that required for import
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mnuExport_Click(object sender, System.EventArgs e)
{
SaveFileDialog sf = new SaveFileDialog();
sf.DefaultExt = ".csv";
sf.FileName = "WorkorderStatus.csv";
if (sf.ShowDialog() != DialogResult.OK) return;
Cursor=Cursors.WaitCursor;
FileHelperEngine engine = new FileHelperEngine(typeof(FHWorkorderStatus));
System.Collections.ArrayList allItems = new ArrayList();
FHWorkorderStatus fhc;
PA = WorkorderStatuses.GetItems();
Waiting w=new Waiting();
w.Show();
w.Ops="Exporting items";
foreach (WorkorderStatus p in PA)
{
fhc=new FHWorkorderStatus();
w.Step=p.Name;
fhc.Name=p.Name;
fhc.Description = p.Description;
//fhc.ARGB = p.ARGB.ToString();
fhc.ARGB = ArgbToHex(p.ARGB);
allItems.Add(fhc);
}
w.Close();
try
{
engine.WriteFile(sf.FileName, (object[])allItems.ToArray(typeof(FHWorkorderStatus)));
}
catch(Exception ex)
{
MessageBox.Show("Error writing the export file:\r\n\r\n" + ex.Message);
return;
}
Cursor=Cursors.Default;
MessageBox.Show("All Workorder status items in current AyaNova database have been exported to file:\r\n" +
sf.FileName);
}
#endregion
//------------------------------------------------------------------------
}
}