View Javadoc

1   /**
2    *
3    */
4   package org.kuali.student.lum.workflow.qualifierresolver;
5   
6   import java.util.ArrayList;
7   import java.util.LinkedHashMap;
8   import java.util.List;
9   import java.util.Map;
10  
11  import org.apache.commons.lang.StringUtils;
12  import org.kuali.rice.kew.engine.RouteContext;
13  import org.kuali.rice.student.bo.KualiStudentKimAttributes;
14  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
15  import org.kuali.student.r2.common.util.ContextUtils;
16  import org.kuali.student.r2.core.organization.dto.OrgInfo;
17  import org.kuali.student.r2.core.organization.dto.OrgOrgRelationInfo;
18  import org.kuali.student.r2.core.organization.service.OrganizationService;
19  import org.kuali.student.lum.workflow.node.OrganizationDynamicNode;
20  
21  /**
22   * A qualifier resolver class that is used by the hierarchy routing node {@link OrganizationDynamicNode}.
23   *
24   * This qualifier resolver will get the organization id value from inside the current route node instance and use the
25   * {@link OrganizationService#getOrgOrgRelationsByOrg(String)} method to find all relations to it. From those relations
26   * this class will select the ones that are both active and of the relation type matching
27   * {@link AbstractOrganizationServiceQualifierResolver.KUALI_ORG_TYPE_CURRICULUM_PARENT}. Once the list of those relations has been
28   * determined this qualifier resolver will select any of the organizations that match the above relation details but
29   * also only organizations that are of the type {@link AbstractOrganizationServiceQualifierResolver.KUALI_ORG_COC}. Those
30   * organizations will be returned as qualifications with the details being the organization id and the organization
31   * short name fields.
32   *
33   * If no relation is found that is both active and of the relation type matching
34   * {@link AbstractOrganizationServiceQualifierResolver.KUALI_ORG_TYPE_CURRICULUM_PARENT} then this class will use the organization
35   * found on the current route node instance as the qualification returned.
36   *
37   */
38  public class OrganizationCurriculumCommitteeQualifierResolver extends AbstractOrganizationServiceQualifierResolver {
39      protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OrganizationCurriculumCommitteeQualifierResolver.class);
40  
41      @Override
42      public List<Map<String,String>> resolve(RouteContext routeContext) {
43          // get the organization id from the current route node instance and error out if not found
44          String orgIdValue = routeContext.getNodeInstance().getNodeState(OrganizationDynamicNode.NODE_STATE_ORG_ID_KEY).getValue();
45          if (StringUtils.isBlank(orgIdValue)) {
46              throw new RuntimeException("Cannot find valid organization ID in Route Node Instance Node States");
47          }
48          if (LOG.isDebugEnabled()) {
49              LOG.debug("orgIdValue = '" + orgIdValue + "'");
50          }
51  
52          try {
53              List<Map<String,String>> attributeSets = new ArrayList<Map<String,String>>();
54              // find the OrgOrgRelationInfo objects associated with the org from the route node instance
55              List<OrgOrgRelationInfo> orgRelationInfos = getOrganizationService().getOrgOrgRelationsByOrg(orgIdValue, ContextUtils.getContextInfo());
56              for (OrgOrgRelationInfo orgOrgRelationInfo : orgRelationInfos) {
57                  // check that the relationship is active
58                  if (StringUtils.equals("Active", orgOrgRelationInfo.getStateKey())) {
59                      // check for the proper relationship type
60                      if (StringUtils.equals(AbstractOrganizationServiceQualifierResolver.KUALI_ORG_TYPE_CURRICULUM_PARENT, orgOrgRelationInfo.getTypeKey())) {
61                          OrgInfo nextNodeOrgInfo = getOrganization(orgOrgRelationInfo.getRelatedOrgId());
62                          // check the org type of the related org is the proper org type
63                          if (StringUtils.equals(AbstractOrganizationServiceQualifierResolver.KUALI_ORG_COC, nextNodeOrgInfo.getTypeKey())) {
64                              if (LOG.isDebugEnabled()) {
65                                  LOG.debug("---- Related Org Relation: " + nextNodeOrgInfo.getId() + " - " + nextNodeOrgInfo.getShortName() + " (" + nextNodeOrgInfo.getLongName() + ")");
66                              }
67                              Map<String,String> attributeSet = new LinkedHashMap<String,String>();
68                              attributeSet.put(KualiStudentKimAttributes.QUALIFICATION_ORG_ID, nextNodeOrgInfo.getId());
69                              attributeSets.add(attributeSet);
70                          }
71                      }
72                  }
73              }
74              // if no org is found then use the org on the route node instance
75              if (attributeSets.isEmpty()) {
76                  OrgInfo currentNodeOrg = getOrganization(orgIdValue);
77                  Map<String,String> attributeSet = new LinkedHashMap<String,String>();
78                  attributeSet.put(KualiStudentKimAttributes.QUALIFICATION_ORG_ID, currentNodeOrg.getId());
79                  attributeSets.add(attributeSet);
80              }
81              return attributeSets;
82          } catch (Exception e) {
83              LOG.error("Error getting organization(s) or organization relations", e);
84              throw new RuntimeException(e);
85          }
86      }
87  
88      protected OrgInfo getOrganization(String orgId) throws Exception {
89  //        try {
90          	OrgInfo orgInfo = getOrganizationService().getOrg(orgId, null);
91              return orgInfo;
92  //        } catch (DoesNotExistException e) {
93  //            LOG.error("No valid organization found for id '" + orgId + "'", e);
94  //            throw e;
95  //        }
96      }
97  
98  }