[861] | 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 | |
---|
[1023] | 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 | |
---|
[861] | 24 | sub new { |
---|
| 25 | my ($class, $base, $otype) = @_; |
---|
| 26 | bless { |
---|
| 27 | _base => $base, |
---|
| 28 | _otype => $otype, |
---|
| 29 | }, $class; |
---|
| 30 | } |
---|
| 31 | |
---|
[1023] | 32 | =head2 $ochelper->base |
---|
| 33 | |
---|
| 34 | Return base object |
---|
| 35 | |
---|
| 36 | =cut |
---|
| 37 | |
---|
[861] | 38 | sub base { $_[0]->{_base} } |
---|
[1023] | 39 | |
---|
| 40 | =head2 $ochelper->otype |
---|
| 41 | |
---|
| 42 | Return object type for this OChelper. |
---|
| 43 | |
---|
| 44 | =cut |
---|
| 45 | |
---|
[861] | 46 | sub otype { $_[0]->{_otype} } |
---|
| 47 | |
---|
[1023] | 48 | =head2 $ochelper->step($info) |
---|
[861] | 49 | |
---|
[1023] | 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 | |
---|
[861] | 69 | sub step { |
---|
| 70 | my ($self, $info) = @_; |
---|
[2359] | 71 | my $otype = $self->otype; |
---|
[861] | 72 | $info ||= {}; |
---|
| 73 | $info->{step} ||= 0; |
---|
| 74 | $info->{ask} = []; |
---|
[2359] | 75 | |
---|
| 76 | push(@{ $info->{ask} }, 'template') unless ( $otype eq 'templates' ); |
---|
| 77 | |
---|
[861] | 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 |
---|
| 93 | sub _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')) { |
---|
[2087] | 99 | /^oalias$/ and next; |
---|
[2359] | 100 | /^template$/ and next; |
---|
[861] | 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 | |
---|
[1985] | 119 | =head2 $ochelper->Automate($info) |
---|
| 120 | |
---|
| 121 | Try to create object from C<$info> w/o interacting with user. |
---|
| 122 | If given infomation does not allow to create object, it failed. |
---|
| 123 | |
---|
| 124 | Return 1 on success. |
---|
| 125 | |
---|
| 126 | =cut |
---|
| 127 | |
---|
| 128 | sub 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') { |
---|
[2286] | 135 | return $info->{name}{content}; |
---|
[1985] | 136 | } elsif ($status eq 'ERROR') { |
---|
| 137 | return; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
[861] | 142 | 1; |
---|
[1023] | 143 | |
---|
| 144 | __END__ |
---|
| 145 | |
---|
| 146 | =head1 SEE ALSO |
---|
| 147 | |
---|
| 148 | L<LATMOS::Accounts::Bases>, L<LATMOS::Accounts::Bases::Objects> |
---|
| 149 | |
---|
| 150 | =head1 AUTHOR |
---|
| 151 | |
---|
| 152 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
| 153 | |
---|
| 154 | =head1 COPYRIGHT AND LICENSE |
---|
| 155 | |
---|
| 156 | Copyright (C) 2012 CNRS SA/CETP/LATMOS |
---|
| 157 | |
---|
| 158 | This library is free software; you can redistribute it and/or modify |
---|
| 159 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 160 | at your option, any later version of Perl 5 you may have available. |
---|
| 161 | |
---|
| 162 | =cut |
---|