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.service.KRADServiceLocator;
20  import org.kuali.rice.krad.uif.UifConstants;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  /**
26   * TODO Administrator don't forget to fill this in. 
27   * 
28   * @author Kuali Rice Team (rice.collab@kuali.org)
29   */
30  public class IntegerPatternConstraint extends ValidDataPatternConstraint{
31      protected boolean allowNegative;
32      protected boolean onlyNegative;
33      protected boolean omitZero;
34  
35      /**
36       * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersPatternConstraint#getRegexString()
37       */
38      @Override
39      protected String getRegexString() {
40          StringBuffer regex = new StringBuffer();
41  
42          if (isAllowNegative() && !onlyNegative) {
43              regex.append("((-?");
44          }
45          else if(onlyNegative){
46              regex.append("((-");
47          }
48          else {
49              regex.append("((");
50          }
51          if(omitZero){
52              regex.append("[1-9][0-9]*))");
53          }
54          else{
55              regex.append("[1-9][0-9]*)|[0]*)");
56          }
57  
58          return regex.toString();
59      }
60      
61      /**
62       * @return the allowNegative
63       */
64      public boolean isAllowNegative() {
65          return this.allowNegative;
66      }
67  
68      /**
69       * @param allowNegative the allowNegative to set
70       */
71      public void setAllowNegative(boolean allowNegative) {
72          this.allowNegative = allowNegative;
73      }
74  
75      public boolean isOnlyNegative() {
76          return onlyNegative;
77      }
78  
79      /**
80       * When set to true, only allows negative numbers (and zero if allowZero is still true)
81       * @param onlyNegative
82       */
83      public void setOnlyNegative(boolean onlyNegative) {
84          this.onlyNegative = onlyNegative;
85      }
86  
87      public boolean isOmitZero() {
88          return omitZero;
89      }
90  
91      /**
92       * When set to true, zero is not allowed in the set of allowed numbers.
93       * @param omitZero
94       */
95      public void setOmitZero(boolean omitZero) {
96          this.omitZero = omitZero;
97      }
98  
99      /**
100      * This overridden method ...
101      * 
102      * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidDataPatternConstraint#getValidationMessageParams()
103      */
104     @Override
105     public List<String> getValidationMessageParams() {
106         if (validationMessageParams == null) {
107             validationMessageParams = new ArrayList<String>();
108             ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
109             if (allowNegative && !onlyNegative) {
110                 if(omitZero){
111                     validationMessageParams.add(configService
112                             .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX
113                                     + "positiveOrNegative"));
114                 }
115                 else{
116                     validationMessageParams.add(configService
117                             .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX
118                                     + "positiveOrNegativeOrZero"));
119                 }
120             }
121             else if(onlyNegative){
122                 if(omitZero){
123                     validationMessageParams.add(configService
124                             .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "negative"));
125                 }
126                 else{
127                     validationMessageParams.add(configService
128                             .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "negativeOrZero"));
129                 }
130             }
131             else {
132                 if(omitZero){
133                     validationMessageParams.add(configService
134                             .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "positive"));
135                 }
136                 else{
137                     validationMessageParams.add(configService
138                             .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "positiveOrZero"));
139                 }
140             }
141         }
142         return validationMessageParams;
143     }
144 }