1
2
3
4 package org.kuali.student.lum.workflow.qualifierresolver;
5
6 import java.io.StringReader;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.xpath.XPath;
13 import javax.xml.xpath.XPathConstants;
14
15 import org.kuali.rice.kew.engine.RouteContext;
16 import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
17 import org.kuali.rice.kim.bo.types.dto.AttributeSet;
18 import org.kuali.rice.student.bo.KualiStudentKimAttributes;
19 import org.kuali.student.core.organization.dto.OrgInfo;
20 import org.w3c.dom.DOMException;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Node;
23 import org.w3c.dom.NodeList;
24 import org.xml.sax.InputSource;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class StaticOrganizationQualifierResolver extends AbstractOrganizationServiceQualifierResolver {
46 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(StaticOrganizationQualifierResolver.class);
47
48 protected static final String ROUTE_NODE_ORGANIZATION_ID_XML_TAG_NAME = "organizationId";
49
50
51
52
53 @Override
54 public List<AttributeSet> resolve(RouteContext context) {
55 List<AttributeSet> attributeSets = new ArrayList<AttributeSet>();
56 XPath xPath = XPathHelper.newXPath();
57 NodeList organizationElements;
58 try {
59 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
60 Document document = db.parse(new InputSource(new StringReader(context.getNodeInstance().getRouteNode().getContentFragment())));
61 organizationElements = (NodeList) xPath.evaluate("//" + getOrganizationIdXmlTagName(), document, XPathConstants.NODESET);
62 } catch (Exception e) {
63 LOG.error(e);
64 throw new RuntimeException("Encountered an issue fetching organization ids using xml tag name '" + getOrganizationIdXmlTagName() + "'.", e);
65 }
66 if (organizationElements.getLength() == 0) {
67 LOG.error("No organizations found in Route Node xml configuration using xml tag name '" + getOrganizationIdXmlTagName() + "'");
68 throw new RuntimeException("No organizations found in Route Node xml configuration using xml tag name '" + getOrganizationIdXmlTagName() + "'");
69 }
70 String orgId = "";
71 try {
72 for (int i = 0; i < organizationElements.getLength(); i++) {
73 Node organizationElement = organizationElements.item(i);
74 orgId = "";
75 orgId = organizationElement.getTextContent();
76 OrgInfo orgInfo = getOrganizationService().getOrganization(orgId);
77 AttributeSet attrSet = new AttributeSet();
78 attrSet.put(KualiStudentKimAttributes.QUALIFICATION_ORG_ID, orgInfo.getId());
79 attributeSets.add(attrSet);
80 }
81 } catch (DOMException e) {
82 LOG.error(e);
83 throw new RuntimeException("Error getting organization from XML node", e);
84 } catch (Exception e) {
85 LOG.error(e);
86 throw new RuntimeException("Error getting organization with id '" + orgId + "' from OrganizationService", e);
87 }
88 return attributeSets;
89 }
90
91 protected String getOrganizationIdXmlTagName() {
92 return ROUTE_NODE_ORGANIZATION_ID_XML_TAG_NAME;
93 }
94 }