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/26
2.7
 
 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 org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.kew.api.KewApiServiceLocator;
 20  
 import org.kuali.rice.kew.api.action.WorkflowDocumentActionsService;
 21  
 import org.kuali.rice.kew.api.document.WorkflowDocumentService;
 22  
 import org.kuali.rice.kew.api.exception.WorkflowException;
 23  
 import org.kuali.rice.kim.api.KimConstants;
 24  
 import org.kuali.rice.kim.api.role.RoleMembership;
 25  
 import org.kuali.rice.kns.kim.role.DerivedRoleTypeServiceBase;
 26  
 import org.kuali.student.common.rice.StudentIdentityConstants;
 27  
 import org.kuali.student.lum.kim.KimQualificationHelper;
 28  
 
 29  
 import java.util.*;
 30  
 
 31  
 /**
 32  
  *
 33  
  */
 34  0
 public class KSRouteLogDerivedRoleTypeServiceImpl extends DerivedRoleTypeServiceBase {
 35  0
     private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KSRouteLogDerivedRoleTypeServiceImpl.class);
 36  
 
 37  
     public static final String INITIATOR_ROLE_NAME = "Initiator";
 38  
     public static final String INITIATOR_OR_REVIEWER_ROLE_NAME = "Initiator or Reviewer";
 39  
     public static final String ROUTER_ROLE_NAME = "Router";
 40  
 
 41  0
     private boolean checkFutureRequests = false;
 42  0
         protected Set<List<String>> newRequiredAttributes = new HashSet<List<String>>();
 43  
 
 44  
         {
 45  
                 // add document number as one required attribute set
 46  0
                 List<String> listOne = new ArrayList<String>();
 47  0
                 listOne.add( KimConstants.AttributeConstants.DOCUMENT_NUMBER );
 48  0
                 newRequiredAttributes.add(listOne);
 49  
         // add document type name and KEW application id as one required attribute set
 50  0
                 List<String> listTwo = new ArrayList<String>();
 51  0
                 listTwo.add( KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME );
 52  0
                 listTwo.add( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_ID );
 53  0
                 newRequiredAttributes.add(listTwo);
 54  
         // add object id and object type as one required attribute set
 55  0
                 List<String> listThree = new ArrayList<String>();
 56  0
                 listThree.add( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_ID );
 57  0
                 listThree.add( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_TYPE );
 58  0
                 newRequiredAttributes.add(listThree);
 59  
         // add each proposal reference type as a required attribute set
 60  0
         for (String proposalReferenceType : StudentIdentityConstants.QUALIFICATION_PROPOSAL_ID_REF_TYPES) {
 61  0
             List<String> tempList = new ArrayList<String>();
 62  0
             tempList.add( proposalReferenceType );
 63  0
             newRequiredAttributes.add(tempList);
 64  0
         }
 65  0
         }
 66  
 
 67  
         /** 
 68  
          * The part about where the receivedAttributes list being empty does not return errors is copied from Rice base class.
 69  
          * 
 70  
          * @see org.kuali.rice.kim.service.support.impl.KimTypeServiceBase#validateRequiredAttributesAgainstReceived(org.kuali.rice.kim.bo.types.dto.Map<String,String>)
 71  
          **/
 72  
         @Override
 73  
         protected void validateRequiredAttributesAgainstReceived(Map<String,String> receivedAttributes){
 74  0
                 KimQualificationHelper.validateRequiredAttributesAgainstReceived(newRequiredAttributes, receivedAttributes, isCheckFutureRequests(), COMMA_SEPARATOR);
 75  0
         super.validateRequiredAttributesAgainstReceived(receivedAttributes);
 76  0
         }
 77  
 
 78  
     @Override
 79  
     public Map<String,String> translateInputAttributes(Map<String,String> qualification) {
 80  0
         return KimQualificationHelper.translateInputAttributeSet(super.translateInputAttributes(qualification));
 81  
     }
 82  
 
 83  
         protected String getDocumentNumber(Map<String,String> qualification) throws WorkflowException {
 84  
                 // first check for a valid document id passed in
 85  0
                 String documentId = qualification.get( KimConstants.AttributeConstants.DOCUMENT_NUMBER );
 86  0
         if (StringUtils.isNotEmpty(documentId)) {
 87  0
             return documentId;
 88  
         } else {
 89  0
             LOG.warn("Could not find workflow document id in qualification list:");
 90  0
             LOG.warn(qualification);
 91  0
             return null;
 92  
         }
 93  
 //                if (StringUtils.isNotEmpty(documentId)) {
 94  
 //                        return Long.valueOf(documentId);
 95  
 //                }
 96  
 //                // if no document id passed in get the document via the id and document type name
 97  
 //                String documentTypeName = qualification.get( KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME );
 98  
 //                if (StringUtils.isEmpty(documentTypeName)) {
 99  
 //                        String ksObjectType = qualification.get( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_TYPE );
 100  
 //                        if (StringUtils.equals(ksObjectType, "referenceType.clu.proposal")) {
 101  
 //                    documentTypeName = "kuali.proposal.type.course.create";
 102  
 //                        }
 103  
 //                }
 104  
 //                String appId = qualification.get( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_ID );
 105  
 //                LOG.info("Checking for document id using document type '" + documentTypeName + "' and application id '" + appId + "' with qualifications: " + qualification.toString());
 106  
 //                DocumentDetailDTO docDetail = getWorkflowUtility().getDocumentDetailFromAppId(documentTypeName, appId);
 107  
 //                if (docDetail == null) {
 108  
 //                        throw new RuntimeException("No valid document instance found for document type name '" + documentTypeName + "' and Application Id '" + appId + "'");
 109  
 //                }
 110  
 //                return docDetail.getDocumentId();
 111  
         }
 112  
 
 113  
         public boolean isCheckFutureRequests() {
 114  0
                 return checkFutureRequests;
 115  
         }
 116  
 
 117  
         public void setCheckFutureRequests(boolean checkFutureRequests) {
 118  0
                 this.checkFutureRequests = checkFutureRequests;
 119  0
         }
 120  
 
 121  
         /**
 122  
          *        - qualifier is document number
 123  
          *        - the roles that will be of this type are KR-WKFLW Initiator and KR-WKFLW Initiator or Reviewer, KR-WKFLW Router
 124  
          *        - only the initiator of the document in question gets the KR-WKFLW Initiator role
 125  
          *        - user who routed the document according to the route log should get the KR-WKFLW Router role
 126  
          *        - users who are authorized by the route log, 
 127  
          *                i.e. initiators, people who have taken action, people with a pending action request, 
 128  
          *                or people who will receive an action request for the document in question get the KR-WKFLW Initiator or Reviewer Role 
 129  
          * 
 130  
          * @see org.kuali.rice.kim.service.support.impl.KimRoleTypeServiceBase#getRoleMembersFromDerivedRole(String, String, Map<String,String>)
 131  
          */
 132  
         @Override
 133  
     public List<RoleMembership> getRoleMembersFromDerivedRole(String namespaceCode, String roleName, Map<String,String> paramQualification) {
 134  0
                 List<RoleMembership> members = new ArrayList<RoleMembership>();
 135  0
                 validateRequiredAttributesAgainstReceived(paramQualification);
 136  0
                 Map<String,String> qualification = translateInputAttributes(paramQualification);
 137  0
                 String documentNumber = null;
 138  
                 try {
 139  0
                         documentNumber = getDocumentNumber(qualification);
 140  0
                         if (documentNumber != null) {
 141  0
                                 if (INITIATOR_ROLE_NAME.equals(roleName)) {
 142  0
                                     String principalId = getWorkflowDocumentService().getDocumentInitiatorPrincipalId(documentNumber);
 143  0
                         members.add(RoleMembership.Builder.create(null/*roleId*/, null, principalId, KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE, null).build());
 144  0
                                 } else if(INITIATOR_OR_REVIEWER_ROLE_NAME.equals(roleName)) {
 145  0
                                         List<String> ids = getWorkflowDocumentActionsService().getPrincipalIdsInRouteLog(documentNumber, isCheckFutureRequests());
 146  0
                                         if (ids != null) {
 147  0
                                             for ( String id : ids ) {
 148  0
                                                     if ( StringUtils.isNotBlank(id) ) {
 149  0
                                                             members.add(RoleMembership.Builder.create(null/*roleId*/, null, id, KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE, null).build());
 150  
                                                     }
 151  
                                             }
 152  
                                         }
 153  0
                                 } else if(ROUTER_ROLE_NAME.equals(roleName)) {
 154  0
                                     String principalId = getWorkflowDocumentService().getRoutedByPrincipalIdByDocumentId(documentNumber);
 155  0
                         members.add(RoleMembership.Builder.create(null/*roleId*/, null, principalId, KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE, null).build() );
 156  
                                 }
 157  
                         }
 158  0
                 } catch (WorkflowException wex) {
 159  0
                         LOG.error("Workflow Error: " + wex.getLocalizedMessage(),wex);
 160  0
                         throw new RuntimeException("Error in getting principal Ids in route log for document number: "+documentNumber+" :"+wex.getLocalizedMessage(),wex);
 161  0
                 }
 162  0
                 return members;
 163  
         }
 164  
 
 165  
         /***
 166  
          * @see org.kuali.rice.kim.service.support.impl.KimRoleTypeServiceBase#hasDerivedRole(java.lang.String, java.util.List, java.lang.String, java.lang.String, org.kuali.rice.kim.bo.types.dto.Map<String,String>)
 167  
          */
 168  
         @Override
 169  
         public boolean hasDerivedRole(
 170  
                         String principalId, List<String> groupIds, String namespaceCode, String roleName, Map<String,String> paramQualification){
 171  0
         validateRequiredAttributesAgainstReceived(paramQualification);
 172  0
         Map<String,String> qualification = translateInputAttributes(paramQualification);
 173  0
         boolean isUserInRouteLog = false;
 174  0
                 String documentNumber = null;
 175  
                 try {
 176  0
                         documentNumber = getDocumentNumber(qualification);
 177  0
                         if (documentNumber != null) {
 178  0
                                 if (INITIATOR_ROLE_NAME.equals(roleName)){
 179  0
                                         isUserInRouteLog = principalId.equals(getWorkflowDocumentService().getDocumentInitiatorPrincipalId(documentNumber));
 180  0
                                 } else if(INITIATOR_OR_REVIEWER_ROLE_NAME.equals(roleName)){
 181  0
                                         isUserInRouteLog = getWorkflowDocumentActionsService().isUserInRouteLog(documentNumber, principalId, isCheckFutureRequests());
 182  0
                                 } else if(ROUTER_ROLE_NAME.equals(roleName)){
 183  0
                                         isUserInRouteLog = principalId.equals(getWorkflowDocumentService().getRoutedByPrincipalIdByDocumentId(documentNumber));
 184  
                                 }
 185  
                         }
 186  0
                 } catch (WorkflowException wex) {
 187  0
                         LOG.error("Workflow Error: " + wex.getLocalizedMessage(),wex);
 188  0
                         throw new RuntimeException("Error in determining whether the principal Id: "+principalId+" is in route log for document number: "+documentNumber+" :"+wex.getLocalizedMessage(),wex);
 189  0
                 }
 190  0
         return isUserInRouteLog;
 191  
         }
 192  
 
 193  
         /**
 194  
          * Returns false, as the Route Log changes often enough that role membership is highly volatile
 195  
          * 
 196  
          * @see org.kuali.rice.kim.service.support.impl.KimRoleTypeServiceBase#shouldCacheRoleMembershipResults(java.lang.String, java.lang.String)
 197  
          */
 198  
 //        @Override
 199  
         public boolean shouldCacheRoleMembershipResults(String namespaceCode, String roleName) {
 200  0
                 return false;
 201  
         }
 202  
 
 203  
 
 204  
     protected WorkflowDocumentService getWorkflowDocumentService() {
 205  0
         return KewApiServiceLocator.getWorkflowDocumentService();
 206  
     }
 207  
 
 208  
     public WorkflowDocumentActionsService getWorkflowDocumentActionsService() {
 209  0
         return KewApiServiceLocator.getWorkflowDocumentActionsService();
 210  
     }
 211  
 
 212  
 }