private Dictionary <int, string> ConvertToDictionary(Models.ChildCaseStudy ccs)
        {
            var dictionary = new Dictionary <int, string>();
            int i          = 0;

            AddColumn(ref dictionary, i++, "Individual Child Sponsorship");

            if (_locations != null)
            {
                // Memphis 9/3/13: force to uppercase to prevent mismatch of location codes when not all uppercase in the XML:
                //var location = _locations.Where(l => l.Key == ccs.Project.Number).FirstOrDefault();
                var location = _locations.Where(l => l.Key == ccs.Project.Number.ToUpper()).FirstOrDefault();
                AddColumn(ref dictionary, i++, location.Value);
            }
            else
            {
                AddColumn(ref dictionary, i++, string.Empty);
            }

            AddColumn(ref dictionary, i++, ccs.LastName);
            AddColumn(ref dictionary, i++, ccs.Gender);
            AddColumn(ref dictionary, i++, ccs.Father.WorksAs);
            AddColumn(ref dictionary, i++, ccs.Mother.WorksAs);
            AddColumn(ref dictionary, i++, string.Empty);             //photo date
            AddColumn(ref dictionary, i++, string.Empty);             //status
            AddColumn(ref dictionary, i++, ccs.AdditionalInformation);
            AddColumn(ref dictionary, i++, ccs.Housing.AreaDescription);
            AddColumn(ref dictionary, i++, ccs.School.AttendsSchool);
            AddColumn(ref dictionary, i++, ccs.Housing.BambooWalls);
            AddColumn(ref dictionary, i++, ccs.DOB.ToShortDateString());
            AddColumn(ref dictionary, i++, ccs.BirthDateAccuracy);
            AddColumn(ref dictionary, i++, ccs.Housing.BlockWalls);
            AddColumn(ref dictionary, i++, ccs.Housing.WellWater);
            AddColumn(ref dictionary, i++, ccs.CareGiver.ReasonFor);
            AddColumn(ref dictionary, i++, ccs.CareGiver.ReasonForDetail);
            AddColumn(ref dictionary, i++, ccs.CareGiver.Relationship);
            AddColumn(ref dictionary, i++, ccs.CareGiver.RelationshipDetail);
            AddColumn(ref dictionary, i++, ccs.CareGiver.WorksAs);
            AddColumn(ref dictionary, i++, ccs.CareGiver.WorksAsDetail.Left(100));
            AddColumn(ref dictionary, i++, ccs.WorkstationID);
            if (FixTempChildID)
            {
                AddColumn(ref dictionary, i++, ccs.TempChildID + FixTempChildIDSuffix);
            }
            else
            {
                AddColumn(ref dictionary, i++, ccs.TempChildID);
            }
            AddColumn(ref dictionary, i++, ccs.AboutMe.SpiritualDevelopment);
            AddColumn(ref dictionary, i++, ccs.AboutMe.PhysicalDevelopment);
            AddColumn(ref dictionary, i++, ccs.ChildLivesWith);
            AddColumn(ref dictionary, i++, ccs.Suffix);
            AddColumn(ref dictionary, i++, ccs.AboutMe.HowTheChildInteracts);
            AddColumn(ref dictionary, i++, _selectedChildProfileUpdate);             //Child Profile Update
            AddColumn(ref dictionary, i++, ccs.School.ClassLevel);
            AddColumn(ref dictionary, i++, ccs.Housing.CommunityWater);
            AddColumn(ref dictionary, i++, ccs.Housing.OtherCookingDescription.OtherDescription(ccs.Housing.OtherCooking));
            AddColumn(ref dictionary, i++, string.Empty);             //current completion date - should this map to completion date from xml or original?
            AddColumn(ref dictionary, i++, string.Empty);             //Disability/Illness
            AddColumn(ref dictionary, i++, ccs.Housing.ElectricCooking);
            AddColumn(ref dictionary, i++, ccs.Housing.ElectrictyLight);
            AddColumn(ref dictionary, i++, ccs.Father.WorksAsDetail);             //Father Works As Other
            AddColumn(ref dictionary, i++, ccs.School.BestSubject);
            AddColumn(ref dictionary, i++, ccs.FirstName);
            AddColumn(ref dictionary, i++, false);             //Funded
            AddColumn(ref dictionary, i++, ccs.Housing.GasCooking);
            AddColumn(ref dictionary, i++, ccs.Housing.GeneratorLight);
            AddColumn(ref dictionary, i++, ccs.Housing.StrawRoof);
            AddColumn(ref dictionary, i++, false);             //HIV positive
            AddColumn(ref dictionary, i++, string.Empty);      //Child photo
            AddColumn(ref dictionary, i++, ccs.Housing.IndoorWater);
            AddColumn(ref dictionary, i++, ccs.Housing.OtherLightDescription.OtherDescription(ccs.Housing.OtherLight));
            AddColumn(ref dictionary, i++, ccs.Housing.NoneLight);
            AddColumn(ref dictionary, i++, ccs.MiddleName);
            AddColumn(ref dictionary, i++, ccs.Mother.WorksAsDetail);
            AddColumn(ref dictionary, i++, ccs.Housing.MudWalls);
            AddColumn(ref dictionary, i++, ccs.AboutMe.FavoriteThingsToDo);
            AddColumn(ref dictionary, i++, ccs.AboutMe.TwoFavoriteThingsAndWhy);
            AddColumn(ref dictionary, i++, ccs.Brothers);
            AddColumn(ref dictionary, i++, ccs.Sisters);
            AddColumn(ref dictionary, i++, ccs.Housing.LampLight);
            AddColumn(ref dictionary, i++, string.Empty);      //original completion date - should this map to completion date from xml or current?
            AddColumn(ref dictionary, i++, false);             //orphaned
            AddColumn(ref dictionary, i++, ccs.AboutMe.AlsoEnjoys);
            AddColumn(ref dictionary, i++, ccs.Housing.OtherWallsDescription.OtherDescription(ccs.Housing.OtherWalls));
            AddColumn(ref dictionary, i++, false);             //photo stored
            AddColumn(ref dictionary, i++, string.Empty);      //profile update notification
            AddColumn(ref dictionary, i++, ccs.School.NonAttendenceReason);
            AddColumn(ref dictionary, i++, ccs.Housing.RiverWater);
            AddColumn(ref dictionary, i++, ccs.Housing.OtherRoofDescription.OtherDescription(ccs.Housing.OtherRoof));
            AddColumn(ref dictionary, i++, ccs.Housing.WoodRoof);
            AddColumn(ref dictionary, i++, ccs.AboutMe.WantsToBe);
            AddColumn(ref dictionary, i++, ccs.Housing.TileRoof);
            AddColumn(ref dictionary, i++, ccs.Housing.TinRoof);
            AddColumn(ref dictionary, i++, ccs.School.VocationalOrLifeSkill);
            AddColumn(ref dictionary, i++, ccs.Housing.OtherWaterDescription.OtherDescription(ccs.Housing.OtherWater));
            AddColumn(ref dictionary, i++, ccs.AboutMe.HelpsOutBy);
            AddColumn(ref dictionary, i++, ccs.AboutMe.WhenIPlayWithMyFriends);
            AddColumn(ref dictionary, i++, ccs.AboutMe.AsksGod);
            AddColumn(ref dictionary, i++, ccs.Housing.WoodCooking);
            AddColumn(ref dictionary, i++, ccs.Housing.WoodWalls);
            AddColumn(ref dictionary, i++, ccs.CCHZipFileName);

            return(dictionary);
        }
        public void CreateCSV()
        {
            bool continueProcessing = ParseLocationsCSV();

            if (!continueProcessing)
            {
                return;
            }

            if (_zipFileNames == null || _zipFileNames.Count < 1)
            {
                MessageBox.Show("At least one zip file must be selected.");
                return;
            }

            if (Path.GetExtension(OutputCSVFileName).ToLower() != ".csv")
            {
                MessageBox.Show("The output CSV filename must end with the '.csv' extension.", "Invalid Output File Extension");
                return;
            }

            if (File.Exists(OutputCSVFileName))
            {
                var result = MessageBox.Show("The output CSV file already exists, do you want to overwrite it?", "File already exists", MessageBoxButton.YesNo);

                if (result != MessageBoxResult.Yes)
                {
                    return;
                }
            }

            int row = 0;

            int truncationCount = 0;

            var dictionary = new Dictionary <int, Dictionary <int, string> >();

            dictionary.Add(row++, GetHeaders());

            if (Directory.Exists(TempFolderName))
            {
                Directory.Delete(TempFolderName, true);
            }

            if (!Directory.Exists(TempFolderName))
            {
                Directory.CreateDirectory(TempFolderName);
            }

            if (_zipFileNames != null)
            {
                foreach (var path in _zipFileNames)
                {
                    if (!continueProcessing)
                    {
                        continue;
                    }

                    using (ZipFile zipFile = ZipFile.Read(path))
                    {
                        var entries = from e in zipFile.Entries
                                      where Path.GetExtension(e.FileName).ToLower() == ".xml"
                                      select e;

                        foreach (var entry in entries)
                        {
                            if (!continueProcessing)
                            {
                                continue;
                            }

                            Models.ChildCaseStudy ccs = null;

                            // Memphis 8-13-2015 need to trap for exception here, to identify if there's a duplicate file in this .zip file:
                            try
                            {
                                entry.Extract(TempFolderName);
                            }
                            catch (ZipException zipEx)
                            {
                                // check the message for something like this: The file ZipTemp\SZ01-1498062742.xml already exists.
                                if (zipEx.Message != null)
                                {
                                    string exMsg = zipEx.Message.ToString();
                                    if ((exMsg != null || exMsg.Length > 0) && (exMsg.Contains("already exists")))
                                    {
                                        MessageBox.Show(string.Format("**WARNING***: There is a duplicate CCH form in the .zip files:{0}The duplicate CCH Form is: {1} {2}Found in this zip file: {3}", Environment.NewLine, entry.FileName, Environment.NewLine, zipFile.Name), "Duplicate CCH Form!", MessageBoxButton.OK, MessageBoxImage.Warning);
                                        //string.Format("first line{0}second line", Environment.NewLine);
                                        continueProcessing = false;
                                    }
                                }
                                else
                                {
                                    continueProcessing = false;
                                    throw;
                                }
                            }


                            XmlSerializer serializer = new XmlSerializer(typeof(Models.ChildCaseStudy));
                            using (StreamReader reader = new StreamReader(Path.Combine(TempFolderName, entry.FileName)))
                            {
                                ccs = (Models.ChildCaseStudy)serializer.Deserialize(reader);
                            }

                            if (ccs != null)
                            {
                                if (_locations != null)
                                {
                                    var location = _locations.Where(l => l.Key == ccs.Project.Number.ToUpper().Replace(" ", "").Replace("_", "-")).FirstOrDefault();                                     /* Fix project id that has a space or an underscore instead of a dash */

                                    if (location.Value != null)
                                    {
                                        ccs.Project.LocationID = location.Value;
                                    }
                                    else
                                    {
                                        continueProcessing = MessageBox.Show(string.Format("A Location ID was not found for the provided Project ID.\n\nProject ID: {0}\nFile Name: {1}\nZip File Name: {2}\n\nDo you want to continue processing?", ccs.Project.Number, entry.FileName, Path.GetFileName(zipFile.Name)), "Location ID not found", MessageBoxButton.YesNo) == MessageBoxResult.Yes;
                                    }
                                }
                                string fieldLengthErrors = "";

                                /* Check text field lengths, because the plug-in host does not limit the text boxes */
                                ccs.Father.WorksAsDetail            = TrimAndCheckFieldLength(ccs.Father.WorksAsDetail, 100, ref fieldLengthErrors, "Father works as");
                                ccs.Mother.WorksAsDetail            = TrimAndCheckFieldLength(ccs.Mother.WorksAsDetail, 100, ref fieldLengthErrors, "Mother works as");
                                ccs.CareGiver.WorksAsDetail         = TrimAndCheckFieldLength(ccs.CareGiver.WorksAsDetail, 100, ref fieldLengthErrors, "Caregiver works as");
                                ccs.CareGiver.RelationshipDetail    = TrimAndCheckFieldLength(ccs.CareGiver.RelationshipDetail, 50, ref fieldLengthErrors, "Caregiver relationship");
                                ccs.CareGiver.ReasonForDetail       = TrimAndCheckFieldLength(ccs.CareGiver.ReasonForDetail, 50, ref fieldLengthErrors, "Caregiver reason");
                                ccs.Housing.OtherWallsDescription   = TrimAndCheckFieldLength(ccs.Housing.OtherWallsDescription, 50, ref fieldLengthErrors, "Housing wall other");
                                ccs.Housing.OtherRoofDescription    = TrimAndCheckFieldLength(ccs.Housing.OtherRoofDescription, 50, ref fieldLengthErrors, "Housing roofing other");
                                ccs.Housing.OtherWaterDescription   = TrimAndCheckFieldLength(ccs.Housing.OtherWaterDescription, 50, ref fieldLengthErrors, "Housing water source other");
                                ccs.Housing.OtherCookingDescription = TrimAndCheckFieldLength(ccs.Housing.OtherCookingDescription, 50, ref fieldLengthErrors, "Housing cooking source other");
                                ccs.Housing.OtherLightDescription   = TrimAndCheckFieldLength(ccs.Housing.OtherLightDescription, 50, ref fieldLengthErrors, "Housing light source other");
                                ccs.Housing.AreaDescription         = TrimAndCheckFieldLength(ccs.Housing.AreaDescription, 1000, ref fieldLengthErrors, "Area description");
                                ccs.AboutMe.FavoriteThingsToDo      = TrimAndCheckFieldLength(ccs.AboutMe.FavoriteThingsToDo, 1000, ref fieldLengthErrors, "Favorite thing to do");
                                ccs.AboutMe.WhenIPlayWithMyFriends  = TrimAndCheckFieldLength(ccs.AboutMe.WhenIPlayWithMyFriends, 1000, ref fieldLengthErrors, "When I playing with friends");
                                ccs.AboutMe.HelpsOutBy = TrimAndCheckFieldLength(ccs.AboutMe.HelpsOutBy, 1000, ref fieldLengthErrors, "When at home I help out by");
                                ccs.AboutMe.AsksGod    = TrimAndCheckFieldLength(ccs.AboutMe.AsksGod, 1000, ref fieldLengthErrors, "When I talk to God");
                                ccs.AboutMe.WantsToBe  = TrimAndCheckFieldLength(ccs.AboutMe.WantsToBe, 1000, ref fieldLengthErrors, "Someday I would like");
                                ccs.AboutMe.TwoFavoriteThingsAndWhy = TrimAndCheckFieldLength(ccs.AboutMe.TwoFavoriteThingsAndWhy, 1000, ref fieldLengthErrors, "My most favorite things");
                                ccs.AboutMe.AlsoEnjoys           = TrimAndCheckFieldLength(ccs.AboutMe.AlsoEnjoys, 1000, ref fieldLengthErrors, "Other things the child enjoys");
                                ccs.AboutMe.HowTheChildInteracts = TrimAndCheckFieldLength(ccs.AboutMe.HowTheChildInteracts, 1000, ref fieldLengthErrors, "Child's personality");
                                ccs.AboutMe.PhysicalDevelopment  = TrimAndCheckFieldLength(ccs.AboutMe.PhysicalDevelopment, 1000, ref fieldLengthErrors, "Physical development");
                                ccs.AboutMe.SpiritualDevelopment = TrimAndCheckFieldLength(ccs.AboutMe.SpiritualDevelopment, 1000, ref fieldLengthErrors, "Spiritual development");
                                ccs.School.NonAttendenceReason   = TrimAndCheckFieldLength(ccs.School.NonAttendenceReason, 255, ref fieldLengthErrors, "Reason for not attending school");
                                ccs.School.ClassLevel            = TrimAndCheckFieldLength(ccs.School.ClassLevel, 20, ref fieldLengthErrors, "Class level");
                                ccs.School.BestSubject           = TrimAndCheckFieldLength(ccs.School.BestSubject, 1000, ref fieldLengthErrors, "Favorite subject");
                                ccs.School.VocationalOrLifeSkill = TrimAndCheckFieldLength(ccs.School.VocationalOrLifeSkill, 1000, ref fieldLengthErrors, "Vocational or life skills");


                                /* If any text box fields had to be truncated, list them in the Additional Information field because that is a memo field. */
                                if (fieldLengthErrors.Length > 0)
                                {
                                    if (string.IsNullOrEmpty(ccs.AdditionalInformation))
                                    {
                                        ccs.AdditionalInformation = "Field truncation occurred in the following field(s):" + fieldLengthErrors;
                                    }
                                    else
                                    {
                                        ccs.AdditionalInformation += "\n\nField truncation occurred in the following field(s):" + fieldLengthErrors;
                                    }

                                    truncationCount += 1;
                                }

                                ccs.CCHZipFileName = Path.GetFileNameWithoutExtension(zipFile.Name);

                                dictionary.Add(row++, ConvertToDictionary(ccs));
                            }
                        }
                    }
                }

                if (continueProcessing)
                {
                    bool success = WriteCSVFile(dictionary);

                    if (Directory.Exists(TempFolderName))
                    {
                        Directory.Delete(TempFolderName, true);
                    }

                    if (success)
                    {
                        if (truncationCount > 0)
                        {
                            MessageBox.Show(string.Format("Successfully created file at '{0}'\n\nNumber of cch forms added to file: {1}\nNumber of cch forms with truncation issues fixed: {2}", OutputCSVFileName, dictionary.Count - 1, truncationCount), "Success");
                        }
                        else
                        {
                            MessageBox.Show(string.Format("Successfully created file at '{0}'\n\nNumber of cch forms added to file: {1}", OutputCSVFileName, dictionary.Count - 1), "Success");
                        }
                    }
                }
            }
        }