/[cvs]/nfo/perl/libs/Data/Storage/Handler/Tangram.pm
ViewVC logotype

Diff of /nfo/perl/libs/Data/Storage/Handler/Tangram.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.30 by joko, Thu Mar 27 15:31:14 2003 UTC revision 1.45 by jonen, Sun Dec 14 01:48:36 2003 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.45  2003/12/14 01:48:36  jonen
7    #  small HACK at _insertChildNode: some special Childnodes should not be created because existing objects have to be selected!
8    #  TODO: make this more generic, e.g. implement a special flag at Schema
9    #
10    #  Revision 1.44  2003/12/04 01:01:50  joko
11    #  + sendQuery now returns result even on crud=UPDATE
12    #
13    #  Revision 1.43  2003/07/02 11:07:12  jonen
14    #  re-activate filtering of results *after* results are fetched from tangram
15    #    (needed for e.g. UserManagment)
16    #
17    #  Revision 1.42  2003/07/01 23:24:17  joko
18    #  now using package before using function
19    #
20    #  Revision 1.41  2003/06/29 02:03:45  joko
21    #  fix:? initialize schema on startup
22    #
23    #  Revision 1.40  2003/06/25 22:57:54  joko
24    #  major rework of "sub sendQuery / sub getListFiltered": now should be capable of "sorting"
25    #
26    #  Revision 1.39  2003/06/06 11:40:40  jonen
27    #  fixed bug at 'getFilteredList'
28    #
29    #  Revision 1.38  2003/05/13 16:38:38  joko
30    #  problems with "tied" on 5.6.1/win32
31    #
32    #  Revision 1.37  2003/05/10 17:37:39  jonen
33    #  corrected last commit
34    #
35    #  Revision 1.36  2003/05/10 17:31:18  jonen
36    #  + added new functions related to 'create' item
37    #   - createNode()
38    #     # creates non-persistent 'deep dummy filled' object
39    #   - insertChildNode()
40    #     # inserts child node at given parent (child node haven't to exists,
41    #         createNode will be injected transparently)
42    #
43    #  Revision 1.35  2003/04/19 16:09:48  jonen
44    #  + added operator dispatching (currently for getting ref-type) at 'getListFiltered'
45    #
46    #  Revision 1.34  2003/04/11 01:18:53  joko
47    #  sendQuery:
48    #  + introduced crud action 'DELETE'
49    #
50    #  Revision 1.33  2003/04/09 06:07:43  joko
51    #  revamped 'sub sendQuery'
52    #
53    #  Revision 1.32  2003/04/08 22:52:22  joko
54    #  modified 'querySchema': better behaviour regarding filtering result
55    #
56    #  Revision 1.31  2003/04/05 21:24:09  joko
57    #  modified 'sub getChildNodes': now contains code from 'querySchema'
58    #
59  #  Revision 1.30  2003/03/27 15:31:14  joko  #  Revision 1.30  2003/03/27 15:31:14  joko
60  #  fixes to modules regarding new namespace(s) below Data::Mungle::*  #  fixes to modules regarding new namespace(s) below Data::Mungle::*
61  #  #
# Line 127  use base ("Data::Storage::Handler::Abstr Line 180  use base ("Data::Storage::Handler::Abstr
180    
181  use Data::Dumper;  use Data::Dumper;
182  use Tangram;  use Tangram;
183    use Class::Tangram;
184    
185  use DesignPattern::Object;  use DesignPattern::Object;
186  use Data::Storage::Result::Tangram;  use Data::Storage::Result::Tangram;
187  use Data::Mungle::Compare::Struct qw( isEmpty );  use Data::Mungle::Compare::Struct qw( isEmpty );
188  use Data::Mungle::Transform::Deep qw( expand );  use Data::Mungle::Transform::Deep qw( expand deep_copy merge_to );
189    
190  # get logger instance  # get logger instance
191  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
192    
193    #$Tangram::TRACE = *STDOUT;
194    
195  # this holds the complete instantiated schema from tangram  # this holds the complete instantiated schema from tangram
196  my $schema_tangram;  my $schema_tangram;
# Line 154  sub _initSchema { Line 209  sub _initSchema {
209    #if (!$schema_tangram) {    #if (!$schema_tangram) {
210      #my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } );      #my $obj = getNewPerlObjectByPkgName($self->{locator}->{schema}, { EXPORT_OBJECTS => $self->{locator}->{classnames}, want_transactions => $self->{locator}->{want_transactions} } );
211      my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } );      my $obj = DesignPattern::Object->fromPackage($self->{locator}->{schema}, { 'EXPORT_OBJECTS' => $self->{locator}->{classnames}, 'want_transactions' => $self->{locator}->{want_transactions} } );
212      $schema_tangram = $obj->getSchema();      $schema_tangram = $obj->getSchema() if $obj;
213    #}    #}
214    if (!$schema_tangram) {    if (!$schema_tangram) {
215      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{schema}" );      $logger->error( __PACKAGE__ . "->_initSchema: No Schema available for $self->{locator}->{schema}." );
216      return 0;      return 0;
217    }    }
218    #$self->_patchSchema();    #$self->_patchSchema();
# Line 203  sub connect { Line 258  sub connect {
258  #      return;  #      return;
259  #    }  #    }
260    
261      #return unless $self->_initSchema();      return unless $self->_initSchema();
262      $self->_initSchema();      #$self->_initSchema();
263    
264      # create the main tangram storage object      # create the main tangram storage object
265      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );      #$self->{COREHANDLE} = Tangram::Relational->connect( $schema, $dsn );
# Line 231  sub connect { Line 286  sub connect {
286  sub getChildNodes {  sub getChildNodes {
287    
288    my $self = shift;    my $self = shift;
289    my @nodes;    my $mode = shift;
290      my $filter = shift;
291      
292      $mode ||= 'core';
293      $filter ||= 'all';
294      
295      $logger->debug( __PACKAGE__ . "->getChildNodes($mode)" );
296    
297    $logger->debug( __PACKAGE__ . "->getChildNodes()" );    if ($mode eq 'core') {
298    
299    # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE      my @nodes;
300    #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });      
301    #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );      # create new DBI - Data::Storage - object from already connected DBI::db - handle inside the current COREHANDLE
302          #my $loc = new Data::Storage::Locator( type => "DBI", dbi => { db => $self->{COREHANDLE}->{db} });
303    # todo: should we retrieve information from the schema here      #my $loc = new Data::Storage::Locator( type => "DBI", COREHANDLE => $self->{COREHANDLE}->{db} );
304    # rather than poorly getting table names from underlying dbi?      
305    my $storage = $self->_getSubLayerHandle();      # todo: should we retrieve information from the schema here
306    @nodes = @{$storage->getChildNodes()};      # rather than poorly getting table names from underlying dbi?
307    #$storage->_configureCOREHANDLE();      my $storage = $self->_getSubLayerHandle();
308  #print "getchildnodes\n";      @nodes = @{$storage->getChildNodes()};
309  #print Dumper($self);      #$storage->_configureCOREHANDLE();
310    #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {    #print "getchildnodes\n";
311      #print Dumper($self);
312        #if (my $result = $self->sendCommand( 'SHOW TABLES;' ) ) {
313        
314        # TODO: REVIEW
315        #$storage->disconnect();
316        
317        $self->{meta}->{childnodes} = \@nodes;
318    
319        return \@nodes;
320        
321    # TODO: REVIEW    } elsif ($mode eq 'root') {
322    #$storage->disconnect();      
323        # FIXME: this will return *all* known classes to 'Class::Tangram',
324        # which might not be what you expect since more than one instance
325        # of Tangram may be in memory and Class::Tangram seems to
326        # offer no methods to determine this or filter its result(s) according
327        # to a specific database.
328        my @object_names = Class::Tangram::known_classes();
329        my @concret_names;
330        my $o_cnt;
331        foreach (sort @object_names) {
332          push @concret_names, $_  if (!Class::Tangram::class_is_abstract($_));
333          $o_cnt++;
334        }
335    
336        if ($filter eq 'all') {
337          return \@object_names;
338        } elsif ($filter eq 'concrete') {
339          return \@concret_names;
340        }
341        
342    $self->{meta}->{childnodes} = \@nodes;    }
343        
   return \@nodes;  
344    
345  }  }
346    
   
347  sub testIntegrity {  sub testIntegrity {
348    
349    my $self = shift;    my $self = shift;
# Line 435  sub getListFiltered { Line 521  sub getListFiltered {
521    # redirect to unfiltered mode    # redirect to unfiltered mode
522    #return $self->getListUnfiltered(@_);    #return $self->getListUnfiltered(@_);
523    
524    my $nodename = shift;    my $in = {};
525    my $filters = shift;    $in->{nodename} = shift;
526      $in->{filters} = shift;
527      $in->{sorting} = shift;
528      
529    my @results;    my @results;
530    $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $nodename . "' )" );    $logger->debug( __PACKAGE__ . "->getListFiltered( nodename => '" . $in->{nodename} . "' )" );
531    
532      #print Dumper($filters);
533    
534  #print Dumper($filters);    # 1. "Remote Object Handle" - get set of objects from odbms by object name
535      my $remote = $self->{_COREHANDLE}->remote($in->{nodename});
536        
537      # 2. Transfer $in to $orm_query
538      my $orm_query = {};
539      
540      # 2.a. Filters
541    my @tfilters;    my @tfilters;
542      my $orm_filter = undef;
543        
544    foreach my $filter (@$filters) {    foreach my $filter (@{$in->{filters}}) {
545        
546      # get filter - TODO: for each filter      # get filter - TODO: for each filter
547      #my $filter = $filters->[0];      #my $filter = $filters->[0];
548      
549      # build filter      # 1. The parts of a filter source entry
550      my $lexpr = $filter->{key};      my $lexpr = $filter->{key};
551      #my $op = $filter->{op};      #my $op = $filter->{op};
552      my $op = '=';      my $op = '=';
553      my $rexpr = $filter->{val};      my $rexpr = $filter->{val};
554      my $tight = 100;      my $tight = 100;
555    
556        # 2. Build filter target entry
557            
558    #  my $tfilter = Tangram::Filter->new(      # Test 1 - didn't work out!
559    #    expr => "t1.$lexpr $op '$rexpr'",      #  my $tfilter = Tangram::Filter->new(
560    #    tight => $tight,      #    expr => "t1.$lexpr $op '$rexpr'",
561    #    objects => $objects,      #    tight => $tight,
562    #  );      #    objects => $objects,
563          #  );
564      # HACK: build eval-string (sorry) to get filtered list - please give advice here  
565      push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";      my $orm_filter_tmp = undef;
566        # was:
567        # TODO: is_op?
568        # dispatch un-common operators if exists
569        if ($filter->{op} eq "ref") {
570          # do nothing, results will be filtered later cause 'tangram-filter' doesn't support 'ref' query
571          #print "Filter->op eq 'ref'.\n";
572          #push @tfilters, 'ref($remote->{' . $filter->{key} . '})' . " eq '$filter->{val}'";
573        } else {
574          # HACK: build eval-string (sorry) to get filtered list - please give advice here
575          #push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
576          # better: calculate expression right here!
577          #push @tfilters, '$remote->{' . $filter->{key} . '}' . " $filter->{op} '$filter->{val}'";
578          
579          #print "key: ", $filter->{key}, "\n";
580          
581          my $left = $remote->{$filter->{key}};
582          #my $r = Tangram::Expr->new( 'string' => "'" . $filter->{val} . "'" );
583          my $right = $filter->{val};
584          my $op = $filter->{op};
585          $orm_filter_tmp = $left->$op($right);
586        }
587    
588        if (not $orm_filter) {
589          $orm_filter = $orm_filter_tmp;
590        } else {
591          $orm_filter = $orm_filter->and($orm_filter_tmp);
592        }
593    
594    }    }
595    
596    my $tfilter = join(' & ', @tfilters);    $orm_query->{filter} = $orm_filter;
597    
   # get set of objects from odbms by object name  
   my $remote = $self->{_COREHANDLE}->remote($nodename);  
     
598    # was:    # was:
599    #@results = $self->{COREHANDLE}->select($object_set, $tfilter);  
600      # 2.b. sorting [new as of 2003-06-17]
601      if ($in->{sorting}) {
602        my $sorting_rules = [];
603        my $sorting_order = 'ASC';
604        foreach my $sorting_column (keys %{$in->{sorting}}) {
605          $sorting_order = $in->{sorting}->{$sorting_column} if $in->{sorting}->{$sorting_column};
606          push @$sorting_rules, Tangram::Expr->new( 'string' => $sorting_column );
607        }
608        $orm_query->{order} = $sorting_rules;
609        if ($sorting_order eq 'DESC') {
610          $orm_query->{desc} = 1;
611        }
612      }
613    
614    
615      # 3. build and issue query - return result as Tangram::Cursor
616      
617      # coerce hash into array (This is valid in Perl)
618      my @orm_query_args = %$orm_query;
619      # send query (arguments) to orm
620      @results = $self->{_COREHANDLE}->select($remote, @orm_query_args);
621      
622      #my $cursor = $self->{_COREHANDLE}->cursor($remote, @orm_query_args);
623      #return $cursor;
624      #print Dumper(@results);
625    
626    # is:    # is:
627    # HACK: build eval-string (sorry) to get filtered list - please give advice here    # HACK: build eval-string (sorry) to get filtered list - please give advice here
628    my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . ');';      #my $evalstring = 'return $self->{_COREHANDLE}->select($remote, ' . $tfilter . $sorting . ');';
629          #print "eval: $evalstring", "\n";
630    #print "eval: $evalstring", "\n";      # get filtered list/set
631          #@results = eval($evalstring);
632    # get filtered list/set      #die $@ if $@;
633    @results = eval($evalstring);  
634    die $@ if $@;    # filter results - NEEDED for e.g. UserManagment !!
635      if ($in->{filters}->[0]->{op} && ($in->{filters}->[0]->{op} eq "ref")) {
636          #print "Filter->op eq 'ref'.\n";
637          my $att_name = $in->{filters}->[0]->{key};
638          my $att_val = $in->{filters}->[0]->{val};
639          my @filtered;
640          foreach(@results) {
641            if(ref($_->{$att_name}) eq $att_val) {
642              push @filtered, $_;
643            }
644          }
645          @results = @filtered;
646      }
647    
648      #print "results: " . Dumper(\@results);
649        
650    return \@results;    return \@results;
651  }  }
# Line 513  sub createSet { Line 675  sub createSet {
675    return $result;    return $result;
676  }  }
677    
678    sub createResult {
679      my $self = shift;
680      my $rh = shift;
681      my $result = Data::Storage::Result::Tangram->new( RESULTHANDLE => $rh );
682      return $result;
683    }
684    
685  sub sendQuery {  sub sendQuery {
686    my $self = shift;    my $self = shift;
687    my $query = shift;    my $query = shift;
   #my $sql = "SELECT cs FROM $self->{metainfo}->{$descent}->{node} WHERE $self->{metainfo}->{$descent}->{IdentProvider}->{arg}='$self->{entry}->{source}->{ident}';";  
   #my $result = $self->{metainfo}->{$descent}->{storage}->sendCommand($sql);  
688    
689    #print Dumper($query);    #print Dumper($query);
690    
691    # HACK: special case: querying by id does not translate into a common tangram query    # type = ITEM|LIST|TRANSPARENT
692    # just load the object by given id(ent)    my $type = '';
693    if ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq') {    # mode = OID|SPECIAL
694      #print "LOAD!!!", "\n";    my $mode = '';
695      #exit;    my $ident = '';
696      #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );    my $crud = '';
697      my $ident = $query->{criterias}->[0]->{val};    
698  #print "load obj", "\n";    
699      #return $self->createSet() if $ident == 5;    # dispatch type and mode
700      $self->{_COREHANDLE}->unload($ident);    
701      my $object = $self->{_COREHANDLE}->load($ident);      # defaults - 1
702  #print "get id", "\n";      if ($query->{options}) {
703      my $oid = $self->{_COREHANDLE}->id($object);        $crud = $query->{options}->{crud};
704  #print Dumper($object);        $crud ||= $query->{options}->{action};
705  #print "oid: $oid", "\n";      }
706      return $self->createSet($object);  
707      #return $self->createSet( $self->{COREHANDLE}->load('300090018') );      # defaults - 2
708    }      $type ||= 'TRANSPARENT';
709        $crud ||= 'RETRIEVE';
710    my $list = $self->getListFiltered($query->{node}, $query->{criterias});  
711    #return $self->createSet($object);      if ($query->{options}->{OID}) {
712    #return $self->createSet($list);        $type = 'ITEM';
713    return $self->createSet(@$list);        $mode = 'OID';
714          $ident = $query->{options}->{OID};
715    #die("This should not be reached for now - redirect to \$self->getListFiltered() here!");      
716        } elsif (my $guid = $query->{options}->{GUID}) {
717          $type = 'TRANSPARENT';
718          $query->{criterias} = [ { key => 'guid', op => 'eq', val => $guid } ];    
719    
720        # if operator is different (dispatcher for 'getListFiltered')
721        } elsif (my $op = $query->{options}->{op}) {
722          $type = 'TRANSPARENT';
723          $query->{criterias} = [ { key => $query->{options}->{meta_label}, op => $op, val => $query->{options}->{meta_value} } ];    
724    
725        }
726      
727        # HACK: special case: querying by id does not translate into a common tangram query
728        # just load the object by given identifier (OID) named 'id' - this is required by Data::Transfer::Sync!
729        if ($query->{criterias} && ($query->{criterias}->[0]->{key} eq 'id' && $query->{criterias}->[0]->{op} eq 'eq')) {
730          $type = 'ITEM';
731          $mode = 'SPECIAL.SYNC';
732          $ident = $query->{criterias}->[0]->{val};
733        }
734      
735    
736      # execute query
737      my $result;
738    
739      if ($type eq 'ITEM' && $ident) {
740    
741        if ($mode eq 'OID') {    
742          # TODO: review this case!
743          $result = $self->getObject($ident, $query->{options});
744        
745        } elsif ($mode eq 'SPECIAL.SYNC') {
746    
747          # V1 - failed
748          #return Set::Object->new( $self->{COREHANDLE}->load($query->{criterias}->[0]->{val}) );
749      
750          # hmm....
751          #return $self->createSet() if $ident == 5;
752          
753          # Unload single object before doing any further operations to
754          # expect a "fresh" object from orm when performing the next calls.
755          $self->{_COREHANDLE}->unload($ident);
756          
757          # Load object from orm.
758          my $object = $self->{_COREHANDLE}->load($ident);
759      
760          # determine object identifier (OID)
761          my $oid = $self->{_COREHANDLE}->id($object);
762          
763          # encapsulate into result/response container and return this one
764          $result = $self->createSet($object);
765          
766          # debugging
767          #$result = $self->createSet( $self->{COREHANDLE}->load('300090018') );
768    
769        }
770        
771      
772      } elsif ($type eq 'TRANSPARENT') {
773    
774        if ($crud eq 'RETRIEVE') {
775    
776          my $list = $self->getListFiltered($query->{node}, $query->{criterias}, $query->{sorting});
777          #return $list;
778          return $self->createResult($list);
779          
780          #return $self->createSet($object);
781          #return $self->createSet($list);
782          return $self->createSet(@$list);
783        
784          #die("This should not be reached for now - redirect to \$self->getListFiltered() here!");
785        
786          # try a generic tangram query here
787          # TODO: try to place an oql on top of that (search.cpan.org!)
788          my @crits;
789          foreach (@{$query->{criterias}}) {
790            my $op = '';
791            $op = '=' if lc $_->{op} eq 'eq';
792            push @crits, "$_->{key}$op'$_->{val}'";
793          }
794          my $subnodes = {};
795          map { $subnodes->{$_}++ } @{$query->{subnodes}};
796          # HACK: this is hardcoded ;(    expand possibilities!
797          #my $crit = join(' AND ', @crits);
798          #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);
799          #return $self->sendCommand($sql);
800          #my $h = $self->{COREHANDLE}->remote($query->{node});
801          #my $res = $self->{COREHANDLE}->select($h, $h->{);
802          $result = $self->createCursor($query->{node});
803    
804        } elsif ($crud eq 'UPDATE') {
805    
806          # Patch current query to be a loader (e.g. change action, remove payload) ...
807          my $childquery = deep_copy($query);
808          $childquery->{options}->{crud} = 'RETRIEVE';
809          delete $childquery->{payload};
810    
811          # ... to use it to fetch a fresh object using ourselves (sendQuery).
812          my $cursor = $self->sendQuery($childquery);
813          my $status = $cursor->getStatus();
814          my $object = $cursor->getNextEntry();
815    
816          # Merge values and apply value modifiers.
817          my $options = { utf8 => 1, php => 1 };
818          merge_to($object, $query->{payload}, $options);
819    
820          #print Dumper($object);
821          
822          # Execute update operation at orm.
823          $self->update($object);
824          $result = $self->createResult([ $object ]);
825        
826        } elsif ($crud eq 'DELETE') {
827    
828          # Patch current query to be a loader (e.g. change action) ...
829          my $childquery = deep_copy($query);
830          $childquery->{options}->{crud} = 'RETRIEVE';
831    
832          # ... to use it to fetch a fresh object using ourselves (sendQuery).
833          my $cursor = $self->sendQuery($childquery);
834          my $status = $cursor->getStatus();
835          my $object = $cursor->getNextEntry();
836    
837          $self->erase($object);
838    
839        } elsif ($crud eq 'CREATE') {
840          
841          my $nodename = $query->{node};      
842          my $newnode = $self->createNode($nodename);
843          my $id = $self->{_COREHANDLE}->insert($newnode);
844      
845          print "Saved new node $nodename with GUID $newnode->{guid}, OID '$id': " . Dumper($newnode) . "\n";
846          
847          return $newnode;
848    
849        }
850    
851      }
852    
853      return $result;
854    
   # try a generic tangram query here  
   # TODO: try to place an oql on top of that (search.cpan.org!)  
   my @crits;  
   foreach (@{$query->{criterias}}) {  
     my $op = '';  
     $op = '=' if lc $_->{op} eq 'eq';  
     push @crits, "$_->{key}$op'$_->{val}'";  
   }  
   my $subnodes = {};  
   map { $subnodes->{$_}++ } @{$query->{subnodes}};  
   # HACK: this is hardcoded ;(    expand possibilities!  
   #my $crit = join(' AND ', @crits);  
   #my $sql = hash2Sql($query->{node}, $subnodes, 'SELECT', $crit);  
   #return $self->sendCommand($sql);  
   #my $h = $self->{COREHANDLE}->remote($query->{node});  
   #my $res = $self->{COREHANDLE}->select($h, $h->{);  
   return $self->createCursor($query->{node});  
855  }  }
856    
857  sub eraseAll {  sub eraseAll {
# Line 602  sub getObject { Line 887  sub getObject {
887    return $object if $object;    return $object if $object;
888  }  }
889    
890  sub getObjectByGuid {  sub getObjectByGuid_old {
891    my $self = shift;    my $self = shift;
892    my $guid = shift;    my $guid = shift;
893    my $options = shift;    my $options = shift;
# Line 632  sub getObjectByGuid { Line 917  sub getObjectByGuid {
917        
918  }  }
919    
920  sub getObjectAsHash {  sub getObjectAsHash_old {
921    my $self = shift;    my $self = shift;
922    my $oid = shift;    my $oid = shift;
923    my $options = shift;      my $options = shift;  
# Line 715  sub disconnect2 { Line 1000  sub disconnect2 {
1000    $self->{dataStorageLayer}->disconnect();    $self->{dataStorageLayer}->disconnect();
1001  }  }
1002    
1003    
1004    sub createNode {
1005      my $self = shift;
1006      my $classname = shift;
1007    
1008      my $obj = $classname->new();
1009      
1010      my $attr_options = Class::Tangram::attribute_options($classname);
1011      #print "Attribute Options: " . Dumper($attr_options);
1012    
1013      my $attr_types = Class::Tangram::attribute_types($classname);
1014      #print "Attribute Types: " . Dumper($attr_types);
1015      
1016      foreach(keys %{$attr_types}) {
1017        if($attr_types->{$_} eq 'string') {
1018          $obj->{$_} = '';
1019        } elsif($attr_types->{$_} eq 'int') {
1020          $obj->{$_} = 0;
1021        } elsif($attr_types->{$_} eq 'real') {
1022          $obj->{$_} = 0;
1023        } elsif($attr_types->{$_} eq 'rawdatetime') {
1024          $obj->{$_} = '0000-00-00 00:00:00';
1025        } elsif($attr_types->{$_} eq 'ref') {
1026          if($attr_options->{$_}->{class}) {
1027            # HACK!!!
1028            # STANDALONE Objects (objects which make sense to instanciat alone) should not created automaticly
1029            # because they maybe exists and should only be SETTED not CREATED!
1030            # TODO: Create a flag at the scheme for that reason!
1031            #   (e.g child_node => 1 for child-nodes only like e.g. UserData)
1032            if($attr_options->{$_}->{class} eq 'NetPerson' || $attr_options->{$_}->{class} eq 'Event') {
1033              #$obj->{$_} = undef();
1034            } else {
1035              $obj->{$_} = $self->createNode($attr_options->{$_}->{class});
1036            }
1037          } else {
1038            #$obj->{$_} = undef();
1039          }
1040        } elsif($attr_types->{$_} eq 'iarray') {
1041            $obj->{$_} = [ ];
1042        } elsif($attr_types->{$_} eq 'hash') {
1043            $obj->{$_} = {  };
1044        } elsif($attr_types->{$_} eq 'flat_hash') {
1045            $obj->{$_} = { };
1046        }
1047      }
1048      
1049      #print "New Object: " . Dumper($obj);
1050      
1051      return $obj;
1052    }
1053    
1054    
1055      sub insertChildNode {
1056        my $self = shift;
1057        my $child_entry = shift;
1058        my $query_args = shift;
1059        
1060        my $core = $self->{_COREHANDLE};
1061        my $nodename = $query_args->{nodename};
1062        
1063        # get parent object
1064        my $query = {
1065              node => $query_args->{parent}->{nodename},
1066              options => { GUID => $query_args->{parent}->{guid}, },
1067              };
1068        my $cursor = $self->sendQuery($query);
1069        my $parent = $cursor->getNextEntry();
1070    
1071        # debug
1072        #print "Parent_org: " . Dumper($parent);
1073            
1074        # Create child node object if isn't already done
1075        # ($child_entry have to be the class name then...)
1076        if(!ref($child_entry)) {
1077          $child_entry = $self->createNode($child_entry);
1078          # it could be insert 'manually' or will be insert 'transparent' if parent will be updated
1079          #$core->insert($child_entry);
1080          #print "Create child object [$nodename]: " . Dumper($child_entry);
1081        }
1082    
1083        # get reference of tied node (seems, only on Linux node's are tied!!)
1084        my $tied_node = tied $parent->{$nodename};
1085    
1086        # insert/change child entry at parent
1087        #print "reference: " . ref($parent->{$nodename}) . "\n";
1088        if(ref($parent->{$nodename}) eq 'ARRAY') {
1089          # (seems, only on Linux node's are tied!!)
1090          if($tied_node) {
1091            # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type,
1092            # so a 'PUSH' is not implemented (which could be then done transparently)
1093            my $array = $tied_node->FETCH;
1094            push @$array, $child_entry;
1095            $tied_node->STORE($array);
1096            # node will be normaly untied at 'STORE'
1097            if(tied $parent->{$nodename}) { print "already tied !!\n"; }
1098            else { undef $tied_node; }
1099          } else {
1100            push @{$parent->{$nodename}}, $child_entry;
1101          }
1102        }
1103        elsif(ref($parent->{$nodename}) eq 'HASH') {
1104          if(my $key = $query_args->{hash_key}) {
1105            # (seems, only on Linux node's are tied!!)
1106            if($tied_node) {
1107              # same problem as with 'ARRAY':
1108              # all tangram types are tied as 'SCALAR' with special 'FETCH', 'STORE' methods per type.
1109              my $hash = $tied_node->FETCH;
1110              $hash->{$key} = $child_entry;
1111              $tied_node->STORE($hash);
1112              # node will be normaly untied at 'STORE'
1113              if(tied $parent->{$nodename}) { print "already tied !!\n"; }
1114              else { undef $tied_node; }
1115            } else {
1116              $parent->{$nodename}->{$key} = $child_entry;
1117            }
1118          } else {
1119           print "ERROR: No HASH KEY given, so not able to insert hash entry!";
1120          }
1121        }
1122        else {
1123          $parent->{$nodename} = $child_entry;
1124        }
1125    
1126        # debug
1127        #print "Parent_new: " . Dumper($parent);
1128        
1129        # save parent
1130        $core->update($parent);
1131        
1132        # debug
1133        #print "Saved Parent: ". Dumper($parent);
1134        
1135        return $child_entry;
1136      }
1137    
1138    
1139  1;  1;
1140  __END__  __END__

Legend:
Removed from v.1.30  
changed lines
  Added in v.1.45

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed