Esempio n. 1
0
        private static int RunSendFromCsvAndReturnExitCode(SendLDocCSVOptions options)
        {
            var provider = CultureInfo.CreateSpecificCulture("en-US");
            if (string.IsNullOrWhiteSpace(options.CsvListFileName))
            {
                return 3; // this shouldn't happen anyway, because parameter is already required
            }
            var csvListFile = new FileInfo(options.CsvListFileName);
            if (!csvListFile.Exists)
            {
                log.Error(string.Format("ERROR: file {0} doesn't exist.", csvListFile.Name));
                return 4;
            }
            if (!csvListFile.Extension.ToLower().Equals(".csv"))
            {
                log.Error(string.Format("ERROR: file {0} should have .csv extnsion.", csvListFile.Name));
                return 4;
            }

            MutableDataTable dt;
            try
            {
                dt = DataTable.New.ReadCsv(csvListFile.FullName);
                int totalRows = dt.NumRows;
                if (totalRows < 1)
                {
                    log.Fatal(string.Format("ERROR: file {0} doesn't contain any rows.", csvListFile.Name));
                    return 4;
                }
                const int requiredColumns = 11;
                if (dt.Columns.Count() != requiredColumns)
                {
                    log.Fatal(string.Format("ERROR: file {0} should contain exactly {1} columns.", csvListFile.Name, requiredColumns));
                    return 4;
                }
                log.Info("Found " + totalRows + " rows.");
            }
            catch (Exception ex)
            {
                log.Fatal(ex);
                return 22;
            }
            var errors = 0;

            foreach (var r in dt.Rows)
            {
                try
                {
                    var inputFiles = r["files"].Split(';');
                    var files = new List<FileInfo>();
                    foreach (var f in inputFiles)
                    {
                        var fi = new FileInfo(f);
                        if (!fi.Exists)
                        {
                            log.Error(string.Format("ERROR: file {0} doesn't exist. Skipping...", fi.Name));
                            errors++;
                            continue;
                        }
                        files.Add(fi);
                    }

                    var naziv = r["naziv"];
                    var koda = r["koda"];
                    var nazivDobavitelja = r["nazivdobavitelja"];
                    var sifraDobavitelja = r["sifradobavitelja"];
                    var davcnaStevilkaDobavitelja = r["davcnastevilkadobavitelja"];
                    var stevilkaRacuna = r["stevilkaracuna"];
                    var node = r["Node"];
                    DateTime datumIzdajeRacuna = DateTime.ParseExact(r["datumizdajeracuna"].Trim(), "yyyy-dd-M", provider);
                    int leto = Int32.Parse(r["leto"]);

                    Guid guidTransaction = Guid.Empty;
                    guidTransaction = SendLogicaDocument(files, naziv, koda, nazivDobavitelja, sifraDobavitelja,
                                            davcnaStevilkaDobavitelja, stevilkaRacuna, datumIzdajeRacuna,
                                            leto, node, options.Debug);
                    r["guid"] = guidTransaction.ToString();
                    log.Info("Sent document with GUID:" + guidTransaction.ToString() + " Code:" + koda);
                }
                catch (Exception ex)
                {
                    log.Error(ex, "Error in loop: ");
                    errors++;
                }
            }

            try
            {
                var result = Path.Combine(csvListFile.DirectoryName, "out-" + csvListFile.Name);
                dt.SaveCSV(result);
            }
            catch (Exception ex)
            {
                log.Error(ex, "Error saving: ");
                return 222;
            }

            if (errors > 0)
                return 111;
            else
                return 0;
        }
Esempio n. 2
0
        private static int RunSendFromCsvAndReturnExitCode(SendLDocCSVOptions options)
        {
            var provider = CultureInfo.CreateSpecificCulture("en-US");

            if (string.IsNullOrWhiteSpace(options.CsvListFileName))
            {
                return(3); // this shouldn't happen anyway, because parameter is already required
            }
            var csvListFile = new FileInfo(options.CsvListFileName);

            if (!csvListFile.Exists)
            {
                log.Error(string.Format("ERROR: file {0} doesn't exist.", csvListFile.Name));
                return(4);
            }
            if (!csvListFile.Extension.ToLower().Equals(".csv"))
            {
                log.Error(string.Format("ERROR: file {0} should have .csv extnsion.", csvListFile.Name));
                return(4);
            }

            MutableDataTable dt;

            try
            {
                dt = DataTable.New.ReadCsv(csvListFile.FullName);
                int totalRows = dt.NumRows;
                if (totalRows < 1)
                {
                    log.Fatal(string.Format("ERROR: file {0} doesn't contain any rows.", csvListFile.Name));
                    return(4);
                }
                const int requiredColumns = 11;
                if (dt.Columns.Count() != requiredColumns)
                {
                    log.Fatal(string.Format("ERROR: file {0} should contain exactly {1} columns.", csvListFile.Name, requiredColumns));
                    return(4);
                }
                log.Info("Found " + totalRows + " rows.");
            }
            catch (Exception ex)
            {
                log.Fatal(ex);
                return(22);
            }
            var errors = 0;

            log.Info($"Is devel: {options.IsDevelopment}.");
            var tokenClient = GetAuthClient(options.FindCertificateByValue, options.IsDevelopment);

            if (tokenClient == null)
            {
                return(6);
            }

            foreach (var r in dt.Rows)
            {
                try
                {
                    var inputFile = r["files"].Split(';');

                    if (inputFile.Count() != 1)
                    {
                        throw new Exception("multiple files not supported");
                    }

                    var fi = new FileInfo(inputFile[0]);
                    if (!fi.Exists)
                    {
                        log.Error(string.Format("ERROR: file {0} doesn't exist. Skipping...", fi.Name));
                        errors++;
                        continue;
                    }

                    var      naziv                     = r["naziv"];
                    var      koda                      = r["koda"];
                    var      nazivDobavitelja          = r["nazivdobavitelja"];
                    var      sifraDobavitelja          = r["sifradobavitelja"];
                    var      davcnaStevilkaDobavitelja = r["davcnastevilkadobavitelja"];
                    var      stevilkaRacuna            = r["stevilkaracuna"];
                    var      node                      = r["Node"];
                    DateTime datumIzdajeRacuna         = DateTime.ParseExact(r["datumizdajeracuna"].Trim(), "yyyy-dd-M", provider);
                    int      leto                      = Int32.Parse(r["leto"]);
                    Guid     guidTransaction           = SendDocument(tokenClient, options.IsDevelopment, fi, naziv, koda, nazivDobavitelja, sifraDobavitelja,
                                                                      davcnaStevilkaDobavitelja, stevilkaRacuna, datumIzdajeRacuna,
                                                                      leto, node);

                    r["guid"] = guidTransaction.ToString();
                    log.Info("Sent document with GUID:" + guidTransaction.ToString() + " Code:" + koda);
                }
                catch (Exception ex)
                {
                    log.Error(ex, $"Error in loop: {ex.Message}");
                    errors++;
                }
            }

            try
            {
                var result = Path.Combine(csvListFile.DirectoryName, "out-" + csvListFile.Name);
                dt.SaveCSV(result);
            }
            catch (Exception ex)
            {
                log.Error(ex, "Error saving: ");
                return(222);
            }

            if (errors > 0)
            {
                return(111);
            }
            else
            {
                return(0);
            }
        }