/// <summary> /// Transforms the data from the dataset. /// </summary> /// <returns></returns> public override int TransformData(string importUser = null) { ReportProgress(0, "Starting import..."); var rockContext = new RockContext(); var personService = new PersonService(rockContext); var importPerson = personService.GetByFullName(importUser, allowFirstNameOnly: true).FirstOrDefault(); if (importPerson == null) { importPerson = personService.Queryable().FirstOrDefault(); } ImportPersonAlias = new PersonAliasService(rockContext).Get(importPerson.Id); var tableList = TableNodes.Where(n => n.Checked != false).ToList(); ReportProgress(0, "Checking for existing attributes..."); LoadExistingRockData(); ReportProgress(0, "Checking for table dependencies..."); bool isValidImport = ImportedPeople.Any() || tableList.Any(n => n.Name.Equals("Individual_Household")); var tableDependencies = new List <string>(); tableDependencies.Add("Batch"); // needed to attribute contributions properly tableDependencies.Add("Users"); // needed for notes, user logins tableDependencies.Add("Company"); // needed to attribute any business items tableDependencies.Add("Individual_Household"); // needed for just about everything tableDependencies.Add("ActivityMinistry"); // needed for RLC and Attendance tableDependencies.Add("RLC"); // needed for Attendance if (isValidImport) { // Order tables so non-dependents are imported first if (tableList.Any(n => tableDependencies.Contains(n.Name))) { tableList = tableList.OrderByDescending(n => tableDependencies.IndexOf(n.Name)).ToList(); } var scanner = new DataScanner(Database); foreach (var table in tableList) { if (!tableDependencies.Contains(table.Name)) { switch (table.Name) { case "Account": MapBankAccount(scanner.ScanTable(table.Name).AsQueryable()); break; case "Communication": MapCommunication(scanner.ScanTable(table.Name).AsQueryable()); break; case "Contribution": MapContribution(scanner.ScanTable(table.Name).AsQueryable()); break; case "Household_Address": MapFamilyAddress(scanner.ScanTable(table.Name).AsQueryable()); break; case "Notes": MapNotes(scanner.ScanTable(table.Name).AsQueryable()); break; case "Pledge": MapPledge(scanner.ScanTable(table.Name).AsQueryable()); break; case "Attribute": MapAttributes(scanner.ScanTable(table.Name).AsQueryable()); break; case "Groups": MapGroups(scanner.ScanTable(table.Name).AsQueryable()); break; //case "ActivityMinistry": // MapActivityMinistry( scanner.ScanTable( table.Name ).AsQueryable() ); // break; //case "RLC": // MapRLC( scanner.ScanTable( table.Name ).AsQueryable() ); // break; case "Attendance": MapAttendance(scanner.ScanTable(table.Name).AsQueryable()); break; case "IndividualContactNotes": MapIndividualContactNotes(scanner.ScanTable(table.Name).AsQueryable()); break; case "GiftednessProgram": MapGiftednessProgram(scanner.ScanTable(table.Name).AsQueryable()); break; case "IndividualGiftedness": MapIndividualGiftedness(scanner.ScanTable(table.Name).AsQueryable()); break; case "Authorizations": MapAuthorizations(scanner.ScanTable(table.Name).AsQueryable()); break; case "ActivityAssignment": MapActivityAssignment(scanner.ScanTable(table.Name).AsQueryable()); break; case "GroupsAttendance": MapGroupsAttendance(scanner.ScanTable(table.Name).AsQueryable()); break; default: break; } } else { if (table.Name == "Batch") { MapBatch(scanner.ScanTable(table.Name).AsQueryable()); } else if (table.Name == "Company") { MapCompany(scanner.ScanTable(table.Name).AsQueryable()); } else if (table.Name == "Individual_Household") { MapPerson(scanner.ScanTable(table.Name).AsQueryable()); } else if (table.Name == "Users") { MapUsers(scanner.ScanTable(table.Name).AsQueryable()); } else if (table.Name == "ActivityMinistry") { MapActivityMinistry(scanner.ScanTable(table.Name).AsQueryable()); } else if (table.Name == "RLC") { MapRLC(scanner.ScanTable(table.Name).AsQueryable()); } } } ReportProgress(100, "Import completed. "); } else { ReportProgress(0, "No imported people exist. Please include the Individual_Household table during the import."); } return(0); // return total number of rows imported? }
/// <summary> /// Transforms the data from the dataset. /// </summary> public override int TransformData(string importUser = null) { // Report progress to the main thread so it can update the UI ReportProgress(0, "Starting import..."); // Instantiate the object model service var rockContext = new RockContext(); // Connects to the source database (already loaded in memory by the UI) var scanner = new DataScanner(Database); // List of tables the user would like to import var tableList = TableNodes.Where(n => n.Checked != false).Select(n => n.Name).ToList(); // Supplies a lazy-loaded database queryable var tableData = scanner.ScanTable("TableName").AsQueryable(); // Hold a count of how many records have been imported int completed = 0; // Pick a method to save data to Rock: #1 (simple) or #2 (fast) // Option #1. Standard way to put data in Rock foreach (var dataRow in tableData) { // Get a value from the row. This has to be a nullable type. string columnValue = dataRow["ColumnName"] as string; // Create a Rock model and assign data to it Person person = new Person(); person.LastName = columnValue; rockContext.WrapTransaction(() => { // If it's a new model, add it to the database first rockContext.People.Add(person); // Save the data to the database rockContext.SaveChanges(DisableAudit); }); completed++; } // end option #1 // Option #2. More efficient way to import large data sets var newPersonList = new List <Person>(); foreach (var dataRow in tableData) { // Get a value from the row. This has to be a nullable type. string columnValue = dataRow["ColumnName"] as string; // Create a Rock model and assign data to it Person person = new Person(); newPersonList.Add(new Person()); completed++; // Save 100 people at a time if (completed % ReportingNumber < 1) { SaveModel(newPersonList); } } // Outside foreach, save any that haven't been saved yet if (newPersonList.Any()) { SaveModel(newPersonList); } // end option #2 // Report the final imported count ReportProgress(100, string.Format("Completed import: {0:N0} records imported.", completed)); return(completed); }