/** * Compares two Date/Timestamp objects, treating null as the earliest possible date. */ public static int compareWithNullAsEarliest(Date d1, Date d2) { if (d1 == null && d2 == null) { return 0; } if (d1 == null) { return -1; } else if (d2 == null) { return 1; } else { return compare(d1, d2); } }
/** * Compares asOfDate to [startDate, endDate], inclusive of both endpoints. * @param asOfDate date to compare if membership is active or inactive * @return boolean true/false if membership is active/inactive */ public boolean isActive(Date asOfDate) { Date date = asOfDate == null ? new Date() : asOfDate; return !this.getVoided() && OpenmrsUtil.compare(startDate, date) <= 0 && OpenmrsUtil.compareWithNullAsLatest(date, endDate) <= 0; }
/** * Compares two Date/Timestamp objects, treating null as the earliest possible date. */ public static int compareWithNullAsLatest(Date d1, Date d2) { if (d1 == null && d2 == null) { return 0; } if (d1 == null) { return 1; } else if (d2 == null) { return -1; } else { return compare(d1, d2); } }
public static <Arg1, Arg2 extends Arg1> boolean nullSafeEquals(Arg1 d1, Arg2 d2) { if (d1 == null) { return d2 == null; } else if (d2 == null) { return false; } return (d1 instanceof Date && d2 instanceof Date) ? compare((Date) d1, (Date) d2) == 0 : d1.equals(d2); }
/** * Convenience method to determine if the order is activated as of the specified date * * @param checkDate - the date on which to check order. if null, will use current date * @return boolean indicating whether the order was activated before or on the check date * @since 2.0 * @should return true if an order was activated on the check date * @should return true if an order was activated before the check date * @should return false if dateActivated is null * @should return false for an order activated after the check date */ public boolean isActivated(Date checkDate) { if (dateActivated == null) { return false; } if (checkDate == null) { checkDate = new Date(); } return OpenmrsUtil.compare(dateActivated, checkDate) <= 0; }
/** * Rejects a death date if it is before birth date * * @param errors the error object * @param deathdate the date to check * @param birthdate to check with */ private void rejectDeathDateIfBeforeBirthDate(Errors errors, Date deathdate, Date birthdate) { if (OpenmrsUtil.compare(deathdate, birthdate) < 0) { errors.rejectValue("deathDate", "error.deathdate.before.birthdate"); } }
/** * Rejects a date if it is in the future. * * @param errors the error object * @param date the date to check * @param dateField the name of the field */ private void rejectIfFutureDate(Errors errors, Date date, String dateField) { if (OpenmrsUtil.compare(date, new Date()) > 0) { errors.rejectValue(dateField, "error.date.future"); } }
/** * Rejects a date if it is before 120 years ago. * * @param errors the error object * @param date the date to check * @param dateField the name of the field */ private void rejectDateIfBefore120YearsAgo(Errors errors, Date date, String dateField) { Calendar c = Calendar.getInstance(); c.setTime(new Date()); c.add(Calendar.YEAR, -120); if (OpenmrsUtil.compare(date, c.getTime()) < 0) { errors.rejectValue(dateField, "error.date.nonsensical"); } }
/** * Returns true if the associated {@link Patient} is enrolled in the associated {@link Program} * on the passed {@link Date} * * @param onDate - Date to check for PatientProgram enrollment * @return boolean - true if the associated {@link Patient} is enrolled in the associated * {@link Program} on the passed {@link Date} */ public boolean getActive(Date onDate) { if (onDate == null) { onDate = new Date(); } return !getVoided() && (getDateEnrolled() == null || OpenmrsUtil.compare(getDateEnrolled(), onDate) <= 0) && (getDateCompleted() == null || OpenmrsUtil.compare(getDateCompleted(), onDate) > 0); }
return OpenmrsUtil.compare(order1.getEffectiveStopDate(), order2.getEffectiveStartDate()) > -1; return (OpenmrsUtil.compare(order1.getEffectiveStartDate(), order2.getEffectiveStartDate()) > -1) && (OpenmrsUtil.compare(order1.getEffectiveStartDate(), order2.getEffectiveStopDate()) < 1); return (OpenmrsUtil.compare(order1.getEffectiveStartDate(), order2.getEffectiveStopDate()) < 1) && (OpenmrsUtil.compare(order1.getEffectiveStopDate(), order2.getEffectiveStartDate()) > -1);
/** * @see org.openmrs.api.CohortService#notifyPatientUnvoided(Patient, User, Date) */ @Override public void notifyPatientUnvoided(Patient patient, User originallyVoidedBy, Date originalDateVoided) throws APIException { List<CohortMembership> memberships = getCohortMemberships(patient.getPatientId(), null, true); List<CohortMembership> toUnvoid = memberships.stream().filter( m -> m.getVoided() && m.getVoidedBy().equals(originallyVoidedBy) && OpenmrsUtil.compare( truncateToSeconds(m.getDateVoided()), truncateToSeconds(originalDateVoided)) == 0) .collect(Collectors.toList()); for (CohortMembership member : toUnvoid) { member.setVoided(false); member.setDateVoided(null); member.setVoidedBy(null); member.setVoidReason(null); dao.saveCohortMembership(member); } }
&& OpenmrsUtil.compare(lastState.getStartDate(), onDate) > 0) { throw new IllegalArgumentException("You can't change out of a state before that state started");
/** * @see org.openmrs.module.htmlformentry.action.FormSubmissionControllerAction#validateSubmission(org.openmrs.module.htmlformentry.FormEntryContext, * javax.servlet.http.HttpServletRequest) */ @Override public Collection<FormSubmissionError> validateSubmission(FormEntryContext context, HttpServletRequest submission) { List<FormSubmissionError> ret = new ArrayList<FormSubmissionError>(); try { if (dateWidget != null) { Date date = dateWidget.getValue(context, submission); if (date != null) { if (OpenmrsUtil.compare(date, new Date()) > 0) { ret.add(new FormSubmissionError(dateWidget, Context.getMessageSourceService().getMessage( "htmlformentry.error.cannotBeInFuture"))); } } } } catch (Exception ex) { ret.add(new FormSubmissionError(dateWidget, ex.getMessage())); } return ret; }
"htmlformentry.error.dateWithoutValue"))); if (date != null && OpenmrsUtil.compare((Date) date, new Date()) > 0) ret.add(new FormSubmissionError(dateWidget, Context.getMessageSourceService().getMessage( "htmlformentry.error.cannotBeInFuture"))); if (value instanceof Date && !allowFutureDates && OpenmrsUtil.compare((Date) value, new Date()) > 0) { ret.add(new FormSubmissionError(valueWidget, Context.getMessageSourceService().getMessage( "htmlformentry.error.cannotBeInFuture")));
if (OpenmrsUtil.compare((Date) date, new Date()) > 0) throw new Exception("htmlformentry.error.cannotBeInFuture");
try { if (birthDateWidget.getValue(context, request) != null && OpenmrsUtil.compare((Date) birthDateWidget.getValue(context, request), new Date()) > 0) { ret.add(new FormSubmissionError(context.getFieldName(birthDateErrorWidget), Context.getMessageSourceService().getMessage( "htmlformentry.error.cannotBeInFuture")));
exitDate = dateWidget.getValue(context, submission); if (exitDate != null) { if (OpenmrsUtil.compare(exitDate, new Date()) > 0) { ret.add(new FormSubmissionError(context.getFieldName(dateErrorWidget), Context.getMessageSourceService() .getMessage("htmlformentry.error.cannotBeInFuture")));