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.krad.kim;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.kew.api.KewApiConstants;
020import org.kuali.rice.kim.api.KimConstants;
021import org.kuali.rice.kim.api.permission.Permission;
022import org.kuali.rice.kim.impl.permission.PermissionBo;
023
024import java.util.ArrayList;
025import java.util.List;
026import java.util.Map;
027
028/**
029 * 
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 *
032 */
033public class DocumentTypeAndNodeAndRouteStatusPermissionTypeServiceImpl extends DocumentTypePermissionTypeServiceImpl {
034
035    @Override
036    protected boolean isCheckRequiredAttributes() {
037        return true;
038    }
039
040        /**
041         * 
042         *      consider the document type hierarchy - check for a permission that just specifies the document type first at each level 
043         *      - then if you don't find that, check for the doc type and the node, then the doc type and the Action Type.
044         *
045         */
046        @Override
047        protected List<Permission> performPermissionMatches(Map<String, String> requestedDetails,
048                        List<Permission> permissionsList) {
049
050        List<Permission> matchingPermissions = new ArrayList<Permission>();
051                // loop over the permissions, checking the non-document-related ones
052                for ( Permission kpi : permissionsList ) {
053            PermissionBo bo = PermissionBo.from(kpi);
054                        if ( (routeNodeMatches(requestedDetails, bo.getDetails())) &&
055                                 (routeStatusMatches(requestedDetails, bo.getDetails())) ) {
056                                matchingPermissions.add( kpi );
057                        }                       
058                }
059                // now, filter the list to just those for the current document
060                matchingPermissions = super.performPermissionMatches( requestedDetails, matchingPermissions );
061                return matchingPermissions;
062        }
063                
064        protected boolean routeNodeMatches(Map<String, String> requestedDetails, Map<String, String> permissionDetails) {
065        if ( StringUtils.isBlank( permissionDetails.get(KimConstants.AttributeConstants.ROUTE_NODE_NAME) ) ) {
066            return true;
067        }
068        return StringUtils.equals(requestedDetails.get(KimConstants.AttributeConstants.ROUTE_NODE_NAME), permissionDetails.get(
069                KimConstants.AttributeConstants.ROUTE_NODE_NAME));
070    }
071
072    protected boolean routeStatusMatches(Map<String, String> requestedDetails, Map<String, String> permissionDetails) {
073        if ( (StringUtils.isBlank(permissionDetails.get(KimConstants.AttributeConstants.ROUTE_STATUS_CODE))) &&
074             (!(StringUtils.equals(KewApiConstants.ROUTE_HEADER_INITIATED_CD, requestedDetails.get(KimConstants.AttributeConstants.ROUTE_STATUS_CODE))))) {
075            return true;
076        }
077        return StringUtils.equals( requestedDetails.get(KimConstants.AttributeConstants.ROUTE_STATUS_CODE), permissionDetails.get(
078                KimConstants.AttributeConstants.ROUTE_STATUS_CODE));
079    }
080}