001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kns.document.authorization;
017
018import java.util.HashSet;
019import java.util.Set;
020
021import org.kuali.rice.kns.document.authorization.FieldRestriction;
022import org.kuali.rice.kns.document.authorization.InquiryOrMaintenanceDocumentRestrictionsBase;
023import org.kuali.rice.kns.document.authorization.MaintenanceDocumentRestrictions;
024import org.kuali.rice.kns.web.ui.Field;
025
026public class MaintenanceDocumentRestrictionsBase extends InquiryOrMaintenanceDocumentRestrictionsBase implements MaintenanceDocumentRestrictions {
027        private Set<String> readOnlyFields;
028        private Set<String> readOnlySectionIds;
029        
030        public MaintenanceDocumentRestrictionsBase() {
031        }
032        
033        public void addReadOnlyField(String fieldName) {
034                readOnlyFields.add(fieldName);
035        }
036
037        public void addReadOnlySectionId(String sectionId) {
038                readOnlySectionIds.add(sectionId);
039        }
040
041        public Set<String> getReadOnlySectionIds() {
042                return readOnlySectionIds;
043        }
044
045        @Override
046        public FieldRestriction getFieldRestriction(String fieldName) {
047                FieldRestriction fieldRestriction = super
048                                .getFieldRestriction(fieldName);
049                if (fieldRestriction == null && isReadOnlyField(fieldName)) {
050                        fieldRestriction = new FieldRestriction(fieldName, Field.READONLY);
051                }
052                // TODO: next block could probably be removed since the superclass would return null for a read-only field 
053                if (Field.EDITABLE
054                                .equals(fieldRestriction.getKualiFieldDisplayFlag())
055                                && isReadOnlyField(fieldName)) {
056                        fieldRestriction = new FieldRestriction(fieldName,
057                                        Field.READONLY);
058                }
059                return fieldRestriction;
060        }
061
062        @Override
063        public void clearAllRestrictions() {
064                super.clearAllRestrictions();
065                readOnlyFields = new HashSet<String>();
066                readOnlySectionIds = new HashSet<String>();
067        }
068
069        protected boolean isReadOnlyField(String fieldName) {
070                String normalizedFieldName = normalizeFieldName(fieldName);
071                return readOnlyFields.contains(normalizedFieldName);
072        }
073
074        /**
075         * @see org.kuali.rice.krad.authorization.InquiryOrMaintenanceDocumentRestrictionsBase#hasAnyFieldRestrictions()
076         */
077        @Override
078        public boolean hasAnyFieldRestrictions() {
079                return super.hasAnyFieldRestrictions() || !readOnlyFields.isEmpty();
080        }
081
082        /**
083         * @see org.kuali.rice.krad.authorization.InquiryOrMaintenanceDocumentRestrictionsBase#hasRestriction(java.lang.String)
084         */
085        @Override
086        public boolean hasRestriction(String fieldName) {
087                return super.hasRestriction(fieldName) || isReadOnlyField(fieldName);
088        }
089        
090        /**
091         * @see org.kuali.rice.kns.document.authorization.MaintenanceDocumentRestrictions#isReadOnlySectionId(java.lang.String)
092         */
093        public boolean isReadOnlySectionId(String sectionId) {
094                return readOnlySectionIds.contains(sectionId);
095        }
096}