private void SaveOrganization(OrganizationPoco organization) { var org = Organization.CreateInstance(); if (!organization.Id.IsNullOrEmpty()) { org.PrimaryKeyId = new PrimaryKeyId(new Guid(organization.Id)); } org.Name = organization.Name; org.OrganizationType = "Organization"; org.SaveChanges(); MapCreditCardsFromOrgToOrganization(organization.CreditCards, org); foreach (var user in organization.Users) { SaveCustomer(user, org.PrimaryKeyId.Value); } org.SaveChanges(); foreach (var subOrganization in organization.SubOrganizations) { var subOrg = Organization.CreateInstance(); if (!subOrganization.Id.IsNullOrEmpty()) { subOrg.PrimaryKeyId = new PrimaryKeyId(new Guid(subOrganization.Id)); subOrg.ParentId = org.PrimaryKeyId; } subOrg.Name = subOrganization.Name; subOrg.OrganizationType = "Organization Unit"; subOrg.SaveChanges(); MapCreditCardsFromOrgToOrganization(subOrganization.CreditCards, subOrg); foreach (var user in subOrganization.Users) { SaveCustomer(user, subOrg.PrimaryKeyId.Value); } subOrg.SaveChanges(); } }
private void ProcessOrganizations(FileStream stream) { foreach (var xOrganization in GetXElements(stream, "Organization")) { var organization = new OrganizationPoco { Id = xOrganization.Get("Id"), Name = xOrganization.Get("Name"), Users = new List <CustomerPoco>(), CreditCards = new List <CreditCardPoco>(), SubOrganizations = new List <OrganizationPoco>() }; foreach (var xUser in xOrganization.Element("Users")?.Elements("User") ?? Enumerable.Empty <XElement>()) { var customer = new CustomerPoco { Email = xUser.Get("Email"), FirstName = xUser.Get("FirstName"), LastName = xUser.Get("LastName"), Roles = xUser.GetEnumerable("Roles", ','), B2BRole = xUser.Get("B2BRole"), Location = xUser.Get("Location"), ShowInDemoUserMenu = xUser.GetIntOrDefault("ShowInDemoUserMenu", 1), DemoUserTitle = xUser.Get("DemoUserTitle"), DemoUserDescription = xUser.Get("DemoUserDescription"), DemoSortOrder = xUser.GetIntOrDefault("DemoSort"), Addresses = new List <AddressPoco>(), CreditCards = new List <CreditCardPoco>() }; foreach (var xAddress in xUser.Element("Addresses")?.Elements("Address") ?? Enumerable.Empty <XElement>()) { var address = new AddressPoco { Name = xAddress.Get("Name"), Line1 = xAddress.Get("Line1"), City = xAddress.Get("City"), CountryCode = xAddress.Get("CountryCode"), CountryName = xAddress.Get("CountryName"), RegionCode = xAddress.Get("RegionCode"), RegionName = xAddress.Get("RegionName"), PostalCode = xAddress.Get("PostalCode") }; customer.Addresses.Add(address); } organization.Users.Add(customer); } foreach (var xCreditCard in xOrganization.Element("CreditCards")?.Elements("CreditCard") ?? Enumerable.Empty <XElement>()) { var cc = new CreditCardPoco { Number = xCreditCard.Get("Number"), CardType = xCreditCard.Get("CardType"), LastFour = xCreditCard.Get("LastFour"), ExpirationYear = xCreditCard.GetInt("ExpirationYear"), ExpirationMonth = xCreditCard.GetInt("ExpirationMonth") }; organization.CreditCards.Add(cc); } foreach (var xSubOrganization in xOrganization.Element("SubOrganizations")?.Elements("SubOrganization") ?? Enumerable.Empty <XElement>()) { var subOrganization = new OrganizationPoco { Id = xSubOrganization.Get("Id"), Name = xSubOrganization.Get("Name"), Users = new List <CustomerPoco>(), CreditCards = new List <CreditCardPoco>(), SubOrganizations = new List <OrganizationPoco>() }; foreach (var xUser in xSubOrganization.Element("Users")?.Elements("User") ?? Enumerable.Empty <XElement>()) { var customer = new CustomerPoco { Email = xUser.Get("Email"), FirstName = xUser.Get("FirstName"), LastName = xUser.Get("LastName"), Roles = xUser.GetEnumerable("Roles", ','), B2BRole = xUser.Get("B2BRole"), Location = xUser.Get("Location"), ShowInDemoUserMenu = xUser.GetIntOrDefault("ShowInDemoUserMenu", 1), DemoUserTitle = xUser.Get("DemoUserTitle"), DemoUserDescription = xUser.Get("DemoUserDescription"), DemoSortOrder = xUser.GetIntOrDefault("DemoSort"), Addresses = new List <AddressPoco>(), CreditCards = new List <CreditCardPoco>() }; foreach (var xAddress in xUser.Element("Addresses")?.Elements("Address") ?? Enumerable.Empty <XElement>()) { var address = new AddressPoco { Name = xAddress.Get("Name"), Line1 = xAddress.Get("Line1"), City = xAddress.Get("City"), CountryCode = xAddress.Get("CountryCode"), CountryName = xAddress.Get("CountryName"), RegionCode = xAddress.Get("RegionCode"), RegionName = xAddress.Get("RegionName"), PostalCode = xAddress.Get("PostalCode") }; customer.Addresses.Add(address); } subOrganization.Users.Add(customer); } foreach (var xCreditCard in xSubOrganization.Element("CreditCards")?.Elements("CreditCard") ?? Enumerable.Empty <XElement>()) { var cc = new CreditCardPoco { Number = xCreditCard.Get("Number"), CardType = xCreditCard.Get("CardType"), LastFour = xCreditCard.Get("LastFour"), ExpirationYear = xCreditCard.GetInt("ExpirationYear"), ExpirationMonth = xCreditCard.GetInt("ExpirationMonth") }; subOrganization.CreditCards.Add(cc); } organization.SubOrganizations.Add(subOrganization); } SaveOrganization(organization); } }