[852] | 1 | package LATMOS::Accounts::Bases::Attributes; |
---|
| 2 | |
---|
| 3 | # $Id$ |
---|
| 4 | |
---|
| 5 | use strict; |
---|
| 6 | use warnings; |
---|
| 7 | use LATMOS::Accounts::Log; |
---|
| 8 | |
---|
[1722] | 9 | use Date::Parse; |
---|
| 10 | use DateTime; |
---|
| 11 | use DateTime::TimeZone; |
---|
| 12 | |
---|
| 13 | |
---|
[852] | 14 | =head1 NAME |
---|
| 15 | |
---|
| 16 | LATMOS::Accounts::Bases::Attributes - Object to handle attribute schema |
---|
| 17 | |
---|
| 18 | =head1 FUNCTIONS |
---|
| 19 | |
---|
| 20 | =head2 new($attributes, $base_or_object, $maybe_otype) |
---|
| 21 | |
---|
[1023] | 22 | Instanciate a new Attributes object. |
---|
| 23 | |
---|
[852] | 24 | =cut |
---|
| 25 | |
---|
| 26 | sub new { |
---|
| 27 | my ($class, $attributes, $base_or_object, $maybe_otype) = @_; |
---|
| 28 | |
---|
| 29 | my ($object, $base, $otype) = |
---|
| 30 | $base_or_object->isa('LATMOS::Accounts::Bases::Objects') |
---|
| 31 | ? ($base_or_object, $base_or_object->base, $base_or_object->type) |
---|
| 32 | : (undef, $base_or_object, $maybe_otype); |
---|
| 33 | |
---|
[949] | 34 | if (ref $attributes) { |
---|
| 35 | $attributes->{_base} = $base; |
---|
| 36 | $attributes->{_object} = $object; |
---|
| 37 | $attributes->{_name} = $attributes->{name}; |
---|
| 38 | $attributes->{_otype} = $otype; |
---|
| 39 | return bless($attributes, $class); |
---|
| 40 | } else { |
---|
| 41 | my $attr_info = $base->get_attr_schema($otype, $attributes) or return; |
---|
[852] | 42 | |
---|
[949] | 43 | $attr_info->{_base} = $base; |
---|
| 44 | $attr_info->{_object} = $object; |
---|
| 45 | $attr_info->{_name} = $attributes; |
---|
| 46 | $attr_info->{_otype} = $otype; |
---|
[852] | 47 | |
---|
[949] | 48 | return bless($attr_info, $class); |
---|
| 49 | } |
---|
[852] | 50 | } |
---|
| 51 | |
---|
[1023] | 52 | =head2 base |
---|
| 53 | |
---|
| 54 | Return base handle |
---|
| 55 | |
---|
| 56 | =cut |
---|
| 57 | |
---|
[852] | 58 | sub base { $_[0]->{_base} } |
---|
[1023] | 59 | |
---|
| 60 | =head2 name |
---|
| 61 | |
---|
| 62 | Return attribute name |
---|
| 63 | |
---|
| 64 | =cut |
---|
| 65 | |
---|
[852] | 66 | sub name { $_[0]->{_name} } |
---|
[1023] | 67 | |
---|
| 68 | =head2 otype |
---|
| 69 | |
---|
| 70 | Return object type for this attribute |
---|
| 71 | |
---|
| 72 | =cut |
---|
| 73 | |
---|
| 74 | |
---|
[852] | 75 | sub otype { $_[0]->{_otype} } |
---|
[1023] | 76 | |
---|
| 77 | =head2 mandatory |
---|
| 78 | |
---|
| 79 | Return true if attribute is mandatory |
---|
| 80 | |
---|
| 81 | =cut |
---|
| 82 | |
---|
[861] | 83 | sub mandatory { $_[0]->{mandatory} || 0 } |
---|
[1023] | 84 | |
---|
| 85 | =head2 object |
---|
| 86 | |
---|
| 87 | Return handle object if any |
---|
| 88 | |
---|
| 89 | =cut |
---|
| 90 | |
---|
[861] | 91 | sub object { $_[0]->{_object} } |
---|
[852] | 92 | |
---|
[861] | 93 | =head2 reference |
---|
| 94 | |
---|
| 95 | A object type this attribute refer to |
---|
| 96 | |
---|
| 97 | =cut |
---|
| 98 | |
---|
| 99 | sub reference { |
---|
| 100 | my ($self) = @_; |
---|
| 101 | if ($self->{reference} && |
---|
| 102 | $self->base->is_supported_object($self->{reference})) { |
---|
| 103 | return $self->{reference}; |
---|
| 104 | } else { |
---|
| 105 | return; |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
[1023] | 109 | =head2 iname |
---|
[857] | 110 | |
---|
| 111 | Return internal name of attributes |
---|
| 112 | |
---|
| 113 | =cut |
---|
| 114 | |
---|
| 115 | sub iname { $_[0]->{iname} || $_[0]->name } |
---|
| 116 | |
---|
[1023] | 117 | =head2 label |
---|
| 118 | |
---|
| 119 | Return the label to display |
---|
| 120 | |
---|
| 121 | =cut |
---|
| 122 | |
---|
[852] | 123 | sub label { $_[0]->{label} || $_[0]->{_name} } |
---|
| 124 | |
---|
[1023] | 125 | =head2 has_values_list |
---|
| 126 | |
---|
| 127 | Return true if the attribute have a fixed list of accepted values |
---|
| 128 | |
---|
| 129 | =cut |
---|
| 130 | |
---|
[861] | 131 | sub has_values_list { |
---|
| 132 | my ($self) = @_; |
---|
| 133 | if ($self->base->obj_attr_allowed_values( |
---|
| 134 | $self->otype, |
---|
| 135 | $self->name) || |
---|
| 136 | $self->{can_values} || |
---|
| 137 | $self->reference) { |
---|
| 138 | return 1; |
---|
| 139 | } else { |
---|
| 140 | return 0; |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | |
---|
[1023] | 144 | =head2 can_values |
---|
| 145 | |
---|
| 146 | Return possible value allowed by this attribute |
---|
| 147 | |
---|
| 148 | =cut |
---|
| 149 | |
---|
[852] | 150 | sub can_values { |
---|
| 151 | my ($self) = @_; |
---|
| 152 | if (my @values = $self->base->obj_attr_allowed_values( |
---|
| 153 | $self->otype, |
---|
| 154 | $self->name)) { |
---|
| 155 | return @values; |
---|
| 156 | } elsif ($self->{can_values}) { |
---|
| 157 | if (ref $self->{can_values} eq 'ARRAY') { |
---|
| 158 | return @{$self->{can_values}}; |
---|
| 159 | } elsif (ref $self->{can_values} eq 'CODE') { |
---|
[861] | 160 | $self->{can_values}->($self, $self->object); |
---|
[852] | 161 | } else { |
---|
| 162 | return; |
---|
| 163 | } |
---|
[861] | 164 | } elsif (my $ref = $self->reference) { |
---|
[1699] | 165 | return $self->base->list_objects($ref); |
---|
[852] | 166 | } else { return } |
---|
| 167 | } |
---|
| 168 | |
---|
[1023] | 169 | =head2 display ($value) |
---|
| 170 | |
---|
| 171 | Return the well formated value according attribute |
---|
| 172 | |
---|
| 173 | =cut |
---|
| 174 | |
---|
[861] | 175 | sub display { |
---|
| 176 | my ($self, $value) = @_; |
---|
| 177 | if ($self->{display}) { |
---|
| 178 | return $self->{display}->($self, $value); |
---|
| 179 | } else { |
---|
| 180 | return $value; |
---|
| 181 | } |
---|
| 182 | } |
---|
[857] | 183 | |
---|
[1023] | 184 | =head2 input ($value) |
---|
| 185 | |
---|
| 186 | Return well formated single value for insert into base |
---|
| 187 | |
---|
| 188 | =cut |
---|
| 189 | |
---|
[861] | 190 | sub input { |
---|
| 191 | my ($self, $value) = @_; |
---|
| 192 | if ($self->{input}) { |
---|
[1456] | 193 | if (!defined($value)) { return } |
---|
[861] | 194 | return $self->{input}->($value); |
---|
[1722] | 195 | } elsif ($self->real_form_type =~ /^(DATE|DATETIME)$/) { |
---|
| 196 | if (!$value) { |
---|
| 197 | return; |
---|
| 198 | } else { |
---|
| 199 | $value =~ s:^(\d+)/(\d+)/(\d+):$2/$1/$3:; |
---|
[1725] | 200 | my $epoch = str2time($value); |
---|
| 201 | return $value if (!defined($epoch)); |
---|
| 202 | my $dt = DateTime->from_epoch(epoch => str2time($value)); |
---|
[1722] | 203 | $dt->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); |
---|
| 204 | if ($self->real_form_type eq 'DATE') { |
---|
| 205 | return $dt->ymd('-'); |
---|
| 206 | } else { |
---|
| 207 | return $dt->ymd('-') . ' ' . $dt->hms(':'); |
---|
| 208 | } |
---|
| 209 | } |
---|
[861] | 210 | } else { |
---|
| 211 | return $value; |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | |
---|
[1415] | 215 | =head2 checkinputformat ($value) |
---|
| 216 | |
---|
| 217 | Check input value format, return false on error |
---|
| 218 | |
---|
| 219 | =cut |
---|
| 220 | |
---|
| 221 | sub checkinputformat { |
---|
| 222 | my ($self, $value) = @_; |
---|
| 223 | |
---|
| 224 | if ($self->{checkinputformat}) { |
---|
| 225 | if (!$self->{checkinputformat}->($value)) { |
---|
| 226 | return; |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | return 1; |
---|
| 231 | } |
---|
| 232 | |
---|
[1315] | 233 | =head2 checkinput ($value) |
---|
| 234 | |
---|
| 235 | Check input value, return false on error |
---|
| 236 | |
---|
| 237 | =cut |
---|
| 238 | |
---|
| 239 | sub checkinput { |
---|
| 240 | my ($self, $values) = @_; |
---|
| 241 | |
---|
[1415] | 242 | foreach my $value (ref $values ? @{ $values } : $values) { |
---|
| 243 | if (!$self->checkinputformat($value)) { |
---|
[1416] | 244 | $self->base->log(LA_ERR, "Wrong format for %s/%s: %s", |
---|
| 245 | $self->name, |
---|
| 246 | $self->{_otype}, |
---|
| 247 | $value |
---|
| 248 | ); |
---|
[1415] | 249 | return; |
---|
| 250 | } |
---|
| 251 | } |
---|
| 252 | |
---|
[1315] | 253 | if ($self->mandatory && |
---|
| 254 | (!(defined($values)) || $values eq '')) { |
---|
| 255 | $self->base->log(LA_ERR, |
---|
[1415] | 256 | "%s attribute cannot be empty for object %s", |
---|
[1315] | 257 | $self->name, |
---|
[1415] | 258 | $self->{_otype}, |
---|
[1315] | 259 | ); |
---|
| 260 | return; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | if ($self->has_values_list && $values) { |
---|
| 264 | my @possible = $self->can_values; |
---|
| 265 | foreach my $value (ref $values ? @{ $values } : $values) { |
---|
| 266 | if (! grep { $value eq $_ } @possible) { |
---|
| 267 | $self->base->log(LA_ERR, |
---|
| 268 | "%s attribute cannot have `%s' as value for object %s/%s", |
---|
| 269 | $self->name, |
---|
| 270 | $value, |
---|
| 271 | $self->object->type, |
---|
| 272 | $self->object->id, |
---|
[1396] | 273 | ) if ($self->object); |
---|
[1357] | 274 | $self->base->log(LA_DEBUG, "Possible value for %s: %s", |
---|
| 275 | $self->name, |
---|
| 276 | join(', ', @possible) |
---|
| 277 | ); |
---|
[1315] | 278 | return; |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | if (ref $values eq 'ARRAY' && ! $self->multiple) { |
---|
| 284 | $self->base->log(LA_WARN, 'Attribute %s is not multi valuesd', $self->name); |
---|
| 285 | # TODO: really return an error |
---|
| 286 | # return; |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | if ($self->{checkinput}) { |
---|
[1360] | 290 | foreach my $val (ref $values ? @{ $values } : $values) { |
---|
| 291 | if (!$self->{checkinput}->($val)) { |
---|
| 292 | return; |
---|
| 293 | } |
---|
[1315] | 294 | } |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | return 1; |
---|
| 298 | } |
---|
| 299 | |
---|
[1023] | 300 | =head2 readable |
---|
[861] | 301 | |
---|
[1023] | 302 | Return true if attribute can be read |
---|
| 303 | |
---|
| 304 | =cut |
---|
| 305 | |
---|
[933] | 306 | sub readable { |
---|
| 307 | my ($self) = @_; |
---|
| 308 | if (ref $self->{readable} eq 'CODE') { |
---|
| 309 | return $self->{readable}->($self->object) || 0; |
---|
| 310 | } else { |
---|
| 311 | return defined($_[0]->{readable}) ? $_[0]->{readable} : 1; |
---|
| 312 | } |
---|
| 313 | } |
---|
| 314 | |
---|
[1023] | 315 | =head2 ro |
---|
| 316 | |
---|
| 317 | Return true if the attribute cannot be write by design |
---|
| 318 | |
---|
| 319 | =cut |
---|
| 320 | |
---|
| 321 | sub ro { |
---|
| 322 | my ($self) = @_; |
---|
| 323 | if (ref $self->{ro} eq 'CODE') { |
---|
| 324 | return $self->{ro}->($self->object) || 0; |
---|
| 325 | } else { |
---|
| 326 | return $_[0]->{ro} || 0 |
---|
| 327 | } |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | =head2 readonly |
---|
| 331 | |
---|
| 332 | Return true if attribute cannot be read according acls or attributes state |
---|
| 333 | |
---|
| 334 | =cut |
---|
| 335 | |
---|
[857] | 336 | sub readonly { |
---|
[852] | 337 | my ($self) = @_; |
---|
[857] | 338 | return 1 if ($self->ro); |
---|
[852] | 339 | |
---|
[861] | 340 | return ! $self->check_acl('w'); |
---|
[852] | 341 | } |
---|
| 342 | |
---|
[1023] | 343 | =head2 check_acl ($mode) |
---|
| 344 | |
---|
| 345 | Return true is access to C<$mode> is granted |
---|
| 346 | |
---|
| 347 | =cut |
---|
| 348 | |
---|
[861] | 349 | sub check_acl { |
---|
| 350 | my ($self, $mode) = @_; |
---|
[959] | 351 | |
---|
| 352 | return 1 if ($self->{_noacl}); |
---|
| 353 | |
---|
[861] | 354 | return $self->base->check_acl($self->object |
---|
| 355 | ? ($self->object, $self->name, $mode) |
---|
| 356 | : ($self->otype, '@CREATE', $mode)); |
---|
| 357 | } |
---|
| 358 | |
---|
[1023] | 359 | =head2 real_form_type |
---|
[852] | 360 | |
---|
[861] | 361 | Return the way the fields must be show in GUI. |
---|
[1023] | 362 | For each type option maybe given by form_option |
---|
[852] | 363 | |
---|
[861] | 364 | =head3 LABEL |
---|
| 365 | |
---|
[852] | 366 | =over 4 |
---|
| 367 | |
---|
[861] | 368 | =item length |
---|
[852] | 369 | |
---|
[861] | 370 | The length to use to show the attribute |
---|
[852] | 371 | |
---|
[861] | 372 | =back |
---|
[852] | 373 | |
---|
[861] | 374 | =head3 TEXT |
---|
[852] | 375 | |
---|
[861] | 376 | =head3 TEXTAREA |
---|
[852] | 377 | |
---|
[861] | 378 | =head3 DATE |
---|
| 379 | |
---|
| 380 | =head3 LIST |
---|
| 381 | |
---|
| 382 | =head3 CHECKBOX |
---|
| 383 | |
---|
| 384 | =over 4 |
---|
| 385 | |
---|
| 386 | =item rawvalue |
---|
| 387 | |
---|
| 388 | The real value of the attribute must be show |
---|
| 389 | |
---|
[852] | 390 | =back |
---|
| 391 | |
---|
| 392 | =cut |
---|
| 393 | |
---|
[861] | 394 | sub real_form_type { $_[0]->{formtype} || 'TEXT' } |
---|
[852] | 395 | |
---|
[1023] | 396 | =head2 form_type |
---|
| 397 | |
---|
| 398 | Return the way the attribute must be displayed |
---|
| 399 | |
---|
| 400 | =cut |
---|
| 401 | |
---|
[861] | 402 | sub form_type { |
---|
| 403 | $_[0]->readonly ? 'LABEL' : |
---|
| 404 | $_[0]->{formtype} ? $_[0]->{formtype} : |
---|
| 405 | $_[0]->has_values_list ? 'LIST' : |
---|
| 406 | $_[0]->real_form_type |
---|
| 407 | } |
---|
| 408 | |
---|
[1023] | 409 | =head2 form_option ($option) |
---|
| 410 | |
---|
| 411 | Return the form option C<$option> |
---|
| 412 | |
---|
| 413 | =cut |
---|
| 414 | |
---|
[861] | 415 | sub form_option { |
---|
| 416 | my ($self, $option) = @_; |
---|
| 417 | return $self->{formopts}{$option} |
---|
| 418 | } |
---|
| 419 | |
---|
[1023] | 420 | =head2 uniq |
---|
| 421 | |
---|
| 422 | Return true is attribute value must be uniq |
---|
| 423 | |
---|
| 424 | =cut |
---|
| 425 | |
---|
[852] | 426 | sub uniq { $_[0]->{uniq} || 0 } |
---|
| 427 | |
---|
[1023] | 428 | =head2 multiple |
---|
| 429 | |
---|
| 430 | Return true is attribute value can be set several times |
---|
| 431 | |
---|
| 432 | =cut |
---|
| 433 | |
---|
[852] | 434 | sub multiple { $_[0]->{multiple} || 0 } |
---|
| 435 | |
---|
[1023] | 436 | =head2 hidden |
---|
| 437 | |
---|
| 438 | Return true if attributes must not appear in list by can be query. |
---|
| 439 | |
---|
| 440 | =cut |
---|
| 441 | |
---|
[861] | 442 | sub hidden { $_[0]->{hide} || 0 } |
---|
| 443 | |
---|
[1023] | 444 | =head2 delayed |
---|
| 445 | |
---|
| 446 | Return true if attribute must be set after object creation during |
---|
| 447 | synchronisation |
---|
| 448 | |
---|
| 449 | =cut |
---|
| 450 | |
---|
[861] | 451 | sub delayed { $_[0]->{delayed} || 0 } |
---|
| 452 | |
---|
[1329] | 453 | =head2 get |
---|
[936] | 454 | |
---|
| 455 | Return the value for this attribute |
---|
| 456 | |
---|
| 457 | =cut |
---|
| 458 | |
---|
| 459 | sub get { |
---|
| 460 | my ($self) = @_; |
---|
| 461 | |
---|
[1332] | 462 | if (defined($self->object)) { |
---|
[1564] | 463 | my $res = (ref($self->{get}) eq 'CODE') |
---|
| 464 | ? $self->{get}->($self) |
---|
| 465 | : $self->object->get_field($self->iname); |
---|
| 466 | |
---|
| 467 | # always return undef if empty |
---|
| 468 | # and an array if attribute is multivalued |
---|
[1568] | 469 | if (!defined($res)) { |
---|
[1564] | 470 | return undef; |
---|
| 471 | } elsif (ref $res) { |
---|
[1572] | 472 | return $res; |
---|
[1564] | 473 | } elsif ($self->multiple) { |
---|
[1567] | 474 | return (ref $res ? $res : [$res]) |
---|
[1332] | 475 | } else { |
---|
[1564] | 476 | return $res; |
---|
[1332] | 477 | } |
---|
[936] | 478 | } else { |
---|
[1329] | 479 | return; |
---|
[936] | 480 | } |
---|
| 481 | } |
---|
| 482 | |
---|
[1530] | 483 | =head2 defaultValue |
---|
| 484 | |
---|
| 485 | Return the default static value for this attribute. |
---|
| 486 | |
---|
| 487 | =cut |
---|
| 488 | |
---|
| 489 | sub defaultValue { |
---|
| 490 | my ($self) = @_; |
---|
| 491 | |
---|
| 492 | return $self->base->defaultAttributeValue($self->otype, $self->name) |
---|
| 493 | } |
---|
| 494 | |
---|
[1329] | 495 | =head2 getValues |
---|
| 496 | |
---|
| 497 | Return value for this attribute, results are always return as an array |
---|
| 498 | |
---|
| 499 | =cut |
---|
| 500 | |
---|
| 501 | sub getValues { |
---|
| 502 | my ($self) = @_; |
---|
| 503 | |
---|
| 504 | my $res = $self->get(); |
---|
| 505 | |
---|
| 506 | return ref $res ? grep { $_ } @{ $res } : $res; |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | |
---|
[1023] | 510 | =head2 set ($values) |
---|
| 511 | |
---|
| 512 | Set attribute value to attached object |
---|
| 513 | |
---|
| 514 | =cut |
---|
| 515 | |
---|
[936] | 516 | sub set { |
---|
| 517 | my ($self, $values) = @_; |
---|
| 518 | |
---|
[1315] | 519 | my $inputv = ref $values |
---|
| 520 | ? [ map { $self->input($_) } @$values ] |
---|
| 521 | : $values ? $self->input($values) : undef; |
---|
| 522 | |
---|
| 523 | my $res = ref $self->{set} eq 'CODE' |
---|
| 524 | ? $self->{set}->($self, $inputv) |
---|
| 525 | : $self->object->set_fields($self->iname, $inputv); |
---|
| 526 | |
---|
| 527 | # If the attribute has a post code, call it |
---|
| 528 | if ($res && $self->{post}) { |
---|
| 529 | $self->{post}->($self, $inputv); |
---|
[936] | 530 | } |
---|
[1315] | 531 | |
---|
| 532 | $res |
---|
[936] | 533 | } |
---|
| 534 | |
---|
[1023] | 535 | =head2 default_value |
---|
| 536 | |
---|
| 537 | Return default value for this attribute |
---|
| 538 | |
---|
| 539 | =cut |
---|
| 540 | |
---|
[959] | 541 | sub default_value { |
---|
| 542 | my ($self) = @_; |
---|
[1530] | 543 | $self->defaultValue; |
---|
[959] | 544 | } |
---|
| 545 | |
---|
[1297] | 546 | =head2 monitored |
---|
| 547 | |
---|
| 548 | Return true if the attribute is monitored |
---|
| 549 | |
---|
| 550 | =cut |
---|
| 551 | |
---|
| 552 | sub monitored { |
---|
| 553 | my ($self) = @_; |
---|
| 554 | |
---|
| 555 | if ($self->iname ne $self->name) { |
---|
| 556 | my $attr = $self->base->attribute($self->otype, $self->iname) or return; |
---|
| 557 | return $attr->monitored; |
---|
| 558 | } else { |
---|
| 559 | return $self->{monitored} || 0; |
---|
| 560 | } |
---|
| 561 | } |
---|
| 562 | |
---|
[852] | 563 | 1; |
---|
[1023] | 564 | |
---|
| 565 | __END__ |
---|
| 566 | |
---|
| 567 | =head1 SEE ALSO |
---|
| 568 | |
---|
| 569 | L<LATMOS::Accounts::Bases> |
---|
| 570 | |
---|
| 571 | =head1 AUTHOR |
---|
| 572 | |
---|
| 573 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
| 574 | |
---|
| 575 | =head1 COPYRIGHT AND LICENSE |
---|
| 576 | |
---|
| 577 | Copyright (C) 2012 CNRS SA/CETP/LATMOS |
---|
| 578 | |
---|
| 579 | This library is free software; you can redistribute it and/or modify |
---|
| 580 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 581 | at your option, any later version of Perl 5 you may have available. |
---|
| 582 | |
---|
| 583 | =cut |
---|