Example #1
0
        /* Descripcion: Llena los datos del proyecto en las casillas correspondientes
           *
           * REQ:EntidadProyecto
           *
           * RET: N/A
           */
        public void llenaDatosProyecto(EntidadProyecto proy)
        {
            try
            {
                nombreProyecto.Value = proy.getNombre();
                objetivo.Text = proy.getObjetivo();
                string estado = (proy.getEstado()).ToString();
                barraEstado.Items.Clear();
                lider.Items.Clear();
                DateTime dt = proy.getFecha();
                calendario.Value = dt.ToString("yyy-MM-dd", CultureInfo.InvariantCulture);

                //concatena la cedula y el nombre del lider
                string liderP = (proy.getLider()).ToString();
                string nombreL = proy.getNombreLider();
                lider.Items.Add(new ListItem(liderP + " " + nombreL));

                barraEstado.Items.Add(new ListItem(estado));
                nombreOficina.Value = proy.getNomOf();
                correoOficina.Value = proy.getCorreoOf();
                if (proy.getTelOf() != 0)
                {
                    telefonoOficina.Value = (proy.getTelOf()).ToString();
                }
                else
                {
                    telefonoOficina.Value = "";
                }
                //si hay un segundo telefono lo carga tambien, sino solo muestra el primero y no habilita el boton de mostrar el segundo.
                int num = proy.getTelOf2();
                if (num != 0)
                {
                    tel2.Value = (num).ToString();
                    btnTel2.Disabled = false;
                    tel2.Visible = true;
                }
                else
                {
                    btnTel2.Disabled = true;
                }
                representante.Value = proy.getRep();
            }
            catch
            {

            }
        }
        public EntidadProyecto actualizaProyecto(Object[] nuevos, Object[] originales)
        {
            Object[] dato = new Object[11];
            EntidadProyecto nP = new EntidadProyecto(nuevos);
            EntidadProyecto en = null;

            string nombre = nP.getNombre();
            string objetivo = nP.getObjetivo();
            string estado = nP.getEstado();
            DateTime fecha = nP.getFecha();
            string nombreOf = nP.getNomOf();
            string representante = nP.getRep();
            string correoOf = nP.getCorreoOf();
            int telefonoOf = nP.getTelOf();
            int lider = nP.getLider();
            string nombreLider = nP.getNombreLider();
            int tel2 = nP.getTelOf2();

            string fec = fecha.ToString("yyy-MM-dd", CultureInfo.InvariantCulture);
            string nombreO = (String)originales[0];
            string lOrig = originales[1].ToString();
            string telViejo1 = originales[2].ToString();
            string telViejo2 = originales[3].ToString();

            int idP = traerId(nombreO);//obtiene el idProyecto
            try
            {
                string consulta = "Update Proyecto Set nombre ='" + nombre + "', objetivo ='" + objetivo + "',estado ='" + estado + "', fechaAsignacion= '" + fec + "'" + " where nombre = '" + nombreO + "';";
                SqlDataReader reader = baseDatos.ejecutarConsulta(consulta);
                reader.Close();
            }
            catch (Exception e)
            {
                throw e;
            }

            try
            {
                string consulta2 = "Update OficinaUsuaria Set representante ='" + representante + "', nombre ='" + nombreOf + "',correo ='" + correoOf + "'" +
                "where id= " + idP + "; ";
                SqlDataReader reader = baseDatos.ejecutarConsulta(consulta2);
                reader.Close();
            }
            catch (Exception e)
            {
                throw e;
            }

            try
            {
                string consulta4 = "Update Usuario Set idProy =  null  where cedula = " + lOrig + ";";
                SqlDataReader reader = baseDatos.ejecutarConsulta(consulta4);
                reader.Close();
            }
            catch (Exception e)
            {
                throw e;
            }

            try
            {
                string consulta3 = "Update Usuario Set idProy =" + idP + "where cedula = " + lider + ";";
                SqlDataReader reader = baseDatos.ejecutarConsulta(consulta3);
                reader.Close();
            }
            catch (Exception e)
            {
                throw e;
            }

            try
            {
                if (telefonoOf != 0 && !(telViejo1.Equals("")))
                {
                    string consultaTel1 = "Update TelefonoOficina Set numero ='" + telefonoOf + "' where idCliente= '" + idP + "' and numero = '" + telViejo1 +"'";
                    SqlDataReader res = baseDatos.ejecutarConsulta(consultaTel1);
                    res.Close();
                }

                if (telViejo1.Equals("") && telefonoOf != 0)
                {
                    string consulta = "INSERT INTO TelefonoOficina(numero,idCliente) VALUES (@0, @1);";
                    Object[] args = new Object[2];
                    args[0] = telefonoOf;
                    args[1] = idP;
                    SqlDataReader res = baseDatos.ejecutarConsulta(consulta, args);
                    res.Close();
                }
            }
            catch(SqlException ex)
            {
                throw ex;
            }

            try
            {
                if (tel2 != 0 && !(telViejo2.Equals("")))
                {
                    string consultaTel2 = "Update TelefonoOficina Set numero ='" + tel2 + "' where idCliente= '" + idP + "' and numero = '" + telViejo2 + "'";
                    SqlDataReader res = baseDatos.ejecutarConsulta(consultaTel2);
                    res.Close();
                }

                if ( telViejo2.Equals("") && tel2 != 0)
                {
                    string consulta = "INSERT INTO TelefonoOficina(numero,idCliente) VALUES (@0, @1);";
                    Object[] args = new Object[2];
                    args[0] = tel2;
                    args[1] = idP;
                    SqlDataReader res = baseDatos.ejecutarConsulta(consulta, args);
                    res.Close();
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            try
            {
                //string consulta5 = "select  p.objetivo,p.estado, p.fechaAsignacion, o.nombre, o.representante, o.correo, l.cedula, CONCAT(l.pNombre,' ',l.pApellido,' ',l.sApellido) , tel.numero from Proyecto p, OficinaUsuaria o, TelefonoOficina tel, Usuario l where p.nombre = '" + nombre + "' and p.id = o.idProyecto and tel.idCliente = o.id and l.idProy=p.id and l.rol='Lider';";
                string consulta5 = "select p.objetivo, p.estado,p.fechaAsignacion,o.nombre, o.representante, o.correo,u.cedula,Concat(u.pNombre, ' ', u.pApellido, ' ', u.sApellido), t.numero from Usuario u Left Join Proyecto p ON u.idProy = p.id Left Join OficinaUsuaria o on p.id = o.idProyecto Left Join TelefonoOficina t on t.idCliente = o.id where p.nombre = '" + nombre + "' and u.rol = 'Lider'";

                SqlDataReader reader = baseDatos.ejecutarConsulta(consulta5);
                if (reader.Read())
                {
                    //cuando hay solo un telefono
                    dato[0] = nombre;              //nombre
                    dato[1] = reader.GetString(0); //objetivo
                    dato[2] = reader.GetString(1); //estado
                    dato[3] = reader.GetDateTime(2);  //fechaAsgnacion
                    dato[4] = reader.GetString(3); //nombreOficina
                    dato[5] = reader.GetString(4); // representante
                    dato[6] = reader.GetString(5); //cooreoOficina
                    if (!reader.IsDBNull(8))
                    {
                        dato[7] = reader.GetInt32(8);//Convert.ToInt32(reader.GetString(6)); //telOficina
                    }
                    else
                    {
                        dato[7] = 0;
                    }
                    //dato[7] = reader.GetInt32(8); //telOficina
                    dato[8] = reader.GetInt32(6);//cedula lider
                    dato[9] = reader.GetString(7);//nombreLider
                    dato[10] = null;//tel2
                }
                if (reader.Read())
                {
                    //cuando hay dos telefonos
                    dato[10] = reader.GetInt32(8);//tel2
                }
                reader.Close();
                en = new EntidadProyecto(dato);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return en;
        }
        /** Descripcion: Inserta el proyecto en la base de datos junto con la oficina y sus telefonos
         *
         * REQ: EntidadProyecto
         *
         * RET: string
         */
        public string insertarProyecto(EntidadProyecto proyecto)
        {
            string resultado = "Exito";
            int idP = 0;
            int idOf = 0;
            string consulta = "";

            try
            {
                consulta = "INSERT INTO Proyecto(nombre,objetivo,fechaAsignacion,estado) VALUES (@0, @1, @2, @3);";
                Object[] args = new Object[4];
                args[0] = proyecto.getNombre();
                args[1] = proyecto.getObjetivo();
                args[2] = proyecto.getFecha();
                args[3] = proyecto.getEstado();
                SqlDataReader res = baseDatos.ejecutarConsulta(consulta, args);
                res.Close();
                consulta = "SELECT id FROM Proyecto WHERE nombre = '" + proyecto.getNombre() + "';";
                SqlDataReader reader = baseDatos.ejecutarConsulta(consulta);
                while (reader.Read())
                {
                    idP = reader.GetInt32(0);
                }
                reader.Close();
            }
            catch (SqlException ex)
            {
                resultado = "Error al insertar, Error 1: " + ex.Message;
            }

            try
            {
                consulta = "INSERT INTO OficinaUsuaria(representante,nombre,correo,idProyecto) VALUES (@0, @1, @2, @3);";
                Object[] args = new Object[4];
                args[0] = proyecto.getRep();
                args[1] = proyecto.getNomOf();
                args[2] = proyecto.getCorreoOf();
                args[3] = idP;
                SqlDataReader res = baseDatos.ejecutarConsulta(consulta, args);
                res.Close();
                consulta = "Select id from OficinaUsuaria where idProyecto = '" + idP + "';";
                SqlDataReader reader = baseDatos.ejecutarConsulta(consulta);
                while (reader.Read())
                {
                    idOf = reader.GetInt32(0);
                }
                reader.Close();
            }
            catch (SqlException ex)
            {
                resultado += "\nError 2: " + ex.Message;
            }

            try
            {
                if (proyecto.getTelOf() != 0)
                {
                    consulta = "INSERT INTO TelefonoOficina(numero,idCliente) VALUES (@0, @1);";
                    Object[] args = new Object[2];
                    args[0] = proyecto.getTelOf();
                    args[1] = idOf;
                    SqlDataReader res = baseDatos.ejecutarConsulta(consulta, args);
                    res.Close();
                }
            }
            catch (SqlException ex)
            {
                resultado += "\nError 3: " + ex.Message;
            }

            try
            {
                consulta = "Update Usuario Set idProy = '" + idP + "' where cedula = '" + proyecto.getLider() + "'";
                SqlDataReader reader = baseDatos.ejecutarConsulta(consulta);
                reader.Close();
            }
            catch (SqlException ex)
            {
                resultado += "\nError 4: " + ex.Message;
            }

            return resultado;
        }
        /** Descripcion: Consultar un proyecto por su nombre
         *
         * REQ: string
         *
         * RET: EntidadProyecto
         */
        public EntidadProyecto consultar_Proyecto(string nombreP)
        {
            string consulta = "";

            Object[] datos = new Object[11];

            EntidadProyecto objPro = null;
            //SqlConnection sqlConnection = new SqlConnection(conexion);

            try
            {
                //sqlConnection.Open();
                //--cambio en a consulta--
                consulta = "select p.objetivo, p.estado,p.fechaAsignacion,o.nombre, o.representante, o.correo,u.cedula,Concat(u.pNombre, ' ', u.pApellido, ' ', u.sApellido), t.numero from Usuario u Left Join Proyecto p ON u.idProy = p.id Left Join OficinaUsuaria o on p.id = o.idProyecto Left Join TelefonoOficina t on t.idCliente = o.id where p.nombre = '" + nombreP + "' and u.rol = 'Lider'";
                SqlDataReader reader = baseDatos.ejecutarConsulta(consulta);
                try
                {

                    if (reader.Read())
                    {
                        //cuando hay solo un tekefono
                        datos[0] = "";                  //nombre
                        datos[1] = reader.GetString(0); //objetivo
                        datos[2] = reader.GetString(1); //estado
                        datos[3] = reader.GetDateTime(2);  //fechaAsgnacion
                        datos[4] = reader.GetString(3); //nombreOficina
                        datos[5] = reader.GetString(4); // representante
                        datos[6] = reader.GetString(5); //cooreoOficina

                        datos[8] = reader.GetInt32(6);//cedula lider
                        datos[9] = reader.GetString(7);//nombreLider
                        if (!reader.IsDBNull(8))
                        {
                            datos[7] = reader.GetInt32(8);//Convert.ToInt32(reader.GetString(6)); //telOficina
                        }
                        else
                        {
                            datos[7] = 0;
                        }
                        datos[10] = null;// reader.GetInt32(17);//tel2

                    }
                    if (reader.Read())
                    {
                        //cuando hay dos telefonos
                        datos[10] = reader.GetInt32(8);//tel2

                    }
                    objPro = new EntidadProyecto(datos);
                    reader.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
            }

            catch (SqlException ex)
            {
                //throw ex;
                ex.ToString();
            }

            return objPro;
        }
        /* Descripcion: Casos que revisan que accion ejecutar dentro del IMEC
        *
        * REQ: int,Object,Object
        *
        * RET: string
        */
        public string ejecutarProyecto(int accion, Object[] datos, Object[] originales)
        {
            string resultado = "Exito";

            switch (accion)
            {
                case 1:
                    {
                        nuevoProyecto = new EntidadProyecto(datos);
                        resultado = controladoraBDProyecto.insertarProyecto(nuevoProyecto);
                    }
                    break;

                case 4:
                    {
                        controladoraBDProyecto.eliminarProyecto(datos[0].ToString());
                    }
                    break;
            }

            return resultado;
        }