From: helge Date: Mon, 1 Aug 2005 20:56:49 +0000 (+0000) Subject: started CoreData demo X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=947a6c475afb3c95722d9acbc0fc8a1e6820d7a2;p=sope started CoreData demo git-svn-id: http://svn.opengroupware.org/SOPE/trunk@956 e4a50df8-12e2-0310-a44c-efbce7f8a7e3 --- diff --git a/sope-appserver/samples/CoreDataBlog/BlogDemo_DataModel.xcdatamodel/elements b/sope-appserver/samples/CoreDataBlog/BlogDemo_DataModel.xcdatamodel/elements new file mode 100644 index 00000000..7b9099d4 Binary files /dev/null and b/sope-appserver/samples/CoreDataBlog/BlogDemo_DataModel.xcdatamodel/elements differ diff --git a/sope-appserver/samples/CoreDataBlog/BlogDemo_DataModel.xcdatamodel/layout b/sope-appserver/samples/CoreDataBlog/BlogDemo_DataModel.xcdatamodel/layout new file mode 100644 index 00000000..3a70bb84 Binary files /dev/null and b/sope-appserver/samples/CoreDataBlog/BlogDemo_DataModel.xcdatamodel/layout differ diff --git a/sope-appserver/samples/CoreDataBlog/ChangeLog b/sope-appserver/samples/CoreDataBlog/ChangeLog new file mode 100644 index 00000000..7aeb8c50 --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/ChangeLog @@ -0,0 +1,3 @@ +2005-08-01 Helge Hess + + * started CoreData demo diff --git a/sope-appserver/samples/CoreDataBlog/CoreDataBlog.conf b/sope-appserver/samples/CoreDataBlog/CoreDataBlog.conf new file mode 100644 index 00000000..f7bad532 --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/CoreDataBlog.conf @@ -0,0 +1,12 @@ +# SLog config + + +SetHandler ngobjweb-adaptor +SetAppPort 20000 + + + +SetHandler ngobjweb-adaptor +SetAppPort 20000 + + diff --git a/sope-appserver/samples/CoreDataBlog/CoreDataBlog.m b/sope-appserver/samples/CoreDataBlog/CoreDataBlog.m new file mode 100644 index 00000000..6a4a2de0 --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/CoreDataBlog.m @@ -0,0 +1,88 @@ +/* + Copyright (C) 2005 SKYRIX Software AG + Copyright (C) 2005 by Scott Stevenson + + 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 "WOCoreDataApplication.h" + +@interface CoreDataBlog : WOCoreDataApplication +{ +} + +@end + +#include "common.h" + +@implementation CoreDataBlog + +- (id)init { + if ((self = [super init]) != nil) { + [self logWithFormat:@"Store coordinator: %@", + [self defaultPersistentStoreType]]; + } + return self; +} + +- (NSString *)applicationSupportFolder { + // TODO: simplify that + unsigned char path[1024]; + NSString *p; + FSRef foundRef; + OSErr err; + + err = FSFindFolder(kUserDomain, kApplicationSupportFolderType, + kDontCreateFolder, &foundRef); + if (err != noErr) { + [self errorWithFormat:@"Can't find application support folder."]; + return nil; + } + + FSRefMakePath(&foundRef, path, sizeof(path)); + p = [NSString stringWithUTF8String:(char *)path]; // TODO: is UTF-8 ok? + p = [p stringByAppendingPathComponent:@"BlogDemo"]; + return p; +} + +- (NSURL *)defaultPersistentStoreURL { + NSString *s; + + s = @"BlogDemo.xml"; + s = [[self applicationSupportFolder] stringByAppendingPathComponent:s]; + return [NSURL fileURLWithPath:s]; +} + +@end /* CoreDataBlog */ + + +/* starting the app */ + +int main(int argc, char **argv, char **env) { + NSAutoreleasePool *pool; + + pool = [[NSAutoreleasePool alloc] init]; +#if LIB_FOUNDATION_LIBRARY + [NSProcessInfo initializeWithArguments:argv count:argc environment:env]; +#endif + + WOApplicationMain(@"CoreDataBlog", argc, (void*)argv); + + [pool release]; + return 0; +} diff --git a/sope-appserver/samples/CoreDataBlog/GNUmakefile b/sope-appserver/samples/CoreDataBlog/GNUmakefile new file mode 100644 index 00000000..5b1cb2d3 --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/GNUmakefile @@ -0,0 +1,23 @@ +# GNUstep makefile + +-include ../../../config.make +include $(GNUSTEP_MAKEFILES)/common.make + +WOAPP_NAME = CoreDataBlog + +CoreDataBlog_OBJC_FILES += \ + WOCoreDataApplication.m \ + WOSession+CoreData.m \ + \ + CoreDataBlog.m \ + Session.m \ + Main.m + +CoreDataBlog_COMPONENTS += Main.wo + +CoreDataBlog_COREDATA_MODELS += \ + BlogDemo_DataModel.xcdatamodel + +-include GNUmakefile.preamble +include $(GNUSTEP_MAKEFILES)/woapp.make +-include GNUmakefile.postamble diff --git a/sope-appserver/samples/CoreDataBlog/GNUmakefile.postamble b/sope-appserver/samples/CoreDataBlog/GNUmakefile.postamble new file mode 100644 index 00000000..03379b1b --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/GNUmakefile.postamble @@ -0,0 +1,17 @@ +# compilation targets + +compile-coredata-models :: $($(WOAPP_NAME)_MOM_FILES) + +clean :: + rm -f $(patsubst %.xcdatamodel,%.mom,$($(WOAPP_NAME)_COREDATA_MODELS)) + + +# this is triggered by woapp.make +woapp-resource-files :: compile-coredata-models + + +MOMC="/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc" + +# rule for compilation of CoreData models +%.mom :: %.xcdatamodel + $(MOMC) $< $@ diff --git a/sope-appserver/samples/CoreDataBlog/GNUmakefile.preamble b/sope-appserver/samples/CoreDataBlog/GNUmakefile.preamble new file mode 100644 index 00000000..744528b7 --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/GNUmakefile.preamble @@ -0,0 +1,23 @@ +# compile settings + + +# CoreData files + +$(WOAPP_NAME)_MOM_FILES += \ + $(patsubst %.xcdatamodel,%.mom,$($(WOAPP_NAME)_COREDATA_MODELS)) + +$(WOAPP_NAME)_RESOURCE_FILES += \ + $($(WOAPP_NAME)_MOM_FILES) + + +# static references required for Mach linker + +ADDITIONAL_LDFLAGS += \ + -framework CoreData \ + -framework CoreServices + +ADDITIONAL_TOOL_LIBS += \ + -lEOControl \ + -lDOM -lXmlRpc -lSaxObjC + +ADDITIONAL_LIB_DIRS += -L/usr/local/lib -L/usr/lib diff --git a/sope-appserver/samples/CoreDataBlog/Main.m b/sope-appserver/samples/CoreDataBlog/Main.m new file mode 100644 index 00000000..1c4eef72 --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/Main.m @@ -0,0 +1,49 @@ +/* + 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 + +@class NSArray; + +@interface Main : WOComponent +{ +} + +@end + +#include "common.h" + +@implementation Main + +- (id)initWithContext:(id)_ctx { + if ((self = [super initWithContext:_ctx])) { + } + return self; +} + +- (void)dealloc { + [super dealloc]; +} + +/* accessors */ + + +@end /* Main */ diff --git a/sope-appserver/samples/CoreDataBlog/Main.wo/Main.html b/sope-appserver/samples/CoreDataBlog/Main.wo/Main.html new file mode 100644 index 00000000..7632fe12 --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/Main.wo/Main.html @@ -0,0 +1,25 @@ + + + + SOPE CoreData Test + + + +

SOPE CoreData Test

+ +Ctx: <#Test/>
+ +
+ <#Entities> +
  • <#Entity/>
  • + +
    + <#Posts> +
  • <#Post/>
  • + + +
    +
    Helge Hess
    + + diff --git a/sope-appserver/samples/CoreDataBlog/Main.wo/Main.wod b/sope-appserver/samples/CoreDataBlog/Main.wo/Main.wod new file mode 100644 index 00000000..43417408 --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/Main.wo/Main.wod @@ -0,0 +1,24 @@ +/* bind template to controller */ + +Test: WOString { + value = session.defaultManagedObjectContext; +} + +Entities: WORepetition { + list = session.defaultManagedObjectContext.persistentStoreCoordinator.managedObjectModel.entities; + item = item; +} + +Entity: WOString { + value = item.name; +} + + +Posts: WORepetition { + list = session.posts; + item = item; +} + +Post: WOString { + value = item.title; +} diff --git a/sope-appserver/samples/CoreDataBlog/README b/sope-appserver/samples/CoreDataBlog/README new file mode 100644 index 00000000..8d3ebea8 --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/README @@ -0,0 +1,7 @@ +CoreDataBlog +============ + +A sample SOPE application which access some CoreData storage. + +Works on the same data like: + http://www.cocoadevcentral.com/articles/000085.php diff --git a/sope-appserver/samples/CoreDataBlog/Session.m b/sope-appserver/samples/CoreDataBlog/Session.m new file mode 100644 index 00000000..c6525470 --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/Session.m @@ -0,0 +1,53 @@ +/* + 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 + +@interface Session : WOSession +@end + +#include "WOSession+CoreData.h" +#include "WOCoreDataApplication.h" +#include "common.h" + +@implementation Session + +- (NSArray *)posts { + NSFetchRequest *fs; + NSArray *a; + NSError *error = nil; + + fs = [[[NSFetchRequest alloc] init] autorelease]; + + [fs setEntity:[[[[self application] managedObjectModel] + entitiesByName] objectForKey:@"Post"]]; + [self logWithFormat:@"entity: %@", [fs entity]]; + + a = [[self defaultManagedObjectContext] executeFetchRequest:fs error:&error]; + [self logWithFormat:@"a: %@", a]; + + if (error != nil) + [self errorWithFormat:@"FETCH FAILED: %@", error]; + + return a; +} + +@end /* Session */ diff --git a/sope-appserver/samples/CoreDataBlog/WOCoreDataApplication.h b/sope-appserver/samples/CoreDataBlog/WOCoreDataApplication.h new file mode 100644 index 00000000..a7ca683c --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/WOCoreDataApplication.h @@ -0,0 +1,54 @@ +/* + 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 __WOCoreDataApplication_H__ +#define __WOCoreDataApplication_H__ + +#include + +@class NSString, NSURL; +@class NSManagedObjectContext, NSManagedObjectModel; +@class NSPersistentStoreCoordinator; + +@interface WOCoreDataApplication : WOApplication +{ + NSManagedObjectModel *managedObjectModel; + NSPersistentStoreCoordinator *storeCoordinator; +} + +/* accessors */ + +- (NSManagedObjectModel *)managedObjectModel; +- (NSPersistentStoreCoordinator *)persistentStoreCoordinator; + +/* locating the default store */ + +- (NSString *)applicationSupportFolder; +- (NSURL *)defaultPersistentStoreURL; +- (NSString *)defaultPersistentStoreType; + +/* creating editing contexts */ + +- (NSManagedObjectContext *)createManagedObjectContext; + +@end + +#endif /* __WOCoreDataApplication_H__ */ diff --git a/sope-appserver/samples/CoreDataBlog/WOCoreDataApplication.m b/sope-appserver/samples/CoreDataBlog/WOCoreDataApplication.m new file mode 100644 index 00000000..ace3fd94 --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/WOCoreDataApplication.m @@ -0,0 +1,142 @@ +/* + 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 "WOCoreDataApplication.h" +#include "common.h" + +@implementation WOCoreDataApplication + +- (void)dealloc { + [self->storeCoordinator release]; + [self->managedObjectModel release]; + [super dealloc]; +} + +/* accessors */ + +- (NSManagedObjectModel *)managedObjectModel { + NSMutableSet *allBundles; + + if (self->managedObjectModel != nil) + return [self->managedObjectModel isNotNull] ? self->managedObjectModel:nil; + + allBundles = [[NSMutableSet alloc] initWithCapacity:16]; + [allBundles addObject:[NSBundle mainBundle]]; + [allBundles addObject:[NSBundle allFrameworks]]; + + /* 'nil' says: just scan the mainbundle */ + self->managedObjectModel = + [[NSManagedObjectModel mergedModelFromBundles:nil] retain]; + + [allBundles release]; allBundles = nil; + + if (self->managedObjectModel == nil) + [self errorWithFormat:@"Could not create the managed object model!"]; + + return self->managedObjectModel; +} + +- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { + NSFileManager *fileManager; + NSError *error; + BOOL ok; + + if (self->storeCoordinator != nil) + return [self->storeCoordinator isNotNull] ? self->storeCoordinator : nil; + + /* create support folder */ + + fileManager = [NSFileManager defaultManager]; + if (![fileManager fileExistsAtPath:[self applicationSupportFolder] + isDirectory:NULL] ) { + [fileManager createDirectoryAtPath:[self applicationSupportFolder] + attributes:nil]; + } + + /* create store coordinator */ + + self->storeCoordinator = + [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: + [self managedObjectModel]]; + + ok = [self->storeCoordinator + addPersistentStoreWithType:[self defaultPersistentStoreType] + configuration:nil + URL:[self defaultPersistentStoreURL] + options:nil error:&error] != nil ? YES : NO; + if (!ok) { + [self errorWithFormat: + @"Failed to create persistent store coordinator: %@", error]; + + [self->storeCoordinator release]; self->storeCoordinator = nil; + + self->storeCoordinator = [[NSNull null] retain]; + return nil; + } + + return self->storeCoordinator; +} + +/* locating the default store */ + +- (NSString *)applicationSupportFolder { + unsigned char path[1024]; + NSString *p; + FSRef foundRef; + OSErr err; + + err = FSFindFolder(kUserDomain, kApplicationSupportFolderType, + kDontCreateFolder, &foundRef); + if (err != noErr) { + [self errorWithFormat:@"Can't find application support folder."]; + return nil; + } + + FSRefMakePath(&foundRef, path, sizeof(path)); + p = [NSString stringWithUTF8String:(char *)path]; // TODO: is UTF-8 ok? + p = [p stringByAppendingPathComponent:[self name]]; + return p; +} + +- (NSURL *)defaultPersistentStoreURL { + NSString *s; + + s = [[self name] stringByDeletingPathExtension]; + s = [s stringByAppendingPathExtension:@"xml"]; + s = [[self applicationSupportFolder] stringByAppendingPathComponent:s]; + return [NSURL fileURLWithPath:s]; +} +- (NSString *)defaultPersistentStoreType { + return [[[self defaultPersistentStoreURL] path] hasSuffix:@".xml"] + ? NSXMLStoreType : NSSQLiteStoreType; +} + +/* creating editing contexts */ + +- (NSManagedObjectContext *)createManagedObjectContext { + NSManagedObjectContext *ctx; + + ctx = [[[NSManagedObjectContext alloc] init] autorelease]; + [ctx setPersistentStoreCoordinator:[self persistentStoreCoordinator]]; + return ctx; +} + +@end /* WOCoreDataApplication */ diff --git a/sope-appserver/samples/CoreDataBlog/WOSession+CoreData.h b/sope-appserver/samples/CoreDataBlog/WOSession+CoreData.h new file mode 100644 index 00000000..900611c6 --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/WOSession+CoreData.h @@ -0,0 +1,36 @@ +/* + 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 __WOSession_CoreData_H__ +#define __WOSession_CoreData_H__ + +#include + +@class NSManagedObjectContext; + +@interface WOSession(CoreData) + +- (void)setDefaultManagedObjectContext:(NSManagedObjectContext *)_ctx; +- (NSManagedObjectContext *)defaultManagedObjectContext; + +@end + +#endif /* __WOSession_CoreData_H__ */ diff --git a/sope-appserver/samples/CoreDataBlog/WOSession+CoreData.m b/sope-appserver/samples/CoreDataBlog/WOSession+CoreData.m new file mode 100644 index 00000000..5a156da6 --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/WOSession+CoreData.m @@ -0,0 +1,54 @@ +/* + 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 "WOSession+CoreData.h" +#include "WOCoreDataApplication.h" +#include "common.h" + +@implementation WOSession(CoreData) + +static NSString *NSManagedObjectContextKey = @"NSManagedObjectContext"; + +- (void)setDefaultManagedObjectContext:(NSManagedObjectContext *)_ctx { + if ([_ctx isNotNull]) + [self setObject:_ctx forKey:NSManagedObjectContextKey]; + else + [self removeObjectForKey:NSManagedObjectContextKey]; +} + +- (NSManagedObjectContext *)defaultManagedObjectContext { + NSManagedObjectContext *ctx; + + if ([(ctx = [self objectForKey:NSManagedObjectContextKey]) isNotNull]) + return ctx; + + if ((ctx = [[self application] createManagedObjectContext]) == nil) { + [self errorWithFormat: + @"Failed to create NSManagedObjectContext for session!"]; + return nil; + } + + [self setObject:ctx forKey:NSManagedObjectContextKey]; + return ctx; +} + +@end /* WOSession(CoreData) */ + diff --git a/sope-appserver/samples/CoreDataBlog/common.h b/sope-appserver/samples/CoreDataBlog/common.h new file mode 100644 index 00000000..5dd9b664 --- /dev/null +++ b/sope-appserver/samples/CoreDataBlog/common.h @@ -0,0 +1,5 @@ + +#import +#include + +#import