Search in sources :

Example 1 with EnumInfo

use of com.haulmont.restapi.data.EnumInfo in project cuba by cuba-platform.

the class EnumsControllerManager method getAllEnumInfos.

public List<EnumInfo> getAllEnumInfos() {
    List<EnumInfo> results = new ArrayList<>();
    metadataTools.getAllEnums().stream().filter(enumClass -> EnumClass.class.isAssignableFrom(enumClass) && enumClass.isEnum()).forEach(enumClass -> {
        List<EnumValueInfo> enumValues = new ArrayList<>();
        Object[] enumConstants = enumClass.getEnumConstants();
        for (Object enumConstant : enumConstants) {
            Enum enumValue = (Enum) enumConstant;
            EnumValueInfo enumValueInfo = new EnumValueInfo(enumValue.name(), ((EnumClass) enumValue).getId(), messages.getMessage(enumValue));
            enumValues.add(enumValueInfo);
        }
        results.add(new EnumInfo(enumClass.getName(), enumValues));
    });
    return results;
}
Also used : Inject(javax.inject.Inject) HttpStatus(org.springframework.http.HttpStatus) Component(org.springframework.stereotype.Component) List(java.util.List) MetadataTools(com.haulmont.cuba.core.global.MetadataTools) RestAPIException(com.haulmont.restapi.exception.RestAPIException) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass) Messages(com.haulmont.cuba.core.global.Messages) EnumInfo(com.haulmont.restapi.data.EnumInfo) EnumValueInfo(com.haulmont.restapi.data.EnumValueInfo) ArrayList(java.util.ArrayList) EnumValueInfo(com.haulmont.restapi.data.EnumValueInfo) EnumInfo(com.haulmont.restapi.data.EnumInfo) ArrayList(java.util.ArrayList) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass)

Example 2 with EnumInfo

use of com.haulmont.restapi.data.EnumInfo in project cuba by cuba-platform.

the class EnumsControllerManager method getEnumInfo.

public EnumInfo getEnumInfo(String enumClassName) {
    Class<?> enumClass;
    try {
        enumClass = Class.forName(enumClassName);
    } catch (ClassNotFoundException e) {
        throw new RestAPIException("Enum not found", "Enum with class name " + enumClassName + " not found", HttpStatus.NOT_FOUND);
    }
    List<EnumValueInfo> enumValues = new ArrayList<>();
    Object[] enumConstants = enumClass.getEnumConstants();
    for (Object enumConstant : enumConstants) {
        Enum enumValue = (Enum) enumConstant;
        EnumValueInfo enumValueInfo = new EnumValueInfo(enumValue.name(), ((EnumClass) enumValue).getId(), messages.getMessage(enumValue));
        enumValues.add(enumValueInfo);
    }
    return new EnumInfo(enumClass.getName(), enumValues);
}
Also used : EnumValueInfo(com.haulmont.restapi.data.EnumValueInfo) EnumInfo(com.haulmont.restapi.data.EnumInfo) ArrayList(java.util.ArrayList) RestAPIException(com.haulmont.restapi.exception.RestAPIException)

Aggregations

EnumInfo (com.haulmont.restapi.data.EnumInfo)2 EnumValueInfo (com.haulmont.restapi.data.EnumValueInfo)2 RestAPIException (com.haulmont.restapi.exception.RestAPIException)2 ArrayList (java.util.ArrayList)2 EnumClass (com.haulmont.chile.core.datatypes.impl.EnumClass)1 Messages (com.haulmont.cuba.core.global.Messages)1 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)1 List (java.util.List)1 Inject (javax.inject.Inject)1 HttpStatus (org.springframework.http.HttpStatus)1 Component (org.springframework.stereotype.Component)1