/** * @return fully-qualified classnames of all registered datatypes */ public static List<String> getDatatypeClassnames() { List<String> ret = new ArrayList<>(); for (Class<?> c : Context.getDatatypeService().getAllDatatypeClasses()) { ret.add(c.getName()); } return ret; }
/** * @return full-qualified classnames of all registered handlers */ public static List<String> getHandlerClassnames() { List<String> ret = new ArrayList<>(); for (Class<?> c : Context.getDatatypeService().getAllHandlerClasses()) { ret.add(c.getName()); } return ret; }
/** * @param handler * @param datatype * @return whether or not handler is compatible with datatype */ public static boolean isCompatibleHandler(CustomDatatypeHandler handler, CustomDatatype<?> datatype) { List<Class<? extends CustomDatatypeHandler>> handlerClasses = Context.getDatatypeService().getHandlerClasses( (Class<? extends CustomDatatype<?>>) datatype.getClass()); return handlerClasses.contains(handler.getClass()); }
/** * @see org.openmrs.customdatatype.CustomDatatype#fromReferenceString(java.lang.String) */ @Override public String fromReferenceString(String referenceString) throws InvalidCustomValueException { return Context.getDatatypeService().getClobDatatypeStorageByUuid(referenceString).getValue(); }
/** * @see org.openmrs.customdatatype.CustomDatatype#save(java.lang.Object, java.lang.String) */ @Override public String save(String typedValue, String existingValueReference) throws InvalidCustomValueException { // get existing object or create a new one ClobDatatypeStorage storage = existingValueReference != null ? Context.getDatatypeService() .getClobDatatypeStorageByUuid(existingValueReference) : new ClobDatatypeStorage(); storage.setValue(typedValue); storage = Context.getDatatypeService().saveClobDatatypeStorage(storage); return storage.getUuid(); }
/** * @param dt the datatype that this handler should be for * @param preferredHandlerClassname * @param handlerConfig * @return a configured datatype handler with the given classname and configuration */ public static CustomDatatypeHandler getHandler(CustomDatatype<?> dt, String preferredHandlerClassname, String handlerConfig) { if (preferredHandlerClassname != null) { try { Class<? extends CustomDatatypeHandler> clazz = (Class<? extends CustomDatatypeHandler>) Context .loadClass(preferredHandlerClassname); CustomDatatypeHandler handler = clazz.newInstance(); if (handlerConfig != null) { handler.setHandlerConfiguration(handlerConfig); } return handler; } catch (Exception ex) { log.warn("Failed to instantiate and configure preferred handler with class " + preferredHandlerClassname + " and config " + handlerConfig, ex); } } // if we couldn't get the preferred handler (or none was specified) we get the default one by datatype return Context.getDatatypeService().getHandler(dt, handlerConfig); }
/** * @param datatypeClassname * @param datatypeConfig * @return a configured datatype with the given classname and configuration */ public static CustomDatatype<?> getDatatype(String datatypeClassname, String datatypeConfig) { try { Class dtClass = Context.loadClass(datatypeClassname); CustomDatatype<?> ret = (CustomDatatype<?>) Context.getDatatypeService().getDatatype(dtClass, datatypeConfig); if (ret == null) { throw new CustomDatatypeException("Can't find datatype: " + datatypeClassname); } return ret; } catch (Exception ex) { throw new CustomDatatypeException("Error loading " + datatypeClassname + " and configuring it with " + datatypeConfig, ex); } }
/** * @see DatatypeService#getHandler(CustomDatatype,String) */ @Test public void getHandler_shouldReturnAHandlerForADatatypeThatExtendsAGenericSuperclass() { DatatypeService service = Context.getDatatypeService(); CustomDatatype locationDatatype = CustomDatatypeUtil.getDatatype(LocationDatatype.class.getName(), null); Assert.assertEquals(LocationDatatypeHandler.class, service.getHandler(locationDatatype, null).getClass()); } }
/** * @see DatatypeService#getHandler(CustomDatatype,String) */ @Test public void getHandler_shouldReturnAHandlerForTheSpecifiedDatatype() { DatatypeService service = Context.getDatatypeService(); CustomDatatype dateDatatype = CustomDatatypeUtil.getDatatype(DateDatatype.class.getName(), null); Assert.assertEquals(DateDatatypeHandler.class, service.getHandler(dateDatatype, null).getClass()); }
private List<CustomDatatypeRepresentation> getAllCustomDatatypes() { List<String> datatypeClassnames = CustomDatatypeUtil.getDatatypeClassnames(); List<CustomDatatypeRepresentation> datatypes = new ArrayList<CustomDatatypeRepresentation>(); for (String datatypeClassname : datatypeClassnames) { CustomDatatypeRepresentation datatype = new CustomDatatypeRepresentation(); datatypes.add(datatype); datatype.setDatatypeClassname(datatypeClassname); Class<CustomDatatype<?>> datatypeClass; try { datatypeClass = (Class<CustomDatatype<?>>) Context.loadClass(datatypeClassname); } catch (ClassNotFoundException e) { throw new ConversionException("Failed to load datatype class", e); } List<Class<? extends CustomDatatypeHandler>> handlerClasses = Context.getDatatypeService().getHandlerClasses( datatypeClass); for (Class<? extends CustomDatatypeHandler> handlerClass : handlerClasses) { datatype.getHandlers().add(new CustomDatatypeHandlerRepresentation(datatype, handlerClass.getName())); } } return datatypes; }
@Before public void before() throws Exception { datatypeService = Context.getDatatypeService(); executeDataSet(RestTestConstants1_9.FORM_RESOURCE_DATA_SET); }
@Before public void before() throws Exception { formService = Context.getFormService(); datatypeService = Context.getDatatypeService(); executeDataSet(RestTestConstants1_9.TEST_DATASET); executeDataSet(RestTestConstants1_9.FORM_RESOURCE_DATA_SET); }