Search in sources :

Example 1 with CustomDatatype

use of org.openmrs.customdatatype.CustomDatatype in project openmrs-core by openmrs.

the class DatatypeServiceTest method getHandler_shouldReturnAHandlerForTheSpecifiedDatatype.

/**
 * @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());
}
Also used : CustomDatatype(org.openmrs.customdatatype.CustomDatatype) DateDatatype(org.openmrs.customdatatype.datatype.DateDatatype) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 2 with CustomDatatype

use of org.openmrs.customdatatype.CustomDatatype in project openmrs-core by openmrs.

the class ModuleFileParser method createGlobalPropertyWithDatatype.

private GlobalProperty createGlobalPropertyWithDatatype(String property, String defaultValue, String description, String datatypeClassname, String datatypeConfig) {
    GlobalProperty globalProperty = null;
    try {
        Class<CustomDatatype<?>> datatypeClazz = (Class<CustomDatatype<?>>) Class.forName(datatypeClassname).asSubclass(CustomDatatype.class);
        globalProperty = new GlobalProperty(property, defaultValue, description, datatypeClazz, datatypeConfig);
    } catch (ClassCastException ex) {
        log.error("The class specified by 'datatypeClassname' (" + datatypeClassname + ") must be a subtype of 'org.openmrs.customdatatype.CustomDatatype<?>'.", ex);
    } catch (ClassNotFoundException ex) {
        log.error("The class specified by 'datatypeClassname' (" + datatypeClassname + ") could not be found.", ex);
    }
    return globalProperty;
}
Also used : CustomDatatype(org.openmrs.customdatatype.CustomDatatype) GlobalProperty(org.openmrs.GlobalProperty)

Example 3 with CustomDatatype

use of org.openmrs.customdatatype.CustomDatatype in project openmrs-core by openmrs.

the class DatatypeServiceImpl method datatypeClassHandled.

/**
 * @param t
 * @return the generic type of t or an interface it implements that is a CustomDatatype
 */
private Class datatypeClassHandled(Type t) {
    if (t instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) t;
        Type first = pt.getActualTypeArguments()[0];
        if (first instanceof Class && CustomDatatype.class.isAssignableFrom((Class) first)) {
            return (Class) first;
        } else {
            return datatypeClassHandled(pt.getRawType());
        }
    } else if (t instanceof Class) {
        Type genericSuperclass = ((Class) t).getGenericSuperclass();
        if (genericSuperclass != null) {
            Class ret = datatypeClassHandled(genericSuperclass);
            if (ret != null) {
                return ret;
            }
        }
        for (Type candidate : ((Class) t).getGenericInterfaces()) {
            Class ret = datatypeClassHandled(candidate);
            if (ret != null) {
                return ret;
            }
        }
    }
    return null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) CustomDatatype(org.openmrs.customdatatype.CustomDatatype)

Example 4 with CustomDatatype

use of org.openmrs.customdatatype.CustomDatatype in project openmrs-core by openmrs.

the class DatatypeServiceTest method getHandler_shouldReturnAHandlerForADatatypeThatExtendsAGenericSuperclass.

/**
 * @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());
}
Also used : CustomDatatype(org.openmrs.customdatatype.CustomDatatype) LocationDatatype(org.openmrs.customdatatype.datatype.LocationDatatype) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

CustomDatatype (org.openmrs.customdatatype.CustomDatatype)4 Test (org.junit.Test)2 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 GlobalProperty (org.openmrs.GlobalProperty)1 DateDatatype (org.openmrs.customdatatype.datatype.DateDatatype)1 LocationDatatype (org.openmrs.customdatatype.datatype.LocationDatatype)1