//
//  StudentTableViewController.m
//  StudentManager
//
//  Created by Rich on 2/22/11.
//  Copyright 2011 Swarthmore College. All rights reserved.
//

#import "StudentTableViewController.h"
#import "Student.h"

@implementation StudentTableViewController


- (id)initWithDepartment:(Department *)department
{
	if (self = [super initWithStyle:UITableViewStylePlain]) {
		NSManagedObjectContext *context = department.managedObjectContext;
		
		NSFetchRequest *request = [[NSFetchRequest alloc] init];
		[request setEntity:[NSEntityDescription entityForName:@"Student"
									   inManagedObjectContext:context]];
		
		[request setFetchBatchSize:20];
		
		NSPredicate *predicate = [NSPredicate predicateWithFormat:@"majorDepartment == %@", department];
		[request setPredicate:predicate];
		
		NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" 
																		 ascending:YES 
																		  selector:@selector(compare:)];

		[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
		
		NSFetchedResultsController *frc = [[NSFetchedResultsController alloc]
										   initWithFetchRequest:request
										   managedObjectContext:context
										   sectionNameKeyPath:@"sectionName"
										   cacheName:nil]; //@"StudentCache"];
		[request release];
		
		self.fetchedResultsController = frc;
		[frc release];
		
		self.titleKey = @"titleKey";
		self.searchKey = @"name";
	}
	return self;
}


@end

