"Fossies" - the Fresh Open Source Software Archive

Member "opengroupware-5.5rc3/Logic/LSFoundation/LSTypeManager.m" (5 Dec 2015, 8250 Bytes) of package /linux/privat/opengroupware-5.5rc3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Matlab source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "LSTypeManager.m" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.5rc2_vs_5.5rc3.

    1 /*
    2   Copyright (C) 2000-2005 SKYRIX Software AG
    3 
    4   This file is part of OpenGroupware.org.
    5 
    6   OGo is free software; you can redistribute it and/or modify it under
    7   the terms of the GNU Lesser General Public License as published by the
    8   Free Software Foundation; either version 2, or (at your option) any
    9   later version.
   10 
   11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
   12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
   14   License for more details.
   15 
   16   You should have received a copy of the GNU Lesser General Public
   17   License along with OGo; see the file COPYING.  If not, write to the
   18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
   19   02111-1307, USA.
   20 */
   21 
   22 #include "common.h"
   23 #include <LSFoundation/LSTypeManager.h>
   24 #include <LSFoundation/LSCommandKeys.h>
   25 #include <LSFoundation/LSCommandContext.h>
   26 
   27 @interface LSTypeManager : NSObject < LSTypeManager >
   28 {
   29 @private
   30   LSCommandContext *context; /* non-retained */
   31   NSMapTable *pkeyToGid;
   32 }
   33 
   34 - (id)initWithContext:(LSCommandContext *)_ctx;
   35 - (void)invalidate;
   36 
   37 @end
   38 
   39 @interface NSObject(Misc)
   40 - (EOGlobalID *)globalID;
   41 @end
   42 
   43 @implementation LSTypeManager
   44 
   45 - (id)initWithContext:(LSCommandContext *)_ctx {
   46   self->context = _ctx;
   47   self->pkeyToGid = NSCreateMapTable(NSIntMapKeyCallBacks,
   48                                      NSObjectMapValueCallBacks,
   49                                      1000);
   50   return self;
   51 }
   52 
   53 - (void)invalidate {
   54   self->context = nil;
   55   if (self->pkeyToGid) {
   56     NSFreeMapTable(self->pkeyToGid);
   57     self->pkeyToGid = NULL;
   58   }
   59 }
   60 
   61 - (void)dealloc {
   62   [self invalidate];
   63   [super dealloc];
   64 }
   65 
   66 /* entity names */
   67 
   68 - (NSArray *)entityNamesForObjects:(NSArray *)_objects {
   69   NSArray  *array;
   70   unsigned oc;
   71 
   72   array = nil;
   73   if ((oc = [_objects count])) {
   74     NSString *names[oc];
   75     unsigned i;
   76 
   77     for (i = 0; i < oc; i++) {
   78       id eo;
   79 
   80       eo       = [_objects objectAtIndex:i];
   81       names[i] = [self entityNameForObject:eo];
   82     }
   83     array = [NSArray arrayWithObjects:names count:oc];
   84   }
   85   return array;
   86 }
   87 - (NSArray *)entityNamesForGlobalIDs:(NSArray *)_gids {
   88   NSArray  *array;
   89   unsigned oc;
   90 
   91   array = nil;
   92   if ((oc = [_gids count])) {
   93     NSString *names[oc];
   94     unsigned i;
   95 
   96     for (i = 0; i < oc; i++) {
   97       EOGlobalID *gid;
   98 
   99       gid      = [_gids objectAtIndex:i];
  100       names[i] = [self entityNameForGlobalID:gid];
  101     }
  102     array = [NSArray arrayWithObjects:names count:oc];
  103   }
  104   return array;
  105 }
  106 
  107 - (NSString *)entityNameForObject:(id)_object {
  108   if (_object == nil)
  109     return nil;
  110   
  111   if ([_object respondsToSelector:@selector(entityName)])
  112     return [_object entityName];
  113   
  114   if ([_object respondsToSelector:@selector(globalID)]) {
  115     EOGlobalID *gid;
  116     
  117     gid = [_object globalID];
  118     return [self entityNameForGlobalID:gid];
  119   }
  120   return nil;
  121 }
  122 - (NSString *)entityNameForGlobalID:(EOGlobalID *)_globalId {
  123   static Class EOKeyGlobalIDClass = Nil;
  124   if (EOKeyGlobalIDClass == Nil) EOKeyGlobalIDClass = [EOKeyGlobalID class];
  125 
  126   if (_globalId == nil)
  127     return nil;
  128 
  129   if ([_globalId respondsToSelector:@selector(entityName)])
  130     return [(id)_globalId entityName];
  131   
  132   return nil;
  133 }
  134 
  135 /* primary keys */
  136 
  137 - (NSArray *)globalIDsForObjects:(NSArray *)_objects {
  138   unsigned oc;
  139   NSArray *array;
  140   
  141   array = nil;
  142   if ((oc = [_objects count]) > 0) {
  143     EOGlobalID *gids[oc];
  144     unsigned   i;
  145 
  146     for (i = 0; i < oc; i++) {
  147       id eo;
  148 
  149       gids[i] = nil;
  150       
  151       eo = [_objects objectAtIndex:i];
  152       
  153       if ([eo respondsToSelector:@selector(globalID)])
  154         gids[i] = [eo globalID];
  155     }
  156     array = [NSArray arrayWithObjects:gids count:oc];
  157   }
  158   return array;
  159 }
  160 
  161 - (NSDictionary *)objectsKeyedByEntityName:(NSArray *)_objects {
  162   unsigned     oc;
  163   NSDictionary *dict;
  164   
  165   dict = nil;
  166   if ((oc = [_objects count]) > 0) {
  167     NSMutableDictionary *md;
  168     NSArray *entityNames;
  169     unsigned i;
  170     
  171     md          = [[NSMutableDictionary alloc] initWithCapacity:32];
  172     entityNames = [self entityNamesForObjects:_objects];
  173 
  174     for (i = 0; i < oc; i++) {
  175       NSString       *entityName;
  176       NSMutableArray *objects;
  177       id eo;
  178       
  179       entityName = [entityNames objectAtIndex:i];
  180       eo         = [_objects    objectAtIndex:i];
  181       
  182       if ((objects = [md objectForKey:entityName]) == nil) {
  183         objects = [[NSMutableArray alloc] initWithCapacity:64];
  184         [md setObject:objects forKey:entityName];
  185         [objects release];
  186       }
  187       
  188       [objects addObject:eo];
  189     }
  190     
  191     dict = [md copy];
  192     [md release];
  193   }
  194   return [dict autorelease];
  195 }
  196 
  197 - (NSDictionary *)globalIDsKeyedByEntityName:(NSArray *)_gids {
  198   NSMutableDictionary *md;
  199   NSArray *entityNames;
  200   unsigned i;
  201   unsigned     oc;
  202   NSDictionary *dict;
  203   
  204   dict = nil;
  205   if ((oc = [_gids count]) == 0)
  206     return nil;
  207     
  208   md          = [[NSMutableDictionary alloc] initWithCapacity:32];
  209   entityNames = [self entityNamesForGlobalIDs:_gids];
  210 
  211   for (i = 0; i < oc; i++) {
  212     NSString       *entityName;
  213     NSMutableArray *objects;
  214     EOGlobalID     *gid;
  215       
  216     entityName = [entityNames objectAtIndex:i];
  217     gid        = [_gids       objectAtIndex:i];
  218 
  219     if ((objects = [md objectForKey:entityName]) == nil) {
  220       objects = [[NSMutableArray alloc] initWithCapacity:64];
  221       [md setObject:objects forKey:entityName];
  222       [objects release];
  223     }
  224       
  225     [objects addObject:gid];
  226   }
  227   dict = [md copy];
  228   [md release];
  229   return [dict autorelease];
  230 }
  231 
  232 /* PrimaryKeyTypes */
  233 
  234 - (EOGlobalID *)globalIDForPrimaryKey:(id)_pkey {
  235   EOGlobalID    *gid;
  236   id            pkey;
  237   unsigned long pkeyInt;
  238   NSString      *entityName;
  239   
  240   pkeyInt = [_pkey unsignedLongValue];
  241   
  242   if (pkeyInt == 0) /* '0' as pkey is not allowed here */
  243     return nil;
  244 
  245   if ((gid = NSMapGet(self->pkeyToGid, (void*)pkeyInt)))
  246     return gid;
  247   
  248   pkey = [NSNumber numberWithInt:pkeyInt];
  249   
  250   entityName =
  251     [self->context runCommand:@"system::get-object-type", @"oid", pkey, nil];
  252   
  253   if (entityName == nil) {
  254     /* could not determine entity .. */
  255     return nil;
  256   }
  257 
  258   gid = [EOKeyGlobalID globalIDWithEntityName:entityName
  259                        keys:&pkey keyCount:1
  260                        zone:NULL];
  261   if (gid != nil) 
  262     NSMapInsert(self->pkeyToGid, (void*)pkeyInt, gid);
  263 
  264   return gid;
  265 }
  266 
  267 - (NSArray *)globalIDsForPrimaryKeys:(NSArray *)_pkeys {
  268   NSArray  *entityNames;
  269   unsigned i, pc = [_pkeys count];
  270   id       gids[pc + 1]; // TODO: replace with malloced array?
  271   
  272   if (pc == 0)
  273     return _pkeys;
  274 
  275   /* first check in cache */
  276   
  277   for (i = 0; i < pc; i++) {
  278     NSNumber      *pkey;
  279     unsigned long pkeyInt;
  280     
  281     pkey    = [_pkeys objectAtIndex:i];
  282     pkeyInt = [pkey intValue];
  283     
  284     gids[i] = NSMapGet(self->pkeyToGid, (void*)pkeyInt);
  285     if (gids[i] == nil)
  286       break;
  287   }
  288   if (i == pc) /* all found in cache*/
  289     return [NSArray arrayWithObjects:gids count:pc];
  290   
  291   /* then query entities */
  292   
  293   entityNames =
  294     [self->context runCommand:@"system::get-object-type", @"oids", _pkeys,nil];
  295   
  296   for (i = 0; i < pc; i++) {
  297     NSString      *eName;
  298     EOKeyGlobalID *gid;
  299     id            pkey;
  300     unsigned long pkeyInt;
  301     
  302     eName   = [entityNames objectAtIndex:i];
  303 
  304     if ([eName isNotNull]) {
  305       pkey    = [_pkeys objectAtIndex:i];
  306       pkeyInt = [pkey unsignedLongValue];
  307     
  308       gid = [EOKeyGlobalID globalIDWithEntityName:eName
  309                            keys:&pkey keyCount:1
  310                            zone:NULL];
  311       if (gid)
  312         NSMapInsert(self->pkeyToGid, (void *)pkeyInt, gid);
  313     
  314       gids[i] = gid;
  315       if (gids[i] == nil)
  316         gids[i] = [NSNull null];
  317     }
  318     else {
  319       gids[i] = [NSNull null];
  320     }
  321   }
  322   return [NSArray arrayWithObjects:gids count:pc];
  323 }
  324 
  325 /* description */
  326 
  327 - (NSString *)description {
  328   NSMutableString *ms;
  329 
  330   ms = [NSMutableString stringWithCapacity:128];
  331   [ms appendFormat:@"<0x%p[%@]:", self, NSStringFromClass([self class])];
  332 
  333   [ms appendFormat:@" ctx=0x%p", self->context];
  334   
  335   if (self->pkeyToGid != NULL)
  336     [ms appendFormat:@" #pkey-map=%"PRIuPTR, NSCountMapTable(self->pkeyToGid)];
  337   
  338   [ms appendString:@">"];
  339   return ms;
  340 }
  341 
  342 @end /* LSTypeManager */