Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FM2-636: Enhancements in Existing FHIR Task Resource #539

Merged
merged 5 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/src/main/java/org/openmrs/module/fhir2/FhirConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,16 @@ private FhirConstants() {

public static final String STATUS_SEARCH_HANDLER = "status.search.handler";

public static final String TASK_CODE_SEARCH_HANDLER = "task.code.search.handler";

public static final String FULFILLER_STATUS_SEARCH_HANDLER = "fullfillerStatus.search.handler";

public static final String BASED_ON_REFERENCE_SEARCH_HANDLER = "based.on.reference.search.handler";

public static final String OWNER_REFERENCE_SEARCH_HANDLER = "owner.reference.search.handler";

public static final String FOR_REFERENCE_SEARCH_HANDLER = "for.reference.search.handler";

public static final String RESULT_SEARCH_HANDLER = "result.search.handler";

public static final String COMMON_SEARCH_HANDLER = "common.search.handler";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ protected void setupSearchParams(Criteria criteria, SearchParameterMap theParams
entry.getValue().forEach(
param -> handleReference(criteria, (ReferenceAndListParam) param.getParam(), "ownerReference", "o"));
break;
case FhirConstants.FOR_REFERENCE_SEARCH_HANDLER:
entry.getValue().forEach(
param -> handleReference(criteria, (ReferenceAndListParam) param.getParam(), "forReference", "f"));
break;
case FhirConstants.TASK_CODE_SEARCH_HANDLER:
entry.getValue().forEach(code -> handleTaskCodeConcept(criteria, (TokenAndListParam) code.getParam()));
break;
case FhirConstants.STATUS_SEARCH_HANDLER:
entry.getValue()
.forEach(param -> handleStatus((TokenAndListParam) param.getParam()).ifPresent(criteria::add));
Expand Down Expand Up @@ -108,4 +115,14 @@ private void handleReference(Criteria criteria, ReferenceAndListParam reference,
return Optional.empty();
}).ifPresent(criteria::add);
}

private void handleTaskCodeConcept(Criteria criteria, TokenAndListParam code) {
if (code != null) {
if (lacksAlias(criteria, "tc")) {
criteria.createAlias("taskCode", "tc");
}

handleCodeableConcept(criteria, code, "tc", "tcm", "tcrt").ifPresent(criteria::add);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,23 @@ public class TaskSearchParams extends BaseResourceSearchParams {

private ReferenceAndListParam ownerReference;

private ReferenceAndListParam forReference;

private TokenAndListParam taskCode;

private TokenAndListParam status;

@Builder
public TaskSearchParams(ReferenceAndListParam basedOnReference, ReferenceAndListParam ownerReference,
TokenAndListParam status, TokenAndListParam id, DateRangeParam lastUpdated, SortSpec sort,
HashSet<Include> includes) {
ReferenceAndListParam forReference, TokenAndListParam taskCode, TokenAndListParam status, TokenAndListParam id,
DateRangeParam lastUpdated, SortSpec sort, HashSet<Include> includes) {

super(id, lastUpdated, sort, includes, null);

this.basedOnReference = basedOnReference;
this.ownerReference = ownerReference;
this.forReference = forReference;
this.taskCode = taskCode;
this.status = status;

}
Expand All @@ -63,6 +69,8 @@ public TaskSearchParams(ReferenceAndListParam basedOnReference, ReferenceAndList
public SearchParameterMap toSearchParameterMap() {
return baseSearchParameterMap().addParameter(FhirConstants.BASED_ON_REFERENCE_SEARCH_HANDLER, getBasedOnReference())
.addParameter(FhirConstants.OWNER_REFERENCE_SEARCH_HANDLER, getOwnerReference())
.addParameter(FhirConstants.FOR_REFERENCE_SEARCH_HANDLER, getForReference())
.addParameter(FhirConstants.TASK_CODE_SEARCH_HANDLER, getTaskCode())
.addParameter(FhirConstants.STATUS_SEARCH_HANDLER, getStatus());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private Reference determineServiceRequestPerformer(String orderUuid) {
IBundleProvider results = taskService.searchForTasks(new TaskSearchParams(
new ReferenceAndListParam()
.addAnd(new ReferenceOrListParam().add(new ReferenceParam("ServiceRequest", null, orderUuid))),
null, null, null, null, null, null));
null, null, null, null, null, null, null, null));

Collection<Task> serviceRequestTasks = results.getResources(START_INDEX, END_INDEX).stream().map(p -> (Task) p)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@

import lombok.AccessLevel;
import lombok.Setter;
import org.hl7.fhir.r4.model.Annotation;
import org.hl7.fhir.r4.model.Identifier;
import org.hl7.fhir.r4.model.Period;
import org.hl7.fhir.r4.model.Task;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.translators.ConceptTranslator;
import org.openmrs.module.fhir2.api.translators.ReferenceTranslator;
import org.openmrs.module.fhir2.api.translators.TaskInputTranslator;
import org.openmrs.module.fhir2.api.translators.TaskOutputTranslator;
Expand All @@ -45,6 +48,9 @@ public class TaskTranslatorImpl implements TaskTranslator {
@Autowired
private TaskOutputTranslator taskOutputTranslator;

@Autowired
private ConceptTranslator conceptTranslator;

@Override
public Task toFhirResource(@Nonnull FhirTask openmrsTask) {
notNull(openmrsTask, "The openmrsTask object should not be null");
Expand Down Expand Up @@ -116,6 +122,30 @@ private void setFhirTaskFields(FhirTask openmrsTask, Task fhirTask) {
.filter(Objects::nonNull).collect(Collectors.toList()));
}

if (openmrsTask.getTaskCode() != null) {
fhirTask.setCode(conceptTranslator.toFhirResource(openmrsTask.getTaskCode()));
}

if (openmrsTask.getPartOfReferences() != null && !openmrsTask.getPartOfReferences().isEmpty()) {
fhirTask.setPartOf(openmrsTask.getPartOfReferences().stream().map(referenceTranslator::toFhirResource)
.collect(Collectors.toList()));
}

if (openmrsTask.getExecutionStartTime() != null || openmrsTask.getExecutionEndTime() != null) {
Period period = new Period();
if (openmrsTask.getExecutionStartTime() != null) {
period.setStart(openmrsTask.getExecutionStartTime());
}
if (openmrsTask.getExecutionEndTime() != null) {
period.setEnd(openmrsTask.getExecutionEndTime());
}
fhirTask.setExecutionPeriod(period);
}

if (openmrsTask.getComment() != null) {
fhirTask.addNote(new Annotation().setText(openmrsTask.getComment()));
}

fhirTask.setAuthoredOn(openmrsTask.getDateCreated());

if (openmrsTask.getDateChanged() != null) {
Expand Down Expand Up @@ -183,6 +213,28 @@ private void setOpenmrsTaskFields(FhirTask openmrsTask, Task fhirTask) {
.filter(Objects::nonNull).collect(Collectors.toSet()));
}

if (fhirTask.hasCode()) {
openmrsTask.setTaskCode(conceptTranslator.toOpenmrsType(fhirTask.getCode()));
}

if (fhirTask.hasExecutionPeriod()) {
if (fhirTask.getExecutionPeriod().hasStart()) {
openmrsTask.setExecutionStartTime(fhirTask.getExecutionPeriod().getStart());
}
if (fhirTask.getExecutionPeriod().hasEnd()) {
openmrsTask.setExecutionEndTime(fhirTask.getExecutionPeriod().getEnd());
}
}

if (fhirTask.hasPartOf()) {
openmrsTask.setPartOfReferences(
fhirTask.getPartOf().stream().map(referenceTranslator::toOpenmrsType).collect(Collectors.toSet()));
}

if (fhirTask.hasNote()) {
openmrsTask.setComment(fhirTask.getNoteFirstRep().getText());
}

openmrsTask.setName(FhirConstants.TASK + "/" + fhirTask.getId());

}
Expand Down
36 changes: 36 additions & 0 deletions api/src/main/java/org/openmrs/module/fhir2/model/FhirTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import java.util.Date;
import java.util.Set;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.openmrs.BaseOpenmrsMetadata;
import org.openmrs.Concept;

@Data
@NoArgsConstructor
Expand Down Expand Up @@ -129,4 +132,37 @@ public enum TaskIntent {
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "location_reference_id", referencedColumnName = "reference_id")
private FhirReference locationReference;

/**
* Type of Task
*/
@ManyToOne
@JoinColumn(name = "task_code", referencedColumnName = "concept_id")
protected Concept taskCode;

/**
* Task that this particular task is part of.
*/
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "fhir_task_part_of_reference", joinColumns = @JoinColumn(name = "task_id"), inverseJoinColumns = @JoinColumn(name = "reference_id"))
private Set<FhirReference> partOfReferences;

/**
* Actual start time of the execution
*/
@Column(name = "execution_start_time")
private Date executionStartTime;

/**
* Actual end time of the execution
*/
@Column(name = "execution_end_time")
private Date executionEndTime;

/**
* Comment made about the task
*/
@Column(name = "comment")
private String comment;

}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public OperationOutcome deleteTask(@IdParam IdType id) {
public IBundleProvider searchTasks(
@OptionalParam(name = Task.SP_BASED_ON, chainWhitelist = { "" }) ReferenceAndListParam basedOnReference,
@OptionalParam(name = Task.SP_OWNER, chainWhitelist = { "" }) ReferenceAndListParam ownerReference,
@OptionalParam(name = Task.SP_SUBJECT, chainWhitelist = { "" }) ReferenceAndListParam forReference,
@OptionalParam(name = Task.SP_CODE) TokenAndListParam taskCode,
@OptionalParam(name = Task.SP_STATUS) TokenAndListParam status,
@OptionalParam(name = Task.SP_RES_ID) TokenAndListParam id,
@OptionalParam(name = "_lastUpdated") DateRangeParam lastUpdated, @Sort SortSpec sort,
Expand All @@ -105,7 +107,7 @@ public IBundleProvider searchTasks(
if (CollectionUtils.isEmpty(includes)) {
includes = null;
}
return new SearchQueryBundleProviderR3Wrapper(fhirTaskService.searchForTasks(
new TaskSearchParams(basedOnReference, ownerReference, status, id, lastUpdated, sort, includes)));
return new SearchQueryBundleProviderR3Wrapper(fhirTaskService.searchForTasks(new TaskSearchParams(basedOnReference,
ownerReference, forReference, taskCode, status, id, lastUpdated, sort, includes)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public OperationOutcome deleteTask(@IdParam IdType id) {
public IBundleProvider searchTasks(
@OptionalParam(name = Task.SP_BASED_ON, chainWhitelist = { "" }) ReferenceAndListParam basedOnReference,
@OptionalParam(name = Task.SP_OWNER, chainWhitelist = { "" }) ReferenceAndListParam ownerReference,
@OptionalParam(name = Task.SP_SUBJECT, chainWhitelist = { "" }) ReferenceAndListParam forReference,
@OptionalParam(name = Task.SP_CODE) TokenAndListParam taskCode,
@OptionalParam(name = Task.SP_STATUS) TokenAndListParam status,
@OptionalParam(name = Task.SP_RES_ID) TokenAndListParam id,
@OptionalParam(name = "_lastUpdated") DateRangeParam lastUpdated, @Sort SortSpec sort,
Expand All @@ -111,7 +113,7 @@ public IBundleProvider searchTasks(
if (CollectionUtils.isEmpty(includes)) {
includes = null;
}
return service.searchForTasks(
new TaskSearchParams(basedOnReference, ownerReference, status, id, lastUpdated, sort, includes));
return service.searchForTasks(new TaskSearchParams(basedOnReference, ownerReference, forReference, taskCode, status,
id, lastUpdated, sort, includes));
}
}
43 changes: 43 additions & 0 deletions api/src/main/resources/liquibase.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1067,4 +1067,47 @@
<column name="attribute_type_id" />
</createIndex>
</changeSet>

<changeSet id="alter_fhir_task_20230903" author="r.kalai">
<preConditions onFail="MARK_RAN" onError="WARN">
<tableExists tableName="fhir_task"/>
</preConditions>
<addColumn tableName="fhir_task">
<column name="execution_start_time" type="datetime"/>
<column name="execution_end_time" type="datetime"/>
<column name="task_code" type="int"/>
<column name="comment" type="varchar(255)"/>
</addColumn>
<addForeignKeyConstraint baseTableName="fhir_task" baseColumnNames="task_code"
constraintName="task_code_fk"
referencedTableName="concept"
referencedColumnNames="concept_id"/>
</changeSet>

<changeSet id="add_part_of_reference_join_table_20200311" author="r.kalai">
<preConditions onFail="MARK_RAN" onError="WARN">
<not>
<tableExists tableName="fhir_task_part_of_reference"/>
</not>
<tableExists tableName="fhir_task"/>
<tableExists tableName="fhir_reference"/>
</preConditions>
<createTable tableName="fhir_task_part_of_reference">
<column name="task_id" type="int">
<constraints nullable="false"/>
</column>
<column name="reference_id" type="int">
<constraints nullable="false"/>
</column>
</createTable>
<addForeignKeyConstraint baseTableName="fhir_task_part_of_reference" baseColumnNames="task_id"
constraintName="task_part_of_fk"
referencedTableName="fhir_task"
referencedColumnNames="task_id"/>
<addForeignKeyConstraint baseTableName="fhir_task_part_of_reference" baseColumnNames="reference_id"
constraintName="reference_part_of_fk"
referencedTableName="fhir_reference"
referencedColumnNames="reference_id"/>
</changeSet>

</databaseChangeLog>
Loading
Loading