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 java.util.HashSet;
19  import java.util.Set;
20  
21  import org.kuali.rice.kns.document.authorization.FieldRestriction;
22  import org.kuali.rice.kns.document.authorization.InquiryOrMaintenanceDocumentRestrictionsBase;
23  import org.kuali.rice.kns.document.authorization.MaintenanceDocumentRestrictions;
24  import org.kuali.rice.kns.web.ui.Field;
25  
26  public class MaintenanceDocumentRestrictionsBase extends InquiryOrMaintenanceDocumentRestrictionsBase implements MaintenanceDocumentRestrictions {
27  	private Set<String> readOnlyFields;
28  	private Set<String> readOnlySectionIds;
29  	
30  	public MaintenanceDocumentRestrictionsBase() {
31  	}
32  	
33  	public void addReadOnlyField(String fieldName) {
34  		readOnlyFields.add(fieldName);
35  	}
36  
37  	public void addReadOnlySectionId(String sectionId) {
38  		readOnlySectionIds.add(sectionId);
39  	}
40  
41  	public Set<String> getReadOnlySectionIds() {
42  		return readOnlySectionIds;
43  	}
44  
45  	@Override
46  	public FieldRestriction getFieldRestriction(String fieldName) {
47  		FieldRestriction fieldRestriction = super
48  				.getFieldRestriction(fieldName);
49  		if (fieldRestriction == null && isReadOnlyField(fieldName)) {
50  			fieldRestriction = new FieldRestriction(fieldName, Field.READONLY);
51  		}
52  		// TODO: next block could probably be removed since the superclass would return null for a read-only field 
53  		if (Field.EDITABLE
54  				.equals(fieldRestriction.getKualiFieldDisplayFlag())
55  				&& isReadOnlyField(fieldName)) {
56  			fieldRestriction = new FieldRestriction(fieldName,
57  					Field.READONLY);
58  		}
59  		return fieldRestriction;
60  	}
61  
62  	@Override
63  	public void clearAllRestrictions() {
64  		super.clearAllRestrictions();
65  		readOnlyFields = new HashSet<String>();
66  		readOnlySectionIds = new HashSet<String>();
67  	}
68  
69  	protected boolean isReadOnlyField(String fieldName) {
70  		String normalizedFieldName = normalizeFieldName(fieldName);
71  		return readOnlyFields.contains(normalizedFieldName);
72  	}
73  
74  	/**
75  	 * @see org.kuali.rice.krad.authorization.InquiryOrMaintenanceDocumentRestrictionsBase#hasAnyFieldRestrictions()
76  	 */
77  	@Override
78  	public boolean hasAnyFieldRestrictions() {
79  		return super.hasAnyFieldRestrictions() || !readOnlyFields.isEmpty();
80  	}
81  
82  	/**
83  	 * @see org.kuali.rice.krad.authorization.InquiryOrMaintenanceDocumentRestrictionsBase#hasRestriction(java.lang.String)
84  	 */
85  	@Override
86  	public boolean hasRestriction(String fieldName) {
87  		return super.hasRestriction(fieldName) || isReadOnlyField(fieldName);
88  	}
89  	
90  	/**
91  	 * @see org.kuali.rice.kns.document.authorization.MaintenanceDocumentRestrictions#isReadOnlySectionId(java.lang.String)
92  	 */
93  	public boolean isReadOnlySectionId(String sectionId) {
94  		return readOnlySectionIds.contains(sectionId);
95  	}
96  }