View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Intercepts the method binding used to build the suggest selection list
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 }