예제 #1
0
        protected void btnSaveEmployee_Click(object sender, EventArgs e)
        {
            string errorMessage = validate();

            if (errorMessage.Length == 0)
            {
                _employee.FirstName  = txtFirstName.Text.Trim().Length > 0 ? txtFirstName.Text.Trim() : null;
                _employee.MiddleName = txtMiddleName.Text.Trim().Length > 0 ? txtMiddleName.Text.Trim() : null;
                _employee.LastName   = txtLastName.Text.Trim().Length > 0 ? txtLastName.Text.Trim() : null;
                _employee.Ssn        = txtSSN.Text.Trim().Length > 0 ? txtSSN.Text.Trim().GetNumbers() : null;
                _employee.EmployeeNo = txtEmployeeNo.Text.Trim().Length > 0 ? txtEmployeeNo.Text.Trim() : null;
                _employee.Address1   = txtAddress1.Text.Trim().Length > 0 ? txtAddress1.Text.Trim() : null;
                _employee.Address2   = txtAddress2.Text.Trim().Length > 0 ? txtAddress2.Text.Trim() : null;
                _employee.City       = txtCity.Text.Trim().Length > 0 ? txtCity.Text.Trim() : null;
                _employee.State      = ddlState.SelectedValue.Length > 0 ? ddlState.SelectedValue : null;;
                _employee.ZipCode    = txtZip.Text.Trim().Length > 0 ? txtZip.Text.Trim() : null;
                _employee.Phone      = txtPhone.Text.Trim().GetNumbers().Length > 0 ? txtPhone.Text.Trim().GetNumbers() : null;
                _employee.IsActive   = true;
                _employee.IsEnabled  = true;

                if (txtUsername.Text.Trim().Length > 0)
                {
                    _employee.Username = txtUsername.Text.Trim().ToLower();
                }
                else
                {
                    _employee.Username = null;
                }

                if (txtPassword.Visible && txtPassword.Text.Trim().Length > 0)
                {
                    _employee.Password = Hashing.ComputeHash(txtPassword.Text.Trim(), HashType.MD5);
                }
                else if (txtNewPassword.Visible)
                {
                    if (txtNewPassword.Text.Trim().Length > 0)
                    {
                        _employee.Password = Hashing.ComputeHash(txtNewPassword.Text.Trim(), HashType.MD5);
                    }
                    else
                    {
                        _employee.Password = null;
                    }
                }
                else
                {
                    _employee.Password = null;
                }

                _employee.Save();

                Response.Redirect("~/Employees.aspx");
            }
            else
            {
                messages.Controls.Add(HtmlHelper.Div("error", errorMessage));
            }
        }
예제 #2
0
        public static void Create(EmployeeEntity employee)
        {
            if (!employee.IsNew)
            {
                throw new ArgumentException("The employee should be new", "employee");
            }

            employee.Save();
        }
예제 #3
0
        public static void LoadEmployees()
        {
            using (NorthwindDataContext db = new NorthwindDataContext())
            {
                var duplicateMapping = (from t in db.Territories.ToList()
                                        group int.Parse(t.TerritoryID) by t.TerritoryDescription into g
                                        where g.Count() > 1
                                        let min = g.Min()
                                                  from item in g.Except(new[] { min })
                                                  select new
                {
                    Min = min,
                    Item = item
                }).ToDictionary(a => a.Item, a => a.Min);

                var territoriesDic = Database.RetrieveAll <TerritoryEntity>().ToDictionary(a => a.Id);


                var exmployeeTerritories = (from e in db.Employees
                                            from t in e.EmployeeTerritories
                                            select new { e.EmployeeID, t.TerritoryID }).ToList()
                                           .AgGroupToDictionary(a => a.EmployeeID, gr =>
                                                                gr.Select(a => int.Parse(a.TerritoryID))
                                                                .Select(id => duplicateMapping.TryGet(id, id))
                                                                .Select(id => territoriesDic[id])
                                                                .Distinct().ToMList());


                Administrator.SaveListDisableIdentity(
                    from e in db.Employees
                    select new EmployeeEntity
                {
                    BirthDate       = e.BirthDate,
                    FirstName       = e.FirstName,
                    LastName        = e.LastName,
                    TitleOfCourtesy = e.TitleOfCourtesy,
                    HomePhone       = e.HomePhone,
                    Extension       = e.Extension,
                    HireDate        = e.HireDate,
                    Photo           = new FileEntity {
                        FileName = e.PhotoPath.AfterLast('/'), BinaryFile = RemoveOlePrefix(e.Photo.ToArray())
                    }.ToLiteFat(),
                    PhotoPath = e.PhotoPath,
                    Address   = new AddressEntity
                    {
                        Address    = e.Address,
                        City       = e.City,
                        Country    = e.Country,
                        Region     = e.Region,
                        PostalCode = e.PostalCode,
                    },
                    Notes       = e.Notes,
                    Territories = exmployeeTerritories[e.EmployeeID],
                }.SetId(e.EmployeeID));

                var pairs = (from e in db.Employees
                             where e.ReportsTo != null
                             select new { e.EmployeeID, e.ReportsTo });

                foreach (var pair in pairs)
                {
                    EmployeeEntity employee = Database.Retrieve <EmployeeEntity>(pair.EmployeeID);
                    employee.ReportsTo = Lite.Create <EmployeeEntity>(pair.ReportsTo.Value);
                    employee.Save();
                }
            }
        }