/** * If any item in fieldValues is a Date with non-zero milliseconds, it is replaced with the Date corresponding to * the same second, with no milliseconds. * * @param fieldValues * @return whether anything was modified */ private boolean removeMillisecondsFromDateFields(Object[] fieldValues) { boolean anyChanges = false; for (int i = fieldValues.length - 1; i >= 0; --i) { Object candidate = fieldValues[i]; if (!(candidate instanceof Time) && candidate instanceof Date) { Date noMilliseconds = DateUtil.truncateToSeconds((Date) candidate); if (!noMilliseconds.equals(candidate)) { fieldValues[i] = noMilliseconds; anyChanges = true; } } } return anyChanges; }
@Test public void truncateToSeconds_shouldDropMilliseconds() { Date withMilliseconds = new Date(123L); Date withoutMilliseconds = new Date(0L); assertThat(DateUtil.truncateToSeconds(withMilliseconds), is(withoutMilliseconds)); assertThat(DateUtil.truncateToSeconds(withoutMilliseconds), is(withoutMilliseconds)); } }
/** * @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); } }
/** * @see ConceptService#saveConceptSource(ConceptSource) */ @Test public void saveConceptSource_shouldNotSetDateCreatedIfOneIsSuppliedAlready() { Date expectedDate = new Date(new Date().getTime() - 10000); ConceptSource newConceptSource = new ConceptSource(); newConceptSource.setName("name"); newConceptSource.setDescription("desc"); newConceptSource.setHl7Code("hl7Code"); newConceptSource.setDateCreated(expectedDate); Context.getConceptService().saveConceptSource(newConceptSource); Assert.assertEquals(DateUtil.truncateToSeconds(expectedDate), newConceptSource.getDateCreated()); }
@Test public void shouldAllowRetrospectiveDataEntryOfOrders() { Order order = new TestOrder(); order.setPatient(patientService.getPatient(2)); order.setCareSetting(orderService.getCareSetting(2)); order.setConcept(conceptService.getConcept(5089)); order.setEncounter(encounterService.getEncounter(6)); order.setOrderer(providerService.getProvider(1)); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_WEEK, -1); order.setDateActivated(cal.getTime()); orderService.saveOrder(order, null); cal.add(Calendar.HOUR_OF_DAY, -1); Date stopDate = cal.getTime(); Order dcOrder = orderService.discontinueOrder(order, "Testing", stopDate, order.getOrderer(), order.getEncounter()); Context.flushSession(); // ensures that order is flushed and that the drop milliseconds interceptor is called assertEquals(DateUtil.truncateToSeconds(stopDate), order.getDateStopped()); assertEquals(DateUtil.truncateToSeconds(stopDate), dcOrder.getAutoExpireDate()); }
/** * @see EncounterService#saveEncounterType(EncounterType) */ @Test public void saveEncounterType_shouldNotOverwriteDateCreated() { EncounterService encounterService = Context.getEncounterService(); // the encounter to save without a dateCreated EncounterType encounterType = new EncounterType(); encounterType.setName("testing"); encounterType.setDescription("desc"); Date date = new Date(System.currentTimeMillis() - 5000); // make sure we // have a // date that // isn't // "right now" encounterType.setDateCreated(date); encounterService.saveEncounterType(encounterType); // make sure the encounter type date created wasn't overwritten assertEquals(DateUtil.truncateToSeconds(date), encounterType.getDateCreated()); // make sure we can fetch this new encounter type // from the database and its values are the same as the passed in ones EncounterType newEncounterType = encounterService.getEncounterType(encounterType.getEncounterTypeId()); assertNotNull(newEncounterType); assertEquals(DateUtil.truncateToSeconds(date), encounterType.getDateCreated()); }
/** * @see FormService#duplicateForm(Form) */ @Test public void duplicateForm_shouldClearChangedDetailsAndUpdateCreationDetails() { Date startOfTest = DateUtil.truncateToSeconds(new Date()); FormService formService = Context.getFormService(); Form form = formService.getForm(1); Form dupForm = formService.duplicateForm(form); Assert.assertNull(dupForm.getChangedBy()); Assert.assertNull(dupForm.getDateChanged()); assertEquals(Context.getAuthenticatedUser(), dupForm.getCreator()); assertFalse(dupForm.getDateCreated().before(startOfTest)); }
assertEquals(DateUtil.truncateToSeconds(date), encounter.getDateCreated()); assertEquals(DateUtil.truncateToSeconds(date), encounter.getDateCreated());
assertEquals(DateUtil.truncateToSeconds(date), encounterType.getDateCreated()); assertEquals(4, encounterType.getCreator().getId().intValue()); assertNotSame(encounterType.getCreator(), Context.getAuthenticatedUser()); assertEquals(DateUtil.truncateToSeconds(date), encounterType.getDateCreated());
@Test public void saveOrderWithScheduledDate_shouldAddANewOrderWithScheduledDateToTheDatabase() { Date scheduledDate = new Date(); Order order = new Order(); order.setAction(Action.NEW); order.setPatient(Context.getPatientService().getPatient(7)); order.setConcept(Context.getConceptService().getConcept(5497)); order.setCareSetting(orderService.getCareSetting(1)); order.setOrderer(orderService.getOrder(1).getOrderer()); order.setEncounter(encounterService.getEncounter(3)); order.setDateActivated(new Date()); order.setScheduledDate(scheduledDate); order.setUrgency(Order.Urgency.ON_SCHEDULED_DATE); order.setEncounter(encounterService.getEncounter(3)); order.setOrderType(orderService.getOrderType(17)); order = orderService.saveOrder(order, null); Order newOrder = orderService.getOrder(order.getOrderId()); assertNotNull(order); assertEquals(DateUtil.truncateToSeconds(scheduledDate), order.getScheduledDate()); assertNotNull(newOrder); assertEquals(DateUtil.truncateToSeconds(scheduledDate), newOrder.getScheduledDate()); }
assertFalse("Make sure the dates changed slightly", origDate.equals(d2)); assertTrue("The date needs to have been set", DateUtil.truncateToSeconds(newestEnc.getEncounterDatetime()).equals(DateUtil.truncateToSeconds(d2))); assertFalse("The patient should be different", origPatient.equals(pat2)); assertTrue("The patient should have been set", newestEnc.getPatient().equals(pat2));
assertEquals(DateUtil.truncateToSeconds(date), createdObs.getDateCreated()); assertEquals(creator, createdObs.getCreator()); assertEquals(DateUtil.truncateToSeconds(date), createdOrder.getDateCreated()); assertEquals(creator, createdOrder.getCreator());
assertEquals(concept, saved.getConcept()); assertEquals(encounter, saved.getEncounter()); assertEquals(DateUtil.truncateToSeconds(datetime), saved.getObsDatetime()); assertEquals(location, saved.getLocation()); assertEquals(valueGroupId, saved.getValueGroupId()); assertEquals(DateUtil.truncateToSeconds(valueDatetime), saved.getValueDatetime()); assertEquals(valueCoded, saved.getValueCoded()); assertEquals(valueNumeric, saved.getValueNumeric());