Coverage Report - org.kuali.rice.krad.service.impl.DocumentTypePermissionTypeServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentTypePermissionTypeServiceImpl
0%
0/23
0%
0/16
3
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.krad.service.impl;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.kew.api.KewApiServiceLocator;
 20  
 import org.kuali.rice.kew.api.doctype.DocumentType;
 21  
 import org.kuali.rice.kew.api.doctype.DocumentTypeService;
 22  
 import org.kuali.rice.kim.api.KimConstants;
 23  
 import org.kuali.rice.kim.api.permission.Permission;
 24  
 import org.kuali.rice.kim.impl.permission.PermissionBo;
 25  
 import org.kuali.rice.kns.kim.permission.PermissionTypeServiceBase;
 26  
 
 27  
 import java.util.ArrayList;
 28  
 import java.util.Collections;
 29  
 import java.util.HashSet;
 30  
 import java.util.List;
 31  
 import java.util.Map;
 32  
 import java.util.Set;
 33  
 
 34  
 /**
 35  
  * This is a description of what this class does - mpham don't forget to fill
 36  
  * this in.
 37  
  * 
 38  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 39  
  * 
 40  
  */
 41  0
 public class DocumentTypePermissionTypeServiceImpl extends PermissionTypeServiceBase {
 42  
         protected transient DocumentTypeService documentTypeService;
 43  
 
 44  
     @Override
 45  
     protected List<String> getRequiredAttributes() {
 46  0
         return Collections.singletonList(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME);
 47  
     }
 48  
 
 49  
     @Override
 50  
     protected boolean isCheckRequiredAttributes() {
 51  0
         return true;
 52  
     }
 53  
 
 54  
         /**
 55  
          * Loops over the given permissions and returns the most specific permission that matches.
 56  
          * 
 57  
          * That is, if a permission exists for the document type, then the permission for any 
 58  
          * parent document will not be considered/returned.
 59  
          *
 60  
          */
 61  
         @Override
 62  
         protected List<Permission> performPermissionMatches(Map<String, String> requestedDetails,
 63  
             List<Permission> permissionsList) {
 64  
                 // pull all the potential parent doc type names from the permission list
 65  0
                 Set<String> permissionDocTypeNames = new HashSet<String>( permissionsList.size() );
 66  0
                 for ( Permission permission : permissionsList ) {
 67  0
             PermissionBo bo = PermissionBo.from(permission);
 68  0
                         String docTypeName = bo.getDetails().get( KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME );
 69  0
                         if ( StringUtils.isNotBlank( docTypeName ) ) {
 70  0
                                 permissionDocTypeNames.add( docTypeName );
 71  
                         }
 72  0
                 }
 73  
                 // find the parent documents which match
 74  0
                 DocumentType docType = getDocumentTypeService().getDocumentTypeByName(requestedDetails.get(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME));
 75  0
                 String matchingDocTypeName = getClosestParentDocumentTypeName(docType, permissionDocTypeNames);
 76  
                 // re-loop over the permissions and build a new list of the ones which have the
 77  
                 // matching document type names in their details
 78  0
                 List<Permission> matchingPermissions = new ArrayList<Permission>();
 79  0
                 for ( Permission kpi : permissionsList ) {
 80  0
             PermissionBo bo = PermissionBo.from(kpi);
 81  0
                         String docTypeName = bo.getDetails().get( KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME );
 82  
                         // only allow a match on the "*" type if no matching document types were found
 83  0
                         if((StringUtils.isEmpty(matchingDocTypeName) && StringUtils.equals(docTypeName,"*")) 
 84  
                                 || (StringUtils.isNotEmpty(matchingDocTypeName) && matchingDocTypeName.equals(docTypeName))) {
 85  0
                                 matchingPermissions.add( kpi );
 86  
                         }
 87  0
                 }
 88  
 
 89  0
                 return matchingPermissions;
 90  
         }
 91  
         
 92  
         protected DocumentTypeService getDocumentTypeService() {
 93  0
                 if ( documentTypeService == null ) {
 94  0
                         documentTypeService = KewApiServiceLocator.getDocumentTypeService();
 95  
                 }
 96  0
                 return this.documentTypeService;
 97  
         }
 98  
 
 99  
 }