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.krad.datadictionary.validation.constraint;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.kuali.rice.core.api.config.property.ConfigurationService;
022import org.kuali.rice.krad.service.KRADServiceLocator;
023import org.kuali.rice.krad.uif.UifConstants;
024
025/**
026 * Validation pattern for matching floating point numbers, optionally matching negative numbers
027 * 
028 * @author Kuali Rice Team (rice.collab@kuali.org)
029 */
030public class FloatingPointPatternConstraint extends ConfigurationBasedRegexPatternConstraint {
031
032    protected boolean allowNegative;
033
034    /**
035     * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersPatternConstraint#getRegexString()
036     */
037    @Override
038    protected String getRegexString() {
039        StringBuffer regex = new StringBuffer();
040
041        if (isAllowNegative()) {
042            regex.append("-?");
043        }
044        regex.append(super.getRegexString());
045
046        return regex.toString();
047    }
048
049    /**
050     * @return the allowNegative
051     */
052    public boolean isAllowNegative() {
053        return this.allowNegative;
054    }
055
056    /**
057     * @param allowNegative the allowNegative to set
058     */
059    public void setAllowNegative(boolean allowNegative) {
060        this.allowNegative = allowNegative;
061    }
062
063    /**
064     * This overridden method ...
065     * 
066     * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidDataPatternConstraint#getValidationMessageParams()
067     */
068    @Override
069    public List<String> getValidationMessageParams() {
070        if (validationMessageParams == null) {
071            validationMessageParams = new ArrayList<String>();
072            ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
073            if (allowNegative) {
074                validationMessageParams.add(configService
075                        .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX
076                                + "positiveOrNegative"));
077            } else {
078                validationMessageParams.add(configService
079                        .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "positive"));
080            }
081        }
082        return validationMessageParams;
083    }
084}