PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/DatabasesArrayController.m

http://github.com/bububa/MongoHub-Mac
Objective C | 85 lines | 69 code | 9 blank | 7 comment | 9 complexity | a28fbe587fdc5b95fc534d5f2971f4e9 MD5 | raw file
 1//
 2//  DatabasesArrayCollection.m
 3//  MongoHub
 4//
 5//  Created by Syd on 10-4-25.
 6//  Copyright 2010 MusicPeace.ORG. All rights reserved.
 7//
 8
 9#import "DatabasesArrayController.h"
10#import "Connection.h"
11#import "Database.h"
12
13@implementation DatabasesArrayController
14
15- (void)awakeFromNib
16{
17	if ([NSArrayController instancesRespondToSelector:@selector(awakeFromNib)])
18	{
19		[super awakeFromNib];
20	}
21	[self setClearsFilterPredicateOnInsertion:NO];
22}
23
24- (id)newObjectWithConn:(Connection *) conn name:(NSString *)name user:(NSString *)user password:(NSString *)password
25{
26    id newObj = [super newObject];
27    [newObj setValue:conn forKey:@"connection"];
28    [newObj setValue:name forKey:@"name"];
29    [newObj setValue:user forKey:@"user"];
30    [newObj setValue:password forKey:@"password"];
31    return newObj;
32}
33
34- (void)clean:(Connection *)conn databases:(NSArray *)databases
35{
36    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"connection=%@", conn];
37    NSArray *dblist = [self itemsUsingFetchPredicate:predicate];
38    for (Database *db in dblist) {
39        bool exist = false;
40        for (NSString *d in databases) {
41            if (db.name == d) {
42                exist = true;
43                break;
44            }
45        }
46        if (!exist) {
47            [super remove:db];
48        }
49    }
50}
51
52- (Database *)dbInfo:(Connection *) conn name:(NSString *)name
53{
54    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"connection=%@ AND name=%@", conn, name];
55    if ([[self itemsUsingFetchPredicate:predicate] count]>0) {
56        return [[self itemsUsingFetchPredicate:predicate] objectAtIndex:0];
57    }
58    return nil;
59}
60
61- (BOOL)checkDuplicate:(Connection *) conn name:(NSString *)name
62{
63    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"connection=%@ AND name=%@", conn, name];
64    if ([[self itemsUsingFetchPredicate:predicate] count]>0) {
65        return YES;
66    }else {
67        return NO;
68    }
69}
70
71- (NSArray *)itemsUsingFetchPredicate:(NSPredicate *)fetchPredicate
72{
73    NSError *error = nil;
74    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
75    [request setEntity:[NSEntityDescription entityForName:[self entityName]
76                                   inManagedObjectContext:[self managedObjectContext]]];
77    NSArray *objects = [[self managedObjectContext]  
78                        executeFetchRequest:request error:&error];
79    if (error) {
80        NSLog(@"Fetch error! In AWViewPositionArrayController:itemsUseingFetchPredicate");
81    }
82    return [objects filteredArrayUsingPredicate:fetchPredicate];
83}
84
85@end