source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/OCHelper.pm @ 2045

Last change on this file since 2045 was 1985, checked in by nanardon, 7 years ago

Add tools to create multiple object from csv file

File size: 3.1 KB
Line 
1package LATMOS::Accounts::Bases::OCHelper;
2
3# $Id: OCHelper.pm 2932 2010-08-10 17:19:21Z nanardon $
4
5use strict;
6use warnings;
7
8=head1 NAME
9
10LATMOS::Accounts::Bases::OCHelper - Object creation helper
11
12=head1 DESCRIPTION
13
14This module is designed to be subclassed.
15
16=head1 FUNCTIONS
17
18=cut
19
20=head2 new($base, $otype)
21
22=cut
23
24sub new {
25    my ($class, $base, $otype) = @_;
26    bless { 
27        _base => $base,
28        _otype => $otype,
29    }, $class;
30}
31
32=head2 $ochelper->base
33
34Return base object
35
36=cut
37
38sub base { $_[0]->{_base} }
39
40=head2 $ochelper->otype
41
42Return object type for this OChelper.
43
44=cut
45
46sub otype { $_[0]->{_otype} }
47
48=head2 $ochelper->step($info)
49
50Process next step by submitting C<$info> and return the status and new
51information to continue process.
52
53C<$info> must look like:
54
55    info = {
56      step => 0,
57      name => { # name of object
58        ask => 0/1,
59        content => ...
60      },
61      ask => [ list ],
62      contents => { name => ... }
63    }
64
65STATUS will be one of 'NEEDINFO', 'CREATED', 'ERROR', undef.
66
67=cut
68
69sub step {
70    my ($self, $info) = @_;
71    $info ||= {};
72    $info->{step} ||= 0;
73    $info->{ask} = [];
74    $info->{name}{ask} = 0;
75    my $otype = $self->otype;
76    foreach (keys %{ $self->base->{defattr} || {} }) {
77        /^$otype\.(.*)/ or next;
78        my $attr = $1;
79        my $oattr = $self->base->attribute($otype, $attr) or next;
80        $oattr->ro and next;
81        $info->{contents}{$attr} = $self->base->{defattr}{$_}
82            unless exists($info->{contents}{$attr});
83    }
84
85    return($self->_step($info), $info);
86
87}
88
89# just return status, $info is reference
90sub _step {
91    my ($self, $info) = @_;
92
93    if ($info->{step} == 0) {
94        $info->{name}{ask} = 1;
95        foreach ($self->base->list_canonical_fields($self->otype, 'w')) {
96            push(@{$info->{ask}}, $_);
97        }
98        $info->{step} = 1;
99        return 'NEEDINFO';
100    } elsif ($info->{step} == 1) {
101        if ($self->base->create_c_object($self->otype,
102                $info->{name}{content},
103                %{$info->{contents} || {}},
104            )) {
105            return 'CREATED';
106        } else {
107            return 'ERROR';
108        }
109    } else {
110        return undef;
111    }
112}
113
114=head2 $ochelper->Automate($info)
115
116Try to create object from C<$info> w/o interacting with user.
117If given infomation does not allow to create object, it failed.
118
119Return 1 on success.
120
121=cut
122
123sub Automate {
124    my ($self, $info) = @_;
125
126    for (my $count = 0; $count < 3; $count++) {
127        my $status;
128        ($status, $info) = $self->step($info);
129        if ($status eq 'CREATED') {
130            return 1;
131        } elsif ($status eq 'ERROR') {
132            return;
133        }
134    }
135}
136
1371;
138
139__END__
140
141=head1 SEE ALSO
142
143L<LATMOS::Accounts::Bases>, L<LATMOS::Accounts::Bases::Objects>
144
145=head1 AUTHOR
146
147Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
148
149=head1 COPYRIGHT AND LICENSE
150
151Copyright (C) 2012 CNRS SA/CETP/LATMOS
152
153This library is free software; you can redistribute it and/or modify
154it under the same terms as Perl itself, either Perl version 5.10.0 or,
155at your option, any later version of Perl 5 you may have available.
156
157=cut
Note: See TracBrowser for help on using the repository browser.