1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package net.wotonomy.control;
19
20 import net.wotonomy.foundation.NSMutableDictionary;
21 import net.wotonomy.foundation.NSSelector;
22 import net.wotonomy.foundation.internal.WotonomyException;
23
24 /***
25 * @author ezamudio@nasoft.com
26 * @author $Author: cgruber $
27 * @version $Revision: 894 $
28 */
29 public class EOKeyComparisonQualifier
30 extends EOQualifier
31 implements EOKeyValueArchiving, EOQualifierEvaluation {
32
33 private String _leftKey;
34 private String _rightKey;
35 private NSSelector _selector;
36
37 public EOKeyComparisonQualifier(String leftKey, NSSelector selector, String rightKey) {
38 super();
39 _leftKey = leftKey;
40 _rightKey = rightKey;
41 _selector = selector;
42 }
43
44 public String leftKey() {
45 return _leftKey;
46 }
47
48 public String rightKey() {
49 return _rightKey;
50 }
51
52 public NSSelector selector() {
53 return _selector;
54 }
55
56 /***
57 * Evaluates this qualifier for the specified object,
58 * and returns whether the object is qualified.
59 * selector() is invoked on the value for key() on the
60 * specified object, with value() as the parameter.
61 */
62 public boolean evaluateWithObject(Object eo) {
63 try {
64 Object lvalue, rvalue;
65 if ( eo instanceof EOKeyValueCoding) {
66 lvalue = ((EOKeyValueCoding)eo).valueForKey(leftKey());
67 rvalue = ((EOKeyValueCoding)eo).valueForKey(rightKey());
68 } else {
69 lvalue = EOKeyValueCodingSupport.valueForKey(eo, leftKey());
70 rvalue = EOKeyValueCodingSupport.valueForKey(eo, rightKey());
71 }
72 return ((Boolean)_selector.invoke( lvalue, rvalue)).booleanValue();
73 } catch (Exception exc) {
74 throw new WotonomyException( exc );
75 }
76 }
77
78
79
80
81 public void encodeWithKeyValueArchiver(EOKeyValueArchiver arch) {
82 arch.encodeObject("EOKeyComparisonQualifier", "class");
83 arch.encodeObject(_leftKey, "key");
84 NSMutableDictionary d = new NSMutableDictionary(2);
85 arch.encodeObject(_rightKey, "value");
86 String selname = null;
87 if (_selector.equals(EOQualifier.QualifierOperatorCaseInsensitiveLike))
88 selname = "caseInsensitiveLike:";
89 else if (_selector.equals(EOQualifier.QualifierOperatorContains))
90 selname = "contains:";
91 else if (_selector.equals(EOQualifier.QualifierOperatorEqual))
92 selname = "isEqualTo:";
93 else if (_selector.equals(EOQualifier.QualifierOperatorGreaterThan))
94 selname = "greaterThan:";
95 else if (_selector.equals(EOQualifier.QualifierOperatorGreaterThanOrEqualTo))
96 selname = "greaterThanOrEqualTo:";
97 else if (_selector.equals(EOQualifier.QualifierOperatorLessThan))
98 selname = "lessThan:";
99 else if (_selector.equals(EOQualifier.QualifierOperatorLessThanOrEqualTo))
100 selname = "lessThanOrEqualTo:";
101 else if (_selector.equals(EOQualifier.QualifierOperatorLike))
102 selname = "like:";
103 else if (_selector.equals(EOQualifier.QualifierOperatorNotEqual))
104 selname = "isNotEqualTo:";
105 else
106 selname = _selector.name() + ":";
107 arch.encodeObject(selname, "selectorName");
108 }
109
110 public static Object decodeWithKeyValueArchiver(EOKeyValueUnarchiver arch) {
111 String k = (String)arch.decodeObjectForKey("key");
112 String v = (String)arch.decodeObjectForKey("value");
113 NSSelector sel = null;
114 String sname = (String)arch.decodeObjectForKey("selectorName");
115 if (sname.equals("isEqualTo:"))
116 sel = EOQualifier.QualifierOperatorEqual;
117 else if (sname.equals("isNotEqualTo:"))
118 sel = EOQualifier.QualifierOperatorNotEqual;
119 else if (sname.equals("caseInsensitiveLike:"))
120 sel = EOQualifier.QualifierOperatorCaseInsensitiveLike;
121 else if (sname.equals("contains:"))
122 sel = EOQualifier.QualifierOperatorContains;
123 else if (sname.equals("greaterThan:"))
124 sel = EOQualifier.QualifierOperatorGreaterThan;
125 else if (sname.equals("greaterThanOrEqualTo:"))
126 sel = EOQualifier.QualifierOperatorGreaterThanOrEqualTo;
127 else if (sname.equals("lessThan:"))
128 sel = EOQualifier.QualifierOperatorLessThan;
129 else if (sname.equals("lessThanOrEqualTo:"))
130 sel = EOQualifier.QualifierOperatorLessThanOrEqualTo;
131 else if (sname.equals("like:"))
132 sel = EOQualifier.QualifierOperatorLike;
133 EOKeyComparisonQualifier q = new EOKeyComparisonQualifier(k, sel, v);
134 return q;
135 }
136
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151