]> err.no Git - sope/blob - sope-gdl1/GDLAccess/EOModelGroup.m
removed subminor
[sope] / sope-gdl1 / GDLAccess / EOModelGroup.m
1 /* 
2    EOAdaptorChannel.m
3
4    Copyright (C) 1996 Free Software Foundation, Inc.
5
6    Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
7    Date: October 1996
8
9    This file is part of the GNUstep Database Library.
10
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Library General Public
13    License as published by the Free Software Foundation; either
14    version 2 of the License, or (at your option) any later version.
15
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Library General Public License for more details.
20
21    You should have received a copy of the GNU Library General Public
22    License along with this library; see the file COPYING.LIB.
23    If not, write to the Free Software Foundation,
24    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26 // $Id: EOModelGroup.m 1 2004-08-20 10:38:46Z znek $
27
28 #include "EOModelGroup.h"
29 #include "EOModel.h"
30 #include "EOEntity.h"
31 #import <EOControl/EOClassDescription.h>
32 #include "common.h"
33
34 @implementation EOModelGroup
35
36 static EOModelGroup *defaultGroup = nil;
37 static id<EOModelGroupClassDelegation> classDelegate = nil;
38
39 + (void)setDefaultGroup:(EOModelGroup *)_group {
40   ASSIGN(defaultGroup, _group);
41 }
42
43 + (EOModelGroup *)defaultGroup {
44   EOModelGroup *group;
45
46   group = defaultGroup;
47   
48   if (group == nil)
49     group = [[self classDelegate] defaultModelGroup];
50   
51   if (group == nil)
52     group = [self globalModelGroup];
53   
54   return group;
55 }
56
57 + (EOModelGroup *)globalModelGroup {
58   static EOModelGroup *globalModelGroup = nil;
59   NSEnumerator *bundles;
60   NSBundle *bundle;
61
62   if (globalModelGroup)
63     return globalModelGroup;
64
65   globalModelGroup = [[EOModelGroup alloc] init];
66
67   bundles = [[NSBundle allBundles] objectEnumerator];
68   while ((bundle = [bundles nextObject])) {
69     NSEnumerator *paths;
70     NSString *path;
71
72     paths = [[bundle pathsForResourcesOfType:@"eomodel" inDirectory:nil]
73                      objectEnumerator];
74
75     while ((path = [paths nextObject])) {
76       EOModel *model;
77       
78       model = [[EOModel alloc] initWithContentsOfFile:path];
79       if (model == nil) {
80         NSLog(@"WARNING: couldn't load model %@", path);
81       }
82       else {
83         [globalModelGroup addModel:model];
84         RELEASE(model);
85       }
86     }
87   }
88   return globalModelGroup;
89 }
90
91 + (void)setClassDelegate:(id<EOModelGroupClassDelegation>)_delegate {
92   ASSIGN(classDelegate, _delegate);
93 }
94 + (id<EOModelGroupClassDelegation>)classDelegate {
95   return classDelegate;
96 }
97
98 - (id)init {
99   self->nameToModel = [[NSMutableDictionary allocWithZone:[self zone]] init];
100   return self;
101 }
102
103 - (void)dealloc {
104   RELEASE(self->nameToModel);
105   [super dealloc];
106 }
107
108 /* instance delegate */
109
110 - (void)setDelegate:(id<EOModelGroupDelegation>)_delegate {
111   self->delegate = _delegate;
112 }
113 - (id<EOModelGroupDelegation>)delegate {
114   return self->delegate;
115 }
116
117 /* accessors */
118
119 - (void)addModel:(EOModel *)_model {
120   NSString *name;
121
122   name = [_model name];
123   if (name == nil) name = [[_model path] lastPathComponent];
124   
125   if ([self->nameToModel objectForKey:name]) {
126     [NSException raise:@"NSInvalidArgumentException"
127                  format:@"model group %@ already contains model named %@",
128                    self, name];
129   }
130   
131   [self->nameToModel setObject:_model forKey:name];
132
133   [[NSNotificationCenter defaultCenter]
134                          postNotificationName:@"EOModelAddedNotification"
135                          object:_model];
136 }
137
138 - (void)removeModel:(EOModel *)_model {
139   NSArray *allNames;
140
141   allNames = [self->nameToModel allKeysForObject:_model];
142   [self->nameToModel removeObjectsForKeys:allNames];
143
144   [[NSNotificationCenter defaultCenter]
145                          postNotificationName:@"EOModelInvalidatedNotification"
146                          object:_model];
147 }
148
149 - (EOModel *)addModelWithFile:(NSString *)_path {
150   EOModel *model;
151
152   model = [[EOModel alloc] initWithContentsOfFile:_path];
153   if (model == nil)
154     return nil;
155   AUTORELEASE(model);
156
157   [self addModel:model];
158
159   return model;
160 }
161
162 - (EOModel *)modelNamed:(NSString *)_name {
163   return [self->nameToModel objectForKey:_name];
164 }
165 - (EOModel *)modelWithPath:(NSString *)_path {
166   NSEnumerator *e;
167   EOModel  *m;
168   NSString *p;
169
170   p = [_path stringByStandardizingPath];
171   if (p == nil) p = _path;
172
173   e = [self->nameToModel objectEnumerator];
174   while ((m = [e nextObject])) {
175     NSString *mp;
176
177     mp = [[m path] stringByStandardizingPath];
178     if (mp == nil) mp = [m path];
179
180     if ([p isEqual:mp])
181       return m;
182   }
183   return m;
184 }
185
186 - (NSArray *)modelNames {
187   return [self->nameToModel allKeys];
188 }
189 - (NSArray *)models {
190   return [self->nameToModel allValues];
191 }
192
193 - (void)loadAllModelObjects {
194   [[self->nameToModel allValues] makeObjectsPerformSelector:_cmd];
195 }
196
197 /* entities */
198
199 - (EOEntity *)entityForObject:(id)_object {
200   NSEnumerator *e;
201   EOModel  *m;
202
203   e = [self->nameToModel objectEnumerator];
204   while ((m = [e nextObject])) {
205     EOEntity *entity;
206
207     if ((entity = [m entityForObject:_object]))
208       return entity;
209   }
210   return nil;
211 }
212
213 - (EOEntity *)entityNamed:(NSString *)_name {
214   NSEnumerator *e;
215   EOModel  *m;
216
217   e = [self->nameToModel objectEnumerator];
218   while ((m = [e nextObject])) {
219     EOEntity *entity;
220
221     if ((entity = [m entityNamed:_name]))
222       return entity;
223   }
224   return nil;
225 }
226
227 - (EOFetchSpecification *)fetchSpecificationNamed:(NSString *)_name
228   entityNamed:(NSString *)_entityName
229 {
230   return [[self entityNamed:_entityName] fetchSpecificationNamed:_name];
231 }
232
233 /* description */
234
235 - (NSString *)description {
236   return [NSString stringWithFormat:@"<%@[0x%08X]: models=%@>",
237                      NSStringFromClass([self class]),
238                      self,
239                      [[self modelNames] componentsJoinedByString:@","]];
240 }
241
242 @end /* EOModelGroup */