This commit is contained in:
369
source/Plugins/AyaNova.PlugIn.ImportExportCSV/ImportAssembly.cs
Normal file
369
source/Plugins/AyaNova.PlugIn.ImportExportCSV/ImportAssembly.cs
Normal file
@@ -0,0 +1,369 @@
|
||||
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 ImportAssembly : 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 ImportAssembly()
|
||||
{
|
||||
//
|
||||
// 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";
|
||||
//
|
||||
// ImportAssembly
|
||||
//
|
||||
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 = "ImportAssembly";
|
||||
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® assembly 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 dtAssemblies;
|
||||
|
||||
private FHAssembly[] records;
|
||||
private void mnuOpen_Click(object sender, System.EventArgs e)
|
||||
{
|
||||
|
||||
if(dlgOpen.ShowDialog()!=DialogResult.OK) return;
|
||||
this.mnuImport.Enabled=false;
|
||||
if(dtAssemblies==null)
|
||||
{
|
||||
dtAssemblies=new DataTable("CSVAssemblies");
|
||||
dtAssemblies.Columns.Add("Name",typeof(string));
|
||||
|
||||
}
|
||||
else
|
||||
dtAssemblies.Rows.Clear();
|
||||
|
||||
FileHelperEngine engine = new FileHelperEngine(typeof(FHAssembly));
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
records= (FHAssembly[]) 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(FHAssembly c in records)
|
||||
{
|
||||
|
||||
dtAssemblies.Rows.Add(
|
||||
new object[]{c.Name});
|
||||
nMax++;
|
||||
if(nMax>24) break;
|
||||
}
|
||||
|
||||
|
||||
dataGrid1.DataSource=dtAssemblies;
|
||||
this.mnuImport.Enabled=true;
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Import data
|
||||
private PartAssemblies PA = null;
|
||||
private bool ContainsAssembly(string name)
|
||||
{
|
||||
foreach (PartAssembly 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 Assemblies";
|
||||
PA = PartAssemblies.GetItems();
|
||||
foreach(FHAssembly 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!="" && (!ContainsAssembly(t(255,f.Name))))
|
||||
{
|
||||
w.Step=t(255,f.Name);
|
||||
PartAssembly p=PA.Add();
|
||||
p.Name = t(255, f.Name);
|
||||
lGoodCount++;
|
||||
|
||||
}
|
||||
else
|
||||
lBadCount++;
|
||||
|
||||
|
||||
}
|
||||
if (lGoodCount > 0)
|
||||
{
|
||||
w.Step = "Saving...";
|
||||
PA.Save();
|
||||
}
|
||||
|
||||
w.Close();
|
||||
|
||||
if(lBadCount>0)
|
||||
MessageBox.Show(lGoodCount.ToString() + " Assemblies were imported sucessfully.\r\n" +
|
||||
lBadCount.ToString() + " Assemblies were not imported because a assembly with the same name already\r\n" +
|
||||
"exists.");
|
||||
else
|
||||
MessageBox.Show(lGoodCount.ToString() + " Assemblies 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);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Export data
|
||||
/// <summary>
|
||||
/// Exports all existing clients 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 = "assemblies.csv";
|
||||
if (sf.ShowDialog() != DialogResult.OK) return;
|
||||
|
||||
Cursor=Cursors.WaitCursor;
|
||||
FileHelperEngine engine = new FileHelperEngine(typeof(FHAssembly));
|
||||
System.Collections.ArrayList alAssemblies = new ArrayList();
|
||||
FHAssembly fhc;
|
||||
PA = PartAssemblies.GetItems();
|
||||
Waiting w=new Waiting();
|
||||
w.Show();
|
||||
w.Ops="Exporting assemblies";
|
||||
foreach(PartAssembly p in PA)
|
||||
{
|
||||
fhc=new FHAssembly();
|
||||
w.Step=p.Name;
|
||||
fhc.Name=p.Name;
|
||||
alAssemblies.Add(fhc);
|
||||
|
||||
}
|
||||
w.Close();
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
engine.WriteFile(sf.FileName, (object[])alAssemblies.ToArray(typeof(FHAssembly)));
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
MessageBox.Show("Error writing the export file:\r\n\r\n" + ex.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Cursor=Cursors.Default;
|
||||
MessageBox.Show("All Assemblies in current AyaNova database have been exported to file:\r\n" +
|
||||
sf.FileName);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user