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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
package net.wotonomy.test;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import net.wotonomy.control.ArrayFault;
import net.wotonomy.control.EOEditingContext;
import net.wotonomy.control.EOFetchSpecification;
import net.wotonomy.control.EOGlobalID;
import net.wotonomy.control.EOObjectStore;
import net.wotonomy.control.EOObserverCenter;
import net.wotonomy.control.KeyValueCodingUtilities;
import net.wotonomy.datastore.DataKey;
import net.wotonomy.datastore.DataSoup;
import net.wotonomy.datastore.DataView;
import net.wotonomy.datastore.XMLFileSoup;
import net.wotonomy.foundation.NSArray;
import net.wotonomy.foundation.NSMutableArray;
import net.wotonomy.foundation.NSMutableDictionary;
import net.wotonomy.foundation.NSNotification;
import net.wotonomy.foundation.NSNotificationQueue;
import net.wotonomy.foundation.internal.WotonomyException;
/**
* An object store that wraps a datastore for vending test objects.
*/
public class DataObjectStore extends EOObjectStore
{
DataSoup soup;
/**
* Constructor specifies path to datastore.
*/
public DataObjectStore( String aPath )
{
soup = new XMLFileSoup( aPath );
}
/**
* This implementation returns an appropriately configured array fault.
*/
public NSArray arrayFaultWithSourceGlobalID (
EOGlobalID aGlobalID,
String aRelationship,
EOEditingContext aContext )
{
return new ArrayFault(
aGlobalID, aRelationship, aContext );
}
/**
* This implementation returns the actual
* object for the specified id.
*/
public Object faultForGlobalID (
EOGlobalID aGlobalID,
EOEditingContext aContext )
{
System.out.println( "DataObjectStore.faultForGlobalID: * reading object * : " + aGlobalID );
Object result = soup.getObjectByKey(
((DataKeyID)aGlobalID).getKey() );
if ( result == null ) return null;
//! transpose keys to objects
convertRelationKeysToObjects( aContext, result, aGlobalID );
//!
aContext.recordObject( result, aGlobalID );
return result;
}
/**
* Returns a fault representing an object of
* the specified entity type with values from
* the specified dictionary. The fault should
* belong to the specified editing context.
*/
public Object faultForRawRow (
Map aDictionary,
String anEntityName,
EOEditingContext aContext )
{
//TODO: faults are not yet supported
throw new WotonomyException(
"Faults are not yet supported." );
}
/**
* Given a newly instantiated object, this method
* initializes its properties to values appropriate
* for the specified id. The object should belong
* to the specified editing context.
* This method is called to populate faults.
*/
public void initializeObject(Object anObject, EOGlobalID aGlobalID,
EOEditingContext aContext) {
if (aGlobalID.isTemporary()) {
// TODO: this should never happen, but it does now until we get
// faults.
// do not reinit an uncommitted object
return;
}
System.out.println("DataObjectStore.initializeObject: * reading object * : "
+ aGlobalID);
//net.wotonomy.ui.swing.util.StackTraceInspector.printShortStackTrace();
Object original = soup.getObjectByKey(((DataKeyID) aGlobalID).getKey());
// ! transpose keys to objects
convertRelationKeysToObjects(aContext, original, aGlobalID);
// !
EOObserverCenter.notifyObserversObjectWillChange(anObject);
KeyValueCodingUtilities.copy(aContext, original, aContext, anObject);
}
/**
* Remove all values from all objects in memory, turning them into faults,
* and posts a notification that all objects have been invalidated.
*/
public void invalidateAllObjects ()
{
// does nothing except post notification
NSNotificationQueue.defaultQueue().enqueueNotification(
new NSNotification(
InvalidatedAllObjectsInStoreNotification, this ),
NSNotificationQueue.PostNow );
}
/**
* Removes values with the specified ids from memory,
* turning them into faults, and posts a notification
* that those objects have been invalidated.
*/
public void invalidateObjectsWithGlobalIDs (
List aList )
{
// does nothing
}
/**
* Returns false because locking is not permitted.
*/
public boolean isObjectLockedWithGlobalID (
EOGlobalID aGlobalID,
EOEditingContext aContext )
{
return false;
}
/**
* Does nothing because locking is not permitted.
*/
public void lockObjectWithGlobalID (
EOGlobalID aGlobalID,
EOEditingContext aContext )
{
// does nothing
}
/**
* Returns a List of objects associated with the object
* with the specified id for the specified property
* relationship. This method may not return an array fault
* because array faults call this method to fetch on demand.
* All objects must be registered the specified editing context.
* The specified relationship key must produce a result of
* type Collection for the source object or an exception is thrown.
*/
public NSArray objectsForSourceGlobalID (
EOGlobalID aGlobalID,
String aRelationship,
EOEditingContext aContext )
{
System.out.println( "DataObjectStore.objectsForSourceGlobalID: * reading object * : " + aGlobalID );
Object object = soup.getObjectByKey(((DataKeyID)aGlobalID).getKey() );
if ( object == null ) return null;
Object fault;
EOGlobalID id;
NSMutableArray result = new NSMutableArray();
Iterator it = ((TestObject)object).getChildList().iterator();
while ( it.hasNext() )
{
id = new DataKeyID((DataKey)it.next());
fault = aContext.faultForGlobalID( id, aContext );
// if key still exists
if ( fault != null )
{
//System.out.println( "objectsForSourceGlobalID: found: " + id + " : " + fault );
result.add( fault );
// for testing purposes
((TestObject)fault).setParent( (TestObject) object );
}
else // key no longer exists
{
// do not add
System.out.println( "objectsForSourceGlobalID: could not find fault for id: " + id );
}
}
return result;
}
/**
* Returns a List of objects the meet the criteria of
* the supplied specification.
* Each object is registered with the specified editing context.
* If any object is already registered in the specified context,
* it is not refetched and that object should be used in the array.
*/
public NSArray objectsWithFetchSpecification (
EOFetchSpecification aFetchSpec,
EOEditingContext aContext )
{
//TODO: fetch specs are not yet supported
DataView view = soup.queryObjects( null, null );
System.out.println( "DataObjectStore: ** querying all objects **" );
// we've changed this implementation so that
// it simply calls faultForGlobalID on the context
// for each id in the result set.
// this way, child contexts inherit parent's state.
// however, it's unclear if the specification allows
// faults in the resulting array. sounds like it doesn't.
NSMutableArray result = new NSMutableArray();
DataKeyID id;
Iterator it = view.iterator();
while ( it.hasNext() )
{
id = new DataKeyID( view.getKeyForObject( it.next() ) );
result.addObject( aContext.faultForGlobalID( id, aContext ) );
}
return result;
}
/**
* Removes all values from the specified object,
* converting it into a fault for the specified id.
* New or deleted objects should not be refaulted.
*/
public void refaultObject (
Object anObject,
EOGlobalID aGlobalID,
EOEditingContext aContext )
{
//TODO: faults are not yet supported
// just re-initialize the object
initializeObject( anObject, aGlobalID, aContext );
}
/**
* Writes all changes in the specified editing context
* to the respository.
*/
public void saveChangesInEditingContext (
EOEditingContext aContext )
{
Object o;
DataKeyID id;
Iterator it;
// process deletes
it = aContext.deletedObjects().iterator();
while ( it.hasNext() )
{
o = it.next();
id = (DataKeyID) aContext.globalIDForObject( o );
System.out.println( "DataObjectStore: * deleting object * : " + id );
soup.removeObject( id.getKey() );
// remove object from editing context
aContext.forgetObject( o );
}
// process inserts
NSMutableDictionary userInfo = null;
it = aContext.insertedObjects().iterator();
while ( it.hasNext() )
{
o = it.next();
EOGlobalID oldId = aContext.globalIDForObject( o );
//! transpose objects to keys
convertRelationObjectsToKeys( aContext, (TestObject) o );
id = new DataKeyID( soup.addObject( o ) );
convertRelationKeysToObjects( aContext, (TestObject) o, oldId );
//!
System.out.println( "DataObjectStore: * adding object * : " + id );
// save mapping of old id to new id
if ( userInfo == null )
{
userInfo = new NSMutableDictionary();
}
userInfo.setObjectForKey( id, oldId );
}
// broadcast inserted objects' new ids if necessary
if ( userInfo != null )
{
NSNotificationQueue.defaultQueue().enqueueNotification(
new NSNotification(
EOGlobalID.GlobalIDChangedNotification, null, userInfo ),
NSNotificationQueue.PostNow );
}
System.out.println( aContext.updatedObjects() );
// process updates
it = aContext.updatedObjects().iterator();
while ( it.hasNext() )
{
//if ( true ) // test validation error message handling
//throw new RuntimeException( "Update not allowed." );
o = it.next();
id = (DataKeyID) aContext.globalIDForObject( o );
System.out.println( "DataObjectStore: * updating object * : " + id );
//! transpose objects to keys
convertRelationObjectsToKeys( aContext, (TestObject) o );
soup.updateObject( id.getKey(), o );
convertRelationKeysToObjects( aContext, (TestObject) o, id );
//!
}
}
private void convertRelationKeysToObjects(
EOEditingContext aContext, Object anObject, EOGlobalID aGlobalID )
{ // System.out.println( "convertRelationKeysToObjects: " + anObject );
// set editing context for testing
((TestObject)anObject).editingContext = aContext;
Object fault;
DataKeyID id;
List result = new LinkedList();
Iterator it = ((TestObject)anObject).getChildList().iterator();
while ( it.hasNext() )
{
id = new DataKeyID((DataKey)it.next());
fault = aContext.faultForGlobalID( id, aContext );
// if key still exists
if ( fault != null )
{
//System.out.println( "convertRelationObjectsToKeys: found: " + id + " : " + fault );
result.add( fault );
// for testing purposes
((TestObject)fault).setParent( (TestObject) anObject );
}
else // key no longer exists
{
// do not add
System.out.println( "convertRelationObjectsToKeys: could not find fault for id: " + id );
}
}
// this tests loading manually on-demand
// ((TestObject)anObject).setChildList( null );
// this tests loading immediately
((TestObject)anObject).setChildList( result );
// this tests loading array faults
// ((TestObject)result).setChildList( null );
((TestObject)anObject).setChildList(
aContext.arrayFaultWithSourceGlobalID(
aGlobalID, "childList", aContext ) );
}
private void convertRelationObjectsToKeys(
EOEditingContext aContext, Object anObject )
{ // System.out.println( "convertRelationObjectsToKeys: " + anObject );
Object o;
DataKeyID id;
List result = new LinkedList();
Iterator it = ((TestObject)anObject).getChildList().iterator();
// for testing purposes
((TestObject)anObject).setParent( null );
((TestObject)anObject).editingContext = null;
while ( it.hasNext() )
{
o = it.next();
//System.out.println( "convertRelationObjectsToKeys: " + o + " : " + aContext.globalIDForObject( o ) );
id = (DataKeyID)aContext.globalIDForObject( o );
// if object still exists in context
if ( id != null )
{
result.add( id.getKey() );
}
else // object was deleted
{
// do not add
System.out.println( "convertRelationObjectsToKeys: could not find id for object: " + o );
System.out.println( aContext.registeredObjects() );
}
}
((TestObject)anObject).setChildList( result );
}
/*
* $Log$
* Revision 1.1 2006/02/19 16:30:25 cgruber
* Update imports and maven dependencies.
*
* Revision 1.1 2006/02/16 13:18:56 cgruber
* Check in all sources in eclipse-friendly maven-enabled packages.
*
* Revision 1.18 2002/03/11 03:18:39 mpowers
* Now properly handling ObserverChangesLater.
*
* Revision 1.17 2001/10/26 18:39:44 mpowers
* Posting notifications immediately, rather than delayed.
*
* Revision 1.16 2001/05/06 18:27:10 mpowers
* More broadly catching editing contexts for now.
*
* Revision 1.15 2001/05/05 23:05:43 mpowers
* Implemented Array Faults.
*
* Revision 1.14 2001/05/05 15:00:06 mpowers
* Tested load-on-demand: still works.
* Now using registerClone for consistency.
* Editing context is temporarily posting notification on objectWillChange.
*
* Revision 1.13 2001/05/04 23:24:30 mpowers
* Changes to test code.
*
* Revision 1.12 2001/05/04 16:57:56 mpowers
* Now correctly transposing references to editing contexts when
* cloning/copying between editing contexts.
*
* Revision 1.11 2001/05/02 17:33:28 mpowers
* More changes for testing.
*
* Revision 1.10 2001/04/30 13:15:24 mpowers
* Child contexts re-initializing objects invalidated in parent now
* propery transpose relationships.
*
* Revision 1.9 2001/04/29 22:02:45 mpowers
* Work on id transposing between editing contexts.
*
* Revision 1.8 2001/04/29 02:29:31 mpowers
* Debugging relationship faulting.
*
* Revision 1.7 2001/04/28 22:17:51 mpowers
* Revised PropertyDataSource to be EOClassDescription-aware.
*
* Revision 1.6 2001/04/28 16:18:44 mpowers
* Implementing relationships.
*
* Revision 1.5 2001/04/13 16:33:36 mpowers
* Now broadcasting notifications.
*
* Revision 1.4 2001/04/08 21:00:54 mpowers
* Changes to support new objectsForFetchSpecification scheme.
*
* Revision 1.3 2001/03/22 21:37:52 mpowers
* Testing new features.
*
* Revision 1.2 2001/03/15 21:10:41 mpowers
* Implemented global id re-registration for newly saved inserts.
*
* Revision 1.1 2001/03/05 22:12:11 mpowers
* Created the control package for a datastore-specific implementation
* of EOObjectStore.
*
*
*/
}
|