Coverage Report - org.kuali.student.lum.kim.role.type.KSRouteLogDerivedRoleTypeServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
KSRouteLogDerivedRoleTypeServiceImpl
0%
0/77
0%
0/38
3.556
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.lum.kim.role.type;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashSet;
 20  
 import java.util.List;
 21  
 import java.util.Set;
 22  
 
 23  
 import org.apache.commons.lang.StringUtils;
 24  
 import org.kuali.rice.kew.exception.WorkflowException;
 25  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 26  
 import org.kuali.rice.kew.service.WorkflowUtility;
 27  
 import org.kuali.rice.kim.bo.Role;
 28  
 import org.kuali.rice.kim.bo.impl.KimAttributes;
 29  
 import org.kuali.rice.kim.bo.role.dto.RoleMembershipInfo;
 30  
 import org.kuali.rice.kim.bo.types.dto.AttributeSet;
 31  
 import org.kuali.rice.kim.service.support.impl.KimDerivedRoleTypeServiceBase;
 32  
 import org.kuali.student.common.rice.StudentIdentityConstants;
 33  
 import org.kuali.student.lum.kim.KimQualificationHelper;
 34  
 
 35  
 /**
 36  
  *
 37  
  */
 38  0
 public class KSRouteLogDerivedRoleTypeServiceImpl extends KimDerivedRoleTypeServiceBase {
 39  0
     private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KSRouteLogDerivedRoleTypeServiceImpl.class);
 40  
 
 41  
     public static final String INITIATOR_ROLE_NAME = "Initiator";
 42  
     public static final String INITIATOR_OR_REVIEWER_ROLE_NAME = "Initiator or Reviewer";
 43  
     public static final String ROUTER_ROLE_NAME = "Router";
 44  
 
 45  
     public static final String DERIVED_INITIATOR_ROLE_NAME = "Derived Role: Initiator";
 46  
     public static final String DERIVED_INITIATOR_OR_REVIEWER_ROLE_NAME = "Derived Role: Initiator or Reviewer";
 47  
     public static final String DERIVED_ROUTER_ROLE_NAME = "Derived Role: Router";
 48  
     
 49  0
     private boolean checkFutureRequests = false;
 50  0
         protected Set<List<String>> newRequiredAttributes = new HashSet<List<String>>();
 51  
 
 52  
         {
 53  0
                 checkRequiredAttributes = true;
 54  
         // add document number as one required attribute set
 55  0
                 List<String> listOne = new ArrayList<String>();
 56  0
                 listOne.add( KimAttributes.DOCUMENT_NUMBER );
 57  0
                 newRequiredAttributes.add(listOne);
 58  
         // add document type name and KEW application id as one required attribute set
 59  0
                 List<String> listTwo = new ArrayList<String>();
 60  0
                 listTwo.add( KimAttributes.DOCUMENT_TYPE_NAME );
 61  0
                 listTwo.add( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_ID );
 62  0
                 newRequiredAttributes.add(listTwo);
 63  
         // add object id and object type as one required attribute set
 64  0
                 List<String> listThree = new ArrayList<String>();
 65  0
                 listThree.add( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_ID );
 66  0
                 listThree.add( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_TYPE );
 67  0
                 newRequiredAttributes.add(listThree);
 68  
         // add each proposal reference type as a required attribute set
 69  0
         for (String proposalReferenceType : StudentIdentityConstants.QUALIFICATION_PROPOSAL_ID_REF_TYPES) {
 70  0
             List<String> tempList = new ArrayList<String>();
 71  0
             tempList.add( proposalReferenceType );
 72  0
             newRequiredAttributes.add(tempList);
 73  0
         }
 74  0
         }
 75  
 
 76  
         /** 
 77  
          * The part about where the receivedAttributes list being empty does not return errors is copied from Rice base class.
 78  
          * 
 79  
          * @see org.kuali.rice.kim.service.support.impl.KimTypeServiceBase#validateRequiredAttributesAgainstReceived(org.kuali.rice.kim.bo.types.dto.AttributeSet)
 80  
          **/
 81  
         @Override
 82  
         protected void validateRequiredAttributesAgainstReceived(AttributeSet receivedAttributes){
 83  0
                 KimQualificationHelper.validateRequiredAttributesAgainstReceived(newRequiredAttributes, receivedAttributes, isCheckFutureRequests(), COMMA_SEPARATOR);
 84  0
         super.validateRequiredAttributesAgainstReceived(receivedAttributes);
 85  0
         }
 86  
 
 87  
     @Override
 88  
     public AttributeSet translateInputAttributeSet(AttributeSet qualification) {
 89  0
         return KimQualificationHelper.translateInputAttributeSet(super.translateInputAttributeSet(qualification));
 90  
     }
 91  
 
 92  
         protected Long getDocumentNumber(AttributeSet qualification) throws WorkflowException {
 93  
                 // first check for a valid document id passed in
 94  0
                 String documentId = qualification.get( KimAttributes.DOCUMENT_NUMBER );
 95  0
         if (StringUtils.isNotEmpty(documentId)) {
 96  0
             return Long.valueOf(documentId);
 97  
         } else {
 98  0
             LOG.warn("Could not find workflow document id in qualification list:");
 99  0
             LOG.warn(qualification.formattedDump(20));
 100  0
             return null;
 101  
         }
 102  
 //                if (StringUtils.isNotEmpty(documentId)) {
 103  
 //                        return Long.valueOf(documentId);
 104  
 //                }
 105  
 //                // if no document id passed in get the document via the id and document type name
 106  
 //                String documentTypeName = qualification.get( KimAttributes.DOCUMENT_TYPE_NAME );
 107  
 //                if (StringUtils.isEmpty(documentTypeName)) {
 108  
 //                        String ksObjectType = qualification.get( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_TYPE );
 109  
 //                        if (StringUtils.equals(ksObjectType, "referenceType.clu.proposal")) {
 110  
 //                    documentTypeName = "kuali.proposal.type.course.create";
 111  
 //                        }
 112  
 //                }
 113  
 //                String appId = qualification.get( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_ID );
 114  
 //                LOG.info("Checking for document id using document type '" + documentTypeName + "' and application id '" + appId + "' with qualifications: " + qualification.toString());
 115  
 //                DocumentDetailDTO docDetail = getWorkflowUtility().getDocumentDetailFromAppId(documentTypeName, appId);
 116  
 //                if (docDetail == null) {
 117  
 //                        throw new RuntimeException("No valid document instance found for document type name '" + documentTypeName + "' and Application Id '" + appId + "'");
 118  
 //                }
 119  
 //                return docDetail.getRouteHeaderId();
 120  
         }
 121  
 
 122  
         public boolean isCheckFutureRequests() {
 123  0
                 return checkFutureRequests;
 124  
         }
 125  
 
 126  
         public void setCheckFutureRequests(boolean checkFutureRequests) {
 127  0
                 this.checkFutureRequests = checkFutureRequests;
 128  0
         }
 129  
 
 130  
         /**
 131  
          *        - qualifier is document number
 132  
          *        - the roles that will be of this type are KR-WKFLW Initiator and KR-WKFLW Initiator or Reviewer, KR-WKFLW Router
 133  
          *        - only the initiator of the document in question gets the KR-WKFLW Initiator role
 134  
          *        - user who routed the document according to the route log should get the KR-WKFLW Router role
 135  
          *        - users who are authorized by the route log, 
 136  
          *                i.e. initiators, people who have taken action, people with a pending action request, 
 137  
          *                or people who will receive an action request for the document in question get the KR-WKFLW Initiator or Reviewer Role 
 138  
          * 
 139  
          * @see org.kuali.rice.kim.service.support.impl.KimRoleTypeServiceBase#getRoleMembersFromApplicationRole(String, String, AttributeSet)
 140  
          */
 141  
         @Override
 142  
     public List<RoleMembershipInfo> getRoleMembersFromApplicationRole(String namespaceCode, String roleName, AttributeSet paramQualification) {
 143  0
                 List<RoleMembershipInfo> members = new ArrayList<RoleMembershipInfo>();
 144  0
                 validateRequiredAttributesAgainstReceived(paramQualification);
 145  0
                 AttributeSet qualification = translateInputAttributeSet(paramQualification);
 146  0
                 Long documentNumber = null;
 147  
                 try {
 148  0
                         documentNumber = getDocumentNumber(qualification);
 149  0
                         if (documentNumber != null) {
 150  0
                                 if (INITIATOR_ROLE_NAME.equals(roleName)||DERIVED_INITIATOR_ROLE_NAME.equals(roleName)) {
 151  0
                                     String principalId = getWorkflowUtility().getDocumentInitiatorPrincipalId(documentNumber);
 152  0
                         members.add( new RoleMembershipInfo(null/*roleId*/, null, principalId, Role.PRINCIPAL_MEMBER_TYPE, null) );
 153  0
                                 } else if(INITIATOR_OR_REVIEWER_ROLE_NAME.equals(roleName)||DERIVED_INITIATOR_OR_REVIEWER_ROLE_NAME.equals(roleName)) {
 154  0
                                         String[] ids = getWorkflowUtility().getPrincipalIdsInRouteLog(documentNumber, isCheckFutureRequests());
 155  0
                                         if (ids != null) {
 156  0
                                             for ( String id : ids ) {
 157  0
                                                     if ( StringUtils.isNotBlank(id) ) {
 158  0
                                                             members.add( new RoleMembershipInfo(null/*roleId*/, null, id, Role.PRINCIPAL_MEMBER_TYPE, null) );
 159  
                                                     }
 160  
                                             }
 161  
                                         }
 162  0
                                 } else if(ROUTER_ROLE_NAME.equals(roleName)||DERIVED_ROUTER_ROLE_NAME.equals(roleName)) {
 163  0
                                     String principalId = getWorkflowUtility().getDocumentRoutedByPrincipalId(documentNumber);
 164  0
                         members.add( new RoleMembershipInfo(null/*roleId*/, null, principalId, Role.PRINCIPAL_MEMBER_TYPE, null) );
 165  
                                 }
 166  
                         }
 167  0
                 } catch (WorkflowException wex) {
 168  0
                         LOG.error("Workflow Error: " + wex.getLocalizedMessage(),wex);
 169  0
                         throw new RuntimeException("Error in getting principal Ids in route log for document number: "+documentNumber+" :"+wex.getLocalizedMessage(),wex);
 170  0
                 }
 171  0
                 return members;
 172  
         }
 173  
 
 174  
         /***
 175  
          * @see org.kuali.rice.kim.service.support.impl.KimRoleTypeServiceBase#hasApplicationRole(java.lang.String, java.util.List, java.lang.String, java.lang.String, org.kuali.rice.kim.bo.types.dto.AttributeSet)
 176  
          */
 177  
         @Override
 178  
         public boolean hasApplicationRole(
 179  
                         String principalId, List<String> groupIds, String namespaceCode, String roleName, AttributeSet paramQualification){
 180  0
         validateRequiredAttributesAgainstReceived(paramQualification);
 181  0
         AttributeSet qualification = translateInputAttributeSet(paramQualification);
 182  0
         boolean isUserInRouteLog = false;
 183  0
                 Long documentNumber = null;
 184  
                 try {
 185  0
                         documentNumber = getDocumentNumber(qualification);
 186  0
                         if (documentNumber != null) {
 187  0
                                 if (INITIATOR_ROLE_NAME.equals(roleName)||DERIVED_INITIATOR_ROLE_NAME.equals(roleName)){
 188  0
                                         isUserInRouteLog = principalId.equals(getWorkflowUtility().getDocumentInitiatorPrincipalId(documentNumber));
 189  0
                                 } else if(INITIATOR_OR_REVIEWER_ROLE_NAME.equals(roleName)||DERIVED_INITIATOR_OR_REVIEWER_ROLE_NAME.equals(roleName)){
 190  0
                                         isUserInRouteLog = getWorkflowUtility().isUserInRouteLog(documentNumber, principalId, isCheckFutureRequests());
 191  0
                                 } else if(ROUTER_ROLE_NAME.equals(roleName)||DERIVED_ROUTER_ROLE_NAME.equals(roleName)){
 192  0
                                         isUserInRouteLog = principalId.equals(getWorkflowUtility().getDocumentRoutedByPrincipalId(documentNumber));
 193  
                                 }
 194  
                         }
 195  0
                 } catch (WorkflowException wex) {
 196  0
                         LOG.error("Workflow Error: " + wex.getLocalizedMessage(),wex);
 197  0
                         throw new RuntimeException("Error in determining whether the principal Id: "+principalId+" is in route log for document number: "+documentNumber+" :"+wex.getLocalizedMessage(),wex);
 198  0
                 }
 199  0
         return isUserInRouteLog;
 200  
         }
 201  
 
 202  
         /**
 203  
          * Returns false, as the Route Log changes often enough that role membership is highly volatile
 204  
          * 
 205  
          * @see org.kuali.rice.kim.service.support.impl.KimRoleTypeServiceBase#shouldCacheRoleMembershipResults(java.lang.String, java.lang.String)
 206  
          */
 207  
 //        @Override
 208  
         public boolean shouldCacheRoleMembershipResults(String namespaceCode, String roleName) {
 209  0
                 return false;
 210  
         }
 211  
 
 212  
         protected WorkflowUtility getWorkflowUtility() {
 213  0
                 return KEWServiceLocator.getWorkflowUtilityService();
 214  
         }
 215  
 
 216  
 }