~ubuntu-branches/ubuntu/edgy/sope/edgy

« back to all changes in this revision

Viewing changes to sope-gdl1/GDLAccess/EOModelGroup.m

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Ley
  • Date: 2005-08-19 16:53:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050819165331-hs683wz1osm708pw
Tags: upstream-4.4rc.2
ImportĀ upstreamĀ versionĀ 4.4rc.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
27
#include "EOModelGroup.h"
 
28
#include "EOModel.h"
 
29
#include "EOEntity.h"
 
30
#import <EOControl/EOClassDescription.h>
 
31
#include "common.h"
 
32
 
 
33
@implementation EOModelGroup
 
34
 
 
35
static EOModelGroup *defaultGroup = nil;
 
36
static id<EOModelGroupClassDelegation> classDelegate = nil;
 
37
 
 
38
+ (void)setDefaultGroup:(EOModelGroup *)_group {
 
39
  ASSIGN(defaultGroup, _group);
 
40
}
 
41
 
 
42
+ (EOModelGroup *)defaultGroup {
 
43
  EOModelGroup *group;
 
44
 
 
45
  group = defaultGroup;
 
46
  
 
47
  if (group == nil)
 
48
    group = [[self classDelegate] defaultModelGroup];
 
49
  
 
50
  if (group == nil)
 
51
    group = [self globalModelGroup];
 
52
  
 
53
  return group;
 
54
}
 
55
 
 
56
+ (EOModelGroup *)globalModelGroup {
 
57
  static EOModelGroup *globalModelGroup = nil;
 
58
  NSEnumerator *bundles;
 
59
  NSBundle *bundle;
 
60
 
 
61
  if (globalModelGroup)
 
62
    return globalModelGroup;
 
63
 
 
64
  globalModelGroup = [[EOModelGroup alloc] init];
 
65
 
 
66
  bundles = [[NSBundle allBundles] objectEnumerator];
 
67
  while ((bundle = [bundles nextObject])) {
 
68
    NSEnumerator *paths;
 
69
    NSString *path;
 
70
 
 
71
    paths = [[bundle pathsForResourcesOfType:@"eomodel" inDirectory:nil]
 
72
                     objectEnumerator];
 
73
 
 
74
    while ((path = [paths nextObject])) {
 
75
      EOModel *model;
 
76
      
 
77
      model = [[EOModel alloc] initWithContentsOfFile:path];
 
78
      if (model == nil) {
 
79
        NSLog(@"WARNING: couldn't load model %@", path);
 
80
      }
 
81
      else {
 
82
        [globalModelGroup addModel:model];
 
83
        RELEASE(model);
 
84
      }
 
85
    }
 
86
  }
 
87
  return globalModelGroup;
 
88
}
 
89
 
 
90
+ (void)setClassDelegate:(id<EOModelGroupClassDelegation>)_delegate {
 
91
  ASSIGN(classDelegate, _delegate);
 
92
}
 
93
+ (id<EOModelGroupClassDelegation>)classDelegate {
 
94
  return classDelegate;
 
95
}
 
96
 
 
97
- (id)init {
 
98
  self->nameToModel = [[NSMutableDictionary allocWithZone:[self zone]] init];
 
99
  return self;
 
100
}
 
101
 
 
102
- (void)dealloc {
 
103
  RELEASE(self->nameToModel);
 
104
  [super dealloc];
 
105
}
 
106
 
 
107
/* instance delegate */
 
108
 
 
109
- (void)setDelegate:(id<EOModelGroupDelegation>)_delegate {
 
110
  self->delegate = _delegate;
 
111
}
 
112
- (id<EOModelGroupDelegation>)delegate {
 
113
  return self->delegate;
 
114
}
 
115
 
 
116
/* accessors */
 
117
 
 
118
- (void)addModel:(EOModel *)_model {
 
119
  NSString *name;
 
120
 
 
121
  name = [_model name];
 
122
  if (name == nil) name = [[_model path] lastPathComponent];
 
123
  
 
124
  if ([self->nameToModel objectForKey:name]) {
 
125
    [NSException raise:@"NSInvalidArgumentException"
 
126
                 format:@"model group %@ already contains model named %@",
 
127
                   self, name];
 
128
  }
 
129
  
 
130
  [self->nameToModel setObject:_model forKey:name];
 
131
 
 
132
  [[NSNotificationCenter defaultCenter]
 
133
                         postNotificationName:@"EOModelAddedNotification"
 
134
                         object:_model];
 
135
}
 
136
 
 
137
- (void)removeModel:(EOModel *)_model {
 
138
  NSArray *allNames;
 
139
 
 
140
  allNames = [self->nameToModel allKeysForObject:_model];
 
141
  [self->nameToModel removeObjectsForKeys:allNames];
 
142
 
 
143
  [[NSNotificationCenter defaultCenter]
 
144
                         postNotificationName:@"EOModelInvalidatedNotification"
 
145
                         object:_model];
 
146
}
 
147
 
 
148
- (EOModel *)addModelWithFile:(NSString *)_path {
 
149
  EOModel *model;
 
150
 
 
151
  model = [[EOModel alloc] initWithContentsOfFile:_path];
 
152
  if (model == nil)
 
153
    return nil;
 
154
  AUTORELEASE(model);
 
155
 
 
156
  [self addModel:model];
 
157
 
 
158
  return model;
 
159
}
 
160
 
 
161
- (EOModel *)modelNamed:(NSString *)_name {
 
162
  return [self->nameToModel objectForKey:_name];
 
163
}
 
164
- (EOModel *)modelWithPath:(NSString *)_path {
 
165
  NSEnumerator *e;
 
166
  EOModel  *m;
 
167
  NSString *p;
 
168
 
 
169
  p = [_path stringByStandardizingPath];
 
170
  if (p == nil) p = _path;
 
171
 
 
172
  e = [self->nameToModel objectEnumerator];
 
173
  while ((m = [e nextObject])) {
 
174
    NSString *mp;
 
175
 
 
176
    mp = [[m path] stringByStandardizingPath];
 
177
    if (mp == nil) mp = [m path];
 
178
 
 
179
    if ([p isEqual:mp])
 
180
      return m;
 
181
  }
 
182
  return m;
 
183
}
 
184
 
 
185
- (NSArray *)modelNames {
 
186
  return [self->nameToModel allKeys];
 
187
}
 
188
- (NSArray *)models {
 
189
  return [self->nameToModel allValues];
 
190
}
 
191
 
 
192
- (void)loadAllModelObjects {
 
193
  [[self->nameToModel allValues] makeObjectsPerformSelector:_cmd];
 
194
}
 
195
 
 
196
/* entities */
 
197
 
 
198
- (EOEntity *)entityForObject:(id)_object {
 
199
  NSEnumerator *e;
 
200
  EOModel  *m;
 
201
 
 
202
  e = [self->nameToModel objectEnumerator];
 
203
  while ((m = [e nextObject])) {
 
204
    EOEntity *entity;
 
205
 
 
206
    if ((entity = [m entityForObject:_object]))
 
207
      return entity;
 
208
  }
 
209
  return nil;
 
210
}
 
211
 
 
212
- (EOEntity *)entityNamed:(NSString *)_name {
 
213
  NSEnumerator *e;
 
214
  EOModel  *m;
 
215
 
 
216
  e = [self->nameToModel objectEnumerator];
 
217
  while ((m = [e nextObject])) {
 
218
    EOEntity *entity;
 
219
 
 
220
    if ((entity = [m entityNamed:_name]))
 
221
      return entity;
 
222
  }
 
223
  return nil;
 
224
}
 
225
 
 
226
- (EOFetchSpecification *)fetchSpecificationNamed:(NSString *)_name
 
227
  entityNamed:(NSString *)_entityName
 
228
{
 
229
  return [[self entityNamed:_entityName] fetchSpecificationNamed:_name];
 
230
}
 
231
 
 
232
/* description */
 
233
 
 
234
- (NSString *)description {
 
235
  return [NSString stringWithFormat:@"<%@[0x%08X]: models=%@>",
 
236
                     NSStringFromClass([self class]),
 
237
                     self,
 
238
                     [[self modelNames] componentsJoinedByString:@","]];
 
239
}
 
240
 
 
241
@end /* EOModelGroup */