/* Copyright (C) 2000-2003 SKYRIX Software AG This file is part of OGo 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. */ // $Id$ #include "Session.h" #include "common.h" #include @implementation Session - (id)init { if ((self = [super init])) { [self setStoresIDsInCookies:NO]; } return self; } - (void)dealloc { [self->commandContext release]; [self->accountDS release]; [self->personDS release]; [self->appointmentDS release]; [self->enterpriseDS release]; [self->teamDS release]; [self->projectDS release]; [self->fileManagerCache release]; [super dealloc]; } /* accessors */ - (void)setCommandContext:(id)_ctx { ASSIGN(self->commandContext, _ctx); } - (id)commandContext { return self->commandContext; } /* notifications */ - (void)sleep { if ([self->commandContext isTransactionInProgress]) { if (![self->commandContext commit]) { [self logWithFormat:@"ERROR: could not commit transaction."]; [self->commandContext rollback]; } } [super sleep]; } /* datasource factories */ - (EODataSource *)_dataSourceWithClassName:(NSString *)_dsName { EODataSource *ds; Class clazz; if ((clazz = NSClassFromString(_dsName)) == nil) { [self logWithFormat:@"no datasource named '%@' ...", _dsName]; return nil; } ds = [[clazz alloc] initWithContext:[self commandContext]]; [self debugWithFormat:@"instantiated new datasource '%@' ...", _dsName]; return [ds autorelease]; } - (EODataSource *)personDataSource { if (self->personDS == nil) { self->personDS = [[self _dataSourceWithClassName:@"SkyPersonDataSource"] retain]; } return self->personDS; } - (EODataSource *)enterpriseDataSource { if (self->enterpriseDS == nil) { self->enterpriseDS = [[self _dataSourceWithClassName:@"SkyEnterpriseDataSource"] retain]; } return self->enterpriseDS; } - (EODataSource *)appointmentDataSource { if (self->appointmentDS == nil) { self->appointmentDS = [[self _dataSourceWithClassName:@"SkyAppointmentDataSource"] retain]; } return self->appointmentDS; } - (EODataSource *)accountDataSource { if (self->accountDS == nil) { self->accountDS = [[self _dataSourceWithClassName:@"SkyAccountDataSource"] retain]; } return self->accountDS; } - (EODataSource *)teamDataSource { if (self->teamDS == nil) { self->teamDS = [[self _dataSourceWithClassName:@"SkyTeamDataSource"] retain]; } return self->teamDS; } - (EODataSource *)projectDataSource { if (self->projectDS == nil) { self->projectDS = [[self _dataSourceWithClassName:@"SkyProjectDataSource"] retain]; } return self->projectDS; } /* filemanager factories */ - (Class)fileManagerClass { return NSClassFromString(@"SkyProjectFileManager"); } - (id)fileManagerForCode:(NSString *)_code { // TODO: cleanup method NGFileManager *fm; EOGlobalID *gid = nil; NSRange r; if (self->fileManagerCache == nil) self->fileManagerCache = [[NSMutableDictionary alloc] initWithCapacity:64]; if ((fm = [self->fileManagerCache objectForKey:_code])) return fm; r = [_code rangeOfString:@"://"]; if (r.length != 0) { gid = [[self->commandContext documentManager] globalIDForURL:_code]; } else { EOFetchSpecification *fspec = nil; EOQualifier *qualifier = nil; id pds = nil; id project = nil; NSArray *projects = nil; NSString *pid = nil; NSDictionary *hints; if (self->commandContext == nil) { [self logWithFormat:@"ERROR(%@): missing commandContext.", self]; return nil; } pid = _code; if (pid == nil || [pid length] == 0) { [self logWithFormat:@"ERROR: missing project number"]; return nil; } qualifier = [EOQualifier qualifierWithQualifierFormat:@"number=%@", pid]; hints = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:@"SearchAllProjects"]; fspec = [[EOFetchSpecification alloc] initWithEntityName:nil qualifier:qualifier sortOrderings:nil usesDistinct:YES isDeep:NO hints:hints]; pds = [self projectDataSource]; [pds setFetchSpecification:fspec]; projects = [pds fetchObjects]; if ([projects count] == 1) project = [projects objectAtIndex:0]; else project = nil; [fspec release]; fspec = nil; if (project == nil) { NSLog(@"ERROR[%s] missing project", __PRETTY_FUNCTION__); return nil; } gid = [project valueForKey:@"globalID"]; } if (gid == nil) { [self logWithFormat: @"ERROR(%s): found no global-id for project code: '%@'", __PRETTY_FUNCTION__, _code]; return nil; } return [OGoFileManagerFactory fileManagerInContext:self->commandContext forProjectGID:gid]; } @end /* Session */