/// <summary> /// Map data /// </summary> /// <param name="dbContext"></param> /// <param name="oldObject"></param> /// <param name="user"></param> /// <param name="systemId"></param> /// <param name="smUserId"></param> /// <param name="firstName"></param> /// <param name="lastName"></param> /// <param name="maxUserIndex"></param> private static void CopyToInstance(DbAppContext dbContext, UserHets oldObject, ref User user, string systemId, string smUserId, string firstName, string lastName, ref int maxUserIndex) { try { // File contains multiple records per user (1 for each dsitrict they can access) // We are currently importing 1 only -> where Default_Service_Area = Y if (oldObject.Default_Service_Area != "Y") { return; } if (user != null) { return; // only add new users } user = new User { Id = ++maxUserIndex, Active = true, SmUserId = smUserId, SmAuthorizationDirectory = "IDIR" }; if (!string.IsNullOrEmpty(firstName)) { user.GivenName = firstName; } if (!string.IsNullOrEmpty(lastName)) { user.Surname = lastName; } // ******************************************************************* // create initials // ******************************************************************* string temp = ""; if (!string.IsNullOrEmpty(lastName) && lastName.Length > 0) { temp = lastName.Substring(0, 1).ToUpper(); } if (!string.IsNullOrEmpty(firstName) && firstName.Length > 0) { temp = temp + firstName.Substring(0, 1).ToUpper(); } if (!string.IsNullOrEmpty(temp)) { user.Initials = temp; } // ******************************************************************* // map user to the correct service area // ******************************************************************* int serviceAreaId; try { serviceAreaId = int.Parse(oldObject.Service_Area_Id); } catch { // not mapped correctly throw new ArgumentException(string.Format("User Error - Invalid Service Area (userId: {0}", user.SmUserId)); } ServiceArea serviceArea = dbContext.ServiceAreas.FirstOrDefault(x => x.MinistryServiceAreaID == serviceAreaId); if (serviceArea == null) { // not mapped correctly throw new ArgumentException(string.Format("User Error - Invalid Service Area (userId: {0}", user.SmUserId)); } user.DistrictId = serviceArea.DistrictId; // ******************************************************************* // set the user's role // ** all new users will be added with basic access only // ******************************************************************* UserRole userRole = new UserRole(); Role role = dbContext.Roles.FirstOrDefault(x => x.Name == "HETS Clerk"); int roleId = -1; if (role != null) { roleId = role.Id; } // *********************************************** // create user // *********************************************** user.AppCreateTimestamp = DateTime.UtcNow; user.AppCreateUserid = systemId; user.AppLastUpdateUserid = systemId; user.AppLastUpdateTimestamp = DateTime.UtcNow; userRole.Role = dbContext.Roles.First(x => x.Id == roleId); userRole.EffectiveDate = DateTime.UtcNow.AddDays(-1); userRole.AppCreateTimestamp = DateTime.UtcNow; userRole.AppCreateUserid = systemId; userRole.AppLastUpdateUserid = systemId; userRole.AppLastUpdateTimestamp = DateTime.UtcNow; user.UserRoles = new List <UserRole> { userRole }; dbContext.Users.Add(user); } catch (Exception ex) { Debug.WriteLine("***Error*** - Employee Id: " + oldObject.Popt_Id); Debug.WriteLine("***Error*** - Master User Index: " + maxUserIndex); Debug.WriteLine(ex.Message); throw; } }
/// <summary> /// Map data /// </summary> /// <param name="dbContext"></param> /// <param name="oldObject"></param> /// <param name="user"></param> /// <param name="systemId"></param> private static void CopyToInstance(DbAppContext dbContext, UserHets oldObject, ref User user, string systemId) { string smUserId; int serviceAreaId; int startPos = oldObject.User_Cd.IndexOf(@"\", StringComparison.Ordinal) + 1; try { serviceAreaId = oldObject.Service_Area_Id; smUserId = oldObject.User_Cd.Substring(startPos).Trim(); } catch { return; } // add the user specified in oldObject.Modified_By and oldObject.Created_By if not there in the database User modifiedBy = ImportUtility.AddUserFromString(dbContext, oldObject.Modified_By, systemId); User createdBy = ImportUtility.AddUserFromString(dbContext, oldObject.Created_By, systemId); if (createdBy.SmUserId == smUserId) { user = createdBy; return; } if (modifiedBy.SmUserId == smUserId) { user = modifiedBy; return; } UserRole userRole = new UserRole(); string authority; try { authority = oldObject.Authority.Trim(); } catch { // regular User authority = ""; } int roleId = ImportUtility.GetRoleIdFromAuthority(authority); User user1 = dbContext.Users.FirstOrDefault(x => x.SmUserId == smUserId); ServiceArea serArea = dbContext.ServiceAreas .Include(x => x.District) .FirstOrDefault(x => x.MinistryServiceAreaID == serviceAreaId); if (user1 == null) { if (user == null) { user = new User(); } try { user.SmUserId = smUserId; if (serArea != null) { user.District = serArea.District; user.DistrictId = serArea.DistrictId; } } catch { // do nothing } user.CreateTimestamp = DateTime.UtcNow; user.CreateUserid = createdBy.SmUserId; //a dd user Role - Role Id is limited to 1, or 2 if (roleId > 2) { roleId = 1; } userRole.Role = dbContext.Roles.First(x => x.Id == roleId); userRole.CreateTimestamp = DateTime.UtcNow; userRole.ExpiryDate = DateTime.UtcNow.AddMonths(12); userRole.CreateUserid = createdBy.SmUserId; userRole.EffectiveDate = DateTime.UtcNow.AddDays(-1); user.UserRoles = new List <UserRole> { userRole }; dbContext.Users.Add(user); } else { user = dbContext.Users .Include(x => x.UserRoles) .Include(x => x.GroupMemberships) .First(x => x.SmUserId == smUserId); // if the user does not have the user role, add the user role if (user.UserRoles == null) { user.UserRoles = new List <UserRole>(); } // if the role does not exist for the user, add the user role for the user if (user.UserRoles.FirstOrDefault(x => x.RoleId == roleId) == null) { userRole.Role = dbContext.Roles.First(x => x.Id == roleId); userRole.CreateTimestamp = DateTime.UtcNow; userRole.ExpiryDate = DateTime.UtcNow.AddMonths(12); userRole.CreateUserid = createdBy.SmUserId; userRole.EffectiveDate = DateTime.UtcNow.AddDays(-1); user.UserRoles.Add(userRole); } user.LastUpdateUserid = createdBy.SmUserId; user.CreateTimestamp = DateTime.UtcNow; user.Active = true; dbContext.Users.Update(user); } }