Example #1
0
        // Generar sectorización recibida desde SACTA
        public GeneraSectorizacionDll.Sectorization GeneraSectorizacion(SactaInfo info, DateTime mementoActivacion)
        {
            GeneraSectorizacionDll.Sectorization generaSectorizacion = null;
            bool releaseSem = false;

            info["Resultado"] = 0;

            try
            {
                releaseSem = SemaforoSectorizacionSacta.WaitOne(60000, false);
                if (!releaseSem)
                {
                    throw new SectorizationException(SectorizationResult.TimeOutError, "SectorizationResult.TimeOutError");
                }

                lock (_Sync)
                {
                    GestorBDCD40.ConexionMySql.Open();
                    using (MySqlCommand myCommand = GestorBDCD40.ConexionMySql.CreateCommand())
                    {
                        generaSectorizacion = new GeneraSectorizacionDll.Sectorization(myCommand, (string)info["IdSistema"], (string)info["SectName"], (string)info["SectData"], 'C', 'P', Tipo_Sectorizacion.Sectorizacion_Completa, mementoActivacion, true);
                    }
                }
            }
            catch (SectorizationException x)
            {
                info["Resultado"]  = 1;
                info["ErrorCause"] = x.Message;
            }
            catch (System.Data.SqlClient.SqlException sqlException)
            {
                info["Resultado"] = 1;
                System.Diagnostics.Debug.Assert(false, sqlException.Number.ToString());
                info["ErrorCause"] = sqlException.Message;
            }
            catch (Exception ex)
            {
                info["Resultado"] = 1;
                System.Diagnostics.Debug.Assert(false, ex.Message);
                info["ErrorCause"] = ex.Message;
            }
            finally
            {
                SemaforoSectorizacionSacta.Release();
                GestorBDCD40.ConexionMySql.Close();
            }

            EventResultSectorizacion(info);

            return((int)info["Resultado"] == 0 ? generaSectorizacion : null);
        }
Example #2
0
        public SectorizationResult GeneraSectorizacion(string idSistema, string idSectorizacion, string data)
        {
            Dictionary <int, InfoPosicion> infoSectorizacion = new Dictionary <int, InfoPosicion>();

            //            if (null != CadenaConexion)
            //            {
            try
            {
                #region
                //                    using (MySqlConnection mySqlConnectionToCd40 = new MySqlConnection(CadenaConexion.ToString()))
                //					using (MySqlCommand myCommand = mySqlConnectionToCd40.CreateCommand())
                using (MySqlCommand myCommand = GestorBDCD40.ConexionMySql.CreateCommand())
                {
                    GestorBDCD40.ConexionMySql.Open();

                    //MySqlTransaction mytrans = mySqlConnectionToCd40.BeginTransaction(IsolationLevel.ReadCommitted);
                    MySqlTransaction mytrans = GestorBDCD40.StartTransaction(true);
                    myCommand.Transaction = mytrans;

                    if ((idSectorizacion == null) || (idSectorizacion.Length == 0))
                    {
                        GestorBDCD40.RollBack(mytrans);
                        //mytrans.Rollback();
                        mytrans.Dispose();
                        return(GeneraSectorizacionDll.SectorizationResult.Error);
                    }
                    string[] sectorUcs = data.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
                    if (sectorUcs.Length % 2 != 0)
                    {
                        GestorBDCD40.RollBack(mytrans);
                        //mytrans.Rollback();
                        mytrans.Dispose();
                        return(GeneraSectorizacionDll.SectorizationResult.Error);
                    }
                    if (sectorUcs.Length == 0)
                    {
                        GestorBDCD40.RollBack(mytrans);
                        //mytrans.Rollback();
                        mytrans.Dispose();
                        return(GeneraSectorizacionDll.SectorizationResult.EmptySectorization);
                    }

                    myCommand.CommandText = "DELETE FROM Sectorizaciones WHERE IdSistema='" + idSistema +
                                            "' AND IdSectorizacion='" + idSectorizacion + "'";
                    myCommand.ExecuteNonQuery();
                    // Comprobar que el IdSectorizacion existe en la tabla Sectorizaciones.
                    myCommand.CommandText = "SELECT COUNT(*) FROM Sectorizaciones WHERE IdSistema='" + idSistema +
                                            "' AND IdSectorizacion='" + idSectorizacion + "'";
                    object objCount = myCommand.ExecuteScalar();
                    if ((objCount != null) && (Int32.Parse(objCount.ToString()) > 0))
                    {   //Eliminar la asignacion de sectores a posiciones
                        myCommand.CommandText = "DELETE FROM SectoresSectorizacion WHERE IdSistema='" + idSistema +
                                                "' AND IdSectorizacion='" + idSectorizacion + "'";
                        myCommand.ExecuteNonQuery();
                    }
                    else
                    { //La sectorizacion no existe, intentar crearla
                        myCommand.CommandText = "INSERT INTO Sectorizaciones SET IdSistema='" + idSistema + "',IdSectorizacion='" + idSectorizacion + "'";
                        myCommand.ExecuteNonQuery();
                    }

                    for (int i = 0, total = sectorUcs.Length; i < total; i += 2)
                    {
                        InfoSector elSector = new InfoSector();
                        elSector.numSacta = Int32.Parse(sectorUcs[i]);
                        InfoPosicion laPosicion;
                        bool         nuevaPosicion = false;
                        if (!infoSectorizacion.TryGetValue(Int32.Parse(sectorUcs[i + 1]), out laPosicion))
                        {
                            laPosicion    = new InfoPosicion();
                            nuevaPosicion = true;
                        }
                        laPosicion.numPosicion = Int32.Parse(sectorUcs[i + 1]);

                        //Obtener el IdTop que ocupa esa posicion
                        myCommand.CommandText = "SELECT IdTop FROM TOP WHERE IdSistema='" + idSistema +
                                                "' AND PosicionSacta=" + laPosicion.numPosicion.ToString();
                        MySqlDataReader dr = myCommand.ExecuteReader();
                        if (dr.HasRows)
                        {
                            if (dr.Read())
                            {
                                laPosicion.idPosicion = dr.GetString(0);
                            }
                        }
                        else
                        {
                            dr.Close();
                            GestorBDCD40.RollBack(mytrans);
                            //mytrans.Rollback();
                            mytrans.Dispose();
                            return(GeneraSectorizacionDll.SectorizationResult.Error);
                        }
                        dr.Close();
                        //Obtener los datos del sector
                        myCommand.CommandText = "SELECT IdNucleo,IdSector FROM Sectores WHERE IdSistema='" + idSistema +
                                                "' AND NumSacta=" + elSector.numSacta.ToString();
                        MySqlDataReader dr1 = myCommand.ExecuteReader();
                        //string idNucleo = "";
                        //string idSector = "";
                        if (dr1.HasRows)
                        {
                            if (dr1.Read())
                            {
                                elSector.idNucleo = dr1.GetString(0);
                                elSector.idSector = dr1.GetString(1);
                            }
                        }
                        else
                        {
                            dr1.Close();
                            GestorBDCD40.RollBack(mytrans);
                            //mytrans.Rollback();
                            mytrans.Dispose();
                            return(GeneraSectorizacionDll.SectorizationResult.Error);
                        }
                        dr1.Close();

                        if (laPosicion.sectores == null)
                        {
                            laPosicion.sectores = new List <InfoSector>();
                            elSector.dominante  = true;
                        }
                        else
                        {
                            elSector.dominante = false;
                        }
                        laPosicion.sectores.Add(elSector);

                        if (nuevaPosicion)
                        {
                            infoSectorizacion.Add(laPosicion.numPosicion, laPosicion);
                        }
                    }

                    foreach (KeyValuePair <int, InfoPosicion> kvp in infoSectorizacion)
                    {
                        if (kvp.Value.sectores.Count == 1)
                        {//la posicion solo tiene un sector
                            //Insertar los datos del sector de la posicion en SectoresSectorizacion
                            myCommand.CommandText = "INSERT INTO SectoresSectorizacion VALUES ('" + idSistema + "','" + idSectorizacion + "','" +
                                                    kvp.Value.sectores[0].idNucleo + "','" + kvp.Value.sectores[0].idSector + "','" + kvp.Value.idPosicion + "')";
                            myCommand.ExecuteNonQuery();
                        }
                        else
                        { //Obtener el nombre de la posicion
                            System.Text.StringBuilder lista = new System.Text.StringBuilder();
                            for (int i = 0; i < kvp.Value.sectores.Count; i++)
                            {
                                lista.AppendFormat("'{0}',", kvp.Value.sectores[i].idSector);
                            }
                            lista = lista.Remove(lista.Length - 1, 1);
                            string nomAgrupacion = GetAgrupacion(kvp.Value.sectores.Count, lista.ToString());
                            if (nomAgrupacion == null)
                            {
                                lista.Remove(0, lista.Length);
                                for (int i = 0; i < kvp.Value.sectores.Count; i++)
                                {
                                    lista.AppendFormat("{0},", kvp.Value.sectores[i].idSector);
                                }
                                lista         = lista.Remove(lista.Length - 1, 1);
                                nomAgrupacion = GeneraAlgoritmo(idSistema, lista.ToString(), mytrans);
                            }
                            InfoPosicion po;
                            if (infoSectorizacion.TryGetValue(kvp.Key, out po))
                            {
                                po.nombreResultante = nomAgrupacion;
                            }
                            //((InfoPosicion)infoSectorizacion[kvp.Key]).nombreResultante = nomAgrupacion;
                            //Comprobar si existe ese nombre en la tabla SECTORES
                            myCommand.CommandText = "SELECT COUNT(*) FROM Sectores WHERE IdSistema='" + idSistema +
                                                    "' AND IdSector='" + nomAgrupacion + "'";
                            object objCount1 = myCommand.ExecuteScalar();
                            if ((objCount1 != null) && (Int32.Parse(objCount1.ToString()) > 0))
                            {
                            }
                            else
                            {//no existe el sector => crearlo
                                //myCommand.CommandText = "INSERT INTO Sectores VALUES ('" + idSistema + "','" + kvp.Value.sectores[0].idNucleo +
                                //        "','" + po.nombreResultante + "','','','',0,'R','C',4,0,0)";
                                myCommand.CommandText = "INSERT INTO Sectores SET Idsistema='" + idSistema + "',IdNucleo='" + kvp.Value.sectores[0].idNucleo
                                                        + "',IdSector='" + po.nombreResultante + "',SectorSimple=0,Tipo='R',TipoPosicion='C',PrioridadR2=4,TipoHMI=0,NumSacta=0";
                                myCommand.ExecuteNonQuery();
                                //SectoresSector
                                foreach (InfoSector sector in kvp.Value.sectores)
                                {
                                    myCommand.CommandText = "INSERT INTO SectoresSector VALUES ('" + idSistema + "','" + sector.idNucleo + "','" +
                                                            po.nombreResultante + "','" + sector.idSector + "'," + (sector.dominante ? "1" : "0") + ")";
                                    myCommand.ExecuteNonQuery();
                                }
                            }
                            //Actualizar SectoresSectorizacion
                            myCommand.CommandText = "INSERT INTO SectoresSectorizacion VALUES ('" + idSistema + "','" + idSectorizacion + "','" +
                                                    kvp.Value.sectores[0].idNucleo + "','" + po.nombreResultante + "','" + kvp.Value.idPosicion + "')";
                            myCommand.ExecuteNonQuery();
                        }
                    }

                    //						GestorBDCD40.Commit(mytrans);
                    //mytrans.Commit();
                    //                        mytrans.Dispose();

                    try
                    {
                        GeneraSectorizacionDll.Sectorization generaSectorizacion = new GeneraSectorizacionDll.Sectorization(myCommand, idSistema, idSectorizacion, data, 'C', 'P', Tipo_Sectorizacion.Sectorizacion_Completa, DateTime.Now, true);
                        // DateTime now = DateTime.Now;
                        // CrearSectorizacionActiva(idSistema, idSectorizacion, now);
                        GestorBDCD40.Commit(mytrans);
                    }
                    catch (SectorizationException)
                    {
                        //sException.Result==SectorizationResult.EmptySectorization
                    }
                    catch (System.Data.SqlClient.SqlException sqlException)
                    {
                        System.Diagnostics.Debug.Assert(false, sqlException.Number.ToString());
                    }
                    finally
                    {
                        //mytrans.Commit();
                        mytrans.Dispose();
                    }
                }
                #endregion
            }
            catch (MySqlException)
            {
                return(GeneraSectorizacionDll.SectorizationResult.Error);
            }
            // }

            return(GeneraSectorizacionDll.SectorizationResult.Ok);
        }
Example #3
0
        //(Description = "Servicio encargado de obtener los datos necesarios para la elaboración de la sectorización")
        public SectorizationResult GeneraSectorizacion(string idSistema, string idSectorizacion, Tipo_Sectorizacion tipoSectorizacion, bool comprobarSectoresReales, bool estadoSacta = false)
        {
            string data = "";

            using (MySqlCommand myCommand = GestorBDCD40.ConexionMySql.CreateCommand())
            {
                GestorBDCD40.ConexionMySql.Open();
                MySqlTransaction trans = GestorBDCD40.StartTransaction(true);
                myCommand.Transaction = trans;

                try
                {
                    if (estadoSacta)
                    {
                        myCommand.CommandText = "SELECT sc.NumSacta,t.PosicionSacta	" +
                                                "FROM sectoressector ss, sectoressectorizacion s, sectores sc, top t, sectorizaciones z " +
                                                "WHERE z.Activa='1' AND " +
                                                "s.IdSectorizacion=z.IdSectorizacion AND " +
                                                "s.IdSistema='" + idSistema + "' AND " +
                                                "sc.NumSacta != 20000 AND " +
                                                "ss.IdSistema=s.IdSistema AND " +
                                                "ss.IdNucleo=s.IdNucleo AND " +
                                                "ss.IdSector=s.IdSector AND " +
                                                "t.IdSistema=s.IdSistema AND " +
                                                "t.IdTop=s.IdTOP AND " +
                                                "sc.IdSistema=s.IdSistema AND " +
                                                "sc.IdNucleo=s.IdNucleo AND " +
                                                "sc.IdSector=ss.IdSectorOriginal UNION " +
                                                "SELECT sc.NumSacta,t.PosicionSacta	" +
                                                "FROM sectoressector ss, sectoressectorizacion s, sectores sc, top t " +
                                                "WHERE s.IdSectorizacion='" + idSectorizacion + "' AND " +
                                                "s.IdSistema='" + idSistema + "' AND " +
                                                "(sc.NumSacta >= 10000 AND sc.NumSacta < 20000) AND " +
                                                "ss.IdSistema=s.IdSistema AND " +
                                                "ss.IdNucleo=s.IdNucleo AND " +
                                                "ss.IdSector=s.IdSector AND " +
                                                "t.IdSistema=s.IdSistema AND  " +
                                                "t.IdTop=s.IdTOP AND  " +
                                                "sc.IdSistema=s.IdSistema AND  " +
                                                "sc.IdNucleo=s.IdNucleo AND  " +
                                                "sc.IdSector=ss.IdSectorOriginal AND " +
                                                "t.PosicionSacta not in (SELECT t.PosicionSacta	" +
                                                "FROM sectoressector ss, sectoressectorizacion s, sectores sc, top t , sectorizaciones z " +
                                                "WHERE z.Activa='1' AND " +
                                                "s.IdSectorizacion=z.IdSectorizacion AND " +
                                                "s.IdSistema='" + idSistema + "' AND " +
                                                "sc.NumSacta < 10000 AND " +
                                                "ss.IdSistema=s.IdSistema AND " +
                                                "ss.IdNucleo=s.IdNucleo AND " +
                                                "ss.IdSector=s.IdSector AND " +
                                                "t.IdSistema=s.IdSistema AND " +
                                                "t.IdTop=s.IdTOP AND " +
                                                "sc.IdSistema=s.IdSistema AND " +
                                                "sc.IdNucleo=s.IdNucleo AND " +
                                                "sc.IdSector=ss.IdSectorOriginal) " +
                                                "order by numsacta";
                    }
                    else
                    {
                        myCommand.CommandText = "SELECT sc.NumSacta,t.PosicionSacta	" +
                                                "FROM sectoressector ss, sectoressectorizacion s, sectores sc, top t " +
                                                "WHERE s.IdSectorizacion='" + idSectorizacion + "' AND " +
                                                "s.IdSistema='" + idSistema + "' AND " +
                                                "sc.NumSacta != 20000 AND " +
                                                "ss.IdSistema=s.IdSistema AND " +
                                                "ss.IdNucleo=s.IdNucleo AND " +
                                                "ss.IdSector=s.IdSector AND " +
                                                "t.IdSistema=s.IdSistema AND " +
                                                "t.IdTop=s.IdTOP AND " +
                                                "sc.IdSistema=s.IdSistema AND " +
                                                "sc.IdNucleo=s.IdNucleo AND " +
                                                "sc.IdSector=ss.IdSectorOriginal ORDER BY sc.NumSacta";
                    }

                    using (MySqlDataReader dr = myCommand.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            data += string.Format("{0},{1};", dr.GetUInt32(0), dr.GetUInt32(1));
                        }
                    }

                    GeneraSectorizacionDll.Sectorization generaSectorizacion = new GeneraSectorizacionDll.Sectorization(myCommand, idSistema, idSectorizacion, data, 'C', 'P', tipoSectorizacion, DateTime.Now, comprobarSectoresReales);
                    GestorBDCD40.Commit(trans);
                }
                catch (SectorizationException sEx)
                {
                    GestorBDCD40.RollBack(trans);
                }
                catch (System.Data.SqlClient.SqlException sqlException)
                {
                    GestorBaseDatos.logFile.Error("Utilidades.GeneraSectorizacion SqlException Error:", sqlException);
                    GestorBaseDatos.logFile.Error("En la consulta " + myCommand.CommandText);
                    GestorBDCD40.RollBack(trans);

                    System.Diagnostics.Debug.Assert(false, sqlException.Number.ToString());
                }
                catch (SystemException objEx)
                {
                    GestorBaseDatos.logFile.Error("Utilidades.GeneraSectorizacion--> SystemException");
                    GestorBaseDatos.logFile.Error("Error:", objEx);
                }
                finally
                {
                    GestorBDCD40.ConexionMySql.Close();
                }
            }

            return(GeneraSectorizacionDll.SectorizationResult.Ok);
        }