Advertisement
Guest User

Untitled

a guest
May 5th, 2013
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.68 KB | None | 0 0
  1. package cl.ehs.openmrs.module.midoctorpanel.cohortalert;
  2.  
  3. import cl.ehs.openmrs.module.midoctorpanel.MiDoctorPanelConstants;
  4. import java.text.DateFormat;
  5. import java.text.ParseException;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.Calendar;
  9. import java.util.Date;
  10. import java.util.LinkedList;
  11. import java.util.List;
  12. import java.util.Locale;
  13. import java.util.Set;
  14. import org.apache.commons.logging.Log;
  15. import org.apache.commons.logging.LogFactory;
  16. import org.openmrs.Cohort;
  17. import org.openmrs.Concept;
  18. import org.openmrs.Encounter;
  19. import org.openmrs.EncounterType;
  20. import org.openmrs.Obs;
  21. import org.openmrs.Patient;
  22. import org.openmrs.PatientProgram;
  23. import org.openmrs.Person;
  24. import org.openmrs.api.AdministrationService;
  25. import org.openmrs.api.CohortService;
  26. import org.openmrs.api.ConceptService;
  27. import org.openmrs.api.EncounterService;
  28. import org.openmrs.api.LocationService;
  29. import org.openmrs.api.ObsService;
  30. import org.openmrs.api.PatientIdentifierException;
  31. import org.openmrs.api.context.Context;
  32. import org.openmrs.cohort.CohortDefinitionItemHolder;
  33. import org.openmrs.reporting.PatientSearch;
  34. import org.openmrs.reporting.SearchArgument;
  35.  
  36. /**
  37.  * Class to deal with the implementation of the CohortAlert functionality
  38.  */
  39. public class CohortAlertCompatibilityImpl implements CohortAlertService {
  40.    
  41.     private static Log log = LogFactory.getLog(CohortAlertService.class);
  42.    
  43.     CohortService cohortService;
  44.    
  45.     public CohortAlertCompatibilityImpl() {
  46.         cohortService = Context.getCohortService();
  47.     }
  48.    
  49.     /**
  50.      * Returns the list of patientSearch cohorts.
  51.      * @returns List<CohortDefinitionItemHolder>
  52.      */
  53.     @Override
  54.     public List<CohortDefinitionItemHolder> getCohortDefs() {
  55.         List<CohortDefinitionItemHolder> cohortDefs = new LinkedList<CohortDefinitionItemHolder>();
  56.         List<String> cohortIds = new ArrayList<String>();
  57.         String moduleDependency = Context.getAdministrationService().getGlobalProperty(
  58.             MiDoctorPanelConstants.COHORT_MODULE_GLOBAL_PROPERTY_NAME);
  59.         String cohortIdShowList = Context.getAdministrationService().getGlobalProperty(
  60.             MiDoctorPanelConstants.COHORT_ID_SHOW_LIST_GLOBAL_PROPERTY_NAME);
  61.         if (cohortIdShowList != null) {
  62.             if (cohortIdShowList.contains(",")) {
  63.                 cohortIds = Arrays.asList(cohortIdShowList.replaceAll("\\s", "").split(","));
  64.             } else {
  65.                 cohortIds.add(cohortIdShowList.trim());
  66.             }
  67.         }
  68.         if (moduleDependency.equals("reportingcompatibility")) {
  69.             List<CohortDefinitionItemHolder> allCohortDefs = cohortService.getAllCohortDefinitions();
  70.             for (CohortDefinitionItemHolder c : allCohortDefs) {
  71.                 if (c.getKey().contains("org.openmrs.reporting.PatientSearch")) {
  72.                     if (cohortIds.contains(c.getKey().split(":")[0])) {
  73.                         cohortDefs.add(c);
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.         return cohortDefs;
  79.     }
  80.    
  81.     /**
  82.      * Returns list of all the patients inside the given cohort.
  83.      * @param CohortDefinitionItemHolder
  84.      * @returns List<Patient>
  85.      */
  86.     @Override
  87.     public List<Patient> getPatientsInCohort(CohortDefinitionItemHolder c) throws PatientIdentifierException {
  88.         List<Patient> patients = new LinkedList<Patient>();
  89.         try {
  90.             Cohort cohort = cohortService.evaluate(c.getCohortDefinition(), null);
  91.             Set<Integer> memberIds = cohort.getMemberIds();
  92.             for (Integer i : memberIds) {
  93.                 patients.add(Context.getPatientService().getPatient(i));
  94.             }
  95.         }
  96.         catch (PatientIdentifierException e) {
  97.             log.error(e);
  98.         }
  99.         return patients;
  100.     }
  101.    
  102.     /**
  103.      * @param String
  104.      * @returns cohort from given name
  105.      */
  106.     public Cohort getCohortByName(String name) {
  107.         return cohortService.getCohort(name);
  108.     }
  109.    
  110.     /**
  111.      * Returns the entry date of the given patient in respective cohort
  112.      * @param CohortDefinitionItemHolder
  113.      * @param patient
  114.      * @returns Date
  115.      */
  116.     @Override
  117.     public Date getPatientEntryDateIntoCohort(CohortDefinitionItemHolder c, Patient patient)
  118.             throws IllegalArgumentException, ParseException {
  119.         if (c.getKey().contains("org.openmrs.reporting.PatientSearch")) {
  120.             if (getPatientsInCohort(c).contains(patient)) {
  121.                 PatientSearch ps = (PatientSearch) c.getCohortDefinition();
  122.                 List<SearchArgument> arguments = ps.getArguments();
  123.                 if (arguments != null) {
  124.                     for (SearchArgument sa : arguments) {
  125.                         String searchPropClass = sa.getPropertyClass().getName();
  126.                         if (searchPropClass.contains("TimeModifier")) {
  127.                             //all obs-based
  128.                             ObsService obsService = Context.getObsService();
  129.                             ConceptService conceptService = Context.getConceptService();
  130.                             Concept concept = null;
  131.                             Date withinMonths = null;
  132.                             Date withinDays = null;
  133.                             Date sinceDate = null;
  134.                             Date untilDate = null;
  135.                             List<Person> persons = new LinkedList<Person>();
  136.                             persons.add(patient);
  137.                             for (SearchArgument args : arguments) {
  138.                                 if (args.getName().equals("value")) {
  139.                                     if (!args.getValue().equals("")) {
  140.                                         concept = conceptService.getConcept(Integer.valueOf(args.getValue()));
  141.                                         continue;
  142.                                     }
  143.                                 }
  144.                                 if (args.getName().equals("withinLastMonths")) {
  145.                                     Calendar cal = Calendar.getInstance();
  146.                                     cal.add(Calendar.MONTH, (Integer.valueOf(args.getValue()) * -1));
  147.                                     withinMonths = cal.getTime();
  148.                                     continue;
  149.                                 }
  150.                                 if (args.getName().equals("withinLastDays")) {
  151.                                     Calendar cal = Calendar.getInstance();
  152.                                     cal.add(Calendar.DATE, (Integer.valueOf(args.getValue()) * -1));
  153.                                     withinDays = cal.getTime();
  154.                                     continue;
  155.                                 }
  156.                                 if (args.getName().equals("sinceDate")) {
  157.                                     DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
  158.                                     sinceDate = formatter.parse(args.getValue());
  159.                                     continue;
  160.                                 }
  161.                                 if (args.getName().equals("untilDate")) {
  162.                                     DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
  163.                                     untilDate = formatter.parse(args.getValue());
  164.                                     continue;
  165.                                 }
  166.                                
  167.                             }
  168.                             if (concept != null) {
  169.                                 List<Concept> concepts = new LinkedList<Concept>();
  170.                                 concepts.add(concept);
  171.                                 List<Obs> obs = new LinkedList<Obs>();
  172.                                 if (withinMonths == null || withinDays == null) {
  173.                                     obs = obsService.getObservations(persons, null, null, concepts, null, null, null, null,
  174.                                         null, sinceDate, untilDate, false);
  175.                                 } else if (withinMonths != null) {
  176.                                     obs = obsService.getObservations(persons, null, null, concepts, null, null, null, null,
  177.                                         null, withinMonths, new Date(), false);
  178.                                 } else if (withinDays != null) {
  179.                                     obs = obsService.getObservations(persons, null, null, concepts, null, null, null, null,
  180.                                         null, withinDays, new Date(), false);
  181.                                 }
  182.                                 if (obs.size() > 0) {
  183.                                     return obs.get(obs.size() - 1).getObsDatetime();
  184.                                 }
  185.                             }
  186.                         } else if (searchPropClass.contains("java.util.Date")) {
  187.                             //without timemodifierdate
  188.                             //Dont know what to do
  189.                         } else if (searchPropClass.contains("org.openmrs.EncounterType")) {
  190.                             //last encDate at location
  191.                             EncounterService es = Context.getEncounterService();
  192.                             LocationService ls = Context.getLocationService();
  193.                             List<EncounterType> encType = new LinkedList<EncounterType>();
  194.                             encType.add(es.getEncounterType(Integer.valueOf(sa.getValue())));
  195.                             for (SearchArgument args : arguments) {
  196.                                 if (args.getPropertyClass().getName().contains("org.openmrs.Location")) {
  197.                                     List<Encounter> encounters = es.getEncounters(patient, ls.getLocation(Integer
  198.                                             .valueOf(args.getValue())), null, null, null, encType, false);
  199.                                    
  200.                                     return encounters.get(encounters.size() - 1).getEncounterDatetime();
  201.                                 }
  202.                             }
  203.                             List<Encounter> encounters = es.getEncounters(patient, null, null, null, null, encType, false);
  204.                            
  205.                             return encounters.get(encounters.size() - 1).getEncounterDatetime();
  206.                         } else if (searchPropClass.contains("org.openmrs.Program")) {
  207.                             PatientProgram patientProgram = Context.getProgramWorkflowService().getPatientProgram(
  208.                                 Integer.valueOf(sa.getValue()));
  209.                             for (SearchArgument args : arguments) {
  210.                                 if (args.getName().contains("sinceDate")) {
  211.                                     if (patientProgram.getCurrentStates().size() > 0) {
  212.                                         return patientProgram.getCurrentState().getStartDate();
  213.                                     }
  214.                                 } else if (args.getName().contains("untilDate")) {
  215.                                     if (patientProgram.getCurrentStates().size() > 0) {
  216.                                         return patientProgram.getCurrentState().getEndDate();
  217.                                     }
  218.                                 }
  219.                             }
  220.                            
  221.                             return patientProgram.getDateEnrolled();
  222.                         } else if (sa.getName().contains("Age") || sa.getName().contains("attribute")
  223.                                 || sa.getName().contains("gender") || sa.getName().contains("birthdate")
  224.                                 || sa.getName().contains("aliveOnly")) {
  225.                            
  226.                             return patient.getBirthdate();
  227.                         } else if (sa.getName().contains("deadOnly")) {
  228.                            
  229.                             return patient.getDeathDate();
  230.                         } else {
  231.                            
  232.                             return patient.getDateCreated();
  233.                         }
  234.                        
  235.                     }
  236.                 }
  237.                 return new Date();
  238.             } else {
  239.                 throw new IllegalArgumentException("Patient does not belong in cohort");
  240.             }
  241.         } else {
  242.             throw new IllegalArgumentException("Cohort definition is not valid patient search");
  243.         }
  244.     }
  245.    
  246.     /**
  247.      * Returns CohortAlertStatus as per the given date.
  248.      * @param Date
  249.      * @returns CohortAlertStatus enum
  250.      */
  251.     @Override
  252.     public CohortAlertStatus lateStatusCheck(Date date) {
  253.         AdministrationService as = Context.getAdministrationService();
  254.         Integer daysLate = Integer
  255.                 .valueOf(as.getGlobalProperty(MiDoctorPanelConstants.NO_OF_DAYS_LATE_GLOBAL_PROPERTY_NAME));
  256.         Integer daysSemiLate = Integer.valueOf(as
  257.                 .getGlobalProperty(MiDoctorPanelConstants.NO_OF_DAYS_SEMI_LATE_GLOBAL_PROPERTY_NAME));
  258.         Integer daysWithinRange = Integer.valueOf(as
  259.                 .getGlobalProperty(MiDoctorPanelConstants.NO_OF_DAYS_WITHIN_RANGE_GLOBAL_PROPERTY_NAME));
  260.        
  261.         int dateDiff = DateUtils.getDaysBetweenDates(date, new Date());
  262.         if (dateDiff >= daysLate) {
  263.             return CohortAlertStatus.LATE;
  264.         } else if (dateDiff >= daysSemiLate) {
  265.             return CohortAlertStatus.SEMILATE;
  266.         } else {
  267.             return CohortAlertStatus.WITHINRANGE;
  268.         }
  269.     }
  270. }
  271.  
  272. class DateUtils {
  273.    
  274.     private static final long MILISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
  275.    
  276.     /**
  277.      * Returns Number of days between two given dates.
  278.      * @param Date
  279.      * @param Date
  280.      * @returns int
  281.      */
  282.     public static int getDaysBetweenDates(Date startDate, Date endDate) {
  283.         long diff = endDate.getTime() - startDate.getTime();
  284.         int days = (int) Math.floor(diff / MILISECONDS_PER_DAY);
  285.         return Math.abs(days);
  286.     }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement