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.kuali.rice.kim.api.KimConstants;
019import org.kuali.rice.kim.api.permission.Permission;
020import org.kuali.rice.kim.impl.permission.PermissionBo;
021import org.kuali.rice.krad.kim.DocumentTypePermissionTypeServiceImpl;
022
023import java.util.ArrayList;
024import java.util.Iterator;
025import java.util.List;
026import java.util.Map;
027
028/**
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031public class DocumentTypeAndAttachmentTypePermissionTypeService extends DocumentTypePermissionTypeServiceImpl {
032
033        @Override
034        protected List<Permission> performPermissionMatches(
035                        Map<String, String> requestedDetails,
036                        List<Permission> permissionsList) {
037
038                List<Permission> matchingPermissions = new ArrayList<Permission>();
039                if (requestedDetails == null) {
040                        return matchingPermissions; // empty list
041                }
042                // loop over the permissions, checking the non-document-related ones
043                for (Permission kimPermissionInfo : permissionsList) {
044            PermissionBo bo = PermissionBo.from(kimPermissionInfo);
045                        if (!bo.getDetails().containsKey(
046                                                KimConstants.AttributeConstants.ATTACHMENT_TYPE_CODE)
047                          || bo.getDetails().get(KimConstants.AttributeConstants.ATTACHMENT_TYPE_CODE)
048                                 .equals(requestedDetails.get(KimConstants.AttributeConstants.ATTACHMENT_TYPE_CODE)))
049                        {
050                                matchingPermissions.add(kimPermissionInfo);
051                        }
052
053                }
054                // now, filter the list to just those for the current document
055                matchingPermissions = super.performPermissionMatches(requestedDetails,
056                                matchingPermissions);
057                
058                // if we have more than one permission for the document, then we need to check if any have an attachment type code
059                // if so, we throw away the "less specific" one without an attachment type code
060                if ( matchingPermissions.size() > 1 ) {
061                        Iterator<Permission> permIter = matchingPermissions.iterator();
062                        while ( permIter.hasNext() ) {
063                                if ( !permIter.next().getAttributes().containsKey(KimConstants.AttributeConstants.ATTACHMENT_TYPE_CODE) ) {
064                                        permIter.remove();
065                                }
066                        }
067                }
068                
069                return matchingPermissions;
070        }
071}