Search in sources :

Example 1 with DataDrivenEnumeration

use of org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumeration in project BroadleafCommerce by BroadleafCommerce.

the class DataDrivenEnumVariableExpression method getEnumValues.

/**
 * Looks up a list of {@link DataDrivenEnumerationValue} by the {@link DataDrivenEnumeration#getKey()} specified by <b>key</b>
 * @param key lookup for the {@link DataDrivenEnumeration}
 * @param sort optional, either 'ASCENDING' or 'DESCENDING' depending on how you want the result list sorted
 * @return the list of {@link DataDrivenEnumerationValue} for the given <b>key</b>
 */
public List<DataDrivenEnumerationValue> getEnumValues(String key, final String sort) {
    if (StringUtils.isEmpty(key)) {
        throw new IllegalArgumentException("No 'key' parameter was passed to find enumeration values");
    }
    DataDrivenEnumeration ddEnum = enumService.findEnumByKey(key);
    if (ddEnum == null) {
        throw new IllegalArgumentException("Could not find a data driven enumeration keyed by " + key);
    }
    List<DataDrivenEnumerationValue> enumValues = new ArrayList<>(ddEnum.getEnumValues());
    if (StringUtils.isNotEmpty(sort)) {
        Collections.sort(enumValues, new Comparator<DataDrivenEnumerationValue>() {

            @Override
            public int compare(DataDrivenEnumerationValue arg0, DataDrivenEnumerationValue arg1) {
                if (sort.equals("ASCENDING")) {
                    return arg0.getDisplay().compareTo(arg1.getDisplay());
                } else {
                    return arg1.getDisplay().compareTo(arg0.getDisplay());
                }
            }
        });
    }
    return enumValues;
}
Also used : DataDrivenEnumerationValue(org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumerationValue) ArrayList(java.util.ArrayList) DataDrivenEnumeration(org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumeration)

Aggregations

ArrayList (java.util.ArrayList)1 DataDrivenEnumeration (org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumeration)1 DataDrivenEnumerationValue (org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumerationValue)1