//
//  MyWindowController.m
//  DatabaseExample
//
//  Created by Bernard Teo on 27/06/2007.
//  Copyright 2007 Cutedge. All rights reserved.
//
// This simple example illustrates how to access three database systems in the same uniform way
// from Objective-C code. With just two calls, lookup and execute, it's possible already to do
// quite a lot of stuff with the database you create using Maven. 
//
// I've used quite a bit more convenience methods in the actual source code for Luca and Maven, 
// but this is the basic structure. It's useful for starters and I will make more of the 
// code available as I build on this example.
// 
// The only thing to note for now is that PostgreSQL is case-sensitive. So for table and
// column names. I've enclosed them within single back quotes, e.g., `act_accounts`, in the
// SQL statement that I pass to the database frameworks and this will make that single statement
// work across all three database systems - MySQL5, PostgreSQL 8, and SQLite3.
//
// Comments and feedback to bernard@cutedgesystems.com

#import "MyWindowController.h"

@implementation MyWindowController

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
	[self chooseDatabaseType:databaseType];
	[self showDatabaseConnectionSheet:nil];
}

- (IBAction)chooseDatabaseType:(id)sender
{
	NSDictionary *dict = [[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:[sender titleOfSelectedItem]];
	if (dict != nil)
	{
		[databaseName setStringValue:[dict valueForKey:@"databaseName"]];
		[databaseURL setStringValue:[dict valueForKey:@"databaseURL"]];
		[databaseUserName setStringValue:[dict valueForKey:@"databaseUserName"]];
		[databasePassword setStringValue:[dict valueForKey:@"databasePassword"]];
	}
}

- (IBAction)showDatabaseConnectionSheet:(id)sender
{
	[NSApp beginSheet:databasePanel modalForWindow:[self window] modalDelegate:self didEndSelector:nil contextInfo:nil]; 
}

- (IBAction)closeSheet:(id)sender
{
	NSWindow *sheet = [[self window] attachedSheet];
	[NSApp endSheet:sheet];
	[sheet orderOut:nil];
	
	if ([sender tag] != 0)
		[self selectDatabase:nil];
}

- (IBAction)selectDatabase:(id)sender
{
	[self loadDataFromDatabase:nil];
	
	[[self window] setTitle:[NSString stringWithFormat:@"Accounts from %@  %@ @ %@", [databaseType titleOfSelectedItem], [databaseName stringValue], [databaseURL stringValue]]];
	
	NSMutableDictionary *dict = [NSMutableDictionary dictionary];
	[dict setObject:[databaseName stringValue] forKey:@"databaseName"];
	[dict setObject:[databaseURL stringValue] forKey:@"databaseURL"];
	[dict setObject:[databaseUserName stringValue] forKey:@"databaseUserName"];
	[dict setObject:[databasePassword stringValue] forKey:@"databasePassword"];
	// note: safer to store password in Keychain - will be improved on
	[[[NSUserDefaultsController sharedUserDefaultsController] values] setValue:dict forKey:[databaseType titleOfSelectedItem]];			
}

- (IBAction)loadDataFromDatabase:(id)sender
{
	[arrayController removeObjects:[arrayController arrangedObjects]];
	[arrayController addObjects:[[self dataModeller] lookup:@"SELECT * FROM `act_accounts` WHERE `acctParent` = 'Chart of Accounts' ORDER BY `acctCode`"]];
}

- (IBAction)saveDataToDatabase:(id)sender
{
	[[self window] makeFirstResponder:[self window]];
	
	NSMutableDictionary *acctRecord = [[arrayController arrangedObjects] objectAtIndex:[arrayController selectionIndex]];
	NSString *acctCode = [acctRecord valueForKey:@"acctCode"];
	NSString *acctTitle = [acctRecord valueForKey:@"acctTitle"];
	NSString *acctGroup = [acctRecord valueForKey:@"acctGroup"];
	NSString *acctDebitCredit = [acctRecord valueForKey:@"acctDebitCredit"];
	NSString *acctKey = [acctRecord valueForKey:@"acctKey"];
	
	NSString *sqlString = [NSString stringWithFormat:@"UPDATE `act_accounts` SET `acctCode`='%@', `acctTitle`='%@', `acctGroup`='%@', `acctDebitCredit`='%@' WHERE `acctKey`='%@' ", acctCode,  acctTitle, acctGroup, acctDebitCredit, acctKey];
	// NSLog(@"sqlString = %@", sqlString);
	[[self dataModeller] execute:sqlString];
}

- (DataModeller *)dataModeller
{
	DataModeller *dm = [DataModeller dataModellerWithConnectionForDatabaseDriver:[databaseType titleOfSelectedItem] url:[databaseURL stringValue] databaseName:[databaseName stringValue] user:[databaseUserName stringValue] password:[databasePassword stringValue]];
	
	return dm;
}

@end
