View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.datadictionary.validation.constraint;
17  
18  import org.kuali.rice.core.api.config.property.ConfigurationService;
19  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
20  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
21  import org.kuali.rice.krad.messages.MessageService;
22  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
23  import org.kuali.rice.krad.uif.UifConstants;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  /**
29   * TODO Administrator don't forget to fill this in.
30   *
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  @BeanTag(name = "integerPatternConstraint", parent = "IntegerPatternConstraint")
34  public class IntegerPatternConstraint extends ValidDataPatternConstraint {
35      protected boolean allowNegative;
36      protected boolean onlyNegative;
37      protected boolean omitZero;
38  
39      /**
40       * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersPatternConstraint#getRegexString()
41       */
42      @Override
43      protected String getRegexString() {
44          StringBuffer regex = new StringBuffer();
45  
46          if (isAllowNegative() && !onlyNegative) {
47              regex.append("((-?");
48          } else if (onlyNegative) {
49              regex.append("((-");
50          } else {
51              regex.append("((");
52          }
53          if (omitZero) {
54              regex.append("[1-9][0-9]*))");
55          } else {
56              regex.append("[1-9][0-9]*)|[0]*)");
57          }
58  
59          return regex.toString();
60      }
61  
62      /**
63       * @return the allowNegative
64       */
65      @BeanTagAttribute(name = "allowNegative")
66      public boolean isAllowNegative() {
67          return this.allowNegative;
68      }
69  
70      /**
71       * @param allowNegative the allowNegative to set
72       */
73      public void setAllowNegative(boolean allowNegative) {
74          this.allowNegative = allowNegative;
75      }
76  
77      @BeanTagAttribute(name = "onlyNegative")
78      public boolean isOnlyNegative() {
79          return onlyNegative;
80      }
81  
82      /**
83       * When set to true, only allows negative numbers (and zero if allowZero is still true)
84       *
85       * @param onlyNegative
86       */
87      public void setOnlyNegative(boolean onlyNegative) {
88          this.onlyNegative = onlyNegative;
89      }
90  
91      @BeanTagAttribute(name = "omitZero")
92      public boolean isOmitZero() {
93          return omitZero;
94      }
95  
96      /**
97       * When set to true, zero is not allowed in the set of allowed numbers.
98       *
99       * @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 }