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

Last change on this file since 861 was 861, checked in by nanardon, 13 years ago
  • reimport missing files from previous svn
File size: 1.7 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
8sub new {
9    my ($class, $base, $otype) = @_;
10    bless { 
11        _base => $base,
12        _otype => $otype,
13    }, $class;
14}
15
16sub base { $_[0]->{_base} }
17sub otype { $_[0]->{_otype} }
18
19# return STATUS, $info
20# info = {
21#   step => 0,
22#   name => { # name of object
23#     ask => 0/1,
24#     content => ...
25#   },
26#   ask => [ list ],
27#   contents => { name => ... }
28# }
29# STATUS => 'NEEDINFO', 'CREATED', 'ERROR', undef (what??)
30
31sub step {
32    my ($self, $info) = @_;
33    $info ||= {};
34    $info->{step} ||= 0;
35    $info->{ask} = [];
36    $info->{name}{ask} = 0;
37    my $otype = $self->otype;
38    foreach (keys %{ $self->base->{defattr} || {} }) {
39        /^$otype\.(.*)/ or next;
40        my $attr = $1;
41        my $oattr = $self->base->attribute($otype, $attr) or next;
42        $oattr->ro and next;
43        $info->{contents}{$attr} = $self->base->{defattr}{$_}
44            unless exists($info->{contents}{$attr});
45    }
46
47    return($self->_step($info), $info);
48
49}
50
51# just return status, $info is reference
52sub _step {
53    my ($self, $info) = @_;
54
55    if ($info->{step} == 0) {
56        $info->{name}{ask} = 1;
57        foreach ($self->base->list_canonical_fields($self->otype, 'w')) {
58            push(@{$info->{ask}}, $_);
59        }
60        $info->{step} = 1;
61        return 'NEEDINFO';
62    } elsif ($info->{step} == 1) {
63        if ($self->base->create_c_object($self->otype,
64                $info->{name}{content},
65                %{$info->{contents} || {}},
66            )) {
67            return 'CREATED';
68        } else {
69            return 'ERROR';
70        }
71    } else {
72        return undef;
73    }
74}
75
761;
Note: See TracBrowser for help on using the repository browser.