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.kim.api.KimConstants;
020import org.kuali.rice.kim.api.permission.Permission;
021import org.kuali.rice.kim.impl.permission.PermissionBo;
022import org.kuali.rice.krad.kim.DocumentTypePermissionTypeServiceImpl;
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 DocumentTypeAndNodeOrStatePermissionTypeServiceImpl extends DocumentTypePermissionTypeServiceImpl {
034
035        /**
036     *  Permission type service which can check the route node and status as well as the document hierarchy.
037     *  
038     *  Permission should be able to (in addition to taking the routingStatus, routingNote, and documentTypeName attributes) 
039     *  should take a documentNumber and retrieve those values from workflow before performing the comparison.
040     *
041     *  consider the document type hierarchy - check for a permission that just specifies the document type first at each level 
042     *  - then if you don't find that, check for the doc type and the node, then the doc type and the state. 
043     *
044         */
045    @Override
046    protected List<Permission> performPermissionMatches(Map<String, String> requestedDetails,
047            List<Permission> permissionsList) {
048        List<Permission> matchingPermissions = new ArrayList<Permission>();
049        // loop over the permissions, checking the non-document-related ones
050        for ( Permission kpi : permissionsList ) {
051            PermissionBo bo = PermissionBo.from(kpi);
052            if ( routeNodeMatches(requestedDetails, bo.getDetails()) &&
053                 routeStatusMatches(requestedDetails, bo.getDetails()) &&
054                 appDocStatusMatches(requestedDetails, bo.getDetails()) ) {
055                matchingPermissions.add( kpi );
056            }           
057        }
058        // now, filter the list to just those for the current document
059        matchingPermissions = super.performPermissionMatches( requestedDetails, matchingPermissions );
060        return matchingPermissions;
061    }
062        
063        protected boolean routeNodeMatches(Map<String, String> requestedDetails, Map<String, String> permissionDetails) {
064                if ( StringUtils.isBlank( permissionDetails.get(KimConstants.AttributeConstants.ROUTE_NODE_NAME) ) ) {
065                        return true;
066                }
067                return StringUtils.equals( requestedDetails.get(KimConstants.AttributeConstants.ROUTE_NODE_NAME), permissionDetails.get(KimConstants.AttributeConstants.ROUTE_NODE_NAME));
068        }
069        
070        protected boolean routeStatusMatches(Map<String, String> requestedDetails, Map<String, String> permissionDetails) {
071                if ( StringUtils.isBlank( permissionDetails.get(KimConstants.AttributeConstants.ROUTE_STATUS_CODE) ) ) {
072                        return true;
073                }
074                return StringUtils.equals( requestedDetails.get(KimConstants.AttributeConstants.ROUTE_STATUS_CODE), permissionDetails.get(
075                KimConstants.AttributeConstants.ROUTE_STATUS_CODE));
076        }
077
078    protected boolean appDocStatusMatches(Map<String, String> requestedDetails, Map<String, String> permissionDetails) {
079        if ( StringUtils.isBlank( permissionDetails.get(KimConstants.AttributeConstants.APP_DOC_STATUS) ) ) {
080            return true;
081        }
082        return StringUtils.equals( requestedDetails.get(KimConstants.AttributeConstants.APP_DOC_STATUS), permissionDetails.get(
083                KimConstants.AttributeConstants.APP_DOC_STATUS));
084    }
085
086}