| 1 | package Gtk2::Ex::TreeMaker::FlatInterface; | ||
| 2 | |||
| 3 | our $VERSION = '0.10'; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Carp; | ||
| 8 | use Data::Dumper; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | B<Gtk2::Ex::TreeMaker::FlatInterface> - This module is not to be used directly. It is called from Gtk2::Ex::TreeMaker as a utility module. | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | This module contains a utility method C<sub flat_to_tree>. This utility method will accept an array of arrays (a set of relational records) as its input and then return a treelike data structure that is in-turn used by the Gtk2::Ex::TreeMaker to spin the Gtk2::TreeModel. | ||
| 17 | |||
| 18 | =head1 METHODS | ||
| 19 | |||
| 20 | =head2 Gtk2::Ex::TreeMaker::FlatInterface->new | ||
| 21 | |||
| 22 | Accepts no arguments. Just returns a reference to the object | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | sub new { | ||
| 27 | my ($class) = @_; | ||
| 28 | my $self = {}; | ||
| 29 | $self->{column_count} = undef; | ||
| 30 | $self->{record_count} = 0; | ||
| 31 | bless ($self, $class); | ||
| 32 | return $self; | ||
| 33 | } | ||
| 34 | |||
| 35 | =head2 Gtk2::Ex::TreeMaker::FlatInterface->flat_to_tree | ||
| 36 | |||
| 37 | Accepts an array of arrays (a set of relational records) as its input. Returns a special tree-like data structure that is then used by the Gtk2::Ex::TreeMaker to spin the Gtk2::TreeModel. | ||
| 38 | |||
| 39 | Here is a sample input: | ||
| 40 | |||
| 41 | my $recordset = [ | ||
| 42 | ['Texas','Dallas','Fruits','Dec-2003','300',0,1,'red'], | ||
| 43 | ['Texas','Dallas','Veggies','Jan-2004','120',1,0,'blue'], | ||
| 44 | ['Texas','Austin','Fruits','Nov-2003','310',1,1,'white'], | ||
| 45 | ]; | ||
| 46 | |||
| 47 | Here is the corresponding output: | ||
| 48 | |||
| 49 | my $output = { | ||
| 50 | 'Node' => [ | ||
| 51 | { | ||
| 52 | 'Node' => [ | ||
| 53 | { | ||
| 54 | 'Node' => [ | ||
| 55 | { | ||
| 56 | 'Node' => [ | ||
| 57 | { | ||
| 58 | 'text' => '310', | ||
| 59 | 'editable' => 1, | ||
| 60 | 'background' => 'white', | ||
| 61 | 'hyperlinked' => 1, | ||
| 62 | 'Name' => 'Nov-2003' | ||
| 63 | } | ||
| 64 | ], | ||
| 65 | 'Name' => 'Fruits' | ||
| 66 | } | ||
| 67 | ], | ||
| 68 | 'Name' => 'Austin' | ||
| 69 | }, | ||
| 70 | { | ||
| 71 | 'Node' => [ | ||
| 72 | { | ||
| 73 | 'Node' => [ | ||
| 74 | { | ||
| 75 | 'text' => '120', | ||
| 76 | 'editable' => 1, | ||
| 77 | 'background' => 'blue', | ||
| 78 | 'hyperlinked' => 0, | ||
| 79 | 'Name' => 'Jan-2004' | ||
| 80 | } | ||
| 81 | ], | ||
| 82 | 'Name' => 'Veggies' | ||
| 83 | }, | ||
| 84 | { | ||
| 85 | 'Node' => [ | ||
| 86 | { | ||
| 87 | 'text' => '300', | ||
| 88 | 'editable' => 0, | ||
| 89 | 'background' => 'red', | ||
| 90 | 'hyperlinked' => 1, | ||
| 91 | 'Name' => 'Dec-2003' | ||
| 92 | } | ||
| 93 | ], | ||
| 94 | 'Name' => 'Fruits' | ||
| 95 | } | ||
| 96 | ], | ||
| 97 | 'Name' => 'Dallas' | ||
| 98 | } | ||
| 99 | ], | ||
| 100 | 'Name' => 'Texas' | ||
| 101 | } | ||
| 102 | ], | ||
| 103 | 'Name' => 'ROOT' | ||
| 104 | }; | ||
| 105 | |||
| 106 | This data structure is really the key input into the Gtk2::Ex::TreeMaker module. If you can provide this data structure through external means, then we can build Gtk2::Ex::TreeMaker using that. More on this later... | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | sub flat_to_tree { | ||
| 111 | my ($self, $data_attributes, $flat) = @_; | ||
| 112 | my @attributes; | ||
| 113 | foreach my $attr (@$data_attributes) { | ||
| 114 | foreach my $key (keys %$attr) { | ||
| 115 | push @attributes, $key; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | my $intermediate = $self->_flat_to_intermediate(\@attributes, $flat); | ||
| 119 | my $withroot = {}; | ||
| 120 | $withroot->{'ROOT'} = $intermediate; | ||
| 121 | my $tree = _intermediate_to_tree($withroot); | ||
| 122 | return $tree; | ||
| 123 | } | ||
| 124 | |||
| 125 | # This is a private method | ||
| 126 | sub _flat_to_intermediate { | ||
| 127 | my ($self, $attributes, $flat) = @_; | ||
| 128 | my $intermediate = {}; | ||
| 129 | foreach my $record (@$flat) { | ||
| 130 | my $sub_intermediate = $intermediate; | ||
| 131 | $self->{record_count}++; | ||
| 132 | if ($self->{column_count}) { | ||
| 133 | if ($self->{column_count} != $#{@$record}) { | ||
| 134 | carp "Warning ! Input record expected ".($self->{column_count}+1)." columns; got ".($#{@$record}+1). | ||
| 135 | " at record number ".$self->{record_count}; | ||
| 136 | } | ||
| 137 | } else { | ||
| 138 | $self->{column_count} = $#{@$record}; | ||
| 139 | } | ||
| 140 | foreach (my $i=0; $i<$#{@$record}-$#{@$attributes}; $i++){ | ||
| 141 | my $column = $record->[$i]; | ||
| 142 | next unless $column; | ||
| 143 | if (!exists $sub_intermediate->{$column}) { | ||
| 144 | $sub_intermediate->{$column} = {}; | ||
| 145 | } | ||
| 146 | $sub_intermediate = $sub_intermediate->{$column}; | ||
| 147 | } | ||
| 148 | foreach (my $i=$#{@$record}-$#{@$attributes}; $i<=$#{@$record}; $i++){ | ||
| 149 | $sub_intermediate->{$attributes->[$i-$#{@$record}+$#{@$attributes}]} = $record->[$i]; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | return $intermediate; | ||
| 153 | } | ||
| 154 | |||
| 155 | # This is a private method | ||
| 156 | sub _intermediate_to_tree { | ||
| 157 | my ($intermediate) = shift; | ||
| 158 | foreach my $singlekey ( sort keys %$intermediate) { | ||
| 159 | my $node = {}; | ||
| 160 | $node->{'Name'} = $singlekey; | ||
| 161 | foreach my $key (sort keys %{$intermediate->{$singlekey}}) { | ||
| 162 | if (ref ($intermediate->{$singlekey}->{$key}) eq 'HASH') { | ||
| 163 | $node->{'Node'} = [] unless ($node->{'Node'}); | ||
| 164 | my $newtree = {}; | ||
| 165 | $newtree->{$key} = $intermediate->{$singlekey}->{$key}; | ||
| 166 | push @{$node->{'Node'}}, _intermediate_to_tree($newtree); | ||
| 167 | } else { | ||
| 168 | $node->{$key} = $intermediate->{$singlekey}->{$key}; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | return $node; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | 1; | ||
| 176 | |||
| 177 | __END__ |