Example #1
0
        public static void SyncUserProfilesToEmployees()
        {
            var ldapUsers = UserProfileHelper.Query();

            var info = "ldapUsers.Count: " + ldapUsers.Count.ToString();

            DebugLog.Log("SyncUserProfilesToEmployees", "OGE450", "OOGC.Data.SharePoint.Employee", "SyncUserProfilesToEmployees", info, "Info", DateTime.Now);

            var knownEmployees = Employee.GetAll();
            var prefix         = "";

            info = "knownEmployees.Count: " + knownEmployees.Count.ToString();
            DebugLog.Log("SyncUserProfilesToEmployees", "OGE450", "OOGC.Data.SharePoint.Employee", "SyncUserProfilesToEmployees", info, "Info", DateTime.Now);

            prefix = "i:0e.t|adfs|";

            // Find all new employees or employees who are now inactive
            var result =
                from ldap in ldapUsers
                join emp in knownEmployees on prefix + ldap.UPN.ToLower() equals emp.AccountName.ToLower() into emps
                from emp in emps.DefaultIfEmpty()
                    where emp == null || ldap.Inactive != emp.Inactive || !emp.AccountCreatedDate.HasValue
                select new Employee()
            {
                Id                 = emp == null ? 0 : emp.Id,
                AccountName        = prefix + ldap.UPN,
                Title              = ldap.DisplayName,
                Inactive           = ldap.Inactive,
                EmailAddress       = ldap.Email,
                EmployeeStatus     = ldap.Inactive ? Constants.EmployeeStatus.INACTIVE : Constants.EmployeeStatus.ACTIVE,
                AccountCreatedDate = Convert.ToDateTime(ldap.WhenCreated),
                FilerType          = emp == null ? "" : emp.FilerType,
                ReportingStatus    = emp == null ? "" : emp.ReportingStatus,
                Last450Date        = emp == null ? null : emp.Last450Date,
                Division           = emp == null ? "" : emp.Division
            };

            if (result != null)
            {
                var employees = result.ToList();

                info = "employees.Count: " + employees.Count.ToString();
                DebugLog.Log("SyncUserProfilesToEmployees", "OGE450", "OOGC.Data.SharePoint.Employee", "SyncUserProfilesToEmployees", info, "Info", DateTime.Now);

                foreach (Employee emp in employees)
                {
                    if (emp.Inactive)
                    {
                        emp.Deactivate();
                    }

                    if (emp.Id == 0)
                    {
                        emp.FilerType = Constants.FilerType.NOT_ASSIGNED;
                    }

                    emp.Save();
                }
            }

            knownEmployees = knownEmployees.Where(x => x.Inactive == false).ToList();

            // Find all employees who are no longer found within the ldap query
            result = from emp in knownEmployees
                     join ldap in ldapUsers on emp.AccountName.ToLower() equals prefix + ldap.UPN.ToLower() into ldaps
                     from ldap in ldaps.DefaultIfEmpty()
                         where ldap == null
                     select emp;

            if (result != null)
            {
                var employees = result.ToList();

                foreach (Employee emp in employees)
                {
                    // known employee no longer found.
                    emp.Deactivate();

                    emp.Save();
                }
            }
        }