Coverage Report - org.kuali.student.lum.workflow.qualifierresolver.StaticOrganizationQualifierResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
StaticOrganizationQualifierResolver
0%
0/32
0%
0/4
5.5
 
 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.LinkedHashMap;
 9  
 import java.util.List;
 10  
 import java.util.Map;
 11  
 
 12  
 import javax.xml.parsers.DocumentBuilder;
 13  
 import javax.xml.parsers.DocumentBuilderFactory;
 14  
 import javax.xml.xpath.XPath;
 15  
 import javax.xml.xpath.XPathConstants;
 16  
 
 17  
 import org.kuali.rice.kew.engine.RouteContext;
 18  
 import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
 19  
 import org.kuali.rice.student.bo.KualiStudentKimAttributes;
 20  
 import org.kuali.student.core.organization.dto.OrgInfo;
 21  
 import org.w3c.dom.DOMException;
 22  
 import org.w3c.dom.Document;
 23  
 import org.w3c.dom.Node;
 24  
 import org.w3c.dom.NodeList;
 25  
 import org.xml.sax.InputSource;
 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  0
 public class StaticOrganizationQualifierResolver extends AbstractOrganizationServiceQualifierResolver {
 47  0
     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  
     @Override
 55  
     public List<Map<String,String>> resolve(RouteContext context) {
 56  0
         List<Map<String,String>> attributeSets = new ArrayList<Map<String,String>>();
 57  0
         XPath xPath = XPathHelper.newXPath();
 58  
         NodeList organizationElements;
 59  
         try {
 60  0
             DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 61  0
             Document document = db.parse(new InputSource(new StringReader(context.getNodeInstance().getRouteNode().getContentFragment())));
 62  0
             organizationElements = (NodeList) xPath.evaluate("//" + getOrganizationIdXmlTagName(), document, XPathConstants.NODESET);
 63  0
         } catch (Exception e) {
 64  0
             LOG.error(e);
 65  0
             throw new RuntimeException("Encountered an issue fetching organization ids using xml tag name '" + getOrganizationIdXmlTagName() + "'.", e);
 66  0
         }
 67  0
         if (organizationElements.getLength() == 0) {
 68  0
             LOG.error("No organizations found in Route Node xml configuration using xml tag name '" + getOrganizationIdXmlTagName() + "'");
 69  0
             throw new RuntimeException("No organizations found in Route Node xml configuration using xml tag name '" + getOrganizationIdXmlTagName() + "'");
 70  
         }
 71  0
         String orgId = "";
 72  
         try {
 73  0
             for (int i = 0; i < organizationElements.getLength(); i++) {
 74  0
                 Node organizationElement = organizationElements.item(i);
 75  0
                 orgId = "";
 76  0
                 orgId = organizationElement.getTextContent();
 77  0
                 OrgInfo orgInfo = getOrganizationService().getOrganization(orgId);
 78  0
                 Map<String,String> attrSet = new LinkedHashMap<String,String>();
 79  0
                 attrSet.put(KualiStudentKimAttributes.QUALIFICATION_ORG_ID, orgInfo.getId());
 80  0
                 attributeSets.add(attrSet);
 81  
             }
 82  0
         } catch (DOMException e) {
 83  0
             LOG.error(e);
 84  0
             throw new RuntimeException("Error getting organization from XML node", e);
 85  0
         } catch (Exception e) {
 86  0
             LOG.error(e);
 87  0
             throw new RuntimeException("Error getting organization with id '" + orgId + "' from OrganizationService", e);
 88  0
         }
 89  0
         return attributeSets;
 90  
     }
 91  
 
 92  
     protected String getOrganizationIdXmlTagName() {
 93  0
         return ROUTE_NODE_ORGANIZATION_ID_XML_TAG_NAME;
 94  
     }
 95  
 }