View Javadoc

1   package net.wotonomy.test;
2   
3   import net.wotonomy.control.EOClassDescription;
4   import net.wotonomy.control.EODataSource;
5   import net.wotonomy.control.EOEditingContext;
6   import net.wotonomy.control.EOFetchSpecification;
7   import net.wotonomy.foundation.NSArray;
8   
9   /***
10  * A custom DataSource that works with 
11  * the datastore package for persistence.
12  */
13  public class TestDataSource extends EODataSource
14  {
15      private EOEditingContext context;
16      private Object source;
17      private String key;
18      
19  	public TestDataSource()
20  	{
21          this( Test.editingContext );
22  	}
23      
24  	public TestDataSource( EOEditingContext aContext )
25  	{
26          context = aContext;
27  	}
28      
29      public EOEditingContext editingContext()
30      {
31          return context;
32      }
33  	
34      /***
35      * This implementation does nothing.
36      */
37      public void insertObject ( Object anObject )
38  	{
39          // creates are handled by createObject().
40  	}
41  
42      /***
43      * Deletes the specified object from this data source.
44      */
45      public void deleteObject ( Object anObject )
46  	{
47          editingContext().deleteObject( anObject );
48  	}
49  
50      /***
51      * Returns a List containing the objects in this
52      * data source.  This implementation returns all
53  	* TestObjects that have been persisted to the 
54  	* datastore in the data directory.
55      */
56      public NSArray fetchObjects ()
57      {
58          if ( source == null )
59          {
60              NSArray result = editingContext().objectsWithFetchSpecification( 
61                  new EOFetchSpecification() );
62              if ( result.size() > 0 )
63              {
64                  result = new NSArray( result.objectAtIndex( 0 ) );
65  //result.add( result.objectAtIndex( 0 ) );
66              }
67              return result;
68          }
69          else
70          {
71              return new NSArray( 
72                  ((TestObject)source).getChildList() );
73          }
74      }
75  
76      /***
77      * Returns a data source that is capable of
78      * manipulating objects of the type returned by 
79      * applying the specified key to objects 
80      * vended by this data source.
81      * @see #qualifyWithRelationshipKey
82      */
83      public EODataSource 
84          dataSourceQualifiedByKey ( String aKey )
85  	{
86  		return new TestDataSource( editingContext() );	
87  	}
88  
89      /***
90      * Restricts this data source to vend those 
91      * objects that are associated with the specified 
92      * key on the specified object.
93      */
94      public void 
95          qualifyWithRelationshipKey ( 
96          String aKey, Object anObject )
97  	{
98          key = aKey;
99          source = anObject;
100 	}
101 
102     /***
103     * Returns the description of the class of the
104     * objects that is vended by this data source,
105     * or null if this cannot be determined.
106 	* This implementation returns TestObject.
107     */
108     public EOClassDescription 
109         classDescriptionForObjects ()
110     {
111         return EOClassDescription.classDescriptionForClass( 
112                         TestObject.class );
113     }
114 
115 }