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.mask;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
20  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
21  
22  /**
23   * The maskTo element is to used hide the beginning part of the value for
24   * unauthorized users. The number of leading characters to hide and the
25   * replacement character can be specified.
26   *
27   * @author Kuali Rice Team (rice.collab@kuali.org)
28   */
29  @BeanTag(name = "maskFormatteSubString-bean")
30  public class MaskFormatterSubString implements MaskFormatter {
31      private static final long serialVersionUID = -876112522775686636L;
32  
33      protected String maskCharacter = "*";
34      protected int maskLength;
35  
36      public String maskValue(Object value) {
37          if (value == null) {
38              return null;
39          }
40  
41          // TODO: MOVE TO UNIT TEST: move this validation into the unit tests
42          if (maskCharacter == null) {
43              throw new RuntimeException("Mask character not specified. Check DD maskTo attribute.");
44          }
45  
46          String strValue = value.toString();
47          if (strValue.length() < maskLength) {
48              return StringUtils.repeat(maskCharacter, maskLength);
49          }
50          if (maskLength > 0) {
51              return StringUtils.repeat(maskCharacter, maskLength) + strValue.substring(maskLength);
52          } else {
53              return strValue;
54          }
55      }
56  
57      /**
58       * Gets the maskCharacter attribute.
59       *
60       * @return Returns the maskCharacter.
61       */
62      @BeanTagAttribute(name = "maskCharacter")
63      public String getMaskCharacter() {
64          return maskCharacter;
65      }
66  
67      /**
68       * Specify the character with which to mask the original value.
69       *
70       * @param maskCharacter for masking values
71       */
72      public void setMaskCharacter(String maskCharacter) {
73          this.maskCharacter = maskCharacter;
74      }
75  
76      /**
77       * Gets the maskLength attribute.
78       *
79       * @return Returns the maskLength.
80       */
81      @BeanTagAttribute(name = "maskLength")
82      public int getMaskLength() {
83          return maskLength;
84      }
85  
86      /**
87       * Set the number of characters to mask at the beginning of the string.
88       *
89       * @param maskLength The maskLength to set.
90       */
91      public void setMaskLength(int maskLength) {
92          this.maskLength = maskLength;
93      }
94  
95  }