/// <summary> /// Maps the activity ministry. /// </summary> /// <param name="tableData">The table data.</param> /// <returns></returns> private void MapActivityMinistry(IQueryable <Row> tableData) { var lookupContext = new RockContext(); // Add an Attribute for the unique F1 Ministry Id int groupEntityTypeId = EntityTypeCache.Read("Rock.Model.Group").Id; var ministryAttributeId = new AttributeService(lookupContext).Queryable().Where(a => a.EntityTypeId == groupEntityTypeId && a.Key == "F1MinistryId").Select(a => a.Id).FirstOrDefault(); if (ministryAttributeId == 0) { var newMinistryAttribute = new Rock.Model.Attribute(); newMinistryAttribute.Key = "F1MinistryId"; newMinistryAttribute.Name = "F1 Ministry Id"; newMinistryAttribute.FieldTypeId = IntegerFieldTypeId; newMinistryAttribute.EntityTypeId = groupEntityTypeId; newMinistryAttribute.EntityTypeQualifierValue = string.Empty; newMinistryAttribute.EntityTypeQualifierColumn = string.Empty; newMinistryAttribute.Description = "The FellowshipOne identifier for the ministry that was imported"; newMinistryAttribute.DefaultValue = string.Empty; newMinistryAttribute.IsMultiValue = false; newMinistryAttribute.IsRequired = false; newMinistryAttribute.Order = 0; lookupContext.Attributes.Add(newMinistryAttribute); lookupContext.SaveChanges(DisableAudit); ministryAttributeId = newMinistryAttribute.Id; } // Get previously imported Ministries var importedMinistries = new AttributeValueService(lookupContext).GetByAttributeId(ministryAttributeId) .ToDictionary(t => t.Value.AsType <int?>(), t => t.EntityId); var newGroups = new List <Group>(); int completed = 0; int totalRows = tableData.Count(); int percentage = (totalRows - 1) / 100 + 1; ReportProgress(0, string.Format("Verifying ministry import ({0:N0} found).", totalRows)); foreach (var row in tableData) { int?ministryId = row["Ministry_ID"] as int?; if (ministryId != null && !importedMinistries.ContainsKey(ministryId)) { string ministryName = row["Ministry_Name"] as string; bool? ministryIsActive = row["Ministry_Active"] as bool?; int? activityId = row["Activity_ID"] as int?; string activityName = row["Activity_Name"] as string; bool? activityIsActive = row["Activity_Active"] as bool?; if (ministryName != null) { var ministry = new Group(); ministry.Name = ministryName.Trim(); ministry.IsActive = ministryIsActive ?? false; ministry.CampusId = CampusList.Where(c => ministryName.StartsWith(c.Name) || ministryName.StartsWith(c.ShortCode)) .Select(c => (int?)c.Id).FirstOrDefault(); // create new group for activity with ministry as parent group newGroups.Add(ministry); completed++; if (completed % percentage < 1) { int percentComplete = completed / percentage; ReportProgress(percentComplete, string.Format("{0:N0} ministries imported ({1}% complete).", completed, percentComplete)); } else if (completed % ReportingNumber < 1) { SaveActivityMinistry(newGroups); ReportPartialProgress(); newGroups.Clear(); } } } } if (newGroups.Any()) { SaveActivityMinistry(newGroups); } ReportProgress(100, string.Format("Finished ministry import: {0:N0} ministries imported.", completed)); }