/// <summary>
        /// Handles the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="response">The response.</param>
        protected override void Handle(SaveSectionDtoRequest request, SaveDtoResponse <IKeyedDataTransferObject> response)
        {
            var dto = request.DataTransferObject;

            var assessment = _assessmentRepository.GetByKey(dto.Key);

            var dtoType            = dto.GetType();
            var entityPropertyInfo = assessment.GetType().GetProperty(request.Section);
            var entity             = entityPropertyInfo.GetValue(assessment) as RevisionBase;

            if (entity == null && request.Section == "ReviewSection")
            {
                entity = Activator.CreateInstance <ReviewSection>();
                entityPropertyInfo.SetValue(assessment, entity);
            }
            if (request.SubSection != null)
            {
                entityPropertyInfo = entityPropertyInfo.PropertyType.GetProperty(request.SubSection);
                entity             = entityPropertyInfo.GetValue(entity) as RevisionBase;
            }
            var entityType = entityPropertyInfo.PropertyType;

            foreach (var propertyInfo in dtoType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public).Where(pi =>
            {
                var readonlyAttribute = pi.GetCustomAttributes(typeof(ReadOnlyAttribute), false).FirstOrDefault() as ReadOnlyAttribute;
                return(readonlyAttribute == null || !readonlyAttribute.IsReadOnly);
            }))
            {
                var valueToSet = propertyInfo.GetValue(dto);
                var lookupDto  = valueToSet as LookupDto;
                if (lookupDto != null)
                {
                    var entityProperty = entityType.GetProperty(propertyInfo.Name);
                    valueToSet = Lookup.Find(entityProperty.PropertyType.Name, lookupDto.Code);
                }
                var enumerableLookups = valueToSet as IEnumerable <LookupDto>;
                if (enumerableLookups != null)
                {
                    var entityProperty = entityType.GetProperty(propertyInfo.Name);
                    var lookupType     = entityProperty.PropertyType.GetGenericArguments()[0];
                    var newValues      = Activator.CreateInstance(typeof(List <>).MakeGenericType(entityProperty.PropertyType.GetGenericArguments()[0])) as IList;
                    foreach (var lookup in (valueToSet as IEnumerable <LookupDto>))
                    {
                        newValues.Add(Lookup.Find(lookupType.Name, lookup.Code));
                    }
                    valueToSet = newValues;
                }
                entity.ReviseProperty(assessment.Key, propertyInfo.Name, valueToSet);
            }

            response.DataTransferObject = Mapper.Map(entity, entityType, dtoType) as IKeyedDataTransferObject;

            response.DataTransferObject.Key = assessment.Key;
        }
        protected override void Handle(SaveDtoRequest <PatientDto> request, SaveDtoResponse <PatientDto> response)
        {
            var patient = _patientRepository.GetByKey(request.DataTransferObject.Key);

            if (patient != null)
            {
                //patient.ReviseName(request.DataTransferObject.Name);
                //patient.ReviseDateOfBirth(request.DataTransferObject.DateOfBirth);
                //patient.ReviseGender(Lookup.Find<Gender>(request.DataTransferObject.Gender.Code));
                patient.ReviseEthnicity(Lookup.Find <Ethnicity>(request.DataTransferObject.Ethnicity.Code));
                patient.ReviseReligion(Lookup.Find <Religion>(request.DataTransferObject.Religion.Code));

                response.DataTransferObject = Mapper.Map <Patient, PatientDto>(patient);
            }
        }
        /// <summary>
        ///     Handles the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="response">The response.</param>
        protected override void Handle(SaveDtoRequest <PatientDto> request, SaveDtoResponse <PatientDto> response)
        {
            var           patient       = _patientRepository.GetByKey(request.DataTransferObject.Key);
            DataErrorInfo dataErrorInfo = null;

            if (patient != null)
            {
                patient.ReviseName(request.DataTransferObject.Name);
                patient.ReviseDateOfBirth(request.DataTransferObject.DateOfBirth);
                patient.ReviseGender(_lookupProvider.Find <Gender> (request.DataTransferObject.Gender.Code));
                if (request.DataTransferObject.Ethnicity != null && !string.IsNullOrEmpty(request.DataTransferObject.Ethnicity.Code))
                {
                    patient.ReviseEthnicity(_lookupProvider.Find <Ethnicity> (request.DataTransferObject.Ethnicity.Code));
                }
                if (request.DataTransferObject.Religion != null && !string.IsNullOrEmpty(request.DataTransferObject.Religion.Code))
                {
                    patient.ReviseReligion(_lookupProvider.Find <Religion> (request.DataTransferObject.Religion.Code));
                }
                Email newEmail = null;
                try
                {
                    if (!string.IsNullOrWhiteSpace(request.DataTransferObject.Email))
                    {
                        newEmail = new Email(request.DataTransferObject.Email);
                    }
                }
                catch (ArgumentException ae)
                {
                    if (!ae.Message.Contains("email address", StringComparison.OrdinalIgnoreCase))
                    {
                        throw;
                    }
                    dataErrorInfo = new DataErrorInfo(ae.Message, ErrorLevel.Error, PropertyUtil.ExtractPropertyName <PatientDto, string> (s => s.Email));
                }
                patient.ReviseEmail(string.IsNullOrWhiteSpace(request.DataTransferObject.Email) ? null : newEmail);

                response.DataTransferObject = Mapper.Map <Patient, PatientDto> (patient);
                if (dataErrorInfo != null)
                {
                    response.DataTransferObject.AddDataErrorInfo(dataErrorInfo);
                }
            }
        }
        /// <summary>
        ///     Handles the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="response">The response.</param>
        protected override void Handle(CreatePatientRequest request, SaveDtoResponse <PatientDto> response)
        {
            var patientFactory = new PatientFactory();
            var patient        = patientFactory.Create(request.PatientDto.OrganizationKey, request.PatientDto.Name, request.PatientDto.DateOfBirth, _lookupProvider.Find <Gender> (request.PatientDto.Gender.Code));

            if (patient != null)
            {
                if (request.PatientDto.Religion != null && !string.IsNullOrEmpty(request.PatientDto.Religion.Code))
                {
                    patient.ReviseReligion(_lookupProvider.Find <Religion> (request.PatientDto.Religion.Code));
                }
                if (request.PatientDto.Ethnicity != null && !string.IsNullOrEmpty(request.PatientDto.Ethnicity.Code))
                {
                    patient.ReviseEthnicity(_lookupProvider.Find <Ethnicity> (request.PatientDto.Ethnicity.Code));
                }

                var patientDto = Mapper.Map <Patient, PatientDto> (patient);

                response.DataTransferObject = patientDto;
            }
        }
        /// <summary>
        ///     Handles the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="response">The response.</param>
        protected override void Handle(SaveDtoRequest <PatientDto> request, SaveDtoResponse <PatientDto> response)
        {
            var patient = _patientRepository.GetByKey(request.DataTransferObject.Key);

            if (patient != null)
            {
                patient.ReviseName(request.DataTransferObject.Name);
                patient.ReviseDateOfBirth(request.DataTransferObject.DateOfBirth);
                patient.ReviseGender(_lookupProvider.Find <Gender> (request.DataTransferObject.Gender.Code));
                if (request.DataTransferObject.Ethnicity != null && !string.IsNullOrEmpty(request.DataTransferObject.Ethnicity.Code))
                {
                    patient.ReviseEthnicity(_lookupProvider.Find <Ethnicity> (request.DataTransferObject.Ethnicity.Code));
                }
                if (request.DataTransferObject.Religion != null && !string.IsNullOrEmpty(request.DataTransferObject.Religion.Code))
                {
                    patient.ReviseReligion(_lookupProvider.Find <Religion> (request.DataTransferObject.Religion.Code));
                }
                patient.ReviseEmail(string.IsNullOrWhiteSpace(request.DataTransferObject.Email) ? null : new Email(request.DataTransferObject.Email));

                response.DataTransferObject = Mapper.Map <Patient, PatientDto> (patient);
            }
        }
        protected override void Handle(CreatePatientRequest request, SaveDtoResponse <PatientDto> response)
        {
            var organization   = _organizationRepository.GetByKey(UserContext.OrganizationKey);
            var patientFactory = _patientFactory;
            var patient        = patientFactory.Create(organization, request.PatientDto.Name,
                                                       request.PatientDto.DateOfBirth,
                                                       Lookup.Find <Gender> (request.PatientDto.Gender.Code));

            if (patient != null)
            {
                if (request.PatientDto.Religion != null)
                {
                    patient.ReviseReligion(Lookup.Find <Religion>(request.PatientDto.Religion.Code));
                }
                if (request.PatientDto.Ethnicity != null)
                {
                    patient.ReviseEthnicity(Lookup.Find <Ethnicity>(request.PatientDto.Ethnicity.Code));
                }

                var patientDto = Mapper.Map <Patient, PatientDto>(patient);

                response.DataTransferObject = patientDto;
            }
        }