| 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 | 0 |  public class StaticOrganizationQualifierResolver extends AbstractOrganizationServiceQualifierResolver { | 
  | 46 | 0 |      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 | 0 |          List<AttributeSet> attributeSets = new ArrayList<AttributeSet>(); | 
  | 56 | 0 |          XPath xPath = XPathHelper.newXPath(); | 
  | 57 |  |          NodeList organizationElements; | 
  | 58 |  |          try { | 
  | 59 | 0 |              DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); | 
  | 60 | 0 |              Document document = db.parse(new InputSource(new StringReader(context.getNodeInstance().getRouteNode().getContentFragment()))); | 
  | 61 | 0 |              organizationElements = (NodeList) xPath.evaluate("//" + getOrganizationIdXmlTagName(), document, XPathConstants.NODESET); | 
  | 62 | 0 |          } catch (Exception e) { | 
  | 63 | 0 |              LOG.error(e); | 
  | 64 | 0 |              throw new RuntimeException("Encountered an issue fetching organization ids using xml tag name '" + getOrganizationIdXmlTagName() + "'.", e); | 
  | 65 | 0 |          } | 
  | 66 | 0 |          if (organizationElements.getLength() == 0) { | 
  | 67 | 0 |              LOG.error("No organizations found in Route Node xml configuration using xml tag name '" + getOrganizationIdXmlTagName() + "'"); | 
  | 68 | 0 |              throw new RuntimeException("No organizations found in Route Node xml configuration using xml tag name '" + getOrganizationIdXmlTagName() + "'"); | 
  | 69 |  |          } | 
  | 70 | 0 |          String orgId = ""; | 
  | 71 |  |          try { | 
  | 72 | 0 |              for (int i = 0; i < organizationElements.getLength(); i++) { | 
  | 73 | 0 |                  Node organizationElement = organizationElements.item(i); | 
  | 74 | 0 |                  orgId = ""; | 
  | 75 | 0 |                  orgId = organizationElement.getTextContent(); | 
  | 76 | 0 |                  OrgInfo orgInfo = getOrganizationService().getOrganization(orgId); | 
  | 77 | 0 |                  AttributeSet attrSet = new AttributeSet(); | 
  | 78 | 0 |                  attrSet.put(KualiStudentKimAttributes.QUALIFICATION_ORG_ID, orgInfo.getId()); | 
  | 79 | 0 |                  attributeSets.add(attrSet); | 
  | 80 |  |              } | 
  | 81 | 0 |          } catch (DOMException e) { | 
  | 82 | 0 |              LOG.error(e); | 
  | 83 | 0 |              throw new RuntimeException("Error getting organization from XML node", e); | 
  | 84 | 0 |          } catch (Exception e) { | 
  | 85 | 0 |              LOG.error(e); | 
  | 86 | 0 |              throw new RuntimeException("Error getting organization with id '" + orgId + "' from OrganizationService", e); | 
  | 87 | 0 |          } | 
  | 88 | 0 |          return attributeSets; | 
  | 89 |  |      } | 
  | 90 |  |   | 
  | 91 |  |      protected String getOrganizationIdXmlTagName() { | 
  | 92 | 0 |          return ROUTE_NODE_ORGANIZATION_ID_XML_TAG_NAME; | 
  | 93 |  |      } | 
  | 94 |  |  } |