private void btnCargarFactBruta_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "CSV files (*.csv)|*.csv", InitialDirectory = inDir, Title = "INNCODE || ---> SELECCIONE EL ARCHIVO" }; if (openFileDialog.ShowDialog() == DialogResult.OK) { LimpiarValidacionDeCampos(); txtRutaFactBruta.Text = openFileDialog.FileName; string fullPath = openFileDialog.FileName; inDir = @fullPath.Substring(0, fullPath.LastIndexOf('\\')); txtRutaExport.Text = inDir; try { var engine = new DelimitedFileEngine <BaseCSV>(); engine.Encoding = Encoding.UTF8; var result = engine.ReadFile(txtRutaFactBruta.Text); }catch (ConvertException ex) { txtRutaFactBruta.Text = ""; MessageBoxEx.Show(this, "ERROR AL PROCESAR ARCHIVO. PUEDE QUE SE DEBA A QUE ALGUNA DE LA COLUMNAS TENGAN UN CARACTER '|' DE MAS.\nPOR FAVOR PONGASE EN CONTACTO CON OFIN.\n" + ex.Message + "\nNUMERO REGISTRO ERROR: " + ex.LineNumber, "ERROR DEL SISTEMA", MessageBoxButtons.OK, MessageBoxIcon.Error); }catch (IOException ex) { txtRutaFactBruta.Text = ""; MessageBoxEx.Show(this, "NO SE PUEDE TENER ACCESO AL ARCHIVO PORQUE ESTÁ SIENDO UTILIZADO POR OTRO POGRAMA.\n" + ex.Message, "ERROR DEL SISTEMA", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
public School[] Read(string fileName) { var engine = new DelimitedFileEngine <School>(); var list = engine.ReadFile(fileName); return(list); }
//-> {Example.Name:Ignore Mode Error handling} //-> {Example.Description:Read the file dropping bad records} //-> {Example.File:RunEngine.cs} /// <summary> /// Run an example of running a file with an error using the /// IgnoreMode option to silently drop bad records /// </summary> public override void Run() { var engine = new DelimitedFileEngine <Customer>(); // Switch error mode on engine.ErrorManager.ErrorMode = ErrorMode.IgnoreAndContinue; // This fails with not in enumeration error Customer[] customers = engine.ReadFile("Input.txt"); // This wont display anything, we have dropped it foreach (ErrorInfo err in engine.ErrorManager.Errors) { Console.WriteLine(); Console.WriteLine("Error on Line number: {0}", err.LineNumber); Console.WriteLine("Record causing the problem: {0}", err.RecordString); Console.WriteLine("Complete exception information: {0}", err.ExceptionInfo.ToString()); } // This will display only 3 of the four records foreach (var cust in customers) { Console.WriteLine("Customer name {0} is a {1}", cust.ContactName, cust.ContactTitle); } }
private void ReadCSVFile(string fullName) { try { var engine = new DelimitedFileEngine <ImportCLassModel>(Encoding.UTF8); engine.AfterReadRecord += AfterReadCSVEvent; engine.Options.Delimiter = ";"; var records = engine.ReadFile(fullName); foreach (ImportCLassModel rec in records) { if (string.IsNullOrEmpty(rec.PurchaseOrder)) { throw new Exception("Some of Purchase Order is null/empty."); } if (string.IsNullOrEmpty(rec.Material)) { throw new Exception("Some of Material is null/empty."); } if (string.IsNullOrEmpty(rec.MaterialShortText)) { throw new Exception("Some of Material Short Text is null/empty."); } if (string.IsNullOrEmpty(rec.Eun)) { throw new Exception("Some of Eun Order is null/empty."); } if (string.IsNullOrEmpty(rec.StorageLoc)) { throw new Exception("Some of Storage Loc is null/empty."); } if (string.IsNullOrEmpty(rec.StorageBin)) { throw new Exception("Some of Storage Bin is null/empty."); } PopulateRecords(rec, tempCollection); } if (tempCollection.Count() > 0) { ImportBtn = true; IsEnableAutoRefresh = false; _ImportGRCommand.RaiseCanExecuteChanged(); _ExportGRCommand.RaiseCanExecuteChanged(); GoodReceives = new ListCollectionView(tempCollection); GoodReceives.SortDescriptions.Add(new SortDescription("PurchaseOrder", ListSortDirection.Ascending)); CollectionViewSource.GetDefaultView(GoodReceives).Filter = Filter; } } catch (Exception ex) { MessageBox.Show("Unable to read contents:\n\n" + ex.Message, "Error"); } }
public void LoadTargetsFromFile(string filename) { var fileHelpers = new DelimitedFileEngine <DwellableFileEntry>(); var fileCollection = fileHelpers.ReadFile(filename); foreach (var entry in fileCollection) { var target = new DwellableTarget(new Point(entry.x, entry.y), entry.label, entry.type, entry.page); AddTarget(target); } }
//-> /File //-> File:RunEngine.cs public override void Run() { var engine = new DelimitedFileEngine <Customer>(); // Read input records, enumeration automatically converted Customer[] customers = engine.ReadFile("Input.txt"); foreach (var cust in customers) { Console.WriteLine("Customer name {0} is a {1}", cust.ContactName, cust.ContactTitle); } }
// Declaration of Variables /// <summary> /// used to load and save the data from csv to database /// </summary> /// <param name="FilePath"></param> public SaveToDatabase(string FilePath) { var engine = new DelimitedFileEngine <CsvStructure>(); var rows = engine.ReadFile(FilePath); var fileName = Path.GetFileNameWithoutExtension(FilePath); var result = CheckIfExisted(fileName, rows); if (result) { foreach (var row in rows) { using (var database = _Database.Database.BeginTransaction()) { try { Barcode barcode = new Barcode(); barcode.Matched = row.Barcoded; barcode.BarcodeItem = row.BarItem; barcode.Barcodes = row.Barcode; barcode.CardNumber = MaskedNumber(row.CardNumber); barcode.Filename = row.FileName; barcode.Finalized = row.Finalized; barcode.LeanBarcode = row.LeanBarcode; barcode.LeanBarcodeItem = row.LeanBarItem; barcode.Batch = row.Batch; barcode.SequenceNumber = row.SequenceNumber; barcode.DateTime = DateTime.Now; barcode.User = Environment.UserName; _Database.Barcodes.Add(barcode); _Database.SaveChanges(); database.Commit(); _Logs.CreateLogs(">>>>>> Sequence Number: " + row.SequenceNumber + " successfully save into the database"); } catch (DbEntityValidationException e) { database.Rollback(); _Logs.CreateLogs(">>>>>> Database Error: " + e); MessageBox.Show("There's an error loading the file. \nPlease check the logs for more information about the error.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } MessageBox.Show("CSV successfully saved and loaded!", "INFORMATION", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { _Logs.CreateLogs(">>>>>> ERROR: File Existed"); } }
public void Import(IObjectSpace os, String acc_sys_code, String file_name) { DelimitedFileEngine engine = new DelimitedFileEngine(typeof(FAAccountImport)); FAAccountImport[] records = (FAAccountImport[])engine.ReadFile(file_name); fmCFAAccountSystem acc_sys = os.GetObjects <fmCFAAccountSystem>(new BinaryOperator("Code", acc_sys_code), true).FirstOrDefault(); if (acc_sys == null) { acc_sys = os.CreateObject <fmCFAAccountSystem>(); acc_sys.Code = "1000"; acc_sys.Name = "Áóõãàëòåðèÿ"; } foreach (FAAccountImport rec in records) { fmCFAAccount bs_acc = acc_sys.Accounts.Where(x => x.Code == rec.BsCode).FirstOrDefault(); if (bs_acc == null) { bs_acc = os.CreateObject <fmCFAAccount>(); bs_acc.Code = rec.BsCode; acc_sys.Accounts.Add(bs_acc); } if (rec.SsCode == "0000") { bs_acc.Name = rec.NameShort; bs_acc.NameFull = rec.NameFull; } else { fmCFAAccount ss_acc = acc_sys.Accounts.Where(x => x.Code == rec.SsCode).FirstOrDefault(); if (ss_acc == null) { ss_acc = os.CreateObject <fmCFAAccount>(); ss_acc.Code = rec.SsCode; bs_acc.SubAccounts.Add(ss_acc); } ss_acc.BuhCode = rec.BuhCode; if (rec.NameShort.Length > 60) { ss_acc.Name = rec.NameShort.Substring(0, 60); } else { ss_acc.Name = rec.NameShort; } ss_acc.NameFull = rec.NameFull; ss_acc.IsSelectabled = true; } } }
public void GetNCDxfData(Action <List <InternalCut>, Exception> callback, string filePath, string delimiter, int headers) { try { var engine = new DelimitedFileEngine <InternalCut>(Encoding.Default); engine.Options.Delimiter = delimiter; engine.Options.IgnoreFirstLines = headers; var result = engine.ReadFile(filePath).ToList(); callback(result, null); } catch (Exception e) { Console.WriteLine(e); callback(null, e); } }
public void Import(IObjectSpace os, String file_name) { DelimitedFileEngine engine = new DelimitedFileEngine(typeof(SimpleAnalyticImport)); SimpleAnalyticImport[] records = (SimpleAnalyticImport[])engine.ReadFile(file_name); foreach (SimpleAnalyticImport rec in records) { T obj = os.GetObjects <T>(new BinaryOperator("Code", rec.Code)).FirstOrDefault(); if (obj == default(T)) { obj = os.CreateObject <T>(); } obj.Code = rec.Code; obj.Name = rec.Name; } }
//-> {Example.Name:ErrorMode saving Errors} //-> {Example.Description:Read the file saving bad records} //-> {Example.File:RunEngine.cs} /// <summary> /// Run an example of running a file with an error using the /// ErrorMode option to capture bad records and then saving them /// </summary> public override void Run() { var engine = new DelimitedFileEngine <Customer>(); // Switch error mode on engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue; // This fails with not in enumeration error Customer[] customers = engine.ReadFile("Input.txt"); if (engine.ErrorManager.HasErrors) { engine.ErrorManager.SaveErrors("errors.out"); } LoadErrors(); }
//-> /File protected override void Run() { //-> File:Example.txt var engine = new DelimitedFileEngine <CustomersVerticalBar>(); engine.Options.Fields[2].TrimMode = TrimMode.Both; engine.Options.RemoveField("DummyField"); // City is optional engine.Options.Fields[engine.Options.Fields.Count - 1].IsOptional = true; engine.ReadFile("Input.txt"); //-> /File }
//-> /File public override void Run() { //-> File:Example.cs try { var engine = new DelimitedFileEngine <Customer>(); // This fails with not in enumeration error var customers = engine.ReadFile("Input.txt"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); // with stack trace } //-> /File }
private async void btnMergeFact_Click(object sender, EventArgs e) { pgbAnalisis.Visible = true; pgbAnalisis.Style = eDotNetBarStyle.Windows7; btnMergeFact.Enabled = false; await Task.Run(() => { try { CheckForIllegalCrossThreadCalls = false; var engineNotas = new FileHelperEngine <NotasCSV>(); NotasCSV[] resultNotas = engineNotas.ReadFile(archFact.rutaNotas); conexion.InsertarRegistrosNotas(resultNotas); var engineBase = new DelimitedFileEngine <BaseCSV>(); engineBase.Encoding = Encoding.UTF8; BaseCSV[] resultBases = engineBase.ReadFile(archFact.rutaFactBruta); conexion.InsertarRegistrosEnBaseFacturacion(resultBases); notifyIcon1.Visible = true; notifyIcon1.Icon = SystemIcons.Information; notifyIcon1.BalloonTipTitle = "MENSAJE DEL SISTEMA"; notifyIcon1.BalloonTipText = "PROCESO COMPLETADO SATISFACTORIAMENTE."; notifyIcon1.BalloonTipIcon = ToolTipIcon.Info; notifyIcon1.ShowBalloonTip(1000); } catch (Exception ex) { MessageBoxEx.Show(this, "ERROR AL GENERAR ARCHIVO.\n" + ex.Message, "ERROR DEL SISTEMA", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }); FlashWindowHelper.Flash(this); //btnImport.Enabled = true; pgbAnalisis.Visible = false; // MessageBoxEx.Show(this, "PROCESO COMPLETADO SATISFACTORIAMENTE", "MENSAJE DEL SISTEMA", MessageBoxButtons.OK, MessageBoxIcon.Information); archFact.GenerarFacturacinBruta = true; if (!archFact.GenerarFacturacionNeta) { btnGenerarFactNeta.Enabled = true; } }
public List <object> ProcessCSVData(string fileClass, string fullFilePath, out ErrorInfo[] erroredRecords) { //Generic implementation of mapping corresponding model based on FileType and processing csv records. //Easily extendable for additional types of files by creating additional FileHelper classes Assembly asm = typeof(IFileHelperModel).Assembly; Type type = asm.GetType(fileClass); var engine = new DelimitedFileEngine(type); engine.Options.IgnoreFirstLines = 1; engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue; List <object> processedCSVRecords = engine.ReadFile(fullFilePath).ToList(); erroredRecords = engine.ErrorManager.Errors; return(processedCSVRecords); }
public void GetPlyData(Action <List <PlyFile>, Exception> callback, string filePath, string delimiter, int headers) { // Use this to connect to the actual data service try { var engine = new DelimitedFileEngine <PlyFile>(Encoding.Default); engine.Options.Delimiter = delimiter; engine.Options.IgnoreFirstLines = headers; var result = engine.ReadFile(filePath).ToList(); callback(result, null); } catch (Exception e) { Console.WriteLine(e); callback(null, e); } }
/// <summary> /// Generates XML documents from the extracted and converted files with appropriate encoding. /// </summary> /// <param name="skipGenerating">Value that specifies whether to skip generating XML documents or not.</param> public override void GenerateXmlDocuments(bool skipGenerating) { if (skipGenerating) { this.OnStatusChanged(new StatusChangedEventArgs("XML generation skipped")); return; } var csv = new DelimitedFileEngine <StreetBasedAddress>() { Encoding = Encoding.GetEncoding(949) }; csv.AfterReadRecord += Csv_AfterReadRecord; csv.Options.Delimiter = "|"; csv.Options.IgnoreFirstLines = 1; //foreach (var filepath in Directory.GetFiles(this.ExtractDirectory).Where(p => p.EndsWith(".csv"))) foreach (var filepath in Directory.GetFiles(this.ExtractDirectory).Where(p => p.EndsWith(".txt"))) { var filename = ConversionHelper.GetFilenameFromFilepath(filepath, this.Settings); this.OnStatusChanged(new StatusChangedEventArgs(String.Format("Generating an XML document from - {0}", filename))); var address = csv.ReadFile(filepath); var addresses = new StreetBasedAddresses() { StreetBasedAddress = address }; //using (var writer = new XmlTextWriter(filepath.Replace(".csv", ".xml"), Encoding.UTF8) { Formatting = Formatting.Indented }) using (var writer = new XmlTextWriter(filepath.Replace(".txt", ".xml"), Encoding.UTF8) { Formatting = Formatting.Indented }) { var serialiser = new XmlSerializer(typeof(StreetBasedAddresses)); serialiser.Serialize(writer, addresses); } //this.OnStatusChanged(new StatusChangeEventArgs(String.Format("Generated the XML document - {0}", filename.Replace(".csv", ".xml")))); this.OnStatusChanged(new StatusChangedEventArgs(String.Format("Generated the XML document - {0}", filename.Replace(".txt", ".xml")))); } }
public void Import(IObjectSpace os, String file_name) { DelimitedFileEngine engine = new DelimitedFileEngine(typeof(Tr)); engine.Options.IgnoreFirstLines = 1; Tr[] records = (Tr[])engine.ReadFile(file_name); IList <TrwRefCashFlow> obj_refs = os.GetObjects <TrwRefCashFlow>(); foreach (Tr rec in records) { TrwRefCashFlow obj = obj_refs.FirstOrDefault(x => x.Code == rec.Code.Trim()); if (obj == null) { obj = os.CreateObject <TrwRefCashFlow>(); obj_refs.Add(obj); obj.Code = rec.Code.Trim(); } obj.Name = rec.Name.Trim(); obj.NameFull = rec.Name.Trim(); } foreach (TrwRefCashFlow obj_ref in obj_refs) { String [] comp = obj_ref.Code.Split('.'); if (comp.Length > 1) { String[] tops_comp = new String[comp.Length - 1]; for (int i = 0; i < tops_comp.Length; i++) { tops_comp[i] = comp[i]; } String top_code = String.Join(".", tops_comp); TrwRefCashFlow top_obj = obj_refs.FirstOrDefault(x => x.Code == top_code); top_obj.Childs.Add(obj_ref); } } foreach (TrwRefCashFlow obj_ref in obj_refs) { if (obj_ref.Childs.Count == 0) { obj_ref.IsSelectabled = true; } } }
//-> {Example.Name:ErrorMode Error handling} //-> {Example.Description:Read the file rejecting bad records} //-> {Example.File:RunEngine.cs} /// <summary> /// Run an example of running a file with an error using the /// ErrorMode option to capture bad records /// </summary> /// <remarks> /// In the standard mode you can catch the exceptions when something fails. /// </remarks> public override void Run() { var engine = new DelimitedFileEngine <Customer>(); // Switch error mode on engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue; // Only record that fails will not be present Customer[] customers = engine.ReadFile("Input.txt"); // This will display error from line 2 of the file. foreach (ErrorInfo err in engine.ErrorManager.Errors) { Console.WriteLine(); Console.WriteLine("Error on Line number: {0}", err.LineNumber); Console.WriteLine("Record causing the problem: {0}", err.RecordString); Console.WriteLine("Complete exception information: {0}", err.ExceptionInfo.ToString()); } }
public void Import(IObjectSpace os, String file_name) { DelimitedFileEngine engine = new DelimitedFileEngine(typeof(Tr)); Tr[] records = (Tr[])engine.ReadFile(file_name); foreach (Tr rec in records) { To obj = os.GetObjects <To>(new BinaryOperator("Code", rec.Code)).FirstOrDefault(); if (obj == default(To)) { obj = os.CreateObject <To>(); } obj.Code = rec.Code; obj.Name = rec.Name; if (ProcessRecordEvent != null) { ProcessRecordEvent(this, new ProcessRecordEventArgs(os, obj, rec)); } } }
//-> Name:Simple Error handling //-> Description:Read the file or reject the whole file //-> File:RunEngine.cs /// <summary> /// Run an example of running a file with an error through and /// using a try catch to collect the error. /// </summary> /// <remarks> /// In the standard mode you can catch the exceptions when something fails. /// </remarks> public override void Run() { try { var engine = new DelimitedFileEngine <Customer>(); // This fails with not in enumeration error Customer[] customers = engine.ReadFile("Input.txt"); // this will not happen because of the exception foreach (var cust in customers) { Console.WriteLine("Customer name {0} is a {1}", cust.ContactName, cust.ContactTitle); } } catch (Exception ex) { // Console.WriteLine(ex.ToString()); // with stack trace this.Console.WriteLine(ex.Message); } }
public void Import(IObjectSpace os, String file_name) { DelimitedFileEngine engine = new DelimitedFileEngine(typeof(Tr)); engine.Options.IgnoreFirstLines = 1; Tr[] records = (Tr[])engine.ReadFile(file_name); IList <fmÑOrderAnalitycFinanceSource> type_refs = os.GetObjects <fmÑOrderAnalitycFinanceSource>(); foreach (Tr rec in records) { fmCOrder order = os.GetObjects <fmCOrder>(new BinaryOperator("Code", rec.Code)).First(); if (rec.OrderType == "ÊÏ") { order.AnalitycFinanceSource = type_refs.First(x => x.Code == "Ïð.êîììåð."); } if (rec.OrderType == "×Ï") { order.AnalitycFinanceSource = type_refs.First(x => x.Code == "×èñò.Ïðèáûëü"); } } }
public static async Task Import() { var legalRequirement = "R"; var engine = new DelimitedFileEngine <MedicineTemplate>(); //var res = engine.ReadFile("C:\\Users\\ACER\\Desktop\\projekt_ravim\\ravimid_3.csv"); var res = engine.ReadFile("wwwroot\\est_med.csv"); List <MedicineTemplate> meds = new List <MedicineTemplate>(); List <string> effectsList = new List <string>(); foreach (MedicineTemplate med in res) { med.ValidFrom = DateTime.Today; //Valmisprogrammis ravimite lisamiseks: //if (med.LegalStatus != legalRequirement) //{ // meds.Add(med); //} meds.Add(med); } AddMedicines(meds); }
public Order[] GetOrders(string filename) { var engine = new DelimitedFileEngine <Order>(); // Switch error mode on engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue; // This fails with not in enumeration error var orders = engine.ReadFile(filename); if (engine.ErrorManager.HasErrors) { engine.ErrorManager.SaveErrors("errors.out"); LoadErrors(filename); } // Console.WriteLine("====================================================="); //foreach (var order in orders) // Console.WriteLine("Order id {0} dt {1} amount {2} productId {3}", order.Id, order.Dt.ToString("yyyy-MM-ddThh:mm:ss"), order.Amount, order.ProductId); return(orders); }
public void Import(IObjectSpace os, String file_name) { DelimitedFileEngine engine = new DelimitedFileEngine(typeof(Tr)); engine.Options.IgnoreFirstLines = 1; Tr[] records = (Tr[])engine.ReadFile(file_name); foreach (Tr rec in records) { if (String.IsNullOrEmpty(rec.RefCode)) { continue; } TrwRefBase obj = os.GetObjects <TrwRefBase>( new BinaryOperator("Code", rec.Code) & new BinaryOperator("RefCode", rec.RefCode) ).FirstOrDefault(); if (obj == null) { obj = TrwRefBase.Create(os, rec.RefCode); } obj.Code = rec.Code; obj.Name = rec.Name; } }
public void Import(IObjectSpace os, String file_name) { // CsvOptions options = new CsvOptions(typeof(FinIndexItemImport).Name, ';', 6); // options.DecimalSeparator = ","; // CsvEngine engine = new CsvEngine(options); DelimitedFileEngine engine = new DelimitedFileEngine(typeof(FinIndexItemImport)); FinIndexItemImport[] records = (FinIndexItemImport[])engine.ReadFile(file_name); IList <fmCFinIndex> indexes = os.GetObjects <fmCFinIndex>(null, true); fmCOrderExt order = null; foreach (FinIndexItemImport rec in records) { fmCFinIndex index = indexes.FirstOrDefault(x => x.Code == rec.IndexCode); // os.GetObjects<fmCFinIndex>(new BinaryOperator("Code", rec.IndexCode),true).FirstOrDefault(); if (index == null) { throw new InvalidDataException("Unknow Index: " + rec.IndexCode); } if (order == null || order.Code != rec.OrderCode) { order = os.GetObjects <fmCOrderExt>(new BinaryOperator("Code", rec.OrderCode), true).FirstOrDefault(); } if (order == null) { // DialogResult result = MessageBox.Show("Unknow Order: " + rec.OrderCode); // throw new InvalidDataException("Unknow Order: " + rec.OrderCode); continue; } fmIFinIndexStructureItem item = order.FinIndexes.FirstOrDefault(x => x.FinIndex == index); if (item == null) { item = order.FinIndexesCreateItem(index); } if (rec.SummCKBM == null) { rec.SummCKBM = 0; } if (rec.SummOZM == null) { rec.SummOZM = 0; } if (rec.SummOrion == null) { rec.SummOrion = 0; } if (rec.SummPersonalContract == null) { rec.SummPersonalContract = 0; } if (rec.SummOther == null) { rec.SummOther = 0; } if (rec.SummAll == null) { rec.SummOther = 0; } item.SummKB = (Decimal)rec.SummCKBM * 1000; item.SummOZM = (Decimal)rec.SummOZM * 1000; item.SummOrion = (Decimal)rec.SummOrion * 1000; item.SummPersonalContract = (Decimal)rec.SummPersonalContract * 1000; item.SummOther = (Decimal)rec.SummOther * 1000; if (item.SummKB + item.SummOZM + item.SummOrion + item.SummPersonalContract + item.SummOther != rec.SummAll * 1000) { throw new InvalidDataException("Order: " + order.Code + " Index: " + index.Code + " All summ" + rec.SummAll.ToString()); } } }
private static void RunOptionsAndReturnExitCode(Options opts) { if (!File.Exists(opts.InputFile) || !File.Exists(opts.MappingFile)) { Console.WriteLine("Input and mapping definition files are required for processing"); return; } if (string.IsNullOrEmpty(opts.Domain)) { opts.Domain = Environment.UserDomainName; Console.WriteLine(" ==> Domain has been set to: " + opts.Domain); } Dictionary <string, string> fieldMappings = new Dictionary <string, string>(); using (StreamReader sr = new StreamReader(opts.MappingFile)) { while (!sr.EndOfStream) { var line = sr.ReadLine(); var split = line.Split('='); if (split.Length == 2 && !string.IsNullOrEmpty(split[0]) && !string.IsNullOrEmpty(split[1])) { fieldMappings.Add(split[0], split[1]); } } } var detector = new FileHelpers.Detection.SmartFormatDetector(); detector.MaxSampleLines = 10; var formats = detector.DetectFileFormat(opts.InputFile); bool keyFound = false; DelimitedClassBuilder dcb; DelimitedFileEngine fileEngine = null; foreach (var format in formats) { Console.WriteLine("File Format Detected, confidence:" + format.Confidence + "%"); var delimited = format.ClassBuilderAsDelimited; Console.WriteLine(" Delimiter:" + delimited.Delimiter); dcb = new DelimitedClassBuilder("InputFile", delimited.Delimiter); Console.WriteLine(" Fields:"); foreach (var field in delimited.Fields) { dcb.AddField(field); var mappedField = "<Ignored>"; if (fieldMappings.ContainsKey(field.FieldName)) { mappedField = fieldMappings[field.FieldName]; } var keyField = string.Empty; if (field.FieldName == opts.Key) { keyField = " << UNIQUE KEY >>"; keyFound = true; } Console.WriteLine(" " + field.FieldName + ": " + field.FieldType + " => " + mappedField + keyField); } fileEngine = new DelimitedFileEngine(dcb.CreateRecordClass()); } if (!keyFound) { Console.WriteLine("The specified unique key field, " + opts.Key + ", has not been found in the input file - aborting."); return; } if (fileEngine == null) { return; } DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://" + opts.Domain, null, null, AuthenticationTypes.Secure); DirectorySearcher search = new DirectorySearcher(ldapConnection); foreach (var mapping in fieldMappings) { search.PropertiesToLoad.Add(mapping.Value); } dynamic[] sourceFile = fileEngine.ReadFile(opts.InputFile); Console.WriteLine(); Console.WriteLine("Input File loaded; " + (sourceFile.Length - 1) + " records"); bool firstLineSkipped = false; int count = 1; foreach (dynamic rec in sourceFile) { if (firstLineSkipped) { string key = Dynamic.InvokeGet(rec, opts.Key); search.Filter = "(employeeID=" + key + ")"; SearchResultCollection result = search.FindAll(); if (result.Count == 1) { var user = result[0].GetDirectoryEntry(); Console.WriteLine(count + "/" + (sourceFile.Length - 1) + ": Checking user " + user.Name); foreach (var mapping in fieldMappings) { if (mapping.Value == opts.Key) { continue; } bool updateValue = false; string oldValue = string.Empty; string newValue = string.Empty; newValue = Dynamic.InvokeGet(rec, mapping.Key); if (user.Properties[mapping.Value].Value != null) { if (mapping.Value.ToLower() == "manager") { if (!string.IsNullOrEmpty(Dynamic.InvokeGet(rec, mapping.Key))) { // Manager has to be treated differently, as its a DN reference to another AD object. string[] man = Dynamic.InvokeGet(rec, mapping.Key).Split(' '); // Lookup what the manager DN SHOULD be DirectorySearcher managerSearch = new DirectorySearcher(ldapConnection); managerSearch.PropertiesToLoad.Add("distinguishedName"); managerSearch.Filter = "(&(givenName=" + man[0] + ")(sn=" + man[1] + "))"; var manager = managerSearch.FindOne(); if (manager != null) { newValue = manager.GetDirectoryEntry().Properties["distinguishedName"].Value.ToString(); if (user.Properties[mapping.Value].Value.ToString() != newValue) { updateValue = true; oldValue = user.Properties[mapping.Value].Value.ToString(); } } } } else { if (user.Properties[mapping.Value].Value.ToString() != Dynamic.InvokeGet(rec, mapping.Key)) { updateValue = true; oldValue = user.Properties[mapping.Value].Value.ToString(); } } } else { updateValue = true; } if (updateValue) { if (mapping.Value.ToLower() == "manager") { if (!string.IsNullOrEmpty(Dynamic.InvokeGet(rec, mapping.Key))) { // Manager has to be treated differently, as its a DN reference to another AD object. string[] man = Dynamic.InvokeGet(rec, mapping.Key).Split(' '); // Lookup what the manager DN SHOULD be DirectorySearcher managerSearch = new DirectorySearcher(ldapConnection); managerSearch.PropertiesToLoad.Add("distinguishedName"); managerSearch.Filter = "(&(givenName=" + man[0] + ")(sn=" + man[1] + "))"; var manager = managerSearch.FindOne(); if (manager != null) { newValue = manager.GetDirectoryEntry().Properties["distinguishedName"].Value.ToString(); } } } Console.WriteLine(" >> Value to be updated (" + mapping.Value + ") - " + oldValue + " != " + newValue); if (!opts.WhatIf) { user.Properties[mapping.Value].Clear(); user.Properties[mapping.Value].Add(newValue); } } } if (!opts.WhatIf) { user.CommitChanges(); Console.WriteLine("Updated user " + user.Name); } } else { if (result.Count > 1) { Console.WriteLine(count + "/" + (sourceFile.Length - 1) + ": -> Multiple entries returned; " + key); continue; } else { Console.WriteLine(count + "/" + (sourceFile.Length - 1) + ": -> No record found for " + key); if (opts.LooseMatching) { string userFullName = string.Empty; string firstName = string.Empty; string lastName = string.Empty; foreach (var mapping in fieldMappings) { if (mapping.Value == "givenName") { firstName = Dynamic.InvokeGet(rec, mapping.Key); } else if (mapping.Value == "sn") { lastName = Dynamic.InvokeGet(rec, mapping.Key); } } if (string.IsNullOrEmpty(firstName) || string.IsNullOrEmpty(lastName)) { Console.WriteLine("No firstname and lastname values found mapped, can not loosely match - aborting run"); return; } userFullName = firstName + " " + lastName; Console.WriteLine(" -> Trying search for users full name " + userFullName); search.Filter = "(cn=" + userFullName + ")"; result = search.FindAll(); if (result.Count == 1) { var user = result[0].GetDirectoryEntry(); Console.WriteLine("Checking user " + user.Name); foreach (var mapping in fieldMappings) { bool updateValue = false; if (user.Properties[mapping.Value].Value != null) { if (user.Properties[mapping.Value].Value.ToString() != Dynamic.InvokeGet(rec, mapping.Key)) { updateValue = true; } } else { updateValue = true; } if (updateValue) { Console.WriteLine("Value to be updated (" + mapping.Value + ") "); if (!opts.WhatIf) { user.Properties[mapping.Value].Clear(); user.Properties[mapping.Value].Add(Dynamic.InvokeGet(rec, mapping.Key)); } } } if (!opts.WhatIf) { user.CommitChanges(); Console.WriteLine("Updated user " + user.Name); } } else { Console.WriteLine(" E -> Too many or no hits to for a loose match"); } } } } count++; } firstLineSkipped = true; } }
/// <summary> /// Loads street-based addresses to database. /// </summary> /// <param name="filepaths">List of XML document paths.</param> private void LoadStreetBasedAddresses(IEnumerable <string> filepaths) { foreach (var filepath in filepaths) { var filename = ConversionHelper.GetFilenameFromFilepath(filepath, this._settings); using (var context = new KoreanPostcodeAddressDataContext()) { this.OnStatusChanged(new StatusChangedEventArgs(String.Format("Loading a file - {0} - to database", filename))); var csv = new DelimitedFileEngine <Aliencube.Utilities.KoreanPostcodeAddressConverter.Services.Models.StreetBasedAddress>() { Encoding = Encoding.GetEncoding(949) }; csv.AfterReadRecord += Csv_AfterReadRecord; csv.Options.Delimiter = "|"; csv.Options.IgnoreFirstLines = 1; // Loads objects directly from the CSV file. var addresses = csv.ReadFile(filepath); var blockSize = this._settings.GetProcessRequests <int>("databaseLoadingBlockSize"); var block = (int)Math.Ceiling((double)addresses.Length / blockSize); for (var i = 0; i < block; i++) { this.OnStatusChanged(new StatusChangedEventArgs(String.Format("Loading a file - {0} - to database ({1}/{2})", filename, i + 1, block))); var records = i < block - 2 ? blockSize : addresses.Length - (i * block); foreach (var address in addresses.Skip(i * (int)blockSize) .Take((int)records) .Select(p => new StreetBasedAddress() { Postcode = ConversionHelper.ConvertToString(p.Postcode), SequenceNumber = ConversionHelper.ConvertToString(p.SequenceNumber), Province = ConversionHelper.GetProvince(p.Province), ProvinceEng = ConversionHelper.GetProvinceEng(p.ProvinceEng), City = ConversionHelper.GetCity(p.Province), CityEng = ConversionHelper.GetCityEng(p.ProvinceEng), County = ConversionHelper.GetCounty(p.Province, p.County), CountyEng = ConversionHelper.GetCountyEng(p.ProvinceEng, p.CountyEng), District = ConversionHelper.GetDistrict(p.Province, p.County), DistrictEng = ConversionHelper.GetDistrictEng(p.ProvinceEng, p.CountyEng), Suburb = ConversionHelper.GetSuburb(p.Suburb), SuburbEng = ConversionHelper.GetSuburbEng(p.SuburbEng), StreetNameCode = ConversionHelper.ConvertToString(p.StreetNameCode), StreetName = ConversionHelper.GetStreet(p.StreetName, p.StreetNameEng), StreetNameEng = ConversionHelper.GetStreetEng(p.StreetNameEng), Basement = ConversionHelper.ConvertToNullableInt32(p.Basement), BuildingNumberMajor = ConversionHelper.ConvertToNullableInt32(p.BuildingNumberMajor), BuildingNumberMinor = ConversionHelper.ConvertToNullableInt32(p.BuildingNumberMinor), BuildingCode = ConversionHelper.ConvertToString(p.BuildingCode), BuildingNameForBulk = ConversionHelper.ConvertToString(p.BuildingNameForBulk), BuildingName = ConversionHelper.ConvertToString(p.BuildingName), RegisteredSuburbCode = ConversionHelper.ConvertToString(p.RegisteredSuburbCode), RegisteredSuburb = ConversionHelper.GetSuburb(p.RegisteredSuburb), SuburbSequenceNumber = ConversionHelper.ConvertToNullableInt32(p.SuburbSequenceNumber), Village = ConversionHelper.GetVillage(p.Village), San = ConversionHelper.ConvertToNullableBoolean(p.San), LotNumberMajor = ConversionHelper.ConvertToNullableInt32(p.LotNumberMajor), LotNumberMinor = ConversionHelper.ConvertToNullableInt32(p.LotNumberMinor) })) { context.StreetBasedAddresses.AddObject(address); } context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave); this.OnStatusChanged(new StatusChangedEventArgs(String.Format("Loaded the file - {0} - to database ({1}/{2})", filename, i + 1, block))); } } } }
public void Import(IObjectSpace os, String file_name) { DelimitedFileEngine engine = new DelimitedFileEngine(typeof(fmOrderImport)); fmOrderImport[] records = (fmOrderImport[])engine.ReadFile(file_name); IList <fmCOrderExt> orders = os.GetObjects <fmCOrderExt>(null, true); fmCFAAccountSystem acc_sys = os.GetObjects <fmCFAAccountSystem>(new BinaryOperator("Code", "1000"), true).FirstOrDefault(); IList <fmÑOrderAnalitycAccouterType> acc_types = os.GetObjects <fmÑOrderAnalitycAccouterType>(); foreach (fmOrderImport rec in records) { fmCOrderExt order = orders.FirstOrDefault(item => item.Code == rec.Code); if (order == null) { Trace.TraceWarning("Updater_1.1.1.220_OrderImport: Order >" + rec.Code + "< not found"); order = os.CreateObject <fmCOrderExt>(); order.Code = rec.Code; if (rec.IsClosed != "X") { order.Status = fmIOrderStatus.Loaded; } else { order.Status = fmIOrderStatus.FinClosed; } orders.Add(order); } order.BuhIntNum = rec.Oid; if (rec.IsClosed != "X") { if (order.Status == fmIOrderStatus.Project || order.Status == fmIOrderStatus.FinOpened) { order.Status = fmIOrderStatus.Opened; order.IsClosed = false; } } else { if (order.Status == fmIOrderStatus.Project || order.Status == fmIOrderStatus.FinClosed) { order.Status = fmIOrderStatus.Closed; order.IsClosed = true; } } if (!String.IsNullOrEmpty(rec.AccType) && rec.AccType != "0") { order.AnalitycAccouterType = acc_types.FirstOrDefault(x => x.Code == rec.AccType); } if (!String.IsNullOrEmpty(rec.AccBuhCode) && rec.AccBuhCode != "0") { order.BuhAccount = acc_sys.Accounts.FirstOrDefault(x => x.BuhCode == rec.AccBuhCode); if (order.BuhAccount == null) { Trace.TraceWarning("Updater_1.1.1.220_OrderImport: Order >" + rec.Code + "< Account: >" + rec.AccBuhCode + "< not found"); } } } foreach (fmCOrderExt order in orders) { fmOrderImport order_record = records.FirstOrDefault(x => x.Code == order.Code); if (order.Status != fmIOrderStatus.Project && order.Status != fmIOrderStatus.FinOpened && order_record == null) { order.Status = fmIOrderStatus.Deleting; Trace.TraceWarning("Updater_1.1.1.220_OrderImport: Delete order >" + order.Code + "< Status: " + order.Status.ToString()); } if (order.Status != fmIOrderStatus.Deleting && order.Status != fmIOrderStatus.Project && order.Status != fmIOrderStatus.FinOpened && order.Status != fmIOrderStatus.FinClosed && order.Status != fmIOrderStatus.Opened && order.Status != fmIOrderStatus.Closed) { order.Status = fmIOrderStatus.Loaded; } } }