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.krms.framework.type;
017
018import org.kuali.rice.core.api.mo.common.Coded;
019
020import java.util.HashSet;
021import java.util.Set;
022
023/**
024 * enum used to specify the action type to be specified in the vended {@link ValidationAction}s. WARNING ERROR
025 *
026 * @author Kuali Rice Team (rice.collab@kuali.org)
027 */
028public enum ValidationActionType implements Coded {
029
030    /**
031     * use this flag with the static factory to get a {@link ValidationActionTypeService} that creates
032     * warning actions.
033     */
034    WARNING("W"),
035
036    /**
037     * use this flag with the static factory to get a {@link ValidationActionTypeService} that creates
038     * error actions.
039     */
040    ERROR("E");
041
042    private final String code;
043
044    /**
045     * Create a ValidationActionType of the given typeCode
046     * @param typeCode - typeCode created ValidationActionType should be of.
047     */
048    private ValidationActionType(String typeCode) {
049        this.code = typeCode;
050    }
051
052    @Override
053    public String toString() {
054        return this.name().toLowerCase();
055    }
056
057    /**
058     * for each type, check the input with the lowercase version of the type name, and returns any match.
059     * @param s the type to retrieve
060     * @return the type, or null if a match is not found.
061     */
062    public static ValidationActionType fromString(String s) {
063        for (ValidationActionType type : ValidationActionType.values()) {
064            if (type.toString().equals(s.toLowerCase())) {
065                return type;
066            }
067        }
068        return null;
069    }
070
071    /**
072     * for each type, check the input with the uppercase type code, and returns any match.
073     * @param code the type to retrieve
074     * @return the type, or null if a match is not found.
075     */
076    public static ValidationActionType fromCode(String code) {
077        if (code == null) {
078            return null;
079        }
080        for (ValidationActionType type : values()) {
081            if (type.code.equals(code)) {
082                return type;
083            }
084        }
085        throw new IllegalArgumentException("Failed to locate the ValidationActionType with the given code: " + code);
086    }
087
088    /**
089     * Set of valid type codes
090     */
091    public static final Set<String> VALID_TYPE_CODES = new HashSet<String>();
092    static {
093        for (ValidationActionType type : values()) {
094            VALID_TYPE_CODES.add(type.getCode());
095        }
096    }
097
098    /**
099     * The code value for this object.  In general a code value cannot be null or a blank string.
100     *
101     * @return the code value for this object.
102     */
103    @Override
104    public String getCode() {
105        return code;
106    }
107}