001    /**
002     * Copyright 2005-2013 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     */
016    package org.kuali.rice.krad.datadictionary.state;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.core.api.config.property.ConfigurationService;
020    import org.kuali.rice.krad.datadictionary.parse.BeanTag;
021    import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
022    import org.kuali.rice.krad.datadictionary.parse.BeanTags;
023    import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
024    import org.kuali.rice.krad.service.KRADServiceLocator;
025    import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    import java.util.Map;
030    
031    /**
032     * Base implementation of StateMapping.
033     *
034     * @author Kuali Rice Team (rice.collab@kuali.org)
035     * @see StateMapping
036     * @since 2.2
037     */
038    @BeanTags({@BeanTag(name = "stateMapping-bean", parent = "StateMapping"),
039            @BeanTag(name = "workflowStateMapping-bean", parent = "workflowStateMapping")})
040    public class StateMappingBase implements StateMapping {
041    
042        private Map<String, String> stateNameMessageKeyMap;
043        private List<String> states;
044        private String statePropertyName;
045        private Map<String, String> customClientSideValidationStates;
046    
047        /**
048         * @see StateMapping#getStateNameMessage(String)
049         */
050        @Override
051        public String getStateNameMessage(String state) {
052            String message = null;
053            if (StringUtils.isNotBlank(state) && this.getStates().contains(state)) {
054                if (this.getStateNameMessageKeyMap() != null) {
055                    ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
056                    String key = this.getStateNameMessageKeyMap().get(state);
057                    message = configService.getPropertyValueAsString(key);
058                }
059    
060                if (message == null) {
061                    message = state;
062                }
063            }
064            return message;
065        }
066    
067        /**
068         * @see StateMapping#getCurrentState(Object object)
069         */
070        @Override
071        public String getCurrentState(Object object) {
072            return ObjectPropertyUtils.getPropertyValue(object, this.getStatePropertyName());
073        }
074    
075        /**
076         * @see StateMapping#getNextState(Object)
077         */
078        @Override
079        public String getNextState(Object object) {
080            int currentStateIndex = this.getStates().indexOf(this.getCurrentState(object));
081            if (currentStateIndex != -1) {
082                int index = currentStateIndex + 1;
083                if (index == this.getStates().size()) {
084                    return this.getCurrentState(object);
085                } else {
086                    return this.getStates().get(index);
087                }
088            } else {
089                return this.getCurrentState(object);
090            }
091        }
092    
093        /**
094         * @see org.kuali.rice.krad.datadictionary.state.StateMapping#getStateNameMessageKeyMap()
095         */
096        @Override
097        @BeanTagAttribute(name = "stateNameMessageKeyMap", type = BeanTagAttribute.AttributeType.MAPVALUE)
098        public Map<String, String> getStateNameMessageKeyMap() {
099            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    }