]> err.no Git - sope/blob - sope-core/EOControl/EODetailDataSource.m
Drop apache 1 build-dependency
[sope] / sope-core / EOControl / EODetailDataSource.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include <EOControl/EODetailDataSource.h>
23 #include <EOControl/EOClassDescription.h>
24 #include <EOControl/EOKeyValueCoding.h>
25 #include "common.h"
26
27 @implementation EODetailDataSource
28
29 - (id)initWithMasterClassDescription:(EOClassDescription *)_cd
30   detailKey:(NSString *)_relKey
31 {
32   if ((self = [super init])) {
33     self->masterClassDescription = [_cd retain];
34
35     [self qualifyWithRelationshipKey:_relKey ofObject:nil];
36   }
37   return self;
38 }
39
40 - (id)initWithMasterDataSource:(EODataSource *)_ds
41   detailKey:(NSString *)_relKey
42 {
43   if ((self = [self initWithMasterClassDescription:nil detailKey:_relKey])) {
44     self->masterDataSource = [_ds retain];
45   }
46   return self;
47 }
48
49 - (id)init {
50   return [self initWithMasterClassDescription:nil detailKey:nil];
51 }
52
53 - (void)dealloc {
54   [self->detailKey              release];
55   [self->masterObject           release];
56   [self->masterClassDescription release];
57   [self->masterDataSource       release];
58   [super dealloc];
59 }
60
61 /* reflection */
62
63 - (void)setMasterClassDescription:(EOClassDescription *)_cd {
64   ASSIGN(self->masterClassDescription, _cd);
65 }
66 - (EOClassDescription *)masterClassDescription {
67   return self->masterClassDescription;
68 }
69
70 - (EODataSource *)masterDataSource {
71   return self->masterDataSource;
72 }
73
74 /* editing context */
75
76 - (id)editingContext {
77   return [[self masterObject] editingContext];
78 }
79
80 /* master-detail */
81
82 - (id)masterObject {
83   return self->masterObject;
84 }
85 - (NSString *)detailKey {
86   return self->detailKey;
87 }
88
89 - (void)qualifyWithRelationshipKey:(NSString *)_relKey ofObject:(id)_object {
90   id tmp;
91
92   tmp = self->detailKey;
93   self->detailKey = [_relKey copy];
94   [tmp release];
95
96   ASSIGN(self->masterObject, _object);
97 }
98
99 /* operations */
100
101 - (NSArray *)fetchObjects {
102   id       eo;
103   NSString *dk;
104   
105   if ((eo = [self masterObject]) == nil)
106     return [NSArray array];
107
108   if ((dk = [self detailKey]) == nil)
109     return [NSArray arrayWithObject:eo];
110
111   return [eo valueForKey:dk];
112 }
113
114 - (void)insertObject:(id)_object {
115   id       eo;
116   NSString *dk;
117   
118   if ((eo = [self masterObject]) == nil) {
119     [NSException raise:@"NSInternalInconsistencyException"
120                  format:
121                    @"detail datasource %@ has no master object set "
122                    @"for insertion of object %@",
123                    self, _object];
124   }
125   if ((dk = [self detailKey]) == nil) {
126     [NSException raise:@"NSInternalInconsistencyException"
127                  format:
128                    @"detail datasource %@ has no detail key set "
129                    @"for insertion of object %@ into master %@",
130                    self, _object, eo];
131   }
132   
133   [eo addObject:_object toBothSidesOfRelationshipWithKey:dk];
134 }
135
136 - (void)deleteObject:(id)_object {
137   id       eo;
138   NSString *dk;
139   
140   if ((eo = [self masterObject]) == nil) {
141     [NSException raise:@"NSInternalInconsistencyException"
142                  format:
143                    @"detail datasource %@ has no master object set "
144                    @"for deletion of object %@",
145                    self, _object];
146   }
147   if ((dk = [self detailKey]) == nil) {
148     [NSException raise:@"NSInternalInconsistencyException"
149                  format:
150                    @"detail datasource %@ has no detail key set "
151                    @"for deletion of object %@ from master %@",
152                    self, _object, eo];
153   }
154   
155   [eo removeObject:_object fromPropertyWithKey:dk];
156 }
157
158 /* key/value archiving */
159
160 - (id)initWithKeyValueUnarchiver:(EOKeyValueUnarchiver *)_unarchiver {
161   /*
162       dataSource = {
163         class                  = EODetailDataSource; 
164         detailKey              = roles; 
165         masterClassDescription = Movie; 
166       }; 
167   */
168   if ((self = [super init])) {
169     NSString *ename;
170     
171     self->detailKey = [[_unarchiver decodeObjectForKey:@"detailKey"] copy];
172     
173     ename = [_unarchiver decodeObjectForKey:@"masterClassDescription"];
174     NSLog(@"TODO(%s): set class description: %@", 
175           __PRETTY_FUNCTION__, ename);
176   }
177   return self;
178 }
179 - (void)encodeWithKeyValueArchiver:(EOKeyValueArchiver *)_archiver {
180   [_archiver encodeObject:[self detailKey] forKey:@"detailKey"];
181   [_archiver encodeObject:[[self masterClassDescription] entityName]
182              forKey:@"masterClassDescription"];
183 }
184
185 @end /* EODetailDataSource */