| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 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.core.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 |  |           | 
  | 81 | 0 |                  List<String> listOne = new ArrayList<String>(); | 
  | 82 | 0 |                  listOne.add( KimAttributes.DOCUMENT_NUMBER ); | 
  | 83 | 0 |                  newRequiredAttributes.add(listOne); | 
  | 84 |  |           | 
  | 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 |  |           | 
  | 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 |  |           | 
  | 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 |  |   | 
  | 104 |  |   | 
  | 105 |  |   | 
  | 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 |  |                   | 
  | 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 |  |   | 
  | 129 |  |   | 
  | 130 |  |   | 
  | 131 |  |   | 
  | 132 |  |   | 
  | 133 |  |   | 
  | 134 |  |   | 
  | 135 |  |   | 
  | 136 |  |   | 
  | 137 |  |   | 
  | 138 |  |   | 
  | 139 |  |   | 
  | 140 |  |   | 
  | 141 |  |   | 
  | 142 |  |   | 
  | 143 |  |   | 
  | 144 |  |   | 
  | 145 |  |   | 
  | 146 |  |   | 
  | 147 |  |   | 
  | 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 |  |           | 
  | 158 |  |   | 
  | 159 |  |   | 
  | 160 |  |          @Override | 
  | 161 |  |          public List<RoleMembershipInfo> getRoleMembersFromApplicationRole( | 
  | 162 |  |                          String namespaceCode, String roleName, AttributeSet paramQualification) { | 
  | 163 |  |                   | 
  | 164 | 0 |                  validateRequiredAttributesAgainstReceived(paramQualification); | 
  | 165 | 0 |                  AttributeSet qualification = translateInputAttributeSet(paramQualification); | 
  | 166 | 0 |                  List<RoleMembershipInfo> members = new ArrayList<RoleMembershipInfo>(); | 
  | 167 |  |                  try { | 
  | 168 |  |                           | 
  | 169 | 0 |                          Long documentNumber = getDocumentNumber(qualification); | 
  | 170 | 0 |                          if (documentNumber != null) { | 
  | 171 |  |                                   | 
  | 172 | 0 |                                  ActionRequestDTO[] actionRequests = getWorkflowUtility().getAllActionRequests(documentNumber); | 
  | 173 | 0 |                                  Map<String,List<ActionRequestDTO>> requestsByPrincipalId = new HashMap<String, List<ActionRequestDTO>>(); | 
  | 174 |  |                                   | 
  | 175 | 0 |                      for (ActionRequestDTO actionRequest: actionRequests) { | 
  | 176 |  |                               | 
  | 177 | 0 |                              if (actionRequest.getPrincipalId() != null) { | 
  | 178 | 0 |                                      addMember(requestsByPrincipalId, actionRequest.getPrincipalId(), actionRequest); | 
  | 179 |  |                              } | 
  | 180 |  |                               | 
  | 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, 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 |  |           | 
  | 201 |  |   | 
  | 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 |  |   | 
  | 262 |  |   | 
  | 263 |  |   | 
  | 264 |  |   | 
  | 265 |  |   | 
  | 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 |  |  } |