2008-06-17 16:37:08 +00:00
|
|
|
/* Copyright (c) 2008 Dan Knapp
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
2010-10-20 16:17:24 +00:00
|
|
|
#import <CoreData/NSEntityDescription.h>
|
2020-05-11 15:52:05 +00:00
|
|
|
#import <CoreData/NSManagedObjectModel.h>
|
2010-10-20 16:17:24 +00:00
|
|
|
|
2009-10-19 13:59:16 +00:00
|
|
|
#import <Foundation/NSKeyedUnarchiver.h>
|
2010-10-20 16:17:24 +00:00
|
|
|
#import <Foundation/NSRaise.h>
|
2008-06-17 16:37:08 +00:00
|
|
|
|
|
|
|
@implementation NSManagedObjectModel
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
+ (NSManagedObjectModel *) modelByMergingModels: (NSArray *) models {
|
|
|
|
NSManagedObjectModel *result = [[NSManagedObjectModel alloc] init];
|
|
|
|
NSMutableArray *entities = [NSMutableArray array];
|
|
|
|
|
|
|
|
for (NSManagedObjectModel *merge in models) {
|
|
|
|
[entities addObjectsFromArray: [merge entities]];
|
|
|
|
}
|
|
|
|
|
|
|
|
[result setEntities: entities];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSManagedObjectModel *) mergedModelFromBundles: (NSArray *) bundles {
|
|
|
|
NSMutableArray *models = [NSMutableArray array];
|
|
|
|
|
|
|
|
if (bundles == nil)
|
|
|
|
bundles = [NSArray arrayWithObject: [NSBundle mainBundle]];
|
|
|
|
|
|
|
|
for (NSBundle *bundle in bundles) {
|
|
|
|
NSArray *moms = [bundle pathsForResourcesOfType: @"mom"
|
|
|
|
inDirectory: nil];
|
|
|
|
|
|
|
|
for (NSString *path in moms) {
|
|
|
|
NSURL *url = [NSURL fileURLWithPath: path];
|
|
|
|
NSManagedObjectModel *model =
|
2020-05-12 18:51:39 +00:00
|
|
|
[[NSManagedObjectModel alloc] initWithContentsOfURL: url];
|
2020-05-11 15:52:05 +00:00
|
|
|
|
|
|
|
if (model == nil)
|
|
|
|
NSLog(@"-[%@ initWithContentsOfURL:] failed. url=%@", self,
|
|
|
|
url);
|
|
|
|
|
|
|
|
if (model != nil)
|
|
|
|
[models addObject: model];
|
|
|
|
}
|
2010-10-20 16:17:24 +00:00
|
|
|
}
|
2020-05-11 15:52:05 +00:00
|
|
|
|
|
|
|
return [self modelByMergingModels: models];
|
2008-06-17 16:37:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- init {
|
|
|
|
_entities = [[NSMutableDictionary alloc] init];
|
|
|
|
_fetchRequestTemplates = [[NSMutableDictionary alloc] init];
|
|
|
|
_configurations = [[NSMutableDictionary alloc] init];
|
|
|
|
return self;
|
2008-06-17 16:37:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- initWithCoder: (NSCoder *) coder {
|
|
|
|
if (![coder allowsKeyedCoding])
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"%@ can not initWithCoder:%@", [self class],
|
|
|
|
[coder class]];
|
|
|
|
|
|
|
|
_entities = [[coder decodeObjectForKey: @"NSEntities"] retain];
|
|
|
|
for (NSEntityDescription *entity in [_entities allValues])
|
|
|
|
[_entities setObject: entity forKey: [[entity name] uppercaseString]];
|
|
|
|
|
|
|
|
_fetchRequestTemplates =
|
2020-05-12 18:51:39 +00:00
|
|
|
[[coder decodeObjectForKey: @"NSFetchRequestTemplates"] retain];
|
2020-05-11 15:52:05 +00:00
|
|
|
_versionIdentifiers =
|
2020-05-12 18:51:39 +00:00
|
|
|
[[coder decodeObjectForKey: @"NSVersionIdentifiers"] retain];
|
2008-06-17 16:37:08 +00:00
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
return self;
|
2008-06-17 16:37:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- initWithContentsOfURL: (NSURL *) url {
|
|
|
|
[self dealloc];
|
2008-06-17 16:37:08 +00:00
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
NSData *data = [[NSData alloc] initWithContentsOfURL: url];
|
|
|
|
|
|
|
|
if (data == nil)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
NSKeyedUnarchiver *unarchiver =
|
2020-05-12 18:51:39 +00:00
|
|
|
[[NSKeyedUnarchiver alloc] initForReadingWithData: data];
|
2020-05-11 15:52:05 +00:00
|
|
|
NSManagedObjectModel *result = [unarchiver decodeObjectForKey: @"root"];
|
|
|
|
|
|
|
|
[unarchiver release];
|
|
|
|
[data release];
|
|
|
|
|
|
|
|
return result;
|
2008-06-17 16:37:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (NSArray *) entities {
|
|
|
|
return [_entities allValues];
|
2008-06-17 16:37:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (NSDictionary *) entitiesByName {
|
|
|
|
return _entities;
|
2008-06-17 16:37:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (NSDictionary *) localizationDictionary {
|
|
|
|
return _localizationDictionary;
|
2008-06-17 16:37:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (void) setEntities: (NSArray *) entities {
|
|
|
|
[_entities removeAllObjects];
|
|
|
|
|
|
|
|
for (NSEntityDescription *entity in entities) {
|
|
|
|
[_entities setObject: entity forKey: [entity name]];
|
|
|
|
[_entities setObject: entity forKey: [[entity name] uppercaseString]];
|
|
|
|
}
|
2008-06-17 16:37:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (void) setLocalizationDictionary: (NSDictionary *) dictionary {
|
|
|
|
dictionary = [dictionary copy];
|
|
|
|
[_localizationDictionary release];
|
|
|
|
_localizationDictionary = dictionary;
|
2008-06-17 16:37:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (NSArray *) configurations {
|
|
|
|
return [_configurations allKeys];
|
2008-06-17 16:37:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (NSArray *) entitiesForConfiguration: (NSString *) configuration {
|
|
|
|
return [_configurations objectForKey: configuration];
|
2008-06-17 16:37:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (void) setEntities: (NSArray *) entities
|
2020-05-12 18:51:39 +00:00
|
|
|
forConfiguration: (NSString *) configuration
|
2020-05-12 00:04:26 +00:00
|
|
|
{
|
2020-05-11 15:52:05 +00:00
|
|
|
[_configurations setObject: entities forKey: configuration];
|
2008-06-17 16:37:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (NSFetchRequest *) fetchRequestTemplateForName: (NSString *) name {
|
|
|
|
return [_fetchRequestTemplates objectForKey: name];
|
2008-06-17 16:37:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (NSFetchRequest *) fetchRequestFromTemplateWithName: (NSString *) name
|
|
|
|
substitutionVariables:
|
2020-05-12 18:51:39 +00:00
|
|
|
(NSDictionary *) variables
|
2020-05-12 00:04:26 +00:00
|
|
|
{
|
2008-06-17 16:37:08 +00:00
|
|
|
NSUnimplementedMethod();
|
|
|
|
}
|
|
|
|
|
2020-05-11 15:52:05 +00:00
|
|
|
- (void) setFetchRequestTemplate: (NSFetchRequest *) fetchRequest
|
2020-05-12 00:04:26 +00:00
|
|
|
forName: (NSString *) name
|
|
|
|
{
|
2020-05-11 15:52:05 +00:00
|
|
|
[_fetchRequestTemplates setObject: fetchRequest forKey: name];
|
2010-10-20 16:17:24 +00:00
|
|
|
}
|
2008-06-17 16:37:08 +00:00
|
|
|
|
|
|
|
@end
|