| 1 |
/** |
|---|
| 2 |
* The contents of this file are subject to the OpenMRS Public License |
|---|
| 3 |
* Version 1.0 (the "License"); you may not use this file except in |
|---|
| 4 |
* compliance with the License. You may obtain a copy of the License at |
|---|
| 5 |
* http://license.openmrs.org |
|---|
| 6 |
* |
|---|
| 7 |
* Software distributed under the License is distributed on an "AS IS" |
|---|
| 8 |
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|---|
| 9 |
* License for the specific language governing rights and limitations |
|---|
| 10 |
* under the License. |
|---|
| 11 |
* |
|---|
| 12 |
* Copyright (C) OpenMRS, LLC. All Rights Reserved. |
|---|
| 13 |
*/ |
|---|
| 14 |
package org.openmrs.module.chica.rule; |
|---|
| 15 |
|
|---|
| 16 |
import java.util.Calendar; |
|---|
| 17 |
import java.util.Date; |
|---|
| 18 |
import java.util.Map; |
|---|
| 19 |
import java.util.Set; |
|---|
| 20 |
|
|---|
| 21 |
import org.openmrs.Patient; |
|---|
| 22 |
import org.openmrs.logic.LogicContext; |
|---|
| 23 |
import org.openmrs.logic.LogicException; |
|---|
| 24 |
import org.openmrs.logic.Rule; |
|---|
| 25 |
import org.openmrs.logic.result.Result; |
|---|
| 26 |
import org.openmrs.logic.result.Result.Datatype; |
|---|
| 27 |
import org.openmrs.logic.rule.RuleParameterInfo; |
|---|
| 28 |
import org.openmrs.module.atd.hibernateBeans.FormInstance; |
|---|
| 29 |
import org.openmrs.module.dss.util.Util; |
|---|
| 30 |
|
|---|
| 31 |
/** |
|---|
| 32 |
* |
|---|
| 33 |
* Calculates a person's age in years based from their date of birth to the |
|---|
| 34 |
* index date |
|---|
| 35 |
* |
|---|
| 36 |
*/ |
|---|
| 37 |
public class ChicaAgeRule implements Rule { |
|---|
| 38 |
|
|---|
| 39 |
/** |
|---|
| 40 |
* |
|---|
| 41 |
* @see org.openmrs.logic.Rule#eval(org.openmrs.logic.LogicContext, org.openmrs.Patient, java.util.Map) |
|---|
| 42 |
*/ |
|---|
| 43 |
public Result eval(LogicContext context, Patient patient, |
|---|
| 44 |
Map<String, Object> parameters) throws LogicException { |
|---|
| 45 |
|
|---|
| 46 |
Date birthdate = context.read(patient, |
|---|
| 47 |
context.getLogicDataSource("person"), "BIRTHDATE").toDatetime(); |
|---|
| 48 |
|
|---|
| 49 |
if(birthdate == null){ |
|---|
| 50 |
return Result.emptyResult(); |
|---|
| 51 |
} |
|---|
| 52 |
int age = 0; |
|---|
| 53 |
Calendar bdate = Calendar.getInstance(); |
|---|
| 54 |
bdate.setTime(birthdate); |
|---|
| 55 |
|
|---|
| 56 |
Calendar now = Calendar.getInstance(); |
|---|
| 57 |
now.setTime(context.getIndexDate()); |
|---|
| 58 |
|
|---|
| 59 |
// calculate age as the difference in what the parameter says. |
|---|
| 60 |
if (parameters != null) |
|---|
| 61 |
{ |
|---|
| 62 |
String param = (String) parameters.get("param1"); |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
if(param.compareToIgnoreCase("years") == 0) { |
|---|
| 66 |
|
|---|
| 67 |
age = org.openmrs.module.dss.util.Util.getAgeInUnits(birthdate, now.getTime(), Util.YEAR_ABBR); |
|---|
| 68 |
} |
|---|
| 69 |
else if(param.compareToIgnoreCase("months") == 0) { |
|---|
| 70 |
|
|---|
| 71 |
age = org.openmrs.module.dss.util.Util.getAgeInUnits(birthdate, now.getTime(), Util.MONTH_ABBR); |
|---|
| 72 |
} |
|---|
| 73 |
else if(param.compareToIgnoreCase("days") == 0) { |
|---|
| 74 |
|
|---|
| 75 |
age = org.openmrs.module.dss.util.Util.getAgeInUnits(birthdate, now.getTime(), Util.DAY_ABBR); |
|---|
| 76 |
} |
|---|
| 77 |
else if(param.compareToIgnoreCase("weeks") == 0) { |
|---|
| 78 |
|
|---|
| 79 |
age = org.openmrs.module.dss.util.Util.getAgeInUnits(birthdate, now.getTime(), Util.WEEK_ABBR); |
|---|
| 80 |
} |
|---|
| 81 |
} |
|---|
| 82 |
else |
|---|
| 83 |
{ |
|---|
| 84 |
return Result.emptyResult(); |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
return new Result(age); |
|---|
| 88 |
|
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
/** |
|---|
| 92 |
* @see org.openmrs.logic.rule.Rule#getParameterList() |
|---|
| 93 |
*/ |
|---|
| 94 |
public Set<RuleParameterInfo> getParameterList() { |
|---|
| 95 |
return null; |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
/** |
|---|
| 99 |
* @see org.openmrs.logic.rule.Rule#getDependencies() |
|---|
| 100 |
*/ |
|---|
| 101 |
public String[] getDependencies() { |
|---|
| 102 |
return new String[] { "%%patient.birthdate" }; |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
/** |
|---|
| 106 |
* @see org.openmrs.logic.rule.Rule#getTTL() |
|---|
| 107 |
*/ |
|---|
| 108 |
public int getTTL() { |
|---|
| 109 |
return 60 * 60 * 24; // 1 day |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
/** |
|---|
| 113 |
* @see org.openmrs.logic.rule.Rule#getDatatype(String) |
|---|
| 114 |
*/ |
|---|
| 115 |
public Datatype getDefaultDatatype() { |
|---|
| 116 |
return Datatype.NUMERIC; |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
} |
|---|