View Javadoc

1   /*
2    * $Id: OrderConverter.java,v 1.18 2011/06/16 09:29:40 jbmeslin Exp $
3    * 
4    */
5   package org.rcfaces.core.internal.converter;
6   
7   import java.util.HashMap;
8   import java.util.Iterator;
9   import java.util.Map;
10  
11  import javax.faces.component.UIComponent;
12  import javax.faces.context.FacesContext;
13  import javax.faces.convert.Converter;
14  
15  import org.rcfaces.core.model.AbstractConverter;
16  
17  /**
18   * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
19   * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:40 $
20   */
21  public class OrderConverter extends AbstractConverter {
22      private static final String REVISION = "$Revision: 1.18 $";
23  
24      public static final Converter SINGLETON = new OrderConverter();
25  
26      private static final String ASCENDING_ORDER_NAME = "ascending";
27  
28      private static final String DESCENDING_ORDER_NAME = "descending";
29  
30      private static final Boolean DEFAULT_ORDER = Boolean.TRUE;
31  
32      private static Map ORDER_TYPES = new HashMap(5);
33      static {
34          ORDER_TYPES.put(ASCENDING_ORDER_NAME, Boolean.TRUE);
35  
36          ORDER_TYPES.put(DESCENDING_ORDER_NAME, Boolean.FALSE);
37      }
38  
39      /*
40       * (non-Javadoc)
41       * 
42       * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext,
43       *      javax.faces.component.UIComponent, java.lang.String)
44       */
45      public Object getAsObject(FacesContext context, UIComponent component,
46              String value) {
47  
48          if (value == null || value.length() < 1 || "default".equals(value)) {
49              return DEFAULT_ORDER;
50          }
51  
52          value = value.toLowerCase();
53  
54          Boolean b = (Boolean) ORDER_TYPES.get(value);
55          if (b != null) {
56              return b;
57          }
58  
59          throw new IllegalArgumentException("Keyword '" + value
60                  + "' is not supported for an order type !");
61      }
62  
63      /*
64       * (non-Javadoc)
65       * 
66       * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext,
67       *      javax.faces.component.UIComponent, java.lang.Object)
68       */
69      public String getAsString(FacesContext context, UIComponent component,
70              Object value) {
71  
72          if (value == null) {
73              return (String) ORDER_TYPES.get(DEFAULT_ORDER);
74          }
75  
76          if ((value instanceof Boolean) == false) {
77              throw new IllegalArgumentException("Value must be a Boolean !");
78          }
79  
80          for (Iterator it = ORDER_TYPES.entrySet().iterator(); it.hasNext();) {
81              Map.Entry entry = (Map.Entry) it.next();
82  
83              if (value.equals(entry.getValue())) {
84                  return (String) entry.getKey();
85              }
86          }
87  
88          throw new IllegalArgumentException("Value '" + value
89                  + "' is not supported for an order type !");
90      }
91  
92  }