01: package ru.emdev.EmForge.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.acegisecurity.userdetails.UserDetails;
09: import org.apache.commons.lang.StringUtils;
10:
11: import ru.emdev.EmForge.security.UserFactory;
12:
13: /** This Converter is Used for passing Users into JSF Select-Controls */
14: public class UserConverter implements Converter {
15:
16: public Object getAsObject(FacesContext context,
17: UIComponent component, String value)
18: throws ConverterException {
19: if (StringUtils.isEmpty(value)) {
20: return null;
21: }
22:
23: UserFactory userFactory = getUserFactory(context);
24:
25: return userFactory.getUser(value);
26: }
27:
28: public String getAsString(FacesContext context,
29: UIComponent component, Object value)
30: throws ConverterException {
31: if (value instanceof String) {
32: return (String) value;
33: }
34:
35: if (value == null) {
36: return "";
37: }
38:
39: UserDetails user = (UserDetails) value;
40:
41: return user.getUsername();
42: }
43:
44: private UserFactory getUserFactory(FacesContext context) {
45: UserFactory userFactory = (UserFactory) context
46: .getApplication().getVariableResolver()
47: .resolveVariable(context, "userFactory");
48: assert userFactory != null;
49:
50: return userFactory;
51: }
52:
53: }
|