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;
017
018import org.kuali.rice.krad.datadictionary.mask.MaskFormatter;
019import org.kuali.rice.krad.datadictionary.parse.BeanTag;
020import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
021import org.kuali.rice.krad.datadictionary.uif.UifDictionaryBeanBase;
022import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
023
024/**
025 * Defines a set of restrictions that are possible on an attribute
026 *
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 */
029@BeanTag(name = "attributeSecurity")
030public class AttributeSecurity extends UifDictionaryBeanBase {
031    private static final long serialVersionUID = -7923499408946975318L;
032
033    private boolean readOnly = false;
034    private boolean hide = false;
035    private boolean mask = false;
036    private boolean partialMask = false;
037
038    private MaskFormatter partialMaskFormatter;
039    private MaskFormatter maskFormatter;
040
041    /**
042     * @return the readOnly
043     */
044    @BeanTagAttribute(name = "readOnly")
045    public boolean isReadOnly() {
046        return this.readOnly;
047    }
048
049    /**
050     * @param readOnly the readOnly to set
051     */
052    public void setReadOnly(boolean readOnly) {
053        this.readOnly = readOnly;
054    }
055
056    /**
057     * @return the hide
058     */
059    @BeanTagAttribute(name = "hide")
060    public boolean isHide() {
061        return this.hide;
062    }
063
064    /**
065     * @param hide the hide to set
066     */
067    public void setHide(boolean hide) {
068        this.hide = hide;
069    }
070
071    /**
072     * @return the mask
073     */
074    @BeanTagAttribute(name = "mask")
075    public boolean isMask() {
076        return this.mask;
077    }
078
079    /**
080     * @param mask the mask to set
081     */
082    public void setMask(boolean mask) {
083        this.mask = mask;
084    }
085
086    /**
087     * @return the partialMask
088     */
089    @BeanTagAttribute(name = "partialMask")
090    public boolean isPartialMask() {
091        return this.partialMask;
092    }
093
094    /**
095     * @param partialMask the partialMask to set
096     */
097    public void setPartialMask(boolean partialMask) {
098        this.partialMask = partialMask;
099    }
100
101    /**
102     * @return the maskFormatter
103     */
104    @BeanTagAttribute(name = "maskFormatter", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
105    public MaskFormatter getMaskFormatter() {
106        return this.maskFormatter;
107    }
108
109    /**
110     * @param maskFormatter the maskFormatter to set
111     */
112    public void setMaskFormatter(MaskFormatter maskFormatter) {
113        this.maskFormatter = maskFormatter;
114    }
115
116    /**
117     * @return the partialMaskFormatter
118     */
119    @BeanTagAttribute(name = "partialMaskFormatter", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
120    public MaskFormatter getPartialMaskFormatter() {
121        return this.partialMaskFormatter;
122    }
123
124    /**
125     * @param partialMaskFormatter the partialMaskFormatter to set
126     */
127    public void setPartialMaskFormatter(MaskFormatter partialMaskFormatter) {
128        this.partialMaskFormatter = partialMaskFormatter;
129    }
130
131    /**
132     * This overridden method ...
133     *
134     * @see org.kuali.rice.krad.datadictionary.DataDictionaryDefinition#completeValidation(java.lang.Class,
135     *      java.lang.Class)
136     */
137    public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass) {
138        completeValidation(rootBusinessObjectClass, otherBusinessObjectClass, new ValidationTrace());
139    }
140
141    /**
142     * Directly validate simple fields
143     *
144     * @see org.kuali.rice.krad.datadictionary.DataDictionaryEntry#completeValidation(org.kuali.rice.krad.datadictionary.validator.ValidationTrace)
145     */
146    public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass,
147            ValidationTrace tracer) {
148        tracer.addBean(this.getClass().getSimpleName(), ValidationTrace.NO_BEAN_ID);
149
150        if (mask && maskFormatter == null) {
151            String currentValues[] = {"mask = " + mask, "maskFormatter = " + maskFormatter};
152            tracer.createError("MaskFormatter is required", currentValues);
153        }
154        if (partialMask && partialMaskFormatter == null) {
155            String currentValues[] = {"partialMask = " + partialMask, "partialMaskFormatter = " + partialMaskFormatter};
156            tracer.createError("PartialMaskFormatter is required", currentValues);
157        }
158
159    }
160
161    /**
162     * Returns whether any of the restrictions defined in this class are true.
163     */
164    public boolean hasAnyRestriction() {
165        return readOnly || mask || partialMask || hide;
166    }
167
168    /**
169     * Returns whether any of the restrictions defined in this class indicate that the attribute
170     * value potentially needs to be not shown to the user (i.e. masked, partial mask, hide). Note
171     * that readonly does not fall in this category.
172     * 
173     * @return true if the value should be hidden from the user
174     */
175    public boolean hasRestrictionThatRemovesValueFromUI() {
176        return mask || partialMask || hide;
177    }
178
179    @Override
180    public String toString() {
181        StringBuilder builder = new StringBuilder();
182        builder.append("DataObjectAttributeSecurityBase [readOnly=").append(readOnly).append(", hide=").append(hide)
183                .append(", mask=").append(mask).append(", partialMask=").append(partialMask).append(", ");
184        if (maskFormatter != null) {
185            builder.append("maskFormatter=").append(maskFormatter).append(", ");
186        }
187        if (partialMaskFormatter != null) {
188            builder.append("partialMaskFormatter=").append(partialMaskFormatter);
189        }
190        builder.append("]");
191        return builder.toString();
192    }
193}