1
2
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
19
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
41
42
43
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
65
66
67
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 }