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