|
|
Sample Programs from NeXSTEP 3.3
/*--------------------------------------------------------------------------
*
* You may freely copy, distribute, and reuse the code in this example.
* SHL Systemhouse disclaims any warranty of any kind, expressed or
* implied, as to its fitness for any particular use.
*
*
* AccessLevel
*
* Inherits From: NSObject
*
* Conforms To: None
*
* Declared In: AccessLevel.h
*
*------------------------------------------------------------------------*/
#import "AccessLevel.h"
#import "ScrollViewExtensions.h"
#import <eoaccess/eoaccess.h>
#import <appkit/appkit.h>
@implementation AccessLevel
- connect: sender
{
NSString *path;
[messageConsole clear: nil];
[messageConsole sprintf: "connecting to database...\n"];
[messageConsole sprintf: "obtaining model path...\n"];
if ((path = [EOModel findPathForModelNamed:
[NSString stringWithCString:"Authors"]]) == nil)
{
NXRunAlertPanel (NULL, "Unable to obtain model path",
NULL, NULL, NULL);
[messageConsole sprintf: "unable to obtain model path\n"];
return nil;
}
[modelPath setStringValue: [path cString]];
[messageConsole sprintf: "initializing model...\n"];
if ((model = [[EOModel alloc] initWithContentsOfFile:path]) == nil)
{
NXRunAlertPanel (NULL, "Unable to initialize model", NULL, NULL, NULL);
[messageConsole sprintf: "unable to initialize model\n"];
return nil;
}
[messageConsole sprintf: "creating adaptor...\n"];
if ((adaptor = [EOAdaptor adaptorWithModel: model]) == nil)
{
NXRunAlertPanel (NULL, "Unable to create adaptor", NULL, NULL, NULL);
[messageConsole sprintf: "unable to create adaptor\n"];
return nil;
}
[messageConsole sprintf: "creating context...\n"];
if ((context = [adaptor createAdaptorContext]) == nil)
{
NXRunAlertPanel (NULL, "Unable to create context", NULL, NULL, NULL);
[messageConsole sprintf: "unable to create context\n"];
return nil;
}
[messageConsole sprintf: "creating channel...\n"];
if ((channel = [context createAdaptorChannel]) == nil)
{
NXRunAlertPanel (NULL, "Unable to create channel", NULL, NULL, NULL);
[messageConsole sprintf: "unable to create channel\n"];
return nil;
}
// Bring up a login panel and get the valid connection dictionary
if(![adaptor runLoginPanelAndValidateConnectionDictionary]) {
[messageConsole sprintf: "invalid login\n"];
return nil;
}
else {
[adaptor setConnectionDictionary: [adaptor connectionDictionary]];
[messageConsole sprintf: "opening channel...\n"];
if ([channel openChannel] == NO)
{
NXRunAlertPanel (NULL, "Unable to open channel",
NULL, NULL, NULL);
[messageConsole sprintf: "unable to open channel\n"];
return nil;
}
}
[messageConsole sprintf:"channel %s open\n\n",
[channel isOpen] ? "is" : "is not"];
[connectButton setEnabled: ! [channel isOpen]];
[selectButton setEnabled: [channel isOpen]];
return self;
}
- select: sender
{
int count = 0;
id entity, qualifier, attributes, dictionary;
[resultConsole clear: nil];
[messageConsole sprintf: "beginning transaction...\n"];
if ([context beginTransaction] == NO)
{
NXRunAlertPanel (NULL, "Unable to begin transaction",
NULL, NULL, NULL);
[messageConsole sprintf: "unable to begin transaction\n"];
return nil;
}
[messageConsole sprintf: "obtaining entity...\n"];
if ((entity = [model entityNamed: @"authors"]) == nil)
{
NXRunAlertPanel (NULL, "Unable to obtain entity authors",
NULL, NULL, NULL);
[messageConsole sprintf: "unable to obtain entity authors\n"];
return nil;
}
[messageConsole sprintf: "obtaining qualifier...\n"];
if ((qualifier = [entity qualifier]) == nil)
{
NXRunAlertPanel (NULL, "Unable to obtain qualifier",
NULL, NULL, NULL);
[messageConsole sprintf: "unable to obtain qualifier\n"];
return nil;
}
[messageConsole sprintf: "obtaining attributes...\n"];
if ((attributes = [entity attributes]) == nil)
{
NXRunAlertPanel (NULL, "Unable to obtain attributes",
NULL, NULL, NULL);
[messageConsole sprintf: "unable to obtain attributes\n"];
return nil;
}
[messageConsole sprintf: "selecting attributes...\n"];
if ([channel selectAttributes:attributes
describedByQualifier: qualifier fetchOrder:nil lock:NO] == NO)
{
NXRunAlertPanel (NULL, "Unable to select attributes",
NULL, NULL, NULL);
[messageConsole sprintf: "unable to select attributes\n"];
return nil;
}
while ((dictionary = [channel fetchAttributes:attributes
withZone:[(EOAdaptor*)adaptor zone]]) != nil)
{
[resultConsole sprintf: "%s\n",
[[(NSString*)dictionary description] cString]];
count++;
}
[messageConsole sprintf: "\n%d records found\n\n", count];
[messageConsole sprintf: "rolling back transaction...\n"];
if ([context rollbackTransaction] == NO)
{
NXRunAlertPanel (NULL, "Unable to rollback transaction",
NULL, NULL, NULL);
[messageConsole sprintf: "unable to rollback transaction\n"];
return nil;
}
[messageConsole sprintf: "done\n\n"];
return self;
}
@end
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.