| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |  package org.kuali.student.lum.workflow.node; | 
  | 5 |  |   | 
  | 6 |  |  import java.util.ArrayList; | 
  | 7 |  |  import java.util.HashSet; | 
  | 8 |  |  import java.util.List; | 
  | 9 |  |  import java.util.Set; | 
  | 10 |  |   | 
  | 11 |  |  import javax.xml.namespace.QName; | 
  | 12 |  |  import javax.xml.xpath.XPath; | 
  | 13 |  |  import javax.xml.xpath.XPathConstants; | 
  | 14 |  |  import javax.xml.xpath.XPathExpressionException; | 
  | 15 |  |   | 
  | 16 |  |  import org.apache.commons.lang.StringUtils; | 
  | 17 |  |  import org.kuali.rice.core.exception.RiceRuntimeException; | 
  | 18 |  |  import org.kuali.rice.core.resourceloader.GlobalResourceLoader; | 
  | 19 |  |  import org.kuali.rice.kew.doctype.bo.DocumentType; | 
  | 20 |  |  import org.kuali.rice.kew.engine.RouteContext; | 
  | 21 |  |  import org.kuali.rice.kew.engine.RouteHelper; | 
  | 22 |  |  import org.kuali.rice.kew.engine.node.Branch; | 
  | 23 |  |  import org.kuali.rice.kew.engine.node.DynamicNode; | 
  | 24 |  |  import org.kuali.rice.kew.engine.node.DynamicResult; | 
  | 25 |  |  import org.kuali.rice.kew.engine.node.NodeState; | 
  | 26 |  |  import org.kuali.rice.kew.engine.node.Process; | 
  | 27 |  |  import org.kuali.rice.kew.engine.node.RoleNode; | 
  | 28 |  |  import org.kuali.rice.kew.engine.node.RouteNode; | 
  | 29 |  |  import org.kuali.rice.kew.engine.node.RouteNodeInstance; | 
  | 30 |  |  import org.kuali.rice.kew.exception.WorkflowException; | 
  | 31 |  |  import org.kuali.rice.kew.role.RoleRouteModule; | 
  | 32 |  |  import org.kuali.rice.kew.rule.xmlrouting.XPathHelper; | 
  | 33 |  |  import org.kuali.rice.kew.service.KEWServiceLocator; | 
  | 34 |  |  import org.kuali.rice.kew.util.KEWConstants; | 
  | 35 |  |  import org.kuali.rice.kew.util.XmlHelper; | 
  | 36 |  |  import org.kuali.student.core.organization.dto.OrgInfo; | 
  | 37 |  |  import org.kuali.student.core.organization.dto.OrgOrgRelationInfo; | 
  | 38 |  |  import org.kuali.student.core.organization.service.OrganizationService; | 
  | 39 |  |  import org.kuali.student.lum.workflow.qualifierresolver.AbstractOrganizationServiceQualifierResolver; | 
  | 40 |  |  import org.kuali.student.lum.workflow.qualifierresolver.OrganizationCurriculumCommitteeQualifierResolver; | 
  | 41 |  |  import org.w3c.dom.Document; | 
  | 42 |  |  import org.w3c.dom.Element; | 
  | 43 |  |  import org.w3c.dom.NodeList; | 
  | 44 |  |   | 
  | 45 |  |   | 
  | 46 |  |   | 
  | 47 |  |   | 
  | 48 |  |   | 
  | 49 |  |   | 
  | 50 |  |   | 
  | 51 | 0 |  public class OrganizationDynamicNode implements DynamicNode { | 
  | 52 | 0 |      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OrganizationDynamicNode.class); | 
  | 53 |  |   | 
  | 54 |  |       | 
  | 55 |  |      protected static final String ORG_HIERARCHY_NODE = "Org Hierarchy Review"; | 
  | 56 |  |       | 
  | 57 |  |      public static final String NODE_STATE_ORG_ID_KEY = "org.id.key"; | 
  | 58 |  |   | 
  | 59 |  |       | 
  | 60 |  |   | 
  | 61 |  |   | 
  | 62 |  |   | 
  | 63 |  |   | 
  | 64 |  |   | 
  | 65 |  |   | 
  | 66 |  |      public static final String QUALIFIER_RESOLVER_ELEMENT = "qualifierResolver"; | 
  | 67 |  |      public static final String QUALIFIER_RESOLVER_CLASS_ELEMENT = "qualifierResolverClass"; | 
  | 68 |  |      public static final String RESPONSIBILITY_TEMPLATE_NAME_ELEMENT = "responsibilityTemplateName"; | 
  | 69 |  |      public static final String NAMESPACE_ELEMENT = "namespace"; | 
  | 70 |  |   | 
  | 71 |  |      private OrganizationService organizationService; | 
  | 72 |  |   | 
  | 73 |  |      public OrganizationService getOrganizationService() { | 
  | 74 | 0 |          if (null == organizationService) { | 
  | 75 | 0 |              organizationService = (OrganizationService) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/organization", "OrganizationService")); | 
  | 76 |  |          } | 
  | 77 | 0 |          return organizationService; | 
  | 78 |  |      } | 
  | 79 |  |   | 
  | 80 |  |      public void setOrganizationService(OrganizationService organizationService) { | 
  | 81 | 0 |          this.organizationService = organizationService; | 
  | 82 | 0 |      } | 
  | 83 |  |   | 
  | 84 |  |      @Override | 
  | 85 |  |      public DynamicResult transitioningInto(RouteContext context, RouteNodeInstance dynamicNodeInstance, RouteHelper helper) throws Exception { | 
  | 86 | 0 |          LOG.debug("Entering transitioningInto"); | 
  | 87 | 0 |          DocumentType docType = setUpDocumentType(context.getDocument().getDocumentType(), dynamicNodeInstance); | 
  | 88 |  |           | 
  | 89 | 0 |          String prototypeNodeName = ORG_HIERARCHY_NODE; | 
  | 90 | 0 |          RouteNode roleNodePrototype = docType.getNamedProcess(prototypeNodeName).getInitialRouteNode(); | 
  | 91 | 0 |          if (roleNodePrototype == null) { | 
  | 92 | 0 |              throw new WorkflowException("Couldn't locate node for name: " + prototypeNodeName); | 
  | 93 |  |          } | 
  | 94 |  |   | 
  | 95 | 0 |          List<String> orgIds = getInitialOrganizationIdsForRouting(context, dynamicNodeInstance, helper); | 
  | 96 | 0 |          if ((orgIds != null) && (orgIds.size() > 1)) { | 
  | 97 | 0 |              throw new RuntimeException("Found a total of " + orgIds.size() + " organizations for routing on document when only one is allowed."); | 
  | 98 |  |          } | 
  | 99 | 0 |          DynamicResult result = new DynamicResult(orgIds == null || orgIds.isEmpty(), null); | 
  | 100 | 0 |          for (String orgId : orgIds) { | 
  | 101 | 0 |              RouteNodeInstance nodeInstance = generateNextNodeInstance(orgId, roleNodePrototype, context, dynamicNodeInstance.getBranch(), helper); | 
  | 102 | 0 |              LOG.debug("Exiting transitioningInto with " + ((nodeInstance == null) ? "no" : "a") + " valid next node instance"); | 
  | 103 | 0 |              if (nodeInstance != null) { | 
  | 104 | 0 |                  result.setNextNodeInstance(nodeInstance); | 
  | 105 |  |              } | 
  | 106 | 0 |          } | 
  | 107 | 0 |          return result; | 
  | 108 |  |      } | 
  | 109 |  |   | 
  | 110 |  |       | 
  | 111 |  |   | 
  | 112 |  |   | 
  | 113 |  |   | 
  | 114 |  |   | 
  | 115 |  |   | 
  | 116 |  |   | 
  | 117 |  |   | 
  | 118 |  |   | 
  | 119 |  |   | 
  | 120 |  |   | 
  | 121 |  |   | 
  | 122 |  |   | 
  | 123 |  |   | 
  | 124 |  |   | 
  | 125 |  |      protected List<String> getInitialOrganizationIdsForRouting(RouteContext context, RouteNodeInstance dynamicNodeInstance, RouteHelper helper) { | 
  | 126 | 0 |          List<String> orgIds = getOrganizationIdsFromDocumentContent(context); | 
  | 127 | 0 |          if ((orgIds != null) && (orgIds.size() > 1)) { | 
  | 128 | 0 |              throw new RuntimeException("Found a total of " + orgIds.size() + " organizations for routing on document when only one is allowed."); | 
  | 129 |  |          } | 
  | 130 |  |   | 
  | 131 |  |          try { | 
  | 132 | 0 |              for (String orgId : orgIds) { | 
  | 133 | 0 |                  OrgInfo orgInfo = getOrganizationService().getOrganization(orgId); | 
  | 134 | 0 |                  LOG.debug("Org on Document: " + getOrgInfoForPrint(orgInfo)); | 
  | 135 | 0 |                  List<OrgOrgRelationInfo> orgRelationInfos = getOrganizationService().getOrgOrgRelationsByOrg(orgId); | 
  | 136 | 0 |                  for (OrgOrgRelationInfo orgOrgRelationInfo : orgRelationInfos) { | 
  | 137 | 0 |                      LOG.debug("---- Org Relation:"); | 
  | 138 | 0 |                      LOG.debug("------------ Org ID: " + orgOrgRelationInfo.getOrgId()); | 
  | 139 | 0 |                      orgInfo = getOrganizationService().getOrganization(orgOrgRelationInfo.getRelatedOrgId()); | 
  | 140 | 0 |                      LOG.debug("------------ Related Org on Document: " + getOrgInfoForPrint(orgInfo)); | 
  | 141 | 0 |                      LOG.debug("------------ Relation State: " + orgOrgRelationInfo.getState()); | 
  | 142 | 0 |                      LOG.debug("------------ Relation Type: " + orgOrgRelationInfo.getType()); | 
  | 143 |  |                  } | 
  | 144 | 0 |                  List<OrgOrgRelationInfo> relatedOrgRelationInfos = getOrganizationService().getOrgOrgRelationsByRelatedOrg(orgId); | 
  | 145 | 0 |                  for (OrgOrgRelationInfo orgOrgRelationInfo : relatedOrgRelationInfos) { | 
  | 146 | 0 |                      LOG.debug("---- Related Org Relation:"); | 
  | 147 | 0 |                      LOG.debug("------------ Related Org ID: " + orgOrgRelationInfo.getRelatedOrgId()); | 
  | 148 | 0 |                      orgInfo = getOrganizationService().getOrganization(orgOrgRelationInfo.getOrgId()); | 
  | 149 | 0 |                      LOG.debug("------------ Org of Relation: " + getOrgInfoForPrint(orgInfo)); | 
  | 150 | 0 |                      LOG.debug("------------ Relation State: " + orgOrgRelationInfo.getState()); | 
  | 151 | 0 |                      LOG.debug("------------ Relation Type: " + orgOrgRelationInfo.getType()); | 
  | 152 |  |                  } | 
  | 153 | 0 |              } | 
  | 154 | 0 |          } catch (Exception e) { | 
  | 155 | 0 |              LOG.error(e); | 
  | 156 | 0 |              throw new RuntimeException("Caught Exception using Organization Service", e); | 
  | 157 | 0 |          } | 
  | 158 | 0 |          return orgIds; | 
  | 159 |  |      } | 
  | 160 |  |   | 
  | 161 |  |       | 
  | 162 |  |   | 
  | 163 |  |   | 
  | 164 |  |   | 
  | 165 |  |   | 
  | 166 |  |   | 
  | 167 |  |   | 
  | 168 |  |   | 
  | 169 |  |      protected List<String> getOrganizationIdsFromDocumentContent(RouteContext context) { | 
  | 170 | 0 |          Document xmlContent = context.getDocumentContent().getDocument(); | 
  | 171 | 0 |          XPath xPath = XPathHelper.newXPath(); | 
  | 172 |  |          try { | 
  | 173 | 0 |              List<String> orgIds = new ArrayList<String>(); | 
  | 174 | 0 |              NodeList orgElements = (NodeList) xPath.evaluate("/documentContent/applicationContent/" + AbstractOrganizationServiceQualifierResolver.DOCUMENT_CONTENT_XML_ROOT_ELEMENT_NAME + "/orgId", xmlContent, | 
  | 175 |  |                      XPathConstants.NODESET); | 
  | 176 | 0 |              for (int index = 0; index < orgElements.getLength(); index++) { | 
  | 177 | 0 |                  Element attributeElement = (Element) orgElements.item(index); | 
  | 178 | 0 |                  String attributeValue = attributeElement.getTextContent(); | 
  | 179 | 0 |                  orgIds.add(attributeValue); | 
  | 180 |  |              } | 
  | 181 | 0 |              if (LOG.isDebugEnabled()) { | 
  | 182 | 0 |                  LOG.debug("Found " + orgElements.getLength() + " organization ids to parse for routing:"); | 
  | 183 | 0 |                  XmlHelper.printDocumentStructure(xmlContent); | 
  | 184 |  |              } | 
  | 185 | 0 |              return orgIds; | 
  | 186 | 0 |          } catch (XPathExpressionException e) { | 
  | 187 | 0 |              throw new RiceRuntimeException("Encountered an issue executing XPath.", e); | 
  | 188 |  |          } | 
  | 189 |  |      } | 
  | 190 |  |   | 
  | 191 |  |      @Override | 
  | 192 |  |      public DynamicResult transitioningOutOf(RouteContext context, RouteHelper helper) throws Exception { | 
  | 193 | 0 |          LOG.debug("Variables for transitioningOutOf"); | 
  | 194 | 0 |          RouteNodeInstance processInstance = context.getNodeInstance().getProcess(); | 
  | 195 |  |   | 
  | 196 | 0 |          List<String> relatedOrgIds = getNextOrganizationIdsForRouting(context, helper); | 
  | 197 |  |           | 
  | 198 | 0 |          DynamicResult result = new DynamicResult(relatedOrgIds.isEmpty(), null); | 
  | 199 | 0 |          for (String relatedOrgId : relatedOrgIds) { | 
  | 200 | 0 |              RouteNodeInstance nodeInstance = generateNextNodeInstance(relatedOrgId, context, processInstance.getBranch(), helper); | 
  | 201 | 0 |              result.getNextNodeInstances().add(nodeInstance); | 
  | 202 | 0 |          } | 
  | 203 | 0 |          return result; | 
  | 204 |  |      } | 
  | 205 |  |   | 
  | 206 |  |       | 
  | 207 |  |   | 
  | 208 |  |   | 
  | 209 |  |      protected String getOrgInfoForPrint(OrgInfo orgInfo) { | 
  | 210 | 0 |          return orgInfo.getId() + " - " + orgInfo.getShortName() + " (" + orgInfo.getLongName() + ")"; | 
  | 211 |  |      } | 
  | 212 |  |   | 
  | 213 |  |       | 
  | 214 |  |   | 
  | 215 |  |   | 
  | 216 |  |   | 
  | 217 |  |   | 
  | 218 |  |   | 
  | 219 |  |   | 
  | 220 |  |   | 
  | 221 |  |   | 
  | 222 |  |   | 
  | 223 |  |   | 
  | 224 |  |   | 
  | 225 |  |   | 
  | 226 |  |   | 
  | 227 |  |   | 
  | 228 |  |   | 
  | 229 |  |   | 
  | 230 |  |      protected List<String> getNextOrganizationIdsForRouting(RouteContext context, RouteHelper helper) { | 
  | 231 | 0 |          RouteNodeInstance currentNode = context.getNodeInstance(); | 
  | 232 | 0 |          String currentNodeName = currentNode.getName(); | 
  | 233 | 0 |          LOG.debug("currentNodeName = '" + currentNodeName + "'"); | 
  | 234 | 0 |          NodeState currentNodeOrgIdState = currentNode.getNodeState(NODE_STATE_ORG_ID_KEY); | 
  | 235 | 0 |          LOG.debug("currentNodeOrgIdState is " + ((currentNodeOrgIdState != null) ? "not " : "") + "null"); | 
  | 236 | 0 |          String currentNodeOrgId = (currentNodeOrgIdState != null) ? currentNodeOrgIdState.getValue() : null; | 
  | 237 | 0 |          LOG.debug("currentNodeOrgId = '" + currentNodeOrgId + "'"); | 
  | 238 | 0 |          Set<String> relatedOrgIds = new HashSet<String>(); | 
  | 239 |  |          try { | 
  | 240 | 0 |              List<OrgOrgRelationInfo> relatedOrgRelationInfos = getOrganizationService().getOrgOrgRelationsByRelatedOrg(currentNodeOrgId); | 
  | 241 | 0 |              for (OrgOrgRelationInfo orgOrgRelationInfo : relatedOrgRelationInfos) { | 
  | 242 | 0 |                  if (StringUtils.equals("Active", orgOrgRelationInfo.getState())) { | 
  | 243 | 0 |                      if (StringUtils.equals(AbstractOrganizationServiceQualifierResolver.KUALI_ORG_TYPE_CURRICULUM_PARENT, orgOrgRelationInfo.getType())) { | 
  | 244 | 0 |                          LOG.debug("---- Related Org Relation:"); | 
  | 245 | 0 |                          OrgInfo referenceOrgInfo = getOrganizationService().getOrganization(orgOrgRelationInfo.getRelatedOrgId()); | 
  | 246 | 0 |                          OrgInfo nextNodeOrgInfo = getOrganizationService().getOrganization(orgOrgRelationInfo.getOrgId()); | 
  | 247 | 0 |                          LOG.debug("------------ Reference Org: " + getOrgInfoForPrint(referenceOrgInfo)); | 
  | 248 | 0 |                          LOG.debug("------------ Org for Next Node: " + getOrgInfoForPrint(nextNodeOrgInfo)); | 
  | 249 | 0 |                          relatedOrgIds.add(nextNodeOrgInfo.getId()); | 
  | 250 | 0 |                      } | 
  | 251 |  |                  } | 
  | 252 |  |              } | 
  | 253 | 0 |          } catch (Exception e) { | 
  | 254 | 0 |              LOG.error("Exception caught attempting to use org hierarchy routing", e); | 
  | 255 | 0 |              throw new RuntimeException("Exception caught attempting to use org hierarchy routing", e); | 
  | 256 | 0 |          } | 
  | 257 | 0 |          return new ArrayList<String>(relatedOrgIds); | 
  | 258 |  |      } | 
  | 259 |  |   | 
  | 260 |  |       | 
  | 261 |  |   | 
  | 262 |  |   | 
  | 263 |  |   | 
  | 264 |  |   | 
  | 265 |  |      protected RouteNodeInstance generateNextNodeInstance(String orgId, RouteContext context, Branch branch, RouteHelper helper) { | 
  | 266 | 0 |          return generateNextNodeInstance(orgId, helper.getNodeFactory().getRouteNode(context, ORG_HIERARCHY_NODE), context, branch, helper); | 
  | 267 |  |      } | 
  | 268 |  |   | 
  | 269 |  |       | 
  | 270 |  |   | 
  | 271 |  |   | 
  | 272 |  |   | 
  | 273 |  |      protected RouteNodeInstance generateNextNodeInstance(String orgId, RouteNode routeNodeDefinition, RouteContext context, Branch branch, RouteHelper helper) { | 
  | 274 | 0 |          LOG.debug("Adding new node with name '" + routeNodeDefinition.getRouteNodeName() + "'"); | 
  | 275 | 0 |          RouteNodeInstance actualRouteNodeInstance = helper.getNodeFactory().createRouteNodeInstance(context.getDocument().getRouteHeaderId(), routeNodeDefinition); | 
  | 276 | 0 |          actualRouteNodeInstance.setBranch(branch); | 
  | 277 | 0 |          actualRouteNodeInstance.addNodeState(new NodeState(NODE_STATE_ORG_ID_KEY, orgId)); | 
  | 278 | 0 |          return actualRouteNodeInstance; | 
  | 279 |  |      } | 
  | 280 |  |   | 
  | 281 |  |       | 
  | 282 |  |   | 
  | 283 |  |   | 
  | 284 |  |   | 
  | 285 |  |   | 
  | 286 |  |   | 
  | 287 |  |   | 
  | 288 |  |   | 
  | 289 |  |   | 
  | 290 |  |   | 
  | 291 |  |   | 
  | 292 |  |      protected DocumentType setUpDocumentType(DocumentType documentType, RouteNodeInstance dynamicNodeInstance) { | 
  | 293 | 0 |          boolean altered = false; | 
  | 294 |  |           | 
  | 295 | 0 |          if (documentType.getNamedProcess(ORG_HIERARCHY_NODE) == null) { | 
  | 296 | 0 |              RouteNode hierarchyNode = getKimRoleNode(ORG_HIERARCHY_NODE, dynamicNodeInstance); | 
  | 297 | 0 |              documentType.addProcess(getPrototypeProcess(hierarchyNode, documentType)); | 
  | 298 | 0 |              altered = true; | 
  | 299 |  |          } | 
  | 300 | 0 |          if (altered) { | 
  | 301 |  |               | 
  | 302 | 0 |              KEWServiceLocator.getDocumentTypeService().save(documentType); | 
  | 303 |  |          } | 
  | 304 | 0 |          return KEWServiceLocator.getDocumentTypeService().findByName(documentType.getName()); | 
  | 305 |  |      } | 
  | 306 |  |   | 
  | 307 |  |       | 
  | 308 |  |   | 
  | 309 |  |   | 
  | 310 |  |   | 
  | 311 |  |   | 
  | 312 |  |   | 
  | 313 |  |      protected RouteNode getKimRoleNode(String routeNodeName, RouteNodeInstance dynamicNodeInstance) { | 
  | 314 | 0 |          RouteNode roleNode = new RouteNode(); | 
  | 315 | 0 |          roleNode.setFinalApprovalInd(Boolean.FALSE); | 
  | 316 | 0 |          roleNode.setMandatoryRouteInd(Boolean.FALSE); | 
  | 317 | 0 |          roleNode.setActivationType(KEWConstants.ROUTE_LEVEL_PARALLEL); | 
  | 318 | 0 |          roleNode.setDocumentType(dynamicNodeInstance.getRouteNode().getDocumentType()); | 
  | 319 | 0 |          roleNode.setNodeType(RoleNode.class.getName()); | 
  | 320 | 0 |          roleNode.setRouteMethodName(RoleRouteModule.class.getName()); | 
  | 321 | 0 |          roleNode.setRouteMethodCode(KEWConstants.ROUTE_LEVEL_ROUTE_MODULE); | 
  | 322 | 0 |          roleNode.setRouteNodeName(routeNodeName); | 
  | 323 | 0 |          roleNode.setContentFragment("<" + QUALIFIER_RESOLVER_CLASS_ELEMENT + ">" + OrganizationCurriculumCommitteeQualifierResolver.class.getName() + "</" + QUALIFIER_RESOLVER_CLASS_ELEMENT + ">"); | 
  | 324 | 0 |          return roleNode; | 
  | 325 |  |      } | 
  | 326 |  |   | 
  | 327 |  |      protected Process getPrototypeProcess(RouteNode node, DocumentType documentType) { | 
  | 328 | 0 |          Process process = new Process(); | 
  | 329 | 0 |          process.setDocumentType(documentType); | 
  | 330 | 0 |          process.setInitial(false); | 
  | 331 | 0 |          process.setInitialRouteNode(node); | 
  | 332 | 0 |          process.setName(node.getRouteNodeName()); | 
  | 333 | 0 |          return process; | 
  | 334 |  |      } | 
  | 335 |  |   | 
  | 336 |  |  } |