KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > workflow > impl > FormImpl


1 /*
2  * Copyright 2001-2003 The eXo platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  */

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 JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.text.Format JavaDoc;
21
22 /**
23  * Created y the eXo platform team
24  * User: Benjamin Mestrallet
25  * Date: 17 mai 2004
26  */

27 public class FormImpl implements Form{
28
29   private String JavaDoc stateName;
30   private List variableFormats;
31   private List submitButtons;
32
33   private ResourceBundle resourceBundle;
34   private Log log;
35
36   public FormImpl(Long JavaDoc 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 JavaDoc formFileName = Dom4jHelper.getElementText(formElement, "resource-bundle", null);
42
43     //manage properties
44
String JavaDoc 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 JavaDoc(bytes));
58     } catch (IOException JavaDoc 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 JavaDoc getLocalisedString(String JavaDoc 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 JavaDoc variableName =
79         Dom4jHelper.getAttribute(variableElement, "name", null);
80       String JavaDoc 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 JavaDoc e) {
88           //log.error("couldn't instante variable formatter '" + formatClassName + "'", e);
89
}
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 JavaDoc value = Dom4jHelper.getAttribute(submitButtonElement, "value", null);
101       String JavaDoc 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 JavaDoc getStateName() {
115     return stateName;
116   }
117
118   public ResourceBundle getResourceBundle() {
119     return resourceBundle;
120   }
121
122 }
123
Popular Tags