//
//  DeptTableViewController.m
//  StudentManager
//
//  Created by Richard Wicentowski on 2/23/11.
//  Copyright 2011 Swarthmore College. All rights reserved.
//

#import "DeptTableViewController.h"
#import "Department.h"
#import "StudentTableViewController.h"


@implementation DeptTableViewController


- (id)initInManagedObjectContext:(NSManagedObjectContext *)context
{
	if (self = [super initWithStyle:UITableViewStylePlain]) {
		NSFetchRequest *request = [[NSFetchRequest alloc] init];
		[request setEntity:[NSEntityDescription entityForName:@"Department"
									   inManagedObjectContext:context]];
		
		[request setFetchBatchSize:20];
		
		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:@"DeptCache"];
		[request release];
		
		self.fetchedResultsController = frc;
		[frc release];
		
		self.titleKey = @"name";
		self.searchKey = @"name";
	}
	return self;
}



- (void)managedObjectSelected:(NSManagedObject *)managedObject
{
	Department *department = (Department *)managedObject;
	StudentTableViewController *stvc = [[StudentTableViewController alloc] initWithDepartment:department];
	[self.navigationController pushViewController:stvc animated:YES];
	[stvc release];	
}


@end
