001/**
002 * Copyright 2005-2015 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
026/**
027 * @deprecated Only used in KNS classes, use KRAD.
028 */
029@Deprecated
030public class MaintenanceDocumentRestrictionsBase extends InquiryOrMaintenanceDocumentRestrictionsBase implements MaintenanceDocumentRestrictions {
031        private Set<String> readOnlyFields;
032        private Set<String> readOnlySectionIds;
033        
034        public MaintenanceDocumentRestrictionsBase() {
035        }
036        
037        public void addReadOnlyField(String fieldName) {
038                readOnlyFields.add(fieldName);
039        }
040
041        public void addReadOnlySectionId(String sectionId) {
042                readOnlySectionIds.add(sectionId);
043        }
044
045        public Set<String> getReadOnlySectionIds() {
046                return readOnlySectionIds;
047        }
048
049        @Override
050        public FieldRestriction getFieldRestriction(String fieldName) {
051                FieldRestriction fieldRestriction = super
052                                .getFieldRestriction(fieldName);
053                if (fieldRestriction == null && isReadOnlyField(fieldName)) {
054                        fieldRestriction = new FieldRestriction(fieldName, Field.READONLY);
055                }
056                // TODO: next block could probably be removed since the superclass would return null for a read-only field 
057                if (Field.EDITABLE
058                                .equals(fieldRestriction.getKualiFieldDisplayFlag())
059                                && isReadOnlyField(fieldName)) {
060                        fieldRestriction = new FieldRestriction(fieldName,
061                                        Field.READONLY);
062                }
063                return fieldRestriction;
064        }
065
066        @Override
067        public void clearAllRestrictions() {
068                super.clearAllRestrictions();
069                readOnlyFields = new HashSet<String>();
070                readOnlySectionIds = new HashSet<String>();
071        }
072
073        protected boolean isReadOnlyField(String fieldName) {
074                String normalizedFieldName = normalizeFieldName(fieldName);
075                return readOnlyFields.contains(normalizedFieldName);
076        }
077
078        /**
079         * @see org.kuali.rice.krad.authorization.InquiryOrMaintenanceDocumentRestrictionsBase#hasAnyFieldRestrictions()
080         */
081        @Override
082        public boolean hasAnyFieldRestrictions() {
083                return super.hasAnyFieldRestrictions() || !readOnlyFields.isEmpty();
084        }
085
086        /**
087         * @see org.kuali.rice.krad.authorization.InquiryOrMaintenanceDocumentRestrictionsBase#hasRestriction(java.lang.String)
088         */
089        @Override
090        public boolean hasRestriction(String fieldName) {
091                return super.hasRestriction(fieldName) || isReadOnlyField(fieldName);
092        }
093        
094        /**
095         * @see org.kuali.rice.kns.document.authorization.MaintenanceDocumentRestrictions#isReadOnlySectionId(java.lang.String)
096         */
097        public boolean isReadOnlySectionId(String sectionId) {
098                return readOnlySectionIds.contains(sectionId);
099        }
100}