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

#import "Department.h"
#import "Student.h"

@implementation Department 

@dynamic name;
@dynamic majors;

+ (Department *)departmentWithName:(NSString *)deptName
	  inManagedObjectContext:(NSManagedObjectContext *)context
{
	Department *department = nil;
	
	NSFetchRequest *request = [[NSFetchRequest alloc] init];
	request.entity = [NSEntityDescription entityForName:@"Department" inManagedObjectContext:context];
	NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", deptName];
	request.predicate = predicate;
	
	NSError *error = nil;
	department = [[context executeFetchRequest:request error:&error] lastObject];
	
	if (!error && !department) {
		department = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:context];
		department.name = deptName;
	}
	
	return department;
}



- (NSString *)sectionName {
	return [self.name substringToIndex:1];
}


@end
