View Javadoc
1   /**
2    * Copyright 2005-2016 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.krad.datadictionary.state;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreApiServiceLocator;
20  import org.kuali.rice.core.api.config.property.ConfigurationService;
21  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
22  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
23  import org.kuali.rice.krad.datadictionary.parse.BeanTags;
24  import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
25  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Map;
30  
31  /**
32   * Base implementation of StateMapping.
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   * @see StateMapping
36   * @since 2.2
37   */
38  @BeanTags({@BeanTag(name = "stateMapping", parent = "StateMapping"),
39          @BeanTag(name = "workflowStateMapping", parent = "workflowStateMapping")})
40  public class StateMappingBase implements StateMapping {
41  
42      private Map<String, String> stateNameMessageKeyMap;
43      private List<String> states;
44      private String statePropertyName;
45      private Map<String, String> customClientSideValidationStates;
46  
47      /**
48       * @see StateMapping#getStateNameMessage(String)
49       */
50      @Override
51      public String getStateNameMessage(String state) {
52          String message = null;
53          if (StringUtils.isNotBlank(state) && this.getStates().contains(state)) {
54              if (this.getStateNameMessageKeyMap() != null) {
55                  ConfigurationService configService = CoreApiServiceLocator.getKualiConfigurationService();
56                  String key = this.getStateNameMessageKeyMap().get(state);
57                  message = configService.getPropertyValueAsString(key);
58              }
59  
60              if (message == null) {
61                  message = state;
62              }
63          }
64          return message;
65      }
66  
67      /**
68       * @see StateMapping#getCurrentState(Object object)
69       */
70      @Override
71      public String getCurrentState(Object object) {
72          return ObjectPropertyUtils.getPropertyValue(object, this.getStatePropertyName());
73      }
74  
75      /**
76       * @see StateMapping#getNextState(Object)
77       */
78      @Override
79      public String getNextState(Object object) {
80          int currentStateIndex = this.getStates().indexOf(this.getCurrentState(object));
81          if (currentStateIndex != -1) {
82              int index = currentStateIndex + 1;
83              if (index == this.getStates().size()) {
84                  return this.getCurrentState(object);
85              } else {
86                  return this.getStates().get(index);
87              }
88          } else {
89              return this.getCurrentState(object);
90          }
91      }
92  
93      /**
94       * @see org.kuali.rice.krad.datadictionary.state.StateMapping#getStateNameMessageKeyMap()
95       */
96      @Override
97      @BeanTagAttribute(name = "stateNameMessageKeyMap", type = BeanTagAttribute.AttributeType.MAPVALUE)
98      public Map<String, String> getStateNameMessageKeyMap() {
99          return stateNameMessageKeyMap;
100     }
101 
102     /**
103      * @see StateMapping#setStateNameMessageKeyMap(java.util.Map)
104      */
105     @Override
106     public void setStateNameMessageKeyMap(Map<String, String> stateNameMessageKeyMap) {
107         this.stateNameMessageKeyMap = stateNameMessageKeyMap;
108     }
109 
110     /**
111      * @see org.kuali.rice.krad.datadictionary.state.StateMapping#getStates()
112      */
113     @Override
114     @BeanTagAttribute(name = "states", type = BeanTagAttribute.AttributeType.LISTVALUE)
115     public List<String> getStates() {
116         if (states == null) {
117             states = new ArrayList<String>();
118         }
119         return states;
120     }
121 
122     /**
123      * @see StateMapping#setStates(java.util.List)
124      */
125     @Override
126     public void setStates(List<String> states) {
127         this.states = states;
128     }
129 
130     /**
131      * @see org.kuali.rice.krad.datadictionary.state.StateMapping#getStatePropertyName()
132      */
133     @Override
134     @BeanTagAttribute(name = "statePropertyName")
135     public String getStatePropertyName() {
136         return statePropertyName;
137     }
138 
139     /**
140      * @see StateMapping#setStatePropertyName(String)
141      */
142     @Override
143     public void setStatePropertyName(String statePropertyName) {
144         this.statePropertyName = statePropertyName;
145     }
146 
147     /**
148      * @see org.kuali.rice.krad.datadictionary.state.StateMapping#getCustomClientSideValidationStates()
149      */
150     @BeanTagAttribute(name = "customClientSideValidationStates", type = BeanTagAttribute.AttributeType.MAPVALUE)
151     public Map<String, String> getCustomClientSideValidationStates() {
152         return customClientSideValidationStates;
153     }
154 
155     /**
156      * @see StateMapping#setCustomClientSideValidationStates(java.util.Map)
157      */
158     public void setCustomClientSideValidationStates(Map<String, String> customClientSideValidationStates) {
159         this.customClientSideValidationStates = customClientSideValidationStates;
160     }
161 
162     /**
163      * @see StateMapping#completeValidation(org.kuali.rice.krad.datadictionary.validator.ValidationTrace)
164      */
165     public void completeValidation(ValidationTrace tracer) {
166         tracer.addBean("StateMappingBase", getStatePropertyName());
167 
168         // Checking that propertyName is set
169         if (getStatePropertyName() == null) {
170             String currentValues[] = {"statePropertyName = null"};
171             tracer.createWarning("The State Property Name must be set", currentValues);
172         }
173 
174         // Checking that states are set
175         if (getStates() == null) {
176             String currentValues[] = {"states = null"};
177             tracer.createWarning("States should be set", currentValues);
178         }
179     }
180 }