1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2001 Michael Powers
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.access;
import net.wotonomy.foundation.NSArray;
import net.wotonomy.foundation.NSData;
import net.wotonomy.foundation.NSDictionary;
import net.wotonomy.foundation.NSKeyValueCoding;
import net.wotonomy.foundation.NSMutableArray;
import net.wotonomy.foundation.NSMutableDictionary;
import net.wotonomy.foundation.NSTimestamp;
/**
*
* @author ezamudio@nasoft.com
* @author $Author: cgruber $
* @version $Revision: 894 $
*/
public abstract class EOAdaptor {
protected String _name;
@SuppressWarnings("unchecked")
protected NSDictionary<String, Object> _connectionDictionary = (NSDictionary<String, Object>) NSDictionary.EmptyDictionary;
protected NSMutableArray<Object> _contexts = new NSMutableArray<>();
protected Class<? extends EOSQLExpression> _expressionClass;
private static NSMutableDictionary<String, String> _expressionClassesByName = new NSMutableDictionary<>();
public EOAdaptor(String name) {
super();
_name = name;
}
/**
* Creates an adaptor with model's adaptorName and sets its connection
* dictionary to the model's connection dictionary.
*
* @param model The model to take adaptorName and connectionDictionary from.
* @return The adaptor specified in model.
*/
public static EOAdaptor adaptorWithModel(EOModel model) {
if (model == null)
throw new IllegalArgumentException("Model must not be null.");
if (model.adaptorName() == null || model.adaptorName().length() == 0)
throw new IllegalArgumentException("Cannot create an adaptor with an empty name.");
EOAdaptor adaptor = adaptorWithName(model.adaptorName());
if (adaptor == null)
throw new IllegalArgumentException("Cannot create adaptor with name " + model.adaptorName());
adaptor.setConnectionDictionary(model.connectionDictionary());
return adaptor;
}
/**
* Instantiates an adaptor of a concrete EOAdaptor subclass, based on name. If
* name is a fully qualified class name, then it returns an instance of that
* class by invoking the constructor with a string argument. Otherwise, it tries
* to find a class called (name)Adaptor in a package called
* net.wotonomy.(lowercase name)adaptor; if it can't find one, an exception is
* raised.
*
* @param name The name of the adaptor, or a fully qualified class name.
* @return
*/
@SuppressWarnings("unchecked")
public static EOAdaptor adaptorWithName(String name) {
Class<? extends EOAdaptor> adaptorClass = null;
String cname = null;
if (name.endsWith("Adaptor") && name.indexOf('.') > 0) {
cname = name;
int lastdot = name.lastIndexOf('.');
// take off the package and the 'Adaptor' suffix
name = cname.substring(lastdot, cname.length() - 7);
} else {
// construct the fully qualified class name
cname = "net.wotonomy." + name.toLowerCase() + "adaptor." + name + "Adaptor";
}
try {
adaptorClass = (Class<? extends EOAdaptor>) Class.forName(cname);
} catch (ClassNotFoundException ex) {
throw new IllegalArgumentException("Cannot find class named " + name);
}
EOAdaptor adaptor = null;
java.lang.reflect.Constructor<? extends EOAdaptor> callme = null;
try {
callme = adaptorClass.getConstructor(new Class[] { String.class });
adaptor = callme.newInstance((Object[]) new String[] { name });
} catch (ClassCastException ex) {
throw new IllegalArgumentException(
"Class " + adaptorClass.getName() + " must inherit from net.wotonomy.access.EOAdaptor");
} catch (Exception ex) {
throw new IllegalArgumentException(
"Cannot find or invoke constructor with name argument in class " + adaptorClass.getName());
}
return adaptor;
}
public static void setExpressionClassName(String expClassName, String adaptorClassName) {
_expressionClassesByName.setObjectForKey(expClassName, adaptorClassName);
}
public static String expressionClassName(String adaptorClassName) {
return (String) _expressionClassesByName.objectForKey(adaptorClassName);
}
public void assignExternalInfoForAttribute(EOAttribute attribute) {
if (!attribute.isDerived()) {
attribute.setColumnName(attribute.name().toUpperCase());
}
assignExternalTypeForAttribute(attribute);
}
public void assignExternalTypeForAttribute(EOAttribute attribute) {
}
public void assignExternalInfoForEntity(EOEntity entity) {
entity.setExternalName(entity.name().toUpperCase());
}
public void assignExternalInfoForEntireModel(EOModel model) {
NSArray<EOEntity> ents = model.entities();
for (int i = 0; i < ents.count(); i++) {
EOEntity e = ents.objectAtIndex(i);
// TODO: check that entity is not a prototypes entity
NSArray<EOAttribute> atts = e.attributes();
for (int j = 0; j < atts.count(); j++) {
EOAttribute a = atts.objectAtIndex(i);
assignExternalInfoForAttribute(a);
}
assignExternalInfoForEntity(e);
}
}
public boolean canServiceModel(EOModel model) {
NSDictionary<String, Object> mcd = model.connectionDictionary();
if (mcd == null && _connectionDictionary == null)
return true;
if (mcd == null || _connectionDictionary == null)
return false;
return mcd.equals(_connectionDictionary);
}
public void setConnectionDictionary(NSDictionary<String, Object> connection) {
_connectionDictionary = connection;
}
public NSDictionary<String, Object> connectionDictionary() {
return _connectionDictionary;
}
public NSArray<Object> contexts() {
return new NSArray<>(_contexts);
}
public abstract void assertConnectionDictionaryIsValid();
public abstract EOAdaptorContext createAdaptorContext();
public abstract Class<? extends EOSQLExpression> defaultExpressionClass();
public abstract EOSQLExpressionFactory expressionFactory();
public abstract boolean isValidQualifierType(String typeName, EOModel model);
@SuppressWarnings("unchecked")
public Class<? extends EOSQLExpression> expressionClass() {
if (_expressionClass != null)
return _expressionClass;
String cname = expressionClassName(name());
if (cname != null) {
try {
_expressionClass = (Class<? extends EOSQLExpression>) Class.forName(cname);
} catch (ClassNotFoundException ex) {
throw new IllegalStateException("Cannot find expression class named " + cname);
}
}
return defaultExpressionClass();
}
public NSArray<?> externalTypesWithModel(EOModel model) {
return NSArray.EmptyArray;
}
public NSData fetchedValueForDataValue(NSData value, EOAttribute attr) {
return value;
}
public NSTimestamp fetchedValueForDateValue(NSTimestamp value, EOAttribute attr) {
return value;
}
public Number fetchedValueForNumberValue(Number value, EOAttribute attr) {
return value;
}
public String fetchedValueForStringValue(String value, EOAttribute attr) {
return value;
}
public Object fetchedValueForValue(Object value, EOAttribute attr) {
if (value == NSKeyValueCoding.NullValue)
return value;
if (value instanceof String)
return fetchedValueForStringValue((String) value, attr);
if (value instanceof NSData)
return fetchedValueForDataValue((NSData) value, attr);
if (value instanceof Number)
return fetchedValueForNumberValue((Number) value, attr);
if (value instanceof NSTimestamp)
return fetchedValueForDateValue((NSTimestamp) value, attr);
return value;
}
public void handleDroppedConnection() {
for (int i = 0; i < _contexts.count(); i++) {
EOAdaptorContext c = (EOAdaptorContext) _contexts.objectAtIndex(i);
c.transactionDidRollback();
c.handleDroppedConnection();
}
_contexts.removeAllObjects();
}
public boolean hasOpenChannels() {
for (int i = 0; i < _contexts.count(); i++) {
EOAdaptorContext c = (EOAdaptorContext) _contexts.objectAtIndex(i);
if (c.hasOpenChannels())
return true;
}
return false;
}
public String internalTypeForExternalType(String extType, EOModel model) {
return null;
}
public boolean isDroppedConnectionException(Exception ex) {
return false;
}
public String name() {
return _name;
}
public NSArray<?> prototypeAttributes() {
return NSArray.EmptyArray;
}
}
/*
* $Log$ Revision 1.2 2006/02/16 16:47:14 cgruber Move some classes in to
* "internal" packages and re-work imports, etc.
*
* Also use UnsupportedOperationExceptions where appropriate, instead of
* WotonomyExceptions.
*
* Revision 1.1 2006/02/16 13:19:57 cgruber Check in all sources in
* eclipse-friendly maven-enabled packages.
*
* Revision 1.2 2005/12/08 06:52:32 cgruber Move tests, improve build.xml, and
* make certain casts explicit so that Java 1.5 doesn't complain about varargs.
*
* Revision 1.1 2003/08/13 00:37:45 chochos an almost complete implementation of
* the abstract adaptor-layer classes
*
*/
|