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

Last change on this file was 2359, checked in by nanardon, 4 years ago

Ask for template first at object creation

File size: 3.2 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    my $otype = $self->otype;
72    $info ||= {};
73    $info->{step} ||= 0;
74    $info->{ask} = [];
75
76    push(@{ $info->{ask} }, 'template') unless ( $otype eq 'templates' );
77
78    $info->{name}{ask} = 0;
79    foreach (keys %{ $self->base->{defattr} || {} }) {
80        /^$otype\.(.*)/ or next;
81        my $attr = $1;
82        my $oattr = $self->base->attribute($otype, $attr) or next;
83        $oattr->ro and next;
84        $info->{contents}{$attr} = $self->base->{defattr}{$_}
85            unless exists($info->{contents}{$attr});
86    }
87
88    return($self->_step($info), $info);
89
90}
91
92# just return status, $info is reference
93sub _step {
94    my ($self, $info) = @_;
95
96    if ($info->{step} == 0) {
97        $info->{name}{ask} = 1;
98        foreach ($self->base->list_canonical_fields($self->otype, 'w')) {
99            /^oalias$/ and next;
100            /^template$/ and next;
101            push(@{$info->{ask}}, $_);
102        }
103        $info->{step} = 1;
104        return 'NEEDINFO';
105    } elsif ($info->{step} == 1) {
106        if ($self->base->create_c_object($self->otype,
107                $info->{name}{content},
108                %{$info->{contents} || {}},
109            )) {
110            return 'CREATED';
111        } else {
112            return 'ERROR';
113        }
114    } else {
115        return undef;
116    }
117}
118
119=head2 $ochelper->Automate($info)
120
121Try to create object from C<$info> w/o interacting with user.
122If given infomation does not allow to create object, it failed.
123
124Return 1 on success.
125
126=cut
127
128sub Automate {
129    my ($self, $info) = @_;
130
131    for (my $count = 0; $count < 3; $count++) {
132        my $status;
133        ($status, $info) = $self->step($info);
134        if ($status eq 'CREATED') {
135            return $info->{name}{content};
136        } elsif ($status eq 'ERROR') {
137            return;
138        }
139    }
140}
141
1421;
143
144__END__
145
146=head1 SEE ALSO
147
148L<LATMOS::Accounts::Bases>, L<LATMOS::Accounts::Bases::Objects>
149
150=head1 AUTHOR
151
152Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
153
154=head1 COPYRIGHT AND LICENSE
155
156Copyright (C) 2012 CNRS SA/CETP/LATMOS
157
158This library is free software; you can redistribute it and/or modify
159it under the same terms as Perl itself, either Perl version 5.10.0 or,
160at your option, any later version of Perl 5 you may have available.
161
162=cut
Note: See TracBrowser for help on using the repository browser.