]> err.no Git - sope/blob - sope-gdl1/GDLAccess/EOEntity+Factory.m
fixed bug in GDL adaptor lookup
[sope] / sope-gdl1 / GDLAccess / EOEntity+Factory.m
1 /* 
2    EOAttributeOrdering.m
3
4    Copyright (C) 1996 Free Software Foundation, Inc.
5
6    Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
7    Date: 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 // $Id: EOEntity+Factory.m 1 2004-08-20 10:38:46Z znek $
28
29 #include <GDLAccess/EOEntity+Factory.h>
30 #include <GDLAccess/EOAttribute.h>
31 #include <EOControl/EONull.h>
32 #include <EOControl/EOKeyValueCoding.h>
33 #include "common.h"
34
35 @interface NSObject(PKeyInitializer)
36 - (id)initWithPrimaryKey:(NSDictionary *)_pkey entity:(EOEntity *)_entity;
37 @end
38
39 @implementation EOEntity(AttributeNames)
40
41 - (NSArray *)attributeNames {
42   NSMutableArray *attrNames = [[[NSMutableArray alloc] init] autorelease];
43   NSEnumerator   *attrs     = [self->attributes objectEnumerator];
44   EOAttribute    *attr      = nil;
45
46   while ((attr = [attrs nextObject])) 
47     [attrNames addObject:[attr name]];
48
49   return attrNames;
50 }
51
52 @end /* EOEntity(AttributeNames) */
53
54 @implementation EOEntity(PrimaryKeys)
55
56 - (BOOL)isPrimaryKeyAttribute:(EOAttribute *)_attribute {
57   NSEnumerator *pkeys = [self->primaryKeyAttributeNames objectEnumerator];
58   NSString     *aname = [_attribute name];
59   NSString     *n     = nil;
60
61   while ((n = [pkeys nextObject])) {
62     if ([aname isEqualToString:n])
63       return YES;
64   }
65   return NO;
66 }
67
68 - (unsigned)primaryKeyCount {
69   return [self->primaryKeyAttributeNames count];
70 }
71
72 @end /* EOEntity(PrimaryKeys) */
73
74 @implementation EOEntity(ObjectFactory)
75
76 - (id)produceNewObjectWithPrimaryKey:(NSDictionary *)_key {
77   /* Note: used by LSDBObjectNewCommand */
78   Class objectClass = Nil;
79   id    obj;
80
81   objectClass = NSClassFromString([self className]);
82   NSAssert(objectClass != nil, @"no enterprise object class set in entity");
83
84   obj = [objectClass alloc];
85   NSAssert(objectClass != nil, @"could not allocate enterprise object");
86   
87   if ([obj respondsToSelector:@selector(initWithPrimaryKey:entity:)])
88     [obj initWithPrimaryKey:_key entity:self];
89   else
90     [obj init];
91   
92   return AUTORELEASE(obj);
93 }
94
95 - (void)setAttributesOfObjectToEONull:(id)_object {
96   static EONull *null = nil;
97   NSEnumerator *attrs;
98   EOAttribute  *attr;
99   int          pkeyCount;
100   
101   if (null == nil)
102     null = [[NSNull null] retain];
103
104   attrs     = [self->attributes objectEnumerator];
105   attr      = nil;
106   pkeyCount = [self->primaryKeyAttributeNames count];
107   
108   NSAssert(NSClassFromString([self className]) == [_object class],
109            @"object does not belong to entity");
110   
111   while ((attr = [attrs nextObject])) {
112     if (pkeyCount > 0) {
113       if ([self isPrimaryKeyAttribute:attr]) {
114         pkeyCount--;
115         continue;
116       }
117     }
118     
119     [_object takeValue:null forKey:[attr name]];
120   }
121 }
122
123 @end /* EOEntity(ObjectFactory) */