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.service.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
020import org.kuali.rice.kim.api.KimConstants;
021import org.kuali.rice.kim.api.permission.Permission;
022import org.kuali.rice.kim.impl.permission.PermissionBo;
023import org.kuali.rice.kns.kim.permission.PermissionTypeServiceBase;
024
025import java.util.ArrayList;
026import java.util.List;
027import java.util.Map;
028
029/**
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 *
032 * @deprecated Only used by KNS classes, use KRAD.
033 */
034@Deprecated
035public class ComponentFieldPermissionTypeServiceImpl extends PermissionTypeServiceBase {
036        
037        /**
038         * Compare the component and property names between the request and matching permissions.
039         * Make entries with a matching property name take precedence over those with blank property 
040         * names on the stored permissions.  Only match entries with blank property names if
041         * no entries match on the exact property name. 
042         */
043        @Override
044        protected List<Permission> performPermissionMatches(Map<String, String> requestedDetails,
045                        List<Permission> permissionsList) {
046
047        List<Permission> propertyMatches = new ArrayList<Permission>();
048                List<Permission> prefixPropertyMatches = new ArrayList<Permission>();
049                List<Permission> blankPropertyMatches = new ArrayList<Permission>();
050                String propertyName = requestedDetails.get(KimConstants.AttributeConstants.PROPERTY_NAME);
051                String componentName = requestedDetails.get(KimConstants.AttributeConstants.COMPONENT_NAME);
052                for ( Permission kpi : permissionsList ) {
053            PermissionBo bo = PermissionBo.from(kpi);
054                        if ( StringUtils.equals( componentName, bo.getDetails().get( KimConstants.AttributeConstants.COMPONENT_NAME ) ) ) {
055                                String permPropertyName = bo.getDetails().get(KimConstants.AttributeConstants.PROPERTY_NAME);
056                                if ( StringUtils.isBlank( permPropertyName ) ) {
057                                        blankPropertyMatches.add( kpi );
058                                } else if ( StringUtils.equals( propertyName, permPropertyName ) ) {
059                                        propertyMatches.add( kpi );
060                                } else if ( doesPropertyNameMatch(propertyName, permPropertyName) ) {
061                                        prefixPropertyMatches.add( kpi );
062                                }
063                        }
064                }
065                if ( !propertyMatches.isEmpty() ) {
066                        return propertyMatches;
067                } else if ( !prefixPropertyMatches.isEmpty() ) {
068                        return prefixPropertyMatches;
069                } else {
070                        return blankPropertyMatches;
071                }
072        }
073
074}