View Javadoc

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