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

amrsreports: mapped obs for person evaluator. #61

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,91 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.module.amrsreports.reporting.data;

import org.openmrs.Concept;
import org.openmrs.Obs;
import org.openmrs.module.reporting.common.TimeQualifier;
import org.openmrs.module.reporting.data.BaseDataDefinition;
import org.openmrs.module.reporting.data.person.definition.PersonDataDefinition;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*/
public class MappedObsForPersonDataDefinition extends BaseDataDefinition implements PersonDataDefinition {

private TimeQualifier which;

private Map<Concept, Collection<Concept>> questionAnswerMap;

private Date onOrAfter;

private Date onOrBefore;

/**
* @see org.openmrs.module.reporting.data.DataDefinition#getDataType()
*/
public Class<?> getDataType() {
if (which == TimeQualifier.LAST || which == TimeQualifier.FIRST) {
return Obs.class;
}
return List.class;
}

public TimeQualifier getWhich() {
return which;
}

public void setWhich(final TimeQualifier which) {
this.which = which;
}

public void addQuestionAnswer(final Concept question, final Collection<Concept> answers) {
getQuestionAnswerMap().put(question, answers);
}

public Collection<Concept> getAnswers(final Concept question) {
return getQuestionAnswerMap().get(question);
}

public Map<Concept, Collection<Concept>> getQuestionAnswerMap() {
if (questionAnswerMap == null)
questionAnswerMap = new HashMap<Concept, Collection<Concept>>();
return questionAnswerMap;
}

public void setQuestionAnswerMap(final Map<Concept, Collection<Concept>> questionAnswerMap) {
this.questionAnswerMap = questionAnswerMap;
}

public Date getOnOrAfter() {
return onOrAfter;
}

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

public Date getOnOrBefore() {
return onOrBefore;
}

public void setOnOrBefore(final Date onOrBefore) {
this.onOrBefore = onOrBefore;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.module.amrsreports.reporting.data.evaluator;

import org.openmrs.Cohort;
import org.openmrs.Concept;
import org.openmrs.Obs;
import org.openmrs.annotation.Handler;
import org.openmrs.api.context.Context;
import org.openmrs.module.amrsreports.reporting.data.MappedObsForPersonDataDefinition;
import org.openmrs.module.reporting.common.DateUtil;
import org.openmrs.module.reporting.common.ListMap;
import org.openmrs.module.reporting.common.TimeQualifier;
import org.openmrs.module.reporting.data.person.EvaluatedPersonData;
import org.openmrs.module.reporting.data.person.definition.PersonDataDefinition;
import org.openmrs.module.reporting.data.person.evaluator.PersonDataEvaluator;
import org.openmrs.module.reporting.dataset.query.service.DataSetQueryService;
import org.openmrs.module.reporting.evaluation.EvaluationContext;
import org.openmrs.module.reporting.evaluation.EvaluationException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*/
@Handler(supports = MappedObsForPersonDataDefinition.class, order = 50)
public class MappedObsForPersonEvaluator implements PersonDataEvaluator {

/**
* @should return the obs that match the passed definition configuration
* @see PersonDataEvaluator#evaluate(org.openmrs.module.reporting.data.person.definition.PersonDataDefinition, org.openmrs.module.reporting.evaluation.EvaluationContext)
*/
public EvaluatedPersonData evaluate(PersonDataDefinition definition, EvaluationContext context) throws EvaluationException {

MappedObsForPersonDataDefinition mappedDataDefinition = (MappedObsForPersonDataDefinition) definition;
EvaluatedPersonData data = new EvaluatedPersonData(mappedDataDefinition, context);
Cohort cohort = context.getBaseCohort();
if (cohort != null && !cohort.isEmpty()) {
DataSetQueryService qs = Context.getService(DataSetQueryService.class);

StringBuilder hql = new StringBuilder();
Map<String, Object> mappings = new HashMap<String, Object>();

hql.append(" from Obs ");
hql.append(" where voided = false ");

hql.append(" and personId in (:patientIds) ");
mappings.put("patientIds", context.getBaseCohort());

int counter = 0;
for (Concept question : mappedDataDefinition.getQuestionAnswerMap().keySet()) {
// create the question clause, this question is mandatory!
String questionClause = " concept.conceptId = :question" + counter;
mappings.put("question" + counter, question.getConceptId());
// create the answer clause, this clause might have answer null.
Collection<Concept> answers = mappedDataDefinition.getAnswers(question);
String answerClause = " valueCoded.conceptId is null";
if (answers != null) {
answerClause = " valueCoded.conceptId in ( :answer" + counter + ")";
List<Integer> answerIds = new ArrayList<Integer>();
for (Concept answer : answers)
answerIds.add(answer.getConceptId());
mappings.put("answer" + counter, answerIds);
}
hql.append(" and ( ").append(questionClause).append(" and ").append(answerClause).append(" ) ");
counter ++;
}

if (mappedDataDefinition.getOnOrAfter() != null) {
hql.append(" and obsDatetime >= :onOrAfter ");
mappings.put("onOrAfter", mappedDataDefinition.getOnOrAfter());
}

if (mappedDataDefinition.getOnOrBefore() != null) {
hql.append(" and obsDatetime <= :onOrBefore ");
mappings.put("onOrBefore", DateUtil.getEndOfDayIfTimeExcluded(mappedDataDefinition.getOnOrBefore()));
}

String ordering = " asc ";
if (mappedDataDefinition.getWhich() == TimeQualifier.LAST)
ordering = " desc ";

hql.append(" order by obsDatetime ").append(ordering);

List<Object> queryResult = qs.executeHqlQuery(hql.toString(), mappings);

ListMap<Integer, Obs> obsForPatients = new ListMap<Integer, Obs>();
for (Object object : queryResult) {
Obs obs = (Obs) object;
obsForPatients.putInList(obs.getPersonId(), obs);
}

for (Integer personId : obsForPatients.keySet()) {
List<Obs> obsList = obsForPatients.get(personId);
if (mappedDataDefinition.getWhich() == TimeQualifier.LAST
|| mappedDataDefinition.getWhich() == TimeQualifier.FIRST) {
data.addData(personId, obsList.get(0));
} else {
data.addData(personId, obsList);
}
}
}

return data;
}
}