01: package org.emforge.projectmanager.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.apache.commons.logging.Log;
10: import org.apache.commons.logging.LogFactory;
11: import org.emforge.projectmanager.ProjectService;
12: import org.emforge.projectmanager.base.MilestoneDO;
13:
14: /**
15: * This Converter is used for selecting Milestone in selectOneMenu control
16: *
17: * @author mchirkov
18: */
19: public class MilestoneIdConverter implements Converter {
20:
21: protected final Log logger = LogFactory.getLog(getClass());
22:
23: /**
24: * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext,
25: * javax.faces.component.UIComponent, java.lang.String)
26: */
27: public Object getAsObject(FacesContext i_context,
28: UIComponent i_component, String i_str)
29: throws ConverterException {
30:
31: if (StringUtils.isEmpty(i_str)) {
32: return null;
33: }
34:
35: ProjectService projectService = (ProjectService) i_context
36: .getApplication().getVariableResolver()
37: .resolveVariable(i_context, "projectService");
38: MilestoneDO milestone = projectService.getMilestone(Long
39: .valueOf(i_str));
40:
41: return milestone;
42: }
43:
44: /**
45: * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext,
46: * javax.faces.component.UIComponent, java.lang.Object)
47: */
48: public String getAsString(FacesContext i_context,
49: UIComponent i_component, Object i_obj)
50: throws ConverterException {
51:
52: if (i_obj == null) {
53: return "";
54: }
55:
56: if (i_obj instanceof String) {
57: return (String) i_obj;
58: }
59:
60: if (!(i_obj instanceof MilestoneDO)) {
61: // object is not Milestone.
62: // do not convert it - because maybe it is due to bug ^20799
63: // just return empty line
64: logger
65: .error("The following object cannot be converted to a milestone: "
66: + i_obj.toString()
67: + " ("
68: + i_obj.getClass().toString() + ")");
69: return "";
70: }
71: MilestoneDO milestone = (MilestoneDO) i_obj;
72:
73: return String.valueOf(milestone.getId());
74: }
75:
76: }
|