--- /dev/null
+/*
+ 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 <EOControl/EODataSource.h>
+
+/*
+ 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__ */
--- /dev/null
+/*
+ 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 */