Coverage Report - org.kuali.rice.krad.authorization.FieldRestriction
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldRestriction
0%
0/87
0%
0/62
2.682
 
 1  
 /*
 2  
  * Copyright 2006-2008 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.authorization;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.krad.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  0
     public FieldRestriction() {
 46  0
         editable = true;
 47  0
         viewable = true;
 48  0
     }
 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  0
     public FieldRestriction(String fieldName, boolean canEdit, boolean canView) {
 60  0
         this.fieldName = fieldName;
 61  0
         setEditable(canEdit); // using setters here to run impossible combinations check
 62  0
         setViewable(canView);
 63  0
     }
 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  0
     public FieldRestriction(String fieldName, String fieldAuthorizationFlag) {
 73  
         // if an invalid flag is passed in, the choke on it
 74  0
         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  0
             throw new IllegalArgumentException("The only allowable values are " +
 78  
                             "Field.HIDDEN, Field.READONLY, Field.EDITABLE, Field.MASKED and Field.PARTIALLY_MASKED");
 79  
         }
 80  
 
 81  0
         this.fieldName = fieldName;
 82  
 
 83  0
         if (fieldAuthorizationFlag.equals(Field.EDITABLE)) {
 84  0
             this.editable = true;
 85  0
             this.viewable = true;
 86  0
         } else if (fieldAuthorizationFlag.equals(Field.READONLY)) {
 87  0
             this.editable = false;
 88  0
             this.viewable = true;
 89  0
         } else if (fieldAuthorizationFlag.equals(Field.HIDDEN)) {
 90  0
             this.editable = false;
 91  0
             this.viewable = false;
 92  0
         } else if(fieldAuthorizationFlag.equals(Field.MASKED)){
 93  0
                         this.masked = true;
 94  0
                         this.viewable = true;
 95  0
                         this.editable = false;
 96  0
                 } else if(fieldAuthorizationFlag.equals(Field.PARTIALLY_MASKED)){
 97  0
                         this.partiallyMasked = true;
 98  0
                         this.viewable = true;
 99  0
                         this.editable = false;
 100  
                 }
 101  0
     }
 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  0
         if (!editable && !viewable) {
 114  0
             return Field.HIDDEN;
 115  
         }
 116  0
         if (!editable && viewable) {
 117  0
             return Field.READONLY;
 118  
         }
 119  
         else {
 120  0
             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  0
         if (!editable || !viewable) {
 134  0
             return true;
 135  
         }
 136  
         else {
 137  0
             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  0
         if (!editable && !viewable) {
 150  0
             return true;
 151  
         }
 152  
         else {
 153  0
             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  0
         if (!editable && viewable) {
 166  0
             return true;
 167  
         }
 168  
         else {
 169  0
             return false;
 170  
         }
 171  
     }
 172  
 
 173  
     /**
 174  
      * Gets the editable attribute.
 175  
      * 
 176  
      * @return Returns the editable.
 177  
      */
 178  
     public boolean isEditable() {
 179  0
         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  0
         if (editable && !this.viewable) {
 192  0
             this.viewable = true;
 193  
         }
 194  0
         this.editable = editable;
 195  0
     }
 196  
 
 197  
     /**
 198  
      * Gets the fieldName attribute.
 199  
      * 
 200  
      * @return Returns the fieldName.
 201  
      */
 202  
     public String getFieldName() {
 203  0
         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  0
         this.fieldName = fieldName;
 213  0
     }
 214  
 
 215  
     /**
 216  
      * Gets the viewable attribute.
 217  
      * 
 218  
      * @return Returns the viewable.
 219  
      */
 220  
     public boolean isViewable() {
 221  0
         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  0
         if (!viewable && this.editable) {
 234  0
             this.editable = false;
 235  
         }
 236  0
         this.viewable = viewable;
 237  0
     }
 238  
 
 239  
     /**
 240  
      * @see java.lang.Object#toString()
 241  
      */
 242  
     public String toString() {
 243  0
         StringBuffer sb = new StringBuffer();
 244  0
         sb.append(this.fieldName);
 245  0
         sb.append(" [");
 246  0
         if (this.editable) {
 247  0
             sb.append("editable");
 248  
         }
 249  
         else {
 250  0
             sb.append("not editable");
 251  
         }
 252  0
         sb.append(",");
 253  0
         if (this.viewable) {
 254  0
             sb.append("viewable");
 255  
         }
 256  
         else {
 257  0
             sb.append("not viewable");
 258  
         }
 259  0
         sb.append("]");
 260  0
         return sb.toString();
 261  
     }
 262  
 
 263  
     /**
 264  
      * @see java.lang.Object#equals(java.lang.Object)
 265  
      */
 266  
     public boolean equals(Object obj) {
 267  0
         boolean equal = false;
 268  
 
 269  0
         if (obj != null) {
 270  0
             if (this.getClass().equals(obj.getClass())) {
 271  0
                 FieldRestriction other = (FieldRestriction) obj;
 272  
 
 273  0
                 if (StringUtils.equals(this.fieldName, other.getFieldName())) {
 274  0
                     if (this.editable == other.isEditable() && this.viewable == other.isViewable()) {
 275  0
                         equal = true;
 276  
                     }
 277  
                 }
 278  
             }
 279  
         }
 280  
 
 281  0
         return equal;
 282  
     }
 283  
 
 284  
     /**
 285  
      * @see java.lang.Object#hashCode()
 286  
      */
 287  
     public int hashCode() {
 288  0
         return toString().hashCode();
 289  
     }
 290  
 
 291  
         /**
 292  
          * @return the masked
 293  
          */
 294  
         public boolean isMasked() {
 295  0
                 return this.masked;
 296  
         }
 297  
 
 298  
         /**
 299  
          * @return the partiallyMasked
 300  
          */
 301  
         public boolean isPartiallyMasked() {
 302  0
                 return this.partiallyMasked;
 303  
         }
 304  
 
 305  
         
 306  
         /**
 307  
          * @return the shouldBeEncrypted
 308  
          */
 309  
         public boolean isShouldBeEncrypted() {
 310  0
                 return this.shouldBeEncrypted;
 311  
         }
 312  
 
 313  
         /**
 314  
          * @param shouldBeEncrypted the shouldBeEncrypted to set
 315  
          */
 316  
         public void setShouldBeEncrypted(boolean shouldBeEncrypted) {
 317  0
                 this.shouldBeEncrypted = shouldBeEncrypted;
 318  0
         }
 319  
 
 320  
         /**
 321  
          * @return the maskFormatter
 322  
          */
 323  
         public MaskFormatter getMaskFormatter() {
 324  0
                 return this.maskFormatter;
 325  
         }
 326  
 
 327  
         /**
 328  
          * @param maskFormatter the maskFormatter to set
 329  
          */
 330  
         public void setMaskFormatter(MaskFormatter maskFormatter) {
 331  0
                 this.maskFormatter = maskFormatter;
 332  0
         }
 333  
         
 334  
         
 335  
 }