| 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.common.search.dto.SearchParam; |
| 27 | |
import org.kuali.student.common.search.dto.SearchRequest; |
| 28 | |
import org.kuali.student.common.search.dto.SearchResult; |
| 29 | |
import org.kuali.student.common.search.dto.SearchResultCell; |
| 30 | |
import org.kuali.student.common.search.dto.SearchResultRow; |
| 31 | |
import org.kuali.student.core.organization.service.OrganizationService; |
| 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 | |
|
| 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 | |
|
| 48 | |
|
| 49 | |
public static final String DOCUMENT_CONTENT_XML_ROOT_ELEMENT_NAME = "info"; |
| 50 | |
|
| 51 | |
public static final String KUALI_ORG_TYPE_CURRICULUM_PARENT = "kuali.org.CurriculumParent"; |
| 52 | |
public static final String KUALI_ORG_HIERARCHY_CURRICULUM = "kuali.org.hierarchy.Curriculum"; |
| 53 | |
public static final String KUALI_ORG_DEPARTMENT = "kuali.org.Department"; |
| 54 | |
public static final String KUALI_ORG_COLLEGE = "kuali.org.College"; |
| 55 | |
public static final String KUALI_ORG_COC = "kuali.org.COC"; |
| 56 | |
public static final String KUALI_ORG_DIVISION = "kuali.org.Division"; |
| 57 | |
public static final String KUALI_ORG_PROGRAM = "kuali.org.Program"; |
| 58 | |
|
| 59 | |
private OrganizationService organizationService; |
| 60 | |
|
| 61 | |
protected OrganizationService getOrganizationService() { |
| 62 | 0 | if (null == organizationService) { |
| 63 | 0 | organizationService = (OrganizationService) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/organization", "OrganizationService")); |
| 64 | |
} |
| 65 | 0 | return organizationService; |
| 66 | |
} |
| 67 | |
|
| 68 | |
protected void setOrganizationService(OrganizationService orgSvc) { |
| 69 | 0 | organizationService = orgSvc; |
| 70 | 0 | } |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
protected Set<String> getOrganizationIdsFromDocumentContent(RouteContext context) { |
| 81 | 0 | String baseXpathExpression = "/" + KEWConstants.DOCUMENT_CONTENT_ELEMENT + "/" + KEWConstants.APPLICATION_CONTENT_ELEMENT + "/" + DOCUMENT_CONTENT_XML_ROOT_ELEMENT_NAME; |
| 82 | 0 | String orgXpathExpression = "./" + getOrganizationIdDocumentContentFieldKey(context); |
| 83 | 0 | Document xmlContent = context.getDocumentContent().getDocument(); |
| 84 | 0 | XPath xPath = XPathHelper.newXPath(); |
| 85 | |
try { |
| 86 | 0 | NodeList baseElements = (NodeList) xPath.evaluate(baseXpathExpression, xmlContent, XPathConstants.NODESET); |
| 87 | 0 | if (LOG.isDebugEnabled()) { |
| 88 | 0 | LOG.debug("Found " + baseElements.getLength() + " baseElements to parse for AttributeSets using document XML:"); |
| 89 | 0 | XmlHelper.printDocumentStructure(xmlContent); |
| 90 | |
} |
| 91 | 0 | Set<String> distinctiveOrganizationIds = new HashSet<String>(); |
| 92 | 0 | for (int i = 0; i < baseElements.getLength(); i++) { |
| 93 | 0 | Node baseNode = baseElements.item(i); |
| 94 | 0 | NodeList attributes = (NodeList) xPath.evaluate(orgXpathExpression, baseNode, XPathConstants.NODESET); |
| 95 | 0 | for (int j = 0; j < attributes.getLength(); j++) { |
| 96 | 0 | Element attributeElement = (Element) attributes.item(j); |
| 97 | 0 | distinctiveOrganizationIds.add(attributeElement.getTextContent()); |
| 98 | |
} |
| 99 | |
} |
| 100 | 0 | return distinctiveOrganizationIds; |
| 101 | 0 | } catch (XPathExpressionException e) { |
| 102 | 0 | throw new RuntimeException("Encountered an issue executing XPath.", e); |
| 103 | |
} |
| 104 | |
} |
| 105 | |
|
| 106 | |
protected String getOrganizationIdDocumentContentFieldKey(RouteContext context) { |
| 107 | 0 | String organizationIdFieldKey = RouteNodeUtils.getValueOfCustomProperty(context.getNodeInstance().getRouteNode(), DOCUMENT_CONTENT_XML_ORG_ID_KEY); |
| 108 | 0 | if (StringUtils.isBlank(organizationIdFieldKey)) { |
| 109 | 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 + "'."); |
| 110 | 0 | organizationIdFieldKey = DOCUMENT_CONTENT_XML_DEFAULT_ORG_ID_KEY; |
| 111 | |
} |
| 112 | 0 | return organizationIdFieldKey; |
| 113 | |
} |
| 114 | |
|
| 115 | |
protected List<SearchResultRow> relatedOrgsFromOrgId(String orgId, String relationType, String relatedOrgType) { |
| 116 | 0 | List<SearchResultRow> results = null; |
| 117 | 0 | if (null != orgId) { |
| 118 | 0 | List<SearchParam> queryParamValues = new ArrayList<SearchParam>(3); |
| 119 | 0 | SearchParam qpRelType = new SearchParam(); |
| 120 | 0 | qpRelType.setKey("org.queryParam.relationType"); |
| 121 | 0 | qpRelType.setValue(relationType); |
| 122 | 0 | queryParamValues.add(qpRelType); |
| 123 | |
|
| 124 | 0 | SearchParam qpOrgId = new SearchParam(); |
| 125 | 0 | qpOrgId.setKey("org.queryParam.orgId"); |
| 126 | 0 | qpOrgId.setValue(orgId); |
| 127 | 0 | queryParamValues.add(qpOrgId); |
| 128 | |
|
| 129 | 0 | SearchParam qpRelOrgType = new SearchParam(); |
| 130 | 0 | qpRelOrgType.setKey("org.queryParam.relatedOrgType"); |
| 131 | 0 | qpRelOrgType.setValue(relatedOrgType); |
| 132 | 0 | queryParamValues.add(qpRelOrgType); |
| 133 | |
|
| 134 | 0 | SearchRequest searchRequest = new SearchRequest(); |
| 135 | 0 | searchRequest.setSearchKey("org.search.orgQuickViewByRelationTypeRelatedOrgTypeOrgId"); |
| 136 | 0 | searchRequest.setParams(queryParamValues); |
| 137 | |
try { |
| 138 | 0 | SearchResult result = getOrganizationService().search(searchRequest); |
| 139 | 0 | results = result.getRows(); |
| 140 | 0 | } catch (Exception e) { |
| 141 | 0 | LOG.error("Error calling org service"); |
| 142 | 0 | throw new RuntimeException(e); |
| 143 | 0 | } |
| 144 | |
} |
| 145 | 0 | return results; |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
protected List<AttributeSet> attributeSetFromSearchResult(List<SearchResultRow> results, String orgIdKey) { |
| 152 | 0 | List<AttributeSet> returnAttrSetList = new ArrayList<AttributeSet>(); |
| 153 | 0 | if (results != null) { |
| 154 | 0 | for (SearchResultRow result : results) { |
| 155 | 0 | AttributeSet attributeSet = new AttributeSet(); |
| 156 | 0 | String resolvedOrgId = ""; |
| 157 | 0 | for (SearchResultCell resultCell : result.getCells()) { |
| 158 | 0 | if ("org.resultColumn.orgId".equals(resultCell.getKey())) { |
| 159 | 0 | resolvedOrgId = resultCell.getValue(); |
| 160 | 0 | break; |
| 161 | |
} |
| 162 | |
} |
| 163 | 0 | if (orgIdKey != null) { |
| 164 | 0 | attributeSet.put(orgIdKey, resolvedOrgId); |
| 165 | |
} |
| 166 | 0 | attributeSet.put(KualiStudentKimAttributes.QUALIFICATION_ORG_ID, resolvedOrgId); |
| 167 | 0 | returnAttrSetList.add(attributeSet); |
| 168 | 0 | } |
| 169 | |
} |
| 170 | 0 | return returnAttrSetList; |
| 171 | |
} |
| 172 | |
|
| 173 | |
} |