/// <summary>
        /// Update PID segment from a child
        /// </summary>
        private void UpdatePID(PID pid, GIIS.DataLayer.Child child, SyncData dao = null)
        {
            pid.AdministrativeSex.Value = child.Gender ? "M" : "F";
            if (child.Birthplace != null)
            {
                pid.BirthPlace.Value = child.Birthplace.Name;
            }
            pid.DateTimeOfBirth.Time.Value = new TS(child.Birthdate, DatePrecision.Day).Value;

            // Ethnic group
            if (child.Community != null)
            {
                pid.GetEthnicGroup(0).Identifier.Value = child.Community.Name;
            }

            if (!String.IsNullOrEmpty(child.Phone))
            {
                pid.GetPhoneNumberHome(0).AnyText.Value = child.Phone;
            }

            if (!String.IsNullOrEmpty(child.MotherFirstname))
            {
                pid.GetMotherSMaidenName(0).GivenName.Value = child.MotherFirstname;
            }
            if (!String.IsNullOrEmpty(child.MotherLastname))
            {
                pid.GetMotherSMaidenName(0).FamilyName.Surname.Value = child.MotherLastname;
            }

            if (!String.IsNullOrEmpty(child.Firstname1))
            {
                pid.GetPatientName(0).GivenName.Value = child.Firstname1;
            }
            if (!String.IsNullOrEmpty(child.Firstname2))
            {
                pid.GetPatientName(0).SecondAndFurtherGivenNamesOrInitialsThereof.Value = child.Firstname2;
            }
            if (!String.IsNullOrEmpty(child.Lastname1))
            {
                pid.GetPatientName(0).FamilyName.Surname.Value = child.Lastname1;
            }
            if (!String.IsNullOrEmpty(child.Lastname2))
            {
                pid.GetPatientName(0).FamilyName.OwnSurname.Value = child.Lastname2;
            }

            // Domicile
            if (child.Domicile != null)
            {
                if (!String.IsNullOrEmpty(child.Domicile.Code))
                {
                    pid.GetPatientAddress(0).CensusTract.Value = child.Domicile.Code;
                }

                Queue <AbstractPrimitive> addressParts = new Queue <AbstractPrimitive>(new AbstractPrimitive[] {
                    pid.GetPatientAddress(0).OtherDesignation,                     // Kitongoji
                    pid.GetPatientAddress(0).StreetAddress.StreetOrMailingAddress, // Street or Village
                    pid.GetPatientAddress(0).City,                                 // Ward
                    pid.GetPatientAddress(0).CountyParishCode,                     // district
                    pid.GetPatientAddress(0).StateOrProvince,                      // Region
                    pid.GetPatientAddress(0).Country                               // National
                });

                // Queue places
                Queue <GIIS.DataLayer.Place> domicileParts = new Queue <GIIS.DataLayer.Place>();
                GIIS.DataLayer.Place         current       = child.Domicile;
                while (current != null)
                {
                    domicileParts.Enqueue(current);
                    current = current.Parent;
                }

                // Now trim
                while (addressParts.Count > domicileParts.Count)
                {
                    addressParts.Dequeue();
                }

                // Now map
                while (domicileParts.Count > 0)
                {
                    addressParts.Dequeue().Value = domicileParts.Dequeue().Name;
                }
            }

            if (child.Status.Name == "Dead")
            {
                pid.PatientDeathIndicator.Value = "Y";
            }

            // Identifiers
            int i = 0;

            pid.GetPatientIdentifierList(i).IDNumber.Value = child.Id.ToString();
            pid.GetPatientIdentifierList(i).AssigningAuthority.UniversalID.Value       = ConfigurationManager.AppSettings["giis_child_id_oid"];
            pid.GetPatientIdentifierList(i++).AssigningAuthority.UniversalIDType.Value = "ISO";

            // 1st alt
            if (!String.IsNullOrEmpty(child.IdentificationNo1) && !String.IsNullOrEmpty(ConfigurationManager.AppSettings["giis_child_alt_id_1_oid"]))
            {
                pid.GetPatientIdentifierList(i).IDNumber.Value = child.IdentificationNo1.ToString();
                pid.GetPatientIdentifierList(i).AssigningAuthority.UniversalID.Value       = ConfigurationManager.AppSettings["giis_child_alt_id_1_oid"];
                pid.GetPatientIdentifierList(i++).AssigningAuthority.UniversalIDType.Value = "ISO";
            }
            // 2nd alt
            if (!String.IsNullOrEmpty(child.IdentificationNo2) && !String.IsNullOrEmpty(ConfigurationManager.AppSettings["giis_child_alt_id_2_oid"]))
            {
                pid.GetPatientIdentifierList(i).IDNumber.Value = child.IdentificationNo2.ToString();
                pid.GetPatientIdentifierList(i).AssigningAuthority.UniversalID.Value       = ConfigurationManager.AppSettings["giis_child_alt_id_2_oid"];
                pid.GetPatientIdentifierList(i++).AssigningAuthority.UniversalIDType.Value = "ISO";
            }
            // 3rd alt
            if (!String.IsNullOrEmpty(child.IdentificationNo3) && !String.IsNullOrEmpty(ConfigurationManager.AppSettings["giis_child_alt_id_3_oid"]))
            {
                pid.GetPatientIdentifierList(i).IDNumber.Value = child.IdentificationNo3.ToString();
                pid.GetPatientIdentifierList(i).AssigningAuthority.UniversalID.Value       = ConfigurationManager.AppSettings["giis_child_alt_id_3_oid"];
                pid.GetPatientIdentifierList(i++).AssigningAuthority.UniversalIDType.Value = "ISO";
            }
            // Barcode
            if (!String.IsNullOrEmpty(child.BarcodeId))
            {
                pid.GetPatientIdentifierList(i).IDNumber.Value = child.BarcodeId.ToString();
                pid.GetPatientIdentifierList(i).AssigningAuthority.UniversalID.Value       = ConfigurationManager.AppSettings["giis_child_barcode_oid"];
                pid.GetPatientIdentifierList(i++).AssigningAuthority.UniversalIDType.Value = "ISO";
            }

            // ECID?
            if (dao == null)
            {
                dao = new SyncData();
            }
            string ecid = dao.GetPatientEcid(child.Id);

            if (ecid != null)
            {
                pid.GetPatientIdentifierList(i).IDNumber.Value = ecid;
                pid.GetPatientIdentifierList(i).AssigningAuthority.UniversalID.Value       = ConfigurationManager.AppSettings["ecid"];
                pid.GetPatientIdentifierList(i++).AssigningAuthority.UniversalIDType.Value = "ISO";
            }
        }
        /// <summary>
        /// Update a child record to match the PID segment
        /// </summary>
        private void CopyChildRecordFields(GIIS.DataLayer.Child child, PID pid)
        {
            // Gender
            if (!String.IsNullOrEmpty(pid.AdministrativeSex.Value))
            {
                child.Gender = pid.AdministrativeSex.Value == "M";
            }

            // Date of birth
            if (!String.IsNullOrEmpty(pid.DateTimeOfBirth.Time.Value))
            {
                child.Birthdate = (DateTime)(TS)pid.DateTimeOfBirth.Time.Value;
            }

            // Is part of a multiple birth?
            if (!String.IsNullOrEmpty(pid.MultipleBirthIndicator.Value))
            {
                child.Notes = "twin";
            }

            // Phone
            if (!String.IsNullOrEmpty(pid.GetPhoneNumberHome(0).TelephoneNumber.Value))
            {
                child.Phone = pid.GetPhoneNumberHome(0).TelephoneNumber.Value;
            }
            else if (!String.IsNullOrEmpty(pid.GetPhoneNumberHome(0).AnyText.Value))
            {
                child.Phone = pid.GetPhoneNumberHome(0).AnyText.Value;
            }

            // Mother's name?
            if (pid.MotherSMaidenNameRepetitionsUsed > 0)
            {
                child.MotherLastname  = pid.GetMotherSMaidenName()[0].FamilyName.Surname.Value;
                child.MotherFirstname = pid.GetMotherSMaidenName()[0].GivenName.Value;
            }

            // Name
            if (pid.PatientNameRepetitionsUsed > 0)
            {
                child.Lastname1  = pid.GetPatientName()[0].FamilyName.Surname.Value;
                child.Lastname2  = pid.GetPatientName()[0].FamilyName.OwnSurname.Value;
                child.Firstname1 = pid.GetPatientName()[0].GivenName.Value;
                child.Firstname2 = pid.GetPatientName()[0].SecondAndFurtherGivenNamesOrInitialsThereof.Value;
            }

            // Identification numbers
            foreach (var id in pid.GetPatientIdentifierList())
            {
                if (id.AssigningAuthority.UniversalID.Value == ConfigurationManager.AppSettings["giis_child_alt_id_1_oid"])
                {
                    child.IdentificationNo1 = id.IDNumber.Value;
                }
                else if (id.AssigningAuthority.UniversalID.Value == ConfigurationManager.AppSettings["giis_child_alt_id_2_oid"])
                {
                    child.IdentificationNo2 = id.IDNumber.Value;
                }
                else if (id.AssigningAuthority.UniversalID.Value == ConfigurationManager.AppSettings["giis_child_alt_id_3_oid"])
                {
                    child.IdentificationNo3 = id.IDNumber.Value;
                }
                else if (id.AssigningAuthority.UniversalID.Value == ConfigurationManager.AppSettings["giis_child_barcode_oid"])
                {
                    child.BarcodeId = id.IDNumber.Value;
                }
                else if (id.AssigningAuthority.UniversalID.Value == ConfigurationManager.AppSettings["ecid"])
                {
                    child.TempId = id.IDNumber.Value;
                }
            }

            // Birthplace -
            // TODO: How does this map to GIIS?
            if (!String.IsNullOrEmpty(pid.BirthPlace.Value))
            {
                var place = GIIS.DataLayer.Birthplace.GetBirthplaceByName(pid.BirthPlace.Value);
                if (place == null)
                {
                    Trace.TraceWarning("{0} : Could not map birthplace {1} for child {2}", this.m_context.JobId, pid.BirthPlace.Value, child.Id);
                }
                else
                {
                    child.BirthplaceId = place.Id;
                }
            }

            // Address information
            // TODO: How does this map to GIIS?
            if (pid.PatientAddressRepetitionsUsed > 0)
            {
                // Domicile = Cascade down the heirarchy
                var xad = pid.GetPatientAddress()[0];
                GIIS.DataLayer.Place current = null;

                if (!String.IsNullOrEmpty(xad.CensusTract.Value)) // prefer census tract
                {
                    current = GIIS.DataLayer.Place.GetPlaceByCode(xad.CensusTract.Value);
                }
                if (current == null)
                {
                    Queue <String> placeSearch = new Queue <string>(new String[] {
                        xad.Country.Value,
                        xad.StateOrProvince.Value,                      // Region
                        xad.CountyParishCode.Value,                     // District
                        xad.City.Value,                                 // Ward
                        xad.StreetAddress.StreetOrMailingAddress.Value, // Village
                        xad.OtherDesignation.Value                      // Kitongoji
                    });
                    while (placeSearch.Count > 0)
                    {
                        var xadPart = placeSearch.Dequeue();
                        if (xadPart == null)
                        {
                            continue;
                        }

                        var candidate = GIIS.DataLayer.Place.GetPlaceByName(xadPart, current == null ? 0 : current.Id);
                        if (candidate == null)
                        {
                            break;
                        }
                        current = candidate;
                    }
                }
                if (current != null)
                {
                    if (child.DomicileId != current.Id)
                    {
                        child.DomicileId = current.Id;
                        if (child.Healthcenter == null)
                        {
                            if (current.HealthFacility != null)
                            {
                                child.HealthcenterId = current.HealthFacility.Id;
                            }
                            else
                            {
                                child.HealthcenterId = Int32.Parse(ConfigurationManager.AppSettings["giis_unknown_facility"]);
                            }
                        }
                        else
                        {
                            Trace.TraceInformation("Will not update child {0} health facility", child.Id);
                        }
                    }
                }
            }

            if (child.Healthcenter == null)
            {
                child.HealthcenterId = Int32.Parse(ConfigurationManager.AppSettings["giis_unknown_facility"]);
            }


            // Death?
            if (pid.PatientDeathIndicator.Value == "Y")
            {
                child.StatusId = GIIS.DataLayer.Status.GetStatusByName("Dead").Id;
                child.IsActive = false;
            }
            else
            {
                child.StatusId = GIIS.DataLayer.Status.GetStatusByName("Active").Id;
                child.IsActive = true;
            }
            child.StatusId = child.Status.Id;

            if (pid.LastUpdateDateTime.Time.Value != null)
            {
                child.ModifiedOn = ((TS)pid.LastUpdateDateTime.Time.Value).DateValue;
            }
            else
            {
                child.ModifiedOn = DateTime.Now;
            }
            child.ModifiedBy = Int32.Parse(ConfigurationManager.AppSettings["giis_authority_user_id"]);

            if (child.SystemId == null)
            {
                lock (this.m_syncState)
                    child.SystemId = String.Format("{0:X16}", BitConverter.ToInt64(Guid.NewGuid().ToByteArray().Take(16).ToArray(), 0), BitConverter.ToInt64(Guid.NewGuid().ToByteArray().Skip(8).Take(8).ToArray(), 0));
            }
        }