Search in sources :

Example 1 with Goal

use of org.hl7.fhir.r4.model.Goal in project Gravity-SDOH-Exchange-RI by FHIR.

the class ProblemService method addGoalsToConditionBundle.

private Bundle addGoalsToConditionBundle(Bundle responseBundle) {
    Bundle goals = new GoalQueryFactory().query(ehrClient, SmartOnFhirContext.get().getPatient()).where(Goal.LIFECYCLE_STATUS.exactly().codes(Goal.GoalLifecycleStatus.ACTIVE.toCode(), Goal.GoalLifecycleStatus.COMPLETED.toCode())).returnBundle(Bundle.class).execute();
    if (goals.getLink(IBaseBundle.LINK_NEXT) != null) {
        throw new RuntimeException("Multi-page Goal results returned for the List Problems operation. Pagination not supported yet. Make sure " + "there is a small amount of goals in your FHIR store.");
    }
    Bundle merged = FhirUtil.mergeBundles(ehrClient.getFhirContext(), responseBundle, goals);
    return merged;
}
Also used : GoalQueryFactory(org.hl7.gravity.refimpl.sdohexchange.fhir.query.GoalQueryFactory) IBaseBundle(org.hl7.fhir.instance.model.api.IBaseBundle) Bundle(org.hl7.fhir.r4.model.Bundle)

Example 2 with Goal

use of org.hl7.fhir.r4.model.Goal in project Gravity-SDOH-Exchange-RI by FHIR.

the class TaskReferenceResourcesLoader method processServiceRequestReferences.

private void processServiceRequestReferences(IGenericClient ehrClient, TaskReferencesHolder taskReferencesHolder, String identifierSystem, Bundle bundle) {
    ServiceRequestReferenceResolver referenceResolver = new ServiceRequestReferenceResolver(taskReferencesHolder.getServiceRequest(), identifierSystem);
    // Load local references in one transaction
    referenceResolver.setLocalResources(resourceLoader.getResourcesBySystem(openCpClient, identifierSystem, referenceResolver.getLocalReferences()));
    // Load EHR references in one transaction
    referenceResolver.setExternalResources(resourceLoader.getResources(ehrClient, referenceResolver.getExternalReferences()));
    Map<String, Consumer<IIdType>> referenceConsumers = new HashMap<>();
    referenceConsumers.put(Patient.class.getSimpleName(), new PatientReferenceConsumer(taskReferencesHolder.getPatient().getIdElement().getIdPart()));
    for (Reference serviceRequestRef : referenceResolver.getConsentsRef()) {
        IIdType serviceRequestEl = serviceRequestRef.getReferenceElement();
        Consent consent = referenceResolver.getConsent(serviceRequestEl);
        // Update links only for new conditions
        if (referenceResolver.createConsent(serviceRequestEl)) {
            updateResourceRefs(consent, ehrClient.getServerBase(), referenceConsumers);
            // Create CP Condition resource
            bundle.addEntry(FhirUtil.createPostEntry(consent));
        }
        // Link CP ServiceRequest reference with Consent
        serviceRequestEl.setParts(null, serviceRequestEl.getResourceType(), consent.getIdElement().getIdPart(), null);
        serviceRequestRef.setReferenceElement(serviceRequestEl);
    }
    Map<String, Condition> processedConditions = new HashMap<>();
    for (Reference serviceRequestRef : referenceResolver.getConditionsRefs()) {
        IIdType serviceRequestEl = serviceRequestRef.getReferenceElement();
        Condition condition = referenceResolver.getCondition(serviceRequestEl);
        // Update links only for new conditions
        if (referenceResolver.createCondition(serviceRequestEl)) {
            updateResourceRefs(condition, ehrClient.getServerBase(), referenceConsumers);
            // Create CP Condition resource
            bundle.addEntry(FhirUtil.createPostEntry(condition));
        }
        // Link CP ServiceRequest reference with Condition
        serviceRequestEl.setParts(null, serviceRequestEl.getResourceType(), condition.getIdElement().getIdPart(), null);
        serviceRequestRef.setReferenceElement(serviceRequestEl);
        processedConditions.put(condition.getIdentifierFirstRep().getValue(), condition);
    }
    referenceConsumers.put(Condition.class.getSimpleName(), new ConditionReferenceConsumer(ehrClient.getServerBase(), processedConditions));
    for (Reference serviceRequestRef : referenceResolver.getGoalsRefs()) {
        IIdType serviceRequestEl = serviceRequestRef.getReferenceElement();
        Goal goal = referenceResolver.getGoal(serviceRequestEl);
        // Update links only for new goals
        if (referenceResolver.createGoal(serviceRequestEl)) {
            updateResourceRefs(goal, ehrClient.getServerBase(), referenceConsumers);
            // Create CP Goal resource
            bundle.addEntry(FhirUtil.createPostEntry(goal));
        }
        // Link CP ServiceRequest reference with Gaol
        serviceRequestEl.setParts(null, serviceRequestEl.getResourceType(), goal.getIdElement().getIdPart(), null);
        serviceRequestRef.setReferenceElement(serviceRequestEl);
    }
}
Also used : Condition(org.hl7.fhir.r4.model.Condition) HashMap(java.util.HashMap) Reference(org.hl7.fhir.r4.model.Reference) Patient(org.hl7.fhir.r4.model.Patient) Goal(org.hl7.fhir.r4.model.Goal) Consumer(java.util.function.Consumer) Consent(org.hl7.fhir.r4.model.Consent) IIdType(org.hl7.fhir.instance.model.api.IIdType)

Example 3 with Goal

use of org.hl7.fhir.r4.model.Goal in project Gravity-SDOH-Exchange-RI by FHIR.

the class GoalBundleToDtoConverter method convert.

@Override
public List<GoalDto> convert(Bundle bundle) {
    List<GoalDto> result = FhirUtil.getFromBundle(bundle, Goal.class).stream().map(goal -> {
        GoalDto goalDto = new GoalDto();
        goalDto.setId(goal.getIdElement().getIdPart());
        if (goal.hasDescription() && goal.getDescription().hasText()) {
            goalDto.setName(goal.getDescription().getText());
        } else {
            goalDto.getErrors().add("Goal description.text not found. Name cannot be set.");
        }
        if (goal.hasAchievementStatus()) {
            goalDto.setAchievementStatus(GoalAchievement.fromCode(goal.getAchievementStatus().getCodingFirstRep().getCode()));
        } else {
            goalDto.getErrors().add("No achievement status available.");
        }
        // TODO reused from HealthConcernBundleToDtoConverter class. Refactor!
        Coding categoryCoding = FhirUtil.findCoding(goal.getCategory(), SDOHMappings.getInstance().getSystem());
        // TODO remove this in future. Fow now two different categories might be used before discussed.
        if (categoryCoding == null) {
            categoryCoding = FhirUtil.findCoding(goal.getCategory(), "http://hl7.org/fhir/us/sdoh-clinicalcare/CodeSystem/SDOHCC-CodeSystemTemporaryCodes");
        }
        if (categoryCoding == null) {
            goalDto.getErrors().add("SDOH category is not found.");
        } else {
            goalDto.setCategory(new CodingDto(categoryCoding.getCode(), categoryCoding.getDisplay()));
        }
        Optional<Coding> snomedCodeCodingOptional = findCode(goal.getDescription(), System.SNOMED);
        if (snomedCodeCodingOptional.isPresent()) {
            Coding snomedCodeCoding = snomedCodeCodingOptional.get();
            goalDto.setSnomedCode(new CodingDto(snomedCodeCoding.getCode(), snomedCodeCoding.getDisplay()));
        } else {
            goalDto.getErrors().add("SNOMED-CT code is not found.");
        }
        if (goal.hasExpressedBy()) {
            Reference expressedBy = goal.getExpressedBy();
            goalDto.setAddedBy(new ReferenceDto(expressedBy.getReferenceElement().getIdPart(), expressedBy.getDisplay()));
        } else {
            goalDto.getErrors().add("Goal expressedBy property is missing. addedBy will be null.");
        }
        goalDto.setStartDate(FhirUtil.toLocalDate(goal.getStartDateType()));
        // TODO this is invalid. To clarify!
        if (goal.getLifecycleStatus() == Goal.GoalLifecycleStatus.COMPLETED) {
            if (goal.hasStatusDate()) {
                goalDto.setEndDate(FhirUtil.toLocalDate(goal.getStatusDateElement()));
            } else {
                goalDto.getErrors().add("Goal statusDate must be set when the lifecycleStatus is COMPLETED. endDate will be null.");
            }
        }
        goalDto.setComments(goal.getNote().stream().map(annotationToDtoConverter::convert).collect(Collectors.toList()));
        goalDto.setProblems(goal.getAddresses().stream().filter(ref -> Condition.class.getSimpleName().equals(ref.getReferenceElement().getResourceType())).map(ref -> (Condition) ref.getResource()).map(cond -> new ConditionDto(cond.getIdElement().getIdPart(), codeableConceptToStringConverter.convert(cond.getCode()))).collect(Collectors.toList()));
        return goalDto;
    }).collect(Collectors.toList());
    // TODO refactor. method too long.
    // TODO refactor. Almost the same fragmen exists in a ProblemInfoBundleExtractor
    Map<String, GoalDto> idToDtoMap = result.stream().collect(Collectors.toMap(g -> g.getId(), Function.identity()));
    for (Task task : FhirUtil.getFromBundle(bundle, Task.class)) {
        if (!task.hasFocus() || !(task.getFocus().getResource() instanceof ServiceRequest)) {
            continue;
        }
        ServiceRequest sr = (ServiceRequest) task.getFocus().getResource();
        sr.getSupportingInfo().stream().filter(ref -> Goal.class.getSimpleName().equals(ref.getReferenceElement().getResourceType()) && idToDtoMap.containsKey(ref.getReferenceElement().getIdPart())).forEach(ref -> idToDtoMap.get(ref.getReferenceElement().getIdPart()).getTasks().add(new TaskInfoDto(task.getIdElement().getIdPart(), task.getDescription(), task.getStatus())));
    }
    return result;
}
Also used : Converter(org.springframework.core.convert.converter.Converter) GoalDto(org.hl7.gravity.refimpl.sdohexchange.dto.response.GoalDto) Goal(org.hl7.fhir.r4.model.Goal) ReferenceDto(org.hl7.gravity.refimpl.sdohexchange.dto.response.ReferenceDto) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept) Condition(org.hl7.fhir.r4.model.Condition) Reference(org.hl7.fhir.r4.model.Reference) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Task(org.hl7.fhir.r4.model.Task) CodingDto(org.hl7.gravity.refimpl.sdohexchange.dto.response.CodingDto) List(java.util.List) GoalAchievement(org.hl7.fhir.r4.model.codesystems.GoalAchievement) TaskInfoDto(org.hl7.gravity.refimpl.sdohexchange.dto.response.TaskInfoDto) Coding(org.hl7.fhir.r4.model.Coding) Map(java.util.Map) Optional(java.util.Optional) Bundle(org.hl7.fhir.r4.model.Bundle) ConditionDto(org.hl7.gravity.refimpl.sdohexchange.dto.response.ConditionDto) System(org.hl7.gravity.refimpl.sdohexchange.sdohmappings.System) SDOHMappings(org.hl7.gravity.refimpl.sdohexchange.sdohmappings.SDOHMappings) ServiceRequest(org.hl7.fhir.r4.model.ServiceRequest) FhirUtil(org.hl7.gravity.refimpl.sdohexchange.util.FhirUtil) Condition(org.hl7.fhir.r4.model.Condition) GoalDto(org.hl7.gravity.refimpl.sdohexchange.dto.response.GoalDto) ReferenceDto(org.hl7.gravity.refimpl.sdohexchange.dto.response.ReferenceDto) Task(org.hl7.fhir.r4.model.Task) Optional(java.util.Optional) Reference(org.hl7.fhir.r4.model.Reference) TaskInfoDto(org.hl7.gravity.refimpl.sdohexchange.dto.response.TaskInfoDto) ServiceRequest(org.hl7.fhir.r4.model.ServiceRequest) CodingDto(org.hl7.gravity.refimpl.sdohexchange.dto.response.CodingDto) Goal(org.hl7.fhir.r4.model.Goal) Coding(org.hl7.fhir.r4.model.Coding) ConditionDto(org.hl7.gravity.refimpl.sdohexchange.dto.response.ConditionDto)

Example 4 with Goal

use of org.hl7.fhir.r4.model.Goal in project Gravity-SDOH-Exchange-RI by FHIR.

the class ServiceRequestReferenceResolver method getGoal.

public Goal getGoal(IIdType iIdType) {
    Goal goal = localGoals.get(iIdType.getIdPart());
    if (goal == null) {
        goal = externalGoals.get(iIdType.getIdPart()).copy();
        // Remove SDOH profile, Logica does not support this.
        // TODO Use SDOH Profiles.
        goal.setMeta(null);
        // Set identifier to link resource from EHR
        goal.addIdentifier().setSystem(identifierSystem).setValue(goal.getIdElement().getIdPart());
        goal.setId(IdType.newRandomUuid());
    }
    return goal;
}
Also used : Goal(org.hl7.fhir.r4.model.Goal)

Example 5 with Goal

use of org.hl7.fhir.r4.model.Goal in project Gravity-SDOH-Exchange-RI by FHIR.

the class GoalService method create.

public GoalDto create(NewGoalDto newGoalDto, UserDto user) {
    Assert.notNull(SmartOnFhirContext.get().getPatient(), "Patient id cannot be null.");
    GoalPrepareBundleFactory goalPrepareBundleFactory = new GoalPrepareBundleFactory(SmartOnFhirContext.get().getPatient(), user.getId(), newGoalDto.getProblemIds());
    Bundle goalRelatedResources = ehrClient.transaction().withBundle(goalPrepareBundleFactory.createPrepareBundle()).execute();
    GoalPrepareBundleExtractor.GoalPrepareInfoHolder goalPrepareInfoHolder = new GoalPrepareBundleExtractor().extract(goalRelatedResources);
    GoalBundleFactory bundleFactory = new GoalBundleFactory();
    bundleFactory.setName(newGoalDto.getName());
    String category = newGoalDto.getCategory();
    bundleFactory.setCategory(sdohMappings.findCategoryCoding(category));
    bundleFactory.setSnomedCode(sdohMappings.findCoding(category, Goal.class, System.SNOMED, newGoalDto.getSnomedCode()));
    bundleFactory.setAchievementStatus(newGoalDto.getAchievementStatus());
    bundleFactory.setPatient(goalPrepareInfoHolder.getPatient());
    bundleFactory.setPractitioner(goalPrepareInfoHolder.getPractitioner());
    bundleFactory.setUser(user);
    bundleFactory.setComment(newGoalDto.getComment());
    bundleFactory.setProblems(goalPrepareInfoHolder.getProblems(newGoalDto.getProblemIds()));
    bundleFactory.setStartDate(newGoalDto.getStart());
    Bundle goalCreateBundle = ehrClient.transaction().withBundle(bundleFactory.createPrepareBundle()).execute();
    IdType goalId = FhirUtil.getFromResponseBundle(goalCreateBundle, Goal.class);
    Bundle responseBundle = searchGoalQuery(Goal.GoalLifecycleStatus.ACTIVE).where(Condition.RES_ID.exactly().code(goalId.getIdPart())).returnBundle(Bundle.class).execute();
    Bundle merged = addConditionsToGoalBundle(responseBundle);
    return new GoalBundleToDtoConverter().convert(merged).stream().findFirst().orElseThrow(() -> new HealthConcernCreateException("goal is not found in the response bundle."));
}
Also used : GoalPrepareBundleFactory(org.hl7.gravity.refimpl.sdohexchange.fhir.factory.GoalPrepareBundleFactory) Goal(org.hl7.fhir.r4.model.Goal) GoalBundleFactory(org.hl7.gravity.refimpl.sdohexchange.fhir.factory.GoalBundleFactory) IBaseBundle(org.hl7.fhir.instance.model.api.IBaseBundle) Bundle(org.hl7.fhir.r4.model.Bundle) HealthConcernCreateException(org.hl7.gravity.refimpl.sdohexchange.exception.HealthConcernCreateException) GoalBundleToDtoConverter(org.hl7.gravity.refimpl.sdohexchange.dto.converter.GoalBundleToDtoConverter) GoalPrepareBundleExtractor(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.GoalPrepareBundleExtractor) IdType(org.hl7.fhir.r4.model.IdType)

Aggregations

Goal (org.hl7.fhir.r4.model.Goal)11 Bundle (org.hl7.fhir.r4.model.Bundle)8 Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)6 Condition (org.hl7.fhir.r4.model.Condition)6 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)6 Date (java.util.Date)5 IBaseBundle (org.hl7.fhir.instance.model.api.IBaseBundle)5 CodeableConcept (org.hl7.fhir.r4.model.CodeableConcept)5 JsonObject (com.google.gson.JsonObject)4 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)4 IdType (org.hl7.fhir.r4.model.IdType)4 Reference (org.hl7.fhir.r4.model.Reference)4 List (java.util.List)3 Coding (org.hl7.fhir.r4.model.Coding)3 ServiceRequest (org.hl7.fhir.r4.model.ServiceRequest)3 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)2 JsonElement (com.google.gson.JsonElement)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2