1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.orchestra.dynaForm.jsf.guiBuilder.impl.myfaces;
20
21 import org.apache.commons.beanutils.BeanUtils;
22
23 import javax.faces.context.FacesContext;
24 import javax.faces.el.EvaluationException;
25 import javax.faces.el.MethodBinding;
26 import javax.faces.el.MethodNotFoundException;
27 import javax.faces.model.SelectItem;
28 import java.lang.reflect.InvocationTargetException;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.List;
32
33
34
35
36 public class SuggestSelectListMethodWrapper extends MethodBinding
37 {
38 private final MethodBinding original;
39 private final String valueProperty;
40 private final String[] descriptionPropertiesArray;
41
42 SuggestSelectListMethodWrapper(MethodBinding original, String valueProperty, String descriptionProperties)
43 {
44 this.original = original;
45 this.valueProperty = valueProperty;
46 if (descriptionProperties != null)
47 {
48 descriptionPropertiesArray = descriptionProperties.split(",\\s*");
49 }
50 else
51 {
52 descriptionPropertiesArray = new String[]{};
53 }
54 }
55
56 @Override
57 public Class<?> getType(FacesContext facesContext) throws MethodNotFoundException
58 {
59 return List.class;
60 }
61
62 @Override
63 public Object invoke(FacesContext facesContext, Object[] objects) throws EvaluationException
64 {
65 Object items = original.invoke(facesContext, objects);
66 if (items == null)
67 {
68 return items;
69 }
70
71 if (!(items instanceof Collection))
72 {
73 throw new UnsupportedOperationException(
74 "unknown return type " + items.getClass().getName() +
75 " for " + getExpressionString() +
76 " awaited instanceof java.util.Collection");
77 }
78
79 Collection<?> coll = (Collection<?>) items;
80
81 List<SelectItem> selectItems = new ArrayList<SelectItem>(coll.size());
82 for (Object o : coll)
83 {
84 SelectItem si;
85
86 if (o instanceof SelectItem)
87 {
88 si = (SelectItem) o;
89 }
90 else
91 {
92 Object value = getValueProperty(o, valueProperty);
93 String description = buildDescription(o, descriptionPropertiesArray);
94
95 si = new SelectItem(value, description);
96 }
97
98 selectItems.add(si);
99 }
100
101 return selectItems;
102 }
103
104 protected String buildDescription(Object o, String[] descriptionPropertiesArray)
105 {
106 StringBuffer sb = new StringBuffer(80);
107
108 for (String descriptionProperty : descriptionPropertiesArray)
109 {
110 Object descriptionValue = null;
111 try
112 {
113 descriptionValue = BeanUtils.getProperty(o, descriptionProperty);
114 }
115 catch (IllegalAccessException e)
116 {
117 throw new EvaluationException(e);
118 }
119 catch (InvocationTargetException e)
120 {
121 throw new EvaluationException(e);
122 }
123 catch (NoSuchMethodException e)
124 {
125 throw new EvaluationException(e);
126 }
127
128 if (descriptionValue != null)
129 {
130 if (sb.length() > 0)
131 {
132 sb.append(", ");
133 }
134
135 sb.append(descriptionValue);
136 }
137 }
138
139 return sb.toString();
140 }
141
142 protected Object getValueProperty(Object o, String valueProperty)
143 {
144 if (valueProperty == null || valueProperty.trim().length() < 1)
145 {
146 return o;
147 }
148
149 try
150 {
151 return BeanUtils.getProperty(o, valueProperty);
152 }
153 catch (IllegalAccessException e)
154 {
155 throw new EvaluationException(e);
156 }
157 catch (InvocationTargetException e)
158 {
159 throw new EvaluationException(e);
160 }
161 catch (NoSuchMethodException e)
162 {
163 throw new EvaluationException(e);
164 }
165 }
166 }