Exemple #1
0
 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
 {
     UnhandledException.WriteEvent(sender, e, true);
 }
Exemple #2
0
        static void ProcessFile(FileInfo file, DirectoryInfo toPath)
        {
            Console.WriteLine(file.Name);


            FileInfo dstFile = null;

            try
            {
                //Encoding enc = GetEncoding(file);

                List <String> special = new List <String>();
                if (!String.IsNullOrWhiteSpace(localConfig.ClearSpecial))
                {
                    foreach (String s in localConfig.ClearSpecial.Split(",".ToCharArray()))
                    {
                        if (!String.IsNullOrWhiteSpace(s))
                        {
                            special.Add(s.Trim().ToLower());
                        }
                    }
                }

                List <String> divC = new List <String>();
                if (!String.IsNullOrWhiteSpace(localConfig.FolderDivColumn))
                {
                    foreach (String s in localConfig.FolderDivColumn.Split(",".ToCharArray()))
                    {
                        if (!String.IsNullOrWhiteSpace(s))
                        {
                            divC.Add(s.Trim().ToLower());
                        }
                    }
                }


                TextLog.Log("SplitCsv", "Special: " + (special.Count > 0 ? String.Join(",", special) : "empty"));
                TextLog.Log("SplitCsv", "Divisor: " + (divC.Count > 0 ? String.Join(",", divC) : "empty"));

                List <FileInfo> files = new List <FileInfo>();

                //Byte[] bData = File.ReadAllBytes(file.FullName);

                TextLog.Log("SplitCsv", file.FullName);

                Int64 lineN = 0;


                using (FileStream fs = file.OpenRead())
                    using (StreamReader reader = new StreamReader(fs, Encoding.GetEncoding("iso-8859-1")))
                    {
                        dstFile = null;

                        TextLog.Log("SplitCsv", "reader.EndOfStream? " + reader.EndOfStream);
                        //Int32 lines = 0;
                        List <ColData> cols = new List <ColData>();

                        while (!reader.EndOfStream)
                        {
                            lineN++;

                            String line = reader.ReadLine();

                            try
                            {
                                String[] parts = line.Split(localConfig.Delimiter.ToCharArray());

                                if (cols.Count == 0)
                                {
                                    for (Int32 c = 0; c < parts.Length; c++)
                                    {
                                        if (parts[c].Trim() != "")
                                        {
                                            cols.Add(new ColData(c, parts[c].Trim()));
                                        }
                                    }

                                    //Ordena por indice
                                    cols.Sort(delegate(ColData s1, ColData s2) { return(s1.Index.CompareTo(s2.Index)); });
                                }
                                else
                                {
                                    String fprefix = "none";

                                    List <String> colData = new List <String>();

                                    foreach (ColData c in cols)
                                    {
                                        String value = "";
                                        if (c.Index < parts.Length)
                                        {
                                            value = parts[c.Index];

                                            if (special.Contains(c.Name.ToLower()))
                                            {
                                                value = ClearText(value);
                                            }

                                            if (divC.Contains(c.Name.ToLower()))
                                            {
                                                fprefix = ClearText(value);
                                            }
                                        }
                                        colData.Add(value);
                                    }

                                    //Build Filename
                                    Boolean writeHeader = false;
                                    dstFile = new FileInfo(Path.Combine(Path.Combine(toPath.FullName, fprefix), file.Name.Replace(file.Extension, "")));
                                    if (!dstFile.Directory.Exists)
                                    {
                                        writeHeader = true;
                                    }
                                    else if ((!dstFile.Exists) || (dstFile.Exists && dstFile.Length == 0))
                                    {
                                        writeHeader = true;
                                    }

                                    if (writeHeader)
                                    {
                                        List <String> colsH = new List <String>();

                                        foreach (ColData c in cols)
                                        {
                                            colsH.Add(c.Name);
                                        }

                                        WriteFile(dstFile, String.Join(localConfig.Delimiter, colsH));
                                    }

                                    WriteFile(dstFile, String.Join(localConfig.Delimiter, colData));

                                    if (!files.Exists(f => (f.FullName.ToLower() == dstFile.FullName.ToLower())))
                                    {
                                        files.Add(dstFile);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                TextLog.Log("SplitCsv", "Falha ao importar a linha '" + line + "' do arquivo '" + file.Name + "': " + ex.Message);
                            }
                        }

                        TextLog.Log("SplitCsv", "Processado " + lineN + " linhas");
                    }

                TextLog.Log("SplitCsv", "Movendo arquivo " + file.FullName + " para " + file.FullName + ".imported");
                File.Move(file.FullName, file.FullName + ".imported");

                foreach (FileInfo f in files)
                {
                    if (!f.Directory.Exists)
                    {
                        f.Directory.Create();
                    }

                    TextLog.Log("SplitCsv", "Movendo arquivo " + f.FullName + " para " + f.FullName + ".csv");
                    File.Move(f.FullName, f.FullName + ".csv");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Erro on proccess file '" + file.FullName + "'");
                UnhandledException.WriteEvent(null, new Exception("Erro on proccess file '" + file.FullName + "'", ex), false);

                if (dstFile != null)
                {
                    try
                    {
                        dstFile.Delete();
                    }
                    catch { }
                }
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            ServerLocalConfig localConfig;

            /*************
             * Carrega configurações
             */

            localConfig = new ServerLocalConfig();
            localConfig.LoadConfig();

            if ((localConfig.SqlServer == null) || (localConfig.SqlServer.Trim() == ""))
            {
                StopOnError("Parâmetro 'sqlserver' não localizado no arquivo de configuração 'server.conf'", null);
            }

            if ((localConfig.SqlDb == null) || (localConfig.SqlDb.Trim() == ""))
            {
                StopOnError("Parâmetro 'sqldb' não localizado no arquivo de configuração 'server.conf'", null);
            }

            if ((localConfig.SqlUsername == null) || (localConfig.SqlUsername.Trim() == ""))
            {
                StopOnError("Parâmetro 'sqlusername' não localizado no arquivo de configuração 'server.conf'", null);
            }

            if ((localConfig.SqlPassword == null) || (localConfig.SqlPassword.Trim() == ""))
            {
                StopOnError("Parâmetro 'sqlpassword' não localizado no arquivo de configuração 'server.conf'", null);
            }


            using (IAMDatabase db = new IAMDatabase(localConfig.SqlServer, localConfig.SqlDb, localConfig.SqlUsername, localConfig.SqlPassword))
            {
                try
                {
                    db.openDB();
                }
                catch (Exception ex)
                {
                    StopOnError("Erro on acess database ", ex);
                }

                try
                {
                    ServerKey2 sk = new ServerKey2(db.Connection);
                    sk.RenewCert(db.Connection);

                    Console.WriteLine("Renewed certificate successfully");
                    TextLog.Log("RenewServerCert", "Renewed certificate successfully");
                }
                catch (Exception ex)
                {
                    UnhandledException.WriteEvent(null, ex, false);
                    StopOnError("Error on renew certificate ", ex);
                }
            }

            Console.WriteLine("Pressione ENTER para finalizar");
            Console.ReadLine();
        }
Exemple #4
0
        static void ProcessFile(FileInfo file, DirectoryInfo toPath)
        {
            Console.WriteLine(file.Name);


            FileInfo dstFile = null;

            try
            {
                //Encoding enc = GetEncoding(file);

                List <String> special = new List <String>();
                if (!String.IsNullOrWhiteSpace(localConfig.ClearSpecial))
                {
                    foreach (String s in localConfig.ClearSpecial.Split(",".ToCharArray()))
                    {
                        if (!String.IsNullOrWhiteSpace(s))
                        {
                            special.Add(s.Trim().ToLower());
                        }
                    }
                }

                List <String> divC = new List <String>();
                if (!String.IsNullOrWhiteSpace(localConfig.FolderDivColumn))
                {
                    foreach (String s in localConfig.FolderDivColumn.Split(",".ToCharArray()))
                    {
                        if (!String.IsNullOrWhiteSpace(s))
                        {
                            divC.Add(s.Trim().ToLower());
                        }
                    }
                }

                Dictionary <String, ColData> positions = new Dictionary <String, ColData>();

                Dictionary <String, StringBuilder> filesData = new Dictionary <string, StringBuilder>();
                String header = "";

                List <FileInfo> files = new List <FileInfo>();

                //Byte[] bData = File.ReadAllBytes(file.FullName);

                TextLog.Log("AdpCSV", file.FullName);

                Int64 lineN = 0;


                using (FileStream fs = file.OpenRead())
                    using (StreamReader reader = new StreamReader(fs, Encoding.GetEncoding("iso-8859-1")))
                    {
                        dstFile = null;

                        TextLog.Log("AdpCSV", "reader.EndOfStream? " + reader.EndOfStream);
                        Int32 lines = 0;
                        while (!reader.EndOfStream)
                        {
                            lineN++;

                            try
                            {
                                if (positions.Count == 0)
                                {
                                    TextLog.Log("AdpCSV", "Calc positions");

                                    String line1 = reader.ReadLine();
                                    String line2 = "";
                                    if (!reader.EndOfStream)
                                    {
                                        line2 = reader.ReadLine();
                                    }

                                    lineN++;

                                    if (String.IsNullOrWhiteSpace(line2))
                                    {
                                        break;
                                    }

                                    if (line2.IndexOf("---") == -1)
                                    {
                                        break;
                                    }

                                    String[] columns = line2.Trim().Split(" ".ToCharArray());
                                    TextLog.Log("AdpCSV", "Colunas: " + columns.Length);

                                    Int32 offSet = 0;
                                    foreach (String c in columns)
                                    {
                                        if (c.Length > 0)
                                        {
                                            try
                                            {
                                                String colName = line1.Substring(offSet, c.Length).Trim(" ".ToCharArray());
                                                if (!positions.ContainsKey(colName))
                                                {
                                                    positions.Add(colName, new ColData(offSet, c.Length));
                                                }
                                                else
                                                {
                                                    positions[colName] = new ColData(offSet, c.Length);
                                                }

                                                TextLog.Log("AdpCSV", "Coluna: " + colName);
                                            }
                                            catch (Exception ex)
                                            {
                                                TextLog.Log("AdpCSV", "Erro ao processar a coluna: offSet = " + offSet + ", Length = " + c.Length);
                                            }
                                        }

                                        offSet += c.Length + 1;
                                    }

                                    List <String> cols = new List <String>();

                                    foreach (String k in positions.Keys)
                                    {
                                        cols.Add(k);
                                    }

                                    header = String.Join(",", cols);
                                    //WriteFile(dstFile, String.Join(",",cols));

                                    TextLog.Log("AdpCSV", header);
                                }
                                else
                                {
                                    String line    = reader.ReadLine();
                                    String fprefix = "none";

                                    if (String.IsNullOrWhiteSpace(line))
                                    {
                                        continue;
                                    }

                                    List <String> cols = new List <String>();

                                    foreach (String k in positions.Keys)
                                    {
                                        ColData cd    = positions[k];
                                        String  value = "";
                                        if ((cd.stPos + cd.length) <= line.Length)
                                        {
                                            value = line.Substring(cd.stPos, cd.length).Trim();
                                            if (special.Contains(k.ToLower()))
                                            {
                                                value = ClearText(value);
                                            }

                                            if (divC.Contains(k.ToLower()))
                                            {
                                                fprefix = ClearText(value);
                                            }
                                        }
                                        cols.Add(value);
                                    }


                                    //Build Filename
                                    Boolean writeHeader = false;
                                    dstFile = new FileInfo(Path.Combine(Path.Combine(toPath.FullName, fprefix), file.Name.Replace(file.Extension, "")));
                                    if (!dstFile.Directory.Exists)
                                    {
                                        writeHeader = true;
                                    }
                                    else if ((!dstFile.Exists) || (dstFile.Exists && dstFile.Length == 0))
                                    {
                                        writeHeader = true;
                                    }

                                    if (writeHeader)
                                    {
                                        WriteFile(dstFile, header);
                                    }

                                    WriteFile(dstFile, String.Join(",", cols));
                                    lines++;

                                    if (!files.Exists(f => (f.FullName.ToLower() == dstFile.FullName.ToLower())))
                                    {
                                        files.Add(dstFile);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                TextLog.Log("AdpCSV", "Erro processando linha " + lineN + " em " + file.FullName);
                                UnhandledException.WriteEvent(null, new Exception("Erro processando linha " + lineN + " em " + file.FullName, ex), false);
                                throw ex;
                            }
                        }

                        TextLog.Log("AdpCSV", "Processado " + lines + " linhas");
                    }

                TextLog.Log("AdpCSV", "Movendo arquivo " + file.FullName + " para " + file.FullName + ".imported");
                File.Move(file.FullName, file.FullName + ".imported");

                foreach (FileInfo f in files)
                {
                    if (!f.Directory.Exists)
                    {
                        f.Directory.Create();
                    }

                    TextLog.Log("AdpCSV", "Movendo arquivo " + f.FullName + " para " + f.FullName + ".csv");
                    File.Move(f.FullName, f.FullName + ".csv");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Erro on proccess file '" + file.FullName + "'");
                UnhandledException.WriteEvent(null, new Exception("Erro on proccess file '" + file.FullName + "'", ex), false);

                if (dstFile != null)
                {
                    try
                    {
                        dstFile.Delete();
                    }
                    catch { }
                }
            }
        }