1 | package LATMOS::Accounts::Bases::OCHelper; |
---|
2 | |
---|
3 | # $Id: OCHelper.pm 2932 2010-08-10 17:19:21Z nanardon $ |
---|
4 | |
---|
5 | use strict; |
---|
6 | use warnings; |
---|
7 | |
---|
8 | =head1 NAME |
---|
9 | |
---|
10 | LATMOS::Accounts::Bases::OCHelper - Object creation helper |
---|
11 | |
---|
12 | =head1 DESCRIPTION |
---|
13 | |
---|
14 | This module is designed to be subclassed. |
---|
15 | |
---|
16 | =head1 FUNCTIONS |
---|
17 | |
---|
18 | =cut |
---|
19 | |
---|
20 | =head2 new($base, $otype) |
---|
21 | |
---|
22 | =cut |
---|
23 | |
---|
24 | sub new { |
---|
25 | my ($class, $base, $otype) = @_; |
---|
26 | bless { |
---|
27 | _base => $base, |
---|
28 | _otype => $otype, |
---|
29 | }, $class; |
---|
30 | } |
---|
31 | |
---|
32 | =head2 $ochelper->base |
---|
33 | |
---|
34 | Return base object |
---|
35 | |
---|
36 | =cut |
---|
37 | |
---|
38 | sub base { $_[0]->{_base} } |
---|
39 | |
---|
40 | =head2 $ochelper->otype |
---|
41 | |
---|
42 | Return object type for this OChelper. |
---|
43 | |
---|
44 | =cut |
---|
45 | |
---|
46 | sub otype { $_[0]->{_otype} } |
---|
47 | |
---|
48 | =head2 $ochelper->step($info) |
---|
49 | |
---|
50 | Process next step by submitting C<$info> and return the status and new |
---|
51 | information to continue process. |
---|
52 | |
---|
53 | C<$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 | |
---|
65 | STATUS will be one of 'NEEDINFO', 'CREATED', 'ERROR', undef. |
---|
66 | |
---|
67 | =cut |
---|
68 | |
---|
69 | sub 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 |
---|
90 | sub _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 | |
---|
117 | Try to create object from C<$info> w/o interacting with user. |
---|
118 | If given infomation does not allow to create object, it failed. |
---|
119 | |
---|
120 | Return 1 on success. |
---|
121 | |
---|
122 | =cut |
---|
123 | |
---|
124 | sub 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 | |
---|
138 | 1; |
---|
139 | |
---|
140 | __END__ |
---|
141 | |
---|
142 | =head1 SEE ALSO |
---|
143 | |
---|
144 | L<LATMOS::Accounts::Bases>, L<LATMOS::Accounts::Bases::Objects> |
---|
145 | |
---|
146 | =head1 AUTHOR |
---|
147 | |
---|
148 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
149 | |
---|
150 | =head1 COPYRIGHT AND LICENSE |
---|
151 | |
---|
152 | Copyright (C) 2012 CNRS SA/CETP/LATMOS |
---|
153 | |
---|
154 | This library is free software; you can redistribute it and/or modify |
---|
155 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
156 | at your option, any later version of Perl 5 you may have available. |
---|
157 | |
---|
158 | =cut |
---|