1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package net.wotonomy.access; |
19 |
|
|
20 |
|
import net.wotonomy.foundation.NSArray; |
21 |
|
import net.wotonomy.foundation.NSData; |
22 |
|
import net.wotonomy.foundation.NSDictionary; |
23 |
|
import net.wotonomy.foundation.NSKeyValueCoding; |
24 |
|
import net.wotonomy.foundation.NSMutableArray; |
25 |
|
import net.wotonomy.foundation.NSMutableDictionary; |
26 |
|
import net.wotonomy.foundation.NSTimestamp; |
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
0 |
public abstract class EOAdaptor { |
36 |
|
|
37 |
|
protected String _name; |
38 |
0 |
protected NSDictionary _connectionDictionary = NSDictionary.EmptyDictionary; |
39 |
0 |
protected NSMutableArray _contexts = new NSMutableArray(); |
40 |
|
protected Class _expressionClass; |
41 |
0 |
private static NSMutableDictionary _expressionClassesByName = new NSMutableDictionary(); |
42 |
|
|
43 |
|
public EOAdaptor(String name) { |
44 |
0 |
super(); |
45 |
0 |
_name = name; |
46 |
0 |
} |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
public static EOAdaptor adaptorWithModel(EOModel model) { |
55 |
0 |
if (model == null) |
56 |
0 |
throw new IllegalArgumentException("Model must not be null."); |
57 |
0 |
if (model.adaptorName() == null || model.adaptorName().length() == 0) |
58 |
0 |
throw new IllegalArgumentException("Cannot create an adaptor with an empty name."); |
59 |
0 |
EOAdaptor adaptor = adaptorWithName(model.adaptorName()); |
60 |
0 |
if (adaptor == null) |
61 |
0 |
throw new IllegalArgumentException("Cannot create adaptor with name " + model.adaptorName()); |
62 |
0 |
adaptor.setConnectionDictionary(model.connectionDictionary()); |
63 |
0 |
return adaptor; |
64 |
|
} |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
public static EOAdaptor adaptorWithName(String name) { |
76 |
0 |
Class adaptorClass = null; |
77 |
0 |
String cname = null; |
78 |
0 |
if (name.endsWith("Adaptor") && name.indexOf('.') > 0) { |
79 |
0 |
cname = name; |
80 |
0 |
int lastdot = name.lastIndexOf('.'); |
81 |
|
|
82 |
0 |
name = cname.substring(lastdot, cname.length() - 7); |
83 |
0 |
} else { |
84 |
|
|
85 |
0 |
cname = "net.wotonomy." + name.toLowerCase() + "adaptor." + name + "Adaptor"; |
86 |
|
} |
87 |
|
try { |
88 |
0 |
adaptorClass = Class.forName(cname); |
89 |
0 |
} catch (ClassNotFoundException ex) { |
90 |
0 |
throw new IllegalArgumentException("Cannot find class named " + name); |
91 |
0 |
} |
92 |
0 |
EOAdaptor adaptor = null; |
93 |
0 |
java.lang.reflect.Constructor callme = null; |
94 |
|
try { |
95 |
0 |
callme = adaptorClass.getConstructor(new Class[]{ String.class }); |
96 |
0 |
adaptor = (EOAdaptor)callme.newInstance((Object[])new String[]{ name }); |
97 |
0 |
} catch (ClassCastException ex) { |
98 |
0 |
throw new IllegalArgumentException("Class " + adaptorClass.getName() + " must inherit from net.wotonomy.access.EOAdaptor"); |
99 |
0 |
}catch (Exception ex) { |
100 |
0 |
throw new IllegalArgumentException("Cannot find or invoke constructor with name argument in class " + adaptorClass.getName()); |
101 |
0 |
} |
102 |
0 |
return adaptor; |
103 |
|
} |
104 |
|
|
105 |
|
public static void setExpressionClassName(String expClassName, String adaptorClassName) { |
106 |
0 |
_expressionClassesByName.setObjectForKey(expClassName, adaptorClassName); |
107 |
0 |
} |
108 |
|
public static String expressionClassName(String adaptorClassName) { |
109 |
0 |
return (String)_expressionClassesByName.objectForKey(adaptorClassName); |
110 |
|
} |
111 |
|
|
112 |
|
public void assignExternalInfoForAttribute(EOAttribute attribute) { |
113 |
0 |
if (!attribute.isDerived()) { |
114 |
0 |
attribute.setColumnName(attribute.name().toUpperCase()); |
115 |
|
} |
116 |
0 |
assignExternalTypeForAttribute(attribute); |
117 |
0 |
} |
118 |
|
|
119 |
|
public void assignExternalTypeForAttribute(EOAttribute attribute) { |
120 |
0 |
} |
121 |
|
|
122 |
|
public void assignExternalInfoForEntity(EOEntity entity) { |
123 |
0 |
entity.setExternalName(entity.name().toUpperCase()); |
124 |
0 |
} |
125 |
|
|
126 |
|
public void assignExternalInfoForEntireModel(EOModel model) { |
127 |
0 |
NSArray ents = model.entities(); |
128 |
0 |
for (int i = 0; i < ents.count(); i++) { |
129 |
0 |
EOEntity e = (EOEntity)ents.objectAtIndex(i); |
130 |
|
|
131 |
0 |
NSArray atts = e.attributes(); |
132 |
0 |
for (int j = 0; j < atts.count(); j++) { |
133 |
0 |
EOAttribute a = (EOAttribute)atts.objectAtIndex(i); |
134 |
0 |
assignExternalInfoForAttribute(a); |
135 |
|
} |
136 |
0 |
assignExternalInfoForEntity(e); |
137 |
|
} |
138 |
0 |
} |
139 |
|
|
140 |
|
public boolean canServiceModel(EOModel model) { |
141 |
0 |
NSDictionary mcd = model.connectionDictionary(); |
142 |
0 |
if (mcd == null && _connectionDictionary == null) |
143 |
0 |
return true; |
144 |
0 |
if (mcd == null || _connectionDictionary == null) |
145 |
0 |
return false; |
146 |
0 |
return mcd.equals(_connectionDictionary); |
147 |
|
} |
148 |
|
|
149 |
|
public void setConnectionDictionary(NSDictionary connection) { |
150 |
0 |
_connectionDictionary = connection; |
151 |
0 |
} |
152 |
|
public NSDictionary connectionDictionary() { |
153 |
0 |
return _connectionDictionary; |
154 |
|
} |
155 |
|
|
156 |
|
public NSArray contexts() { |
157 |
0 |
return new NSArray(_contexts); |
158 |
|
} |
159 |
|
|
160 |
|
public abstract void assertConnectionDictionaryIsValid(); |
161 |
|
|
162 |
|
public abstract EOAdaptorContext createAdaptorContext(); |
163 |
|
|
164 |
|
public abstract Class defaultExpressionClass(); |
165 |
|
|
166 |
|
public abstract EOSQLExpressionFactory expressionFactory(); |
167 |
|
|
168 |
|
public abstract boolean isValidQualifierType(String typeName, EOModel model); |
169 |
|
|
170 |
|
public Class expressionClass() { |
171 |
0 |
if (_expressionClass != null) |
172 |
0 |
return _expressionClass; |
173 |
0 |
String cname = expressionClassName(name()); |
174 |
0 |
if (cname != null) { |
175 |
|
try { |
176 |
0 |
_expressionClass = Class.forName(cname); |
177 |
0 |
} catch (ClassNotFoundException ex) { |
178 |
0 |
throw new IllegalStateException("Cannot find expression class named " + cname); |
179 |
0 |
} |
180 |
|
} |
181 |
0 |
return defaultExpressionClass(); |
182 |
|
} |
183 |
|
|
184 |
|
public NSArray externalTypesWithModel(EOModel model) { |
185 |
0 |
return NSArray.EmptyArray; |
186 |
|
} |
187 |
|
|
188 |
|
public NSData fetchedValueForDataValue(NSData value, EOAttribute attr) { |
189 |
0 |
return value; |
190 |
|
} |
191 |
|
|
192 |
|
public NSTimestamp fetchedValueForDateValue(NSTimestamp value, EOAttribute attr) { |
193 |
0 |
return value; |
194 |
|
} |
195 |
|
|
196 |
|
public Number fetchedValueForNumberValue(Number value, EOAttribute attr) { |
197 |
0 |
return value; |
198 |
|
} |
199 |
|
|
200 |
|
public String fetchedValueForStringValue(String value, EOAttribute attr) { |
201 |
0 |
return value; |
202 |
|
} |
203 |
|
|
204 |
|
public Object fetchedValueForValue(Object value, EOAttribute attr) { |
205 |
0 |
if (value == NSKeyValueCoding.NullValue) |
206 |
0 |
return value; |
207 |
0 |
if (value instanceof String) |
208 |
0 |
return fetchedValueForStringValue((String)value, attr); |
209 |
0 |
if (value instanceof NSData) |
210 |
0 |
return fetchedValueForDataValue((NSData)value, attr); |
211 |
0 |
if (value instanceof Number) |
212 |
0 |
return fetchedValueForNumberValue((Number)value, attr); |
213 |
0 |
if (value instanceof NSTimestamp) |
214 |
0 |
return fetchedValueForDateValue((NSTimestamp)value, attr); |
215 |
0 |
return value; |
216 |
|
} |
217 |
|
|
218 |
|
public void handleDroppedConnection() { |
219 |
0 |
for (int i = 0; i < _contexts.count(); i++) { |
220 |
0 |
EOAdaptorContext c = (EOAdaptorContext)_contexts.objectAtIndex(i); |
221 |
0 |
c.transactionDidRollback(); |
222 |
0 |
c.handleDroppedConnection(); |
223 |
|
} |
224 |
0 |
_contexts.removeAllObjects(); |
225 |
0 |
} |
226 |
|
|
227 |
|
public boolean hasOpenChannels() { |
228 |
0 |
for (int i = 0; i < _contexts.count(); i++) { |
229 |
0 |
EOAdaptorContext c = (EOAdaptorContext)_contexts.objectAtIndex(i); |
230 |
0 |
if (c.hasOpenChannels()) |
231 |
0 |
return true; |
232 |
|
} |
233 |
0 |
return false; |
234 |
|
} |
235 |
|
|
236 |
|
public String internalTypeForExternalType(String extType, EOModel model) { |
237 |
0 |
return null; |
238 |
|
} |
239 |
|
|
240 |
|
public boolean isDroppedConnectionException(Exception ex) { |
241 |
0 |
return false; |
242 |
|
} |
243 |
|
|
244 |
|
public String name() { |
245 |
0 |
return _name; |
246 |
|
} |
247 |
|
|
248 |
|
public NSArray prototypeAttributes() { |
249 |
0 |
return NSArray.EmptyArray; |
250 |
|
} |
251 |
|
|
252 |
|
} |
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|