View Javadoc

1   /**
2    *
3    */
4   package org.kuali.student.lum.workflow.qualifierresolver;
5   
6   import org.kuali.rice.kew.engine.RouteContext;
7   import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
8   import org.kuali.rice.student.bo.KualiStudentKimAttributes;
9   import org.kuali.student.r2.common.dto.ContextInfo;
10  import org.kuali.student.r2.core.organization.dto.OrgInfo;
11  import org.w3c.dom.DOMException;
12  import org.w3c.dom.Document;
13  import org.w3c.dom.Node;
14  import org.w3c.dom.NodeList;
15  import org.xml.sax.InputSource;
16  
17  import javax.xml.parsers.DocumentBuilder;
18  import javax.xml.parsers.DocumentBuilderFactory;
19  import javax.xml.xpath.XPath;
20  import javax.xml.xpath.XPathConstants;
21  import java.io.StringReader;
22  import java.util.ArrayList;
23  import java.util.LinkedHashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  /**
28   * A QualifierResolver class that takes one or more organization ids from the Route Node configuration XML on the
29   * document type and uses those organizations as the qualifiers.
30   *
31   * <p>
32   * A sample of the Route Node configuration:
33   * <p>
34   *
35   * <pre>
36   * {@code
37   * <role name="Senate Review">
38   *   <activationType>P</activationType>
39   *   <qualifierResolverClass>org.kuali.student.lum.workflow.qualifierresolver.StaticOrganizationQualifierResolver</qualifierResolverClass>
40   *   <organizationId>141</organizationId>
41   * </role>
42   * }
43   * </pre>
44   *
45   */
46  public class StaticOrganizationQualifierResolver extends AbstractOrganizationServiceQualifierResolver {
47      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(StaticOrganizationQualifierResolver.class);
48  
49      protected static final String ROUTE_NODE_ORGANIZATION_ID_XML_TAG_NAME = "organizationId";
50  
51      /**
52       * @see org.kuali.rice.kew.role.QualifierResolver#resolve(org.kuali.rice.kew.engine.RouteContext)
53       */
54      public List<Map<String,String>> resolve(RouteContext context, ContextInfo contextInfo) {
55          List<Map<String,String>> attributeSets = new ArrayList<Map<String,String>>();
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().getOrg(orgId, contextInfo);
77                  Map<String,String> attrSet = new LinkedHashMap<String,String>();
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  
95      @Override
96      // TODO KSCM-392 we added the logic suplied in ks1.3 still neeeds to be tested.
97      public List<Map<String, String>> resolve(RouteContext context) {
98          List<Map<String,String>> attributeSets = new ArrayList<Map<String,String>>();
99          XPath xPath = XPathHelper.newXPath();
100         NodeList organizationElements;
101         try {
102             DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
103             Document document = db.parse(new InputSource(new StringReader(context.getNodeInstance().getRouteNode().getContentFragment())));
104             organizationElements = (NodeList) xPath.evaluate("//" + getOrganizationIdXmlTagName(), document, XPathConstants.NODESET);
105         } catch (Exception e) {
106             LOG.error(e);
107             throw new RuntimeException("Encountered an issue fetching organization ids using xml tag name '" + getOrganizationIdXmlTagName() + "'.", e);
108         }
109         if (organizationElements.getLength() == 0) {
110             LOG.error("No organizations found in Route Node xml configuration using xml tag name '" + getOrganizationIdXmlTagName() + "'");
111             throw new RuntimeException("No organizations found in Route Node xml configuration using xml tag name '" + getOrganizationIdXmlTagName() + "'");
112         }
113         String orgId = "";
114         try {
115             for (int i = 0; i < organizationElements.getLength(); i++) {
116                 Node organizationElement = organizationElements.item(i);
117                 orgId = "";
118                 orgId = organizationElement.getTextContent();
119                 OrgInfo orgInfo = getOrganizationService().getOrg(orgId, new ContextInfo());
120                 Map<String,String> attrSet = new LinkedHashMap<String,String>();
121                 attrSet.put(KualiStudentKimAttributes.QUALIFICATION_ORG_ID, orgInfo.getId());
122                 attributeSets.add(attrSet);
123             }
124         } catch (DOMException e) {
125             LOG.error(e);
126             throw new RuntimeException("Error getting organization from XML node", e);
127         } catch (Exception e) {
128             LOG.error(e);
129             throw new RuntimeException("Error getting organization with id '" + orgId + "' from OrganizationService", e);
130         }
131         return attributeSets;
132     }
133 }