|
|
1.1 root 1: /*--------------------------------------------------------------------------
2: *
3: * You may freely copy, distribute, and reuse the code in this example.
4: * SHL Systemhouse disclaims any warranty of any kind, expressed or
5: * implied, as to its fitness for any particular use.
6: *
7: *
8: * DataSource
9: *
10: * Inherits From: NSObject
11: *
12: * Conforms To: EOQualifiedDataSources, EORollbackDataSources
13: *
14: * Declared In: DataSource.h
15: *
16: *
17: *------------------------------------------------------------------------*/
18: #import "DataSource.h"
19: #import "Person.h"
20: #import <foundation/NSUtilities.h>
21: #import <appkit/Application.h>
22:
23:
24:
25:
26: @implementation DataSource
27:
28: /*--------------------------------------------------------------------------
29: * EODataSources Protocol
30: *
31: * The EODataSources protocol defines the interface for a source of
32: * data-bearing objects retrieved from some external store, such as an RDBMS.
33: * EODataSources uses a simple insert/delete/update/fetch model.
34: *
35: * Changes to the objects provided by a data source are made in two phases.
36: * First, you can modify an object independently of the data source. These
37: * changes don't affect the external store until you send an -insertObject:,
38: * -deleteObject:, or -updateObject: message. For example, if you release an
39: * object you received from the data source, it isn't deleted from the
40: * external store. Invoking one of the messages listed sends the changes
41: * associated with the object to the external store. You must invoke
42: * -saveObjects to make your changes permanent. If an external store supports
43: * rolling back of changes you can invoke -rollback (declared in the
44: * EORollbackDataSources protocol) to undo the changes made since the last
45: * -saveObjects message.
46: *
47: *------------------------------------------------------------------------*/
48: - (NSArray *) keys
49: {
50: // Returns the names of the keys that describe the data-bearing objects.
51: return [NSArray arrayWithObject: @"Model"];
52: }
53:
54:
55: - createObject
56: {
57: // Returns a new data bearing object with no values set, or nil if the
58: // data source won't allow object insertion. You're responsible for
59: // assigning a proper primary key.
60:
61: return [[Person alloc] init];
62: }
63:
64: - coerceValue: value forKey: (NSString *)key
65: {
66: return value;
67: }
68:
69: - (BOOL) insertObject: object
70: {
71: // Inserts object into the data source. Returns YES on success, NO on
72: // failure for any reason.
73:
74: if ([object isKindOf: [Person class]] == NO) return NO;
75:
76: if (workingStore == nil)
77: workingStore = [[NSMutableArray allocWithZone: [self zone]]
78: initWithCapacity: 1];
79:
80: [workingStore addObject: object];
81: return YES;
82: }
83:
84:
85: - (BOOL) deleteObject: object
86: {
87: // Deletes object from the data source. Returns YES on success, NO on
88: // failure for any reason.
89:
90: if ([object isKindOf: [Person class]] == NO) return NO;
91: if (workingStore == nil) return NO;
92:
93: [workingStore removeObject: object];
94: return YES;
95: }
96:
97:
98: - (BOOL) updateObject: object
99: {
100: // Saves changes to object to the data source. Returns YES on success,
101: // NO on failure for any reason.
102:
103: return YES;
104: }
105:
106:
107: - (NSArray *) fetchObjects
108: {
109: // Returns an array of the data-bearing objects in the data source.
110:
111: return persistentStore;
112: }
113:
114:
115: - (BOOL) saveObjects
116: {
117: // Saves objects to persistent storage, if needed. Returns YES on
118: // success, NO on failure for any reason.
119:
120: if (persistentStore == nil)
121: persistentStore = [[NSMutableArray allocWithZone: [self zone]]
122: initWithCapacity: [workingStore count]];
123:
124: [persistentStore removeAllObjects];
125: [persistentStore addObjectsFromArray: workingStore];
126: return YES;
127: }
128:
129:
130: - (BOOL) canDelete
131: {
132: // Returns YES if the data source allows objects to be deleted, NO if it
133: // doesn't.
134:
135: return YES;
136: }
137:
138:
139: /*--------------------------------------------------------------------------
140: * EOQualifiedDataSources Protocol
141: *
142: * The EOQualifiedDataSources protocol decalres methods that must be
143: * implemented by data sources that provide sub data sources rooted in the
144: * super data source.
145: *
146: *------------------------------------------------------------------------*/
147: - (NSArray *) keysForPath: (NSString *)aPath
148: {
149: // The path supplied is a concatenation of all the keys used to get to
150: // a detail datasource with each key separated by a '.'; for example,
151: // "toAuthor.toPublisher.address".
152:
153: return nil;
154: }
155:
156:
157: - (id <EODataSources>) dataSourceQualifiedByKey:(NSString *)key ofObject:object
158: {
159: // Returns a data source that supplies objects associated with object's
160: // key. This can be used for creating master-detail data sources.
161:
162:
163: return nil;
164: }
165:
166:
167: /*--------------------------------------------------------------------------
168: * EORollbackDataSources Protocol
169: *------------------------------------------------------------------------*/
170: - (void) rollback
171: {
172: // Reverses any changes made by -insertObject:, -deleteObject:, or
173: // -updateObject: since the data source was last sent a -saveObjects
174: // message.
175:
176:
177: [workingStore removeAllObjects];
178: [workingStore addObjectsFromArray: persistentStore];
179: }
180:
181:
182: @end
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.