001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.datadictionary.mask;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.datadictionary.parse.BeanTag;
020import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
021
022/**
023 * The maskTo element is to used hide the beginning part of the value for
024 * unauthorized users. The number of leading characters to hide and the
025 * replacement character can be specified.
026 *
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 */
029@BeanTag(name = "maskFormatteSubString")
030public class MaskFormatterSubString implements MaskFormatter {
031    private static final long serialVersionUID = -876112522775686636L;
032
033    protected String maskCharacter = "*";
034    protected int maskLength;
035
036    public String maskValue(Object value) {
037        if (value == null) {
038            return null;
039        }
040
041        // TODO: MOVE TO UNIT TEST: move this validation into the unit tests
042        if (maskCharacter == null) {
043            throw new RuntimeException("Mask character not specified. Check DD maskTo attribute.");
044        }
045
046        String strValue = value.toString();
047        if (strValue.length() < maskLength) {
048            return StringUtils.repeat(maskCharacter, maskLength);
049        }
050        if (maskLength > 0) {
051            return StringUtils.repeat(maskCharacter, maskLength) + strValue.substring(maskLength);
052        } else {
053            return strValue;
054        }
055    }
056
057    /**
058     * Gets the maskCharacter attribute.
059     *
060     * @return Returns the maskCharacter.
061     */
062    @BeanTagAttribute(name = "maskCharacter")
063    public String getMaskCharacter() {
064        return maskCharacter;
065    }
066
067    /**
068     * Specify the character with which to mask the original value.
069     *
070     * @param maskCharacter for masking values
071     */
072    public void setMaskCharacter(String maskCharacter) {
073        this.maskCharacter = maskCharacter;
074    }
075
076    /**
077     * Gets the maskLength attribute.
078     *
079     * @return Returns the maskLength.
080     */
081    @BeanTagAttribute(name = "maskLength")
082    public int getMaskLength() {
083        return maskLength;
084    }
085
086    /**
087     * Set the number of characters to mask at the beginning of the string.
088     *
089     * @param maskLength The maskLength to set.
090     */
091    public void setMaskLength(int maskLength) {
092        this.maskLength = maskLength;
093    }
094
095}