예제 #1
0
파일: SKPerson.cs 프로젝트: cbump/Slingshot
        public static Person Translate(Individual individual, List <Value> values, List <Family> families, List <Field> tableFields, List <Label> labels)
        {
            Person person = new Person();

            person.Id     = Math.Abs(unchecked (( int )individual.Id));
            person.Campus = new Campus()
            {
                CampusId = 0, CampusName = "Main Campus"
            };

            // Import the family information
            Family family = families.Where(v => v.Id == individual.FamilyId).FirstOrDefault();

            person.FamilyId   = Math.Abs(unchecked (( int )individual.FamilyId));
            person.FamilyName = family.FamilyName;
            person.FamilyRole = individual.Age < 18 ? FamilyRole.Child : FamilyRole.Adult;

            // Import the basic stuff
            person.Gender       = (Core.Model.Gender)individual.Gender;
            person.RecordStatus = individual.RecordStatus;
            Value memberStatus = values.Where(v => v.Id == individual.MemberStatus).FirstOrDefault();

            if (memberStatus != null)
            {
                person.ConnectionStatus = memberStatus.Description;
                if (memberStatus.Description == "Deceased")
                {
                    person.IsDeceased     = true;
                    person.RecordStatus   = RecordStatus.Inactive;
                    person.InactiveReason = memberStatus.Description;
                }
            }
            else
            {
                person.ConnectionStatus = "Unknown";
            }

            person.CreatedDateTime  = individual.CreateDate > family.CreateDate ? individual.CreateDate : family.CreateDate;
            person.ModifiedDateTime = individual.UpdateDate > family.UpdateDate ? individual.UpdateDate : family.UpdateDate;


            person.FirstName  = individual.FirstName;
            person.MiddleName = individual.MiddleName;
            person.LastName   = individual.LastName;
            person.NickName   = individual.NickName;
            person.Salutation = individual.Salutation;
            person.Suffix     = individual.Suffix;

            person.Birthdate       = individual.BirthDate;
            person.AnniversaryDate = individual.WeddingDate;

            if (individual.MaritalCode > 0)
            {
                switch (values.Where(v => v.Id == individual.MaritalCode).Select(v => v.Description).FirstOrDefault())
                {
                case "Married":
                    person.MaritalStatus = MaritalStatus.Married;
                    break;

                case "Single Dad":
                case "Single Mom":
                case "Single":
                    person.MaritalStatus = MaritalStatus.Single;
                    break;

                case "Divorced":
                    person.MaritalStatus = MaritalStatus.Divorced;
                    break;

                default:
                    person.MaritalStatus = MaritalStatus.Unknown;
                    break;
                }
            }
            else
            {
                if (individual.Age < 18)
                {
                    person.MaritalStatus = MaritalStatus.Single;
                }
                else
                {
                    person.MaritalStatus = MaritalStatus.Unknown;
                }
            }

            person.Email           = individual.Email;
            person.EmailPreference = individual.EmailIndicator ? EmailPreference.NoMassEmails : EmailPreference.EmailAllowed;

            Regex digitsOnly = new Regex(@"[^\d]");

            if (!string.IsNullOrWhiteSpace(digitsOnly.Replace(individual.CellPhone, "")) && digitsOnly.Replace(individual.CellPhone, "").Length >= 7)
            {
                PersonPhone phone = new PersonPhone();
                phone.PhoneNumber = digitsOnly.Replace(individual.CellPhone, "");
                phone.PersonId    = person.Id;
                phone.IsUnlisted  = individual.CellPhoneUnlisted;
                phone.PhoneType   = "Cell";
                person.PhoneNumbers.Add(phone);
            }
            if (!string.IsNullOrWhiteSpace(digitsOnly.Replace(individual.HomePhone, "")) && digitsOnly.Replace(individual.HomePhone, "").Length >= 7)
            {
                PersonPhone phone = new PersonPhone();
                phone.PhoneNumber = digitsOnly.Replace(individual.HomePhone, "");
                phone.PersonId    = person.Id;
                phone.IsUnlisted  = individual.HomePhoneUnlisted;
                phone.PhoneType   = "Home";
                person.PhoneNumbers.Add(phone);
            }
            if (!string.IsNullOrWhiteSpace(digitsOnly.Replace(individual.WorkPhone, "")) && digitsOnly.Replace(individual.WorkPhone, "").Length >= 7)
            {
                PersonPhone phone = new PersonPhone();
                phone.PhoneNumber = digitsOnly.Replace(individual.WorkPhone, "");
                phone.PersonId    = person.Id;
                phone.IsUnlisted  = individual.WorkPhoneUnlisted;
                phone.PhoneType   = "Work";
                person.PhoneNumbers.Add(phone);
            }

            // Now export their address
            PersonAddress address = new PersonAddress();

            address.PersonId    = person.Id;
            address.AddressType = AddressType.Home;
            address.Street1     = family.Address1;
            address.Street2     = family.Address2;
            address.City        = family.City;
            address.State       = family.State;
            address.PostalCode  = family.Zip;
            person.Addresses.Add(address);

            // Handle the User Defined Fields
            var properties = individual.GetType().GetProperties();

            foreach (var property in properties)
            {
                if (property.Name.ToLower().Contains("udf"))
                {
                    var attribute = property.CustomAttributes.Where(ca => ca.AttributeType.Name == "ColumnName").FirstOrDefault();

                    if (attribute != null)
                    {
                        var fieldKey   = (( string )attribute.ConstructorArguments.FirstOrDefault().Value).ToLower();
                        var tableField = tableFields.Where(tf => tf.Name.ToLower().Contains(fieldKey)).FirstOrDefault();

                        if (tableField != null)
                        {
                            PersonAttributeValue pav = new PersonAttributeValue();
                            // If this is a string
                            if (property.PropertyType == typeof(string) && !string.IsNullOrWhiteSpace(( string )property.GetValue(individual)))
                            {
                                pav.AttributeValue = ( string )property.GetValue(individual);
                            }
                            // If this is a long (lookup value)
                            else if (property.PropertyType == typeof(long))
                            {
                                pav.AttributeValue = values.Where(v => v.Id == ( long )property.GetValue(individual)).Select(i => i.Description).FirstOrDefault();
                            }
                            // If this is a date
                            else if ((property.PropertyType == typeof(DateTime) && (( DateTime )property.GetValue(individual)) != DateTime.MinValue) ||
                                     (property.PropertyType == typeof(DateTime? ) && property.GetValue(individual) != null))
                            {
                                pav.AttributeValue = property.GetValue(individual).ToString();
                            }
                            // If this is a boolean
                            else if (property.PropertyType == typeof(bool))
                            {
                                pav.AttributeValue = ( bool )property.GetValue(individual) ? "True" : "False";
                            }
                            // Otherwise just continue
                            else
                            {
                                continue;
                            }
                            pav.PersonId = person.Id;
                            // Lookup the key from the table fields
                            pav.AttributeKey = labels.Where(l => l.LabelId == tableField.LabelId).Select(l => l.Description).DefaultIfEmpty(tableField.Description).FirstOrDefault().Replace(" ", string.Empty);
                            person.Attributes.Add(pav);
                        }
                    }
                }
            }

            person.Note = individual.Note;

            if (individual.JoinDate != DateTime.MinValue)
            {
                PersonAttributeValue pav = new PersonAttributeValue();
                pav.AttributeValue = individual.JoinDate.ToString();
                pav.PersonId       = person.Id;
                pav.AttributeKey   = "JoinDate";
                person.Attributes.Add(pav);
            }

            if (individual.HowJoined > 1)
            {
                PersonAttributeValue pav = new PersonAttributeValue();
                pav.AttributeValue = values.Where(v => v.Id == individual.HowJoined).Select(v => v.Description).FirstOrDefault();
                pav.PersonId       = person.Id;
                pav.AttributeKey   = "HowJoined";
                person.Attributes.Add(pav);
            }

            if (individual.BaptizedDate != DateTime.MinValue)
            {
                PersonAttributeValue pav = new PersonAttributeValue();
                pav.AttributeValue = individual.BaptizedDate.ToString();
                pav.PersonId       = person.Id;
                pav.AttributeKey   = "BaptizedDate";
                person.Attributes.Add(pav);
            }

            {
                PersonAttributeValue pav = new PersonAttributeValue();
                pav.AttributeValue = individual.Baptized ? "True" : "False";
                pav.PersonId       = person.Id;
                pav.AttributeKey   = "Baptized";
                person.Attributes.Add(pav);
            }

            if (individual.JobCode > 1)
            {
                PersonAttributeValue pav = new PersonAttributeValue();
                pav.AttributeValue = values.Where(v => v.Id == individual.JobCode).Select(v => v.Description).FirstOrDefault();
                pav.PersonId       = person.Id;
                pav.AttributeKey   = "Occupation";
                person.Attributes.Add(pav);
            }

            if (!string.IsNullOrWhiteSpace(individual.Employer))
            {
                PersonAttributeValue pav = new PersonAttributeValue();
                pav.AttributeValue = individual.Employer;
                pav.PersonId       = person.Id;
                pav.AttributeKey   = "Employer";
                person.Attributes.Add(pav);
            }

            if (!string.IsNullOrWhiteSpace(individual.SundaySchool))
            {
                PersonAttributeValue pav = new PersonAttributeValue();
                pav.AttributeValue = individual.SundaySchool;
                pav.PersonId       = person.Id;
                pav.AttributeKey   = "SundaySchool";
                person.Attributes.Add(pav);
            }

            return(person);
        }
예제 #2
0
        /// <summary>
        /// Writes to package.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model">The model.</param>
        public static void WriteToPackage <T>(T model)
        {
            var typeName = model.GetType().Name;

            if (model is IImportModel)
            {
                var importModel = ( IImportModel )model;
                // check if a textwriter is needed for this model type
                if (!textWriters.ContainsKey(typeName))
                {
                    if (!Directory.Exists(_packageDirectory))
                    {
                        InitalizePackageFolder();
                    }

                    textWriters.Add(typeName, ( TextWriter )File.CreateText($@"{_packageDirectory}\{importModel.GetFileName()}"));

                    // if model is for person create related writers
                    if (importModel is Person)
                    {
                        // person attributes
                        var personAttributeValue = new PersonAttributeValue();
                        textWriters.Add(personAttributeValue.GetType().Name, ( TextWriter )File.CreateText($@"{_packageDirectory}\{personAttributeValue.GetFileName()}"));

                        // person phones
                        var personPhone = new PersonPhone();
                        textWriters.Add(personPhone.GetType().Name, ( TextWriter )File.CreateText($@"{_packageDirectory}\{personPhone.GetFileName()}"));

                        // person addresses
                        var personAddress = new PersonAddress();
                        textWriters.Add(personAddress.GetType().Name, ( TextWriter )File.CreateText($@"{_packageDirectory}\{personAddress.GetFileName()}"));

                        // person search key
                        var personSearchKey = new PersonSearchKey();
                        textWriters.Add(personSearchKey.GetType().Name, ( TextWriter )File.CreateText($@"{_packageDirectory}\{personSearchKey.GetFileName()}"));
                    }

                    if (importModel is PersonAttributeValue)
                    {
                        var personAttributeValue = new PersonAttributeValue();
                        textWriters.Add(personAttributeValue.GetType().Name, ( TextWriter )File.CreateText($@"{_packageDirectory}\{personAttributeValue.GetFileName()}"));
                    }

                    // if model is for financial batch create related writers
                    if (importModel is FinancialBatch)
                    {
                        // financial transactions
                        var financialTransaction = new FinancialTransaction();
                        textWriters.Add(financialTransaction.GetType().Name, ( TextWriter )File.CreateText($@"{_packageDirectory}\{financialTransaction.GetFileName()}"));

                        // financial transation details
                        var financialTransactionDetail = new FinancialTransactionDetail();
                        textWriters.Add(financialTransactionDetail.GetType().Name, ( TextWriter )File.CreateText($@"{_packageDirectory}\{financialTransactionDetail.GetFileName()}"));
                    }

                    // if model is for financial transaction create related writers
                    if (importModel is FinancialTransaction)
                    {
                        // financial transation details
                        var financialTransactionDetail = new FinancialTransactionDetail();
                        textWriters.Add(financialTransactionDetail.GetType().Name, ( TextWriter )File.CreateText($@"{_packageDirectory}\{financialTransactionDetail.GetFileName()}"));
                    }

                    // if model is for group create related writers
                    if (importModel is Group)
                    {
                        // group member
                        var groupMember = new GroupMember();
                        textWriters.Add(groupMember.GetType().Name, ( TextWriter )File.CreateText($@"{_packageDirectory}\{groupMember.GetFileName()}"));

                        // group attributes
                        var groupAttributeValue = new GroupAttributeValue();
                        textWriters.Add(groupAttributeValue.GetType().Name, ( TextWriter )File.CreateText($@"{_packageDirectory}\{groupAttributeValue.GetFileName()}"));

                        // group addresses
                        var groupAddress = new GroupAddress();
                        textWriters.Add(groupAddress.GetType().Name, ( TextWriter )File.CreateText($@"{_packageDirectory}\{groupAddress.GetFileName()}"));
                    }

                    // if model is for business create related writers
                    if (importModel is Business)
                    {
                        // business attributes
                        var businessAttributeValue = new BusinessAttributeValue();
                        textWriters.Add(businessAttributeValue.GetType().Name, ( TextWriter )File.CreateText($@"{_packageDirectory}\{businessAttributeValue.GetFileName()}"));

                        // business phones
                        var businessPhone = new BusinessPhone();
                        textWriters.Add(businessPhone.GetType().Name, ( TextWriter )File.CreateText($@"{_packageDirectory}\{businessPhone.GetFileName()}"));

                        // business addresses
                        var businessAddress = new BusinessAddress();
                        textWriters.Add(businessAddress.GetType().Name, ( TextWriter )File.CreateText($@"{_packageDirectory}\{businessAddress.GetFileName()}"));

                        // business contacts
                        var businessContact = new BusinessContact();
                        textWriters.Add(businessContact.GetType().Name, ( TextWriter )File.CreateText($@"{_packageDirectory}\{businessContact.GetFileName()}"));
                    }
                }

                var txtWriter = textWriters[typeName];

                // check if a csvwriter is needed for this model type
                if (!csvWriters.ContainsKey(typeName))
                {
                    var newCsvWriter = new CsvWriter(txtWriter);
                    csvWriters.Add(typeName, newCsvWriter);
                    newCsvWriter.WriteHeader <T>();
                    //newCsvWriter.Configuration.QuoteAllFields = true;

                    // if model is for person create related writers
                    if (importModel is Person)
                    {
                        // person attributes
                        var personAttributeValue             = new PersonAttributeValue();
                        var newPersonAttributeValueCsvWriter = new CsvWriter(textWriters[personAttributeValue.GetType().Name]);
                        csvWriters.Add(personAttributeValue.GetType().Name, newPersonAttributeValueCsvWriter);
                        newPersonAttributeValueCsvWriter.WriteHeader <PersonAttributeValue>();

                        // person phones
                        var personPhone             = new PersonPhone();
                        var newPersonPhoneCsvWriter = new CsvWriter(textWriters[personPhone.GetType().Name]);
                        csvWriters.Add(personPhone.GetType().Name, newPersonPhoneCsvWriter);
                        newPersonPhoneCsvWriter.WriteHeader <PersonPhone>();

                        // person addresses
                        var personAddress             = new PersonAddress();
                        var newPersonAddressCsvWriter = new CsvWriter(textWriters[personAddress.GetType().Name]);
                        csvWriters.Add(personAddress.GetType().Name, newPersonAddressCsvWriter);
                        newPersonAddressCsvWriter.WriteHeader <PersonAddress>();

                        // person search keys
                        var personSearchKey             = new PersonSearchKey();
                        var newPersonSearchKeyCsvWriter = new CsvWriter(textWriters[personSearchKey.GetType().Name]);
                        csvWriters.Add(personSearchKey.GetType().Name, newPersonSearchKeyCsvWriter);
                        newPersonSearchKeyCsvWriter.WriteHeader <PersonSearchKey>();
                    }

                    if (importModel is PersonAttributeValue)
                    {
                        var personAttributeValue             = new PersonAttributeValue();
                        var newPersonAttributeValueCsvWriter = new CsvWriter(textWriters[personAttributeValue.GetType().Name]);
                        csvWriters.Add(personAttributeValue.GetType().Name, newPersonAttributeValueCsvWriter);
                        newPersonAttributeValueCsvWriter.WriteHeader <PersonAttributeValue>();
                    }

                    // if model is for financial batch create related writers
                    if (importModel is FinancialBatch)
                    {
                        // financial transaction
                        var financialTransaction             = new FinancialTransaction();
                        var newFinancialTransactionCsvWriter = new CsvWriter(textWriters[financialTransaction.GetType().Name]);
                        csvWriters.Add(financialTransaction.GetType().Name, newFinancialTransactionCsvWriter);
                        newFinancialTransactionCsvWriter.WriteHeader <FinancialTransaction>();

                        // financial transaction detail
                        var financialTransactionDetail             = new FinancialTransactionDetail();
                        var newFinancialTransactionDetailCsvWriter = new CsvWriter(textWriters[financialTransactionDetail.GetType().Name]);
                        csvWriters.Add(financialTransactionDetail.GetType().Name, newFinancialTransactionDetailCsvWriter);
                        newFinancialTransactionDetailCsvWriter.WriteHeader <FinancialTransactionDetail>();
                    }

                    //if model is for financial transaction, create related writers
                    if (importModel is FinancialTransaction)
                    {
                        // financial transaction detail
                        var financialTransactionDetail             = new FinancialTransactionDetail();
                        var newFinancialTransactionDetailCsvWriter = new CsvWriter(textWriters[financialTransactionDetail.GetType().Name]);
                        csvWriters.Add(financialTransactionDetail.GetType().Name, newFinancialTransactionDetailCsvWriter);
                        newFinancialTransactionDetailCsvWriter.WriteHeader <FinancialTransactionDetail>();
                    }

                    // if model is for group create related writers
                    if (importModel is Group)
                    {
                        // group member
                        var groupMember             = new GroupMember();
                        var newGroupMemberCsvWriter = new CsvWriter(textWriters[groupMember.GetType().Name]);
                        csvWriters.Add(groupMember.GetType().Name, newGroupMemberCsvWriter);
                        newGroupMemberCsvWriter.WriteHeader <GroupMember>();

                        // group attributes
                        var groupAttributeValue             = new GroupAttributeValue();
                        var newGroupAttributeValueCsvWriter = new CsvWriter(textWriters[groupAttributeValue.GetType().Name]);
                        csvWriters.Add(groupAttributeValue.GetType().Name, newGroupAttributeValueCsvWriter);
                        newGroupAttributeValueCsvWriter.WriteHeader <GroupAttributeValue>();

                        // group addresses
                        var groupAddress             = new GroupAddress();
                        var newGroupAddressCsvWriter = new CsvWriter(textWriters[groupAddress.GetType().Name]);
                        csvWriters.Add(groupAddress.GetType().Name, newGroupAddressCsvWriter);
                        newGroupAddressCsvWriter.WriteHeader <GroupAddress>();
                    }

                    // if model is for business create related writers
                    if (importModel is Business)
                    {
                        // business attributes
                        var businessAttributeValue             = new BusinessAttributeValue();
                        var newBusinessAttributeValueCsvWriter = new CsvWriter(textWriters[businessAttributeValue.GetType().Name]);
                        csvWriters.Add(businessAttributeValue.GetType().Name, newBusinessAttributeValueCsvWriter);
                        newBusinessAttributeValueCsvWriter.WriteHeader <BusinessAttributeValue>();

                        // business phones
                        var businessPhone             = new BusinessPhone();
                        var newBusinessPhoneCsvWriter = new CsvWriter(textWriters[businessPhone.GetType().Name]);
                        csvWriters.Add(businessPhone.GetType().Name, newBusinessPhoneCsvWriter);
                        newBusinessPhoneCsvWriter.WriteHeader <BusinessPhone>();

                        // business addresses
                        var businessAddress             = new BusinessAddress();
                        var newBusinessAddressCsvWriter = new CsvWriter(textWriters[businessAddress.GetType().Name]);
                        csvWriters.Add(businessAddress.GetType().Name, newBusinessAddressCsvWriter);
                        newBusinessAddressCsvWriter.WriteHeader <BusinessAddress>();

                        // business Contacts
                        var businessContacts             = new BusinessContact();
                        var newBusinessContactsCsvWriter = new CsvWriter(textWriters[businessContacts.GetType().Name]);
                        csvWriters.Add(businessContacts.GetType().Name, newBusinessContactsCsvWriter);
                        newBusinessContactsCsvWriter.WriteHeader <BusinessContact>();
                    }
                }

                var csvWriter = csvWriters[typeName];

                csvWriter.WriteRecord <T>(model);

                // if person model write out any related models
                if (importModel is Person)
                {
                    // person attributes
                    var personAttributeValue          = new PersonAttributeValue();
                    var csvPersonAttributeValueWriter = csvWriters[personAttributeValue.GetType().Name];

                    if (csvPersonAttributeValueWriter != null)
                    {
                        foreach (var attribute in (( Person )importModel).Attributes)
                        {
                            csvPersonAttributeValueWriter.WriteRecord(attribute);
                        }
                    }

                    // person phones
                    var personPhone          = new PersonPhone();
                    var csvPersonPhoneWriter = csvWriters[personPhone.GetType().Name];

                    if (csvPersonPhoneWriter != null)
                    {
                        foreach (var phone in (( Person )importModel).PhoneNumbers)
                        {
                            csvPersonPhoneWriter.WriteRecord(phone);
                        }
                    }

                    // person addresses
                    var personAddress          = new PersonAddress();
                    var csvPersonAddressWriter = csvWriters[personAddress.GetType().Name];

                    if (csvPersonAddressWriter != null)
                    {
                        foreach (var address in (( Person )importModel).Addresses)
                        {
                            if ((( Person )importModel).FamilyId.HasValue)
                            {
                                var familyAddress = new FamilyAddress
                                {
                                    FamilyId   = (( Person )importModel).FamilyId.Value,
                                    Street1    = address.Street1,
                                    PostalCode = address.PostalCode.Left(5)
                                };

                                var index = _familyAddresses.FindIndex(a =>
                                                                       a.FamilyId == (( Person )importModel).FamilyId.Value &&
                                                                       a.Street1.Equals(address.Street1, StringComparison.OrdinalIgnoreCase) &&
                                                                       a.PostalCode.Equals(address.PostalCode.Left(5)));

                                if (index == -1)
                                {
                                    _familyAddresses.Add(familyAddress);
                                    csvPersonAddressWriter.WriteRecord(address);
                                }
                            }
                            else
                            {
                                csvPersonAddressWriter.WriteRecord(address);
                            }
                        }
                    }

                    // person search keys
                    var personSearchKey          = new PersonSearchKey();
                    var csvPersonSearchKeyWriter = csvWriters[personSearchKey.GetType().Name];

                    if (csvPersonSearchKeyWriter != null)
                    {
                        foreach (var searchKey in (( Person )importModel).PersonSearchKeys)
                        {
                            csvPersonSearchKeyWriter.WriteRecord(searchKey);
                        }
                    }
                }

                // if financial model write out any related models
                if (importModel is FinancialBatch)
                {
                    // write out financial transactions and transaction details
                    var financialTransaction          = new FinancialTransaction();
                    var csvFinancialTransactionWriter = csvWriters[financialTransaction.GetType().Name];

                    var financialTransactionDetail          = new FinancialTransactionDetail();
                    var csvFinancialTransactionDetailWriter = csvWriters[financialTransactionDetail.GetType().Name];

                    if (csvFinancialTransactionWriter != null && csvFinancialTransactionDetailWriter != null)
                    {
                        foreach (var transaction in (( FinancialBatch )importModel).FinancialTransactions)
                        {
                            csvFinancialTransactionWriter.WriteRecord(transaction);

                            foreach (var transactionDetail in transaction.FinancialTransactionDetails)
                            {
                                csvFinancialTransactionDetailWriter.WriteRecord(transactionDetail);
                            }
                        }
                    }
                }

                // if financial Transaction model write out any related models
                if (importModel is FinancialTransaction)
                {
                    var financialTransactionDetail          = new FinancialTransactionDetail();
                    var csvFinancialTransactionDetailWriter = csvWriters[financialTransactionDetail.GetType().Name];

                    if (csvFinancialTransactionDetailWriter != null)
                    {
                        foreach (var transactionDetail in (( FinancialTransaction )importModel).FinancialTransactionDetails)
                        {
                            csvFinancialTransactionDetailWriter.WriteRecord(transactionDetail);
                        }
                    }
                }

                // if group model write out any related models
                if (importModel is Group)
                {
                    // group members
                    var groupMember          = new GroupMember();
                    var csvGroupMemberWriter = csvWriters[groupMember.GetType().Name];

                    if (csvGroupMemberWriter != null)
                    {
                        foreach (var groupMemberItem in (( Group )importModel).GroupMembers)
                        {
                            csvGroupMemberWriter.WriteRecord(groupMemberItem);
                        }
                    }

                    // group attributes
                    var groupAttributeValue          = new GroupAttributeValue();
                    var csvGroupAttributeValueWriter = csvWriters[groupAttributeValue.GetType().Name];

                    if (csvGroupAttributeValueWriter != null)
                    {
                        foreach (var attribute in (( Group )importModel).Attributes)
                        {
                            csvGroupAttributeValueWriter.WriteRecord(attribute);
                        }
                    }

                    // group addresses
                    var groupAddress          = new GroupAddress();
                    var csvGroupAddressWriter = csvWriters[groupAddress.GetType().Name];

                    if (csvGroupAddressWriter != null)
                    {
                        foreach (var address in (( Group )importModel).Addresses)
                        {
                            csvGroupAddressWriter.WriteRecord(address);
                        }
                    }
                }

                // if business model write out any related models
                if (importModel is Business)
                {
                    // business attributes
                    var personBusinessValue             = new BusinessAttributeValue();
                    var csvBusinessAttributeValueWriter = csvWriters[personBusinessValue.GetType().Name];

                    if (csvBusinessAttributeValueWriter != null)
                    {
                        foreach (var attribute in (( Business )importModel).Attributes)
                        {
                            csvBusinessAttributeValueWriter.WriteRecord(attribute);
                        }
                    }

                    // business phones
                    var businessPhone          = new BusinessPhone();
                    var csvBusinessPhoneWriter = csvWriters[businessPhone.GetType().Name];

                    if (csvBusinessPhoneWriter != null)
                    {
                        foreach (var phone in (( Business )importModel).PhoneNumbers)
                        {
                            csvBusinessPhoneWriter.WriteRecord(phone);
                        }
                    }

                    // business addresses
                    var businessAddress          = new BusinessAddress();
                    var csvBusinessAddressWriter = csvWriters[businessAddress.GetType().Name];

                    if (csvBusinessAddressWriter != null)
                    {
                        foreach (var address in (( Business )importModel).Addresses)
                        {
                            csvBusinessAddressWriter.WriteRecord(address);
                        }
                    }

                    // business contacts
                    var businessContact          = new BusinessContact();
                    var csvBusinessContactWriter = csvWriters[businessContact.GetType().Name];

                    if (csvBusinessContactWriter != null)
                    {
                        foreach (var contact in (( Business )importModel).Contacts)
                        {
                            csvBusinessAddressWriter.WriteRecord(contact);
                        }
                    }
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Writes to package.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model">The model.</param>
        public static void WriteToPackage <T>(T model)
        {
            var typeName = model.GetType().Name;

            if (model is IImportModel)
            {
                var importModel = (IImportModel)model;
                // check if a textwriter is needed for this model type
                if (!textWriters.ContainsKey(typeName))
                {
                    if (!Directory.Exists(_packageDirectory))
                    {
                        InitalizePackageFolder();
                    }

                    textWriters.Add(typeName, (TextWriter)File.CreateText($@"{_packageDirectory}\{importModel.GetFileName()}"));

                    // if model is for person create related writers
                    if (importModel is Person)
                    {
                        // person attributes
                        var personAttributeValue = new PersonAttributeValue();
                        textWriters.Add(personAttributeValue.GetType().Name, (TextWriter)File.CreateText($@"{_packageDirectory}\{personAttributeValue.GetFileName()}"));

                        // person phones
                        var personPhone = new PersonPhone();
                        textWriters.Add(personPhone.GetType().Name, (TextWriter)File.CreateText($@"{_packageDirectory}\{personPhone.GetFileName()}"));

                        // person addresses
                        var personAddress = new PersonAddress();
                        textWriters.Add(personAddress.GetType().Name, (TextWriter)File.CreateText($@"{_packageDirectory}\{personAddress.GetFileName()}"));
                    }

                    // if model is for financial batch create related writers
                    if (importModel is FinancialBatch)
                    {
                        // financial transactions
                        var financialTransaction = new FinancialTransaction();
                        textWriters.Add(financialTransaction.GetType().Name, (TextWriter)File.CreateText($@"{_packageDirectory}\{financialTransaction.GetFileName()}"));

                        // financial transation details
                        var financialTransactionDetail = new FinancialTransactionDetail();
                        textWriters.Add(financialTransactionDetail.GetType().Name, (TextWriter)File.CreateText($@"{_packageDirectory}\{financialTransactionDetail.GetFileName()}"));
                    }

                    // if model is for financial transaction create related writers
                    if (importModel is FinancialTransaction)
                    {
                        // financial transation details
                        var financialTransactionDetail = new FinancialTransactionDetail();
                        textWriters.Add(financialTransactionDetail.GetType().Name, ( TextWriter )File.CreateText($@"{_packageDirectory}\{financialTransactionDetail.GetFileName()}"));
                    }

                    // if model is for group create related writers
                    if (importModel is Group)
                    {
                        // group member
                        var groupMember = new GroupMember();
                        textWriters.Add(groupMember.GetType().Name, (TextWriter)File.CreateText($@"{_packageDirectory}\{groupMember.GetFileName()}"));

                        // group attributes
                        var groupAttributeValue = new GroupAttributeValue();
                        textWriters.Add(groupAttributeValue.GetType().Name, (TextWriter)File.CreateText($@"{_packageDirectory}\{groupAttributeValue.GetFileName()}"));

                        // group addresses
                        var groupAddress = new GroupAddress();
                        textWriters.Add(groupAddress.GetType().Name, (TextWriter)File.CreateText($@"{_packageDirectory}\{groupAddress.GetFileName()}"));
                    }
                }

                var txtWriter = textWriters[typeName];

                // check if a csvwriter is needed for this model type
                if (!csvWriters.ContainsKey(typeName))
                {
                    var newCsvWriter = new CsvWriter(txtWriter);
                    csvWriters.Add(typeName, newCsvWriter);
                    newCsvWriter.WriteHeader <T>();
                    //newCsvWriter.Configuration.QuoteAllFields = true;

                    // if model is for person create related writers
                    if (importModel is Person)
                    {
                        // person attributes
                        var personAttributeValue             = new PersonAttributeValue();
                        var newPersonAttributeValueCsvWriter = new CsvWriter(textWriters[personAttributeValue.GetType().Name]);
                        csvWriters.Add(personAttributeValue.GetType().Name, newPersonAttributeValueCsvWriter);
                        newPersonAttributeValueCsvWriter.WriteHeader <PersonAttributeValue>();

                        // person phones
                        var personPhone             = new PersonPhone();
                        var newPersonPhoneCsvWriter = new CsvWriter(textWriters[personPhone.GetType().Name]);
                        csvWriters.Add(personPhone.GetType().Name, newPersonPhoneCsvWriter);
                        newPersonPhoneCsvWriter.WriteHeader <PersonPhone>();

                        // person addresses
                        var personAddress             = new PersonAddress();
                        var newPersonAddressCsvWriter = new CsvWriter(textWriters[personAddress.GetType().Name]);
                        csvWriters.Add(personAddress.GetType().Name, newPersonAddressCsvWriter);
                        newPersonAddressCsvWriter.WriteHeader <PersonAddress>();
                    }

                    // if model is for financial batch create related writers
                    if (importModel is FinancialBatch)
                    {
                        // financial transaction
                        var financialTransaction             = new FinancialTransaction();
                        var newFinancialTransactionCsvWriter = new CsvWriter(textWriters[financialTransaction.GetType().Name]);
                        csvWriters.Add(financialTransaction.GetType().Name, newFinancialTransactionCsvWriter);
                        newFinancialTransactionCsvWriter.WriteHeader <FinancialTransaction>();

                        // financial transaction detail
                        var financialTransactionDetail             = new FinancialTransactionDetail();
                        var newFinancialTransactionDetailCsvWriter = new CsvWriter(textWriters[financialTransactionDetail.GetType().Name]);
                        csvWriters.Add(financialTransactionDetail.GetType().Name, newFinancialTransactionDetailCsvWriter);
                        newFinancialTransactionDetailCsvWriter.WriteHeader <FinancialTransactionDetail>();
                    }

                    //if model is for financial transaction, create related writers
                    if (importModel is FinancialTransaction)
                    {
                        // financial transaction detail
                        var financialTransactionDetail             = new FinancialTransactionDetail();
                        var newFinancialTransactionDetailCsvWriter = new CsvWriter(textWriters[financialTransactionDetail.GetType().Name]);
                        csvWriters.Add(financialTransactionDetail.GetType().Name, newFinancialTransactionDetailCsvWriter);
                        newFinancialTransactionDetailCsvWriter.WriteHeader <FinancialTransactionDetail>();
                    }

                    // if model is for group create related writers
                    if (importModel is Group)
                    {
                        // group member
                        var groupMember             = new GroupMember();
                        var newGroupMemberCsvWriter = new CsvWriter(textWriters[groupMember.GetType().Name]);
                        csvWriters.Add(groupMember.GetType().Name, newGroupMemberCsvWriter);
                        newGroupMemberCsvWriter.WriteHeader <GroupMember>();

                        // group attributes
                        var groupAttributeValue             = new GroupAttributeValue();
                        var newGroupAttributeValueCsvWriter = new CsvWriter(textWriters[groupAttributeValue.GetType().Name]);
                        csvWriters.Add(groupAttributeValue.GetType().Name, newGroupAttributeValueCsvWriter);
                        newGroupAttributeValueCsvWriter.WriteHeader <GroupAttributeValue>();

                        // group addresses
                        var groupAddress             = new GroupAddress();
                        var newGroupAddressCsvWriter = new CsvWriter(textWriters[groupAddress.GetType().Name]);
                        csvWriters.Add(groupAddress.GetType().Name, newGroupAddressCsvWriter);
                        newGroupAddressCsvWriter.WriteHeader <GroupAddress>();
                    }
                }

                var csvWriter = csvWriters[typeName];

                csvWriter.WriteRecord <T>(model);

                // if person model write out any related models
                if (importModel is Person)
                {
                    // person attributes
                    var personAttributeValue          = new PersonAttributeValue();
                    var csvPersonAttributeValueWriter = csvWriters[personAttributeValue.GetType().Name];

                    if (csvPersonAttributeValueWriter != null)
                    {
                        foreach (var attribute in ((Person)importModel).Attributes)
                        {
                            csvPersonAttributeValueWriter.WriteRecord(attribute);
                        }
                    }

                    // person phones
                    var personPhone          = new PersonPhone();
                    var csvPersonPhoneWriter = csvWriters[personPhone.GetType().Name];

                    if (csvPersonPhoneWriter != null)
                    {
                        foreach (var phone in ((Person)importModel).PhoneNumbers)
                        {
                            csvPersonPhoneWriter.WriteRecord(phone);
                        }
                    }

                    // person addresses
                    var personAddress          = new PersonAddress();
                    var csvPersonAddressWriter = csvWriters[personAddress.GetType().Name];

                    if (csvPersonAddressWriter != null)
                    {
                        foreach (var address in ((Person)importModel).Addresses)
                        {
                            csvPersonAddressWriter.WriteRecord(address);
                        }
                    }
                }

                // if financial model write out any related models
                if (importModel is FinancialBatch)
                {
                    // write out financial transactions and transaction details
                    var financialTransaction          = new FinancialTransaction();
                    var csvFinancialTransactionWriter = csvWriters[financialTransaction.GetType().Name];

                    var financialTransactionDetail          = new FinancialTransactionDetail();
                    var csvFinancialTransactionDetailWriter = csvWriters[financialTransactionDetail.GetType().Name];

                    if (csvFinancialTransactionWriter != null && csvFinancialTransactionDetailWriter != null)
                    {
                        foreach (var transaction in ((FinancialBatch)importModel).FinancialTransactions)
                        {
                            csvFinancialTransactionWriter.WriteRecord(transaction);

                            foreach (var transactionDetail in transaction.FinancialTransactionDetails)
                            {
                                csvFinancialTransactionDetailWriter.WriteRecord(transactionDetail);
                            }
                        }
                    }
                }

                // if financial Transaction model write out any related models
                if (importModel is FinancialTransaction)
                {
                    var financialTransactionDetail          = new FinancialTransactionDetail();
                    var csvFinancialTransactionDetailWriter = csvWriters[financialTransactionDetail.GetType().Name];

                    if (csvFinancialTransactionDetailWriter != null)
                    {
                        foreach (var transactionDetail in ((FinancialTransaction)importModel).FinancialTransactionDetails)
                        {
                            csvFinancialTransactionDetailWriter.WriteRecord(transactionDetail);
                        }
                    }
                }

                // if group model write out any related models
                if (importModel is Group)
                {
                    // group members
                    var groupMember          = new GroupMember();
                    var csvGroupMemberWriter = csvWriters[groupMember.GetType().Name];

                    if (csvGroupMemberWriter != null)
                    {
                        foreach (var groupMemberItem in ((Group)importModel).GroupMembers)
                        {
                            csvGroupMemberWriter.WriteRecord(groupMemberItem);
                        }
                    }

                    // group attributes
                    var groupAttributeValue           = new GroupAttributeValue();
                    var csvPersonAttributeValueWriter = csvWriters[groupAttributeValue.GetType().Name];

                    if (csvPersonAttributeValueWriter != null)
                    {
                        foreach (var attribute in ((Group)importModel).Attributes)
                        {
                            csvPersonAttributeValueWriter.WriteRecord(attribute);
                        }
                    }

                    // group addresses
                    var groupAddress          = new GroupAddress();
                    var csvGroupAddressWriter = csvWriters[groupAddress.GetType().Name];

                    if (csvGroupAddressWriter != null)
                    {
                        foreach (var address in ((Group)importModel).Addresses)
                        {
                            csvGroupAddressWriter.WriteRecord(address);
                        }
                    }
                }
            }
        }