Skip to content
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
3 changes: 3 additions & 0 deletions api/src/main/java/org/openmrs/Obs.java
Original file line number Diff line number Diff line change
Expand Up @@ -1279,5 +1279,8 @@ public ObsReferenceRange getReferenceRange() {
*/
public void setReferenceRange(ObsReferenceRange referenceRange) {
this.referenceRange = referenceRange;
if (referenceRange != null) {
referenceRange.setObs(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,10 @@ public Obs getCurrentObs(String conceptRef, Obs currentObs) {
/**
* Gets the person's latest observation date for a given concept
*
* @param conceptRef can be either concept uuid or conceptMap's code and sourceName
* e.g "bac25fd5-c143-4e43-bffe-4eb1e7efb6ce" or "CIEL:1434"
* @param conceptRef can be either concept uuid or conceptMap's code and sourceName e.g
* "bac25fd5-c143-4e43-bffe-4eb1e7efb6ce" or "CIEL:1434"
* @param person the person
*
* @return the observation date
*
* @since 2.7.8
*/
public Date getLatestObsDate(String conceptRef, Person person) {
Expand All @@ -187,16 +185,14 @@ public Date getLatestObsDate(String conceptRef, Person person) {
/**
* Checks if an observation's value coded answer is equal to a given concept
*
* @param conceptRef can be either concept uuid or conceptMap's code and sourceName
* e.g "bac25fd5-c143-4e43-bffe-4eb1e7efb6ce" or "CIEL:1434" for the observation's question
*
* @param conceptRef can be either concept uuid or conceptMap's code and sourceName e.g
* "bac25fd5-c143-4e43-bffe-4eb1e7efb6ce" or "CIEL:1434" for the observation's
* question
* @param person the person
*
* @param answerConceptRef can be either concept uuid or conceptMap's code and sourceName
* e.g "bac25fd5-c143-4e43-bffe-4eb1e7efb6ce" or "CIEL:1434" for the observation's coded answer
*
* @param answerConceptRef can be either concept uuid or conceptMap's code and sourceName e.g
* "bac25fd5-c143-4e43-bffe-4eb1e7efb6ce" or "CIEL:1434" for the observation's coded
* answer
* @return true if the given concept is equal to the observation's value coded answer
*
* @since 2.7.8
*/
public boolean isObsValueCodedAnswer(String conceptRef, Person person, String answerConceptRef) {
Expand All @@ -219,14 +215,13 @@ public boolean isObsValueCodedAnswer(String conceptRef, Person person, String an
}

/**
* Gets the number of days from the person's latest observation date value for a given concept to the current date
* Gets the number of days from the person's latest observation date value for a given concept
* to the current date
*
* @param conceptRef can be either concept uuid or conceptMap's code and sourceName
* e.g "bac25fd5-c143-4e43-bffe-4eb1e7efb6ce" or "CIEL:1434"
* @param conceptRef can be either concept uuid or conceptMap's code and sourceName e.g
* "bac25fd5-c143-4e43-bffe-4eb1e7efb6ce" or "CIEL:1434"
* @param person the person
*
* @return the number of days
*
* @since 2.7.8
*/
public long getObsDays(String conceptRef, Person person) {
Expand All @@ -238,14 +233,13 @@ public long getObsDays(String conceptRef, Person person) {
}

/**
* Gets the number of weeks from the person's latest observation date value for a given concept to the current date
* Gets the number of weeks from the person's latest observation date value for a given concept
* to the current date
*
* @param conceptRef can be either concept uuid or conceptMap's code and sourceName
* e.g "bac25fd5-c143-4e43-bffe-4eb1e7efb6ce" or "CIEL:1434"
* @param conceptRef can be either concept uuid or conceptMap's code and sourceName e.g
* "bac25fd5-c143-4e43-bffe-4eb1e7efb6ce" or "CIEL:1434"
* @param person the person
*
* @return the number of weeks
*
* @since 2.7.8
*/
public long getObsWeeks(String conceptRef, Person person) {
Expand All @@ -257,14 +251,13 @@ public long getObsWeeks(String conceptRef, Person person) {
}

/**
* Gets the number of months from the person's latest observation date value for a given concept to the current date
* Gets the number of months from the person's latest observation date value for a given concept
* to the current date
*
* @param conceptRef can be either concept uuid or conceptMap's code and sourceName
* e.g "bac25fd5-c143-4e43-bffe-4eb1e7efb6ce" or "CIEL:1434"
* @param conceptRef can be either concept uuid or conceptMap's code and sourceName e.g
* "bac25fd5-c143-4e43-bffe-4eb1e7efb6ce" or "CIEL:1434"
* @param person the person
*
* @return the number of months
*
* @since 2.7.8
*/
public long getObsMonths(String conceptRef, Person person) {
Expand All @@ -276,14 +269,13 @@ public long getObsMonths(String conceptRef, Person person) {
}

/**
* Gets the number of years from the person's latest observation date value for a given concept to the current date
* Gets the number of years from the person's latest observation date value for a given concept
* to the current date
*
* @param conceptRef can be either concept uuid or conceptMap's code and sourceName
* e.g "bac25fd5-c143-4e43-bffe-4eb1e7efb6ce" or "CIEL:1434"
* @param conceptRef can be either concept uuid or conceptMap's code and sourceName e.g
* "bac25fd5-c143-4e43-bffe-4eb1e7efb6ce" or "CIEL:1434"
* @param person the person
*
* @return the number of years
*
* @since 2.7.8
*/
public long getObsYears(String conceptRef, Person person) {
Expand All @@ -299,9 +291,7 @@ public long getObsYears(String conceptRef, Person person) {
*
* @param fromDate the date from which to start counting
* @param dateDate the date up to which to stop counting
*
* @return the number of days between
*
* @since 2.7.8
*/
public long getDaysBetween(Date fromDate, Date toDate) {
Expand All @@ -316,9 +306,7 @@ public long getDaysBetween(Date fromDate, Date toDate) {
*
* @param fromDate the date from which to start counting
* @param dateDate the date up to which to stop counting
*
* @return the number of weeks between
*
* @since 2.7.8
*/
public long getWeeksBetween(Date fromDate, Date toDate) {
Expand All @@ -333,9 +321,7 @@ public long getWeeksBetween(Date fromDate, Date toDate) {
*
* @param fromDate the date from which to start counting
* @param dateDate the date up to which to stop counting
*
* @return the number of months between
*
* @since 2.7.8
*/
public long getMonthsBetween(Date fromDate, Date toDate) {
Expand All @@ -350,9 +336,7 @@ public long getMonthsBetween(Date fromDate, Date toDate) {
*
* @param fromDate the date from which to start counting
* @param dateDate the date up to which to stop counting
*
* @return the number of years between
*
* @since 2.7.8
*/
public long getYearsBetween(Date fromDate, Date toDate) {
Expand All @@ -367,7 +351,6 @@ public long getYearsBetween(Date fromDate, Date toDate) {
*
* @param fromDate the date from which to start counting
* @return the number of days
*
* @since 2.7.8
*/
public long getDays(Date fromDate) {
Expand All @@ -379,7 +362,6 @@ public long getDays(Date fromDate) {
*
* @param fromDate the date from which to start counting
* @return the number of weeks
*
* @since 2.7.8
*/
public long getWeeks(Date fromDate) {
Expand All @@ -391,7 +373,6 @@ public long getWeeks(Date fromDate) {
*
* @param fromDate the date from which to start counting
* @return the number of months
*
* @since 2.7.8
*/
public long getMonths(Date fromDate) {
Expand All @@ -403,7 +384,6 @@ public long getMonths(Date fromDate) {
*
* @param fromDate the date from which to start counting
* @return the number of years
*
* @since 2.7.8
*/
public long getYears(Date fromDate) {
Expand Down
76 changes: 42 additions & 34 deletions api/src/main/java/org/openmrs/validator/ObsValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
package org.openmrs.validator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

import org.apache.commons.text.StringEscapeUtils;
import org.openmrs.BaseReferenceRange;
import org.openmrs.Concept;
import org.openmrs.ConceptDatatype;
import org.openmrs.ConceptNumeric;
Expand All @@ -27,7 +25,6 @@
import org.openmrs.api.context.Context;
import org.openmrs.api.db.hibernate.HibernateUtil;
import org.openmrs.util.ConceptReferenceRangeUtility;
import org.openmrs.util.OpenmrsUtil;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

Expand Down Expand Up @@ -198,7 +195,7 @@ else if (!isObsGroup) {
}
}

validateConceptReferenceRange(obs, errors, atRootNode);
validateReferenceRange(obs, errors, atRootNode);
} else if (dt.isText() && obs.getValueText() == null) {
if (atRootNode) {
errors.rejectValue("valueText", "error.null");
Expand Down Expand Up @@ -259,18 +256,22 @@ else if (!isObsGroup) {
* @param obs Observation to validate
* @param errors Errors to record validation issues
*/
private void validateConceptReferenceRange(Obs obs, Errors errors, boolean atRootNode) {
ConceptReferenceRange conceptReferenceRange = getReferenceRange(obs);
private void validateReferenceRange(Obs obs, Errors errors, boolean atRootNode) {
BaseReferenceRange referenceRange = obs.getReferenceRange();
if (referenceRange == null) {
referenceRange = getReferenceRange(obs);
}

if (conceptReferenceRange != null) {
validateAbsoluteRanges(obs, conceptReferenceRange, errors, atRootNode);
if (referenceRange != null) {
validateAbsoluteRanges(obs, referenceRange, errors, atRootNode);

if (obs.getId() == null) {
setObsReferenceRange(obs, conceptReferenceRange);
setObsReferenceRange(obs, referenceRange);
}
} else if (obs.getId() == null) {
setObsReferenceRange(obs);
}

setObsInterpretation(obs);
}

Expand Down Expand Up @@ -410,16 +411,16 @@ private ConceptReferenceRange findStrictestReferenceRange(List<ConceptReferenceR
* Validates the high and low absolute values of the Obs.
*
* @param obs Observation to validate
* @param conceptReferenceRange ConceptReferenceRange containing the range values
* @param referenceRange ConceptReferenceRange containing the range values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intend to replace ConceptReferenceRange with maybe BaseReferenceRange?

* @param errors Errors to record validation issues
*/
private void validateAbsoluteRanges(Obs obs, ConceptReferenceRange conceptReferenceRange, Errors errors, boolean atRootNode) {
if (conceptReferenceRange.getHiAbsolute() != null && conceptReferenceRange.getHiAbsolute() < obs.getValueNumeric()) {
private void validateAbsoluteRanges(Obs obs, BaseReferenceRange referenceRange, Errors errors, boolean atRootNode) {
if (referenceRange.getHiAbsolute() != null && referenceRange.getHiAbsolute() < obs.getValueNumeric()) {
if (atRootNode) {
errors.rejectValue(
"valueNumeric",
"error.value.outOfRange.high",
new Object[] { conceptReferenceRange.getHiAbsolute() },
new Object[] { referenceRange.getHiAbsolute() },
null
);
} else {
Expand All @@ -432,12 +433,12 @@ private void validateAbsoluteRanges(Obs obs, ConceptReferenceRange conceptRefere
}
}

if (conceptReferenceRange.getLowAbsolute() != null && conceptReferenceRange.getLowAbsolute() > obs.getValueNumeric()) {
if (referenceRange.getLowAbsolute() != null && referenceRange.getLowAbsolute() > obs.getValueNumeric()) {
if (atRootNode) {
errors.rejectValue(
"valueNumeric",
"error.value.outOfRange.low",
new Object[] { conceptReferenceRange.getLowAbsolute() },
new Object[] { referenceRange.getLowAbsolute() },
null
);
} else {
Expand All @@ -455,20 +456,23 @@ private void validateAbsoluteRanges(Obs obs, ConceptReferenceRange conceptRefere
* Builds and sets the ObsReferenceRange for the given Obs.
*
* @param obs Observation to set the reference range
* @param conceptReferenceRange ConceptReferenceRange used to build the ObsReferenceRange
* @param referenceRange ConceptReferenceRange used to build the ObsReferenceRange
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intend to replace ConceptReferenceRange with maybe BaseReferenceRange?

*/
private void setObsReferenceRange(Obs obs, ConceptReferenceRange conceptReferenceRange) {
ObsReferenceRange obsRefRange = new ObsReferenceRange();

obsRefRange.setHiAbsolute(conceptReferenceRange.getHiAbsolute());
obsRefRange.setHiCritical(conceptReferenceRange.getHiCritical());
obsRefRange.setHiNormal(conceptReferenceRange.getHiNormal());
obsRefRange.setLowAbsolute(conceptReferenceRange.getLowAbsolute());
obsRefRange.setLowCritical(conceptReferenceRange.getLowCritical());
obsRefRange.setLowNormal(conceptReferenceRange.getLowNormal());
obsRefRange.setObs(obs);

obs.setReferenceRange(obsRefRange);
private void setObsReferenceRange(Obs obs, BaseReferenceRange referenceRange) {
if (referenceRange instanceof ObsReferenceRange) {
if (((ObsReferenceRange) referenceRange).getObs() == null) {
obs.setReferenceRange((ObsReferenceRange) referenceRange);
}
} else {
ObsReferenceRange obsRefRange = new ObsReferenceRange();
obsRefRange.setHiAbsolute(referenceRange.getHiAbsolute());
obsRefRange.setHiCritical(referenceRange.getHiCritical());
obsRefRange.setHiNormal(referenceRange.getHiNormal());
obsRefRange.setLowAbsolute(referenceRange.getLowAbsolute());
obsRefRange.setLowCritical(referenceRange.getLowCritical());
obsRefRange.setLowNormal(referenceRange.getLowNormal());
obs.setReferenceRange(obsRefRange);
}
}

/**
Expand All @@ -479,33 +483,37 @@ private void setObsReferenceRange(Obs obs, ConceptReferenceRange conceptReferenc
private void setObsReferenceRange(Obs obs) {
if (obs.getConcept() == null) {
return;
} else if (obs.getReferenceRange() != null) {
return;
}

ConceptNumeric conceptNumeric = Context.getConceptService().getConceptNumeric(obs.getConcept().getId());

if (conceptNumeric != null) {
ObsReferenceRange obsRefRange = new ObsReferenceRange();

obsRefRange.setHiAbsolute(conceptNumeric.getHiAbsolute());
obsRefRange.setHiCritical(conceptNumeric.getHiCritical());
obsRefRange.setHiNormal(conceptNumeric.getHiNormal());
obsRefRange.setLowAbsolute(conceptNumeric.getLowAbsolute());
obsRefRange.setLowCritical(conceptNumeric.getLowCritical());
obsRefRange.setLowNormal(conceptNumeric.getLowNormal());
obsRefRange.setObs(obs);

obs.setReferenceRange(obsRefRange);
}
}


/**
* This method sets Obs interpretation based on the current obs' numeric value.
*
* @param obs Observation to set the interpretation
*/
private void setObsInterpretation(Obs obs) {
if (obs.getInterpretation() != null || obs.getValueNumeric() == null) {
return;
}

ObsReferenceRange referenceRange = obs.getReferenceRange();
if (referenceRange == null || obs.getValueNumeric() == null) {
if (referenceRange == null) {
return;
}

Expand Down
Loading
Loading