| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |   | 
  | 17 |  |  package org.kuali.rice.kew.edl; | 
  | 18 |  |   | 
  | 19 |  |  import java.util.Iterator; | 
  | 20 |  |  import java.util.LinkedHashMap; | 
  | 21 |  |  import java.util.Map; | 
  | 22 |  |   | 
  | 23 |  |  import javax.xml.parsers.DocumentBuilderFactory; | 
  | 24 |  |  import javax.xml.transform.Templates; | 
  | 25 |  |   | 
  | 26 |  |  import org.apache.log4j.Logger; | 
  | 27 |  |  import org.kuali.rice.kew.edl.bo.EDocLiteAssociation; | 
  | 28 |  |  import org.kuali.rice.kew.edl.service.EDocLiteService; | 
  | 29 |  |  import org.kuali.rice.kew.exception.WorkflowRuntimeException; | 
  | 30 |  |  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; | 
  | 31 |  |  import org.kuali.rice.kew.service.KEWServiceLocator; | 
  | 32 |  |  import org.kuali.rice.kew.util.XmlHelper; | 
  | 33 |  |  import org.w3c.dom.Document; | 
  | 34 |  |  import org.w3c.dom.Element; | 
  | 35 |  |  import org.w3c.dom.Node; | 
  | 36 |  |  import org.w3c.dom.NodeList; | 
  | 37 |  |   | 
  | 38 |  |   | 
  | 39 |  |   | 
  | 40 |  |   | 
  | 41 |  |   | 
  | 42 |  |   | 
  | 43 |  |   | 
  | 44 |  |   | 
  | 45 |  |   | 
  | 46 | 0 |  public class EDLControllerFactory { | 
  | 47 |  |   | 
  | 48 | 0 |          private static final Logger LOG = Logger.getLogger(EDLControllerFactory.class); | 
  | 49 |  |           | 
  | 50 |  |          private static final String CONFIG_CACHE_GROUP_NAME = "EDLConfig"; | 
  | 51 |  |   | 
  | 52 |  |           | 
  | 53 |  |   | 
  | 54 |  |          public static EDLController createEDLController(EDocLiteAssociation edlAssociation, EDLGlobalConfig edlGlobalConfig) { | 
  | 55 | 0 |          EDLController edlController = new EDLController(); | 
  | 56 | 0 |                  edlController.setEdocLiteAssociation(edlAssociation); | 
  | 57 |  |   | 
  | 58 |  |                  try { | 
  | 59 | 0 |                          edlController.setEdlGlobalConfig(edlGlobalConfig); | 
  | 60 | 0 |                          edlController.setDefaultDOM(getDefaultDOM(edlAssociation)); | 
  | 61 | 0 |                          loadConfigProcessors(edlController, edlGlobalConfig); | 
  | 62 | 0 |                          loadPreProcessors(edlController, edlGlobalConfig); | 
  | 63 | 0 |                          loadPostProcessor(edlController, edlGlobalConfig); | 
  | 64 | 0 |                          loadStateComponents(edlController, edlGlobalConfig); | 
  | 65 | 0 |                          loadStyle(edlController); | 
  | 66 |  |                           | 
  | 67 | 0 |                  } catch (Exception e) { | 
  | 68 | 0 |              String edl = null; | 
  | 69 | 0 |              if (edlAssociation != null) { | 
  | 70 | 0 |                  edl = edlAssociation.getEdlName(); | 
  | 71 |  |              } | 
  | 72 | 0 |              String message = "Error creating controller for EDL" + (edl == null ? "" : ": " + edl); | 
  | 73 | 0 |              LOG.error(message, e); | 
  | 74 | 0 |                          throw new WorkflowRuntimeException("Problems creating controller for EDL: " + edl, e); | 
  | 75 | 0 |                  } | 
  | 76 |  |   | 
  | 77 | 0 |                  return edlController; | 
  | 78 |  |          } | 
  | 79 |  |   | 
  | 80 |  |          public static EDLController createEDLController(EDocLiteAssociation edlAssociation, EDLGlobalConfig edlGlobalConfig, DocumentRouteHeaderValue document) { | 
  | 81 | 0 |                  EDLController edlController = createEDLController(edlAssociation, edlGlobalConfig); | 
  | 82 |  |                  try { | 
  | 83 | 0 |                          Document defaultDom = edlController.getDefaultDOM(); | 
  | 84 | 0 |                          Document documentDom = XmlHelper.readXml(document.getDocContent()); | 
  | 85 |  |                           | 
  | 86 | 0 |                          Element documentData = (Element) documentDom.getElementsByTagName(EDLXmlUtils.DATA_E).item(0); | 
  | 87 | 0 |                          if (documentData != null) { | 
  | 88 | 0 |                                  Element defaultDomEDL = EDLXmlUtils.getEDLContent(defaultDom, false); | 
  | 89 | 0 |                                  Element defaultDomData = (Element) defaultDomEDL.getElementsByTagName(EDLXmlUtils.DATA_E).item(0); | 
  | 90 | 0 |                                  defaultDomEDL.replaceChild(defaultDom.importNode(documentData, true), defaultDomData); | 
  | 91 |  |                          } | 
  | 92 | 0 |                          if (LOG.isDebugEnabled()) { | 
  | 93 | 0 |                                  LOG.debug("Created default Node from document id " + document.getRouteHeaderId() + " content " + XmlHelper.jotNode(defaultDom)); | 
  | 94 |  |                          } | 
  | 95 | 0 |                  } catch (Exception e) { | 
  | 96 | 0 |                          throw new WorkflowRuntimeException("Problems creating controller for EDL " + edlAssociation.getEdlName() + " document " + document.getRouteHeaderId(), e); | 
  | 97 | 0 |                  } | 
  | 98 | 0 |                  return edlController; | 
  | 99 |  |          } | 
  | 100 |  |   | 
  | 101 |  |          private synchronized static void loadStyle(EDLController edlController) throws Exception { | 
  | 102 | 0 |                  EDocLiteService edlService = getEDLService(); | 
  | 103 | 0 |                  Templates styleSheet = null; | 
  | 104 | 0 |                  styleSheet = edlService.getStyleAsTranslet(edlController.getEdocLiteAssociation().getStyle()); | 
  | 105 | 0 |                  edlController.setStyle(styleSheet); | 
  | 106 | 0 |          } | 
  | 107 |  |           | 
  | 108 |  |          private synchronized static void loadPreProcessors(EDLController edlController, EDLGlobalConfig edlGlobalConfig) { | 
  | 109 | 0 |                  edlController.setPreProcessors(cloneConfigMap(edlGlobalConfig.getPreProcessors(), edlController.getDefaultDOM())); | 
  | 110 | 0 |          } | 
  | 111 |  |           | 
  | 112 |  |          private synchronized static void loadPostProcessor(EDLController edlController, EDLGlobalConfig edlGlobalConfig) { | 
  | 113 | 0 |                  edlController.setPostProcessors(cloneConfigMap(edlGlobalConfig.getPostProcessors(), edlController.getDefaultDOM())); | 
  | 114 | 0 |          } | 
  | 115 |  |           | 
  | 116 |  |          private synchronized static void loadStateComponents(EDLController edlController, EDLGlobalConfig edlGlobalConfig) { | 
  | 117 | 0 |                  edlController.setStateComponents(cloneConfigMap(edlGlobalConfig.getStateComponents(), edlController.getDefaultDOM())); | 
  | 118 | 0 |          } | 
  | 119 |  |   | 
  | 120 |  |          private synchronized static void loadConfigProcessors(EDLController edlController, EDLGlobalConfig edlGlobalConfig) throws Exception { | 
  | 121 | 0 |                  EDocLiteAssociation edlAssociation = edlController.getEdocLiteAssociation(); | 
  | 122 | 0 |                  Map configProcessorMappings = fetchConfigFromCache(edlAssociation.getDefinition()); | 
  | 123 | 0 |                  if (configProcessorMappings != null) { | 
  | 124 | 0 |                          edlController.setConfigProcessors(cloneConfigMap(configProcessorMappings, edlController.getDefaultDOM())); | 
  | 125 |  |                  } else { | 
  | 126 |  |                           | 
  | 127 | 0 |                          Document document = getEDLService().getDefinitionXml(edlAssociation); | 
  | 128 | 0 |                          Element definitionElement = (Element) document.getFirstChild(); | 
  | 129 |  |   | 
  | 130 | 0 |                          configProcessorMappings = new LinkedHashMap(); | 
  | 131 | 0 |                          edlController.setEdlGlobalConfig(edlGlobalConfig); | 
  | 132 | 0 |                          NodeList edlDefinitionNodes = definitionElement.getChildNodes(); | 
  | 133 | 0 |                          for (int i = 0; i < edlDefinitionNodes.getLength(); i++) { | 
  | 134 | 0 |                                  Node definitionNode = edlDefinitionNodes.item(i); | 
  | 135 | 0 |                                  Class configProcessorClass = edlGlobalConfig.getConfigProcessor(definitionNode); | 
  | 136 | 0 |                                  if (configProcessorClass != null) { | 
  | 137 | 0 |                                          configProcessorMappings.put(definitionNode, configProcessorClass); | 
  | 138 |  |                                  } | 
  | 139 |  |                          } | 
  | 140 | 0 |                          putConfigInCache(edlAssociation.getDefinition(), configProcessorMappings); | 
  | 141 |  |   | 
  | 142 | 0 |                          loadConfigProcessors(edlController, edlGlobalConfig); | 
  | 143 |  |                  } | 
  | 144 | 0 |          } | 
  | 145 |  |           | 
  | 146 |  |          protected synchronized static Map fetchConfigFromCache(String definition) { | 
  | 147 | 0 |                  return (Map)KEWServiceLocator.getCacheAdministrator().getFromCache(getConfigCacheKey(definition)); | 
  | 148 |  |          } | 
  | 149 |  |           | 
  | 150 |  |          private synchronized static void putConfigInCache(String definition, Map configProcessorMappings) { | 
  | 151 | 0 |                  KEWServiceLocator.getCacheAdministrator().putInCache(getConfigCacheKey(definition), configProcessorMappings, CONFIG_CACHE_GROUP_NAME); | 
  | 152 | 0 |          } | 
  | 153 |  |           | 
  | 154 |  |          private static String getConfigCacheKey(String definition) { | 
  | 155 | 0 |                  return CONFIG_CACHE_GROUP_NAME + ":" + definition; | 
  | 156 |  |          } | 
  | 157 |  |           | 
  | 158 |  |          private synchronized static Map cloneConfigMap(Map configMap, Document defaultDom) { | 
  | 159 | 0 |                  Map tempConfigProcessors = new LinkedHashMap(); | 
  | 160 | 0 |                  for (Iterator iter = configMap.entrySet().iterator(); iter.hasNext();) { | 
  | 161 | 0 |                          Map.Entry configProcessorMapping = (Map.Entry) iter.next(); | 
  | 162 | 0 |                          tempConfigProcessors.put(defaultDom.importNode((Node)configProcessorMapping.getKey(), true), configProcessorMapping.getValue()); | 
  | 163 | 0 |                  } | 
  | 164 | 0 |                  return tempConfigProcessors; | 
  | 165 |  |          } | 
  | 166 |  |   | 
  | 167 |  |          private static EDocLiteService getEDLService() { | 
  | 168 | 0 |                  return KEWServiceLocator.getEDocLiteService(); | 
  | 169 |  |          } | 
  | 170 |  |   | 
  | 171 |  |          private static Document getDefaultDOM(EDocLiteAssociation edlAssociation) throws Exception { | 
  | 172 | 0 |                  Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); | 
  | 173 | 0 |                  Element rootElement = dom.createElement("documentContent");  | 
  | 174 |  |                   | 
  | 175 |  |                   | 
  | 176 |  |                   | 
  | 177 |  |                   | 
  | 178 |  |                   | 
  | 179 |  |                   | 
  | 180 | 0 |                  dom.appendChild(rootElement); | 
  | 181 | 0 |                  Element edlContentElement = EDLXmlUtils.getEDLContent(dom, true); | 
  | 182 | 0 |                  EDLXmlUtils.getDataFromEDLDocument(edlContentElement, true); | 
  | 183 |  |                   | 
  | 184 |  |                   | 
  | 185 | 0 |                  Element edlData = EDLXmlUtils.getChildElement(edlContentElement, EDLXmlUtils.DATA_E); | 
  | 186 |  |                   | 
  | 187 | 0 |                  edlData.setAttribute("edlName", edlAssociation.getEdlName()); | 
  | 188 |  |                   | 
  | 189 | 0 |                  return dom; | 
  | 190 |  |          } | 
  | 191 |  |   | 
  | 192 |  |          public static void flushDefinitionFromConfigCache(String definition) { | 
  | 193 | 0 |                  KEWServiceLocator.getCacheAdministrator().flushEntry(getConfigCacheKey(definition)); | 
  | 194 |  |   | 
  | 195 | 0 |          } | 
  | 196 |  |           | 
  | 197 |  |          public static void flushDefinitionCache() { | 
  | 198 | 0 |                  KEWServiceLocator.getCacheAdministrator().flushGroup(CONFIG_CACHE_GROUP_NAME); | 
  | 199 | 0 |          } | 
  | 200 |  |  } |