View Javadoc

1   /*
2    * $Id: BasicPropertiesAccessor.java,v 1.17 2011/06/16 09:29:40 jbmeslin Exp $
3    */
4   package org.rcfaces.core.internal.component;
5   
6   import java.util.Collections;
7   import java.util.Iterator;
8   import java.util.Map;
9   import java.util.Set;
10  
11  import javax.el.ValueExpression;
12  import javax.faces.FacesException;
13  import javax.faces.component.UIComponentBase;
14  import javax.faces.context.FacesContext;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  import org.rcfaces.core.internal.Constants;
19  import org.rcfaces.core.internal.util.StateHolderTools;
20  
21  /**
22   * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
23   * @version $Revision: 1.17 $ $Date: 2011/06/16 09:29:40 $
24   */
25  public class BasicPropertiesAccessor extends AbstractPropertiesAccessor {
26      private static final String REVISION = "$Revision: 1.17 $";
27  
28      private static final Log LOG = LogFactory
29              .getLog(BasicPropertiesAccessor.class);
30  
31      private static final boolean debugEnabled = LOG.isDebugEnabled();
32  
33      private Map properties;
34  
35      public Object getProperty(String propertyName) {
36          if (properties == null || properties.isEmpty()) {
37              return null;
38          }
39  
40          Object value = properties.get(propertyName);
41  
42          return value;
43      }
44  
45      public Object setProperty(FacesContext context, String propertyName,
46              Object value) {
47          if (value == null) {
48              if (properties == null) {
49                  return null;
50              }
51  
52              Object previousValue = properties.get(propertyName);
53              if ((previousValue instanceof ValueExpression) == false) {
54                  return properties.remove(propertyName);
55              }
56  
57              if (context == null) {
58                  context = FacesContext.getCurrentInstance();
59              }
60  
61              ValueExpression valueBinding = (ValueExpression) previousValue;
62  
63              Object old = valueBinding.getValue(context.getELContext());
64  
65              if (debugEnabled) {
66                  LOG.debug("Set value '" + value + "' to '"
67                          + valueBinding.getExpressionString() + " old='" + old
68                          + "'");
69              }
70  
71              try {
72                  valueBinding.setValue(context.getELContext(), value);
73  
74              } catch (RuntimeException ex) {
75                  LOG.error("Set value '" + value + "' to '"
76                          + valueBinding.getExpressionString() + " old='" + old
77                          + "'", ex);
78  
79                  throw ex;
80              }
81  
82              return old;
83          }
84  
85          if (properties == null) {
86              properties = createMap(8);
87  
88              properties.put(propertyName, value);
89              return null;
90          }
91  
92          Object previousValue = properties.get(propertyName);
93          if ((previousValue instanceof ValueExpression) == false) {
94              properties.put(propertyName, value);
95              return previousValue;
96          }
97  
98          if (context == null) {
99              context = FacesContext.getCurrentInstance();
100         }
101 
102         ValueExpression valueBinding = (ValueExpression) previousValue;
103 
104         Object old = valueBinding.getValue(context.getELContext());
105 
106         if (debugEnabled) {
107             LOG
108                     .debug("Set value '" + value + "' to '"
109                             + valueBinding.getExpressionString() + " old='"
110                             + old + "'");
111         }
112 
113         try {
114             valueBinding.setValue(context.getELContext(), value);
115 
116         } catch (RuntimeException ex) {
117             LOG.error(
118                     "Set value '" + value + "' to '"
119                             + valueBinding.getExpressionString() + " old='"
120                             + old + "'", ex);
121 
122             throw ex;
123         }
124 
125         return old;
126     }
127 
128     public void setProperty(FacesContext context, String propertyName,
129             ValueExpression value) {
130         if (value == null) {
131             if (properties == null) {
132                 return;
133             }
134 
135             properties.remove(propertyName);
136             return;
137         }
138 
139         if (properties == null) {
140             properties = createMap(8);
141         }
142 
143         properties.put(propertyName, value);
144     }
145 
146     public Object removeProperty(FacesContext context, String propertyName) {
147         if (properties == null) {
148             return null;
149         }
150 
151         Object previousValue = properties.remove(propertyName);
152 
153         if ((previousValue instanceof ValueExpression) == false) {
154             return previousValue;
155         }
156 
157         if (context == null) {
158             context = FacesContext.getCurrentInstance();
159         }
160 
161         ValueExpression valueBinding = (ValueExpression) previousValue;
162 
163         Object old = valueBinding.getValue(context.getELContext());
164 
165         return old;
166     }
167 
168     /*
169      * (non-Javadoc)
170      * 
171      * @see
172      * org.rcfaces.core.internal.components.IPropertiesAccessor#saveState(javax
173      * .faces.context.FacesContext)
174      */
175     public Object saveState(FacesContext context) {
176         if (properties == null || properties.isEmpty()) {
177             return null;
178         }
179 
180         Object rets[] = new Object[properties.size() * 2];
181         int i = 0;
182         for (Iterator it = properties.entrySet().iterator(); it.hasNext();) {
183             Map.Entry entry = (Map.Entry) it.next();
184 
185             Object value = entry.getValue();
186             if (value == null) {
187                 continue;
188             }
189 
190             Object key = entry.getKey();
191             if (Constants.COMPACTED_PROPERTY_NAME) {
192                 // La clef est forcement un string ....
193                 // Par contre, on peut la transformer en Integer !
194                 Object k = PropertiesRepository.getKey((String) key);
195                 if (k != null) {
196                     key = k;
197                 }
198             }
199 
200             rets[i++] = key;
201 
202             if (StateHolderTools.isPrimitive(value)) {
203                 rets[i++] = value;
204                 continue;
205             }
206 
207             rets[i++] = UIComponentBase.saveAttachedState(context, value);
208         }
209 
210         return rets;
211     }
212 
213     public void release() {
214         properties = null;
215     }
216 
217     public void restoreState(FacesContext context, Object object) {
218         if (object == null) {
219             return;
220         }
221 
222         if ((object instanceof Object[]) == false) {
223             throw new FacesException(
224                     "Bad serialized format ! (not an objects array !)");
225         }
226 
227         Object datas[] = (Object[]) object;
228 
229         properties = createMap(datas.length / 2);
230 
231         for (int i = 0; i < datas.length;) {
232             Object key = datas[i++];
233             if (Constants.COMPACTED_PROPERTY_NAME) {
234                 if ((key instanceof String) == false) {
235                     key = PropertiesRepository.getPropertyFromKey(key);
236 
237                     if (key == null) {
238                         throw new FacesException("Unknown key '" + key + "' !");
239                     }
240                 }
241             }
242 
243             Object value = datas[i++];
244             if (value == null) {
245                 // ??? Ca ne doit jamais arriver ...
246                 LOG.error("Invalid format for restoreState (key=" + key + ").");
247                 continue;
248             }
249 
250             if (StateHolderTools.isPrimitive(value) == false) {
251                 value = UIComponentBase.restoreAttachedState(context, value);
252             }
253 
254             setProperty(context, (String) key, value);
255         }
256     }
257 
258     public boolean isPropertySetted(String propertyName) {
259         if (properties == null) {
260             return false;
261         }
262         return properties.containsKey(propertyName);
263     }
264 
265     public Set keySet() {
266         if (properties == null || properties.isEmpty()) {
267             return Collections.EMPTY_SET;
268         }
269 
270         return properties.keySet();
271     }
272 
273     public int size() {
274         if (properties == null) {
275             return 0;
276         }
277 
278         return properties.size();
279     }
280 
281     public String toString() {
282         if (properties == null) {
283             return "[EMPTY]";
284         }
285 
286         Set keys = keySet();
287 
288         String s = "{";
289 
290         boolean first = true;
291         for (Iterator it = keys.iterator(); it.hasNext();) {
292             String key = (String) it.next();
293 
294             if (first) {
295                 first = false;
296             } else {
297                 s += ",";
298             }
299             s += key + "=";
300 
301             Object obj = getProperty(key);
302             if (obj instanceof ValueExpression) {
303                 s += "'" + ((ValueExpression) obj).getExpressionString() + "'";
304 
305             } else if (obj != null) {
306                 s += "'" + obj + "'";
307 
308             } else {
309                 s += obj;
310             }
311 
312         }
313 
314         s += "}";
315 
316         return s;
317     }
318 
319 }