Coverage Report - net.wotonomy.test.TestObjectStore
 
Classes in this File Line Coverage Branch Coverage Complexity
TestObjectStore
0% 
0% 
1.538
 
 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  0
     public TestObjectStore( String aPath )
 33  0
     {
 34  0
         soup = new XMLFileSoup( aPath );
 35  0
     }
 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  0
         return objectsForSourceGlobalID( 
 47  0
             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  0
 System.out.println( "TestObjectStore: * reading object * : " + aGlobalID );
 59  0
         return soup.getObjectByKey( 
 60  0
             ((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  
         //TODO: faults are not yet supported
 75  0
         throw new WotonomyException( 
 76  0
             "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  0
 System.out.println( "TestObjectStore: * reading object * : " + aGlobalID );
 92  0
         Object original = soup.getObjectByKey( 
 93  0
             ((DataKeyID)aGlobalID).getKey() );
 94  0
         EOObserverCenter.notifyObserversObjectWillChange( anObject );
 95  0
         Duplicator.deepCopy( original, anObject );
 96  
         //TODO: need to handle child object registration in aContext
 97  0
     }
 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  
         // does nothing        
 107  0
     }
 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  
         // does nothing
 118  0
     }
 119  
         
 120  
     /**
 121  
     * Returns false because locking is not permitted.
 122  
     */
 123  
     public boolean isObjectLockedWithGlobalID ( 
 124  
         EOGlobalID aGlobalID,
 125  
         EOEditingContext aContext )
 126  
     {
 127  0
         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  
         // does nothing   
 138  0
     }
 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  
         //TODO: relationships are not yet supported
 152  0
         throw new WotonomyException( 
 153  0
             "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  
         //TODO: fetch specs are not yet supported
 168  
         
 169  0
         DataView view = soup.queryObjects( null, null );
 170  0
 System.out.println( "TestObjectStore: ** querying all objects **" );
 171  0
         NSMutableArray result = new NSMutableArray();
 172  
         Object o;
 173  
         Object existing;
 174  
         DataKeyID id;
 175  0
         Iterator it = view.iterator();
 176  0
         while ( it.hasNext() )
 177  
         {
 178  0
             o = it.next();
 179  0
             id = new DataKeyID( view.getKeyForObject( o ) );
 180  0
             existing = aContext.objectForGlobalID( id );
 181  0
             if ( existing != null )
 182  
             {
 183  0
                 o = existing;    
 184  0
             }
 185  
             else
 186  
             {
 187  0
                 aContext.recordObject( o, id );
 188  
             }
 189  0
             result.addObject( o );
 190  0
         }
 191  0
         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  
         //TODO: faults are not yet supported
 205  
         // just re-initialize the object
 206  0
         initializeObject( anObject, aGlobalID, aContext );
 207  0
     }
 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  0
         System.out.println( aContext.updatedObjects() );
 221  
         
 222  
         // process updates
 223  0
         it = aContext.updatedObjects().iterator();
 224  0
         while ( it.hasNext() )
 225  
         {
 226  0
             o = it.next();
 227  0
             id = (DataKeyID) aContext.globalIDForObject( o );
 228  0
 System.out.println( "TestObjectStore: * updating object * : " + id );
 229  0
             soup.updateObject( id.getKey(), o );
 230  0
         }
 231  
         
 232  
         // process deletes
 233  0
         it = aContext.deletedObjects().iterator();
 234  0
         while ( it.hasNext() )
 235  
         {
 236  0
             o = it.next();
 237  0
             id = (DataKeyID) aContext.globalIDForObject( o );
 238  0
 System.out.println( "TestObjectStore: * deleting object * : " + id );
 239  0
             soup.removeObject( id.getKey() );
 240  
             // remove object from editing context
 241  0
             aContext.forgetObject( o );
 242  0
         }
 243  
         
 244  
         // process inserts
 245  0
         it = aContext.insertedObjects().iterator();
 246  0
         while ( it.hasNext() )
 247  
         {   
 248  0
             o = it.next();
 249  0
             id = new DataKeyID( soup.addObject( o ) );
 250  0
 System.out.println( "TestObjectStore: * adding object * : " + id );
 251  
             // register object in editing context with new id
 252  0
             aContext.forgetObject( o );
 253  0
             aContext.recordObject( o, id );
 254  0
         }
 255  
         
 256  0
     }
 257  
 }
 258