View Javadoc
1   /**
2    * Copyright 2005-2016 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;
17  
18  import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
19  import org.kuali.rice.krad.datadictionary.mask.MaskFormatter;
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.uif.UifDictionaryBeanBase;
23  import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
24  
25  /**
26   * Defines a set of restrictions that are possible on an attribute
27   *
28   * @author Kuali Rice Team (rice.collab@kuali.org)
29   */
30  @BeanTag(name = "attributeSecurity-bean")
31  public class AttributeSecurity extends UifDictionaryBeanBase {
32      private static final long serialVersionUID = -7923499408946975318L;
33  
34      private boolean readOnly = false;
35      private boolean hide = false;
36      private boolean mask = false;
37      private boolean partialMask = false;
38  
39      private MaskFormatter partialMaskFormatter;
40      private MaskFormatter maskFormatter;
41  
42      /**
43       * @return the readOnly
44       */
45      @BeanTagAttribute(name = "readOnly")
46      public boolean isReadOnly() {
47          return this.readOnly;
48      }
49  
50      /**
51       * @param readOnly the readOnly to set
52       */
53      public void setReadOnly(boolean readOnly) {
54          this.readOnly = readOnly;
55      }
56  
57      /**
58       * @return the hide
59       */
60      @BeanTagAttribute(name = "hide")
61      public boolean isHide() {
62          return this.hide;
63      }
64  
65      /**
66       * @param hide the hide to set
67       */
68      public void setHide(boolean hide) {
69          this.hide = hide;
70      }
71  
72      /**
73       * @return the mask
74       */
75      @BeanTagAttribute(name = "mask")
76      public boolean isMask() {
77          return this.mask;
78      }
79  
80      /**
81       * @param mask the mask to set
82       */
83      public void setMask(boolean mask) {
84          this.mask = mask;
85      }
86  
87      /**
88       * @return the partialMask
89       */
90      @BeanTagAttribute(name = "partialMask")
91      public boolean isPartialMask() {
92          return this.partialMask;
93      }
94  
95      /**
96       * @param partialMask the partialMask to set
97       */
98      public void setPartialMask(boolean partialMask) {
99          this.partialMask = partialMask;
100     }
101 
102     /**
103      * @return the maskFormatter
104      */
105     @BeanTagAttribute(name = "maskFormatter", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
106     public MaskFormatter getMaskFormatter() {
107         return this.maskFormatter;
108     }
109 
110     /**
111      * @param maskFormatter the maskFormatter to set
112      */
113     public void setMaskFormatter(MaskFormatter maskFormatter) {
114         this.maskFormatter = maskFormatter;
115     }
116 
117     /**
118      * @return the partialMaskFormatter
119      */
120     @BeanTagAttribute(name = "partialMaskFormatter", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
121     public MaskFormatter getPartialMaskFormatter() {
122         return this.partialMaskFormatter;
123     }
124 
125     /**
126      * @param partialMaskFormatter the partialMaskFormatter to set
127      */
128     public void setPartialMaskFormatter(MaskFormatter partialMaskFormatter) {
129         this.partialMaskFormatter = partialMaskFormatter;
130     }
131 
132     /**
133      * This overridden method ...
134      *
135      * @see org.kuali.rice.krad.datadictionary.DataDictionaryDefinition#completeValidation(java.lang.Class,
136      *      java.lang.Class)
137      */
138     public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass) {
139 
140         if (mask && maskFormatter == null) {
141             throw new AttributeValidationException("MaskFormatter is required");
142         }
143         if (partialMask && partialMaskFormatter == null) {
144             throw new AttributeValidationException("PartialMaskFormatter is required");
145         }
146     }
147 
148     /**
149      * Directly validate simple fields
150      *
151      * @see org.kuali.rice.krad.datadictionary.DataDictionaryEntry#completeValidation(org.kuali.rice.krad.datadictionary.validator.ValidationTrace)
152      */
153     public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass,
154             ValidationTrace tracer) {
155         tracer.addBean(this.getClass().getSimpleName(), ValidationTrace.NO_BEAN_ID);
156 
157         if (mask && maskFormatter == null) {
158             String currentValues[] = {"mask = " + mask, "maskFormatter = " + maskFormatter};
159             tracer.createError("MaskFormatter is required", currentValues);
160         }
161         if (partialMask && partialMaskFormatter == null) {
162             String currentValues[] = {"partialMask = " + partialMask, "partialMaskFormatter = " + partialMaskFormatter};
163             tracer.createError("PartialMaskFormatter is required", currentValues);
164         }
165 
166     }
167 
168     /**
169      * Returns whether any of the restrictions defined in this class are true.
170      */
171     public boolean hasAnyRestriction() {
172         return readOnly || mask || partialMask || hide;
173     }
174 
175     /**
176      * Returns whether any of the restrictions defined in this class indicate that the attribute value potentially
177      * needs
178      * to be not shown to the user (i.e. masked, partial mask, hide).  Note that readonly does not fall in this
179      * category.
180      *
181      * @return
182      */
183     public boolean hasRestrictionThatRemovesValueFromUI() {
184         return mask || partialMask || hide;
185     }
186 
187     /**
188      * Returns a copy of the component.
189      *
190      * @return AttributeSecurity copy of the component
191      */
192     public <T> T copy() {
193         T copiedClass = null;
194         try {
195             copiedClass = (T)this.getClass().newInstance();
196         }
197         catch(Exception exception) {
198             throw new RuntimeException();
199         }
200 
201         copyProperties(copiedClass);
202 
203         return copiedClass;
204     }
205 
206     /**
207      * Copies the properties over for the copy method
208      *
209      */
210     protected <T> void copyProperties(T component) {
211         super.copyProperties(component);
212         AttributeSecurity attributeSecurityCopy = ((AttributeSecurity)component);
213         attributeSecurityCopy.setHide(this.hide);
214         attributeSecurityCopy.setMask(this.mask);
215         attributeSecurityCopy.setPartialMask(this.partialMask);
216         attributeSecurityCopy.setReadOnly(this.readOnly);
217         attributeSecurityCopy.setMaskFormatter(this.maskFormatter);
218         attributeSecurityCopy.setPartialMaskFormatter(this.partialMaskFormatter);
219     }
220 }