| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # Perl | 
| 2 |  |  |  |  |  |  | # | 
| 3 |  |  |  |  |  |  | # Class Report::Porf::Table::Simple::AutoColumnConfigurator | 
| 4 |  |  |  |  |  |  | # | 
| 5 |  |  |  |  |  |  | # Perl Open Report Framework (Porf) | 
| 6 |  |  |  |  |  |  | # | 
| 7 |  |  |  |  |  |  | # Configure Report columns automatically | 
| 8 |  |  |  |  |  |  | # | 
| 9 |  |  |  |  |  |  | # Ralf Peine, Wed May 14 10:39:50 2014 | 
| 10 |  |  |  |  |  |  | # | 
| 11 |  |  |  |  |  |  | # More documentation at the end of file | 
| 12 |  |  |  |  |  |  | #------------------------------------------------------------------------------ | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | $VERSION = "2.000"; | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 1 |  |  | 1 |  | 6 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 42 |  | 
| 17 | 1 |  |  | 1 |  | 8 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 1209 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | #-------------------------------------------------------------------------------- | 
| 20 |  |  |  |  |  |  | # | 
| 21 |  |  |  |  |  |  | #  Report::Porf::Table::Simple::AutoColumnConfigurator | 
| 22 |  |  |  |  |  |  | # | 
| 23 |  |  |  |  |  |  | #-------------------------------------------------------------------------------- | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | package Report::Porf::Table::Simple::AutoColumnConfigurator; | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | # --- new Instance, Do NOT call direct!!! ----------------- | 
| 28 |  |  |  |  |  |  | sub new | 
| 29 |  |  |  |  |  |  | { | 
| 30 | 13 |  |  | 13 | 0 | 23 | my $caller = $_[0]; | 
| 31 | 13 |  | 33 |  |  | 58 | my $class  = ref($caller) || $caller; | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | # let the class go | 
| 34 | 13 |  |  |  |  | 30 | my $self = {}; | 
| 35 | 13 |  |  |  |  | 68 | bless $self, $class; | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 13 |  |  |  |  | 38 | $self->{Configurators} = {}; | 
| 38 | 13 |  |  |  |  | 29 | $self->{max_column_width} = 60; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 13 |  |  |  |  | 42 | return $self; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | # --- create_report_configuration as string list --- | 
| 44 |  |  |  |  |  |  | sub report_configuration_as_string { | 
| 45 | 1 |  |  | 1 | 0 | 4 | my ($self, | 
| 46 |  |  |  |  |  |  | $list_ref, | 
| 47 |  |  |  |  |  |  | $max_rows_to_inspect | 
| 48 |  |  |  |  |  |  | ) = @_; | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 1 |  |  |  |  | 3 | my $config_list = $self->create_report_configuration($list_ref, $max_rows_to_inspect); | 
| 51 | 1 |  |  |  |  | 3 | my @result; | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 1 |  |  |  |  | 3 | foreach my $config (@$config_list) { | 
| 54 | 6 |  |  |  |  | 7 | my $line = '$report->cc( '; | 
| 55 | 6 |  |  |  |  | 18 | foreach my $key (sort(keys(%$config))) { | 
| 56 | 24 |  |  |  |  | 49 | $line .= " $key => ". $config->{$key}.', '; | 
| 57 |  |  |  |  |  |  | } | 
| 58 | 6 |  |  |  |  | 12 | $line .= ");"; | 
| 59 | 6 |  |  |  |  | 11 | push (@result, $line); | 
| 60 |  |  |  |  |  |  | } | 
| 61 | 1 |  |  |  |  | 10 | return \@result; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | # --- create_report_configuration --- | 
| 65 |  |  |  |  |  |  | sub create_report_configuration { | 
| 66 | 13 |  |  | 13 | 0 | 23 | my ($self, | 
| 67 |  |  |  |  |  |  | $list_ref, | 
| 68 |  |  |  |  |  |  | $max_rows_to_inspect | 
| 69 |  |  |  |  |  |  | ) = @_; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 13 | 50 | 50 |  |  | 64 | return [] unless $list_ref && scalar @$list_ref; | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 13 |  |  |  |  | 27 | my $ref_info = ref($list_ref->[0]); | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 13 | 100 |  |  |  | 62 | return $self->create_hash_report_configuration($list_ref, $max_rows_to_inspect) | 
| 76 |  |  |  |  |  |  | if uc($ref_info) eq 'HASH'; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 2 | 50 |  |  |  | 14 | return $self->create_array_report_configuration($list_ref, $max_rows_to_inspect) | 
| 79 |  |  |  |  |  |  | if uc($ref_info) eq 'ARRAY'; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 0 |  |  |  |  | 0 | die "cannot create auto configuration for '$ref_info' elements."; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | # --- crate the default report configuration for a list of hashes -------- | 
| 85 |  |  |  |  |  |  | sub create_hash_report_configuration { | 
| 86 | 11 |  |  | 11 | 0 | 14 | my ($self, | 
| 87 |  |  |  |  |  |  | $list_ref, | 
| 88 |  |  |  |  |  |  | $max_rows_to_inspect | 
| 89 |  |  |  |  |  |  | ) = @_; | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 11 | 50 | 50 |  |  | 42 | return [] unless $list_ref && scalar @$list_ref; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 11 |  |  |  |  | 12 | my @config_list; | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 11 |  |  |  |  | 19 | my %hash_key_store   = (); | 
| 96 | 11 | 100 |  |  |  | 29 | $max_rows_to_inspect = 10 unless defined $max_rows_to_inspect; | 
| 97 | 11 | 100 |  |  |  | 27 | $max_rows_to_inspect = $#$list_ref if $max_rows_to_inspect == -1; | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 11 |  |  |  |  | 17 | my $row_count = 0; | 
| 100 | 11 |  |  |  |  | 25 | foreach my $data (@$list_ref) { | 
| 101 | 84 |  |  |  |  | 467 | foreach my $key (sort(keys(%$data))) { | 
| 102 | 185 | 50 |  |  |  | 461 | next unless defined $key; | 
| 103 | 185 | 100 |  |  |  | 352 | $hash_key_store{$key} = length ($key) unless $hash_key_store{$key}; | 
| 104 | 185 |  | 100 |  |  | 394 | my $text_length = length ($data->{$key} || '0'); | 
| 105 | 185 | 100 |  |  |  | 426 | $hash_key_store{$key} = $text_length | 
| 106 |  |  |  |  |  |  | if $hash_key_store{$key} < $text_length; | 
| 107 |  |  |  |  |  |  | } | 
| 108 | 84 | 100 |  |  |  | 209 | last if $row_count++ >= $max_rows_to_inspect; | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 11 |  |  |  |  | 45 | foreach my $key (sort(keys(%hash_key_store))) { | 
| 112 | 58 |  |  |  |  | 71 | my $width = $hash_key_store{$key}; | 
| 113 | 58 | 50 |  |  |  | 127 | $width = $self->{max_column_width} if $width > $self->{max_column_width}; | 
| 114 | 58 |  |  |  |  | 241 | push (@config_list, {-h => $key, -vn => $key, -w => $width, -a => 'l'}); | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 11 |  |  |  |  | 59 | return \@config_list; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | # --- crate the default report configuration for a list of arrays -------- | 
| 121 |  |  |  |  |  |  | sub create_array_report_configuration { | 
| 122 | 2 |  |  | 2 | 0 | 4 | my ($self, | 
| 123 |  |  |  |  |  |  | $list_ref, | 
| 124 |  |  |  |  |  |  | $max_rows_to_inspect | 
| 125 |  |  |  |  |  |  | ) = @_; | 
| 126 |  |  |  |  |  |  |  | 
| 127 | 2 | 50 | 50 |  |  | 12 | return [] unless $list_ref && scalar @$list_ref; | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 2 |  |  |  |  | 2 | my @config_list; | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 2 | 50 |  |  |  | 6 | $max_rows_to_inspect = 10 unless defined $max_rows_to_inspect; | 
| 132 | 2 | 50 |  |  |  | 6 | $max_rows_to_inspect = $#$list_ref if $max_rows_to_inspect == -1; | 
| 133 |  |  |  |  |  |  |  | 
| 134 | 2 |  |  |  |  | 3 | my $row_count = 0; | 
| 135 | 2 |  |  |  |  | 4 | my $max_columns = 0; | 
| 136 | 2 |  |  |  |  | 3 | my @column_lengths; | 
| 137 | 2 |  |  |  |  | 4 | foreach my $data (@$list_ref) { | 
| 138 | 12 |  |  |  |  | 14 | my $columns = scalar @$data; | 
| 139 | 12 | 100 |  |  |  | 26 | $max_columns = $columns if $columns > $max_columns; | 
| 140 | 12 |  |  |  |  | 21 | foreach my $idx (0..($columns-1)) { | 
| 141 | 58 |  | 100 |  |  | 130 | $column_lengths[$idx] = $column_lengths[$idx] || '0'; | 
| 142 | 58 |  | 50 |  |  | 250 | my $text_length = length ($data->[$idx] || '0'); | 
| 143 | 58 | 100 |  |  |  | 137 | $column_lengths[$idx] = $text_length | 
| 144 |  |  |  |  |  |  | if $column_lengths[$idx] < $text_length; | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  |  | 
| 147 | 12 | 50 |  |  |  | 272 | last if $row_count++ >= $max_rows_to_inspect; | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  |  | 
| 150 | 2 |  |  |  |  | 7 | foreach my $idx (0..($max_columns-1)) { | 
| 151 | 11 |  |  |  |  | 15 | my $width = $column_lengths[$idx]; | 
| 152 | 11 | 50 |  |  |  | 27 | $width = $self->{max_column_width} if $width > $self->{max_column_width}; | 
| 153 | 11 |  |  |  |  | 63 | push (@config_list, {-h => ($idx+1).'. Column', -vi => $idx, -w => $width, -a => 'l'}); | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 2 |  |  |  |  | 19 | return \@config_list; | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | # --- create report with automatic configured columns ------------------ | 
| 160 |  |  |  |  |  |  | sub create_report { | 
| 161 | 10 |  |  | 10 | 0 | 25 | my ($self, | 
| 162 |  |  |  |  |  |  | $list_ref, | 
| 163 |  |  |  |  |  |  | $report_framework, | 
| 164 |  |  |  |  |  |  | $format | 
| 165 |  |  |  |  |  |  | ) = @_; | 
| 166 |  |  |  |  |  |  |  | 
| 167 | 10 | 50 | 50 |  |  | 50 | return undef unless $list_ref && scalar @$list_ref; | 
| 168 |  |  |  |  |  |  |  | 
| 169 | 10 |  |  |  |  | 19 | my $first_element = $list_ref->[0]; | 
| 170 |  |  |  |  |  |  |  | 
| 171 | 10 | 50 |  |  |  | 22 | $report_framework = Report::Porf::Framework::get() unless $report_framework; | 
| 172 | 10 |  |  |  |  | 60 | my $report        = $report_framework->create_report($format); | 
| 173 |  |  |  |  |  |  |  | 
| 174 | 10 |  |  |  |  | 41 | $report->set_default_cell_value(''); | 
| 175 |  |  |  |  |  |  |  | 
| 176 | 10 |  |  |  |  | 14 | foreach my $config_option (@{$self->create_report_configuration($list_ref)}) { | 
|  | 10 |  |  |  |  | 36 |  | 
| 177 | 50 |  |  |  |  | 220 | $report->cc (%$config_option); | 
| 178 |  |  |  |  |  |  | } | 
| 179 |  |  |  |  |  |  |  | 
| 180 | 10 |  |  |  |  | 99 | $report->configure_complete(); | 
| 181 |  |  |  |  |  |  |  | 
| 182 | 10 |  |  |  |  | 51 | return $report; | 
| 183 |  |  |  |  |  |  | } |