1 package net.wotonomy.test;
2
3 import java.util.Iterator;
4 import java.util.List;
5 import java.util.Map;
6
7 import net.wotonomy.control.EOEditingContext;
8 import net.wotonomy.control.EOFetchSpecification;
9 import net.wotonomy.control.EOGlobalID;
10 import net.wotonomy.control.EOObjectStore;
11 import net.wotonomy.control.EOObserverCenter;
12 import net.wotonomy.test.DataKeyID;
13 import net.wotonomy.datastore.DataSoup;
14 import net.wotonomy.datastore.DataView;
15 import net.wotonomy.datastore.XMLFileSoup;
16 import net.wotonomy.foundation.NSArray;
17 import net.wotonomy.foundation.NSMutableArray;
18 import net.wotonomy.foundation.internal.Duplicator;
19 import net.wotonomy.foundation.internal.WotonomyException;
20
21 /***
22 * An object store that wraps a datastore
23 * for vending test objects.
24 */
25 public class TestObjectStore extends EOObjectStore
26 {
27 DataSoup soup;
28
29 /***
30 * Constructor specifies path to datastore.
31 */
32 public TestObjectStore( String aPath )
33 {
34 soup = new XMLFileSoup( aPath );
35 }
36
37 /***
38 * This implementation simply returns
39 * objectsWithSourceGlobalID.
40 */
41 public NSArray arrayFaultWithSourceGlobalID (
42 EOGlobalID aGlobalID,
43 String aRelationship,
44 EOEditingContext aContext )
45 {
46 return objectsForSourceGlobalID(
47 aGlobalID, aRelationship, aContext );
48 }
49
50 /***
51 * This implementation returns the actual
52 * object for the specified id.
53 */
54 public Object faultForGlobalID (
55 EOGlobalID aGlobalID,
56 EOEditingContext aContext )
57 {
58 System.out.println( "TestObjectStore: * reading object * : " + aGlobalID );
59 return soup.getObjectByKey(
60 ((DataKeyID)aGlobalID).getKey() );
61 }
62
63 /***
64 * Returns a fault representing an object of
65 * the specified entity type with values from
66 * the specified dictionary. The fault should
67 * belong to the specified editing context.
68 */
69 public Object faultForRawRow (
70 Map aDictionary,
71 String anEntityName,
72 EOEditingContext aContext )
73 {
74
75 throw new WotonomyException(
76 "Faults are not yet supported." );
77 }
78
79 /***
80 * Given a newly instantiated object, this method
81 * initializes its properties to values appropriate
82 * for the specified id. The object should belong
83 * to the specified editing context.
84 * This method is called to populate faults.
85 */
86 public void initializeObject (
87 Object anObject,
88 EOGlobalID aGlobalID,
89 EOEditingContext aContext )
90 {
91 System.out.println( "TestObjectStore: * reading object * : " + aGlobalID );
92 Object original = soup.getObjectByKey(
93 ((DataKeyID)aGlobalID).getKey() );
94 EOObserverCenter.notifyObserversObjectWillChange( anObject );
95 Duplicator.deepCopy( original, anObject );
96
97 }
98
99 /***
100 * Remove all values from all objects in memory,
101 * turning them into faults, and posts a notification
102 * that all objects have been invalidated.
103 */
104 public void invalidateAllObjects ()
105 {
106
107 }
108
109 /***
110 * Removes values with the specified ids from memory,
111 * turning them into faults, and posts a notification
112 * that those objects have been invalidated.
113 */
114 public void invalidateObjectsWithGlobalIDs (
115 List aList )
116 {
117
118 }
119
120 /***
121 * Returns false because locking is not permitted.
122 */
123 public boolean isObjectLockedWithGlobalID (
124 EOGlobalID aGlobalID,
125 EOEditingContext aContext )
126 {
127 return false;
128 }
129
130 /***
131 * Does nothing because locking is not permitted.
132 */
133 public void lockObjectWithGlobalID (
134 EOGlobalID aGlobalID,
135 EOEditingContext aContext )
136 {
137
138 }
139
140 /***
141 * Returns a List of objects associated with the object
142 * with the specified id for the specified property
143 * relationship. Faults are not allowed in the array.
144 * All objects should belong to the specified editing context.
145 */
146 public NSArray objectsForSourceGlobalID (
147 EOGlobalID aGlobalID,
148 String aRelationship,
149 EOEditingContext aContext )
150 {
151
152 throw new WotonomyException(
153 "Relationships are not yet supported." );
154 }
155
156 /***
157 * Returns a List of objects the meet the criteria of
158 * the supplied specification. Faults are not allowed in the array.
159 * Each object is registered with the specified editing context.
160 * If any object is already registered in the specified context,
161 * it is not refetched and that object should be used in the array.
162 */
163 public NSArray objectsWithFetchSpecification (
164 EOFetchSpecification aFetchSpec,
165 EOEditingContext aContext )
166 {
167
168
169 DataView view = soup.queryObjects( null, null );
170 System.out.println( "TestObjectStore: ** querying all objects **" );
171 NSMutableArray result = new NSMutableArray();
172 Object o;
173 Object existing;
174 DataKeyID id;
175 Iterator it = view.iterator();
176 while ( it.hasNext() )
177 {
178 o = it.next();
179 id = new DataKeyID( view.getKeyForObject( o ) );
180 existing = aContext.objectForGlobalID( id );
181 if ( existing != null )
182 {
183 o = existing;
184 }
185 else
186 {
187 aContext.recordObject( o, id );
188 }
189 result.addObject( o );
190 }
191 return result;
192 }
193
194 /***
195 * Removes all values from the specified object,
196 * converting it into a fault for the specified id.
197 * New or deleted objects should not be refaulted.
198 */
199 public void refaultObject (
200 Object anObject,
201 EOGlobalID aGlobalID,
202 EOEditingContext aContext )
203 {
204
205
206 initializeObject( anObject, aGlobalID, aContext );
207 }
208
209 /***
210 * Writes all changes in the specified editing context
211 * to the respository.
212 */
213 public void saveChangesInEditingContext (
214 EOEditingContext aContext )
215 {
216 Object o;
217 DataKeyID id;
218 Iterator it;
219
220 System.out.println( aContext.updatedObjects() );
221
222
223 it = aContext.updatedObjects().iterator();
224 while ( it.hasNext() )
225 {
226 o = it.next();
227 id = (DataKeyID) aContext.globalIDForObject( o );
228 System.out.println( "TestObjectStore: * updating object * : " + id );
229 soup.updateObject( id.getKey(), o );
230 }
231
232
233 it = aContext.deletedObjects().iterator();
234 while ( it.hasNext() )
235 {
236 o = it.next();
237 id = (DataKeyID) aContext.globalIDForObject( o );
238 System.out.println( "TestObjectStore: * deleting object * : " + id );
239 soup.removeObject( id.getKey() );
240
241 aContext.forgetObject( o );
242 }
243
244
245 it = aContext.insertedObjects().iterator();
246 while ( it.hasNext() )
247 {
248 o = it.next();
249 id = new DataKeyID( soup.addObject( o ) );
250 System.out.println( "TestObjectStore: * adding object * : " + id );
251
252 aContext.forgetObject( o );
253 aContext.recordObject( o, id );
254 }
255
256 }
257 }
258