@Override public void onApplicationEvent(ContextRefreshedEvent event) { clearCachedHandlers(); } }
@Override @Transactional(readOnly = true) public List<EncounterVisitHandler> getEncounterVisitHandlers() { return HandlerUtil.getHandlersForType(EncounterVisitHandler.class, null); }
throw new IllegalArgumentException("You must specify both a handlerType and a type"); List<H> handlers = getHandlersForType(handlerType, type); if (handlers == null || handlers.isEmpty()) { throw new APIException("handler.type.not.found", new Object[] { handlerType, type }); int order1 = getOrderOfHandler(handlers.get(0).getClass()); int order2 = getOrderOfHandler(handlers.get(1).getClass()); if (order1 == order2) { throw new APIException("handler.type.multiple", new Object[] { handlerType, type });
@Test public void getPreferredHandler_shouldReturnPatientValidatorForPatient() { Validator handler = HandlerUtil.getPreferredHandler(Validator.class, Patient.class); assertThat(handler, is(instanceOf(PatientValidator.class))); }
handlers.sort(Comparator.comparing(o -> getOrderOfHandler(o.getClass())));
@Test public void getPreferredHandler_shouldReturnPersonValidatorForPerson() { Validator handler = HandlerUtil.getPreferredHandler(Validator.class, Person.class); assertThat(handler, is(instanceOf(PersonValidator.class))); } }
/** * @see HandlerUtil#getPreferredHandler(Class, Class) */ @Test public void getPreferredHandler_shouldReturnThePreferredHandlerForThePassedHandlerAndType() { Validator v = HandlerUtil.getPreferredHandler(Validator.class, DrugOrder.class); Assert.assertEquals(DrugOrderValidator.class, v.getClass()); }
/** * Fetches all validators that are registered * * @param obj the object that will be validated * @return list of compatible validators */ protected List<Validator> getValidators(Object obj) { List<Validator> matchingValidators = new ArrayList<>(); List<Validator> validators = HandlerUtil.getHandlersForType(Validator.class, obj.getClass()); for (Validator validator : validators) { if (validator.supports(obj.getClass())) { matchingValidators.add(validator); } } return matchingValidators; }
HandlerUtil.clearCachedHandlers();
/** * @see HandlerUtil#getPreferredHandler(Class, Class) */ @Test public void getPreferredHandler_shouldThrowAAPIExceptionExceptionIfNoHandlerIsFound() { thrown.expect(APIException.class); thrown.expectMessage(Context.getMessageSourceService().getMessage("handler.type.not.found", new Object[] { Validator.class.toString(), Integer.class }, null)); HandlerUtil.getPreferredHandler(Validator.class, Integer.class); }
/** * @see HandlerUtil#getHandlerForType(Class, Class) */ @Test public void getHandlersForType_shouldReturnAListOfAllClassesThatCanHandleThePassedType() { List<Validator> l = HandlerUtil.getHandlersForType(Validator.class, Order.class); Assert.assertEquals(1, l.size()); Assert.assertEquals(OrderValidator.class, l.iterator().next().getClass()); l = HandlerUtil.getHandlersForType(Validator.class, DrugOrder.class); Assert.assertEquals(2, l.size()); }
result = HandlerUtil.getPreferredHandler(Converter.class, clazz);
/** * @see HandlerUtil#getHandlerForType(Class, Class) */ @Test public void getHandlersForType_shouldReturnAnEmptyListIfNoClassesCanHandleThePassedType() { List<Validator> l = HandlerUtil.getHandlersForType(Validator.class, PatientValidator.class); Assert.assertNotNull(l); Assert.assertEquals(0, l.size()); }
List<H> handlers = HandlerUtil.getHandlersForType(handlerType, openmrsObjectClass);