01: package org.emforge.jbpm.web.converter;
02:
03: import javax.faces.component.UIComponent;
04: import javax.faces.context.FacesContext;
05: import javax.faces.convert.Converter;
06: import javax.faces.convert.ConverterException;
07:
08: import org.apache.commons.lang.StringUtils;
09: import org.emforge.BpmService;
10: import org.emforge.EmForgeException;
11: import org.emforge.xfer.WorkflowTO;
12:
13: public class WorkflowIdConverter implements Converter {
14:
15: public Object getAsObject(FacesContext i_context,
16: UIComponent i_component, String i_value)
17: throws ConverterException {
18: if (StringUtils.isEmpty(i_value)) {
19: return null;
20: }
21:
22: //try to convert to the id
23: Long longValue = Long.valueOf(i_value);
24: BpmService bpmService = getBpmContext(i_context);
25:
26: try {
27: return bpmService.getWorkflowById(longValue);
28: } catch (EmForgeException ex) {
29: throw new ConverterException("Cannot get workflow by id "
30: + longValue, ex);
31: }
32: }
33:
34: public String getAsString(FacesContext i_context,
35: UIComponent i_component, Object i_value)
36: throws ConverterException {
37: if (i_value instanceof String) {
38: return (String) i_value;
39: }
40:
41: if (i_value == null) {
42: return "";
43: }
44:
45: WorkflowTO workflow = (WorkflowTO) i_value;
46:
47: return String.valueOf(workflow.getId());
48: }
49:
50: /** Gets BPM Context from Faces Context
51: *
52: * @param i_context - faces context
53: * @return bpmContext bean
54: */
55: private BpmService getBpmContext(FacesContext context) {
56: BpmService bpmService = (BpmService) context.getApplication()
57: .getVariableResolver().resolveVariable(context,
58: "bpmService");
59: assert bpmService != null;
60:
61: return bpmService;
62: }
63:
64: }
|