Coverage Report - org.kuali.student.lum.kim.role.type.KSActionRequestDerivedRoleTypeServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
KSActionRequestDerivedRoleTypeServiceImpl
0%
0/88
0%
0/58
4
KSActionRequestDerivedRoleTypeServiceImpl$REQUESTS_STATUS_TO_CHECK
0%
0/10
0%
0/4
4
KSActionRequestDerivedRoleTypeServiceImpl$REQUESTS_TYPES_TO_CHECK
0%
0/2
N/A
4
 
 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.Collections;
 20  
 import java.util.HashMap;
 21  
 import java.util.HashSet;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 import java.util.Set;
 25  
 
 26  
 import org.apache.commons.lang.StringUtils;
 27  
 import org.apache.log4j.Logger;
 28  
 import org.kuali.rice.kew.dto.ActionRequestDTO;
 29  
 import org.kuali.rice.kew.exception.WorkflowException;
 30  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 31  
 import org.kuali.rice.kew.service.WorkflowUtility;
 32  
 import org.kuali.rice.kew.util.KEWConstants;
 33  
 import org.kuali.rice.kim.bo.Role;
 34  
 import org.kuali.rice.kim.bo.impl.KimAttributes;
 35  
 import org.kuali.rice.kim.bo.role.dto.RoleMembershipInfo;
 36  
 import org.kuali.rice.kim.bo.types.dto.AttributeSet;
 37  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 38  
 import org.kuali.rice.kim.service.support.impl.KimDerivedRoleTypeServiceBase;
 39  
 import org.kuali.student.common.rice.StudentIdentityConstants;
 40  
 import org.kuali.student.lum.kim.KimQualificationHelper;
 41  
 
 42  
 /**
 43  
  *
 44  
  */
 45  0
 public class KSActionRequestDerivedRoleTypeServiceImpl extends KimDerivedRoleTypeServiceBase {
 46  0
     protected final Logger LOG = Logger.getLogger(getClass());
 47  
         
 48  
         private static final String APPROVE_REQUEST_RECIPIENT_ROLE_CONTENT = "Approve";
 49  
         private static final String ACKNOWLEDGE_REQUEST_RECIPIENT_ROLE_CONTENT = "Acknowledge";
 50  
         private static final String FYI_REQUEST_RECIPIENT_ROLE_CONTENT = "FYI";
 51  
 
 52  0
     protected Set<List<String>> newRequiredAttributes = new HashSet<List<String>>();
 53  
 
 54  0
         protected enum REQUESTS_TYPES_TO_CHECK {
 55  0
                 BOTH, ADHOC_ONLY, NON_ADHOC_ONLY;
 56  
         }
 57  
 
 58  0
         protected enum REQUESTS_STATUS_TO_CHECK {
 59  0
                 INITIALIZED(KEWConstants.ACTION_REQUEST_INITIALIZED), ACTIVE(KEWConstants.ACTION_REQUEST_ACTIVATED), 
 60  0
                 DONE(KEWConstants.ACTION_REQUEST_DONE_STATE);
 61  
 
 62  
                 private String kewActionRequestStatusCode;
 63  
 
 64  0
                 private REQUESTS_STATUS_TO_CHECK(String kewActionRequestStatusCode) {
 65  0
                 this.kewActionRequestStatusCode = kewActionRequestStatusCode;
 66  0
         }
 67  
 
 68  
                 public static REQUESTS_STATUS_TO_CHECK getByCode(String code) {
 69  0
                         for (REQUESTS_STATUS_TO_CHECK type : REQUESTS_STATUS_TO_CHECK.values()) {
 70  0
                                 if (type.kewActionRequestStatusCode.equals(code)) {
 71  0
                                         return type;
 72  
                                 }
 73  
                         }
 74  0
                         return null;
 75  
                 }
 76  
         }
 77  
 
 78  
         {
 79  0
                 checkRequiredAttributes = true;
 80  
         // add document number as one required attribute set
 81  0
                 List<String> listOne = new ArrayList<String>();
 82  0
                 listOne.add( KimAttributes.DOCUMENT_NUMBER );
 83  0
                 newRequiredAttributes.add(listOne);
 84  
         // add document type name and KEW application id as one required attribute set
 85  0
                 List<String> listTwo = new ArrayList<String>();
 86  0
                 listTwo.add( KimAttributes.DOCUMENT_TYPE_NAME );
 87  0
                 listTwo.add( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_ID );
 88  0
                 newRequiredAttributes.add(listTwo);
 89  
         // add object id and object type as one required attribute set
 90  0
                 List<String> listThree = new ArrayList<String>();
 91  0
                 listThree.add( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_ID );
 92  0
                 listThree.add( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_TYPE );
 93  0
                 newRequiredAttributes.add(listThree);
 94  
         // add each proposal reference type as a required attribute set
 95  0
         for (String proposalReferenceType : StudentIdentityConstants.QUALIFICATION_PROPOSAL_ID_REF_TYPES) {
 96  0
             List<String> tempList = new ArrayList<String>();
 97  0
             tempList.add( proposalReferenceType );
 98  0
             newRequiredAttributes.add(tempList);
 99  0
         }
 100  0
         }
 101  
 
 102  
         /** 
 103  
          * The part about where the receivedAttributes list being empty does not return errors is copied from Rice base class.
 104  
          * 
 105  
          * @see org.kuali.rice.kim.service.support.impl.KimTypeServiceBase#validateRequiredAttributesAgainstReceived(org.kuali.rice.kim.bo.types.dto.AttributeSet)
 106  
          **/
 107  
         @Override
 108  
         protected void validateRequiredAttributesAgainstReceived(AttributeSet receivedAttributes){
 109  0
                 KimQualificationHelper.validateRequiredAttributesAgainstReceived(newRequiredAttributes, receivedAttributes, isCheckRequiredAttributes(), COMMA_SEPARATOR);
 110  0
                 super.validateRequiredAttributesAgainstReceived(receivedAttributes);
 111  0
         }
 112  
 
 113  
     @Override
 114  
     public AttributeSet translateInputAttributeSet(AttributeSet qualification) {
 115  0
         return KimQualificationHelper.translateInputAttributeSet(super.translateInputAttributeSet(qualification));
 116  
     }
 117  
 
 118  
         protected Long getDocumentNumber(AttributeSet qualification) throws WorkflowException {
 119  
                 // first check for a valid document id passed in
 120  0
                 String documentId = qualification.get( KimAttributes.DOCUMENT_NUMBER );
 121  0
         if (StringUtils.isNotEmpty(documentId)) {
 122  0
             return Long.valueOf(documentId);
 123  
         } else {
 124  0
             LOG.warn("Could not find workflow document id in qualification list:");
 125  0
             LOG.warn(qualification.formattedDump(20));
 126  0
             return null;
 127  
         }
 128  
 //                if (StringUtils.isNotEmpty(documentId)) {
 129  
 //                        return Long.valueOf(documentId);
 130  
 //                }
 131  
 //                // if no document id passed in get the document via the id and document type name
 132  
 //                String documentTypeName = qualification.get( KimAttributes.DOCUMENT_TYPE_NAME );
 133  
 //                if (StringUtils.isEmpty(documentTypeName)) {
 134  
 //                        String ksObjectType = qualification.get( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_TYPE );
 135  
 //                        if (StringUtils.equals(ksObjectType, "referenceType.clu.proposal")) {
 136  
 //                    documentTypeName = "kuali.proposal.type.course.create";
 137  
 //                        }
 138  
 //                }
 139  
 //                String appId = qualification.get( StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_ID );
 140  
 //                LOG.info("Checking for document id using document type '" + documentTypeName + "' and application id '" + appId + "' with qualifications: " + qualification.toString());
 141  
 //                DocumentDetailDTO docDetail = getWorkflowUtility().getDocumentDetailFromAppId(documentTypeName, appId);
 142  
 //                if (docDetail == null) {
 143  
 //                        LOG.info("No valid document instance found for document type name '" + documentTypeName + "' and Application Id '" + appId + "'");
 144  
 //                        return null;
 145  
 ////                        throw new RuntimeException("No valid document instance found for document type name '" + documentTypeName + "' and Application Id '" + appId + "'");
 146  
 //                }
 147  
 //                return docDetail.getRouteHeaderId();
 148  
         }
 149  
 
 150  
         protected void addMember(Map<String,List<ActionRequestDTO>> requestsByPrincipalId, String principalId, ActionRequestDTO actionRequest) {
 151  0
                 if (!requestsByPrincipalId.containsKey(principalId)) {
 152  0
                         requestsByPrincipalId.put(principalId, new ArrayList<ActionRequestDTO>());
 153  
                 }
 154  0
                 requestsByPrincipalId.get(principalId).add(actionRequest);
 155  0
         }
 156  
 
 157  
         /* (non-Javadoc)
 158  
          * @see org.kuali.rice.kew.role.service.impl.ActionRequestDerivedRoleTypeServiceImpl#getRoleMembersFromApplicationRole(java.lang.String, java.lang.String, org.kuali.rice.kim.bo.types.dto.AttributeSet)
 159  
          */
 160  
         @Override
 161  
         public List<RoleMembershipInfo> getRoleMembersFromApplicationRole(
 162  
                         String namespaceCode, String roleName, AttributeSet paramQualification) {
 163  
                 // validate required attributes
 164  0
                 validateRequiredAttributesAgainstReceived(paramQualification);
 165  0
                 AttributeSet qualification = translateInputAttributeSet(paramQualification);
 166  0
                 List<RoleMembershipInfo> members = new ArrayList<RoleMembershipInfo>();
 167  
                 try {
 168  
                         // check for valid qualification data to check
 169  0
                         Long documentNumber = getDocumentNumber(qualification);
 170  0
                         if (documentNumber != null) {
 171  
                                 // get all action requests for the document id given
 172  0
                                 ActionRequestDTO[] actionRequests = getWorkflowUtility().getAllActionRequests(documentNumber);
 173  0
                                 Map<String,List<ActionRequestDTO>> requestsByPrincipalId = new HashMap<String, List<ActionRequestDTO>>();
 174  
                                 // build a map by principal id of action requests for the document
 175  0
                     for (ActionRequestDTO actionRequest: actionRequests) {
 176  
                             // if the request has a principal id
 177  0
                             if (actionRequest.getPrincipalId() != null) {
 178  0
                                     addMember(requestsByPrincipalId, actionRequest.getPrincipalId(), actionRequest);
 179  
                             }
 180  
                             // if the request is a group request
 181  0
                             else if (actionRequest.isGroupRequest()) {
 182  0
                                     for (String principalId : KIMServiceLocator.getGroupService().getMemberPrincipalIds(actionRequest.getGroupId())) {
 183  0
                                             addMember(requestsByPrincipalId, principalId, actionRequest);
 184  
                                                 }
 185  
                             }
 186  
                     }
 187  0
                     for (Map.Entry<String, List<ActionRequestDTO>> mapEntry : requestsByPrincipalId.entrySet()) {
 188  0
                                         if (containsActivatedRequest(roleName, mapEntry.getValue().toArray(new ActionRequestDTO[]{}))) {
 189  0
                                 members.add( new RoleMembershipInfo(null/*roleId*/, null, mapEntry.getKey(), Role.PRINCIPAL_MEMBER_TYPE, null) );
 190  
                                         }
 191  
                                 }
 192  
                         }
 193  0
                         return members;
 194  0
                 } catch (WorkflowException e) {
 195  0
                         LOG.error("Workflow Error: " + e.getLocalizedMessage(), e);
 196  0
                         throw new RuntimeException("Unable to load route header", e);
 197  
                 }
 198  
         }
 199  
 
 200  
         /* (non-Javadoc)
 201  
          * @see org.kuali.rice.kew.role.service.impl.ActionRequestDerivedRoleTypeServiceImpl#hasApplicationRole(java.lang.String, java.util.List, java.lang.String, java.lang.String, org.kuali.rice.kim.bo.types.dto.AttributeSet)
 202  
          */
 203  
         @Override
 204  
         public boolean hasApplicationRole(String principalId,
 205  
                         List<String> groupIds, String namespaceCode, String roleName,
 206  
                         AttributeSet paramQualification) {
 207  0
         validateRequiredAttributesAgainstReceived(paramQualification);
 208  0
         AttributeSet qualification = translateInputAttributeSet(paramQualification);
 209  
                 try {
 210  0
                         Long documentNumber = getDocumentNumber(qualification);
 211  0
                         if (documentNumber != null) {
 212  0
                                 ActionRequestDTO[] actionRequests = getWorkflowUtility().getActionRequests(documentNumber, null, principalId);
 213  0
                                 return containsActivatedRequest(roleName, actionRequests);
 214  
                         }
 215  0
                         return false;
 216  0
                 } catch (WorkflowException e) {
 217  0
                         LOG.error("Workflow Error: " + e.getLocalizedMessage(), e);
 218  0
                         throw new RuntimeException("Unable to load route header", e);
 219  
                 }
 220  
         }
 221  
 
 222  
         protected boolean containsActivatedRequest(String roleName, ActionRequestDTO[] actionRequests) {
 223  0
                 if (StringUtils.containsIgnoreCase(roleName, APPROVE_REQUEST_RECIPIENT_ROLE_CONTENT)) {
 224  0
                         for ( ActionRequestDTO ar : actionRequests ) {
 225  0
                                 if ( ar.isApprovalRequest() && verifyActionRequest(ar)) {
 226  0
                                         return true;
 227  
                                 }
 228  
                         }
 229  
                 }
 230  0
                 else if (StringUtils.containsIgnoreCase(roleName, ACKNOWLEDGE_REQUEST_RECIPIENT_ROLE_CONTENT)) {
 231  0
                         for ( ActionRequestDTO ar : actionRequests ) {
 232  0
                                 if ( ar.isAcknowledgeRequest() && verifyActionRequest(ar)) {
 233  0
                                         return true;
 234  
                                 }
 235  
                         }
 236  
                 }
 237  0
                 else if (StringUtils.containsIgnoreCase(roleName, FYI_REQUEST_RECIPIENT_ROLE_CONTENT)) {
 238  0
                         for ( ActionRequestDTO ar : actionRequests ) {
 239  0
                                 if ( ar.isFyiRequest() && verifyActionRequest(ar)) {
 240  0
                                         return true;
 241  
                                 }
 242  
                         }
 243  
                 }
 244  0
                 return false;
 245  
         }
 246  
 
 247  
         protected boolean verifyActionRequest(ActionRequestDTO ar) {
 248  0
                 REQUESTS_STATUS_TO_CHECK statusEnum = REQUESTS_STATUS_TO_CHECK.getByCode(ar.getStatus());
 249  0
                 if (getRequestStatusesToCheck().contains(statusEnum)) {
 250  0
                         if (ar.isAdHocRequest()) {
 251  0
                                 return getRequestTypesToCheck().equals(REQUESTS_TYPES_TO_CHECK.BOTH) || getRequestTypesToCheck().equals(REQUESTS_TYPES_TO_CHECK.ADHOC_ONLY);
 252  
                         }
 253  
                         else {
 254  0
                                 return getRequestTypesToCheck().equals(REQUESTS_TYPES_TO_CHECK.BOTH) || getRequestTypesToCheck().equals(REQUESTS_TYPES_TO_CHECK.NON_ADHOC_ONLY);
 255  
                         }
 256  
                 }
 257  0
                 return false;
 258  
         }
 259  
 
 260  
         /**
 261  
          * Returns false, as the Action Requests change often enough that role membership is highly volatile
 262  
          * 
 263  
          * @see org.kuali.rice.kim.service.support.impl.KimRoleTypeServiceBase#shouldCacheRoleMembershipResults(java.lang.String, java.lang.String)
 264  
          */
 265  
 //        @Override
 266  
         public boolean shouldCacheRoleMembershipResults(String namespaceCode, String roleName) {
 267  0
                 return false;
 268  
         }
 269  
 
 270  
         protected REQUESTS_TYPES_TO_CHECK getRequestTypesToCheck() {
 271  0
                 return REQUESTS_TYPES_TO_CHECK.BOTH;
 272  
         }
 273  
 
 274  
         protected List<REQUESTS_STATUS_TO_CHECK> getRequestStatusesToCheck() {
 275  0
                 return Collections.singletonList(REQUESTS_STATUS_TO_CHECK.ACTIVE);
 276  
         }
 277  
 
 278  
         protected WorkflowUtility getWorkflowUtility() {
 279  0
                 return KEWServiceLocator.getWorkflowUtilityService();
 280  
         }
 281  
 }