GNUstep Core Data  0.1
NSRelationshipDescription.m
1 /* Implementation of the NSRelationshipDescription class for the GNUstep
2  Core Data framework.
3  Copyright (C) 2005 Free Software Foundation, Inc.
4 
5  Written by: Saso Kiselkov <diablos@manga.sk>
6  Date: August 2005
7 
8  This file is part of the GNUstep Core Data framework.
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Lesser General Public
12  License as published by the Free Software Foundation; either
13  version 2.1 of the License, or (at your option) any later version.
14 
15  This library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public
21  License along with this library; if not, write to the Free
22  Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
23  */
24 
25 #import "CoreDataHeaders.h"
26 
27 @implementation NSRelationshipDescription
28 
29 - (void) dealloc
30 {
31  TEST_RELEASE(_destinationEntity);
32 
33  [super dealloc];
34 }
35 
36 - (NSEntityDescription *) destinationEntity
37 {
38  return _destinationEntity;
39 }
40 
41 - (void) setDestinationEntity: (NSEntityDescription *) entity
42 {
43  [self _ensureEditableWithReason: _(@"Tried to set the destination "
44  @"entity of a relationship "
45  @"already in use")];
46  ASSIGN(_destinationEntity, entity);
47 
48  // destroy the inverse relationship - we'll have to set it up anew
49  if (_inverseRelationship != nil)
50  [_inverseRelationship setInverseRelationship: nil];
51  [self setInverseRelationship: nil];
52 }
53 
54 - (NSRelationshipDescription *) inverseRelationship
55 {
56  return _inverseRelationship;
57 }
58 
59 - (void) setInverseRelationship: (NSRelationshipDescription *) relationship
60 {
61  [self _ensureEditableWithReason: _(@"Tried to set the inverse "
62  @"relationship of a relationship "
63  @"already in use")];
64 
65  // make sure the destination entity contains the relationship
66  if (relationship != nil &&
67  ![[_destinationEntity properties] containsObject: relationship])
68  {
69  [NSException raise: NSInvalidArgumentException
70  format: _(@"Tried to set inverse relationship which is not in the destination entity.")];
71  }
72 
73  _inverseRelationship = relationship;
74 }
75 
76 - (NSDeleteRule) deleteRule
77 {
78  return _deleteRule;
79 }
80 
81 - (void) setDeleteRule: (NSDeleteRule) rule
82 {
83  [self _ensureEditableWithReason: _(@"Tried to set the delete rule "
84  @"of a relationship already in use")];
85  _deleteRule = rule;
86 }
87 
88 - (int) minCount
89 {
90  return _minCount;
91 }
92 
93 - (void) setMinCount: (int) aCount
94 {
95  [self _ensureEditableWithReason: _(@"Tried to set minimum count "
96  @"of a relationship already in use")];
97  if (aCount > _maxCount)
98  {
99  [NSException raise: NSInvalidArgumentException
100  format: _(@"Tried to set minimum count of a relationship "
101  @"higher than it's maximum count")];
102  }
103 
104  _minCount = aCount;
105 }
106 
107 - (int) maxCount
108 {
109  return _maxCount;
110 }
111 
112 - (void) setMaxCount: (int) aCount
113 {
114  [self _ensureEditableWithReason: @"Tried to set maximum count "
115  @"of a relationship already in use."];
116  if (aCount < _minCount)
117  {
118  [NSException raise: NSInvalidArgumentException
119  format: @"Tried to set maximum count of a relationship "
120  @"lower than it's minimum count."];
121  }
122 
123  _maxCount = aCount;
124 }
125 
126 - (BOOL) isToMany
127 {
128  return (_maxCount > 1);
129 }
130 
131 // NSCopying
132 
133 - (id) copyWithZone: (NSZone *) zone
134 {
135  NSRelationshipDescription * relationship = [super copyWithZone: zone];
136 
137  [relationship setDestinationEntity: _destinationEntity];
138  [relationship setInverseRelationship: _inverseRelationship];
139  [relationship setDeleteRule: _deleteRule];
140  [relationship setMaxCount: _maxCount];
141  [relationship setMinCount: _minCount];
142 
143  return relationship;
144 }
145 
146 // NSCoding
147 
148 - (id) initWithCoder: (NSCoder *) coder
149 {
150  if ((self = [super initWithCoder: coder]))
151  {
152  if ([coder allowsKeyedCoding])
153  {
154  ASSIGN(_destinationEntity, [coder decodeObjectForKey:
155  @"DestinationEntity"]);
156  ASSIGN(_inverseRelationship, [coder decodeObjectForKey:
157  @"InverseRelationship"]);
158 
159  _deleteRule = [coder decodeIntForKey: @"DeleteRule"];
160  _minCount = [coder decodeIntForKey: @"MinCount"];
161  _maxCount = [coder decodeIntForKey: @"MaxCount"];
162  }
163  else
164  {
165  ASSIGN(_destinationEntity, [coder decodeObject]);
166  ASSIGN(_inverseRelationship, [coder decodeObject]);
167 
168  [coder decodeValueOfObjCType: @encode(typeof(_deleteRule))
169  at: &_deleteRule];
170  [coder decodeValueOfObjCType: @encode(typeof(_minCount))
171  at: &_minCount];
172  [coder decodeValueOfObjCType: @encode(typeof(_maxCount))
173  at: &_maxCount];
174  }
175  }
176  return self;
177 }
178 
179 - (void) encodeWithCoder: (NSCoder *) coder
180 {
181  [super encodeWithCoder: coder];
182  if ([coder allowsKeyedCoding])
183  {
184  [coder encodeObject: _destinationEntity forKey: @"DestinationEntity"];
185  [coder encodeObject: _inverseRelationship
186  forKey: @"InverseRelationship"];
187 
188  [coder encodeInt: _deleteRule forKey: @"DeleteRule"];
189  [coder encodeInt: _minCount forKey: @"MinCount"];
190  [coder encodeInt: _maxCount forKey: @"MaxCount"];
191  }
192  else
193  {
194  [coder encodeObject: _destinationEntity];
195  [coder encodeObject: _inverseRelationship];
196 
197  [coder encodeValueOfObjCType: @encode(typeof(_deleteRule))
198  at: &_deleteRule];
199  [coder encodeValueOfObjCType: @encode(typeof(_minCount))
200  at: &_minCount];
201  [coder encodeValueOfObjCType: @encode(typeof(_maxCount))
202  at: &_maxCount];
203  }
204 }
205 
206 @end