| 1 | package WebService::Hexonet::Connector::Response; | ||
| 2 | |||
| 3 | use 5.030; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use WebService::Hexonet::Connector::Column; | ||
| 7 | use WebService::Hexonet::Connector::Record; | ||
| 8 | use parent qw(WebService::Hexonet::Connector::ResponseTemplate); | ||
| 9 | use POSIX qw(ceil floor); | ||
| 10 | use List::MoreUtils qw(first_index); | ||
| 11 | use Readonly; | ||
| 12 | Readonly my $INDEX_NOT_FOUND => -1; | ||
| 13 | |||
| 14 | our $VERSION = 'v2.10.4'; | ||
| 15 | |||
| 16 | |||
| 17 | sub new { | ||
| 18 | my ( $class, $raw, $cmd, $ph ) = @_; | ||
| 19 | my $self = WebService::Hexonet::Connector::ResponseTemplate->new($raw); | ||
| 20 | # care about getting placeholder variables replaced | ||
| 21 | if ( $self->{raw} =~ /[{][[:upper:]_]+[}]/gsmx ) { | ||
| 22 | if ( !defined $ph ) { | ||
| 23 | $ph = {}; | ||
| 24 | } | ||
| 25 | foreach my $key ( keys %{$ph} ) { | ||
| 26 | my $find = "[{]${key}[}]"; | ||
| 27 | my $replace = $ph->{$key}; | ||
| 28 | $self->{raw} =~ s/$find/$replace/gsmx; | ||
| 29 | } | ||
| 30 | $self->{raw} =~ s/[{][[:upper:]_]+[}]//gsmx; | ||
| 31 | $self = WebService::Hexonet::Connector::ResponseTemplate->new( $self->{raw} ); | ||
| 32 | } | ||
| 33 | $self = bless $self, $class; | ||
| 34 | $self->{command} = $cmd; | ||
| 35 | if ( defined $self->{command}->{PASSWORD} ) { | ||
| 36 | # make password no longer accessible | ||
| 37 | $self->{command}->{PASSWORD} = '***'; | ||
| 38 | } | ||
| 39 | $self->{columnkeys} = []; | ||
| 40 | $self->{columns} = []; | ||
| 41 | $self->{records} = []; | ||
| 42 | $self->{recordIndex} = 0; | ||
| 43 | |||
| 44 | my $h = $self->getHash(); | ||
| 45 | if ( defined $h->{PROPERTY} ) { | ||
| 46 | my @keys = keys %{ $h->{PROPERTY} }; | ||
| 47 | my $count = 0; | ||
| 48 | foreach my $key (@keys) { | ||
| 49 | my @d = @{ $h->{PROPERTY}->{$key} }; | ||
| 50 | $self->addColumn( $key, @d ); | ||
| 51 | my $len = scalar @d; | ||
| 52 | if ( $len > $count ) { | ||
| 53 | $count = $len; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | $count--; | ||
| 57 | for my $i ( 0 .. $count ) { | ||
| 58 | my %d = (); | ||
| 59 | foreach my $colkey (@keys) { | ||
| 60 | my $col = $self->getColumn($colkey); | ||
| 61 | if ( defined $col ) { | ||
| 62 | my $v = $col->getDataByIndex($i); | ||
| 63 | if ( defined $v ) { | ||
| 64 | $d{$colkey} = $v; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | $self->addRecord( \%d ); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | return $self; | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | sub addColumn { | ||
| 76 | my ( $self, $key, @data ) = @_; | ||
| 77 | push @{ $self->{columns} }, WebService::Hexonet::Connector::Column->new( $key, @data ); | ||
| 78 | push @{ $self->{columnkeys} }, $key; | ||
| 79 | return $self; | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | sub addRecord { | ||
| 84 | my ( $self, $h ) = @_; | ||
| 85 | push @{ $self->{records} }, WebService::Hexonet::Connector::Record->new($h); | ||
| 86 | return $self; | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | sub getColumn { | ||
| 91 | my ( $self, $key ) = @_; | ||
| 92 | if ( $self->_hasColumn($key) ) { | ||
| 93 | my $idx = first_index { $_ eq $key } @{ $self->{columnkeys} }; | ||
| 94 | return $self->{columns}[ $idx ]; | ||
| 95 | } | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | sub getColumnIndex { | ||
| 101 | my ( $self, $key, $idx ) = @_; | ||
| 102 | my $col = $self->getColumn($key); | ||
| 103 | return $col->getDataByIndex($idx) if defined $col; | ||
| 104 | return; | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | sub getColumnKeys { | ||
| 109 | my $self = shift; | ||
| 110 | return \@{ $self->{columnkeys} }; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | sub getColumns { | ||
| 115 | my $self = shift; | ||
| 116 | return \@{ $self->{columns} }; | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | sub getCommand { | ||
| 121 | my $self = shift; | ||
| 122 | return $self->{command}; | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | sub getCommandPlain { | ||
| 127 | my $self = shift; | ||
| 128 | my $str = q{}; | ||
| 129 | foreach my $key ( sort keys %{ $self->{command} } ) { | ||
| 130 | my $val = $self->{command}->{$key}; | ||
| 131 | $str .= "${key} = ${val}\n"; | ||
| 132 | } | ||
| 133 | return $str; | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | sub getCurrentPageNumber { | ||
| 138 | my $self = shift; | ||
| 139 | my $first = $self->getFirstRecordIndex(); | ||
| 140 | my $limit = $self->getRecordsLimitation(); | ||
| 141 | if ( defined $first && $limit > 0 ) { | ||
| 142 | return floor( $first / $limit ) + 1; | ||
| 143 | } | ||
| 144 | return $INDEX_NOT_FOUND; | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | sub getCurrentRecord { | ||
| 149 | my $self = shift; | ||
| 150 | return $self->{records}[ $self->{recordIndex} ] | ||
| 151 | if $self->_hasCurrentRecord(); | ||
| 152 | return; | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | sub getFirstRecordIndex { | ||
| 157 | my $self = shift; | ||
| 158 | my $col = $self->getColumn('FIRST'); | ||
| 159 | if ( defined $col ) { | ||
| 160 | my $f = $col->getDataByIndex(0); | ||
| 161 | if ( defined $f ) { | ||
| 162 | return int $f; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | my $len = scalar @{ $self->{records} }; | ||
| 166 | return 0 if ( $len > 0 ); | ||
| 167 | return; | ||
| 168 | } | ||
| 169 | |||
| 170 | |||
| 171 | sub getLastRecordIndex { | ||
| 172 | my $self = shift; | ||
| 173 | my $col = $self->getColumn('LAST'); | ||
| 174 | if ( defined $col ) { | ||
| 175 | my $l = $col->getDataByIndex(0); | ||
| 176 | if ( defined $l ) { | ||
| 177 | return int $l; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | my $len = $self->getRecordsCount(); | ||
| 181 | if ( $len > 0 ) { | ||
| 182 | return ( $len - 1 ); | ||
| 183 | } | ||
| 184 | return; | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | sub getListHash { | ||
| 189 | my $self = shift; | ||
| 190 | my @lh = (); | ||
| 191 | foreach my $rec ( @{ $self->getRecords() } ) { | ||
| 192 | push @lh, $rec->getData(); | ||
| 193 | } | ||
| 194 | my $r = { | ||
| 195 | LIST => \@lh, | ||
| 196 | meta => { | ||
| 197 | columns => $self->getColumnKeys(), | ||
| 198 | pg => $self->getPagination() | ||
| 199 | } | ||
| 200 | }; | ||
| 201 | return $r; | ||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | sub getNextRecord { | ||
| 206 | my $self = shift; | ||
| 207 | return $self->{records}[ ++$self->{recordIndex} ] | ||
| 208 | if ( $self->_hasNextRecord() ); | ||
| 209 | return; | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | sub getNextPageNumber { | ||
| 214 | my $self = shift; | ||
| 215 | my $cp = $self->getCurrentPageNumber(); | ||
| 216 | if ( $cp < 0 ) { | ||
| 217 | return $INDEX_NOT_FOUND; | ||
| 218 | } | ||
| 219 | my $page = $cp + 1; | ||
| 220 | my $pages = $self->getNumberOfPages(); | ||
| 221 | return $page if ( $page <= $pages ); | ||
| 222 | return $pages; | ||
| 223 | } | ||
| 224 | |||
| 225 | |||
| 226 | sub getNumberOfPages { | ||
| 227 | my $self = shift; | ||
| 228 | my $t = $self->getRecordsTotalCount(); | ||
| 229 | my $limit = $self->getRecordsLimitation(); | ||
| 230 | if ( $t > 0 && $limit > 0 ) { | ||
| 231 | return ceil( $t / $limit ); | ||
| 232 | } | ||
| 233 | return 0; | ||
| 234 | } | ||
| 235 | |||
| 236 | |||
| 237 | sub getPagination { | ||
| 238 | my $self = shift; | ||
| 239 | my $r = { | ||
| 240 | COUNT => $self->getRecordsCount(), | ||
| 241 | CURRENTPAGE => $self->getCurrentPageNumber(), | ||
| 242 | FIRST => $self->getFirstRecordIndex(), | ||
| 243 | LAST => $self->getLastRecordIndex(), | ||
| 244 | LIMIT => $self->getRecordsLimitation(), | ||
| 245 | NEXTPAGE => $self->getNextPageNumber(), | ||
| 246 | PAGES => $self->getNumberOfPages(), | ||
| 247 | PREVIOUSPAGE => $self->getPreviousPageNumber(), | ||
| 248 | TOTAL => $self->getRecordsTotalCount() | ||
| 249 | }; | ||
| 250 | return $r; | ||
| 251 | } | ||
| 252 | |||
| 253 | |||
| 254 | sub getPreviousPageNumber { | ||
| 255 | my $self = shift; | ||
| 256 | my $cp = $self->getCurrentPageNumber(); | ||
| 257 | if ( $cp < 0 ) { | ||
| 258 | return $INDEX_NOT_FOUND; | ||
| 259 | } | ||
| 260 | my $np = $cp - 1; | ||
| 261 | return $np if ( $np > 0 ); | ||
| 262 | return $INDEX_NOT_FOUND; | ||
| 263 | } | ||
| 264 | |||
| 265 | |||
| 266 | sub getPreviousRecord { | ||
| 267 | my $self = shift; | ||
| 268 | return $self->{records}[ --$self->{recordIndex} ] | ||
| 269 | if ( $self->_hasPreviousRecord() ); | ||
| 270 | return; | ||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | sub getRecord { | ||
| 275 | my ( $self, $idx ) = @_; | ||
| 276 | if ( $idx >= 0 && $self->getRecordsCount() > $idx ) { | ||
| 277 | return $self->{records}[ $idx ]; | ||
| 278 | } | ||
| 279 | return; | ||
| 280 | } | ||
| 281 | |||
| 282 | |||
| 283 | sub getRecords { | ||
| 284 | my $self = shift; | ||
| 285 | return \@{ $self->{records} }; | ||
| 286 | } | ||
| 287 | |||
| 288 | |||
| 289 | sub getRecordsCount { | ||
| 290 | my $self = shift; | ||
| 291 | my $len = scalar @{ $self->{records} }; | ||
| 292 | return $len; | ||
| 293 | } | ||
| 294 | |||
| 295 | |||
| 296 | sub getRecordsTotalCount { | ||
| 297 | my $self = shift; | ||
| 298 | my $col = $self->getColumn('TOTAL'); | ||
| 299 | if ( defined $col ) { | ||
| 300 | my $t = $col->getDataByIndex(0); | ||
| 301 | if ( defined $t ) { | ||
| 302 | return int $t; | ||
| 303 | } | ||
| 304 | } | ||
| 305 | return $self->getRecordsCount(); | ||
| 306 | } | ||
| 307 | |||
| 308 | |||
| 309 | sub getRecordsLimitation { | ||
| 310 | my $self = shift; | ||
| 311 | my $col = $self->getColumn('LIMIT'); | ||
| 312 | if ( defined $col ) { | ||
| 313 | my $l = $col->getDataByIndex(0); | ||
| 314 | if ( defined $l ) { | ||
| 315 | return int $l; | ||
| 316 | } | ||
| 317 | } | ||
| 318 | return $self->getRecordsCount(); | ||
| 319 | } | ||
| 320 | |||
| 321 | |||
| 322 | sub hasNextPage { | ||
| 323 | my $self = shift; | ||
| 324 | my $cp = $self->getCurrentPageNumber(); | ||
| 325 | if ( $cp < 0 ) { | ||
| 326 | return 0; | ||
| 327 | } | ||
| 328 | my $np = $cp + 1; | ||
| 329 | if ( $np <= $self->getNumberOfPages() ) { | ||
| 330 | return 1; | ||
| 331 | } | ||
| 332 | return 0; | ||
| 333 | } | ||
| 334 | |||
| 335 | |||
| 336 | sub hasPreviousPage { | ||
| 337 | my $self = shift; | ||
| 338 | my $cp = $self->getCurrentPageNumber(); | ||
| 339 | if ( $cp < 0 ) { | ||
| 340 | return 0; | ||
| 341 | } | ||
| 342 | my $pp = $cp - 1; | ||
| 343 | if ( $pp > 0 ) { | ||
| 344 | return 1; | ||
| 345 | } | ||
| 346 | return 0; | ||
| 347 | } | ||
| 348 | |||
| 349 | |||
| 350 | sub rewindRecordList { | ||
| 351 | my $self = shift; | ||
| 352 | $self->{recordIndex} = 0; | ||
| 353 | return $self; | ||
| 354 | } | ||
| 355 | |||
| 356 | |||
| 357 | sub _hasColumn { | ||
| 358 | my ( $self, $key ) = @_; | ||
| 359 | my $idx = first_index { $_ eq $key } @{ $self->{columnkeys} }; | ||
| 360 | return ( $idx > $INDEX_NOT_FOUND ); | ||
| 361 | } | ||
| 362 | |||
| 363 | |||
| 364 | sub _hasCurrentRecord { | ||
| 365 | my $self = shift; | ||
| 366 | my $len = scalar @{ $self->{records} }; | ||
| 367 | return ( $len > 0 && $self->{recordIndex} >= 0 && $self->{recordIndex} < $len ); | ||
| 368 | } | ||
| 369 | |||
| 370 | |||
| 371 | sub _hasNextRecord { | ||
| 372 | my $self = shift; | ||
| 373 | my $next = $self->{recordIndex} + 1; | ||
| 374 | my $len = scalar @{ $self->{records} }; | ||
| 375 | return ( $self->_hasCurrentRecord() && $next < $len ); | ||
| 376 | } | ||
| 377 | |||
| 378 | |||
| 379 | sub _hasPreviousRecord { | ||
| 380 | my $self = shift; | ||
| 381 | return ( $self->{recordIndex} > 0 && $self->_hasCurrentRecord() ); | ||
| 382 | } | ||
| 383 | |||
| 384 | 1; | ||
| 385 | |||
| 386 | __END__ |