001    /**
002     * Copyright 2005-2011 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.validation.constraint;
017    
018    import org.kuali.rice.core.api.config.property.ConfigurationService;
019    import org.kuali.rice.krad.service.KRADServiceLocator;
020    import org.kuali.rice.krad.uif.UifConstants;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    /**
026     * TODO Administrator don't forget to fill this in. 
027     * 
028     * @author Kuali Rice Team (rice.collab@kuali.org)
029     */
030    public class IntegerPatternConstraint extends ValidDataPatternConstraint{
031        protected boolean allowNegative;
032        protected boolean onlyNegative;
033        protected boolean omitZero;
034    
035        /**
036         * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersPatternConstraint#getRegexString()
037         */
038        @Override
039        protected String getRegexString() {
040            StringBuffer regex = new StringBuffer();
041    
042            if (isAllowNegative() && !onlyNegative) {
043                regex.append("((-?");
044            }
045            else if(onlyNegative){
046                regex.append("((-");
047            }
048            else {
049                regex.append("((");
050            }
051            if(omitZero){
052                regex.append("[1-9][0-9]*))");
053            }
054            else{
055                regex.append("[1-9][0-9]*)|[0]*)");
056            }
057    
058            return regex.toString();
059        }
060        
061        /**
062         * @return the allowNegative
063         */
064        public boolean isAllowNegative() {
065            return this.allowNegative;
066        }
067    
068        /**
069         * @param allowNegative the allowNegative to set
070         */
071        public void setAllowNegative(boolean allowNegative) {
072            this.allowNegative = allowNegative;
073        }
074    
075        public boolean isOnlyNegative() {
076            return onlyNegative;
077        }
078    
079        /**
080         * When set to true, only allows negative numbers (and zero if allowZero is still true)
081         * @param onlyNegative
082         */
083        public void setOnlyNegative(boolean onlyNegative) {
084            this.onlyNegative = onlyNegative;
085        }
086    
087        public boolean isOmitZero() {
088            return omitZero;
089        }
090    
091        /**
092         * When set to true, zero is not allowed in the set of allowed numbers.
093         * @param omitZero
094         */
095        public void setOmitZero(boolean omitZero) {
096            this.omitZero = omitZero;
097        }
098    
099        /**
100         * This overridden method ...
101         * 
102         * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidDataPatternConstraint#getValidationMessageParams()
103         */
104        @Override
105        public List<String> getValidationMessageParams() {
106            if (validationMessageParams == null) {
107                validationMessageParams = new ArrayList<String>();
108                ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
109                if (allowNegative && !onlyNegative) {
110                    if(omitZero){
111                        validationMessageParams.add(configService
112                                .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX
113                                        + "positiveOrNegative"));
114                    }
115                    else{
116                        validationMessageParams.add(configService
117                                .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX
118                                        + "positiveOrNegativeOrZero"));
119                    }
120                }
121                else if(onlyNegative){
122                    if(omitZero){
123                        validationMessageParams.add(configService
124                                .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "negative"));
125                    }
126                    else{
127                        validationMessageParams.add(configService
128                                .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "negativeOrZero"));
129                    }
130                }
131                else {
132                    if(omitZero){
133                        validationMessageParams.add(configService
134                                .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "positive"));
135                    }
136                    else{
137                        validationMessageParams.add(configService
138                                .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "positiveOrZero"));
139                    }
140                }
141            }
142            return validationMessageParams;
143        }
144    }