1package OpenXPKI::Connector::DataPool;
2
3use strict;
4use warnings;
5use English;
6use Moose;
7use DateTime;
8use Data::Dumper;
9use OpenXPKI::DateTime;
10use OpenXPKI::Server::Context qw( CTX );
11
12extends 'Connector';
13
14has key => (
15    is  => 'ro',
16    isa => 'Str',
17);
18
19has value => (
20    is  => 'rw',
21    isa => 'HashRef|Str',
22);
23
24has encrypt => (
25    is => 'ro',
26    isa => 'Bool',
27    default => 0,
28);
29
30
31# FIXME - the get* methods are untested
32sub get {
33
34    my $self = shift;
35    my $args = shift;
36    my $params = shift;
37
38    my @args = $self->_build_path( $args );
39
40    my $ttarg = {
41        ARGS => \@args
42    };
43
44    if (defined $params->{extra}) {
45        $ttarg->{EXTRA} = $params->{extra};
46    }
47
48    $self->log()->trace('Template args ' . Dumper $ttarg ) if $self->log->is_trace;
49
50    # Process the key using template if necessary
51    my $key = $self->key();
52    my $template = Template->new({});
53    my $parsed_key;
54    $template->process(\$key, $ttarg, \$parsed_key) || die "Error processing argument template.";
55
56    my $result = CTX('api2')->get_data_pool_entry(
57        'namespace' => $self->LOCATION(),
58        'key' => $parsed_key,
59    );
60
61    if (!defined $result) {
62        return $self->_node_not_exists();
63    }
64
65    return $result->{value};
66
67}
68
69sub get_list {
70
71    my $self = shift;
72
73    my $val = $self->get( @_ );
74
75    if (!defined $val) {
76        return;
77    }
78
79    my $res;
80    eval {
81        $res = OpenXPKI::Serialization::Simple->new()->deserialize( $val );
82    };
83    if (!defined $res || ref $res ne 'ARRAY') {
84        die "requested value is not a list";
85    }
86    return @{$res};
87
88}
89
90sub get_hash {
91
92    my $self = shift;
93
94    my $val = $self->get( @_ );
95
96    if (!defined $val) {
97        return;
98    }
99
100    my $res;
101    eval {
102        $res = OpenXPKI::Serialization::Simple->new()->deserialize( $val );
103    };
104    if (!defined $res || ref $res ne 'HASH') {
105        die "requested value is not a hash";
106    }
107    return $res;
108
109}
110
111sub get_meta {
112
113    my $self = shift;
114
115    my $val = $self->get( @_ );
116
117    if (!defined $val) {
118        return;
119    }
120
121    my $res;
122    eval {
123        $res = OpenXPKI::Serialization::Simple->new()->deserialize( $val );
124    };
125    if (!defined $res || ref $res eq '') {
126        # Treat it as a scalar
127        return {TYPE  => "scalar" };
128    } elsif (ref $res eq 'ARRAY') {
129        return {TYPE  => "list" };
130    } elsif (ref $res eq 'HASH') {
131        return {TYPE  => "hash" };
132    }
133
134    die "Unknown data structure";
135
136}
137
138sub set {
139
140    my $self = shift;
141    my $args = shift;
142    my $value = shift;
143    my $params = shift;
144
145    my @args = $self->_build_path( $args );
146
147    $self->log()->trace('Set called on ' . Dumper \@args ) if $self->log->is_trace;
148
149    my $template = Template->new({});
150
151    my $ttarg = {
152        ARGS => \@args
153    };
154
155    if (defined $params->{extra}) {
156        $ttarg->{EXTRA} = $params->{extra};
157    }
158
159    $self->log()->trace('Template args ' . Dumper $ttarg ) if $self->log->is_trace;
160    # Process the key using template
161    my $key = $self->key();
162    my $parsed_key;
163    $template->process(\$key, $ttarg, \$parsed_key) || die "Error processing argument template.";
164
165    # Parse values
166    my $dpval;
167    my $valmap = $self->value();
168
169    if (ref $valmap eq '') {
170        $template->process(\$valmap, $ttarg, \$dpval);
171    } else {
172        foreach my $key (keys %{$valmap}) {
173            my $val;
174            $template->process(\$valmap->{$key}, $ttarg, \$val);
175            $dpval->{$key} = $val if ($val);
176        }
177        $dpval = OpenXPKI::Serialization::Simple->new()->serialize( $dpval );
178    }
179
180    $self->log()->debug('Namespace key is' . $parsed_key . ', values ' . $dpval);
181
182    CTX('api2')->set_data_pool_entry(
183        'namespace' => $self->LOCATION(),
184        'key' => $parsed_key,
185        'value' => $dpval,
186        'encrypt' => $self->encrypt(),
187        'force' => 1
188    );
189
190    return 1;
191
192}
193
194
195no Moose;
196__PACKAGE__->meta->make_immutable;
197
1981;
199__END__
200
201=head1 NAME
202
203OpenXPKI::Connector::DataPool;
204
205=head1 DESCRIPTION
206
207Connector to interact with the datapool, the LOCATION defindes the namespace.
208If you pass additional parameters (e.g. from workflow context) by setting
209{ extra => ... } in the control parameter, those values are available in the
210EXTRA hash inside all template operations.
211
212=head2 Configuration
213
214=over
215
216=item key
217
218Scalar, evaluated using template toolkit.
219
220=item value
221
222scalar value or key/value list, values are evaluated using template toolkit.
223If value is a hash, OpenXPKI::Serialization::Simple is used to store the value.
224
225
226=back
227
228