source: trunk/LATMOS-Accounts/bin/la-sql-valid-request @ 1659

Last change on this file since 1659 was 1044, checked in by nanardon, 12 years ago

Kill redundant LATMOS::Account::default_base()

Use $LA->base(undef) to get default base instead

  • Property svn:executable set to *
File size: 3.9 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use LATMOS::Accounts::Utils;
7use Getopt::Long;
8use Pod::Usage;
9
10=head1 NAME
11
12    la-sql-valid-request - Validate changes request
13
14=head1 SYNOPSIS
15
16    la-sql-valid-request [options] obj_id
17
18=cut
19
20GetOptions(
21    'c|config=s' => \my $config,
22    'b|base=s'   => \my $base,
23    'comment=s'  => \my $comment,
24    'help'       => sub { pod2usage(0) },
25) or pod2usage();
26
27=head1 OPTIONS
28
29=over 4
30
31=item -c|--config configdir
32
33Use this configuration directory instead of the default one.
34
35=item -b|--base basename
36
37Query this specific base instead of the default one.
38
39=back
40
41=cut
42
43if (!$ARGV[0]) {
44    warn "You must specify 'obj_id', aborting\n";
45    pod2usage();
46}
47
48my $LA = LATMOS::Accounts->new($config, noacl => 1);
49my $labase = $LA->base($base);
50$labase && $labase->load or die "Cannot load base";
51$labase->wexported(1);
52
53my $datarequest = $labase->get_datarequest($ARGV[0]) or do {
54    die "Data request $ARGV[0] not found\n";
55};
56
57sub input_from_handle {
58    my ($fh) = @_;
59    my %attr = LATMOS::Accounts::Utils::parse_obj_file($fh);
60    my $res = $datarequest->apply_to_object($comment, %attr);
61    if($res) {
62        print "Changes applied\n";
63        $LA->call_batch_sync;
64    }
65    return
66    $res ? 1 : 0;
67}
68
69sub create_object {
70    my ($fh) = @_;
71
72    my %attrs = LATMOS::Accounts::Utils::parse_obj_file($fh);
73
74    my %newvalues = $datarequest->_prepare_attrs(%attrs);
75
76    # this mean creating a new object (woot)
77    my $ochelper = $labase->ochelper($datarequest->otype);
78    my ($status, $info);
79
80    foreach my $attr (keys %newvalues) {
81        $info->{contents}{$attr} = $newvalues{$attr};
82    }
83    while (1) {
84        ($status, $info) = $ochelper->step($info);
85        if ($status eq 'CREATED') {
86            $datarequest->_register_applied($comment);
87            printf 
88                "Object %s %s created\n",
89                $datarequest->otype,
90                $info->{name}{content}; 
91            $labase->commit;
92            return 1;
93        } elsif ($status eq 'ERROR') {
94            die "Error when creating object\n";
95        } elsif ($status eq 'NEEDINFO') {
96            LATMOS::Accounts::Utils::dump_read_temp_file(
97                sub {
98                    my ($fh) = @_;
99                    if ($info->{name}{ask}) {
100                        printf $fh "name: %s\n", $info->{name}{content};
101                    }
102                    foreach (@{ $info->{ask} }) {
103                        printf $fh "%s: %s\n", $_, $info->{contents}{$_} || '';
104                    }
105                    return 1;
106                },
107                sub {
108                    my ($fh) = @_;
109                    my %attrs = LATMOS::Accounts::Utils::parse_obj_file($fh);
110                    if ($info->{name}{ask}) {
111                        $info->{name}{content} = $attrs{name};
112                        delete($attrs{name});
113                    }
114                    foreach (keys %attrs) {
115                        $info->{contents}{$_} = $attrs{$_};
116                    }
117                    return 1;
118                },
119            );
120            next;
121        }
122    }
123}
124
125exit(
126    ! LATMOS::Accounts::Utils::dump_read_temp_file(
127        sub {
128            my ($fh) = @_;
129            if ($datarequest->accreq->get_attributes('requireObject')) {
130                printf $fh "# Modify Object %s/%s\n",
131                $datarequest->otype,
132                $datarequest->oobject->id;
133            }
134            my %vals = $datarequest->get_values;
135            foreach my $attr ($datarequest->attributes) {
136                foreach (ref $vals{$attr} ? @{ $vals{$attr} } :
137                    $vals{$attr}) {
138                    printf $fh "$attr: %s\n", $_ || '';
139                }
140            }
141            return 1;
142        },
143        sub {
144            my ($fh) = @_;
145            if ($datarequest->is_for_new_object) {
146                return create_object($fh);
147            } else {
148                return input_from_handle($fh);
149            }
150        }
151    )
152);
Note: See TracBrowser for help on using the repository browser.