/** * @see org.openmrs.customdatatype.SerializingCustomDatatype#doGetTextSummary(java.lang.Object) */ @Override public CustomDatatype.Summary doGetTextSummary(Date typedValue) { return new CustomDatatype.Summary(Context.getDateFormat().format(typedValue), true); }
/** * @see java.beans.PropertyEditorSupport#getAsText() * @should print date without time * @should print date and time with time */ @Override public String getAsText() { Date date = (Date) getValue(); if (date == null) { return ""; } if ("0000".equals(new SimpleDateFormat("HmsS").format(date))) { return Context.getDateFormat().format(date); } else { return Context.getDateTimeFormat().format(date); } }
/** * @see org.openmrs.Attributable#hydrate(java.lang.String) */ @Override public AttributableDate hydrate(String s) { // don't do anything to empty dates if (StringUtils.isEmpty(s)) { return null; } try { // try to parse as the current user ( return new AttributableDate(((Date) Context.getDateFormat().parseObject(s)).getTime()); } catch (ParseException e) { try { return new AttributableDate(((Date) new SimpleDateFormat(dateFormat).parseObject(s)).getTime()); } catch (ParseException e2) { // if we can't parse it as the normalized string or as the current // user's date format, bail out throw new APIException("unable.parse.string.as.date", new Object[] { s }); } } }
/** * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String) * @should handle date * @should handle date and time * @should fail on partial date * @should fail on partial date and time */ @Override public void setAsText(String asString) throws IllegalArgumentException { if (StringUtils.isEmpty(asString)) { setValue(null); return; } try { // first try date+time setValue(Context.getDateTimeFormat().parse(asString)); } catch (ParseException dateTimeEx) { // next try just date try { setValue(Context.getDateFormat().parse(asString)); } catch (ParseException dateEx) { // those were the only two options, so we fail throw new IllegalArgumentException(dateTimeEx); } } }
return (valueCoded == null ? "" : valueCoded.getName(Context.getLocale()).getName()); case DATETIME: return (valueDatetime == null ? "" : Context.getDateFormat().format(valueDatetime)); case NUMERIC: return (valueNumeric == null ? "" : String.valueOf(valueNumeric));
return Context.getDateFormat().parse(valueText);
/** * @throws ParseException * @see RelationshipValidator#validate(Object,Errors) */ @Test public void validate_shouldFailIfEndDateIsBeforeStartDate() throws ParseException { Relationship relationship = new Relationship(1); relationship.setStartDate(Context.getDateFormat().parse("18/02/2012")); relationship.setEndDate(Context.getDateFormat().parse("18/02/2001")); Map<String, String> map = new HashMap<>(); MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName()); new RelationshipValidator().validate(relationship, errors); Assert.assertEquals(true, errors.hasErrors()); }
/** * @throws ParseException * @see RelationshipValidator#validate(Object,Errors) */ @Test public void validate_shouldPassIfEndDateIsAfterStartDate() throws ParseException { Relationship relationship = new Relationship(1); relationship.setStartDate(Context.getDateFormat().parse("18/02/2012")); relationship.setEndDate(Context.getDateFormat().parse("18/03/2012")); Map<String, String> map = new HashMap<>(); MapBindingResult errors = new MapBindingResult(map, Relationship.class.getName()); new RelationshipValidator().validate(relationship, errors); Assert.assertFalse(errors.hasErrors()); }
CustomDateEditor ed = new CustomDateEditor(Context.getDateFormat(), true, 10); ed.setAsText(string); return ed.getValue();
@Test(expected = LogicException.class) public void toObject_shouldFailWhenContainsMultipleResults() throws ParseException { Result parentResult = new Result(); Result firstResult = new Result(Context.getDateFormat().parse("12/08/2008"), "some value", new Encounter(123)); Result secondResult = new Result(Context.getDateFormat().parse("15/08/2008"), "some other value", new Encounter(124)); parentResult.add(firstResult); parentResult.add(secondResult); Object toObject = parentResult.toObject(); Assert.assertNull(toObject); } }
@Test public void latest_shouldGetTheMostRecentResultGivenMultipleResults() throws ParseException { Result parentResult = new Result(); Result firstResult = new Result(Context.getDateFormat().parse("12/08/2008"), "some other value", new Encounter(124)); Result secondResult = new Result(Context.getDateFormat().parse("15/08/2008"), "some value", new Encounter(123)); parentResult.add(firstResult); parentResult.add(secondResult); Assert.assertEquals("some value", parentResult.latest().toString()); }
@Test public void earliest_shouldGetTheFirstResultGivenMultipleResults() throws ParseException { Result parentResult = new Result(); Result secondResult = new Result(Context.getDateFormat().parse("15/08/2008"), "some other value", new Encounter(124)); Result firstResult = new Result(Context.getDateFormat().parse("12/08/2008"), "some value", new Encounter(123)); parentResult.add(firstResult); parentResult.add(secondResult); Assert.assertEquals("some value", parentResult.earliest().toString()); }
@Test public void earliest_shouldGetTheResultGivenASingleResult() throws ParseException { Result parentResult = new Result(); Result secondResult = new Result(Context.getDateFormat().parse("15/08/2008"), "some other value", new Encounter(124)); Result firstResult = new Result(Context.getDateFormat().parse("12/08/2008"), "some value", new Encounter(123)); parentResult.add(firstResult); parentResult.add(secondResult); Assert.assertEquals("some value", parentResult.earliest().toString()); }
@Test public void latest_shouldGetTheResultGivenASingleResult() throws ParseException { Result result = new Result(Context.getDateFormat().parse("12/08/2008"), "some other value", new Encounter(124)); Assert.assertEquals("some other value", result.latest().toString()); }
@Test public void earliest_shouldNotGetTheResultWithNullResultDateGivenOtherResults() throws ParseException { Result parentResult = new Result(); Result secondResult = new Result(null, "some value", new Encounter(123)); Result firstResult = new Result(Context.getDateFormat().parse("12/08/2008"), "some other value", new Encounter(124)); parentResult.add(firstResult); parentResult.add(secondResult); Assert.assertEquals("some other value", parentResult.earliest().toString()); }
@Test public void latest_shouldGetTheResultWithNullResultDate() throws ParseException { Result parentResult = new Result(); Result firstResult = new Result(Context.getDateFormat().parse("15/08/2008"), "some value", new Encounter(123)); Result secondResult = new Result(null, "some other value", new Encounter(124)); parentResult.add(firstResult); parentResult.add(secondResult); Assert.assertEquals("some value", parentResult.latest().toString()); }
@Test public void get_shouldGetEmptyResultForIndexesOutOfRange() throws ParseException { Result parentResult = new Result(); Result secondResult = new Result(null, "some value", new Encounter(123)); Result firstResult = new Result(Context.getDateFormat().parse("12/08/2008"), "some other value", new Encounter(124)); parentResult.add(firstResult); parentResult.add(secondResult); // 3 is greater than the number of entries in the parentResult Assert.assertEquals(new EmptyResult(), parentResult.get(3)); }
assertEquals(12, orders.size()); Date asOfDate = Context.getDateFormat().parse("10/12/2007"); orders = orderService.getActiveOrders(patient, null, null, asOfDate); assertEquals(2, orders.size()); asOfDate = Context.getDateFormat().parse("10/04/2008"); orders = orderService.getActiveOrders(patient, null, null, asOfDate); assertEquals(3, orders.size()); assertThat(orders, hasItems(expectedOrders4)); asOfDate = Context.getDateFormat().parse("04/12/2008"); orders = orderService.getActiveOrders(patient, null, null, asOfDate); assertEquals(5, orders.size()); assertThat(orders, hasItems(expectedOrders5)); asOfDate = Context.getDateFormat().parse("06/12/2008"); orders = orderService.getActiveOrders(patient, null, null, asOfDate); assertEquals(5, orders.size());
private SimpleDateFormat dateFormat() { String df = dateFormat != null ? dateFormat : Context.getAdministrationService().getGlobalProperty(HtmlFormEntryConstants.GP_DATE_FORMAT); if (StringUtils.hasText(df)) { return new SimpleDateFormat(df, Context.getLocale()); } else { return Context.getDateFormat(); } } public String getYearsRange()
/** * @param encounter * @return encounter type and date */ @PropertyGetter("display") public String getDisplayString(Encounter encounter) { String ret = encounter.getEncounterType() == null ? "?" : encounter.getEncounterType().getName(); ret += " "; ret += encounter.getEncounterDatetime() == null ? "?" : Context.getDateFormat().format( encounter.getEncounterDatetime()); return ret; }