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 description for Form1. /// public class ImportClient : System.Windows.Forms.Form { private System.Windows.Forms.DataGridView DataGridView1; 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 ImportClient() { // // Required for Windows Form Designer support // InitializeComponent(); this.Icon = Resource.ImportExportCSV16icon; } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.DataGridView1 = 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.DataGridView1)).BeginInit(); this.SuspendLayout(); // // DataGridView1 // this.DataGridView1.Dock = System.Windows.Forms.DockStyle.Fill; this.DataGridView1.Location = new System.Drawing.Point(0, 0); this.DataGridView1.Name = "DataGridView1"; this.DataGridView1.ReadOnly = true; this.DataGridView1.Size = new System.Drawing.Size(632, 433); this.DataGridView1.TabIndex = 1; this.DataGridView1.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(this.DataGridView1_DataBindingComplete); this.DataGridView1.BindingContextChanged += new System.EventHandler(this.DataGridView1_BindingContextChanged); // // 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; this.mnuImportOptions.Click += new System.EventHandler(this.mnuImportOptions_Click); // // dlgOpen // this.dlgOpen.DefaultExt = "csv"; this.dlgOpen.Filter = "CSV files|*.csv"; this.dlgOpen.Title = "Comma separated values client file"; // // ImportClient // this.AutoScaleBaseSize = new System.Drawing.Size(6, 15); this.ClientSize = new System.Drawing.Size(632, 433); this.Controls.Add(this.DataGridView1); this.Menu = this.mainMenu1; this.Name = "ImportClient"; this.Text = "Client importer v2.0.0.0"; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.DataGridView1)).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ź client 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; } mnuImportOptions.Visible = Util.LicensedMode; 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 dtClients; //private FHClient[] records; private void mnuOpen_Click(object sender, System.EventArgs e) { if (dlgOpen.ShowDialog() != DialogResult.OK) return; this.mnuImport.Enabled = false; Util.ReadCSV(DataGridView1, ref dtClients, RootObjectTypes.Client, dlgOpen.FileName); this.mnuImport.Enabled = true; } #endregion #region Import data Dictionary lsMap = null; private void mnuImport_Click(object sender, System.EventArgs e) { //Save grid settings Util.SaveGridMappings(DataGridView1, RootObjectTypes.Client); if(dtClients.Rows.Count<1) { MessageBox.Show("There are no records to import"); return; } //Get the map lsMap = Util.BuildGridImportMap(DataGridView1); if (!lsMap.ContainsKey("Name")) { MessageBox.Show("\"Name\" is a required field to uniquely identify Clients. Select a column to be used as the unique Client name to proceed with import."); return; } long lBadCount=0; long lGoodCount=0; long lUpdatedCount = 0; //case 1737 cache ClientGroups cgroups = ClientGroups.GetItems(); Dictionary regions = new Dictionary(); DispatchZones dzones = DispatchZones.GetItems(false); RegionList rl = RegionList.GetList(""); foreach (RegionList.RegionListInfo i in rl) regions.Add(i.LT_Region_Label_Name.Display, i.LT_Region_Label_Name.Value); Waiting w = new Waiting(); w.Show(); w.Ops = "Importing clients"; foreach (DataRow dr in dtClients.Rows) { if (fld(dr,"Name",255)=="") continue; Client c = null; string sClientName = fld(dr, "Name", 255); Guid gClientID = Client.IDFromName(sClientName); if (gClientID!=Guid.Empty) { //case 1404 Open existing for update? if (Util.LicensedMode && Util.IEData.ClientUpdate) { //update existing client c = Client.GetItemNoMRU(gClientID); } else//not licensed or nothing chosen to update { lBadCount++; continue; } } else { c = Client.NewItem(); } w.Step = sClientName; if(c.IsNew) c.Name = sClientName; if (c.IsNew || Util.IEData.ClientPostalAddress) { c.MailToAddress.DeliveryAddress = fld(dr, "MailAddress", 255);// t(255, f.MailAddress); c.MailToAddress.City = fld(dr, "MailCity", 255); //t(255, f.MailCity); c.MailToAddress.StateProv = fld(dr, "MailStateProvince", 255); //t(255, f.MailStateProvince); c.MailToAddress.Country = fld(dr, "MailCountry", 255); //t(255, f.MailCountry); c.MailToAddress.Postal = fld(dr, "MailPostal", 255); //t(255, f.MailPostal); } if (c.IsNew || Util.IEData.ClientPhysicalAddress) { c.GoToAddress.DeliveryAddress = fld(dr, "DeliveryAddress", 255);// t(255, f.DeliveryAddress); c.GoToAddress.City = fld(dr, "DeliveryCity", 255); //t(255, f.DeliveryCity); c.GoToAddress.StateProv = fld(dr, "DeliveryStateProvince", 255); //t(255, f.DeliveryStateProvince); c.GoToAddress.Country = fld(dr, "DeliveryCountry", 255); //t(255, f.DeliveryCountry); c.GoToAddress.Postal = fld(dr, "DeliveryPostal", 255); //t(255, f.DeliveryPostal); } if (c.IsNew || Util.IEData.ClientContact) { c.Contact = fld(dr, "Contact", 500);// t(500, f.Contact); } if (c.IsNew || Util.IEData.ClientContactNotes) { c.ContactNotes = fld(dr, "ContactNotes", 1024); //t(1024, f.ContactNotes); } if (c.IsNew || Util.IEData.ClientPhoneNumbers) { c.Phone1 = fld(dr, "Phone1", 255); //t(255, f.Phone1); c.Phone2 = fld(dr, "Phone2", 255); //t(255, f.Phone2); c.Phone3 = fld(dr, "Phone3", 255); //t(255, f.Phone3); c.Phone4 = fld(dr, "Phone4", 255); //t(255, f.Phone4); c.Phone5 = fld(dr, "Phone5", 255); //t(255, f.Phone5); } if (c.IsNew || Util.IEData.ClientEmail) { c.Email = fld(dr, "EmailAddress", 255); //t(255, f.EmailAddress); } if (c.IsNew || Util.IEData.ClientAccountNumber) { c.AccountNumber = fld(dr, "AccountNumber", 255); //t(255, f.AccountNumber); } if (c.IsNew || Util.IEData.ClientWebSite) { c.WebAddress = fld(dr, "WebSite", 255); //t(255, f.WebSite); } if (c.IsNew || Util.IEData.ClientTechNotes) { c.TechNotes = fld(dr, "TechNotes", 32767); //t(32767, f.TechNotes); } if (c.IsNew || Util.IEData.ClientNotes) { c.Notes = fld(dr, "GeneralNotes", 32767); //t(32767, f.GeneralNotes); } if (c.IsNew || Util.IEData.ClientPopupNotes) { c.PopUpNotes = fld(dr, "PopupNotes", 32767); //t(32767, f.PopupNotes); } //client group, region, dispatch zone if (c.IsNew || Util.IEData.ClientGroup) { Guid ClientGroupID=Guid.Empty; string s = fld(dr, "ClientGroup", 255).Trim(); if (!string.IsNullOrWhiteSpace(s)) { foreach (ClientGroup cg in cgroups) { if (cg.Name.Equals(s, StringComparison.CurrentCultureIgnoreCase)) { ClientGroupID = cg.ID; break; } } if (ClientGroupID == Guid.Empty) { //wasn't in the list so add it and save it ClientGroup cg=cgroups.Add(); cg.Name = s; ClientGroupID = cg.ID; cgroups = (ClientGroups)cgroups.Save(); } } c.ClientGroupID = ClientGroupID; } //region if (c.IsNew || Util.IEData.ClientRegion) { Guid RegionID = GZTW.AyaNova.BLL.Region.DefaultRegionID; string s = fld(dr, "Region", 255).Trim(); if (!string.IsNullOrWhiteSpace(s)) { if (regions.ContainsKey(s)) RegionID = regions[s]; else { GZTW.AyaNova.BLL.Region r = GZTW.AyaNova.BLL.Region.NewItem(); r.Active = true; r.Name = s; RegionID = r.ID; r=(GZTW.AyaNova.BLL.Region)r.Save(); regions.Add(r.Name, r.ID); } } c.RegionID = RegionID; } //Dispatch zone if (c.IsNew || Util.IEData.ClientDispatchZone) { Guid DispatchZoneID = Guid.Empty; string s = fld(dr, "DispatchZone", 255).Trim(); if (!string.IsNullOrWhiteSpace(s)) { foreach (DispatchZone cg in dzones) { if (cg.Name.Equals(s, StringComparison.CurrentCultureIgnoreCase)) { DispatchZoneID = cg.ID; break; } } if (DispatchZoneID == Guid.Empty) { //wasn't in the list so add it and save it DispatchZone cg = dzones.Add(); cg.Name = s; DispatchZoneID = cg.ID; dzones = (DispatchZones)dzones.Save(); } } c.DispatchZoneID = DispatchZoneID; } if (c.IsSavable) { bool bWasNew = c.IsNew; c.Save(); if (bWasNew) lGoodCount++; else lUpdatedCount++; } else { lBadCount++; } } w.Close(); if (Util.LicensedMode && Util.IEData.ClientUpdate) { if (lBadCount > 0) MessageBox.Show(lGoodCount.ToString() + " Clients were imported / "+lUpdatedCount + " duplicate clients were updated sucessfully.\r\n" + lBadCount.ToString() + " Clients were not imported because they had no name at all or did not pass the business / update rules for a client object."); else MessageBox.Show(lGoodCount.ToString() + " Clients were imported / "+ lUpdatedCount + " duplicate clients were updated sucessfully."); } else { if (lBadCount > 0) MessageBox.Show(lGoodCount.ToString() + " Clients were imported sucessfully.\r\n" + lBadCount.ToString() + " Clients were not imported because a client with the same name already\r\n" + "exists or they had no name at all or did not pass the rules for a client object."); else MessageBox.Show(lGoodCount.ToString() + " Clients were imported sucessfully."); } } /// /// Get a field from the current data row /// /// /// /// private string fld(DataRow dr, string sColumnNameKey, int nLength) { if (lsMap.ContainsKey(sColumnNameKey)) { return t(nLength, dr[lsMap[sColumnNameKey]].ToString()); } else return ""; } /// /// /// /// /// /// private static string t(int nLength, string s) { if(string.IsNullOrWhiteSpace(s)) return ""; if(s.Length<=nLength) return s; else return s.Substring(0,nLength); } #endregion #region Export data /// /// Exports all existing clients to CSV format identical /// to that required for import /// /// /// private void mnuExport_Click(object sender, System.EventArgs e) { SaveFileDialog sf = new SaveFileDialog(); sf.DefaultExt = ".csv"; sf.FileName = "clients.csv"; if (sf.ShowDialog() != DialogResult.OK) return; Cursor=Cursors.WaitCursor; FileHelperEngine engine = new FileHelperEngine(typeof(FHClient)); System.Collections.ArrayList alClients=new ArrayList(); FHClient fhc; Client c; ClientPickList cpl=ClientPickList.GetList(); Waiting w=new Waiting(); w.Show(); w.Ops="Exporting clients"; foreach(ClientPickList.ClientPickListInfo i in cpl) { c=Client.GetItem(i.ID); ClientList cl = ClientList.GetListForSingleItem(i.ID); fhc = new FHClient(); w.Step = cl[0].LT_O_Client.Display; fhc.Name = cl[0].LT_O_Client.Display; fhc.AccountNumber = cl[0].LT_Client_Label_AccountNumber; //contact fields fhc.Contact = cl[0].LT_Client_Label_Contact; fhc.ContactNotes = c.ContactNotes; fhc.Phone1 = cl[0].LT_Client_Label_Phone1; fhc.Phone2 = cl[0].LT_Client_Label_Phone2; fhc.Phone3 = cl[0].LT_Client_Label_Phone3; fhc.Phone4 = cl[0].LT_Client_Label_Phone4; fhc.Phone5 = cl[0].LT_Client_Label_Phone5; fhc.EmailAddress = cl[0].LT_Client_Label_Email; fhc.DeliveryAddress = c.GoToAddress.DeliveryAddress; fhc.DeliveryCity=c.GoToAddress.City; fhc.DeliveryCountry=c.GoToAddress.Country; fhc.DeliveryPostal=c.GoToAddress.Postal; fhc.DeliveryStateProvince=c.GoToAddress.StateProv; fhc.GeneralNotes=c.Notes; fhc.MailAddress=c.MailToAddress.DeliveryAddress; fhc.MailCity=c.MailToAddress.City; fhc.MailCountry=c.MailToAddress.Country; fhc.MailPostal=c.MailToAddress.Postal; fhc.MailStateProvince=c.MailToAddress.StateProv; fhc.PopupNotes=c.PopUpNotes; fhc.TechNotes=c.TechNotes; fhc.WebSite=c.WebAddress; //case 1737 fhc.ClientGroup = cl[0].LT_O_ClientGroup.Display; fhc.DispatchZone = cl[0].LT_O_DispatchZone.Display; fhc.Region = cl[0].LT_O_Region.Display; alClients.Add(fhc); } w.Close(); try { engine.WriteFile(sf.FileName, (object[]) alClients.ToArray(typeof(FHClient))); } catch(Exception ex) { MessageBox.Show("Error writing the export file:\r\n\r\n" + ex.Message); return; } Cursor=Cursors.Default; MessageBox.Show("All clients in current AyaNova database have been exported to file:\r\n" + sf.FileName); } #endregion private void mnuImportOptions_Click(object sender, EventArgs e) { Util.ShowOptionsDialog(); } #region case 1588 user selectable column placement private void DataGridView1_BindingContextChanged(object sender, EventArgs e) { // Continue only if the data source has been set. if (DataGridView1.DataSource == null) { return; } //these are the columns that can be selected by the user to import into List ls = new List(); ls.Add("Name"); ls.Add("MailAddress"); ls.Add("MailCity"); ls.Add("MailStateProvince"); ls.Add("MailCountry"); ls.Add("MailPostal"); ls.Add("DeliveryAddress"); ls.Add("DeliveryCity"); ls.Add("DeliveryStateProvince"); ls.Add("DeliveryCountry"); ls.Add("DeliveryPostal"); ls.Add("Contact"); ls.Add("ContactNotes"); ls.Add("Phone1"); ls.Add("Phone2"); ls.Add("Phone3"); ls.Add("Phone4"); ls.Add("Phone5"); ls.Add("EmailAddress"); ls.Add("AccountNumber"); ls.Add("WebSite"); ls.Add("GeneralNotes"); ls.Add("TechNotes"); ls.Add("PopupNotes"); //case 1737 ls.Add("ClientGroup"); ls.Add("DispatchZone"); ls.Add("Region"); // Add the AutoFilter header cell to each column. foreach (DataGridViewColumn col in DataGridView1.Columns) { col.HeaderCell = new GZDataGridViewComboColumnHeaderCell(col.HeaderCell,ls); } // Resize the columns to fit their contents. DataGridView1.AutoResizeColumns(); } private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { } #endregion case 1588 //------------------------------------------------------------------------ } }