From: helge Date: Wed, 3 Aug 2005 18:57:26 +0000 (+0000) Subject: more work on CoreData objects X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=83661edb281d39c90f8bc37a9f6e7ba9f78611fc;p=sope more work on CoreData objects git-svn-id: http://svn.opengroupware.org/SOPE/trunk@960 e4a50df8-12e2-0310-a44c-efbce7f8a7e3 --- diff --git a/sope-appserver/samples/CoreDataBlog/ChangeLog b/sope-appserver/samples/CoreDataBlog/ChangeLog index bbb6ed54..aa7af7da 100644 --- a/sope-appserver/samples/CoreDataBlog/ChangeLog +++ b/sope-appserver/samples/CoreDataBlog/ChangeLog @@ -1,5 +1,7 @@ 2005-08-03 Helge Hess + * added EOCoreDataSource + * added classes to map EOControl objects to CoreData ones 2005-08-01 Helge Hess diff --git a/sope-appserver/samples/CoreDataBlog/EOCoreDataSource.h b/sope-appserver/samples/CoreDataBlog/EOCoreDataSource.h new file mode 100644 index 00000000..0e9b1bee --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/EOCoreDataSource.h @@ -0,0 +1,77 @@ +/* + Copyright (C) 2005 SKYRIX Software AG + + This file is part of SOPE. + + SOPE is free software; you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + SOPE is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with SOPE; see the file COPYING. If not, write to the + Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. +*/ + +#ifndef __EOCoreDataSource_H__ +#define __EOCoreDataSource_H__ + +#include + +/* + EOCoreDataSource + + This wraps a NSManagedObjectContext in an EODataSource. It corresponds to + the EODatabaseDataSource available in EOF. +*/ + +@class NSArray, NSDictionary; +@class NSManagedObjectContext, NSEntityDescription; +@class EOQualifier, EOFetchSpecification; + +@interface EOCoreDataSource : EODataSource +{ + NSManagedObjectContext *managedObjectContext; + NSEntityDescription *entity; + EOFetchSpecification *fetchSpecification; + EOQualifier *auxiliaryQualifier; + NSDictionary *qualifierBindings; + struct { + int isFetchEnabled:1; + int reserved:31; + } ecdFlags; +} + +- (id)initWithManagedObjectContext:(NSManagedObjectContext *)_moc + entity:(NSEntityDescription *)_entity; + +/* fetch-spec */ + +- (void)setFetchSpecification:(EOFetchSpecification *)_fspec; +- (EOFetchSpecification *)fetchSpecification; +- (EOFetchSpecification *)fetchSpecificationForFetch; + +- (void)setAuxiliaryQualifier:(EOQualifier *)_qualifier; +- (EOQualifier *)auxiliaryQualifier; + +- (void)setIsFetchEnabled:(BOOL)_flag; +- (BOOL)isFetchEnabled; + +- (NSArray *)qualifierBindingKeys; +- (void)setQualifierBindings:(NSDictionary *)_bindings; +- (NSDictionary *)qualifierBindings; + +/* accessors */ + +- (NSEntityDescription *)entity; +- (NSManagedObjectContext *)managedObjectContext; + +@end + +#endif /* __EOCoreDataSource_H__ */ diff --git a/sope-appserver/samples/CoreDataBlog/EOCoreDataSource.m b/sope-appserver/samples/CoreDataBlog/EOCoreDataSource.m new file mode 100644 index 00000000..b3b2e3a6 --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/EOCoreDataSource.m @@ -0,0 +1,230 @@ +/* + Copyright (C) 2005 SKYRIX Software AG + + This file is part of SOPE. + + SOPE is free software; you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + SOPE is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with SOPE; see the file COPYING. If not, write to the + Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. +*/ + +#include "EOCoreDataSource.h" +#include "EOFetchSpecification+CoreData.h" +#include "common.h" + +@implementation EOCoreDataSource + +- (id)initWithManagedObjectContext:(NSManagedObjectContext *)_moc + entity:(NSEntityDescription *)_entity +{ + if ((self = [super init]) != nil) { + if (_moc == nil || _entity == nil) { + NSLog(@"ERROR(%s): missing object-context or entity parameter!", + __PRETTY_FUNCTION__); + [self release]; + return nil; + } + + self->managedObjectContext = [_moc retain]; + self->entity = [_entity retain]; + } + return self; +} + +- (id)init { + return [self initWithManagedObjectContext:nil entity:nil]; +} + +- (void)dealloc { + [self->qualifierBindings release]; + [self->entity release]; + [self->managedObjectContext release]; + [self->fetchSpecification release]; + [self->auxiliaryQualifier release]; + [super dealloc]; +} + +/* fetch-spec */ + +- (void)setFetchSpecification:(EOFetchSpecification *)_fspec { + if ([self->fetchSpecification isEqual:_fspec]) + return; + + [self->fetchSpecification autorelease]; + self->fetchSpecification = [_fspec copy]; + + [self postDataSourceChangedNotification]; +} + +- (EOFetchSpecification *)fetchSpecification { + return self->fetchSpecification; +} + +- (EOFetchSpecification *)fetchSpecificationForFetch { + EOFetchSpecification *fs; + EOQualifier *aq; + NSDictionary *bindings; + + fs = [[[self fetchSpecification] copy] autorelease]; + + /* add auxiliary-qualifier */ + + if ((aq = [self auxiliaryQualifier]) != nil) { + EOQualifier *q; + + if ((q = [fs qualifier]) != nil) { + q = [[EOAndQualifier alloc] initWithQualifiers:q, aq, nil]; + [fs setQualifier:q]; + [q release]; q = nil; + } + else + [fs setQualifier:aq]; + } + + /* apply bindings */ + + if ((bindings = [self qualifierBindings]) != nil ) { +#warning IMPLEMENT ME! + } + + return fs; +} + +- (void)setAuxiliaryQualifier:(EOQualifier *)_qualifier { + if ([self->auxiliaryQualifier isEqual:_qualifier]) + return; + + [self postDataSourceChangedNotification]; +} +- (EOQualifier *)auxiliaryQualifier { + return self->auxiliaryQualifier; +} + +- (NSArray *)qualifierBindingKeys { + NSMutableSet *join; + NSArray *b, *ab; + + b = [[[self fetchSpecification] qualifier] bindingKeys]; + ab = [[self auxiliaryQualifier] bindingKeys]; + if (ab == nil) return b; + if (b == nil) return ab; + + join = [[NSMutableSet alloc] initWithCapacity:16]; + [join addObjectsFromArray:b]; + [join addObjectsFromArray:ab]; + b = [join allObjects]; + [join release]; + return b; +} + +- (void)setQualifierBindings:(NSDictionary *)_bindings { + ASSIGN(self->qualifierBindings, _bindings); +} +- (NSDictionary *)qualifierBindings { + return self->qualifierBindings; +} + +- (void)setIsFetchEnabled:(BOOL)_flag { + self->ecdFlags.isFetchEnabled = _flag ? 1 : 0; +} +- (BOOL)isFetchEnabled { + return self->ecdFlags.isFetchEnabled ? YES : NO; +} + +/* accessors */ + +- (NSEntityDescription *)entity { + return self->entity; +} +- (NSManagedObjectContext *)managedObjectContext { + return self->managedObjectContext; +} + +/* fetching */ + +- (NSArray *)fetchObjects { + EOFetchSpecification *fs; + NSFetchRequest *fr; + NSError *error = nil; + NSArray *results; + + if (![self isFetchEnabled]) + return [NSArray array]; + + fs = [self fetchSpecificationForFetch]; + fr = [fs fetchRequestWithEntity:[self entity]]; + + results = [[self managedObjectContext] executeFetchRequest:fr error:&error]; + if (results == nil) { + } + + // TODO: add grouping support? + + return results; +} + +/* operations */ + +- (void)deleteObject:(id)_object { + [[self managedObjectContext] deleteObject:_object]; + [self postDataSourceChangedNotification]; +} + +- (void)insertObject:(id)_object { + [[self managedObjectContext] insertObject:_object]; + [self postDataSourceChangedNotification]; +} + +- (id)createObject { + Class clazz; + id newObject; + + clazz = NSClassFromString([[self entity] managedObjectClassName]); + + newObject = [[clazz alloc] initWithEntity:[self entity] + insertIntoManagedObjectContext: + [self managedObjectContext]]; + return [newObject autorelease]; +} + +/* class description */ + +- (EOClassDescription *)classDescriptionForObjects { + // TODO: should we create an EOClassDescription or just add + // EOClassDescription description stuff to NSEntityDescription? + return (id)[self entity]; +} + +/* description */ + +- (NSString *)description { + NSMutableString *ms; + + ms = [NSMutableString stringWithCapacity:64]; + [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])]; + + if (self->fetchSpecification != nil) + [ms appendFormat:@" fs=%@", self->fetchSpecification]; + + if (self->auxiliaryQualifier != nil) + [ms appendFormat:@" aux=%@", self->auxiliaryQualifier]; + + if (self->entity != nil) + [ms appendFormat:@" entity=%@", self->entity]; + + [ms appendString:@">"]; + return ms; +} + +@end /* EOCoreDataSource */ diff --git a/sope-appserver/samples/CoreDataBlog/EOFetchSpecification+CoreData.h b/sope-appserver/samples/CoreDataBlog/EOFetchSpecification+CoreData.h index 6d143e6e..02be4adf 100644 --- a/sope-appserver/samples/CoreDataBlog/EOFetchSpecification+CoreData.h +++ b/sope-appserver/samples/CoreDataBlog/EOFetchSpecification+CoreData.h @@ -35,10 +35,13 @@ */ @class NSArray; -@class NSFetchRequest, NSManagedObjectModel; +@class NSFetchRequest, NSManagedObjectModel, NSEntityDescription; @interface EOFetchSpecification(CoreData) +- (id)initWithFetchRequest:(NSFetchRequest *)_fr; + +- (NSFetchRequest *)fetchRequestWithEntity:(NSEntityDescription *)_entity; - (NSFetchRequest *)fetchRequestWithModel:(NSManagedObjectModel *)_model; - (NSArray *)sortOrderingsAsSortDescriptors; diff --git a/sope-appserver/samples/CoreDataBlog/EOFetchSpecification+CoreData.m b/sope-appserver/samples/CoreDataBlog/EOFetchSpecification+CoreData.m index be52a1ba..5ace1b43 100644 --- a/sope-appserver/samples/CoreDataBlog/EOFetchSpecification+CoreData.m +++ b/sope-appserver/samples/CoreDataBlog/EOFetchSpecification+CoreData.m @@ -27,6 +27,11 @@ @implementation EOFetchSpecification(CoreData) +- (id)initWithFetchRequest:(NSFetchRequest *)_fr { +#warning IMPLEMENT ME + return nil; +} + - (NSArray *)sortOrderingsAsSortDescriptors { NSMutableArray *ma; NSArray *a; @@ -46,20 +51,26 @@ return ma; } -- (NSFetchRequest *)fetchRequestWithModel:(NSManagedObjectModel *)_model { +- (NSFetchRequest *)fetchRequestWithEntity:(NSEntityDescription *)_entity { NSFetchRequest *fr; - NSString *s; fr = [[[NSFetchRequest alloc] init] autorelease]; - - if ((s = [self entityName]) != nil) - [fr setEntity:[[_model entitiesByName] objectForKey:s]]; - + [fr setEntity:_entity]; [fr setFetchLimit:[self fetchLimit]]; [fr setPredicate:[[self qualifier] asPredicate]]; [fr setSortDescriptors:[self sortOrderingsAsSortDescriptors]]; - return fr; } +- (NSFetchRequest *)fetchRequestWithModel:(NSManagedObjectModel *)_model { + NSEntityDescription *entity; + NSString *s; + + entity = ((s = [self entityName]) != nil) + ? [[_model entitiesByName] objectForKey:s] + : nil; + + return [self fetchRequestWithEntity:entity]; +} + @end /* EOFetchSpecification(CoreData) */ diff --git a/sope-appserver/samples/CoreDataBlog/GNUmakefile b/sope-appserver/samples/CoreDataBlog/GNUmakefile index a0e1f1a3..0b89f268 100644 --- a/sope-appserver/samples/CoreDataBlog/GNUmakefile +++ b/sope-appserver/samples/CoreDataBlog/GNUmakefile @@ -11,6 +11,7 @@ CoreDataBlog_OBJC_FILES += \ EOFetchSpecification+CoreData.m \ EOQualifier+CoreData.m \ EOSortOrdering+CoreData.m \ + EOCoreDataSource.m \ \ CoreDataBlog.m \ Session.m \