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