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.impl.validation;
017
018import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
019import org.kuali.rice.krms.framework.engine.Action;
020import org.kuali.rice.krms.framework.type.ValidationActionType;
021import org.kuali.rice.krms.framework.type.ValidationActionTypeService;
022
023/**
024 * An {@link Action} that when executed appends its type and message to the results
025 *
026 * @author Kuali Rice Team (rice.collab@kuali.org)
027 */
028public class ValidationAction implements Action {
029
030
031    private final ValidationActionType type;
032    private final String message;
033
034    /**
035     * create a Validation action of the given type with the given message
036     * @param type {@link ValidationActionType}
037     * @param message for when action executes
038     */
039    public ValidationAction(ValidationActionType type, String message) {
040        if (type == null) throw new IllegalArgumentException("type must not be null");
041
042        this.type = type;
043        this.message = message;
044    }
045
046    @Override
047    public void execute(ExecutionEnvironment environment) {
048        // create or extend an existing attribute on the EngineResults to communicate the selected Validation and
049        // action
050
051        Object value = environment.getEngineResults().getAttribute(ValidationActionTypeService.VALIDATIONS_ACTION_ATTRIBUTE);
052        StringBuilder selectedAttributesStringBuilder = new StringBuilder();
053
054        if (value != null) {
055            // assume the value is what we think it is
056            selectedAttributesStringBuilder.append(value.toString());
057            // we need a comma after the initial value
058            selectedAttributesStringBuilder.append(",");
059        }
060
061        // add our people flow action to the string using our convention
062        selectedAttributesStringBuilder.append(type.getCode());
063        selectedAttributesStringBuilder.append(":");
064        selectedAttributesStringBuilder.append(message);
065
066        // set our attribute on the engine results
067        environment.getEngineResults().setAttribute(ValidationActionTypeService.VALIDATIONS_ACTION_ATTRIBUTE,
068                selectedAttributesStringBuilder.toString()
069        );
070    }
071
072    @Override
073    public void executeSimulation(ExecutionEnvironment environment) {
074        // our action doesn't need special handling during simulations
075        execute(environment);
076    }
077}