001/**
002 * Copyright 2005-2016 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;
019
020/**
021 * The maskTo element is to used hide the beginning part of the value for
022 * unauthorized users. The number of leading characters to hide and the
023 * replacement character can be specified.
024 * 
025 * @author Kuali Rice Team (rice.collab@kuali.org)
026 */
027public class MaskFormatterSubString implements MaskFormatter {
028    private static final long serialVersionUID = -876112522775686636L;
029    
030    protected String maskCharacter = "*";
031    protected int maskLength;
032
033    public String maskValue(Object value) {
034        if (value == null) {
035            return null;
036        }
037
038        // TODO: MOVE TO UNIT TEST: move this validation into the unit tests
039        if (maskCharacter == null) {
040            throw new RuntimeException("Mask character not specified. Check DD maskTo attribute.");
041        }
042
043        String strValue = value.toString();
044        if (strValue.length() < maskLength) {
045                return StringUtils.repeat(maskCharacter, maskLength);
046        }
047        if(maskLength >0){
048                return StringUtils.repeat(maskCharacter, maskLength) + strValue.substring(maskLength);
049        }else{
050                return strValue;
051        }
052    }
053
054    /**
055     * Gets the maskCharacter attribute.
056     * 
057     * @return Returns the maskCharacter.
058     */
059    public String getMaskCharacter() {
060        return maskCharacter;
061    }
062
063    /**
064     * Specify the character with which to mask the original value.
065     *
066     * @param maskCharacter for masking values
067     */
068    public void setMaskCharacter(String maskCharacter) {
069        this.maskCharacter = maskCharacter;
070    }
071
072    /**
073     * Gets the maskLength attribute.
074     * 
075     * @return Returns the maskLength.
076     */
077    public int getMaskLength() {
078        return maskLength;
079    }
080
081    /**
082     * Set the number of characters to mask at the beginning of the string.
083     * 
084     * @param maskLength The maskLength to set.
085     */
086    public void setMaskLength(int maskLength) {
087        this.maskLength = maskLength;
088    }
089
090
091}