4 Copyright (C) 1996 Free Software Foundation, Inc.
6 Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
9 This file is part of the GNUstep Database Library.
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.
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.
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.
26 // $Id: EOModelGroup.m 1 2004-08-20 10:38:46Z znek $
28 #include "EOModelGroup.h"
31 #import <EOControl/EOClassDescription.h>
34 @implementation EOModelGroup
36 static EOModelGroup *defaultGroup = nil;
37 static id<EOModelGroupClassDelegation> classDelegate = nil;
39 + (void)setDefaultGroup:(EOModelGroup *)_group {
40 ASSIGN(defaultGroup, _group);
43 + (EOModelGroup *)defaultGroup {
49 group = [[self classDelegate] defaultModelGroup];
52 group = [self globalModelGroup];
57 + (EOModelGroup *)globalModelGroup {
58 static EOModelGroup *globalModelGroup = nil;
59 NSEnumerator *bundles;
63 return globalModelGroup;
65 globalModelGroup = [[EOModelGroup alloc] init];
67 bundles = [[NSBundle allBundles] objectEnumerator];
68 while ((bundle = [bundles nextObject])) {
72 paths = [[bundle pathsForResourcesOfType:@"eomodel" inDirectory:nil]
75 while ((path = [paths nextObject])) {
78 model = [[EOModel alloc] initWithContentsOfFile:path];
80 NSLog(@"WARNING: couldn't load model %@", path);
83 [globalModelGroup addModel:model];
88 return globalModelGroup;
91 + (void)setClassDelegate:(id<EOModelGroupClassDelegation>)_delegate {
92 ASSIGN(classDelegate, _delegate);
94 + (id<EOModelGroupClassDelegation>)classDelegate {
99 self->nameToModel = [[NSMutableDictionary allocWithZone:[self zone]] init];
104 RELEASE(self->nameToModel);
108 /* instance delegate */
110 - (void)setDelegate:(id<EOModelGroupDelegation>)_delegate {
111 self->delegate = _delegate;
113 - (id<EOModelGroupDelegation>)delegate {
114 return self->delegate;
119 - (void)addModel:(EOModel *)_model {
122 name = [_model name];
123 if (name == nil) name = [[_model path] lastPathComponent];
125 if ([self->nameToModel objectForKey:name]) {
126 [NSException raise:@"NSInvalidArgumentException"
127 format:@"model group %@ already contains model named %@",
131 [self->nameToModel setObject:_model forKey:name];
133 [[NSNotificationCenter defaultCenter]
134 postNotificationName:@"EOModelAddedNotification"
138 - (void)removeModel:(EOModel *)_model {
141 allNames = [self->nameToModel allKeysForObject:_model];
142 [self->nameToModel removeObjectsForKeys:allNames];
144 [[NSNotificationCenter defaultCenter]
145 postNotificationName:@"EOModelInvalidatedNotification"
149 - (EOModel *)addModelWithFile:(NSString *)_path {
152 model = [[EOModel alloc] initWithContentsOfFile:_path];
157 [self addModel:model];
162 - (EOModel *)modelNamed:(NSString *)_name {
163 return [self->nameToModel objectForKey:_name];
165 - (EOModel *)modelWithPath:(NSString *)_path {
170 p = [_path stringByStandardizingPath];
171 if (p == nil) p = _path;
173 e = [self->nameToModel objectEnumerator];
174 while ((m = [e nextObject])) {
177 mp = [[m path] stringByStandardizingPath];
178 if (mp == nil) mp = [m path];
186 - (NSArray *)modelNames {
187 return [self->nameToModel allKeys];
189 - (NSArray *)models {
190 return [self->nameToModel allValues];
193 - (void)loadAllModelObjects {
194 [[self->nameToModel allValues] makeObjectsPerformSelector:_cmd];
199 - (EOEntity *)entityForObject:(id)_object {
203 e = [self->nameToModel objectEnumerator];
204 while ((m = [e nextObject])) {
207 if ((entity = [m entityForObject:_object]))
213 - (EOEntity *)entityNamed:(NSString *)_name {
217 e = [self->nameToModel objectEnumerator];
218 while ((m = [e nextObject])) {
221 if ((entity = [m entityNamed:_name]))
227 - (EOFetchSpecification *)fetchSpecificationNamed:(NSString *)_name
228 entityNamed:(NSString *)_entityName
230 return [[self entityNamed:_entityName] fetchSpecificationNamed:_name];
235 - (NSString *)description {
236 return [NSString stringWithFormat:@"<%@[0x%08X]: models=%@>",
237 NSStringFromClass([self class]),
239 [[self modelNames] componentsJoinedByString:@","]];
242 @end /* EOModelGroup */