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 org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreApiServiceLocator;
20  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
21  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
22  import org.kuali.rice.krad.datadictionary.parse.BeanTags;
23  import org.kuali.rice.krad.uif.UifConstants;
24  
25  /**
26   * ConfigurationBasedRegexPatternConstraint uses a patternTypeKey to get the regex used for validation by key from
27   * the KualiConfigurationService
28   *
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  @BeanTags({@BeanTag(name = "configurationBasedRegexPatternConstraint-bean"),
32          @BeanTag(name = "phoneNumberPatternConstraint-bean", parent = "PhoneNumberPatternConstraint"),
33          @BeanTag(name = "timePatternConstraint-bean", parent = "TimePatternConstraint"),
34          @BeanTag(name = "time24HPatternConstraint-bean", parent = "Time24HPatternConstraint"),
35          @BeanTag(name = "urlPatternConstraint-bean", parent = "UrlPatternConstraint"),
36          @BeanTag(name = "noWhitespacePatternConstraint-bean", parent = "NoWhitespacePatternConstraint"),
37          @BeanTag(name = "javaClassPatternConstraint-bean", parent = "JavaClassPatternConstraint"),
38          @BeanTag(name = "emailAddressPatternConstraint-bean", parent = "EmailAddressPatternConstraint"),
39          @BeanTag(name = "timestampPatternConstraint-bean", parent = "TimestampPatternConstraint"),
40          @BeanTag(name = "yearPatternConstraint-bean", parent = "YearPatternConstraint"),
41          @BeanTag(name = "monthPatternConstraint-bean", parent = "MonthPatternConstraint"),
42          @BeanTag(name = "zipcodePatternConstraint-bean", parent = "ZipcodePatternConstraint")})
43  public class ConfigurationBasedRegexPatternConstraint extends ValidDataPatternConstraint {
44      protected String patternTypeKey;
45  
46      /**
47       * Message key used to identify the validation pattern
48       *
49       * @return the patternTypeKey
50       */
51      @BeanTagAttribute(name = "patternTypeKey")
52      public String getPatternTypeKey() {
53          return this.patternTypeKey;
54      }
55  
56      /**
57       * Setter for the pattern message key
58       *
59       * @param patternTypeKey the patternTypeKey to set
60       */
61      public void setPatternTypeKey(String patternTypeKey) {
62          this.patternTypeKey = patternTypeKey;
63      }
64  
65      /**
66       * MessageKey in used in this class have the patternTypeKey appended to the VALIDATION_MSG_KEY_PREFIX by default,
67       * if it is not explicitly set to something else
68       *
69       * @see org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint#getMessageKey()
70       */
71      @Override
72      public String getMessageKey() {
73          if (StringUtils.isNotEmpty(messageKey)) {
74              return messageKey;
75          }
76  
77          StringBuilder buf = new StringBuilder();
78          buf.append(UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX).append(getPatternTypeKey());
79          return buf.toString();
80      }
81  
82      /**
83       * Uses the key returned by {@link #getPatternTypeKey()} to fetch the
84       * validationPattern's regex string from the ConfigurationService which should not include
85       * the start(^) and end($) symbols
86       *
87       * @return String regex validation string
88       */
89      @Override
90      protected String getRegexString() {
91          if ( StringUtils.isBlank(getPatternTypeKey())) {
92              throw new IllegalArgumentException("patternTypeKey is null, configuration of " + this.getClass().getName() + " is incomplete" );
93          }
94          return CoreApiServiceLocator.getKualiConfigurationService().getPropertyValueAsString(getPatternTypeKey());
95      }
96  
97  }