| 1 | package Crop::Object; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Crop::Object - Base object class for the Crop framework | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use base qw/ Crop::Object /; | ||
| 12 | # ...usage... | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | Crop::Object is the base object class for the Crop framework, providing common object functionality. | ||
| 17 | |||
| 18 | =head1 AUTHORS | ||
| 19 | |||
| 20 | Euvgenio (Core Developer) | ||
| 21 | |||
| 22 | Alex (Contributor) | ||
| 23 | |||
| 24 | =head1 COPYRIGHT AND LICENSE | ||
| 25 | |||
| 26 | Apache 2.0 | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | |||
| 31 | use base qw/ Crop /; | ||
| 32 | |||
| 33 | =begin nd | ||
| 34 | Class: Crop::Object | ||
| 35 | ORM. | ||
| 36 | |||
| 37 | Collects class attributes including attributes of all the parent classes (multiple inheritance). | ||
| 38 | Generates getters/setters conforming to the class declaration. | ||
| 39 | |||
| 40 | Derived class could set up the <Extern> objects. | ||
| 41 | |||
| 42 | Attributes: | ||
| 43 | |||
| 44 | STATE - flags REMOVED, NOSYNC, DWHLINK, MODIFIED; see <Crop::Object::Constants>. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | use strict; | ||
| 49 | use v5.14; | ||
| 50 | use experimental qw/ switch /; | ||
| 51 | |||
| 52 | use Crop::Error; | ||
| 53 | use Crop::Util; | ||
| 54 | use Crop::Object::Collection; | ||
| 55 | use Crop::Object::Constants; | ||
| 56 | use Crop::Object::Attrs; | ||
| 57 | use Crop::Object::Warehouse; | ||
| 58 | |||
| 59 | use Crop::Debug; | ||
| 60 | |||
| 61 | =begin nd | ||
| 62 | Constant: READ | ||
| 63 | Name of the read method. | ||
| 64 | |||
| 65 | Constant: WRITE | ||
| 66 | Name of the write method. | ||
| 67 | =cut | ||
| 68 | use constant { | ||
| 69 | READ => 'read', | ||
| 70 | WRITE => 'write', | ||
| 71 | }; | ||
| 72 | |||
| 73 | =begin nd | ||
| 74 | Variable: our $AUTOLOAD | ||
| 75 | Name of the method call. | ||
| 76 | =cut | ||
| 77 | our $AUTOLOAD; | ||
| 78 | |||
| 79 | =begin nd | ||
| 80 | Variable: my %Attr_cache | ||
| 81 | Remember Attributes() for childs. | ||
| 82 | =cut | ||
| 83 | my %Attr_cache; | ||
| 84 | |||
| 85 | =begin nd | ||
| 86 | Variable: my %Done | ||
| 87 | Yet processed methods; there is no need to process it again. | ||
| 88 | =cut | ||
| 89 | my %Done; | ||
| 90 | |||
| 91 | =begin nd | ||
| 92 | Variable: my $WH | ||
| 93 | Warehouse stores objects. | ||
| 94 | =cut | ||
| 95 | my $WH = Crop::Object::Warehouse->new; | ||
| 96 | |||
| 97 | =begin nd | ||
| 98 | Constructor: new ($nosync, @attrs) | ||
| 99 | Init exemplar. | ||
| 100 | |||
| 101 | Determines <Crop::Object::Constants::DONT_SYNC> flag. | ||
| 102 | |||
| 103 | Setup value: from either argument or default. | ||
| 104 | |||
| 105 | Establish initial STATE. | ||
| 106 | |||
| 107 | Parameters: | ||
| 108 | $nosync - either exemplar will be syncronized to a warehouse; optional | ||
| 109 | @attrs - hash of attribute=>value packed to an array | ||
| 110 | |||
| 111 | Returns: | ||
| 112 | $self | ||
| 113 | =cut | ||
| 114 | sub new { | ||
| 115 | my $either = shift; | ||
| 116 | my $class = ref $either || $either; | ||
| 117 | |||
| 118 | my $nosync; | ||
| 119 | $nosync = shift if @_ and defined $_[0] and $_[0] eq DONT_SYNC; | ||
| 120 | |||
| 121 | my @in; | ||
| 122 | while (my ($i, $val) = each @_) { | ||
| 123 | # only a key could be dereferenced; value can be an object of the same class, i.e. in linked list | ||
| 124 | push @in, !($i % 2) && ref $val && ref $val eq $class ? %$val : $val; | ||
| 125 | } | ||
| 126 | my $in = expose_hashes \@in; | ||
| 127 | |||
| 128 | my $self = bless {}, $class; | ||
| 129 | |||
| 130 | for ($self->Attributes->List) { | ||
| 131 | my $name = $_->name; | ||
| 132 | $self->{$name} = exists $in->{$name} ? $in->{$name} : $_->default; | ||
| 133 | } | ||
| 134 | $self->{STATE} = OBJINIT; | ||
| 135 | $self->{STATE} |= NOSYNC if $nosync; | ||
| 136 | |||
| 137 | $self; | ||
| 138 | } | ||
| 139 | |||
| 140 | =begin nd | ||
| 141 | Method: __Access_r ($attr) | ||
| 142 | Read-access for attribute. | ||
| 143 | |||
| 144 | Parameters: | ||
| 145 | $attr - <Crop::Object::Attr> object | ||
| 146 | |||
| 147 | Returns: | ||
| 148 | Method reference. | ||
| 149 | =cut | ||
| 150 | sub __Access_r { | ||
| 151 | my ($self, $attr) = @_; | ||
| 152 | |||
| 153 | sub { | ||
| 154 | my $self = shift; | ||
| 155 | |||
| 156 | my $attr_name = $attr->name; | ||
| 157 | return warn "OBJECT|ERR: Write access for $attr_name denied by rules" if @_; | ||
| 158 | |||
| 159 | return $self->{$attr->name}; | ||
| 160 | }; | ||
| 161 | } | ||
| 162 | |||
| 163 | =begin nd | ||
| 164 | Method: __Access_rw ($attr) | ||
| 165 | Read-write-access for attibute. | ||
| 166 | |||
| 167 | Parameters: | ||
| 168 | $attr - <Crop::Object::Attr> object | ||
| 169 | |||
| 170 | Returns: | ||
| 171 | Method reference. | ||
| 172 | =cut | ||
| 173 | sub __Access_rw { | ||
| 174 | my ($self, $attr) = @_; | ||
| 175 | |||
| 176 | sub { | ||
| 177 | my $self = shift; | ||
| 178 | |||
| 179 | if (@_) { | ||
| 180 | $self->{$attr->name} = shift; | ||
| 181 | $self->{STATE} |= MODIFIED if $self->{STATE} & DWHLINK and $attr->of_type(STORED); | ||
| 182 | |||
| 183 | return $self; | ||
| 184 | } | ||
| 185 | |||
| 186 | $self->{$attr->name}; | ||
| 187 | }; | ||
| 188 | } | ||
| 189 | |||
| 190 | =begin nd | ||
| 191 | Method: __Access_no ($attr) | ||
| 192 | Access to an attribute without any accessor. | ||
| 193 | |||
| 194 | An error 'OBJECT|ALERT' will be arised. | ||
| 195 | |||
| 196 | Parameters: | ||
| 197 | $attr - <Crop::Object::Attr> object | ||
| 198 | |||
| 199 | Returns: | ||
| 200 | Method reference. | ||
| 201 | =cut | ||
| 202 | sub __Access_no { | ||
| 203 | my (undef, $attr) = @_; | ||
| 204 | |||
| 205 | sub { | ||
| 206 | return warn @_ > 1 ? | ||
| 207 | 'OBJECT|ALERT: Write access for ' . $attr->name . ' denied by rules' | ||
| 208 | : 'OBJECT|ALERT: Read access for ' . $attr->name . ' denied by rules'; | ||
| 209 | }; | ||
| 210 | } | ||
| 211 | |||
| 212 | =begin nd | ||
| 213 | Method: __Access_w ($attr) | ||
| 214 | Write-accessor for an attibute. | ||
| 215 | |||
| 216 | Parameters: | ||
| 217 | $attr - <Crop::Object::Attr> object | ||
| 218 | |||
| 219 | Returns: | ||
| 220 | Method reference. | ||
| 221 | =cut | ||
| 222 | sub __Access_w { | ||
| 223 | my ($self, $attr) = @_; | ||
| 224 | |||
| 225 | sub { | ||
| 226 | my $self = shift; | ||
| 227 | |||
| 228 | return warn 'OBJECT|ERR: Read access for \'' . $attr->name . '\' denied by rules' unless @_; | ||
| 229 | |||
| 230 | $self->{$attr->name} = shift; | ||
| 231 | $self->{STATE} |= MODIFIED if $self->{STATE} & DWHLINK and $attr->of_type(STORED); | ||
| 232 | |||
| 233 | $self; | ||
| 234 | }; | ||
| 235 | } | ||
| 236 | |||
| 237 | =begin nd | ||
| 238 | Method: All (@filter) | ||
| 239 | Get a <Crop::Object::Collection> of objects from warehouse. | ||
| 240 | |||
| 241 | (start code) | ||
| 242 | Class->All; # Get the Collection of all the exemplars | ||
| 243 | Class->All(attr1=>val1, attr_2=>val2); # Get the exemplars restricted by their attribute values | ||
| 244 | Class->All(attr1=>val, SORT=>'attr2'); # With the 'order by' clause | ||
| 245 | |||
| 246 | With 'extended' exemplars | ||
| 247 | Class->All(attr1=>val, EXT=>[ext1, ext2 => [ext3]]); | ||
| 248 | Class->All(attr1=>val, | ||
| 249 | SORT=> 'ext3.attr1', # works fine while 'ext3.attr1' matches real tablename.attrname | ||
| 250 | EXT=>[ext1, ext2 => [ext3]] | ||
| 251 | ); | ||
| 252 | |||
| 253 | # extended with filter in inner objects | ||
| 254 | Class->All(EXT=>[doc => {visible => 1} => ['file'], qw/ picture units /]); | ||
| 255 | (end code) | ||
| 256 | |||
| 257 | The following features are not yet implemented! | ||
| 258 | |||
| 259 | In addition to filter, an argument could be a SLICEN or SORT expression | ||
| 260 | (start code) | ||
| 261 | My::Class->All(SLICEN => [20, 30]); | ||
| 262 | # produces: | ||
| 263 | SELECT * from class_table OFFSET 30 LIMIT 20 ORDER BY id; | ||
| 264 | -- by default SLICEN does sort by primary keys | ||
| 265 | |||
| 266 | My::Class->All(SLICEN => [20, 30], SORT => ['ctime DESC']); | ||
| 267 | My::Class->All(SLICEN => [20, 30], SORT => 'ctime DESC'); -- is equivalent | ||
| 268 | SELECT * from class_table OFFSET 30 LIMIT 20 ORDER BY ctime DESC; -- explicit sort | ||
| 269 | |||
| 270 | My::Class->All(SORT => ['ctime DESC']); | ||
| 271 | SELECT * from class_table ORDER BY ctime; -- sort without slice | ||
| 272 | (end code) | ||
| 273 | |||
| 274 | Parameters: | ||
| 275 | @filter - attr=>val pairs, {attr=>val} hashref, SORT=>[expression, ...], | ||
| 276 | SORT=>'expression', SLICEN=>[limit, offset], LIMIT=>limit, | ||
| 277 | EXT=>[obj, ...] | ||
| 278 | |||
| 279 | Returns: | ||
| 280 | Collection of exemplars | ||
| 281 | =cut | ||
| 282 | sub All { | ||
| 283 | my ($either, @filter) = @_; | ||
| 284 | my $class = ref $either || $either; | ||
| 285 | |||
| 286 | my $in = expose_hashes \@filter; | ||
| 287 | exists $in->{$_} and not ref $in->{$_} and $in->{$_} = [$in->{$_}] for qw/ EXT SORT /; | ||
| 288 | |||
| 289 | $WH->all($class, %$in)->Set_state(DWHLINK); | ||
| 290 | } | ||
| 291 | |||
| 292 | =begin nd | ||
| 293 | Method: Attributes ($mode) | ||
| 294 | Get the class attributes corresponding to the $mode. | ||
| 295 | |||
| 296 | If no $mode has specified, all the attibutes will return. | ||
| 297 | |||
| 298 | Fetch all the attributes including (multi) inherited ones. | ||
| 299 | |||
| 300 | Parameters: | ||
| 301 | $mode - fetch attibutes with according to $mode; optional; (ANY,CACHED,STORED,KEY) | ||
| 302 | |||
| 303 | Returns: | ||
| 304 | arrayref - if $mode is defined | ||
| 305 | Attributes <Crop::Object::Attrs> - otherwise | ||
| 306 | =cut | ||
| 307 | sub Attributes { | ||
| 308 | my ($either, $mode) = @_; | ||
| 309 | my $class = ref $either || $either; | ||
| 310 | |||
| 311 | return $class->_attrs($mode) if $mode; | ||
| 312 | |||
| 313 | return Crop::Object::Attrs->new(class => $class) if $class eq __PACKAGE__; | ||
| 314 | return $Attr_cache{$class} if exists $Attr_cache{$class}; | ||
| 315 | |||
| 316 | my ($isa, $class_attr); | ||
| 317 | { | ||
| 318 | no strict; | ||
| 319 | |||
| 320 | $isa = \@{$class . '::ISA'}; | ||
| 321 | $class_attr = \%{$class . '::Attributes'}; | ||
| 322 | } | ||
| 323 | |||
| 324 | my %source; | ||
| 325 | for my $parent_class (@$isa) { | ||
| 326 | next unless $parent_class->can('Attributes'); | ||
| 327 | |||
| 328 | my $parent_attr = $parent_class->Attributes; | ||
| 329 | %source = (%source, %{$parent_attr->source}); | ||
| 330 | } | ||
| 331 | %source = (%source, %$class_attr); | ||
| 332 | |||
| 333 | my $Attributes = Crop::Object::Attrs->new(source => \%source, class => $class); | ||
| 334 | |||
| 335 | $Attr_cache{$class} = $Attributes; | ||
| 336 | } | ||
| 337 | |||
| 338 | =begin nd | ||
| 339 | Method: _attrs($mode) | ||
| 340 | Get all the object attributes declaration according to the $mode. | ||
| 341 | |||
| 342 | Parameters: | ||
| 343 | @mode - mode such a 'CACHED', 'STORED'; See <Crop::Object::Constants> | ||
| 344 | |||
| 345 | Returns: | ||
| 346 | array of <Crop::Object::Attr> | ||
| 347 | =cut | ||
| 348 | sub _attrs { | ||
| 349 | my ($class, $mode) = @_; | ||
| 350 | |||
| 351 | [ | ||
| 352 | grep $_->of_type($mode), $class->Attributes->List | ||
| 353 | ]; | ||
| 354 | } | ||
| 355 | |||
| 356 | =begin nd | ||
| 357 | Method: AUTOLOAD ($AUTOLOAD) | ||
| 358 | Generate accessors for class attributes. | ||
| 359 | |||
| 360 | Class declaration specifies a mode - {mode => 'read'}, 'write', or 'read/writer'. | ||
| 361 | |||
| 362 | Call of inappropriate accessor arise an error. | ||
| 363 | |||
| 364 | An accessor will be build only once. Next call will get it from the cache. | ||
| 365 | |||
| 366 | Parameters: | ||
| 367 | $AUTOLOAD - method name; See Perl-package 'UNIVERAL' | ||
| 368 | =cut | ||
| 369 | sub AUTOLOAD { | ||
| 370 | my $self = shift; | ||
| 371 | |||
| 372 | my $method = $self->_get_method_name($AUTOLOAD); | ||
| 373 | # debug "ACCESS_ATTR='$method'"; | ||
| 374 | |||
| 375 | return warn "OBJECT|ERR: Can't AUTOLOAD method $method oF class $self without the exemplar." unless ref $self; | ||
| 376 | return if $method eq 'DESTROY'; | ||
| 377 | return warn "OBJECT|ERR: Method AUTOLOAD: $AUTOLOAD in loop" if $Done{$AUTOLOAD}; | ||
| 378 | |||
| 379 | return sub { undef } if $method eq 'Table'; | ||
| 380 | |||
| 381 | my $Attributes = $self->Attributes; | ||
| 382 | |||
| 383 | my $accessor; | ||
| 384 | my $attr = $Attributes->have($method); | ||
| 385 | |||
| 386 | if ($attr) { | ||
| 387 | $accessor = | ||
| 388 | $attr->accessible(READ) && $attr->accessible(WRITE) ? '__Access_rw' | ||
| 389 | : $attr->accessible(READ) ? '__Access_r' | ||
| 390 | : $attr->accessible(WRITE) ? '__Access_w' | ||
| 391 | : '__Access_no' | ||
| 392 | ; | ||
| 393 | } else { | ||
| 394 | return warn "OBJECT|ERR: Either Object hasn't attribute or method $AUTOLOAD"; | ||
| 395 | } | ||
| 396 | |||
| 397 | { | ||
| 398 | no strict 'refs'; | ||
| 399 | *{$AUTOLOAD} = $self->$accessor($attr); | ||
| 400 | } | ||
| 401 | |||
| 402 | $Done{$AUTOLOAD}++; | ||
| 403 | $self->$method(@_); | ||
| 404 | } | ||
| 405 | |||
| 406 | =begin nd | ||
| 407 | Method: _Create ( ) | ||
| 408 | Create a new object. | ||
| 409 | |||
| 410 | Put an object to the Warehouse. | ||
| 411 | |||
| 412 | A subclass could redefine this method with specific behavior. | ||
| 413 | |||
| 414 | Returns: | ||
| 415 | $self - if ok | ||
| 416 | undef - otherwise | ||
| 417 | =cut | ||
| 418 | sub _Create { | ||
| 419 | my $self = shift; | ||
| 420 | |||
| 421 | $WH->create($self); | ||
| 422 | } | ||
| 423 | |||
| 424 | =begin nd | ||
| 425 | Method: DESTROY ( ) | ||
| 426 | Sync an exemplar with warehouse. | ||
| 427 | |||
| 428 | Do not perform save of object when: | ||
| 429 | |||
| 430 | - no table specified for the class | ||
| 431 | - exemplar was removed from warehouse | ||
| 432 | - exemplar has a special 'Dont sync' flag | ||
| 433 | - exemplar was not modified | ||
| 434 | =cut | ||
| 435 | sub DESTROY { | ||
| 436 | my $self = shift; | ||
| 437 | |||
| 438 | return unless $self->Table; | ||
| 439 | return if $self->{STATE} & REMOVED; | ||
| 440 | return if $self->{STATE} & NOSYNC; | ||
| 441 | return if $self->{STATE} & DWHLINK and not $self->{STATE} & MODIFIED; | ||
| 442 | |||
| 443 | $self->Save; | ||
| 444 | } | ||
| 445 | |||
| 446 | =begin nd | ||
| 447 | Method Erase ( ) | ||
| 448 | Cleanup all non-stable attributes of an object. | ||
| 449 | =cut | ||
| 450 | sub Erase { | ||
| 451 | my $self = shift; | ||
| 452 | |||
| 453 | for ($self->Attributes->List) { | ||
| 454 | next if $_->is_stable; | ||
| 455 | |||
| 456 | if ($_->has_default) { | ||
| 457 | $self->{$_->name} = $_->default; | ||
| 458 | } else { | ||
| 459 | undef $self->{$_->name}; | ||
| 460 | } | ||
| 461 | } | ||
| 462 | } | ||
| 463 | |||
| 464 | =begin nd | ||
| 465 | Method: Genkey ( ) | ||
| 466 | Generate Primary key. | ||
| 467 | |||
| 468 | This method must be redefined by subclass, it is pure virtual. | ||
| 469 | |||
| 470 | Produces error. | ||
| 471 | |||
| 472 | Returns: | ||
| 473 | undef | ||
| 474 | =cut | ||
| 475 | sub Genkey { | ||
| 476 | my $self = shift; | ||
| 477 | my $class = ref $self; | ||
| 478 | |||
| 479 | return warn "OBJECT|ALERT: Genkey method must be redefined by subclass '$class'"; | ||
| 480 | } | ||
| 481 | |||
| 482 | =begin nd | ||
| 483 | Method: Get (@filter) | ||
| 484 | Get an exemplar from warehouse. | ||
| 485 | |||
| 486 | (start code) | ||
| 487 | Class->All(EXT=>[doc => {visible => 1} => ['file'], qw/ picture units /]); | ||
| 488 | (end code) | ||
| 489 | |||
| 490 | See <All(@filter)>. | ||
| 491 | Parameters: | ||
| 492 | @filter - clause, exemplar must satisfy to | ||
| 493 | |||
| 494 | Returns: | ||
| 495 | an exemplar - if ok | ||
| 496 | false - if an error has acquired | ||
| 497 | =cut | ||
| 498 | sub Get { | ||
| 499 | my $either = shift; | ||
| 500 | my $class = ref $either || $either; | ||
| 501 | |||
| 502 | my $collection = $either->All(@_); | ||
| 503 | |||
| 504 | return unless $collection->Size; | ||
| 505 | return warn "OBJECT: Multiple exemplars for Get() method found for $class class" if $collection->Size > 1; | ||
| 506 | |||
| 507 | my $self = $collection->First; | ||
| 508 | |||
| 509 | $self->{STATE} |= DWHLINK; | ||
| 510 | |||
| 511 | $self; | ||
| 512 | } | ||
| 513 | |||
| 514 | =begin nd | ||
| 515 | Method: _get_method_name ($full_name) | ||
| 516 | Get method name stripping package name. | ||
| 517 | |||
| 518 | Parameters: | ||
| 519 | $full_name - method name with the package name as a prefix | ||
| 520 | |||
| 521 | Returns: | ||
| 522 | String, method name only. | ||
| 523 | =cut | ||
| 524 | sub _get_method_name { | ||
| 525 | my ($self, $full_name) = @_; | ||
| 526 | $full_name =~ /.*::(\w+)/; | ||
| 527 | |||
| 528 | $1; | ||
| 529 | } | ||
| 530 | |||
| 531 | =begin nd | ||
| 532 | Method: Global ($action, %values, $clause) | ||
| 533 | Perform changes on an entire class. | ||
| 534 | |||
| 535 | Performs action immediately. | ||
| 536 | |||
| 537 | Param: | ||
| 538 | $action - 'UPDATE', 'SAFE_UPDATE', and 'DELETE': | ||
| 539 | - UPDATE update with no paying attantion to the warehouse constraints | ||
| 540 | - SAFE_UPDATE update with care to wh constrains | ||
| 541 | - DELETE delete exemplars | ||
| 542 | |||
| 543 | %values - hash packed on the list where each pair consists attr=>value; for UPDATE only | ||
| 544 | $clause - hashref defines elements of class that will change; {attr1=>{gt=>25},...} | ||
| 545 | |||
| 546 | Returns: | ||
| 547 | true - if ok | ||
| 548 | false - otherwise | ||
| 549 | =cut | ||
| 550 | sub Global { | ||
| 551 | my ($either, $action) = splice @_, 0, 2; | ||
| 552 | my $class = ref $either || $either; | ||
| 553 | |||
| 554 | my $clause; | ||
| 555 | $clause = pop if @_ % 2; | ||
| 556 | |||
| 557 | my @val = @_; | ||
| 558 | |||
| 559 | given ($action) { | ||
| 560 | when ('UPDATE') { | ||
| 561 | my %values = @_ or return; | ||
| 562 | |||
| 563 | $WH->global_update($class, \%values, $clause); | ||
| 564 | } | ||
| 565 | when ('SAFE_UPDATE') { | ||
| 566 | my $val = @val == 1 ? shift @val : {@val}; | ||
| 567 | debug 'CROPOBJECT_GLOBAL_CLASS=', $class; | ||
| 568 | debug 'CROPOBJECT_GLOBAL_VAL=', $val; | ||
| 569 | debug 'CROPOBJECT_GLOBAL_CLAUSE=', $clause; | ||
| 570 | |||
| 571 | $WH->global_safe_update($class, $val, $clause) | ||
| 572 | } | ||
| 573 | when ('DELETE') { $WH->global_delete($class, $clause) } | ||
| 574 | |||
| 575 | default { return warn "OBJECT|CRIT: No such action '$action' for global operation" } | ||
| 576 | } | ||
| 577 | } | ||
| 578 | |||
| 579 | =begin nd | ||
| 580 | Method: Increase (@val, @clause) | ||
| 581 | Safely update all the exemplars in a class. | ||
| 582 | |||
| 583 | Use it where warehouse constrains broke consistency correctness of intermediate operations. | ||
| 584 | |||
| 585 | Action is performed immediately. | ||
| 586 | |||
| 587 | >lkey => +2, rkey => +2, {lkey => {GE => $self->{rkey}}, | ||
| 588 | Param: | ||
| 589 | @val - attr1=>val1,attr2=>val2 ... pairs to be step-by-step updated | ||
| 590 | @clause - select clauses, each packed in a hashref | ||
| 591 | |||
| 592 | Returns: | ||
| 593 | true - ok | ||
| 594 | false - error | ||
| 595 | =cut | ||
| 596 | sub Increase { | ||
| 597 | my $either = shift; | ||
| 598 | my $class = ref $either || $either; | ||
| 599 | |||
| 600 | $WH->increase($class, @_); | ||
| 601 | } | ||
| 602 | |||
| 603 | =begin nd | ||
| 604 | Method: _Is_key_defined ( ) | ||
| 605 | If an exemplar has a key attibutes fully defined. | ||
| 606 | |||
| 607 | This method can be redefined by multi-ancestors, so the 'generate-cache-call' pattern is ommited. | ||
| 608 | |||
| 609 | Returns: | ||
| 610 | true - is defined | ||
| 611 | false - otherwise | ||
| 612 | =cut | ||
| 613 | sub _Is_key_defined { | ||
| 614 | my $self = shift; | ||
| 615 | my $class = ref $self; | ||
| 616 | |||
| 617 | my $keys = $self->Attributes(KEY); | ||
| 618 | return warn "OBJECT|CRIT: No keys found for class $class" unless @$keys; | ||
| 619 | |||
| 620 | defined $self->{$_->name} or return for @$keys; | ||
| 621 | |||
| 622 | 1; | ||
| 623 | } | ||
| 624 | |||
| 625 | =begin nd | ||
| 626 | Method: Max ($attr) | ||
| 627 | Get maximum of $attr value in all the class. | ||
| 628 | |||
| 629 | Method of class either method of exemplar. | ||
| 630 | Param: | ||
| 631 | $attr - attribute name | ||
| 632 | |||
| 633 | Returns: | ||
| 634 | Maximum value of $attr across all the class exemplars. | ||
| 635 | =cut | ||
| 636 | sub Max { | ||
| 637 | my ($either, $attr) = @_; | ||
| 638 | my $class = ref $either || $either; | ||
| 639 | # debug 'CROPOBJECT_MAX_ATTR=', $attr; | ||
| 640 | |||
| 641 | $WH->max($class, $attr); | ||
| 642 | } | ||
| 643 | |||
| 644 | =begin nd | ||
| 645 | Method: Modified ($modify) | ||
| 646 | Set or unset the 'MODIFIED' flag. | ||
| 647 | |||
| 648 | Parameters: | ||
| 649 | $state - if defined and if is FALSE than unset flag; optional | ||
| 650 | |||
| 651 | Returns: | ||
| 652 | $self | ||
| 653 | =cut | ||
| 654 | sub Modified { | ||
| 655 | my ($self, $modify) = @_; | ||
| 656 | $modify //= 1; | ||
| 657 | |||
| 658 | $modify ? $self->Set_state(MODIFIED) : $self->Remove_state(MODIFIED); | ||
| 659 | |||
| 660 | $self; | ||
| 661 | } | ||
| 662 | |||
| 663 | =begin nd | ||
| 664 | Method: Nosync ( ) | ||
| 665 | Mark an object as unsynced. | ||
| 666 | =cut | ||
| 667 | sub Nosync { | ||
| 668 | my $self = shift; | ||
| 669 | |||
| 670 | $self->Set_state(NOSYNC); | ||
| 671 | } | ||
| 672 | |||
| 673 | =begin | ||
| 674 | Method: _Prepare_key ( ) | ||
| 675 | Try to prepare the key for insert. | ||
| 676 | |||
| 677 | Pure virtual. | ||
| 678 | |||
| 679 | Arise an error. | ||
| 680 | |||
| 681 | Returns: | ||
| 682 | false | ||
| 683 | =cut | ||
| 684 | sub _Prepare_key { | ||
| 685 | my $self = shift; | ||
| 686 | my $class = ref $self; | ||
| 687 | |||
| 688 | return warn "OBJECT: _Prepare_key method must be redefined by subclass $class"; | ||
| 689 | } | ||
| 690 | |||
| 691 | =begin nd | ||
| 692 | Method: Remove ( ) | ||
| 693 | Delete exemplar from the warehouse immediately. | ||
| 694 | |||
| 695 | Returns: | ||
| 696 | undef | ||
| 697 | =cut | ||
| 698 | sub Remove { | ||
| 699 | my $self = shift; | ||
| 700 | my $class = ref $self; | ||
| 701 | |||
| 702 | return warn "OBJECT|ERR: Can't remove object with no Table specified in class $class" unless $self->Table; | ||
| 703 | |||
| 704 | $WH->remove($self); | ||
| 705 | $self->{STATE} |= REMOVED; | ||
| 706 | |||
| 707 | undef $self; | ||
| 708 | } | ||
| 709 | |||
| 710 | =begin nd | ||
| 711 | Method: Remove_State ($flags) | ||
| 712 | Remove $flags from STATE. | ||
| 713 | |||
| 714 | Parameters: | ||
| 715 | $flags - the 'OR'ed flags to remove | ||
| 716 | |||
| 717 | Returns: | ||
| 718 | $self | ||
| 719 | =cut | ||
| 720 | sub Remove_State { | ||
| 721 | my ($self, $flags) = @_; | ||
| 722 | |||
| 723 | $self->{STATE} &= ~$flags; | ||
| 724 | |||
| 725 | $self; | ||
| 726 | } | ||
| 727 | |||
| 728 | =begin nd | ||
| 729 | Method: Save ( ) | ||
| 730 | Save current exemplar state to the warehouse. | ||
| 731 | |||
| 732 | In case of multi-ancestors the preparation walks through all the extra parents first. | ||
| 733 | |||
| 734 | Returns: | ||
| 735 | $self - if saved successfully | ||
| 736 | undef - otherwise | ||
| 737 | =cut | ||
| 738 | sub Save { | ||
| 739 | my $self = shift; | ||
| 740 | my $class = ref $self; | ||
| 741 | |||
| 742 | return warn "OBJECT|ERR: Can't Save object with no TABLE specified for class $class" unless defined $self->Table; | ||
| 743 | |||
| 744 | if ($self->{STATE} & DWHLINK) { # object is in the Warehouse | ||
| 745 | return $self unless $self->{STATE} & MODIFIED; # object corresponds to the state in the Warehouse | ||
| 746 | $WH->refresh($self); | ||
| 747 | } else { # object is missing in the Warehouse | ||
| 748 | my $isa; | ||
| 749 | { | ||
| 750 | no strict 'refs'; | ||
| 751 | $isa = \@{$class . '::ISA'}; | ||
| 752 | } | ||
| 753 | my @isa_original = @$isa; | ||
| 754 | |||
| 755 | my $err; | ||
| 756 | if (my $extra_parent = @isa_original - 1) { | ||
| 757 | while ($extra_parent--) { | ||
| 758 | my $parent = pop @$isa; | ||
| 759 | unshift @$isa, $parent; | ||
| 760 | |||
| 761 | $self->_Is_key_defined or $self->_Prepare_key or ++$err, last; # once @ISA has been modified, return not allowed | ||
| 762 | } | ||
| 763 | @$isa = @isa_original; | ||
| 764 | |||
| 765 | return warn "OBJECT|ERR: Can't Generate Primary key either required attributes for class $class" if $err; | ||
| 766 | } | ||
| 767 | |||
| 768 | $self->_Is_key_defined or $self->_Prepare_key or return warn "OBJECT|ERR: Can't Generate Primary key either required attributes for class $class"; | ||
| 769 | $self->_Create; | ||
| 770 | } | ||
| 771 | $self->{STATE} |= DWHLINK; | ||
| 772 | $self->{STATE} &= ~MODIFIED; | ||
| 773 | |||
| 774 | $self; | ||
| 775 | } | ||
| 776 | |||
| 777 | =begin nd | ||
| 778 | Method: Set_state ($flags) | ||
| 779 | Set up STATE-$flags. | ||
| 780 | |||
| 781 | Parameters: | ||
| 782 | $flags - binary mask to be ORed | ||
| 783 | |||
| 784 | Returns: | ||
| 785 | $self | ||
| 786 | =cut | ||
| 787 | sub Set_state { | ||
| 788 | my ($self, $flags) = @_; | ||
| 789 | |||
| 790 | $self->{STATE} |= $flags; | ||
| 791 | |||
| 792 | $self; | ||
| 793 | } | ||
| 794 | |||
| 795 | =begin nd | ||
| 796 | Method: Table ( ) | ||
| 797 | Return false for Objects that should not be saved to Warehouse. | ||
| 798 | |||
| 799 | Returns: | ||
| 800 | false | ||
| 801 | =cut | ||
| 802 | sub Table { } | ||
| 803 | |||
| 804 | =begin nd | ||
| 805 | Method: TO_JSON ($data) | ||
| 806 | Prepare blessed objects for JSON output. | ||
| 807 | |||
| 808 | All the objects are a blessed hashrefs, so will return UNblessed original hashref. | ||
| 809 | |||
| 810 | Removes the 'STATE' container from result. | ||
| 811 | |||
| 812 | This metod is called automatically by JSON module for each object. | ||
| 813 | |||
| 814 | Parameters: | ||
| 815 | $data - blessed hashref | ||
| 816 | |||
| 817 | Returns: | ||
| 818 | unblessed hashref | ||
| 819 | =cut | ||
| 820 | sub TO_JSON { | ||
| 821 | my $data = shift; | ||
| 822 | |||
| 823 | my $json = {%$data}; | ||
| 824 | delete $json->{STATE}; | ||
| 825 | |||
| 826 | $json; | ||
| 827 | } | ||
| 828 | |||
| 829 | =begin nd | ||
| 830 | Method: WH ( ) | ||
| 831 | Get WareHouse object. | ||
| 832 | |||
| 833 | User do not should use this method directly at top-level code. | ||
| 834 | |||
| 835 | Returns: | ||
| 836 | Warehouse object <Crop::Object::Warehouse> | ||
| 837 | =cut | ||
| 838 | sub WH { $WH } | ||
| 839 | |||
| 840 | 1; |