GNUstep Core Data  0.1
NSRelationshipDescription.m
00001 /* Implementation of the NSRelationshipDescription class for the GNUstep
00002    Core Data framework.
00003    Copyright (C) 2005 Free Software Foundation, Inc.
00004 
00005    Written by:  Saso Kiselkov <diablos@manga.sk>
00006    Date: August 2005
00007 
00008    This file is part of the GNUstep Core Data framework.
00009 
00010    This library is free software; you can redistribute it and/or
00011    modify it under the terms of the GNU Lesser General Public
00012    License as published by the Free Software Foundation; either
00013    version 2.1 of the License, or (at your option) any later version.
00014 
00015    This library is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018    Lesser General Public License for more details.
00019 
00020    You should have received a copy of the GNU Lesser General Public
00021    License along with this library; if not, write to the Free
00022    Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
00023  */
00024 
00025 #import "CoreDataHeaders.h"
00026 
00027 @implementation NSRelationshipDescription
00028 
00029 - (void) dealloc
00030 {
00031   TEST_RELEASE(_destinationEntity);
00032 
00033   [super dealloc];
00034 }
00035 
00036 - (NSEntityDescription *) destinationEntity
00037 {
00038   return _destinationEntity;
00039 }
00040 
00041 - (void) setDestinationEntity: (NSEntityDescription *) entity
00042 {
00043   [self _ensureEditableWithReason: _(@"Tried to set the destination "
00044                                     @"entity of a relationship "
00045                                     @"already in use")];
00046   ASSIGN(_destinationEntity, entity);
00047 
00048   // destroy the inverse relationship - we'll have to set it up anew
00049   if (_inverseRelationship != nil)
00050     [_inverseRelationship setInverseRelationship: nil];
00051   [self setInverseRelationship: nil];
00052 }
00053 
00054 - (NSRelationshipDescription *) inverseRelationship
00055 {
00056   return _inverseRelationship;
00057 }
00058 
00059 - (void) setInverseRelationship: (NSRelationshipDescription *) relationship
00060 {
00061   [self _ensureEditableWithReason: _(@"Tried to set the inverse "
00062                                     @"relationship of a relationship "
00063                                     @"already in use")];
00064 
00065   // make sure the destination entity contains the relationship
00066   if (relationship != nil &&
00067     ![[_destinationEntity properties] containsObject: relationship])
00068     {
00069       [NSException raise: NSInvalidArgumentException
00070                   format: _(@"Tried to set inverse relationship which is not in the destination entity.")];
00071     }
00072 
00073   _inverseRelationship = relationship;
00074 }
00075 
00076 - (NSDeleteRule) deleteRule
00077 {
00078   return _deleteRule;
00079 }
00080 
00081 - (void) setDeleteRule: (NSDeleteRule) rule
00082 {
00083   [self _ensureEditableWithReason: _(@"Tried to set the delete rule "
00084                                     @"of a relationship already in use")];
00085   _deleteRule = rule;
00086 }
00087 
00088 - (int) minCount
00089 {
00090   return _minCount;
00091 }
00092 
00093 - (void) setMinCount: (int) aCount
00094 {
00095   [self _ensureEditableWithReason: _(@"Tried to set minimum count "
00096                                     @"of a relationship already in use")];
00097   if (aCount > _maxCount)
00098     {
00099       [NSException raise: NSInvalidArgumentException
00100                   format: _(@"Tried to set minimum count of a relationship "
00101                             @"higher than it's maximum count")];
00102     }
00103 
00104   _minCount = aCount;
00105 }
00106 
00107 - (int) maxCount
00108 {
00109   return _maxCount;
00110 }
00111 
00112 - (void) setMaxCount: (int) aCount
00113 {
00114   [self _ensureEditableWithReason: @"Tried to set maximum count "
00115                                    @"of a relationship already in use."];
00116   if (aCount < _minCount)
00117     {
00118       [NSException raise: NSInvalidArgumentException
00119                   format: @"Tried to set maximum count of a relationship "
00120                           @"lower than it's minimum count."];
00121     }
00122 
00123   _maxCount = aCount;
00124 }
00125 
00126 - (BOOL) isToMany
00127 {
00128   return (_maxCount > 1);
00129 }
00130 
00131 // NSCopying
00132 
00133 - (id) copyWithZone: (NSZone *) zone
00134 {
00135   NSRelationshipDescription * relationship = [super copyWithZone: zone];
00136 
00137   [relationship setDestinationEntity: _destinationEntity];
00138   [relationship setInverseRelationship: _inverseRelationship];
00139   [relationship setDeleteRule: _deleteRule];
00140   [relationship setMaxCount: _maxCount];
00141   [relationship setMinCount: _minCount];
00142 
00143   return relationship;
00144 }
00145 
00146 // NSCoding
00147 
00148 - (id) initWithCoder: (NSCoder *) coder
00149 {
00150   if ((self = [super initWithCoder: coder]))
00151     {
00152       if ([coder allowsKeyedCoding])
00153         {
00154           ASSIGN(_destinationEntity, [coder decodeObjectForKey:
00155             @"DestinationEntity"]);
00156           ASSIGN(_inverseRelationship, [coder decodeObjectForKey:
00157             @"InverseRelationship"]);
00158 
00159           _deleteRule = [coder decodeIntForKey: @"DeleteRule"];
00160           _minCount = [coder decodeIntForKey: @"MinCount"];
00161           _maxCount = [coder decodeIntForKey: @"MaxCount"];
00162         }
00163       else
00164         {
00165           ASSIGN(_destinationEntity, [coder decodeObject]);
00166           ASSIGN(_inverseRelationship, [coder decodeObject]);
00167 
00168           [coder decodeValueOfObjCType: @encode(typeof(_deleteRule))
00169                                     at: &_deleteRule];
00170           [coder decodeValueOfObjCType: @encode(typeof(_minCount))
00171                                     at: &_minCount];
00172           [coder decodeValueOfObjCType: @encode(typeof(_maxCount))
00173                                     at: &_maxCount];
00174         }
00175         }
00176       return self;
00177 }
00178 
00179 - (void) encodeWithCoder: (NSCoder *) coder
00180 {
00181   [super encodeWithCoder: coder];
00182   if ([coder allowsKeyedCoding])
00183     {
00184       [coder encodeObject: _destinationEntity forKey: @"DestinationEntity"];
00185       [coder encodeObject: _inverseRelationship
00186                    forKey: @"InverseRelationship"];
00187 
00188       [coder encodeInt: _deleteRule forKey: @"DeleteRule"];
00189       [coder encodeInt: _minCount forKey: @"MinCount"];
00190       [coder encodeInt: _maxCount forKey: @"MaxCount"];
00191     }
00192   else
00193     {
00194       [coder encodeObject: _destinationEntity];
00195       [coder encodeObject: _inverseRelationship];
00196 
00197       [coder encodeValueOfObjCType: @encode(typeof(_deleteRule))
00198                                 at: &_deleteRule];
00199       [coder encodeValueOfObjCType: @encode(typeof(_minCount))
00200                                 at: &_minCount];
00201       [coder encodeValueOfObjCType: @encode(typeof(_maxCount))
00202                                 at: &_maxCount];
00203     }
00204 }
00205 
00206 @end