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.kuali.rice.kns.web.ui.Field;
19  import org.kuali.rice.krad.datadictionary.mask.MaskFormatter;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.Set;
24  
25  public class BusinessObjectRestrictionsBase implements
26  		BusinessObjectRestrictions {
27  	private Map<String, MaskFormatter> partiallyMaskedFields;
28  	private Map<String, MaskFormatter> fullyMaskedFields;
29  
30  	protected Set<String> allRestrictedFields;
31  
32  	public BusinessObjectRestrictionsBase() {
33  		clearAllRestrictions();
34  	}
35  
36  	public boolean hasAnyFieldRestrictions() {
37  		return !partiallyMaskedFields.isEmpty() || !fullyMaskedFields.isEmpty();
38  	}
39  
40  	public boolean hasRestriction(String fieldName) {
41  		return isPartiallyMaskedField(fieldName) || isFullyMaskedField(fieldName);
42  	}
43  
44  	public void addFullyMaskedField(String fieldName,
45  			MaskFormatter maskFormatter) {
46  		fullyMaskedFields.put(fieldName, maskFormatter);
47  	}
48  
49  	public void addPartiallyMaskedField(String fieldName,
50  			MaskFormatter maskFormatter) {
51  		partiallyMaskedFields.put(fieldName, maskFormatter);
52  	}
53  
54  	/**
55  	 * 
56  	 * This method returns the authorization setting for the given field name.
57  	 * If the field name is not restricted in any way, a default full-editable
58  	 * value is returned.
59  	 * 
60  	 * @param fieldName
61  	 *            - name of field to get authorization restrictions for.
62  	 * @return a populated FieldAuthorization class for this field
63  	 * 
64  	 */
65  	public FieldRestriction getFieldRestriction(String fieldName) {
66  		if (hasRestriction(fieldName)) {
67  			FieldRestriction fieldRestriction = null;
68  			if (isPartiallyMaskedField(fieldName)) {
69  				fieldRestriction = new FieldRestriction(fieldName,
70  						Field.PARTIALLY_MASKED);
71  				fieldRestriction.setMaskFormatter(partiallyMaskedFields
72  						.get(normalizeFieldName(fieldName)));
73  			}
74  			if (isFullyMaskedField(fieldName)) {
75  				fieldRestriction = new FieldRestriction(fieldName, Field.MASKED);
76  				fieldRestriction.setMaskFormatter(fullyMaskedFields
77  						.get(normalizeFieldName(fieldName)));
78  			}
79  			return fieldRestriction;
80  		} else {
81  			return new FieldRestriction(fieldName, Field.EDITABLE);
82  		}
83  	}
84  
85  	public void clearAllRestrictions() {
86  		partiallyMaskedFields = new HashMap<String, MaskFormatter>();
87  		fullyMaskedFields = new HashMap<String, MaskFormatter>();
88  		allRestrictedFields = null;
89  	}
90  	
91  	
92  	/**
93  	 * This method is used to convert field names on forms into a format that's compatible with field names
94  	 * that are registered with a restriction.  The base implementation of this method just returns the string.
95  	 * 
96  	 * @param fieldName The field name that would be rendered on a form
97  	 * @return
98  	 */
99  	protected String normalizeFieldName(String fieldName) {
100 		return fieldName;
101 	}
102 	
103 	protected boolean isFullyMaskedField(String fieldName) {
104 		String normalizedFieldName = normalizeFieldName(fieldName);
105 		return fullyMaskedFields.containsKey(normalizedFieldName);
106 	}
107 	
108 	protected boolean isPartiallyMaskedField(String fieldName) {
109 		String normalizedFieldName = normalizeFieldName(fieldName);
110 		return partiallyMaskedFields.containsKey(normalizedFieldName);
111 	}
112 }