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