1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.promotego.editors;
20
21 import java.beans.PropertyEditorSupport;
22
23 import org.promotego.beans.BeanSupport;
24 import org.promotego.dao.interfaces.Dao;
25
26 /***
27 * A parameterized property editor that maps BeanSupport<T> objects to and from
28 * their id's as mapped by the provided Dao. Used by controllers to map object
29 * ID's posted by the browser to the proper object types.
30 *
31 * @author alf
32 *
33 * @param <T>
34 */
35 public class BeanIdEditor<T extends BeanSupport<T>> extends PropertyEditorSupport
36 {
37 private T m_tee;
38 private Dao<T> m_dao;
39
40 public BeanIdEditor(Dao<T> addressDao)
41 {
42 m_dao = addressDao;
43 }
44
45 public String getAsText()
46 {
47 if (m_tee != null)
48 {
49 return m_tee.getId().toString();
50 }
51 else
52 {
53 return null;
54 }
55 }
56
57 public Object getValue()
58 {
59 return m_tee;
60 }
61
62 public void setAsText(String text) throws IllegalArgumentException
63 {
64 m_tee = m_dao.getById(Long.parseLong(text));
65 }
66
67 public void setValue(Object value)
68 {
69 if (value == null)
70 {
71
72 return;
73 }
74
75 @SuppressWarnings("unchecked")
76 T address = (T)value;
77
78 assert address.getId() != null : "Id may not be null";
79 m_tee = address;
80 }
81 }