GNUstep Core Data  0.1
NSPropertyDescription.m
00001 /* Implementation of the NSPropertyDescription 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 NSPropertyDescription
00028 
00029 - (void) dealloc
00030 {
00031   TEST_RELEASE(_name);
00032   TEST_RELEASE(_userInfo);
00033   TEST_RELEASE(_validationPredicates);
00034   TEST_RELEASE(_validationWarnings);
00035 
00036   [super dealloc];
00037 }
00038 
00039 - (NSString *) name
00040 {
00041   return _name;
00042 }
00043 
00044 - (void) setName: (NSString *) aName
00045 {
00046   [self _ensureEditableWithReason: @"Tried to set name of a property "
00047                                    @"already in use."];
00048   ASSIGN(_name, aName);
00049 }
00050 
00051 - (NSEntityDescription *) entity
00052 {
00053   return _entity;
00054 }
00055 
00056 - (BOOL) isOptional
00057 {
00058   return _optional;
00059 }
00060 
00061 - (void) setOptional: (BOOL) flag
00062 {
00063   [self _ensureEditableWithReason: @"Tried to optionality of a property "
00064                                    @"already in use."];
00065   _optional = flag;
00066 }
00067 
00068 - (BOOL) isTransient
00069 {
00070   return _transient;
00071 }
00072 
00073 - (void) setTransient: (BOOL) flag
00074 {
00075   [self _ensureEditableWithReason: @"Tried to set transient-ness of a"
00076                                    @" property already in use."];
00077   _transient = flag;
00078 }
00079 
00080 - (NSDictionary *) userInfo
00081 {
00082   return _userInfo;
00083 }
00084 
00085 - (void) setUserInfo: (NSDictionary *) userInfo
00086 {
00087   [self _ensureEditableWithReason: @"Tried to set user info of a property "
00088                                    @"already in use."];
00089   ASSIGN(_userInfo, userInfo);
00090 }
00091 
00092 - (NSArray *) validationPredicates
00093 {
00094   return _validationPredicates;
00095 }
00096 
00097 - (NSArray *) validationWarnings
00098 {
00099   return _validationWarnings;
00100 }
00101 
00102 - (void) setValidationPredicates: (NSArray *) someValidationPredicates
00103           withValidationWarnings: (NSArray *) someValidationWarnings
00104 {
00105   [self _ensureEditableWithReason: @"Tried to set validation predicates and "
00106                                    @"validation warnings of a property "
00107                                    @"already in use."];
00108   ASSIGN(_validationPredicates, someValidationPredicates);
00109   ASSIGN(_validationWarnings, someValidationWarnings);
00110 }
00111 
00112 // NSCopying
00113 
00114 - (id) copyWithZone: (NSZone *) zone
00115 {
00116   NSPropertyDescription * property;
00117 
00118   property = [NSPropertyDescription new];
00119   [property setName: _name];
00120   [property setOptional: _optional];
00121   [property setTransient: _transient];
00122   [property setUserInfo: _userInfo];
00123   [property setValidationPredicates: _validationPredicates
00124              withValidationWarnings: _validationWarnings];
00125 
00126   return property;
00127 }
00128 
00129 // NSCoding
00130 
00131 - (id) initWithCoder: (NSCoder *) coder
00132 {
00133         if ((self = [super init]))
00134     {
00135       if ([coder allowsKeyedCoding])
00136         {
00137           ASSIGN(_name, [coder decodeObjectForKey: @"Name"]);
00138           ASSIGN(_userInfo, [coder decodeObjectForKey: @"UserInfo"]);
00139           ASSIGN(_validationPredicates, [coder decodeObjectForKey:
00140             @"ValidationPredicates"]);
00141           ASSIGN(_validationWarnings, [coder decodeObjectForKey:
00142             @"ValidationPredicates"]);
00143 
00144           _entity = [coder decodeObjectForKey: @"Entity"];
00145 
00146           _optional = [coder decodeBoolForKey: @"Optional"];
00147           _transient = [coder decodeBoolForKey: @"Transient"];
00148         }
00149       else
00150         {
00151           ASSIGN(_name, [coder decodeObject]);
00152           ASSIGN(_userInfo, [coder decodeObject]);
00153           ASSIGN(_validationPredicates, [coder decodeObject]);
00154           ASSIGN(_validationWarnings, [coder decodeObject]);
00155 
00156           _entity = [coder decodeObject];
00157 
00158           [coder decodeValueOfObjCType: @encode(typeof(_optional))
00159                                     at: &_optional];
00160           [coder decodeValueOfObjCType: @encode(typeof(_transient))
00161                                     at: &_transient];
00162         }
00163         }
00164       return self;
00165 }
00166 
00167 - (void) encodeWithCoder: (NSCoder *) coder
00168 {
00169   if ([coder allowsKeyedCoding])
00170     {
00171       [coder encodeObject: _name forKey: @"Name"];
00172       [coder encodeObject: _userInfo forKey: @"UserInfo"];
00173       [coder encodeObject: _validationPredicates
00174                    forKey: @"ValidationPredicates"];
00175       [coder encodeObject: _validationWarnings
00176                    forKey: @"ValidationWarnings"];
00177 
00178       [coder encodeObject: _entity forKey: @"Entity"];
00179 
00180       [coder encodeBool: _optional forKey: @"Optional"];
00181       [coder encodeBool: _transient forKey: @"Transient"];
00182     }
00183   else
00184     {
00185       [coder encodeObject: _name];
00186       [coder encodeObject: _userInfo];
00187       [coder encodeObject: _validationPredicates];
00188       [coder encodeObject: _validationWarnings];
00189 
00190       [coder encodeObject: _entity];
00191 
00192       [coder encodeValueOfObjCType: @encode(typeof(_optional))
00193                                 at: &_optional];
00194       [coder encodeValueOfObjCType: @encode(typeof(_transient))
00195                                 at: &_transient];
00196     }
00197 }
00198 
00199 @end
00200 
00201 @implementation NSPropertyDescription (GSCoreDataPrivate)
00202 
00207 - (void) _setEntity: (NSEntityDescription *) entity
00208 {
00209   _entity = entity;
00210 }
00211 
00220 - (void) _ensureEditableWithReason: (NSString *) reason
00221 {
00222   NSManagedObjectModel * model;
00223 
00224   model = [_entity managedObjectModel];
00225   if (model != nil && [model _isEditable] == NO)
00226     {
00227       [NSException raise: NSGenericException format: _(reason)];
00228     }
00229 }
00230 
00231 @end