001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.keyvalues;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.commons.lang.WordUtils;
020import org.kuali.rice.core.api.util.ConcreteKeyValue;
021import org.kuali.rice.core.api.util.KeyValue;
022import org.kuali.rice.kew.api.util.CodeTranslator;
023
024import java.util.ArrayList;
025import java.util.List;
026
027/**
028 * ValuesFinder that derives values directly from a Java enum.
029 * KeyValues are provided in enum definition order, enum name
030 * is the key, capitalized lowercase enum name is the label.
031 */
032public class EnumValuesFinder extends KeyValuesBase {
033    private Class<? extends Enum> enumeration;
034
035    public EnumValuesFinder(Class<? extends Enum> enumeration) {
036        this.enumeration = enumeration;
037    }
038
039    @Override
040    public List<KeyValue> getKeyValues() {
041        List<KeyValue> labels = new ArrayList<KeyValue>();
042        for (Enum enumval: enumeration.getEnumConstants()) {
043            labels.add(new ConcreteKeyValue(getEnumKey(enumval), getEnumLabel(enumval)));
044        }
045        return labels;
046    }
047
048    /**
049     * Derives a key value from an enum
050     */
051    protected String getEnumKey(Enum enm) {
052        return enm.name();
053    }
054
055    /**
056     * Derives a label value from an enum
057     */
058    protected String getEnumLabel(Enum enm) {
059        return WordUtils.capitalize(enm.name().toLowerCase());
060    }
061}