001    /**
002     * 
003     */
004    package org.kuali.student.lum.workflow.qualifierresolver;
005    
006    import java.util.ArrayList;
007    import java.util.List;
008    
009    import javax.xml.namespace.QName;
010    
011    import org.apache.commons.lang.StringUtils;
012    import org.kuali.rice.core.resourceloader.GlobalResourceLoader;
013    import org.kuali.rice.kew.engine.RouteContext;
014    import org.kuali.rice.kew.role.QualifierResolver;
015    import org.kuali.rice.kim.bo.types.dto.AttributeSet;
016    import org.kuali.rice.student.bo.KualiStudentKimAttributes;
017    import org.kuali.student.common.exceptions.DoesNotExistException;
018    import org.kuali.student.core.organization.dto.OrgInfo;
019    import org.kuali.student.core.organization.dto.OrgOrgRelationInfo;
020    import org.kuali.student.core.organization.service.OrganizationService;
021    import org.kuali.student.lum.workflow.node.OrganizationDynamicNode;
022    
023    /**
024     * A qualifier resolver class that is used by the hierarchy routing node {@link OrganizationDynamicNode}.
025     * 
026     * This qualifier resolver will get the organization id value from inside the current route node instance and use the
027     * {@link OrganizationService#getOrgOrgRelationsByOrg(String)} method to find all relations to it. From those relations
028     * this class will select the ones that are both active and of the relation type matching
029     * {@link AbstractOrganizationServiceQualifierResolver.KUALI_ORG_TYPE_CURRICULUM_PARENT}. Once the list of those relations has been
030     * determined this qualifier resolver will select any of the organizations that match the above relation details but
031     * also only organizations that are of the type {@link AbstractOrganizationServiceQualifierResolver.KUALI_ORG_COC}. Those
032     * organizations will be returned as qualifications with the details being the organization id and the organization
033     * short name fields.
034     * 
035     * If no relation is found that is both active and of the relation type matching
036     * {@link AbstractOrganizationServiceQualifierResolver.KUALI_ORG_TYPE_CURRICULUM_PARENT} then this class will use the organization
037     * found on the current route node instance as the qualification returned.
038     * 
039     */
040    public class OrganizationCurriculumCommitteeQualifierResolver extends AbstractOrganizationServiceQualifierResolver {
041        protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OrganizationCurriculumCommitteeQualifierResolver.class);
042    
043        @Override
044        public List<AttributeSet> resolve(RouteContext routeContext) {
045            // get the organization id from the current route node instance and error out if not found
046            String orgIdValue = routeContext.getNodeInstance().getNodeState(OrganizationDynamicNode.NODE_STATE_ORG_ID_KEY).getValue();
047            if (StringUtils.isBlank(orgIdValue)) {
048                throw new RuntimeException("Cannot find valid organization ID in Route Node Instance Node States");
049            }
050            if (LOG.isDebugEnabled()) {
051                LOG.debug("orgIdValue = '" + orgIdValue + "'");
052            }
053    
054            try {
055                List<AttributeSet> attributeSets = new ArrayList<AttributeSet>();
056                // find the OrgOrgRelationInfo objects associated with the org from the route node instance
057                List<OrgOrgRelationInfo> orgRelationInfos = getOrganizationService().getOrgOrgRelationsByOrg(orgIdValue);
058                for (OrgOrgRelationInfo orgOrgRelationInfo : orgRelationInfos) {
059                    // check that the relationship is active
060                    if (StringUtils.equals("Active", orgOrgRelationInfo.getState())) {
061                        // check for the proper relationship type
062                        if (StringUtils.equals(AbstractOrganizationServiceQualifierResolver.KUALI_ORG_TYPE_CURRICULUM_PARENT, orgOrgRelationInfo.getType())) {
063                            OrgInfo nextNodeOrgInfo = getOrganization(orgOrgRelationInfo.getRelatedOrgId());
064                            // check the org type of the related org is the proper org type
065                            if (StringUtils.equals(AbstractOrganizationServiceQualifierResolver.KUALI_ORG_COC, nextNodeOrgInfo.getType())) {
066                                if (LOG.isDebugEnabled()) {
067                                    LOG.debug("---- Related Org Relation: " + nextNodeOrgInfo.getId() + " - " + nextNodeOrgInfo.getShortName() + " (" + nextNodeOrgInfo.getLongName() + ")");
068                                }
069                                AttributeSet attributeSet = new AttributeSet();
070                                attributeSet.put(KualiStudentKimAttributes.QUALIFICATION_ORG_ID, nextNodeOrgInfo.getId());
071                                attributeSets.add(attributeSet);
072                            }
073                        }
074                    }
075                }
076                // if no org is found then use the org on the route node instance
077                if (attributeSets.isEmpty()) {
078                    OrgInfo currentNodeOrg = getOrganization(orgIdValue);
079                    AttributeSet attributeSet = new AttributeSet();
080                    attributeSet.put(KualiStudentKimAttributes.QUALIFICATION_ORG_ID, currentNodeOrg.getId());
081                    attributeSets.add(attributeSet);
082                }
083                return attributeSets;
084            } catch (Exception e) {
085                LOG.error("Error getting organization(s) or organization relations", e);
086                throw new RuntimeException(e);
087            }
088        }
089    
090        protected OrgInfo getOrganization(String orgId) throws Exception {
091            try {
092                return getOrganizationService().getOrganization(orgId);
093            } catch (DoesNotExistException e) {
094                LOG.error("No valid organization found for id '" + orgId + "'", e);
095                throw e;
096            }
097        }
098    
099    }