View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krms.impl.validation;
17  
18  import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
19  import org.kuali.rice.krms.framework.engine.Action;
20  import org.kuali.rice.krms.framework.type.ValidationActionType;
21  import org.kuali.rice.krms.framework.type.ValidationActionTypeService;
22  
23  /**
24   * An {@link Action} that when executed appends its type and message to the results
25   *
26   * @author Kuali Rice Team (rice.collab@kuali.org)
27   */
28  public class ValidationAction implements Action {
29  
30  
31      private final ValidationActionType type;
32      private final String message;
33  
34      /**
35       * create a Validation action of the given type with the given message
36       * @param type {@link ValidationActionType}
37       * @param message for when action executes
38       */
39      public ValidationAction(ValidationActionType type, String message) {
40          if (type == null) throw new IllegalArgumentException("type must not be null");
41  
42          this.type = type;
43          this.message = message;
44      }
45  
46      @Override
47      public void execute(ExecutionEnvironment environment) {
48          // create or extend an existing attribute on the EngineResults to communicate the selected Validation and
49          // action
50  
51          Object value = environment.getEngineResults().getAttribute(ValidationActionTypeService.VALIDATIONS_ACTION_ATTRIBUTE);
52          StringBuilder selectedAttributesStringBuilder = new StringBuilder();
53  
54          if (value != null) {
55              // assume the value is what we think it is
56              selectedAttributesStringBuilder.append(value.toString());
57              // we need a comma after the initial value
58              selectedAttributesStringBuilder.append(",");
59          }
60  
61          // add our people flow action to the string using our convention
62          selectedAttributesStringBuilder.append(type.getCode());
63          selectedAttributesStringBuilder.append(":");
64          selectedAttributesStringBuilder.append(message);
65  
66          // set our attribute on the engine results
67          environment.getEngineResults().setAttribute(ValidationActionTypeService.VALIDATIONS_ACTION_ATTRIBUTE,
68                  selectedAttributesStringBuilder.toString()
69          );
70      }
71  
72      @Override
73      public void executeSimulation(ExecutionEnvironment environment) {
74          // our action doesn't need special handling during simulations
75          execute(environment);
76      }
77  }