Browse files

- Started using MCLImporter with beginning of persistence into separa…

…te files.
  • Loading branch information...
1 parent 6118325 commit e4bfb2a699b2ec663bf67754a8b2e58aae487bca @metacosm committed Apr 26, 2012
View
8 DataImporter/MCLAppDelegate.m
@@ -8,19 +8,15 @@
#import "MCLAppDelegate.h"
#import "MCLSkillImporter.h"
-#import "MCLSpellImporter.h"
@implementation MCLAppDelegate
@synthesize window = _window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
- MCLSkillImporter *skillImporter = [[MCLSkillImporter alloc] init];
- [skillImporter importFrom:@"/Users/claprun/Downloads/skills.html"];
-
- MCLSpellImporter *spellImporter = [[MCLSpellImporter alloc] init];
- [spellImporter importFrom:@"/Users/claprun/Downloads/spells.html"];
+ [MCLImporter import:SKILLS from:@"/Users/claprun/Downloads/skills.html" withVersion:SR4];
+ [MCLImporter import:SPELLS from:@"/Users/claprun/Downloads/spells.html" withVersion:SR4];
/*NSDictionary *categories = [MCLCategory categories:YES];
NSArray *const sortedKeys = [[categories allKeys] sortedArrayUsingSelector:@selector(compare:)];
View
12 DataImporter/MCLImporter.h
@@ -7,13 +7,25 @@
#import <Foundation/Foundation.h>
+typedef enum {
+ SKILLS,
+ SPELLS,
+ UNKNOWN
+} ImporterType;
+
+static NSString *const SR4 = @"SR4";
@interface MCLImporter : NSObject
- (NSArray *)importFrom:(NSString *)pathOrURL;
+- (NSString *)typeAsString;
+
@property(readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property(readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property(readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
+@property(readonly, nonatomic) NSString *version;
+@property(readonly, nonatomic) ImporterType type;
++ (void)import:(ImporterType)type from:(NSString *)pathOrURL withVersion:(NSString *)dataVersion;
@end
View
78 DataImporter/MCLImporter.m
@@ -7,6 +7,8 @@
#import "MCLImporter.h"
#import "MCLCategory.h"
+#import "MCLSkillImporter.h"
+#import "MCLSpellImporter.h"
@implementation MCLImporter {
@@ -16,8 +18,26 @@ @implementation MCLImporter {
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize managedObjectContext = __managedObjectContext;
+@synthesize type = _type;
+- (NSString *)version {
+ return SR4;
+}
+
++ (void)import:(ImporterType)type from:(NSString *)pathOrURL withVersion:(NSString *)dataVersion {
+ if (SKILLS == type) {
+ [[[MCLSkillImporter alloc] init] importFrom:pathOrURL];
+ }
+ else if (SPELLS == type) {
+ [[[MCLSpellImporter alloc] init] importFrom:pathOrURL];
+ }
+ else {
+ [[NSApplication sharedApplication] presentError:[NSError errorWithDomain:@"info.metacosm.DataImporter" code:-1 userInfo:nil]];
+ }
+
+}
+
// Returns the directory the application uses to store the Core Data store file. This code uses a directory named "info.metacosm.DataImporter" in the user's Downloads directory.
- (NSURL *)applicationFilesDirectory {
NSFileManager *fileManager = [NSFileManager defaultManager];
@@ -35,6 +55,10 @@ - (NSManagedObjectModel *)managedObjectModel {
return __managedObjectModel;
}
+- (NSString *)dataFileName {
+ return [[[self.typeAsString stringByAppendingString:@"-"] stringByAppendingString:self.version] stringByAppendingPathExtension:@"srad"];
+}
+
// Returns the persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. (The directory for the store is created, if necessary.)
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (__persistentStoreCoordinator) {
@@ -76,7 +100,7 @@ - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
}
}
- NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"DataImporter.storedata"];
+ NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:[self dataFileName]];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
if (![coordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error]) {
[[NSApplication sharedApplication] presentError:error];
@@ -114,7 +138,7 @@ - (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window {
}
// Performs the save action for the application, which is to send the save: message to the application's managed object context. Any encountered errors are presented to the user.
-- (IBAction)saveAction:(id)sender {
+- (void)saveAction:(id)sender {
NSError *error = nil;
if (![[self managedObjectContext] commitEditing]) {
@@ -126,51 +150,6 @@ - (IBAction)saveAction:(id)sender {
}
}
-- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
- // Save changes in the application's managed object context before the application terminates.
-
- if (!__managedObjectContext) {
- return NSTerminateNow;
- }
-
- if (![[self managedObjectContext] commitEditing]) {
- NSLog(@"%@:%@ unable to commit editing to terminate", [self class], NSStringFromSelector(_cmd));
- return NSTerminateCancel;
- }
-
- if (![[self managedObjectContext] hasChanges]) {
- return NSTerminateNow;
- }
-
- NSError *error = nil;
- if (![[self managedObjectContext] save:&error]) {
-
- // Customize this code block to include application-specific recovery steps.
- BOOL result = [sender presentError:error];
- if (result) {
- return NSTerminateCancel;
- }
-
- NSString *question = NSLocalizedString(@"Could not save changes while quitting. Quit anyway?", @"Quit without saves error question message");
- NSString *info = NSLocalizedString(@"Quitting now will lose any changes you have made since the last successful save", @"Quit without saves error question info");
- NSString *quitButton = NSLocalizedString(@"Quit anyway", @"Quit anyway button title");
- NSString *cancelButton = NSLocalizedString(@"Cancel", @"Cancel button title");
- NSAlert *alert = [[NSAlert alloc] init];
- [alert setMessageText:question];
- [alert setInformativeText:info];
- [alert addButtonWithTitle:quitButton];
- [alert addButtonWithTitle:cancelButton];
-
- NSInteger answer = [alert runModal];
-
- if (answer == NSAlertAlternateReturn) {
- return NSTerminateCancel;
- }
- }
-
- return NSTerminateNow;
-}
-
- (NSData *)dataFrom:(NSString *)pathOrURL {
return nil;
@@ -236,6 +215,7 @@ - (NSArray *)importFrom:(NSString *)pathOrURL {
// we found a category
if ([currentChildName isEqualToString:@"h3"]) {
currentCategory = [MCLCategory forName:childText atTopLevel:YES];
+
// NSLog(@"Category '%@'", childText);
globalCategoryCount++;
@@ -284,9 +264,11 @@ - (NSArray *)importFrom:(NSString *)pathOrURL {
NSLog(@"Total Categories: %i", globalCategoryCount);
NSLog(@"Total Subcategories: %i", globalSubCategoryCount);
NSLog(@"Total elements: %i", globalElementCount);
+
+ [self saveAction:nil];
+
return nil;
}
-
@end
View
4 DataImporter/MCLSkillImporter.m
@@ -120,4 +120,8 @@ - (BOOL)matchesSubCategory:(NSString *)currentChildName {
return [currentChildName isEqualToString:@"h4"];
}
+- (NSString *)typeAsString {
+ return @"skills";
+}
+
@end
View
4 DataImporter/MCLSpellImporter.m
@@ -176,4 +176,8 @@ - (void)furtherProcessSubCategory:(MCLCategory *)subCategory from:(NSXMLElement
}
}
+- (NSString *)typeAsString {
+ return @"spells";
+}
+
@end

0 comments on commit e4bfb2a

Please sign in to comment.