1 5 6 package org.exoplatform.services.workflow.impl; 7 8 import org.dom4j.Element; 9 import org.exoplatform.container.PortalContainer; 10 import org.exoplatform.services.log.LogService; 11 import org.exoplatform.services.workflow.Form; 12 import org.exoplatform.services.workflow.WorkflowExecutionService; 13 import org.exoplatform.services.workflow.format.DefaultFormat; 14 import org.jbpm.util.xml.Dom4jHelper; 15 import org.apache.commons.logging.Log; 16 17 import java.util.*; 18 import java.io.ByteArrayInputStream ; 19 import java.io.IOException ; 20 import java.text.Format ; 21 22 27 public class FormImpl implements Form{ 28 29 private String stateName; 30 private List variableFormats; 31 private List submitButtons; 32 33 private ResourceBundle resourceBundle; 34 private Log log; 35 36 public FormImpl(Long definitionId, Element formElement, WorkflowExecutionService executionService, 37 Locale locale) { 38 39 this.log = ((LogService)PortalContainer.getInstance().getComponentInstanceOfType(LogService.class)). 40 getLog("org.exoplatform.services.workflow"); 41 String formFileName = Dom4jHelper.getElementText(formElement, "resource-bundle", null); 42 43 String localisedFileName = getLocalisedString(formFileName, locale); 45 log.debug("Try to find localised resource : " + localisedFileName); 46 byte[] bytes = executionService.getFile(definitionId, localisedFileName); 47 if(bytes == null) { 48 log.debug("Try to find default resource : " + formFileName + ".properties"); 49 bytes = executionService.getFile(definitionId, formFileName + ".properties"); 50 } 51 if(bytes != null) 52 log.debug("resource bundle found true"); 53 else 54 log.debug("resource bundle found false"); 55 56 try { 57 resourceBundle = new PropertyResourceBundle(new ByteArrayInputStream (bytes)); 58 } catch (IOException e) { 59 e.printStackTrace(); 60 } 61 62 this.stateName = Dom4jHelper.getElementText(formElement, "state-name", null); 63 64 initializeVariableFormats(formElement); 65 initializeSubmitButtons(formElement); 66 } 67 68 private String getLocalisedString(String fileName, Locale locale) { 69 return fileName + "_" + locale.getLanguage() + ".properties"; 70 } 71 72 private void initializeVariableFormats(Element formElement) { 73 this.variableFormats = new ArrayList(); 74 Iterator iter = formElement.elements("variable").iterator(); 75 while (iter.hasNext()) { 76 Element variableElement = (Element) iter.next(); 77 78 String variableName = 79 Dom4jHelper.getAttribute(variableElement, "name", null); 80 String formatClassName = 81 Dom4jHelper.getAttribute(variableElement, "format", null); 82 Format variableFormat = new DefaultFormat(); 83 if (formatClassName != null) { 84 try { 85 variableFormat = (Format) Thread.currentThread().getContextClassLoader(). 86 loadClass(formatClassName).newInstance(); 87 } catch (Exception e) { 88 } 90 } 91 this.variableFormats.add(new Attribute(variableName, variableFormat)); 92 } 93 } 94 95 private void initializeSubmitButtons(Element formElement) { 96 this.submitButtons = new ArrayList(); 97 Iterator iter = formElement.elements("submitbutton").iterator(); 98 while (iter.hasNext()) { 99 Element submitButtonElement = (Element) iter.next(); 100 String value = Dom4jHelper.getAttribute(submitButtonElement, "value", null); 101 String transitionName = Dom4jHelper.getAttribute(submitButtonElement, "transition-name", null); 102 this.submitButtons.add(new Attribute(value, transitionName)); 103 } 104 } 105 106 public List getVariableFormats() { 107 return variableFormats; 108 } 109 110 public List getSubmitButtons() { 111 return submitButtons; 112 } 113 114 public String getStateName() { 115 return stateName; 116 } 117 118 public ResourceBundle getResourceBundle() { 119 return resourceBundle; 120 } 121 122 } 123 | Popular Tags |