/***************************************************************** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. ****************************************************************/ #import #import "AppController.h" #import #import #import #import #import #import #import #import #import #import #import @implementation AppController - (IBAction)commit:(id)sender { NSError *error = nil; BOOL ok = [[self objectContext] commitChanges:&error]; if(!ok) { NSLog(@"Error in commitChanges: %@", [error localizedDescription]); [NSApp presentError:error]; } } - (IBAction)query:(id)sender { if([self objectContext] == nil) { NSLog(@" *** initing cayenne client *** "); // TODO: user interface for username/password // NSURL * url = [NSURL URLWithString:@"http://localhost:8080/cayenne-service"]; NSURL * url = [NSURL URLWithString:@"http://cayenne-user:secret@localhost:8080/cayenne-service"]; CAYClientConnection *conn = [[CAYHessianConnection alloc] initWithUrl:url]; [conn connect]; CAYObjectContext *ctxt = [[CAYObjectContext alloc] init]; [ctxt setConnection:conn]; [conn release]; [self setObjectContext:ctxt]; [ctxt release]; // set the undo manager if you want undo support. see also windowWillReturnUndoManager that make // the undoManager accessible to the application NSUndoManager *um = [[NSUndoManager alloc] init]; [ctxt setUndoManager:um]; [um release]; // set the object context of the custom array controller. needed to // be able to add artists [artistsController setObjectContext:[self objectContext]]; [artistsController setDeleteObjectOnRemove:YES]; [galleriesController setObjectContext:[self objectContext]]; [galleriesController setDeleteObjectOnRemove:YES]; // try without context. most stuff will still work as it is a // related entity. [paintingsController setDeleteObjectOnRemove:YES]; // test creating core data model: // CAYEntityResolver *entityResolver = [[self objectContext] entityResolver]; // CAYDataMap *dataMap = [[entityResolver maps] objectAtIndex:0]; // NSManagedObjectModel *coreDataModel = [CAYDataMapIO dataMapAsManagedObjectModel:dataMap entityResolver:entityResolver]; // NSLog(@"DEBUG: core data model: %@", coreDataModel); } NSError *error = nil; // prepare/refresh the galleries controller CAYNamedQuery *gquery = [[CAYNamedQuery alloc] init]; [gquery setName:@"galleryQuery"]; NSArray *grows = [[self objectContext] performQuery:gquery error:&error]; [gquery release]; if(!grows) { [NSApp presentError:error]; return; } [galleriesController setContent:grows]; // prepare/refresh the artist controller CAYNamedQuery *aquery = [[CAYNamedQuery alloc] init]; [aquery setName:@"artistQuery"]; NSArray *arows = [[self objectContext] performQuery:aquery error:&error]; [aquery release]; if(!arows) { [NSApp presentError:error]; return; } [artistsController setContent:arows]; } -(void)setObjectContext:(CAYObjectContext *)ctxt { [ctxt retain]; [objectContext release]; objectContext = ctxt; } -(CAYObjectContext *)objectContext { return objectContext; } // trick to use NSUndoManager in a non-document based application // http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles/cdFAQ.html#//apple_ref/doc/uid/TP40001802-244036 - (NSUndoManager *) windowWillReturnUndoManager:(NSWindow *)sender { return [[self objectContext] undoManager]; } -(void)dealloc { [self setObjectContext:nil]; [super dealloc]; } @end