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