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.kns.datadictionary.validation.fieldlevel;
017
018import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
019import org.kuali.rice.krad.datadictionary.validation.FieldLevelValidationPattern;
020
021/**
022 * Validation pattern for matching floating point numbers, optionally matching negative numbers
023 * 
024 * 
025 */
026public class FloatingPointValidationPattern extends FieldLevelValidationPattern {
027    protected boolean allowNegative;
028
029    /**
030     * @return allowNegative
031     */
032    public boolean getAllowNegative() {
033        return allowNegative;
034    }
035
036    /**
037     * @param allowNegative
038     */
039    public void setAllowNegative(boolean allowNegative) {
040        this.allowNegative = allowNegative;
041    }
042
043    /**
044     * Adds special handling to account for optional allowNegative
045     * 
046     * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
047     */
048    protected String getRegexString() {
049        StringBuffer regex = new StringBuffer();
050
051        if (allowNegative) {
052            regex.append("-?");
053        }
054        regex.append(super.getRegexString());
055
056        return regex.toString();
057    }
058
059    /**
060     * @see org.kuali.rice.krad.datadictionary.validation.FieldLevelValidationPattern#getPatternTypeName()
061     */
062    protected String getPatternTypeName() {
063        return "floatingPoint";
064    }
065
066
067    /**
068     * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#buildExportMap(java.lang.String)
069     */
070    public ExportMap buildExportMap(String exportKey) {
071        ExportMap exportMap = super.buildExportMap(exportKey);
072
073        if (allowNegative) {
074            exportMap.set("allowNegative", "true");
075        }
076
077        return exportMap;
078    }
079    
080        /**
081         * @see org.kuali.rice.krad.datadictionary.validation.FieldLevelValidationPattern#getValidationErrorMessageKey()
082         */
083        @Override
084        public String getValidationErrorMessageKey() {
085                StringBuilder buf = new StringBuilder();
086                buf.append("error.format.").append(getClass().getName());
087                if (allowNegative) {
088                        buf.append(".allowNegative");
089                }
090                return buf.toString();
091        }
092}