/** * @see org.openmrs.logic.Rule#eval(org.openmrs.logic.LogicContext, java.lang.Integer, * java.util.Map) */ public Result eval(LogicContext context, Integer patientId, Map<String, Object> parameters) throws LogicException { Integer index = null; List<Result> results = null; Result distinctResult = null; if (parameters != null) { Object param1Obj = parameters.get("param1"); if (param1Obj != null) { index = Integer.parseInt((String) param1Obj); } results = (List<Result>) parameters.get("param2"); } if (index != null && results != null && index < results.toArray().length) { // Sort the results by date Collections.sort(results, new ResultDateComparator()); // Reverse the list Collections.reverse(results); distinctResult = (Result) results.toArray()[index]; } if (distinctResult == null) { distinctResult = new Result(); } if (distinctResult.toString() == null) { distinctResult.setValueText(String.valueOf(distinctResult.toNumber())); } return distinctResult; }
/** * @return string representation of the result. For non-text results, this will either be the * overridden text value (if specifically defined) or a string representation of the default * datatype value. If the result is a list, then the string representation of all members a * joined with commas. */ public String toString() { if (isSingleResult()) { if (datatype == null) { return valueText == null ? "" : valueText; } switch (datatype) { case BOOLEAN: return (valueBoolean ? "true" : "false"); case CODED: return (valueCoded == null ? "" : valueCoded.getBestName(Context.getLocale()).getName()); case DATETIME: return (valueDatetime == null ? "" : Context.getDateFormat().format(valueDatetime)); case NUMERIC: return (valueNumeric == null ? "" : String.valueOf(valueNumeric)); case TEXT: return (valueText == null ? "" : valueText); default: return valueText; } } StringBuffer s = new StringBuffer(); for (Result r : this) { if (s.length() > 0) { s.append(","); } s.append(r.toString()); } return s.toString(); }