1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.wotonomy.control;
20
21 import net.wotonomy.foundation.NSKeyValueCoding;
22 import net.wotonomy.foundation.NSMutableDictionary;
23 import net.wotonomy.foundation.NSSelector;
24 import net.wotonomy.foundation.internal.WotonomyException;
25
26 /***
27 * EOKeyValueQualifier performs a property-based
28 * comparison against a specified value. The comparison
29 * is specified in the form of a NSSelector. The
30 * selector is expected to take two arguments, the
31 * property value on an object and the comparison value,
32 * and return a Boolean indicating whether the object
33 * is qualified. EOQualifier defines selectors that
34 * may be used in creating EOKeyValueQualifiers.
35 *
36 * @author michael@mpowers.net
37 * @author yjcheung@intersectsoft.com
38 * @author $Author: cgruber $
39 * @version $Revision: 894 $
40 */
41 public class EOKeyValueQualifier extends EOQualifier
42 implements EOKeyValueArchiving, EOQualifierEvaluation
43 {
44 private String key;
45 private NSSelector selector;
46 private Object value;
47
48 /***
49 * Constructor specifying a property key, a selector,
50 * and a value for comparison. The selector may be
51 * one of the constant selectors defined on EOQualifier.
52 */
53 public EOKeyValueQualifier(
54 String aKey,
55 NSSelector aSelector,
56 Object aValue )
57 {
58 key = aKey;
59 selector = aSelector;
60 value = aValue;
61 }
62
63 /***
64 * Returns the key for this qualifier.
65 */
66 public String key()
67 {
68 return key;
69 }
70
71 /***
72 * Returns the selector for this qualifier.
73 */
74 public NSSelector selector()
75 {
76 return selector;
77 }
78
79 /***
80 * Returns the value for this qualifier.
81 */
82 public Object value()
83 {
84 return value;
85 }
86
87 /***
88 * Evaluates this qualifier for the specified object,
89 * and returns whether the object is qualified.
90 * selector() is invoked on the value for key() on the
91 * specified object, with value() as the parameter.
92 */
93 public boolean evaluateWithObject( Object anObject )
94 {
95 try
96 {
97 Object value;
98 if ( anObject instanceof EOKeyValueCoding )
99 {
100 value = ((EOKeyValueCoding)anObject).valueForKey( key() );
101 }
102 else
103 {
104 value = EOKeyValueCodingSupport.valueForKey( anObject, key() );
105 }
106 return ((Boolean)selector.invoke( value(), value)).booleanValue();
107 }
108 catch ( Exception exc )
109 {
110 throw new WotonomyException( exc );
111 }
112 }
113
114 /***
115 * Returns a string representation of this qualifier.
116 */
117 public String toString()
118 {
119 return "( " + key + " " + selector + " " + value + " )";
120 }
121
122 public void encodeWithKeyValueArchiver(EOKeyValueArchiver arch) {
123 arch.encodeObject("EOKeyValueQualifier", "class");
124 arch.encodeObject(key, "key");
125 NSMutableDictionary d = new NSMutableDictionary(2);
126 if (value instanceof String)
127 d.setObjectForKey("NSString", "class");
128 else if (value instanceof java.math.BigDecimal)
129 d.setObjectForKey("NSDecimalNumber", "class");
130 else if (value instanceof Number)
131 d.setObjectForKey("NSNumber", "class");
132 else if (value instanceof NSKeyValueCoding.Null)
133 d.setObjectForKey("EONull", "class");
134 if (value != null && !(value instanceof NSKeyValueCoding.Null))
135 d.setObjectForKey(value.toString(), "value");
136 arch.encodeObject(d, "value");
137 String selname = null;
138 if (selector.equals(EOQualifier.QualifierOperatorCaseInsensitiveLike))
139 selname = "caseInsensitiveLike:";
140 else if (selector.equals(EOQualifier.QualifierOperatorContains))
141 selname = "contains:";
142 else if (selector.equals(EOQualifier.QualifierOperatorEqual))
143 selname = "isEqualTo:";
144 else if (selector.equals(EOQualifier.QualifierOperatorGreaterThan))
145 selname = "greaterThan:";
146 else if (selector.equals(EOQualifier.QualifierOperatorGreaterThanOrEqualTo))
147 selname = "greaterThanOrEqualTo:";
148 else if (selector.equals(EOQualifier.QualifierOperatorLessThan))
149 selname = "lessThan:";
150 else if (selector.equals(EOQualifier.QualifierOperatorLessThanOrEqualTo))
151 selname = "lessThanOrEqualTo:";
152 else if (selector.equals(EOQualifier.QualifierOperatorLike))
153 selname = "like:";
154 else if (selector.equals(EOQualifier.QualifierOperatorNotEqual))
155 selname = "isNotEqualTo:";
156 else
157 selname = selector.name() + ":";
158 arch.encodeObject(selname, "selectorName");
159 }
160
161 public static Object decodeWithKeyValueUnarchiver(EOKeyValueUnarchiver arch) {
162 String k = (String)arch.decodeObjectForKey("key");
163 Object v = arch.decodeObjectForKey("value");
164 NSSelector sel = null;
165 String sname = (String)arch.decodeObjectForKey("selectorName");
166 if (sname.equals("isEqualTo:"))
167 sel = EOQualifier.QualifierOperatorEqual;
168 else if (sname.equals("isNotEqualTo:"))
169 sel = EOQualifier.QualifierOperatorNotEqual;
170 else if (sname.equals("caseInsensitiveLike:"))
171 sel = EOQualifier.QualifierOperatorCaseInsensitiveLike;
172 else if (sname.equals("contains:"))
173 sel = EOQualifier.QualifierOperatorContains;
174 else if (sname.equals("greaterThan:"))
175 sel = EOQualifier.QualifierOperatorGreaterThan;
176 else if (sname.equals("greaterThanOrEqualTo:"))
177 sel = EOQualifier.QualifierOperatorGreaterThanOrEqualTo;
178 else if (sname.equals("lessThan:"))
179 sel = EOQualifier.QualifierOperatorLessThan;
180 else if (sname.equals("lessThanOrEqualTo:"))
181 sel = EOQualifier.QualifierOperatorLessThanOrEqualTo;
182 else if (sname.equals("like:"))
183 sel = EOQualifier.QualifierOperatorLike;
184 EOKeyValueQualifier q = new EOKeyValueQualifier(k, sel, v);
185 return q;
186 }
187
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233