View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Michael Powers
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
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  	/* (non-Javadoc)
79  	 * @see net.wotonomy.control.EOKeyValueArchiving#encodeWithKeyValueArchiver(net.wotonomy.control.EOKeyValueArchiver)
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  * $Log$
140  * Revision 1.2  2006/02/16 16:47:14  cgruber
141  * Move some classes in to "internal" packages and re-work imports, etc.
142  *
143  * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
144  *
145  * Revision 1.1  2006/02/16 13:19:57  cgruber
146  * Check in all sources in eclipse-friendly maven-enabled packages.
147  *
148  * Revision 1.1  2003/08/12 01:42:36  chochos
149  * this qualifier was missing
150  *
151  */