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 org.kuali.rice.kns.web.ui.Field;
019import org.kuali.rice.krad.datadictionary.mask.MaskFormatter;
020
021import java.util.HashMap;
022import java.util.Map;
023import java.util.Set;
024
025public class BusinessObjectRestrictionsBase implements
026                BusinessObjectRestrictions {
027        private Map<String, MaskFormatter> partiallyMaskedFields;
028        private Map<String, MaskFormatter> fullyMaskedFields;
029
030        protected Set<String> allRestrictedFields;
031
032        public BusinessObjectRestrictionsBase() {
033                clearAllRestrictions();
034        }
035
036        public boolean hasAnyFieldRestrictions() {
037                return !partiallyMaskedFields.isEmpty() || !fullyMaskedFields.isEmpty();
038        }
039
040        public boolean hasRestriction(String fieldName) {
041                return isPartiallyMaskedField(fieldName) || isFullyMaskedField(fieldName);
042        }
043
044        public void addFullyMaskedField(String fieldName,
045                        MaskFormatter maskFormatter) {
046                fullyMaskedFields.put(fieldName, maskFormatter);
047        }
048
049        public void addPartiallyMaskedField(String fieldName,
050                        MaskFormatter maskFormatter) {
051                partiallyMaskedFields.put(fieldName, maskFormatter);
052        }
053
054        /**
055         * 
056         * This method returns the authorization setting for the given field name.
057         * If the field name is not restricted in any way, a default full-editable
058         * value is returned.
059         * 
060         * @param fieldName
061         *            - name of field to get authorization restrictions for.
062         * @return a populated FieldAuthorization class for this field
063         * 
064         */
065        public FieldRestriction getFieldRestriction(String fieldName) {
066                if (hasRestriction(fieldName)) {
067                        FieldRestriction fieldRestriction = null;
068                        if (isPartiallyMaskedField(fieldName)) {
069                                fieldRestriction = new FieldRestriction(fieldName,
070                                                Field.PARTIALLY_MASKED);
071                                fieldRestriction.setMaskFormatter(partiallyMaskedFields
072                                                .get(normalizeFieldName(fieldName)));
073                        }
074                        if (isFullyMaskedField(fieldName)) {
075                                fieldRestriction = new FieldRestriction(fieldName, Field.MASKED);
076                                fieldRestriction.setMaskFormatter(fullyMaskedFields
077                                                .get(normalizeFieldName(fieldName)));
078                        }
079                        return fieldRestriction;
080                } else {
081                        return new FieldRestriction(fieldName, Field.EDITABLE);
082                }
083        }
084
085        public void clearAllRestrictions() {
086                partiallyMaskedFields = new HashMap<String, MaskFormatter>();
087                fullyMaskedFields = new HashMap<String, MaskFormatter>();
088                allRestrictedFields = null;
089        }
090        
091        
092        /**
093         * This method is used to convert field names on forms into a format that's compatible with field names
094         * that are registered with a restriction.  The base implementation of this method just returns the string.
095         * 
096         * @param fieldName The field name that would be rendered on a form
097         * @return
098         */
099        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}