2006-06-15 21:34:10 +02:00
|
|
|
/*
|
|
|
|
Copyright (C) 2005 SKYRIX Software AG
|
|
|
|
|
|
|
|
This file is part of OpenGroupware.org.
|
|
|
|
|
|
|
|
OGo 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.
|
|
|
|
|
|
|
|
OGo 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 OGo; see the file COPYING. If not, write to the
|
|
|
|
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
#import <Foundation/NSValue.h>
|
|
|
|
#import <GDLContentStore/GCSChannelManager.h>
|
|
|
|
#import <GDLContentStore/NSURL+GCS.h>
|
|
|
|
#import <GDLAccess/EOAdaptorChannel.h>
|
|
|
|
#import <GDLAccess/EOAdaptorContext.h>
|
|
|
|
#import <GDLAccess/EOAttribute.h>
|
|
|
|
|
|
|
|
#import "common.h"
|
|
|
|
#import "AgenorUserDefaults.h"
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
@implementation AgenorUserDefaults
|
|
|
|
|
|
|
|
static NSString *uidColumnName = @"uid";
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (id) initWithTableURL: (NSURL *) tableURL
|
|
|
|
uid: (NSString *) userID
|
|
|
|
fieldName: (NSString *) defaultsFieldName
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
if (tableURL && [userID length] > 0
|
|
|
|
&& [defaultsFieldName length] > 0)
|
|
|
|
{
|
|
|
|
parent = [[NSUserDefaults standardUserDefaults] retain];
|
|
|
|
fieldName = [defaultsFieldName copy];
|
|
|
|
url = [tableURL copy];
|
|
|
|
uid = [userID copy];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self errorWithFormat: @"missing arguments"];
|
|
|
|
[self release];
|
|
|
|
self = nil;
|
|
|
|
}
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
2007-03-27 20:12:02 +02:00
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
[self release];
|
|
|
|
|
|
|
|
return nil;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[values release];
|
|
|
|
[lastFetch release];
|
|
|
|
[parent release];
|
|
|
|
[url release];
|
|
|
|
[uid release];
|
|
|
|
[fieldName release];
|
2006-06-15 21:34:10 +02:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* accessors */
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (NSURL *) tableURL
|
|
|
|
{
|
|
|
|
return url;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (NSString *) uid
|
|
|
|
{
|
|
|
|
return uid;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (NSString *) fieldName
|
|
|
|
{
|
|
|
|
return fieldName;
|
|
|
|
}
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (NSUserDefaults *) parentDefaults
|
|
|
|
{
|
|
|
|
return parent;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
/* operation */
|
|
|
|
|
|
|
|
- (BOOL) primaryFetchProfile
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
GCSChannelManager *cm;
|
2007-03-27 20:12:02 +02:00
|
|
|
EOAdaptorChannel *channel;
|
2007-05-03 17:12:51 +02:00
|
|
|
NSDictionary *row, *oldValues;
|
2007-03-27 20:12:02 +02:00
|
|
|
NSException *ex;
|
|
|
|
NSString *sql;
|
|
|
|
NSArray *attrs;
|
|
|
|
BOOL rc;
|
2007-05-10 17:52:45 +02:00
|
|
|
id column;
|
2007-03-27 20:12:02 +02:00
|
|
|
|
|
|
|
rc = NO;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
cm = [GCSChannelManager defaultChannelManager];
|
2007-03-27 20:12:02 +02:00
|
|
|
channel = [cm acquireOpenChannelForURL: [self tableURL]];
|
|
|
|
if (channel)
|
|
|
|
{
|
|
|
|
/* generate SQL */
|
|
|
|
sql = [NSString stringWithFormat: (@"SELECT %@"
|
|
|
|
@" FROM %@"
|
|
|
|
@" WHERE %@ = '%@'"),
|
|
|
|
fieldName, [[self tableURL] gcsTableName],
|
|
|
|
uidColumnName, [self uid]];
|
2007-05-03 17:12:51 +02:00
|
|
|
|
|
|
|
[values release];
|
|
|
|
values = [NSMutableDictionary new];
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
/* run SQL */
|
|
|
|
|
|
|
|
ex = [channel evaluateExpressionX: sql];
|
|
|
|
if (ex)
|
2007-05-03 17:12:51 +02:00
|
|
|
[self errorWithFormat:@"could not run SQL '%@': %@", sql, ex];
|
2007-03-27 20:12:02 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* fetch schema */
|
|
|
|
attrs = [channel describeResults: NO /* don't beautify */];
|
|
|
|
|
|
|
|
/* fetch values */
|
|
|
|
row = [channel fetchAttributes: attrs withZone: NULL];
|
|
|
|
defFlags.isNew = (row == nil);
|
|
|
|
[channel cancelFetch];
|
|
|
|
|
|
|
|
/* remember values */
|
2007-05-10 17:52:45 +02:00
|
|
|
column = [row objectForKey: fieldName];
|
|
|
|
if ([column isNotNull])
|
|
|
|
{
|
|
|
|
oldValues = [column propertyList];
|
|
|
|
[values setDictionary: oldValues];
|
|
|
|
}
|
2007-05-03 17:12:51 +02:00
|
|
|
|
|
|
|
ASSIGN (lastFetch, [NSCalendarDate date]);
|
2007-03-27 20:12:02 +02:00
|
|
|
defFlags.modified = NO;
|
|
|
|
rc = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
[cm releaseChannel:channel];
|
|
|
|
}
|
|
|
|
else
|
2006-06-15 21:34:10 +02:00
|
|
|
[self errorWithFormat:@"failed to acquire channel for URL: %@",
|
|
|
|
[self tableURL]];
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
return rc;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (NSString *) generateSQLForInsert
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSMutableString *sql;
|
2007-03-29 21:13:27 +02:00
|
|
|
NSString *serializedDefaults;
|
2007-03-29 20:28:57 +02:00
|
|
|
|
|
|
|
#if LIB_FOUNDATION_LIBRARY
|
2007-03-29 21:04:57 +02:00
|
|
|
serializedDefaults = [values stringRepresentation];
|
|
|
|
|
|
|
|
sql = [NSString stringWithFormat: (@"INSERT INTO %@"
|
|
|
|
@" (%@, %@)"
|
|
|
|
@" VALUES ('%@', '%@')"),
|
|
|
|
[[self tableURL] gcsTableName], uidColumnName, fieldName,
|
|
|
|
[self uid],
|
|
|
|
[serializedDefaults stringByReplacingString:@"'" withString:@"''"]];
|
2007-03-29 20:28:57 +02:00
|
|
|
#else
|
2007-03-27 20:12:02 +02:00
|
|
|
NSData *serializedDefaultsData;
|
2007-03-29 21:13:27 +02:00
|
|
|
NSString *error;
|
2007-03-27 20:12:02 +02:00
|
|
|
|
|
|
|
serializedDefaultsData
|
|
|
|
= [NSPropertyListSerialization dataFromPropertyList: values
|
|
|
|
format: NSPropertyListOpenStepFormat
|
|
|
|
errorDescription: &error];
|
2007-03-29 20:28:57 +02:00
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
if (error)
|
|
|
|
sql = nil;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
serializedDefaults = [[NSString alloc] initWithData: serializedDefaultsData
|
|
|
|
encoding: NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
sql = [NSString stringWithFormat: (@"INSERT INTO %@"
|
|
|
|
@" (%@, %@)"
|
|
|
|
@" VALUES ('%@', '%@')"),
|
|
|
|
[[self tableURL] gcsTableName], uidColumnName, fieldName,
|
|
|
|
[self uid],
|
|
|
|
[serializedDefaults stringByReplacingString:@"'" withString:@"''"]];
|
|
|
|
[serializedDefaults release];
|
|
|
|
}
|
2007-03-29 21:04:57 +02:00
|
|
|
#endif
|
2007-03-27 20:12:02 +02:00
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return sql;
|
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (NSString *) generateSQLForUpdate
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
NSMutableString *sql;
|
2007-03-29 21:13:27 +02:00
|
|
|
NSString *serializedDefaults;
|
2007-03-29 20:28:57 +02:00
|
|
|
|
|
|
|
#if LIB_FOUNDATION_LIBRARY
|
2007-03-29 21:04:57 +02:00
|
|
|
serializedDefaults = [values stringRepresentation];
|
|
|
|
|
|
|
|
sql = [NSString stringWithFormat: (@"UPDATE %@"
|
|
|
|
@" SET %@ = '%@'"
|
|
|
|
@" WHERE %@ = '%@'"),
|
|
|
|
[[self tableURL] gcsTableName],
|
|
|
|
fieldName,
|
|
|
|
[serializedDefaults stringByReplacingString:@"'" withString:@"''"],
|
|
|
|
uidColumnName, [self uid]];
|
2007-03-29 20:28:57 +02:00
|
|
|
#else
|
2007-03-27 20:12:02 +02:00
|
|
|
NSData *serializedDefaultsData;
|
2007-03-29 21:13:27 +02:00
|
|
|
NSString *error;
|
2007-03-27 20:12:02 +02:00
|
|
|
|
|
|
|
serializedDefaultsData
|
|
|
|
= [NSPropertyListSerialization dataFromPropertyList: values
|
|
|
|
format: NSPropertyListOpenStepFormat
|
|
|
|
errorDescription: &error];
|
2007-03-29 20:28:57 +02:00
|
|
|
error = nil;
|
2007-03-27 20:12:02 +02:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
sql = nil;
|
|
|
|
[error release];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
serializedDefaults = [[NSString alloc] initWithData: serializedDefaultsData
|
|
|
|
encoding: NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
sql = [NSString stringWithFormat: (@"UPDATE %@"
|
|
|
|
@" SET %@ = '%@'"
|
|
|
|
@" WHERE %@ = '%@'"),
|
|
|
|
[[self tableURL] gcsTableName],
|
|
|
|
fieldName,
|
|
|
|
[serializedDefaults stringByReplacingString:@"'" withString:@"''"],
|
|
|
|
uidColumnName, [self uid]];
|
|
|
|
[serializedDefaults release];
|
|
|
|
}
|
2007-03-29 21:04:57 +02:00
|
|
|
#endif
|
2007-03-27 20:12:02 +02:00
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
return sql;
|
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (BOOL) primaryStoreProfile
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
GCSChannelManager *cm;
|
2007-03-27 20:12:02 +02:00
|
|
|
EOAdaptorChannel *channel;
|
|
|
|
NSException *ex;
|
|
|
|
NSString *sql;
|
|
|
|
BOOL rc;
|
|
|
|
|
|
|
|
rc = NO;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
cm = [GCSChannelManager defaultChannelManager];
|
2007-03-27 20:12:02 +02:00
|
|
|
sql = ((defFlags.isNew)
|
|
|
|
? [self generateSQLForInsert]
|
|
|
|
: [self generateSQLForUpdate]);
|
|
|
|
if (sql)
|
|
|
|
{
|
|
|
|
channel = [cm acquireOpenChannelForURL: [self tableURL]];
|
|
|
|
if (channel)
|
|
|
|
{
|
|
|
|
ex = [channel evaluateExpressionX:sql];
|
|
|
|
if (ex)
|
|
|
|
[self errorWithFormat: @"could not run SQL '%@': %@", sql, ex];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ([[channel adaptorContext] hasOpenTransaction])
|
|
|
|
{
|
|
|
|
ex = [channel evaluateExpressionX: @"COMMIT TRANSACTION"];
|
|
|
|
if (ex)
|
|
|
|
[self errorWithFormat:@"could not commit transaction for update: %@", ex];
|
|
|
|
else
|
|
|
|
rc = YES;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rc = YES;
|
|
|
|
|
|
|
|
defFlags.modified = NO;
|
|
|
|
defFlags.isNew = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
[cm releaseChannel: channel];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"failed to acquire channel for URL: %@",
|
|
|
|
[self tableURL]];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[self errorWithFormat: @"failed to generate SQL for storing defaults"];
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
return rc;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (BOOL) fetchProfile
|
|
|
|
{
|
|
|
|
return (values || [self primaryFetchProfile]);
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* value access */
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (void) setObject: (id) value
|
|
|
|
forKey: (NSString *) key
|
|
|
|
{
|
|
|
|
id old;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
if (![self fetchProfile])
|
2006-06-15 21:34:10 +02:00
|
|
|
return;
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
/* check whether the value is actually modified */
|
|
|
|
if (!defFlags.modified)
|
|
|
|
{
|
|
|
|
old = [values objectForKey: key];
|
|
|
|
if (old == value || [old isEqual: value]) /* value didn't change */
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* we need to this because our typed accessors convert to strings */
|
|
|
|
// TODO: especially problematic with bools
|
|
|
|
if ([value isKindOfClass: [NSString class]]) {
|
|
|
|
if (![old isKindOfClass: [NSString class]])
|
|
|
|
if ([[old description] isEqualToString: value])
|
|
|
|
return;
|
|
|
|
}
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
2007-03-27 20:12:02 +02:00
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
/* set in hash and mark as modified */
|
2007-03-27 20:12:02 +02:00
|
|
|
[values setObject: value forKey: key];
|
|
|
|
|
|
|
|
defFlags.modified = YES;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (id) objectForKey: (NSString *) key
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
id value;
|
|
|
|
|
|
|
|
if (![self fetchProfile])
|
2007-03-27 20:12:02 +02:00
|
|
|
value = nil;
|
|
|
|
else
|
|
|
|
value = [values objectForKey: key];
|
|
|
|
|
|
|
|
return value;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (void) removeObjectForKey: (NSString *) key
|
|
|
|
{
|
|
|
|
[self setObject: nil forKey: key];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* saving changes */
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (BOOL) synchronize
|
|
|
|
{
|
|
|
|
// if (!defFlags.modified) /* was not modified */
|
|
|
|
// return YES;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
/* ensure fetched data (more or less guaranteed by modified!=0) */
|
|
|
|
if (![self fetchProfile])
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
/* store */
|
2007-03-27 20:12:02 +02:00
|
|
|
if (![self primaryStoreProfile])
|
|
|
|
{
|
|
|
|
[self primaryFetchProfile];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2006-06-15 21:34:10 +02:00
|
|
|
/* refetch */
|
|
|
|
return [self primaryFetchProfile];
|
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (void) flush
|
|
|
|
{
|
|
|
|
[values release];
|
|
|
|
[lastFetch release];
|
|
|
|
values = nil;
|
|
|
|
lastFetch = nil;
|
|
|
|
defFlags.modified = NO;
|
|
|
|
defFlags.isNew = NO;
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* typed accessors */
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (NSArray *) arrayForKey: (NSString *) key
|
|
|
|
{
|
|
|
|
return [self objectForKey: key];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (NSDictionary *) dictionaryForKey: (NSString *) key
|
|
|
|
{
|
|
|
|
return [self objectForKey: key];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (NSData *) dataForKey: (NSString *) key
|
|
|
|
{
|
|
|
|
return [self objectForKey: key];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (NSString *) stringForKey: (NSString *) key
|
|
|
|
{
|
|
|
|
return [self objectForKey: key];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (BOOL) boolForKey: (NSString *) key
|
|
|
|
{
|
|
|
|
return [[self objectForKey: key] boolValue];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (float) floatForKey: (NSString *) key
|
|
|
|
{
|
|
|
|
return [[self objectForKey: key] floatValue];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (int) integerForKey: (NSString *) key
|
|
|
|
{
|
|
|
|
return [[self objectForKey: key] intValue];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (void) setBool: (BOOL) value
|
|
|
|
forKey: (NSString *) key
|
|
|
|
{
|
2006-06-15 21:34:10 +02:00
|
|
|
// TODO: need special support here for int-DB fields
|
2007-03-27 20:12:02 +02:00
|
|
|
[self setObject: [NSNumber numberWithBool: value]
|
|
|
|
forKey: key];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (void) setFloat: (float) value
|
|
|
|
forKey: (NSString *) key
|
|
|
|
{
|
|
|
|
[self setObject: [NSNumber numberWithFloat: value]
|
|
|
|
forKey: key];
|
|
|
|
}
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-03-27 20:12:02 +02:00
|
|
|
- (void) setInteger: (int) value
|
|
|
|
forKey: (NSString *) key
|
|
|
|
{
|
|
|
|
[self setObject: [NSNumber numberWithInt: value]
|
|
|
|
forKey: key];
|
2006-06-15 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end /* AgenorUserDefaults */
|