source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Attributes.pm @ 1847

Last change on this file since 1847 was 1839, checked in by nanardon, 8 years ago

Fix value test on undef list

  • Property svn:keywords set to Id
File size: 10.9 KB
Line 
1package LATMOS::Accounts::Bases::Attributes;
2
3# $Id$
4
5use strict;
6use warnings;
7use LATMOS::Accounts::Log;
8
9use Date::Parse;
10use DateTime;
11use DateTime::TimeZone;
12
13
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
22Instanciate a new Attributes object.
23
24=cut
25
26sub 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
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;
42
43        $attr_info->{_base} = $base;
44        $attr_info->{_object} = $object;
45        $attr_info->{_name} = $attributes;
46        $attr_info->{_otype} = $otype;
47
48        return bless($attr_info, $class);
49    }
50}
51
52=head2 base
53
54Return base handle
55
56=cut
57
58sub base  { $_[0]->{_base}  }
59
60=head2 name
61
62Return attribute name
63
64=cut
65
66sub name  { $_[0]->{_name}  }
67
68=head2 otype
69
70Return object type for this attribute
71
72=cut
73
74
75sub otype { $_[0]->{_otype} }
76
77=head2 mandatory
78
79Return true if attribute is mandatory
80
81=cut
82
83sub mandatory { $_[0]->{mandatory} || 0 }
84
85=head2 object
86
87Return handle object if any
88
89=cut
90
91sub object { $_[0]->{_object} }
92
93=head2 reference
94
95A object type this attribute refer to
96
97=cut
98
99sub 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
109=head2 iname
110
111Return internal name of attributes
112
113=cut
114
115sub iname { $_[0]->{iname} || $_[0]->name }
116
117=head2 label
118
119Return the label to display
120
121=cut
122
123sub label { $_[0]->{label} || $_[0]->{_name} }
124
125=head2 has_values_list
126
127Return true if the attribute have a fixed list of accepted values
128
129=cut
130
131sub 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
144=head2 can_values
145
146Return possible value allowed by this attribute
147
148=cut
149
150sub 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') {
160            $self->{can_values}->($self, $self->object);
161        } else {
162            return;
163        }
164    } elsif (my $ref = $self->reference) {
165        return $self->base->list_objects($ref);
166    } else { return }
167}
168
169=head2 display ($value)
170
171Return the well formated value according attribute
172
173=cut
174
175sub display {
176    my ($self, $value) = @_;
177    if ($self->{display}) {
178        return $self->{display}->($self, $value);
179    } else {
180        return $value;
181    }
182}
183
184=head2 input ($value)
185
186Return well formated single value for insert into base
187
188=cut
189
190sub input {
191    my ($self, $value) = @_;
192    if ($self->{input}) {
193        if (!defined($value)) { return }
194        return $self->{input}->($value);
195    } elsif ($self->real_form_type =~ /^(DATE|DATETIME)$/) {
196        if (!$value) {
197            return;
198        } else {
199            $value =~ s:^(\d+)/(\d+)/(\d+):$2/$1/$3:;
200            my $epoch = str2time($value);
201            return $value if (!defined($epoch));
202            my $dt = DateTime->from_epoch(epoch => str2time($value));
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        }
210    } else {
211        return $value;
212    }
213}
214
215=head2 checkinputformat ($value)
216
217Check input value format, return false on error
218
219=cut
220
221sub 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
233=head2 checkinput ($value)
234
235Check input value, return false on error
236
237=cut
238
239sub checkinput {
240    my ($self, $values) = @_;
241
242    foreach my $value (grep { $_ } (ref $values ? @{ $values } : $values)) {
243        if (!$self->checkinputformat($value)) {
244            $self->base->log(LA_ERR, "Wrong format for  %s/%s: %s",
245               $self->name,
246               $self->{_otype},
247               $value
248            );
249            return;
250        }
251    }
252
253    if ($self->mandatory &&
254        (!(defined($values)) || $values eq '')) {
255        $self->base->log(LA_ERR,
256            "%s attribute cannot be empty for object %s",
257            $self->name,
258            $self->{_otype},
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,
273                ) if ($self->object);
274                $self->base->log(LA_DEBUG, "Possible value for %s: %s",
275                    $self->name,
276                    join(', ', @possible)
277                );
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}) {
290        foreach my $val (ref $values ? @{ $values } : $values) {
291            if (!$self->{checkinput}->($val)) {
292                return;
293            }
294        }
295    }
296
297    return 1;
298}
299
300=head2 readable
301
302Return true if attribute can be read
303
304=cut
305
306sub 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
315=head2 ro
316
317Return true if the attribute cannot be write by design
318
319=cut
320
321sub 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
332Return true if attribute cannot be read according acls or attributes state
333
334=cut
335
336sub readonly { 
337    my ($self) = @_;
338    return 1 if ($self->ro);
339   
340    return ! $self->check_acl('w');
341}
342
343=head2 check_acl ($mode)
344
345Return true is access to C<$mode> is granted
346
347=cut
348
349sub check_acl {
350    my ($self, $mode) = @_;
351
352    return 1 if ($self->{_noacl});
353
354    return $self->base->check_acl($self->object
355        ? ($self->object, $self->name, $mode)
356        : ($self->otype, '@CREATE', $mode));
357}
358
359=head2 real_form_type
360
361Return the way the fields must be show in GUI.
362For each type option maybe given by form_option
363
364=head3 LABEL
365
366=over 4
367
368=item length
369
370The length to use to show the attribute
371
372=back
373
374=head3 TEXT
375
376=head3 TEXTAREA
377
378=head3 DATE
379
380=head3 LIST
381
382=head3 CHECKBOX
383
384=over 4
385
386=item rawvalue
387
388The real value of the attribute must be show
389
390=back
391
392=cut
393
394sub real_form_type { $_[0]->{formtype} || 'TEXT' }
395
396=head2 form_type
397
398Return the way the attribute must be displayed
399
400=cut
401
402sub form_type {
403    $_[0]->readonly ? 'LABEL' :
404    $_[0]->{formtype} ? $_[0]->{formtype} :
405    $_[0]->has_values_list ? 'LIST' :
406    $_[0]->real_form_type
407}
408
409=head2 form_option ($option)
410
411Return the form option C<$option>
412
413=cut
414
415sub form_option {
416    my ($self, $option) = @_;
417    return $self->{formopts}{$option}
418}
419
420=head2 uniq
421
422Return true is attribute value must be uniq
423
424=cut
425
426sub uniq { $_[0]->{uniq} || 0 }
427
428=head2 multiple
429
430Return true is attribute value can be set several times
431
432=cut
433
434sub multiple { $_[0]->{multiple} || 0 }
435
436=head2 hidden
437
438Return true if attributes must not appear in list by can be query.
439
440=cut
441
442sub hidden { $_[0]->{hide} || 0 }
443
444=head2 delayed
445
446Return true if attribute must be set after object creation during
447synchronisation
448
449=cut
450
451sub delayed { $_[0]->{delayed} || 0 }
452
453=head2 get
454
455Return the value for this attribute
456
457=cut
458
459sub get {
460    my ($self) = @_;
461
462    if (defined($self->object)) {
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
469        if (!defined($res)) {
470            return undef;
471        } elsif (ref $res) {
472            return $res;
473        } elsif ($self->multiple) {
474            return (ref $res ? $res : [$res])
475        } else {
476            return $res;
477        }
478    } else {
479        return;
480    }
481}
482
483=head2 defaultValue
484
485Return the default static value for this attribute.
486
487=cut
488
489sub defaultValue {
490    my ($self) = @_;
491
492    return $self->base->defaultAttributeValue($self->otype, $self->name)
493}
494
495=head2 getValues
496
497Return value for this attribute, results are always return as an array
498
499=cut
500
501sub getValues {
502    my ($self) = @_;
503
504    my $res = $self->get();
505
506    return ref $res ? grep { $_ } @{ $res } : $res;
507}
508
509
510=head2 set ($values)
511
512Set attribute value to attached object
513
514=cut
515
516sub set {
517    my ($self, $values) = @_;
518
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);
530    }
531
532    $res
533}
534
535=head2 default_value
536
537Return default value for this attribute
538
539=cut
540
541sub default_value {
542    my ($self) = @_;
543    $self->defaultValue;
544}
545
546=head2 monitored
547
548Return true if the attribute is monitored
549
550=cut
551
552sub 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
5631;
564
565__END__
566
567=head1 SEE ALSO
568
569L<LATMOS::Accounts::Bases>
570
571=head1 AUTHOR
572
573Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
574
575=head1 COPYRIGHT AND LICENSE
576
577Copyright (C) 2012 CNRS SA/CETP/LATMOS
578
579This library is free software; you can redistribute it and/or modify
580it under the same terms as Perl itself, either Perl version 5.10.0 or,
581at your option, any later version of Perl 5 you may have available.
582
583=cut
Note: See TracBrowser for help on using the repository browser.