public Allergy CreateAllergy(PatientAllergyDetail detail, IPersistenceContext context)
        {
            var allergy = new Allergy();

            UpdateAllergy(allergy, detail, context);
            return(allergy);
        }
 public bool CompareAllergy(Allergy allergy, PatientAllergyDetail detail)
 {
     // Not sure how can we determine two allergies are equivalent... so the only way is to compare every field.
     return(Equals(allergy.AllergenType.Code, detail.AllergenType) &&
            Equals(allergy.AllergenDescription, detail.AllergenDescription) &&
            Equals(allergy.Severity.Code, detail.Severity) &&
            Equals(allergy.Reaction, detail.Reaction) &&
            Equals(allergy.SensitivityType.Code, detail.SensitivityType.Code) &&
            Equals(allergy.OnsetTime, detail.OnsetTime) &&
            Equals(allergy.ReportedTime, detail.ReportedTime) &&
            Equals(allergy.Reporter, detail.ReporterName) &&
            Equals(allergy.ReporterRelationshipType.Code, detail.ReporterRelationshipType.Code));
 }
        public void UpdateAllergy(Allergy allergy, PatientAllergyDetail source, IPersistenceContext context)
        {
            var nameAssembler = new PersonNameAssembler();

            allergy.AllergenType        = EnumUtils.GetEnumValue <AllergyAllergenTypeEnum>(source.AllergenType, context);
            allergy.AllergenDescription = source.AllergenDescription;
            allergy.Severity            = EnumUtils.GetEnumValue <AllergySeverityEnum>(source.Severity, context);
            allergy.Reaction            = source.Reaction;
            allergy.SensitivityType     = EnumUtils.GetEnumValue <AllergySensitivityTypeEnum>(source.SensitivityType, context);
            allergy.OnsetTime           = source.OnsetTime;
            allergy.ReportedTime        = source.ReportedTime;
            nameAssembler.UpdatePersonName(source.ReporterName, allergy.Reporter);
            allergy.ReporterRelationshipType = EnumUtils.GetEnumValue <PersonRelationshipTypeEnum>(source.ReporterRelationshipType, context);
        }
        // JR: this method won't compile anymore since the reconciliation service was moved to the Extended plugins... do we really need it?
        ///// <summary>
        ///// Creates a new patient with multiple profiles.
        ///// </summary>
        ///// <returns></returns>
        //public static PatientProfileSummary CreatePatient(String[] InfoAuth)
        //{
        //    var result=new List<PatientProfileSummary>();

        //    if (InfoAuth.Length==0)
        //        result.Add(CreatePatient());
        //    else
        //    {
        //        var timespan = new TimeSpan(GetRandomInteger(0,100 * 365 * 24), 0, 0);
        //        InitReferenceDataCacheOnce();
        //        var birthDate = Platform.Time - timespan;

        //        var profile = new PatientProfileDetail
        //            {
        //                Healthcard = new HealthcardDetail(
        //                    GenerateRandomIntegerString(10),
        //                    ChooseRandom(_patientEditorFormData.HealthcardAssigningAuthorityChoices),
        //                    "", null),
        //                DateOfBirth = birthDate,
        //                Sex = ChooseRandom(_patientEditorFormData.SexChoices),
        //                PrimaryLanguage = ChooseRandom(_patientEditorFormData.PrimaryLanguageChoices),
        //                Religion = ChooseRandom(_patientEditorFormData.ReligionChoices),
        //                DeathIndicator = false,
        //                TimeOfDeath = null
        //            };

        //        profile.Name = new PersonNameDetail
        //            {
        //                FamilyName = GetRandomNameFromFile(RandomUtilsSettings.Default.FamilyNameDictionary),
        //                GivenName = profile.Sex.Code == "F"
        //                    ? GetRandomNameFromFile(RandomUtilsSettings.Default.FemaleNameDictionary) + " Anonymous"
        //                    : GetRandomNameFromFile(RandomUtilsSettings.Default.MaleNameDictionary) + " Anonymous"
        //            };

        //        AddPatientResponse addResponse = null;

        //        for(var i=0; i<InfoAuth.Length; i++)
        //        {
        //            profile.Mrn = new CompositeIdentifierDetail(
        //                GenerateRandomIntegerString(10),
        //                CollectionUtils.SelectFirst(_patientEditorFormData.MrnAssigningAuthorityChoices, MAAC => MAAC.Code == InfoAuth[i]));

        //            Platform.GetService(
        //                delegate(IPatientAdminService service)
        //                {
        //                    addResponse = service.AddPatient(new AddPatientRequest(profile));
        //                });

        //            result.Add(addResponse.PatientProfile);
        //        }

        //        if (InfoAuth.Length > 1)
        //        {
        //            //reconcile patients
        //            var checkedPatients = new List<EntityRef>();
        //            foreach (var pps in result)
        //            {
        //                checkedPatients.Add(pps.PatientRef);
        //            }

        //            // reconcile
        //            Platform.GetService<IPatientReconciliationService>(
        //                service => service.ReconcilePatients(new ReconcilePatientsRequest(checkedPatients)));
        //        }
        //    }

        //    return result[0];
        //}

        /// <summary>
        /// Creates a new patient with a single profile.
        /// </summary>
        /// <returns></returns>
        public static PatientProfileSummary CreatePatient()
        {
            var timespan = new TimeSpan(GetRandomInteger(0, 100 * 365 * 24), 0, 0);

            InitReferenceDataCacheOnce();
            var birthDate = Platform.Time - timespan;

            var profile = new PatientProfileDetail
            {
                Mrn = new CompositeIdentifierDetail(
                    GenerateRandomIntegerString(10),
                    ChooseRandom(_patientEditorFormData.MrnAssigningAuthorityChoices)),
                Healthcard = new HealthcardDetail(
                    GenerateRandomIntegerString(10),
                    ChooseRandom(_patientEditorFormData.HealthcardAssigningAuthorityChoices),
                    "", null),
                DateOfBirth     = birthDate,
                Sex             = ChooseRandom(_patientEditorFormData.SexChoices),
                PrimaryLanguage = ChooseRandom(_patientEditorFormData.PrimaryLanguageChoices),
                Religion        = ChooseRandom(_patientEditorFormData.ReligionChoices),
                DeathIndicator  = false,
                TimeOfDeath     = null
            };

            // Randomly create 0-2 allergies
            for (var i = 0; i < GetRandomInteger(0, 2); i++)
            {
                var allergy = new PatientAllergyDetail(
                    ChooseRandom(_patientEditorFormData.AllergenTypeChoices),
                    string.Format("description {0}", GenerateRandomString(3)),
                    ChooseRandom(_patientEditorFormData.AllergySeverityChoices),
                    string.Format("reaction {0}", GenerateRandomString(3)),
                    ChooseRandom(_patientEditorFormData.AllergySensitivityTypeChoices),
                    Platform.Time,
                    Platform.Time,
                    new PersonNameDetail
                {
                    FamilyName = GetRandomNameFromFile(RandomUtilsSettings.Default.FamilyNameDictionary),
                    GivenName  = GetRandomNameFromFile(RandomUtilsSettings.Default.MaleNameDictionary) + " Anonymous"
                },
                    ChooseRandom(_patientEditorFormData.PersonRelationshipTypeChoices));

                profile.Allergies.Add(allergy);
            }

            profile.Name = new PersonNameDetail
            {
                FamilyName = GetRandomNameFromFile(RandomUtilsSettings.Default.FamilyNameDictionary),
                GivenName  = profile.Sex.Code == "F"
                                        ? GetRandomNameFromFile(RandomUtilsSettings.Default.FemaleNameDictionary) + " Anonymous"
                                        : GetRandomNameFromFile(RandomUtilsSettings.Default.MaleNameDictionary) + " Anonymous"
            };

            AddPatientResponse addResponse = null;

            Platform.GetService(
                delegate(IPatientAdminService service)
            {
                addResponse = service.AddPatient(new AddPatientRequest(profile));
            });

            return(addResponse.PatientProfile);
        }