Beispiel #1
0
        private Dictionary<int, Employee> LoadEmpleados()
        {
            if (listaEmpleados != null)
            {
                return listaEmpleados;
            }
            else
            {
                listaEmpleados = new Dictionary<int, Employee>();

                int idEmpleado;
                string Nombre;
                string Apellido;
                bool Sexo = true;
                string eMail;
                string Direccion = "";
                //string Nacionalidad;
                //string Ciudad;
                DateTime FechaNacimiento;
                string Telefono;
                string Celular;
                string TipoDocumento;
                string NumeroDocumento;
                DateTime FechaExpedicionDocumento;
                DateTime FechaVencimientoDocumento;
                string Empresa;
                string Imagen;          // El path + nombre del archivo
                int IdImagen;
                byte[] imageBytes = null;
                string ultimaActualizacion;

                SqlConnection cnn = new SqlConnection(conexion);
                try
                {
                    //SqlCommand cmd = new SqlCommand("ListarEmpleados", cnn);
                    //cmd.CommandType = CommandType.StoredProcedure;

                    //cnn.Open();

                    //SqlDataReader lector = cmd.ExecuteReader();
                    cnn.Open();
                    SqlCommand cmd = cnn.CreateCommand();
                    cmd.CommandText = "select * from Empleados";
                    cmd.CommandType = CommandType.Text;

                    SqlDataReader lector = cmd.ExecuteReader();

                    int cant = 0;
                    Regex rHora = new Regex(@"(.*)-(.*)-(.*) (.*):(.*):(.*)");
                    while (lector.Read())
                    {
                        idEmpleado = Convert.ToInt32(lector["idEmpleado"]);
                        Nombre = lector["Nombre"].ToString();
                        Apellido = lector["Apellido"].ToString();

                        if (lector["Sexo"] != DBNull.Value)
                        {
                            Sexo = Convert.ToBoolean(lector["Sexo"]);
                        }
                        eMail = lector["eMail"].ToString();

                        if (lector["Direccion"] != DBNull.Value)
                        {
                            Direccion = lector["Direccion"].ToString();
                        }
                        FechaNacimiento = new DateTime(2013, 01, 01);

                        if (lector["FechaNacimiento"] != null)
                        {
                            if (lector["FechaNacimiento"].ToString().Length > 6)
                            {
                                FechaNacimiento = Convert.ToDateTime(lector["FechaNacimiento"].ToString());
                            }
                        }

                        Telefono = lector["Telefono"].ToString();
                        Celular = lector["Celular"].ToString();
                        TipoDocumento = lector["TipoDocumento"].ToString();

                        NumeroDocumento = lector["NumeroDocumento"].ToString();

                        FechaExpedicionDocumento = new DateTime(2013, 1, 1);
                        if (lector["FechaExpedicionDocumento"] != null)
                        {
                            if (lector["FechaExpedicionDocumento"].ToString().Length > 6)
                            {
                                FechaExpedicionDocumento = Convert.ToDateTime(lector["FechaExpedicionDocumento"]);
                            }

                        }
                        FechaVencimientoDocumento = new DateTime(2013, 12, 12);
                        if (lector["FechaVencimientoDocumento"] != null)
                        {
                            if (lector["FechaVencimientoDocumento"].ToString().Length > 6)
                            {
                                FechaVencimientoDocumento = Convert.ToDateTime(lector["FechaVencimientoDocumento"]);
                            }
                        }

                        Empresa = lector["Empresa"].ToString();

                        Imagen = lector["Imagen"].ToString();
                        IdImagen = Convert.ToInt32(lector["IdImagen"]);

                        ultimaActualizacion = lector["ultimaActualizacion"].ToString();

                        Employee emp = new Employee();

                        Match mHora = rHora.Match(ultimaActualizacion);

                        if (mHora.Success)
                        {
                            DateTime d = new DateTime(Convert.ToInt32(mHora.Groups[1].Value), Convert.ToInt32(mHora.Groups[2].Value), Convert.ToInt32(mHora.Groups[3].Value), Convert.ToInt32(mHora.Groups[4].Value), Convert.ToInt32(mHora.Groups[5].Value), Convert.ToInt32(mHora.Groups[6].Value));
                            emp.ultimaActualizacion = d;
                        }
                        else { loguearString("No paso la expresion regular de la hora. LoadEmpleados", TiposLOG.HH); }

                        bool valido = true;
                        int version = -1;
                        try { version = Convert.ToInt32(lector["Version"].ToString()); }
                        catch { valido = false; }

                        int personID = -1;
                        try { personID = Convert.ToInt32(lector["PersonID"].ToString()); }
                        catch { valido = false; }

                        emp.VersionEmpleado = version;
                        emp.PersonID = personID;

                        emp.Id = idEmpleado;
                        emp.Nombre = Nombre;
                        emp.Apellido = Apellido;
                        emp.Sexo = Sexo;
                        emp.EMail = eMail;
                        emp.Direccion = Direccion;
                        // emp.Nacionalidad = Nacionalidad;
                        // emp.Ciudad = Ciudad;
                        emp.FechaNacimiento = FechaNacimiento;
                        emp.Telefono = Telefono;
                        emp.Celular = Celular;
                        emp.TipoDocumento = TipoDocumento;
                        emp.NumeroDocumento = NumeroDocumento;
                        emp.FechaExpedicionDocumento = FechaExpedicionDocumento;
                        emp.FechaVencimientoDocumento = FechaVencimientoDocumento;

                        //DEBUG - ACA POR AHORA ES ""
                        emp.Empresa = Empresa;
                        emp.Empresa = "";

                        emp.Imagen = Imagen;
                        emp.imageVersion = IdImagen;

                        if (Imagen != "")
                        {
                            try
                            {
                                string fileName = Path.GetFileName(Imagen);
                                if (fileName != "")
                                {
                                    string imagePath = SystemConfiguration.ImagesPath;
                                    imageBytes = File.ReadAllBytes(imagePath + @"/" + fileName);
                                    cant++;
                                }
                                else
                                {
                                    imageBytes = null;
                                    emp.Imagen = "";
                                }

                            }
                            catch (Exception)
                            {
                                imageBytes = null;
                                emp.Imagen = "";
                            }
                        }
                        else
                        {
                            imageBytes = null;
                            emp.Imagen = "";
                        }

                        emp.attachImage(imageBytes);

                        if (valido) listaEmpleados.Add(idEmpleado, emp);

                    }

                }
                catch (Exception ex)
                {
                    loguearString("Excepcion en LoadEmpleados: " + ex.Message, TiposLOG.HH);

                }
                finally
                {
                    cnn.Close();
                }
            }

            return listaEmpleados;
        }
 private void attachImageByteArray(Employee v_emp, string imagename)
 {
     byte[] bytes = File.ReadAllBytes(imagename);
     v_emp.attachImage(bytes);
 }