001package org.kuali.student.lum.workflow.qualifierresolver;
002
003import java.util.ArrayList;
004import java.util.List;
005import java.util.Map;
006
007import org.apache.commons.lang.StringUtils;
008import org.kuali.rice.kew.engine.RouteContext;
009import org.kuali.rice.kew.engine.node.RouteNodeUtils;
010import org.kuali.student.r2.core.search.dto.SearchResultRowInfo;
011
012public class ParentOrganizationQualifierResolver extends
013                AbstractOrganizationServiceQualifierResolver {
014    protected static final String ROUTE_NODE_XML_ORG_ID_QUALIFIER_KEY = "organizationIdQualifierKey";
015    protected static final String ROUTE_NODE_XML_USE_NON_DERIVED_ROLES = "useNonDerivedRoles";
016    
017    protected static final String ROUTE_NODE_XML_ORG_RELATION_TYPE = "organizationRelationType";
018    protected static final String ROUTE_NODE_XML_RELATED_ORG_TYPE = "relatedOrganizationType";
019    
020        @Override
021        public List<Map<String,String>> resolve(RouteContext context) {
022        List<Map<String,String>> attributeSets = new ArrayList<Map<String,String>>();
023        
024        String orgIdKey = getNodeSpecificOrganizationIdAttributeSetKey(context);
025        String orgRelationType = getOrganizationRelationTypeCode(context);
026        String relatedOrgType = getRelatedOrganizationTypeCode(context);
027        
028        for (String orgId : getOrganizationIdsFromDocumentContent(context)) {
029            List<SearchResultRowInfo> results = relatedOrgsFromOrgId(orgId, orgRelationType, relatedOrgType);
030            attributeSets.addAll(attributeSetFromSearchResult(results, orgIdKey));
031        }
032        return attributeSets;
033        }
034        
035    protected String getRelatedOrganizationTypeCode(RouteContext context) {
036         String relatedOrganizationType = RouteNodeUtils.getValueOfCustomProperty(context.getNodeInstance().getRouteNode(), ROUTE_NODE_XML_RELATED_ORG_TYPE);
037         if(StringUtils.isBlank(relatedOrganizationType)){
038                 throw new RuntimeException("Cannot find required XML element '" + ROUTE_NODE_XML_RELATED_ORG_TYPE + "' on the Route Node XML configuration.");
039         }
040         return relatedOrganizationType;
041        }
042
043        protected String getOrganizationRelationTypeCode(RouteContext context) {
044         String orgRelationType = RouteNodeUtils.getValueOfCustomProperty(context.getNodeInstance().getRouteNode(), ROUTE_NODE_XML_ORG_RELATION_TYPE);
045         if(StringUtils.isBlank(orgRelationType)){
046                 throw new RuntimeException("Cannot find required XML element '" + ROUTE_NODE_XML_ORG_RELATION_TYPE + "' on the Route Node XML configuration.");
047         }
048         return orgRelationType;
049        }
050
051        public String getNodeSpecificOrganizationIdAttributeSetKey(RouteContext context) {
052        String organizationIdFieldKey = RouteNodeUtils.getValueOfCustomProperty(context.getNodeInstance().getRouteNode(), ROUTE_NODE_XML_ORG_ID_QUALIFIER_KEY);
053        if (StringUtils.isBlank(organizationIdFieldKey)) {
054            if (usesNonDerivedOrganizationRoles(context)) {
055                throw new RuntimeException("Cannot find required XML element '" + ROUTE_NODE_XML_ORG_ID_QUALIFIER_KEY + "' on the Route Node XML configuration.");
056            }
057        }
058        return organizationIdFieldKey;
059    }
060    
061    public Boolean usesNonDerivedOrganizationRoles(RouteContext context) {
062        String useNonDerivedOrganizationRoles = RouteNodeUtils.getValueOfCustomProperty(context.getNodeInstance().getRouteNode(), ROUTE_NODE_XML_USE_NON_DERIVED_ROLES);
063        if (StringUtils.isNotBlank(useNonDerivedOrganizationRoles)) {
064            return Boolean.valueOf(useNonDerivedOrganizationRoles);
065        }
066        return true;
067    }
068}