Esempio n. 1
0
        /// <summary>
        /// Proceso de sincronizacion
        /// </summary>
        private void sincroniceKPIs()
        {
            bool local = true;
            this.addText("*****************  INICIANDO PROCESO DE SINCRONIZACIÓN DE KPIS  *****************\n");
            string compartida = System.Configuration.ConfigurationManager.AppSettings["CarpetaCompartida"];
            this.addText("Obteniendo archivos de la carpeta remota \n");
            string[] archivosTerritorio = this.obtenerArchivos(compartida,territorio+"*.zip");
            if (archivosTerritorio.Length == 0)
                this.addText("No se detectaron archivos en la carpeta remota\n");
            setMaxBar(archivosTerritorio.Length);
            Consultas c = new Consultas();
            foreach (string file in archivosTerritorio)
            {
                try
                {
                    FileInfo fkpi = new FileInfo(file);
                    string kpi = fkpi.Name.Substring(fkpi.Name.IndexOf("_") + 1, (fkpi.Name.LastIndexOf("_") - 1 - fkpi.Name.IndexOf("_")));
                    string date = file.Substring(file.LastIndexOf("_")+1, (file.Length-5 - file.LastIndexOf("_")));
                    long dateLong = -1;
                    try
                    {
                        dateLong = Convert.ToInt64(date);
                    }
                    catch { }
                    this.addText("Insertando registro en NMT_TMP_FILES para el KPI "+kpi+"\n");
                    c.InsertNMT_TMP_FILES(file,kpi,territorio,dateLong);
                }
                catch (Exception ex)
                {
                    this.addText("Error insertando registro en NMT_TMP_FILES para el KPI del archivo" + file + "\n" + ex.Message);
                }
                actualProces++;
                Dispatcher.Invoke(updatePbDelegate,
                System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, Convert.ToDouble(actualProces) });
            }
            this.addText("Seleccionando archivos a cargar\n");
            c.CompareNMT_TMP_FILES();
            DataTable kpis = new DataTable();
            kpis = c.SelectNMT_TMP_FILES().Tables[0];
            if (kpis.Rows.Count == 0)
                this.addText("No se detectaron archivos a cargar \n");
            foreach (DataRow g in kpis.Rows)
            {
                string file = (string)g["FULL_FILENAME"];
                string kpi = (string)g["FILENAME_KPI"];
                long dateLong = (long)g["FILENAME_DATE"];
                this.cargaCurrentFiles(file, kpi, territorio, dateLong);
            }
            this.addText("Seleccionando archivos con bit para carga en CURRENT FILES\n");
            DataTable kpisCurrent = new DataTable();
            kpisCurrent = c.SelectNMT_CURRENT_FILES().Tables[0];
            setMaxBar(kpisCurrent.Rows.Count);
            if (kpisCurrent.Rows.Count == 0)
                this.addText("No se detectaron archivos a cargar con bit en 1\n");
            foreach (DataRow g in kpisCurrent.Rows)
            {
                string file = (string)g["FULL_FILENAME"];
                string kpi = (string)g["FILENAME_KPI"];
                long dateLong = (long)g["FILENAME_DATE"];
                DateTime startTime = DateTime.Now;
                try
                {
                    this.addText("Realizando la extracción del .zip\n");
                    List<string> files = this.descomprimir(file);
                    this.addText("Homologando "+kpi+" \n");
                    bool exitoLectura = true;
                    try
                    {
                        string tabla = this.homologue(kpi);
                        DataTable datosTabla = new DataTable();
                        datosTabla = c.CreateTableKPITemp(tabla).Tables[0];
                        this.addText("Iniciando lectura de archivo\n");
                        StreamReader reader = null;
                        try
                        {
                            reader = new StreamReader(files[0]);
                            string line = string.Empty;
                            while ((line = reader.ReadLine()) != null)
                            {
                                try
                                {
                                    string[] valores = line.Split('|');
                                    int index = 0;
                                    string columns = "";
                                    string inserts = "";
                                    bool a = false;
                                    foreach (DataRow gdata in datosTabla.Rows)
                                    {
                                        string column = (string)gdata["COLUMN_NAME"];
                                        if (column == "Ciclo")
                                            a = true;
                                        string tipo = (string)gdata["TYPE_NAME"];
                                        columns += column + ",";
                                        string val = "";

                                        // --INI BZG ADECUACION COMILLAS
                                        if (tipo.ToLower().Contains("char")) val += "{\"}";
                                        // --FIN BZG ADECUACION COMILLAS
                                        if (index < valores.Length) val += valores[index];
                                        // --INI BZG ADECUACION COMILLAS
                                        if (tipo.ToLower().Contains("char")) val += "{\"}";
                                        // --FIN BZG ADECUACION COMILLAS
                                        if (a == true)
                                            val = val.Substring(0, 6);

                                        inserts += val + ",";
                                        index++;
                                        a = false;
                                    }
                                    try
                                    {
                                        c.ExecuteSQLNMT_TMP_FILES(tabla, columns.Remove(columns.Length - 1, 1), inserts.Remove(inserts.Length - 1, 1),local);
                                    }
                                   catch (Exception ex2)
                                    {
                                        this.addText("Error insertando los datos en la tabla temporal\n" + ex2.Message);
                                        this.addText("Registro " + inserts.Remove(inserts.Length - 1, 1) + "\n");
                                        exitoLectura = exitoLectura && false;
                                    }
                                }
                                catch (Exception ex3)
                                {
                                    this.addText("Error mapeando columnas de tabla con columnas del archivo\n" + ex3.Message);
                                    exitoLectura = exitoLectura && false;
                                }
                            }
                            reader.Close();
                        }
                        catch (Exception ex4)
                        {
                            this.addText("Error en la lectura del archivo "+file+"\n" + ex4.Message);
                            if (reader != null)
                                reader.Close();
                            exitoLectura = exitoLectura && false;
                        }
                        if (exitoLectura)
                        {
                            this.addText("Tabla temporal " + tabla + "_TMP creada y cargada\n");
                            this.addText("Truncando la tabla " + tabla + "\n");
                            try
                            {
                                c.Truncate_NMT_Table(tabla, local);
                                this.addText("Tabla " + tabla + " truncada\n");
                                this.addText("Cargando los datos desde la tabla temporal " + tabla + "_TMP\n");
                                try
                                {
                                    c.Merge_NMT_Table(tabla + "_TMP", tabla, local);
                                    this.addText("Carga de datos desde la tabla temporal " + tabla + "_TMP exitosa\n");
                                    //--------- BZG
                                    c.SetLoadedNMT_CURRENT_FILES(kpi);
                                    //---------
                                    try
                                    {
                                        this.addText("Insertando a bitacora\n");
                                        DateTime stopTime = DateTime.Now;
                                        TimeSpan duration = stopTime - startTime;
                                        double totalSeconds = (double)stopTime.Ticks / TimeSpan.TicksPerSecond;
                                        string dateFormat = stopTime.ToString("yyyyMMddHHmmssffff");
                                        long fecha_sin_int = Convert.ToInt64(dateFormat);
                                        int duracion = Convert.ToInt32(duration.TotalSeconds);
                                        c.InsertVitacora(usuario, WindowsIdentity.GetCurrent().Name, DateTime.Now, versionCliente, duracion, kpi, territorio, fecha_sin_int, true);
                                    }
                                    catch (Exception ex5)
                                    {
                                        this.addText("Error en la inserción a bitacora\n" + ex5.Message);
                                    }
                                    this.addText("Eliminando tabla temporal " + tabla + "_TMP \n");
                                    try
                                    {
                                        c.Drop_NMT_Table(tabla + "_TMP", local);
                                    }
                                    catch (Exception ex6)
                                    {
                                        this.addText("Error eliminando la tabla temporal " + tabla + "_TMP\n" + ex6.Message);
                                    }
                                }
                                catch (Exception ex7)
                                {
                                    this.addText("Error al intentar cargar los datos desde la tabla temporal " + tabla + "_TMP\n" + ex7.Message);
                                }
                            }
                            catch (Exception ex8)
                            {
                                this.addText("Error al intentar truncar la tabla\n" + ex8.Message);
                            }
                        }
                        else
                        {
                            try
                            {
                                c.Drop_NMT_Table(tabla + "_TMP", local);
                            }
                            catch (Exception ex9)
                            {
                                this.addText("Error eliminando la tabla temporal " + tabla + "_TMP\n" + ex9.Message);
                            }
                        }
                    }
                    catch (Exception ex10)
                    {
                        this.addText("Ocurrió un error tratando de crear la tabla temporal\n" + ex10.Message);
                    }

                }
                catch(Exception ex11)
                {
                    this.addText("Ocurrió un error en la extracción del .zip\n" + ex11.Message);
                }
                actualProces++;
                Dispatcher.Invoke(updatePbDelegate,
                System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, Convert.ToDouble(actualProces) });
            }
        }