PageRenderTime 97ms CodeModel.GetById 79ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Importer/MySpotlightImporter.m

http://github.com/bububa/MongoHub-Mac
Objective C | 169 lines | 87 code | 46 blank | 36 comment | 13 complexity | b5dbb55e68f49626074ad4cf697b9edd MD5 | raw file
  1//
  2//  MySpotlightImporter.m
  3//  MongoHub
  4//
  5//  Created by Syd on 10-4-24.
  6//  Copyright MusicPeace.ORG 2010 . All rights reserved.
  7//
  8
  9#import "MySpotlightImporter.h"
 10
 11#define YOUR_STORE_TYPE NSXMLStoreType
 12
 13@interface MySpotlightImporter ()
 14@property (nonatomic, retain) NSURL *modelURL;
 15@property (nonatomic, retain) NSURL *storeURL;
 16@end
 17
 18@implementation MySpotlightImporter
 19
 20@synthesize modelURL, storeURL;
 21
 22- (BOOL)importFileAtPath:(NSString *)filePath attributes:(NSMutableDictionary *)spotlightData error:(NSError **)error {
 23        
 24    NSDictionary *pathInfo = [NSPersistentStoreCoordinator elementsDerivedFromExternalRecordURL:[NSURL fileURLWithPath:filePath]];
 25            
 26    self.modelURL = [NSURL fileURLWithPath:[pathInfo valueForKey:NSModelPathKey]];
 27    self.storeURL = [NSURL fileURLWithPath:[pathInfo valueForKey:NSStorePathKey]];
 28
 29
 30    NSURL  *objectURI = [pathInfo valueForKey:NSObjectURIKey];
 31    NSManagedObjectID *oid = [[self persistentStoreCoordinator] managedObjectIDForURIRepresentation:objectURI];
 32
 33    if (!oid) {
 34        NSLog(@"%@:%s to find object id from path %@", [self class], _cmd, filePath);
 35        return NO;
 36    }
 37
 38    NSManagedObject *instance = [[self managedObjectContext] objectWithID:oid];
 39
 40    // how you process each instance will depend on the entity that the instance belongs to
 41
 42    if ([[[instance entity] name] isEqualToString:@"YOUR_ENTITY_NAME"]) {
 43
 44        // set the display name for Spotlight search result
 45
 46        NSString *yourDisplayString =  [NSString stringWithFormat:@"YOUR_DISPLAY_STRING %@",[instance valueForKey:@"SOME_KEY"]]; 
 47        [spotlightData setObject: yourDisplayString forKey:(NSString *)kMDItemDisplayName];
 48        
 49         /*
 50            Determine how you want to store the instance information in 'spotlightData' dictionary.
 51            For each property, pick the key kMDItem... from MDItem.h that best fits its content.  
 52            If appropriate, aggregate the values of multiple properties before setting them in the dictionary.
 53            For relationships, you may want to flatten values. 
 54
 55            id YOUR_FIELD_VALUE = [instance valueForKey: ATTRIBUTE_NAME];
 56            [spotlightData setObject: YOUR_FIELD_VALUE forKey: (NSString *) kMDItem...];
 57            ... more property values; 
 58            To determine if a property should be indexed, call isIndexedBySpotlight
 59
 60         */
 61
 62    }
 63
 64    return YES;
 65}
 66
 67- (void)dealloc {
 68
 69    [managedObjectContext release];
 70    [persistentStoreCoordinator release];
 71    [managedObjectModel release];
 72    [modelURL release];
 73    [storeURL release];
 74    
 75    [super dealloc];
 76}
 77
 78/**
 79	Returns the managed object model. 
 80	The last read model is cached in a global variable and reused
 81	if the URL and modification date are identical
 82 */
 83static NSURL				*cachedModelURL = nil;
 84static NSManagedObjectModel *cachedModel = nil;
 85static NSDate				*cachedModelModificationDate =nil;
 86
 87- (NSManagedObjectModel *)managedObjectModel {
 88	
 89    if (managedObjectModel != nil) return managedObjectModel;
 90	
 91	NSDictionary *modelFileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[modelURL path] error:nil];
 92	NSDate *modelModificationDate =  [modelFileAttributes objectForKey:NSFileModificationDate];
 93	
 94	if ([cachedModelURL isEqual:modelURL] && [modelModificationDate isEqualToDate:cachedModelModificationDate]) {
 95		managedObjectModel = [cachedModel retain];
 96	} 	
 97	
 98	if (!managedObjectModel) {
 99		managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
100
101		if (!managedObjectModel) {
102			NSLog(@"%@:%s unable to load model at URL %@", [self class], _cmd, modelURL);
103			return nil;
104		}
105
106		// Clear out all custom classes used by the model to avoid having to link them
107		// with the importer. Remove this code if you need to access your custom logic.
108		NSString *managedObjectClassName = [NSManagedObject className];
109		for (NSEntityDescription *entity in managedObjectModel) {
110			[entity setManagedObjectClassName:managedObjectClassName];
111		}
112		
113		// cache last loaded model
114
115		[cachedModelURL release];
116		cachedModelURL = [modelURL retain];
117		[cachedModel release];
118		cachedModel = [managedObjectModel retain];
119		[cachedModelModificationDate release];
120		cachedModelModificationDate = [modelModificationDate retain];
121	}
122	
123	return managedObjectModel;
124}
125
126/**
127    Returns the persistent store coordinator for the importer.  
128 */
129
130- (NSPersistentStoreCoordinator *) persistentStoreCoordinator {
131
132    if (persistentStoreCoordinator) return persistentStoreCoordinator;
133
134    NSError *error = nil;
135        
136    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
137    if (![persistentStoreCoordinator addPersistentStoreWithType:YOUR_STORE_TYPE 
138										configuration:nil 
139										URL:storeURL 
140										options:nil 
141										error:&error]){
142        NSLog(@"%@:%s unable to add persistent store coordinator - %@", [self class], _cmd, error);
143    }    
144
145    return persistentStoreCoordinator;
146}
147
148/**
149    Returns the managed object context for the importer; already
150    bound to the persistent store coordinator. 
151 */
152 
153- (NSManagedObjectContext *) managedObjectContext {
154
155    if (managedObjectContext) return managedObjectContext;
156
157    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
158	if (!coordinator) {
159        NSLog(@"%@:%s unable to get persistent store coordinator", [self class], _cmd);
160		return nil;
161	}
162
163	managedObjectContext = [[NSManagedObjectContext alloc] init];
164	[managedObjectContext setPersistentStoreCoordinator: coordinator];
165    
166    return managedObjectContext;
167}
168
169@end