Esempio n. 1
0
        public override PluginConnectorBaseFetchResult FetchFields(Dictionary <String, Object> config)
        {
            PluginConnectorBaseFetchResult ret = new PluginConnectorBaseFetchResult();

            LogEvent iLog = new LogEvent(delegate(Object sender, PluginLogType type, string text)
            {
                if (Log != null)
                {
                    Log(sender, type, text);
                }
            });


            if (!CheckInputConfig(config, true, iLog, true, true))
            {
                ret.success = false;
                return(ret);
            }

            List <PluginConfigFields> cfg = new List <PluginConfigFields>();

            PluginConfigFields[] tmpF = this.GetConfigFields();
            foreach (PluginConfigFields cf in tmpF)
            {
                try
                {
                    iLog(this, PluginLogType.Information, "Field " + cf.Name + " (" + cf.Key + "): " + (config.ContainsKey(cf.Key) ? config[cf.Key].ToString() : "empty"));
                }
                catch (Exception ex)
                {
                    iLog(this, PluginLogType.Information, "Field " + cf.Name + " (" + cf.Key + "): error on get data -> " + ex.Message);
                }
            }


            String table = config["sheet"].ToString();

            table = table.Trim("$ []".ToCharArray());

            try
            {
                DirectoryInfo importDir = null;;
                try
                {
                    importDir = new DirectoryInfo(config["import_folder"].ToString());
                    if (!importDir.Exists)
                    {
                        throw new DirectoryNotFoundException();
                    }
                }
                catch (Exception ex)
                {
                    iLog(this, PluginLogType.Error, "Erro ao localizar o diretório de importação (" + config["import_folder"].ToString() + "): " + ex.Message);
                    return(ret);
                }

                foreach (FileInfo f in importDir.GetFiles("*.xls"))
                {
                    iLog(this, PluginLogType.Information, "Iniciando mapeamento do arquivo '" + f.Name + "'");

                    OdbcDB db = null;
                    try
                    {
                        db = new OdbcDB(f);
                        db.openDB();


                        iLog(this, PluginLogType.Error, "Listatando schema da tabela...");
                        DataTable dtSchema = db.GetSchema(table);

                        if (dtSchema == null)
                        {
                            throw new Exception("Erro ao listar o schema da tabela: " + db.LastError);
                        }

                        try
                        {
                            foreach (DataColumn dc in dtSchema.Columns)
                            {
                                if (!ret.fields.ContainsKey(dc.ColumnName))
                                {
                                    try
                                    {
                                        ret.fields.Add(dc.ColumnName, new List <string>());

                                        iLog(this, PluginLogType.Information, "Column " + dc.ColumnName + ": DataType=" + dc.DataType.ToString() + ", MaxLength=" + dc.MaxLength + ", AllowDBNull=" + dc.AllowDBNull);
                                    }
                                    catch (Exception ex)
                                    {
                                        iLog(this, PluginLogType.Information, "Column " + dc.ColumnName + ": error on get data -> " + ex.Message);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            iLog(this, PluginLogType.Error, "Erro ao listar as colunas: " + ex.Message);
                        }


                        String sql = "select * from [" + table + "$]";

                        DataTable dtSource = db.Select(sql, 0, 10);

                        if (dtSource == null)
                        {
                            throw new Exception("Erro on select: " + db.LastError);
                        }

                        try
                        {
                            foreach (DataColumn dc in dtSource.Columns)
                            {
                                if (!ret.fields.ContainsKey(dc.ColumnName))
                                {
                                    ret.fields.Add(dc.ColumnName, new List <string>());
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            iLog(this, PluginLogType.Error, "Erro ao listar as colunas: " + ex.Message);
                        }

                        Int32 qty = 0;

                        foreach (DataRow dr in dtSource.Rows)
                        {
                            qty++;

                            if (qty < 10)
                            {
                                break;
                            }

                            String regId = Guid.NewGuid().ToString();

                            try
                            {
                                foreach (DataColumn dc in dtSource.Columns)
                                {
                                    if (!ret.fields.ContainsKey(dc.ColumnName))
                                    {
                                        ret.fields.Add(dc.ColumnName, new List <string>());
                                    }

                                    ret.fields[dc.ColumnName].Add(dr[dc.ColumnName].ToString());
                                }
                            }
                            catch (Exception ex)
                            {
                                iLog(this, PluginLogType.Error, "Erro ao importar o registro: " + ex.Message);
                            }
                        }

                        ret.success = true;
                    }
                    catch (Exception ex)
                    {
                        iLog(this, PluginLogType.Error, "Falha ao mapear os dados do arquivo '" + f.Name + "': " + ex.Message);
                    }
                    finally
                    {
                        if (db != null)
                        {
                            db.closeDB();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                iLog(this, PluginLogType.Error, ex.Message);
            }

            return(ret);
        }
Esempio n. 2
0
        public override void ProcessImport(String cacheId, String importId, Dictionary <String, Object> config, List <PluginConnectorBaseDeployPackageMapping> fieldMapping)
        {
            if (!CheckInputConfig(config, true, Log))
            {
                return;
            }


            String table = config["sheet"].ToString();

            table = table.Trim("$ []".ToCharArray());

            try
            {
                DirectoryInfo importDir = null;;
                try
                {
                    importDir = new DirectoryInfo(config["import_folder"].ToString());
                    if (!importDir.Exists)
                    {
                        throw new DirectoryNotFoundException();
                    }
                }
                catch (Exception ex)
                {
                    Log2(this, PluginLogType.Error, 0, 0, "Erro ao localizar o diretório de importação (" + config["import_folder"].ToString() + ")", ex.Message);
                    return;
                }

                foreach (FileInfo f in importDir.GetFiles("*.xls"))
                {
                    OdbcDB db = null;
                    try
                    {
                        db = new OdbcDB(f);
                        db.openDB();

                        String sql = "select * from [" + table + "$]";

                        DataTable dtSource = db.Select(sql);

                        if (dtSource == null)
                        {
                            throw new Exception("Erro on select: " + db.LastError);
                        }

                        foreach (DataRow dr in dtSource.Rows)
                        {
                            PluginConnectorBaseImportPackageUser package = new PluginConnectorBaseImportPackageUser(importId);
                            try
                            {
                                foreach (DataColumn dc in dtSource.Columns)
                                {
                                    package.AddProperty(dc.ColumnName, dr[dc.ColumnName].ToString(), dc.DataType.ToString());
                                }

                                ImportPackageUser(package);
                            }
                            catch (Exception ex)
                            {
                                Log(this, PluginLogType.Error, "Erro ao importar o registro: " + ex.Message);
                            }
                            finally
                            {
                                package.Dispose();
                                package = null;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log2(this, PluginLogType.Error, 0, 0, "Falha ao mapear os dados do arquivo '" + f.Name + "'", ex.Message);
                    }
                    finally
                    {
                        if (db != null)
                        {
                            db.Dispose();
                        }
                    }


                    f.MoveTo(f.FullName + ".imported");

                    Log(this, PluginLogType.Information, "Importação do arquivo '" + f.Name + "' concluida");
                }
            }
            catch (Exception ex)
            {
                Log2(this, PluginLogType.Error, 0, 0, "Error on process import: " + ex.Message, "");
                Log(this, PluginLogType.Error, ex.Message);
            }
        }