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.emforge.projectmanager.ProjectService;
09: import org.emforge.projectmanager.base.ProjectDO;
10:
11: /** This Converter is used for selecting Project in selectOneMenu control
12: *
13: * @author akakunin
14: */
15: public class ProjectIdConverter implements Converter {
16:
17: /** Converts from id - specified as string to Project
18: * Requested Project readed from ProjectDAO
19: */
20: public Object getAsObject(FacesContext i_context,
21: UIComponent i_component, String i_str)
22: throws ConverterException {
23:
24: if (i_str == null || i_str.length() == 0) {
25: return null;
26: }
27:
28: Long id = Long.valueOf(i_str);
29:
30: ProjectService projectService = getProjectService(i_context);
31: ProjectDO project = projectService.getProject(id);
32:
33: return project;
34: }
35:
36: /** Convert specified project to id
37: * Since SelectItem is not allowed to put null as object,
38: * but we should represent null-project (in case then project is not set for process)
39: * we also proces situation then string directly specified to the SelectItem
40: */
41: public String getAsString(FacesContext i_context,
42: UIComponent i_component, Object i_obj)
43: throws ConverterException {
44: if (i_obj == null) {
45: return "";
46: }
47:
48: if (i_obj instanceof String) {
49: return (String) i_obj;
50: }
51:
52: ProjectDO project = (ProjectDO) i_obj;
53:
54: return String.valueOf(project.getId());
55: }
56:
57: /** Gets ProjectService from Faces Context
58: *
59: * @param i_context - faces context
60: * @return projectService bean
61: */
62: private ProjectService getProjectService(FacesContext i_context) {
63: ProjectService projectService = (ProjectService) i_context
64: .getApplication().getVariableResolver()
65: .resolveVariable(i_context, "projectService");
66:
67: return projectService;
68: }
69:
70: }
|