View Javadoc

1   /**
2    * Copyright 2005-2011 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.kns.document.authorization;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.kns.web.ui.Field;
20  import org.kuali.rice.krad.datadictionary.mask.MaskFormatter;
21  
22  /**
23   * This class represents the authorization restrictions (or lack of) for a given field.
24   * 
25   * 
26   */
27  /**
28   * This is a description of what this class does - zjzhou don't forget to fill this in. 
29   * 
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   *
32   */
33  public class FieldRestriction {
34  
35      private String fieldName;
36      private boolean editable;
37      private boolean viewable;
38      private boolean masked;
39      private boolean partiallyMasked;
40      private MaskFormatter maskFormatter;
41      private boolean shouldBeEncrypted;
42      /**
43       * Constructs a FieldAuthorization.java.
44       */
45      public FieldRestriction() {
46          editable = true;
47          viewable = true;
48      }
49  
50      /**
51       * 
52       * Constructs a FieldAuthorization.java.
53       * 
54       * @param fieldName - name of field to represent
55       * @param canEdit - true if the field is editable in this context, false otherwise
56       * @param canView - true if thie field is viewable in this context, false otherwise
57       * 
58       */
59      public FieldRestriction(String fieldName, boolean canEdit, boolean canView) {
60          this.fieldName = fieldName;
61          setEditable(canEdit); // using setters here to run impossible combinations check
62          setViewable(canView);
63      }
64      
65      /**
66       * 
67       * Constructs a FieldAuthorization.java.
68       * 
69       * @param fieldName - name of the field to represent
70       * @param fieldAuthorizationFlag - Field.HIDDEN, Field.READONLY, or Field.EDITABLE
71       */
72      public FieldRestriction(String fieldName, String fieldAuthorizationFlag) {
73          // if an invalid flag is passed in, the choke on it
74          if (!fieldAuthorizationFlag.equals(Field.EDITABLE) && !fieldAuthorizationFlag.equals(Field.READONLY) 
75          		&& !fieldAuthorizationFlag.equals(Field.HIDDEN) && !fieldAuthorizationFlag.equals(Field.MASKED)
76          		&& !fieldAuthorizationFlag.equals(Field.PARTIALLY_MASKED)) {
77              throw new IllegalArgumentException("The only allowable values are " +
78              		"Field.HIDDEN, Field.READONLY, Field.EDITABLE, Field.MASKED and Field.PARTIALLY_MASKED");
79          }
80  
81          this.fieldName = fieldName;
82  
83          if (fieldAuthorizationFlag.equals(Field.EDITABLE)) {
84              this.editable = true;
85              this.viewable = true;
86          } else if (fieldAuthorizationFlag.equals(Field.READONLY)) {
87              this.editable = false;
88              this.viewable = true;
89          } else if (fieldAuthorizationFlag.equals(Field.HIDDEN)) {
90              this.editable = false;
91              this.viewable = false;
92          } else if(fieldAuthorizationFlag.equals(Field.MASKED)){
93  			this.masked = true;
94  			this.viewable = true;
95  			this.editable = false;
96  		} else if(fieldAuthorizationFlag.equals(Field.PARTIALLY_MASKED)){
97  			this.partiallyMasked = true;
98  			this.viewable = true;
99  			this.editable = false;
100 		}
101     }
102 
103     /**
104      * 
105      * This method returns the correct flag from the Kuali Field object, that corresponds to the particular combination of editable
106      * and viewable set on this object.
107      * 
108      * @return Field.HIDDEN, Field.READONLY, or Field.EDITABLE
109      * 
110      */
111     public String getKualiFieldDisplayFlag() {
112 
113         if (!editable && !viewable) {
114             return Field.HIDDEN;
115         }
116         if (!editable && viewable) {
117             return Field.READONLY;
118         }
119         else {
120             return Field.EDITABLE;
121         }
122 
123     }
124 
125     /**
126      * 
127      * This method returns true if the FieldAuthorization is some kind of restriction, and returns false if it is an editable field.
128      * 
129      * @return boolean
130      * 
131      */
132     public boolean isRestricted() {
133         if (!editable || !viewable) {
134             return true;
135         }
136         else {
137             return false;
138         }
139     }
140 
141     /**
142      * 
143      * This method returns true if this authorization prohibits Viewing and Editing, resulting in a hidden field.
144      * 
145      * @return boolean
146      * 
147      */
148     public boolean isHidden() {
149         if (!editable && !viewable) {
150             return true;
151         }
152         else {
153             return false;
154         }
155     }
156 
157     /**
158      * 
159      * This method returns true if this authorization prohibits Editing but not Viewing, resulting in a ReadOnly field.
160      * 
161      * @return boolean
162      * 
163      */
164     public boolean isReadOnly() {
165         if (!editable && viewable) {
166             return true;
167         }
168         else {
169             return false;
170         }
171     }
172 
173     /**
174      * Gets the editable attribute.
175      * 
176      * @return Returns the editable.
177      */
178     public boolean isEditable() {
179         return editable;
180     }
181 
182     /**
183      * Sets the editable attribute value.
184      * 
185      * Note that if editable is being set to true, and the internal value of viewable is false, viewable will be flipped to true, to
186      * avoid impossible combinations of flags.
187      * 
188      * @param editable The editable to set.
189      */
190     public void setEditable(boolean editable) {
191         if (editable && !this.viewable) {
192             this.viewable = true;
193         }
194         this.editable = editable;
195     }
196 
197     /**
198      * Gets the fieldName attribute.
199      * 
200      * @return Returns the fieldName.
201      */
202     public String getFieldName() {
203         return fieldName;
204     }
205 
206     /**
207      * Sets the fieldName attribute value.
208      * 
209      * @param fieldName The fieldName to set.
210      */
211     public void setFieldName(String fieldName) {
212         this.fieldName = fieldName;
213     }
214 
215     /**
216      * Gets the viewable attribute.
217      * 
218      * @return Returns the viewable.
219      */
220     public boolean isViewable() {
221         return viewable;
222     }
223 
224     /**
225      * Sets the viewable attribute value.
226      * 
227      * Note that if viewable is being set to false, and the internal value of editable is true, then editable will be silently
228      * flipped to false. This is done to avoid impossible combinations of authorization flags.
229      * 
230      * @param viewable The viewable to set.
231      */
232     public void setViewable(boolean viewable) {
233         if (!viewable && this.editable) {
234             this.editable = false;
235         }
236         this.viewable = viewable;
237     }
238 
239     /**
240      * @see java.lang.Object#toString()
241      */
242     public String toString() {
243         StringBuffer sb = new StringBuffer();
244         sb.append(this.fieldName);
245         sb.append(" [");
246         if (this.editable) {
247             sb.append("editable");
248         }
249         else {
250             sb.append("not editable");
251         }
252         sb.append(",");
253         if (this.viewable) {
254             sb.append("viewable");
255         }
256         else {
257             sb.append("not viewable");
258         }
259         sb.append("]");
260         return sb.toString();
261     }
262 
263     /**
264      * @see java.lang.Object#equals(java.lang.Object)
265      */
266     public boolean equals(Object obj) {
267         boolean equal = false;
268 
269         if (obj != null) {
270             if (this.getClass().equals(obj.getClass())) {
271                 FieldRestriction other = (FieldRestriction) obj;
272 
273                 if (StringUtils.equals(this.fieldName, other.getFieldName())) {
274                     if (this.editable == other.isEditable() && this.viewable == other.isViewable()) {
275                         equal = true;
276                     }
277                 }
278             }
279         }
280 
281         return equal;
282     }
283 
284     /**
285      * @see java.lang.Object#hashCode()
286      */
287     public int hashCode() {
288         return toString().hashCode();
289     }
290 
291 	/**
292 	 * @return the masked
293 	 */
294 	public boolean isMasked() {
295 		return this.masked;
296 	}
297 
298 	/**
299 	 * @return the partiallyMasked
300 	 */
301 	public boolean isPartiallyMasked() {
302 		return this.partiallyMasked;
303 	}
304 
305 	
306 	/**
307 	 * @return the shouldBeEncrypted
308 	 */
309 	public boolean isShouldBeEncrypted() {
310 		return this.shouldBeEncrypted;
311 	}
312 
313 	/**
314 	 * @param shouldBeEncrypted the shouldBeEncrypted to set
315 	 */
316 	public void setShouldBeEncrypted(boolean shouldBeEncrypted) {
317 		this.shouldBeEncrypted = shouldBeEncrypted;
318 	}
319 
320 	/**
321 	 * @return the maskFormatter
322 	 */
323 	public MaskFormatter getMaskFormatter() {
324 		return this.maskFormatter;
325 	}
326 
327 	/**
328 	 * @param maskFormatter the maskFormatter to set
329 	 */
330 	public void setMaskFormatter(MaskFormatter maskFormatter) {
331 		this.maskFormatter = maskFormatter;
332 	}
333 	
334 	
335 }