Coverage Report - org.kuali.student.lum.workflow.qualifierresolver.AbstractOrganizationServiceQualifierResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractOrganizationServiceQualifierResolver
0%
0/75
0%
0/26
4
 
 1  
 /**
 2  
  * 
 3  
  */
 4  
 package org.kuali.student.lum.workflow.qualifierresolver;
 5  
 
 6  
 import java.util.ArrayList;
 7  
 import java.util.HashSet;
 8  
 import java.util.List;
 9  
 import java.util.Set;
 10  
 
 11  
 import javax.xml.namespace.QName;
 12  
 import javax.xml.xpath.XPath;
 13  
 import javax.xml.xpath.XPathConstants;
 14  
 import javax.xml.xpath.XPathExpressionException;
 15  
 
 16  
 import org.apache.commons.lang.StringUtils;
 17  
 import org.kuali.rice.core.resourceloader.GlobalResourceLoader;
 18  
 import org.kuali.rice.kew.engine.RouteContext;
 19  
 import org.kuali.rice.kew.engine.node.RouteNodeUtils;
 20  
 import org.kuali.rice.kew.role.QualifierResolver;
 21  
 import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
 22  
 import org.kuali.rice.kew.util.KEWConstants;
 23  
 import org.kuali.rice.kew.util.XmlHelper;
 24  
 import org.kuali.rice.kim.bo.types.dto.AttributeSet;
 25  
 import org.kuali.rice.student.bo.KualiStudentKimAttributes;
 26  
 import org.kuali.student.core.organization.service.OrganizationService;
 27  
 import org.kuali.student.core.search.dto.SearchParam;
 28  
 import org.kuali.student.core.search.dto.SearchRequest;
 29  
 import org.kuali.student.core.search.dto.SearchResult;
 30  
 import org.kuali.student.core.search.dto.SearchResultCell;
 31  
 import org.kuali.student.core.search.dto.SearchResultRow;
 32  
 import org.w3c.dom.Document;
 33  
 import org.w3c.dom.Element;
 34  
 import org.w3c.dom.Node;
 35  
 import org.w3c.dom.NodeList;
 36  
 
 37  
 /**
 38  
  * An abstract base class that consolidates convenience methods for using the {@link OrganizationService} class.
 39  
  * 
 40  
  */
 41  0
 public abstract class AbstractOrganizationServiceQualifierResolver implements QualifierResolver {
 42  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AbstractOrganizationServiceQualifierResolver.class);
 43  
 
 44  
     protected static final String DOCUMENT_CONTENT_XML_DEFAULT_ORG_ID_KEY = "orgId";
 45  
     protected static final String DOCUMENT_CONTENT_XML_ORG_ID_KEY = "organizationIdDocumentContentKey";
 46  
 
 47  
     // below string MUST match
 48  
     // org.kuali.student.core.assembly.transform.WorkflowFilter.DOCUMENT_CONTENT_XML_ROOT_ELEMENT_NAME constant
 49  
     public static final String DOCUMENT_CONTENT_XML_ROOT_ELEMENT_NAME = "info";
 50  
 
 51  
     private OrganizationService organizationService;
 52  
 
 53  
     protected OrganizationService getOrganizationService() {
 54  0
         if (null == organizationService) {
 55  0
             organizationService = (OrganizationService) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/organization", "OrganizationService"));
 56  
         }
 57  0
         return organizationService;
 58  
     }
 59  
 
 60  
     protected void setOrganizationService(OrganizationService orgSvc) {
 61  0
         organizationService = orgSvc;
 62  0
     }
 63  
 
 64  
     /**
 65  
      * Method to fetch the organization ids from the KEW document content XML
 66  
      * 
 67  
      * @param context
 68  
      *            - RouteContext class that holds data about the current document's routing and data
 69  
      * @return A list of organization ids that are listed in the XML (may have duplicates if duplicates are allowed by
 70  
      *         KS code)
 71  
      */
 72  
     protected Set<String> getOrganizationIdsFromDocumentContent(RouteContext context) {
 73  0
         String baseXpathExpression = "/" + KEWConstants.DOCUMENT_CONTENT_ELEMENT + "/" + KEWConstants.APPLICATION_CONTENT_ELEMENT + "/" + DOCUMENT_CONTENT_XML_ROOT_ELEMENT_NAME;
 74  0
         String orgXpathExpression = "./" + getOrganizationIdDocumentContentFieldKey(context);
 75  0
         Document xmlContent = context.getDocumentContent().getDocument();
 76  0
         XPath xPath = XPathHelper.newXPath();
 77  
         try {
 78  0
             NodeList baseElements = (NodeList) xPath.evaluate(baseXpathExpression, xmlContent, XPathConstants.NODESET);
 79  0
             if (LOG.isDebugEnabled()) {
 80  0
                 LOG.debug("Found " + baseElements.getLength() + " baseElements to parse for AttributeSets using document XML:");
 81  0
                 XmlHelper.printDocumentStructure(xmlContent);
 82  
             }
 83  0
             Set<String> distinctiveOrganizationIds = new HashSet<String>();
 84  0
             for (int i = 0; i < baseElements.getLength(); i++) {
 85  0
                 Node baseNode = baseElements.item(i);
 86  0
                 NodeList attributes = (NodeList) xPath.evaluate(orgXpathExpression, baseNode, XPathConstants.NODESET);
 87  0
                 for (int j = 0; j < attributes.getLength(); j++) {
 88  0
                     Element attributeElement = (Element) attributes.item(j);
 89  0
                     distinctiveOrganizationIds.add(attributeElement.getTextContent());
 90  
                 }
 91  
             }
 92  0
             return distinctiveOrganizationIds;
 93  0
         } catch (XPathExpressionException e) {
 94  0
             throw new RuntimeException("Encountered an issue executing XPath.", e);
 95  
         }
 96  
     }
 97  
 
 98  
     protected String getOrganizationIdDocumentContentFieldKey(RouteContext context) {
 99  0
         String organizationIdFieldKey = RouteNodeUtils.getValueOfCustomProperty(context.getNodeInstance().getRouteNode(), DOCUMENT_CONTENT_XML_ORG_ID_KEY);
 100  0
         if (StringUtils.isBlank(organizationIdFieldKey)) {
 101  0
             LOG.info("Cannot find element '" + DOCUMENT_CONTENT_XML_ORG_ID_KEY + "' on Route Node XML configuration. Will use default value of '" + DOCUMENT_CONTENT_XML_DEFAULT_ORG_ID_KEY + "'.");
 102  0
             organizationIdFieldKey = DOCUMENT_CONTENT_XML_DEFAULT_ORG_ID_KEY;
 103  
         }
 104  0
         return organizationIdFieldKey;
 105  
     }
 106  
 
 107  
     protected List<SearchResultRow> relatedOrgsFromOrgId(String orgId, String relationType, String relatedOrgType) {
 108  0
         List<SearchResultRow> results = null;
 109  0
         if (null != orgId) {
 110  0
             List<SearchParam> queryParamValues = new ArrayList<SearchParam>(2);
 111  0
             SearchParam qpRelType = new SearchParam();
 112  0
             qpRelType.setKey("org.queryParam.relationType");
 113  0
             qpRelType.setValue(relationType);
 114  0
             queryParamValues.add(qpRelType);
 115  
 
 116  0
             SearchParam qpOrgId = new SearchParam();
 117  0
             qpOrgId.setKey("org.queryParam.orgId");
 118  0
             qpOrgId.setValue(orgId);
 119  0
             queryParamValues.add(qpOrgId);
 120  
 
 121  0
             SearchParam qpRelOrgType = new SearchParam();
 122  0
             qpRelOrgType.setKey("org.queryParam.relatedOrgType");
 123  0
             qpRelOrgType.setValue(relatedOrgType);
 124  0
             queryParamValues.add(qpRelOrgType);
 125  
 
 126  0
             SearchRequest searchRequest = new SearchRequest();
 127  0
             searchRequest.setSearchKey("org.search.orgQuickViewByRelationTypeRelatedOrgTypeOrgId");
 128  0
             searchRequest.setParams(queryParamValues);
 129  
             try {
 130  0
                 SearchResult result = getOrganizationService().search(searchRequest);
 131  0
                 results = result.getRows();
 132  0
             } catch (Exception e) {
 133  0
                 LOG.error("Error calling org service");
 134  0
                 throw new RuntimeException(e);
 135  0
             }
 136  
         }
 137  0
         return results;
 138  
     }
 139  
 
 140  
     protected List<AttributeSet> attributeSetFromSearchResult(List<SearchResultRow> results, String orgShortNameKey, String orgIdKey) {
 141  0
         List<AttributeSet> returnAttrSetList = new ArrayList<AttributeSet>();
 142  0
         if (results != null) {
 143  0
             for (SearchResultRow result : results) {
 144  0
                 AttributeSet attributeSet = new AttributeSet();
 145  0
                 String resolvedOrgId = "";
 146  0
                 String resolvedOrgShortName = "";
 147  0
                 for (SearchResultCell resultCell : result.getCells()) {
 148  0
                     if ("org.resultColumn.orgId".equals(resultCell.getKey())) {
 149  0
                         resolvedOrgId = resultCell.getValue();
 150  0
                     } else if ("org.resultColumn.orgShortName".equals(resultCell.getKey())) {
 151  0
                         resolvedOrgShortName = resultCell.getValue();
 152  
                     }
 153  
                 }
 154  0
                 if (orgShortNameKey != null) {
 155  0
                     attributeSet.put(orgShortNameKey, resolvedOrgShortName);
 156  
                 }
 157  0
                 if (orgIdKey != null) {
 158  0
                     attributeSet.put(orgIdKey, resolvedOrgId);
 159  
                 }
 160  0
                 attributeSet.put(KualiStudentKimAttributes.QUALIFICATION_ORG, resolvedOrgShortName);
 161  0
                 attributeSet.put(KualiStudentKimAttributes.QUALIFICATION_ORG_ID, resolvedOrgId);
 162  0
                 returnAttrSetList.add(attributeSet);
 163  0
             }
 164  
         }
 165  0
         return returnAttrSetList;
 166  
     }
 167  
 
 168  
 }