]> err.no Git - sope/blob - sope-core/EOControl/EODetailDataSource.m
renamed packages as discussed in the developer list
[sope] / sope-core / EOControl / EODetailDataSource.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23 #include <EOControl/EODetailDataSource.h>
24 #include <EOControl/EOClassDescription.h>
25 #include <EOControl/EOKeyValueCoding.h>
26 #include "common.h"
27
28 @implementation EODetailDataSource
29
30 - (id)initWithMasterClassDescription:(EOClassDescription *)_cd
31   detailKey:(NSString *)_relKey
32 {
33   if ((self = [super init])) {
34     self->masterClassDescription = [_cd retain];
35
36     [self qualifyWithRelationshipKey:_relKey ofObject:nil];
37   }
38   return self;
39 }
40
41 - (id)initWithMasterDataSource:(EODataSource *)_ds
42   detailKey:(NSString *)_relKey
43 {
44   if ((self = [self initWithMasterClassDescription:nil detailKey:_relKey])) {
45     self->masterDataSource = [_ds retain];
46   }
47   return self;
48 }
49
50 - (id)init {
51   return [self initWithMasterClassDescription:nil detailKey:nil];
52 }
53
54 - (void)dealloc {
55   [self->detailKey              release];
56   [self->masterObject           release];
57   [self->masterClassDescription release];
58   [self->masterDataSource       release];
59   [super dealloc];
60 }
61
62 /* reflection */
63
64 - (void)setMasterClassDescription:(EOClassDescription *)_cd {
65   ASSIGN(self->masterClassDescription, _cd);
66 }
67 - (EOClassDescription *)masterClassDescription {
68   return self->masterClassDescription;
69 }
70
71 - (EODataSource *)masterDataSource {
72   return self->masterDataSource;
73 }
74
75 /* editing context */
76
77 - (id)editingContext {
78   return [[self masterObject] editingContext];
79 }
80
81 /* master-detail */
82
83 - (id)masterObject {
84   return self->masterObject;
85 }
86 - (NSString *)detailKey {
87   return self->detailKey;
88 }
89
90 - (void)qualifyWithRelationshipKey:(NSString *)_relKey ofObject:(id)_object {
91   id tmp;
92
93   tmp = self->detailKey;
94   self->detailKey = [_relKey copy];
95   [tmp release];
96
97   ASSIGN(self->masterObject, _object);
98 }
99
100 /* operations */
101
102 - (NSArray *)fetchObjects {
103   id       eo;
104   NSString *dk;
105   
106   if ((eo = [self masterObject]) == nil)
107     return [NSArray array];
108
109   if ((dk = [self detailKey]) == nil)
110     return [NSArray arrayWithObject:eo];
111
112   return [eo valueForKey:dk];
113 }
114
115 - (void)insertObject:(id)_object {
116   id       eo;
117   NSString *dk;
118   
119   if ((eo = [self masterObject]) == nil) {
120     [NSException raise:@"NSInternalInconsistencyException"
121                  format:
122                    @"detail datasource %@ has no master object set "
123                    @"for insertion of object %@",
124                    self, _object];
125   }
126   if ((dk = [self detailKey]) == nil) {
127     [NSException raise:@"NSInternalInconsistencyException"
128                  format:
129                    @"detail datasource %@ has no detail key set "
130                    @"for insertion of object %@ into master %@",
131                    self, _object, eo];
132   }
133   
134   [eo addObject:_object toBothSidesOfRelationshipWithKey:dk];
135 }
136
137 - (void)deleteObject:(id)_object {
138   id       eo;
139   NSString *dk;
140   
141   if ((eo = [self masterObject]) == nil) {
142     [NSException raise:@"NSInternalInconsistencyException"
143                  format:
144                    @"detail datasource %@ has no master object set "
145                    @"for deletion of object %@",
146                    self, _object];
147   }
148   if ((dk = [self detailKey]) == nil) {
149     [NSException raise:@"NSInternalInconsistencyException"
150                  format:
151                    @"detail datasource %@ has no detail key set "
152                    @"for deletion of object %@ from master %@",
153                    self, _object, eo];
154   }
155   
156   [eo removeObject:_object fromPropertyWithKey:dk];
157 }
158
159 @end /* EODetailDataSource */