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

REPORT-839 : Update Drug Order Stop Filter #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/

package org.openmrs.module.reporting.cohort.definition;

import org.openmrs.Concept;
import org.openmrs.CareSetting;
import org.openmrs.Drug;

import org.openmrs.module.reporting.common.Match;
import org.openmrs.module.reporting.common.Localized;
import org.openmrs.module.reporting.definition.configuration.ConfigurationProperty;
import org.openmrs.module.reporting.definition.configuration.ConfigurationPropertyCachingStrategy;
import org.openmrs.module.reporting.evaluation.caching.Caching;

import java.util.Date;
import java.util.List;

@Caching(strategy = ConfigurationPropertyCachingStrategy.class)
@Localized("reporting.StartStopDrugOrderCohortDefinition")
public class StartStopDrugOrderCohortDefinition extends BaseCohortDefinition {

public static final long serialVersionUID = 1L;

@ConfigurationProperty(group = "state")
private Match state;

@ConfigurationProperty(value = "drugConcepts")
private List<Concept> drugConcepts;

@ConfigurationProperty(value = "drugSets")
private List<Concept> drugSets;

@ConfigurationProperty(value = "onOrBefore")
private Date onOrBefore;

@ConfigurationProperty(value = "onOrAfter")
private Date onOrAfter;

@ConfigurationProperty(value = "careSetting")
private CareSetting careSetting;

@ConfigurationProperty(value = "drugs")
private List<Drug> drugs;

public StartStopDrugOrderCohortDefinition() {
}

public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Patients ");

if (this.state != null) {
builder.append(" who " + this.state.toString() + " drug(s) ");
}

if (this.getDrugConcepts() != null && this.getDrugConcepts().size() > 0) {
builder.append("taking generic drugs, or drugs with ingredients ");
for (Concept concept : this.getDrugConcepts()) {
builder.append(concept.getDisplayString() + " ");
}
}
if (this.getDrugSets() != null && this.getDrugSets().size() > 0) {
for (Concept concept : this.getDrugSets()) {
builder.append(concept.getDisplayString() + " ");
}
}

if (this.getDrugs() != null && this.getDrugs().size() > 0) {
for (Drug drug : this.getDrugs()) {
builder.append(drug.getDisplayName() + " ");
}

}

if (this.getOnOrAfter() != null) {
builder.append(" on or after " + this.getOnOrAfter() + " ");
}

if (this.getOnOrBefore() != null) {
builder.append(" on or before " + this.getOnOrBefore() + " ");
}

if (this.careSetting != null) {
builder.append("with care setting of " + this.careSetting + " ");
}
return builder.toString();
}

public Match getState() {
return this.state;
}

public void setState(Match state) {
this.state = state;
}

public List<Concept> getDrugConcepts() {
return drugConcepts;
}

public void setDrugConcepts(List<Concept> drugConcepts) {
this.drugConcepts = drugConcepts;
}

public List<Concept> getDrugSets() {
return drugSets;
}

public void setDrugSets(List<Concept> drugSets) {
this.drugSets = drugSets;
}

public List<Drug> getDrugs() {
return drugs;
}

public void setDrugs(List<Drug> drugs) {
this.drugs = drugs;
}

public Date getOnOrBefore() {
return onOrBefore;
}

public void setOnOrBefore(Date onOrBefore) {
this.onOrBefore = onOrBefore;
}

public Date getOnOrAfter() {
return onOrAfter;
}

public void setOnOrAfter(Date onOrAfter) {
this.onOrAfter = onOrAfter;
}

public CareSetting getCareSetting() {
return careSetting;
}

public void setCareSetting(CareSetting careSetting) {
this.careSetting = careSetting;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/

package org.openmrs.module.reporting.cohort.definition.evaluator;

import org.apache.commons.lang3.time.DateUtils;
import org.openmrs.Cohort;
import org.openmrs.DrugOrder;
import org.openmrs.Order;
import org.openmrs.module.reporting.cohort.definition.CohortDefinition;
import org.openmrs.module.reporting.evaluation.EvaluationContext;
import org.openmrs.module.reporting.evaluation.querybuilder.HqlQueryBuilder;
import org.openmrs.module.reporting.evaluation.service.EvaluationService;
import org.openmrs.module.reporting.cohort.EvaluatedCohort;
import org.openmrs.module.reporting.cohort.definition.StartStopDrugOrderCohortDefinition;
import org.openmrs.module.reporting.common.ObjectUtil;
import org.openmrs.module.reporting.common.Match;
import org.openmrs.annotation.Handler;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Date;

@Handler(supports = { StartStopDrugOrderCohortDefinition.class })
public class StartStopDrugOrderCohortDefinitionEvaluator implements CohortDefinitionEvaluator {

@Autowired
EvaluationService evaluationService;

public StartStopDrugOrderCohortDefinitionEvaluator() {
}

public EvaluatedCohort evaluate(CohortDefinition cohortDefinition, EvaluationContext context) {

StartStopDrugOrderCohortDefinition startStopDrugOrderDef = (StartStopDrugOrderCohortDefinition) cohortDefinition;
context = ObjectUtil.nvl(context, new EvaluationContext());

HqlQueryBuilder query = new HqlQueryBuilder();
query.select("drugOrder.patient.patientId");
query.from(DrugOrder.class, "drugOrder");

query.wherePatientIn("drugOrder.patient.patientId", context);

if (startStopDrugOrderDef.getState() == null) {
startStopDrugOrderDef.setState(Match.STARTED);
}

if (startStopDrugOrderDef.getState() == Match.STARTED) {

query.whereEqual("drugOrder.action", Order.Action.NEW)
.whereNull("drugOrder.previousOrder");
}
else if (startStopDrugOrderDef.getState() == Match.STOPPED) {

query.whereEqual("drugOrder.action", Order.Action.DISCONTINUE)
.whereGreaterOrEqualTo("drugOrder.dateActivated", startStopDrugOrderDef.getOnOrAfter())
.whereLessOrEqualTo("drugOrder.dateActivated", startStopDrugOrderDef.getOnOrBefore())
.or()
.whereGreaterOrEqualTo("COALESCE(drugOrder.dateStopped, drugOrder.autoExpireDate)", startStopDrugOrderDef.getOnOrAfter())
.whereLessOrEqualTo("COALESCE(drugOrder.dateStopped, drugOrder.autoExpireDate)", startStopDrugOrderDef.getOnOrBefore())
.where("drugOrder.orderId NOT IN(SELECT drugOrder.previousOrder FROM drugOrder WHERE drugOrder.previousOrder IS NOT NULL)");
}
else if (startStopDrugOrderDef.getState() == Match.CHANGED) {

query.whereNotNull("drugOrder.dateActivated")
.whereGreaterOrEqualTo("COALESCE(drugOrder.dateStopped, drugOrder.autoExpireDate)", startStopDrugOrderDef.getOnOrAfter())
.whereLessOrEqualTo("COALESCE(drugOrder.dateStopped, drugOrder.autoExpireDate)", startStopDrugOrderDef.getOnOrBefore());
}

if (startStopDrugOrderDef.getDrugSets() != null) {
query.whereIn("drugOrder.concept", startStopDrugOrderDef.getDrugSets());
}

if (startStopDrugOrderDef.getDrugConcepts() != null) {
query.whereIn("drugOrder.concept", startStopDrugOrderDef.getDrugConcepts());
}

if (startStopDrugOrderDef.getDrugs() != null) {
query.whereIn("drugOrder.drug", startStopDrugOrderDef.getDrugs());
}

List<Integer> patientIds = evaluationService.evaluateToList(query, Integer.class, context);
Cohort cohort = new Cohort(patientIds);

return new EvaluatedCohort(cohort, startStopDrugOrderDef, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.openmrs.module.reporting.common.Match;
import org.openmrs.module.reporting.cohort.definition.CohortDefinition;
import org.openmrs.module.reporting.cohort.definition.DrugOrderCohortDefinition;
import org.openmrs.module.reporting.cohort.definition.StartStopDrugOrderCohortDefinition;
import org.openmrs.module.reporting.cohort.definition.library.BuiltInCohortDefinitionLibrary;
import org.openmrs.module.reporting.definition.library.DocumentedDefinition;
import org.openmrs.module.reporting.evaluation.parameter.Parameter;
Expand All @@ -29,7 +30,18 @@
@Component
public class BuiltInCohortDefinitionLibrary1_10 extends BuiltInCohortDefinitionLibrary {


@DocumentedDefinition("startStopDrugOrderSearch")
public CohortDefinition getStartStopDrugOrderSearch() {
CohortDefinition startStopdrugOrderCohortDef = new StartStopDrugOrderCohortDefinition();
startStopdrugOrderCohortDef.addParameter(new Parameter("state", "reporting.parameter.state", Match.class));
startStopdrugOrderCohortDef.addParameter(new Parameter("drugConcepts", "reporting.parameter.drugConcepts", Concept.class, List.class, null));
startStopdrugOrderCohortDef.addParameter(new Parameter("drugSets", "reporting.parameter.drugSets", Concept.class, List.class, null));
startStopdrugOrderCohortDef.addParameter(new Parameter("onOrBefore", "reporting.parameter.onOrBefore", Date.class));
startStopdrugOrderCohortDef.addParameter(new Parameter("onOrAfter", "reporting.parameter.onOrAfter", Date.class));
startStopdrugOrderCohortDef.addParameter(new Parameter("careSetting", "reporting.parameter.careSetting", CareSetting.class));
startStopdrugOrderCohortDef.addParameter(new Parameter("drugs", "reporting.parameter.drugs", Drug.class, List.class, null));
return startStopdrugOrderCohortDef;
}

@DocumentedDefinition("drugOrderSearch")
public CohortDefinition getDrugOrderSearch() {
Expand Down
Loading