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 * TODO delyea don't forget to fill this in.
027 * 
028 * @author Kuali Rice Team (rice.collab@kuali.org)
029 */
030public class FixedPointPatternConstraint extends ValidDataPatternConstraint {
031
032    protected boolean allowNegative;
033    protected int precision;
034    protected int scale;
035
036    /**
037     * Overriding retrieval of
038     * 
039     * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersPatternConstraint#getRegexString()
040     */
041    @Override
042    protected String getRegexString() {
043        StringBuilder regex = new StringBuilder();
044
045        if (isAllowNegative()) {
046            regex.append("-?");
047        }
048        // final patter will be: -?([0-9]{0,p-s}\.[0-9]{1,s}|[0-9]{1,p-s}) where p = precision, s=scale
049        regex.append("(");
050        regex.append("[0-9]{0," + (getPrecision() - getScale()) + "}");
051        regex.append("\\.");
052        regex.append("[0-9]{1," + getScale() + "}");
053        regex.append("|[0-9]{1," + (getPrecision() - getScale()) + "}");
054        regex.append(")");
055        return regex.toString();
056    }
057
058    /**
059     * @return the allowNegative
060     */
061    public boolean isAllowNegative() {
062        return this.allowNegative;
063    }
064
065    /**
066     * @param allowNegative the allowNegative to set
067     */
068    public void setAllowNegative(boolean allowNegative) {
069        this.allowNegative = allowNegative;
070    }
071
072    /**
073     * @return the precision
074     */
075    public int getPrecision() {
076        return this.precision;
077    }
078
079    /**
080     * @param precision the precision to set
081     */
082    public void setPrecision(int precision) {
083        this.precision = precision;
084    }
085
086    /**
087     * @return the scale
088     */
089    public int getScale() {
090        return this.scale;
091    }
092
093    /**
094     * @param scale the scale to set
095     */
096    public void setScale(int scale) {
097        this.scale = scale;
098    }
099
100    /**
101     * This overridden method ...
102     * 
103     * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidDataPatternConstraint#getValidationMessageParams()
104     */
105    @Override
106    public List<String> getValidationMessageParams() {
107        if(validationMessageParams == null){
108            validationMessageParams = new ArrayList<String>();
109            ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
110            if (allowNegative) {
111                validationMessageParams.add(configService.getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX
112                        + "positiveOrNegative"));
113            } else {
114                validationMessageParams.add(configService.getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX
115                        + "positive"));
116            }
117    
118            validationMessageParams.add(Integer.toString(precision));
119            validationMessageParams.add(Integer.toString(scale));
120        }
121        return validationMessageParams;
122    }
123
124}