View Javadoc

1   /**
2    * Copyright 2005-2013 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 java.util.ArrayList;
19  import java.util.List;
20  
21  import org.kuali.rice.core.api.config.property.ConfigurationService;
22  import org.kuali.rice.krad.service.KRADServiceLocator;
23  import org.kuali.rice.krad.uif.UifConstants;
24  
25  /**
26   * Validation pattern for matching floating point numbers, optionally matching negative numbers
27   * 
28   * @author Kuali Rice Team (rice.collab@kuali.org)
29   */
30  public class FloatingPointPatternConstraint extends ConfigurationBasedRegexPatternConstraint {
31  
32      protected boolean allowNegative;
33  
34      /**
35       * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidCharactersPatternConstraint#getRegexString()
36       */
37      @Override
38      protected String getRegexString() {
39          StringBuffer regex = new StringBuffer();
40  
41          if (isAllowNegative()) {
42              regex.append("-?");
43          }
44          regex.append(super.getRegexString());
45  
46          return regex.toString();
47      }
48  
49      /**
50       * @return the allowNegative
51       */
52      public boolean isAllowNegative() {
53          return this.allowNegative;
54      }
55  
56      /**
57       * @param allowNegative the allowNegative to set
58       */
59      public void setAllowNegative(boolean allowNegative) {
60          this.allowNegative = allowNegative;
61      }
62  
63      /**
64       * This overridden method ...
65       * 
66       * @see org.kuali.rice.krad.datadictionary.validation.constraint.ValidDataPatternConstraint#getValidationMessageParams()
67       */
68      @Override
69      public List<String> getValidationMessageParams() {
70          if (validationMessageParams == null) {
71              validationMessageParams = new ArrayList<String>();
72              ConfigurationService configService = KRADServiceLocator.getKualiConfigurationService();
73              if (allowNegative) {
74                  validationMessageParams.add(configService
75                          .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX
76                                  + "positiveOrNegative"));
77              } else {
78                  validationMessageParams.add(configService
79                          .getPropertyValueAsString(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + "positive"));
80              }
81          }
82          return validationMessageParams;
83      }
84  }