View Javadoc

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   * A QualifierResolver class that takes one or more organization ids from the Route Node configuration XML on the
28   * document type and uses those organizations as the qualifiers.
29   * 
30   * <p>
31   * A sample of the Route Node configuration:
32   * <p>
33   * 
34   * <pre>
35   * {@code
36   * <role name="Senate Review">
37   *   <activationType>P</activationType>
38   *   <qualifierResolverClass>org.kuali.student.lum.workflow.qualifierresolver.StaticOrganizationQualifierResolver</qualifierResolverClass>
39   *   <organizationId>141</organizationId>
40   * </role>
41   * }
42   * </pre>
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       * @see org.kuali.rice.kew.role.QualifierResolver#resolve(org.kuali.rice.kew.engine.RouteContext)
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  }