@Override
 public boolean visitsOverlap(Visit v1, Visit v2) {
   Location where1 = v1.getLocation();
   Location where2 = v2.getLocation();
   if ((where1 == null && where2 == null)
       || isSameOrAncestor(where1, where2)
       || isSameOrAncestor(where2, where1)) {
     // "same" location, so check if date ranges overlap (assuming startDatetime is never null)
     return (OpenmrsUtil.compareWithNullAsLatest(v1.getStartDatetime(), v2.getStopDatetime()) <= 0)
         && (OpenmrsUtil.compareWithNullAsLatest(v2.getStartDatetime(), v1.getStopDatetime())
             <= 0);
   }
   return false;
 }
  public Visit mergeVisits(Visit preferred, Visit nonPreferred) {
    // extend date range of winning
    if (OpenmrsUtil.compareWithNullAsEarliest(
            nonPreferred.getStartDatetime(), preferred.getStartDatetime())
        < 0) {
      preferred.setStartDatetime(nonPreferred.getStartDatetime());
    }
    if (preferred.getStopDatetime() != null
        && OpenmrsUtil.compareWithNullAsLatest(
                preferred.getStopDatetime(), nonPreferred.getStopDatetime())
            < 0) {
      preferred.setStopDatetime(nonPreferred.getStopDatetime());
    }

    // move encounters from losing into winning
    if (nonPreferred.getEncounters() != null) {
      for (Encounter e : nonPreferred.getEncounters()) {
        e.setPatient(preferred.getPatient());
        preferred.addEncounter(e);
        encounterService.saveEncounter(e);
      }
    }
    nonPreferred.setEncounters(
        null); // we need to manually the encounters from the non-preferred visit before voiding or
               // all the encounters we just moved will also get voided!

    visitService.voidVisit(
        nonPreferred, "EMR - Merge Patients: merged into visit " + preferred.getVisitId());
    visitService.saveVisit(preferred);
    return preferred;
  }
 /**
  * @param visit
  * @param location
  * @param when
  * @return true if when falls in the visits timespan AND location is within visit.location
  */
 @Override
 public boolean isSuitableVisit(Visit visit, Location location, Date when) {
   if (OpenmrsUtil.compare(when, visit.getStartDatetime()) < 0) {
     return false;
   }
   if (OpenmrsUtil.compareWithNullAsLatest(when, visit.getStopDatetime()) > 0) {
     return false;
   }
   return isSameOrAncestor(visit.getLocation(), location);
 }
 /**
  * Discontinues an order given a date and a reason, and saves it to the database if anything has
  * changed.
  *
  * @param order
  * @param effectiveDate
  * @param reason
  * @should change discontinued metadata if order is set to be discontinued after date
  * @should have no effect if order is discontinued before date
  */
 public static void discontinueOrder(Order order, Date date, Concept reason) {
   if (!order.isDiscontinuedRightNow()) {
     order.setDiscontinued(true);
     order.setDiscontinuedDate(date);
     order.setDiscontinuedReason(reason);
     Context.getOrderService().saveOrder(order);
   } else if (OpenmrsUtil.compareWithNullAsLatest(date, order.getDiscontinuedDate()) < 0) {
     order.setDiscontinued(true); // should already be true
     order.setDiscontinuedDate(date);
     order.setDiscontinuedReason(reason);
     Context.getOrderService().saveOrder(order);
   }
 }
示例#5
0
  /** @see java.lang.Comparable#compareTo(java.lang.Object) */
  public int compareTo(PersonAddress other) {
    int retValue = 0;
    if (other != null) {
      retValue = isVoided().compareTo(other.isVoided());
      if (retValue == 0) retValue = other.isPreferred().compareTo(isPreferred());
      if (retValue == 0 && getDateCreated() != null)
        retValue = OpenmrsUtil.compareWithNullAsLatest(getDateCreated(), other.getDateCreated());
      if (retValue == 0)
        retValue =
            OpenmrsUtil.compareWithNullAsGreatest(getPersonAddressId(), other.getPersonAddressId());

      // if we've gotten this far, just check all address values. If they are
      // equal, leave the objects at 0. If not, arbitrarily pick retValue=1
      // and return that (they are not equal).
      if (retValue == 0 && !equalsContent(other)) retValue = 1;
    }
    return retValue;
  }
 /**
  * Extracts patients from a calculation result map with date results in the given range
  *
  * @param results the calculation result map
  * @param minDateInclusive the minimum date (inclusive)
  * @param maxDateInclusive the maximum date (inclusive)
  * @return the extracted patient ids
  */
 protected static Set<Integer> datesWithinRange(
     CalculationResultMap results, Date minDateInclusive, Date maxDateInclusive) {
   Set<Integer> ret = new HashSet<Integer>();
   for (Map.Entry<Integer, CalculationResult> e : results.entrySet()) {
     Date result = null;
     try {
       result = e.getValue().asType(Date.class);
     } catch (Exception ex) {
       // pass
     }
     if (result != null) {
       if (OpenmrsUtil.compareWithNullAsEarliest(result, minDateInclusive) >= 0
           && OpenmrsUtil.compareWithNullAsLatest(result, maxDateInclusive) <= 0) {
         ret.add(e.getKey());
       }
     }
   }
   return ret;
 }