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

Last change on this file since 2136 was 2087, checked in by nanardon, 7 years ago

Don't ask for oalias when creating normal object

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            /^oalias$/ and next;
97            push(@{$info->{ask}}, $_);
98        }
99        $info->{step} = 1;
100        return 'NEEDINFO';
101    } elsif ($info->{step} == 1) {
102        if ($self->base->create_c_object($self->otype,
103                $info->{name}{content},
104                %{$info->{contents} || {}},
105            )) {
106            return 'CREATED';
107        } else {
108            return 'ERROR';
109        }
110    } else {
111        return undef;
112    }
113}
114
115=head2 $ochelper->Automate($info)
116
117Try to create object from C<$info> w/o interacting with user.
118If given infomation does not allow to create object, it failed.
119
120Return 1 on success.
121
122=cut
123
124sub Automate {
125    my ($self, $info) = @_;
126
127    for (my $count = 0; $count < 3; $count++) {
128        my $status;
129        ($status, $info) = $self->step($info);
130        if ($status eq 'CREATED') {
131            return 1;
132        } elsif ($status eq 'ERROR') {
133            return;
134        }
135    }
136}
137
1381;
139
140__END__
141
142=head1 SEE ALSO
143
144L<LATMOS::Accounts::Bases>, L<LATMOS::Accounts::Bases::Objects>
145
146=head1 AUTHOR
147
148Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
149
150=head1 COPYRIGHT AND LICENSE
151
152Copyright (C) 2012 CNRS SA/CETP/LATMOS
153
154This library is free software; you can redistribute it and/or modify
155it under the same terms as Perl itself, either Perl version 5.10.0 or,
156at your option, any later version of Perl 5 you may have available.
157
158=cut
Note: See TracBrowser for help on using the repository browser.