OOM/Class/Base.pm

filter: Statementstmt 17.3% 40 / 230 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 33.3% 14 / 42 Podpod 100% 14 / 14 totaltotal 16.8% 68 / 404 CC 56 SCAR 42.4
CC
56
Cov
12%
_raw_search
210.0
_collapse_data_to_ids
210.0
_retrieve_list
42.0
CRAP 69.2
↑ Summary OOM/DynamicUtils.pm »
1 package OpenERP::OOM::Class::Base;
2  
3 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use RPC::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use MooseX::NotRequired;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Try::Tiny::Retry;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Time::HiRes qw/usleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 extends 'Moose::Object';
15 3 with 'OpenERP::OOM::DynamicUtils';
16  
17 =head1 NAME
18  
19 OpenERP::OOM::Class::Base
20  
21 =head1 SYNOPSYS
22  
23 my $obj = $schema->class('Name')->create(\%args);
24
25 foreach my $obj ($schema->class('Name')->search(@query)) {
26 ...
27 }
28  
29 =head1 DESCRIPTION
30  
31 Provides a base set of methods for OpenERP::OOM classes (search, create, etc).
32  
33 =cut
34  
35 3 has 'schema' => (
36 is => 'ro',
37 );
38  
39 3 has 'object_class' => (
40 is => 'ro',
41 lazy => 1,
42 builder => '_build_object_class',
43 );
44  
45 sub _build_object_class {
46 0 ▶ my $self = shift;
Subroutine _build_object_class: not called CC=2 SCAR=17.9
47
48 # if you get this blow up it probably means the class doesn't compile for some
49 # reason. Run the t/00-load.t tests. If they pass check you have a use_ok
50 # statement for all your modules.
51 0 ▶ die 'Your code doesn\'t compile llamma' if !$self->can('object');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("object") 0 0 0
52 0 $self->ensure_class_loaded($self->object);
53
54 0 ▶ $self->object->meta->add_method('class' => sub{return $self});
Subroutine __ANON__: not called CC=1 SCAR=6.9
55
56 0 return $self->object->new;
57 }
58  
59 #-------------------------------------------------------------------------------
60  
61 =head2 search
62  
63 Searches OpenERP and returns a list of objects matching a given query.
64  
65 my @list = $schema->class('Name')->search(
66 ['name', 'ilike', 'OpusVL'],
67 ['active', '=', 1],
68 );
69  
70 The query is formatted as a list of array references, each specifying a
71 column name, operator, and value. The objects returned will be those where
72 all of these sub-queries match.
73  
74 Searches can be performed against OpenERP fields, linked objects (e.g. DBIx::Class
75 relationships), or a combination of both.
76  
77 my @list = $schema->class('Name')->search(
78 ['active', '=', 1],
79 ['details', {status => 'value'}, {}],
80 )
81  
82 In this example, 'details' is a linked DBIx::Class object with a column called
83 'status'.
84  
85 An optional 'search context' can also be provided at the end of the query list, e.g.
86  
87 my @list = $schema->class('Location')->search(
88 ['usage' => '=' => 'internal'],
89 ['active' => '=' => 1],
90 {
91 active_id => $self->id,
92 active_ids => [$self->id],
93 active_model => 'product.product',
94 full => 1,
95 product_id => $self->id,
96 search_default_in_location => 1,
97 section_id => undef,
98 tz => undef,
99 }
100 );
101  
102 Supplying a context further restricts the search, for example to narrow down a
103 'stock by location' query to 'stock of a specific product by location'.
104  
105 Following the search context, an arrayref of options can be given to return a
106 paged set of results:
107  
108 {
109 limit => 10, # Return max 10 results
110 offset => 20, # Start at result 20
111 }
112  
113 =head2 raw_search
114  
115 This is the same as search but it doesn't turn the results into objects. This
116 is useful if your search is likely to have returned fields that aren't part of
117 the object. Queries like those used by the Stock By Location report are likely
118 to return stock levels as well as the location details for example.
119  
120 =cut
121  
122 sub raw_search {
123 0 ▶ my $self = shift;
Subroutine raw_search: not called CC=1 SCAR=6.9
Pod documented
124 0 return $self->_raw_search(0, @_);
125 }
126  
127 =head2 search_limited_fields
128  
129 This is an alternative version of search that only fills in the required fields
130 of the object.
131  
132 # avoid pulling the whole attachement down for a search
133 my @a = $attachments->search_limited_fields([
134 qw/res_model res_name type url create_uid create_date
135 datas_fname description name res_id/
136 ], [
137 res_model => '=' => 'product.template',
138 res_id => '=' => 1,
139 ]);
140  
141 This allows you to avoid pulling down problem fields. The most obvious example
142 is get a list of attachments for an object, without pulling down all the data
143 for the attachement.
144  
145 =cut
146  
147 sub search_limited_fields {
148 0 ▶ my $self = shift;
Subroutine search_limited_fields: not called CC=1 SCAR=6.9
Pod documented
149 0 return $self->_search_limited_fields(1, @_);
150 }
151  
152 sub _search_limited_fields {
153 0 ▶ my $self = shift;
Subroutine _search_limited_fields: not called CC=5 SCAR=34.0
154 0 my $objects = shift;
155 0 my $fields = shift;
156  
157 0 my $ids = $self->_raw_search(1, @_);
158 0 ▶ return wantarray ? () : undef unless ( defined $ids && ref $ids eq 'ARRAY' && scalar @$ids >= 1 );
Branch: 2 branches
BranchTrueFalseTotal
unless defined $ids and ref $ids eq "ARRAY" and scalar @$ids >= 1 0 0 0
wantarray ? : 0 0 0
Condition0%0/6
defined $ids and ref $ids eq "ARRAY" and scalar @$ids >= 1
!ll&&!rl&&r
000
defined $ids and ref $ids eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $ids and ref $ids eq "ARRAY" and scalar @$ids >= 1
defined $ids ref $ids eq "ARRAY" scalar @$ids >= 1
Truth table0%0/4
A and B and C
A= defined $idsB= ref $ids eq "ARRAY"C= scalar @$ids >= 1
ABCresult
0XX0
10X0
1100
1111
159 0 my ($context) = grep { ref $_ eq 'HASH' } @_;
160 0 return $self->_retrieve_list($objects, $ids, $context, $fields);
161 }
162  
163 sub _raw_search {
164 0 ▶ my ($self, $ids_only, @args) = @_;
Subroutine _raw_search: not called CC=14 SCAR=53.5
165 ### Initial search args: @args
166
167 0 my @search;
168 0 ▶ while (@args && ref $args[0] ne 'HASH') {push @search, shift @args}
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[0] ne "HASH") 0 0 0
Condition0%0/3
@args and ref $args[0] ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@args and ref $args[0] ne "HASH"
@args ref $args[0] ne "HASH"
Truth table0%0/3
A and B
A= @argsB= ref $args[0] ne "HASH"
ABresult
0X0
100
111
169
170 # Loop through each search criteria, and if it is a linked object
171 # search, replace it with a translated OpenERP search parameter.
172 0 foreach my $criteria (@search) {
173 0 ▶ if(ref $criteria eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $criteria eq "ARRAY") 0 0 0
174 0 my $search_field = $criteria->[0];
175  
176 0 ▶ if (my $link = $self->object_class->meta->link->{$search_field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $link = $self->object_class->meta->link->{$search_field}) 0 0 0
177 0 ▶ if ($self->schema->link($link->{class})->can('search')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->schema->link($link->{'class'})->can('search')) { } 0 0 0
178 0 my @results = $self->schema->link($link->{class})->search($link->{args}, @$criteria[1 .. @$criteria-1]);
179  
180 0 ▶ if (@results) {
Branch: 1 branch
BranchTrueFalseTotal
if (@results) { } 0 0 0
181 ### Adding to OpenERP search:
182 ### $link->{key}
183 ### IN
184 ### join(', ', @results)
185 0 $criteria = [$link->{key}, 'in', \@results];
186 } else {
187 0 return; # No results found, so no point searching in OpenERP
188 }
189 } else {
190 0 carp "Cannot search for link type " . $link->{class};
191 }
192 }
193 }
194 }
195
196 0 my $context = $self->_get_context(shift @args);
197 0 my $options = shift @args;
198 0 ▶ $options = {} unless $options;
Branch: 1 branch
BranchTrueFalseTotal
unless $options 0 0 0
199 ### Search: @search
200 ### Search context: $context
201 ### Search options: $options
202 0 ▶ if($ids_only)
Branch: 1 branch
BranchTrueFalseTotal
if ($ids_only) 0 0 0
203 {
204 0 return $self->schema->client->search($self->object_class->model,[@search], $context, $options->{offset}, $options->{limit}, $options->{order});
205 }
206  
207 0 my $objects = $self->schema->client->search_detail($self->object_class->model,[@search], $context, $options->{offset}, $options->{limit}, $options->{order});
208  
209 0 ▶ if ($objects) {
Branch: 1 branch
BranchTrueFalseTotal
if ($objects) { } 0 0 0
210 0 foreach my $attribute ($self->object_class->meta->get_all_attributes) {
211 0 ▶ if($attribute->type_constraint && $attribute->type_constraint =~ /DateTime/)
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->type_constraint and $attribute->type_constraint =~ /DateTime/) 0 0 0
Condition0%0/3
$attribute->type_constraint and $attribute->type_constraint =~ /DateTime/
!ll&&!rl&&r
000
MC/DC0%0/2
$attribute->type_constraint and $attribute->type_constraint =~ /DateTime/
$attribute->type_constraint $attribute->type_constraint =~ /DateTime/
Truth table0%0/3
A and B
A= $attribute->type_constraintB= $attribute->type_constraint =~ /DateTime/
ABresult
0X0
100
111
212 {
213 0 map { $_->{$attribute->name} = $self->_parse_datetime($_->{$attribute->name}) } @$objects;
214 }
215 }
216 0 return $objects;
217 } else {
218 0 return undef;
219 }
220 }
221  
222 sub search
223 {
224 0 ▶ my $self = shift;
Subroutine search: not called CC=3 SCAR=24.8
Pod documented
225 0 my $objects = $self->raw_search(@_);
226 0 ▶ if($objects) {
Branch: 1 branch
BranchTrueFalseTotal
if ($objects) { } 0 0 0
227 0 return map {$self->object_class->new($_)} @$objects;
228 } else {
229 0 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
230 }
231 }
232  
233 =head2 is_not_null
234  
235 Returns search criteria for a not null search. i.e. equivalend to $field is not null in SQL.
236  
237 $self->search($self->is_not_null('x_department'), [ 'other_field', '=', 3 ]);
238  
239 =cut
240  
241 sub is_not_null
242 {
243 0 ▶ my $self = shift;
Subroutine is_not_null: not called CC=1 SCAR=6.9
Pod documented
244 0 my $field = shift;
245 0 return [ $field, '!=', RPC::XML::boolean->new(0) ];
246 }
247  
248 =head2 null
249  
250 Returns a 'null' for use in OpenERP calls and objects. (Actually this is a False value).
251  
252 =cut
253  
254 0 ▶sub null { RPC::XML::boolean->new(0) }
Subroutine null: not called CC=1 SCAR=6.9
Pod documented
255  
256 =head2 is_null
257  
258 Returns search criteria for an is null search. i.e. equivalend to $field is null in SQL.
259  
260 $self->search($self->is_null('x_department'), [ 'other_field', '=', 3 ]);
261  
262 =cut
263  
264 sub is_null
265 {
266 0 ▶ my $self = shift;
Subroutine is_null: not called CC=1 SCAR=6.9
Pod documented
267 0 my $field = shift;
268 0 return [ $field, '=', RPC::XML::boolean->new(0) ];
269 }
270  
271 #-------------------------------------------------------------------------------
272  
273 =head2 find
274  
275 Returns the first object matching a given query.
276  
277 my $obj = $schema->class('Name')->find(['id', '=', 32]);
278  
279 Will return C<undef> if no objects matching the query are found.
280  
281 =cut
282  
283 sub find {
284 0 ▶ my $self = shift;
Subroutine find: not called CC=2 SCAR=17.9
Pod documented
285
286 #my $ids = $self->schema->client->search($self->object_class->model,[@_]);
287 0 my $ids = $self->raw_search(@_);
288
289 0 ▶ if ($ids->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ids->[0]) 0 0 0
290 #return $self->retrieve($ids->[0]);
291 0 return $self->object_class->new($ids->[0]);
292 }
293 }
294  
295  
296 =head2 get_options
297  
298 This returns the options for available for a selection field. It will croak if you
299 try to give it a field that isn't an option.
300  
301 =cut
302  
303 sub get_options
304 {
305 0 ▶ my $self = shift;
Subroutine get_options: not called CC=2 SCAR=17.9
Pod documented
306 0 my $field = shift;
307  
308 0 my $model_info = $self->schema->client->model_fields($self->object_class->model);
309 0 my $field_info = $model_info->{$field};
310 0 ▶ croak 'Can only get options for selection objects' unless $field_info->{type} eq 'selection';
Branch: 1 branch
BranchTrueFalseTotal
unless $field_info->{"type"} eq "selection" 0 0 0
311 0 my $options = $field_info->{selection};
312 0 return $options;
313 }
314  
315 #-------------------------------------------------------------------------------
316  
317 =head2 retrieve
318  
319 Returns an object by ID.
320  
321 my $obj = $schema->class('Name')->retrieve(32);
322  
323 =cut
324  
325 sub retrieve {
326 0 ▶ my ($self, $id, @args) = @_;
Subroutine retrieve: not called CC=2 SCAR=17.9
Pod documented
327
328 # FIXME - This should probably be in a try/catch block
329 0 my $context = $self->_get_context(shift @args);
330 0 $self->_ensure_object_fields(\@args);
331 0 ▶ if (my $object = $self->schema->client->read_single($self->object_class->model, $id, $context, @args))
Branch: 1 branch
BranchTrueFalseTotal
if (my $object = $self->schema->client->read_single($self->object_class->model, $id, $context, @args)) 0 0 0
332 {
333 0 return $self->_inflate_object($self->object, $object);
334 }
335 }
336  
337 sub _ensure_object_fields
338 {
339 0 ▶ my $self = shift;
Subroutine _ensure_object_fields: not called CC=4 SCAR=30.0
340 0 my $args = shift;
341  
342 0 ▶ unless(@$args)
Branch: 1 branch
BranchTrueFalseTotal
unless (@$args) 0 0 0
343 {
344 0 my @fields;
345 0 foreach my $attribute ($self->object_class->meta->get_all_attributes)
346 {
347 0 my $name = $attribute->name;
348 0 ▶ push @fields, $name unless $name =~ /^_/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^_/ 0 0 0
349 }
350 0 push @$args, \@fields;
351 }
352 }
353  
354 sub _get_context
355 {
356 0 ▶ my $self = shift;
Subroutine _get_context: not called CC=2 SCAR=17.9
357 0 my $context = shift;
358  
359 0 my %translation = ( lang => $self->schema->lang );
360 0 ▶ if($context)
Branch: 1 branch
BranchTrueFalseTotal
if ($context) 0 0 0
361 {
362 # merge the context with our language for translation.
363 0 @translation{keys %$context} = values %$context;
364 }
365 0 $context = \%translation;
366 0 return $context;
367 }
368  
369 sub _inflate_object
370 {
371 0 ▶ my $self = shift;
Subroutine _inflate_object: not called CC=4 SCAR=30.0
372 0 my $object_class = shift;
373 0 my $object = shift;
374  
375 0 foreach my $attribute ($self->object_class->meta->get_all_attributes) {
376 0 ▶ if($attribute->type_constraint && $attribute->type_constraint =~ /DateTime/)
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->type_constraint and $attribute->type_constraint =~ /DateTime/) 0 0 0
Condition0%0/3
$attribute->type_constraint and $attribute->type_constraint =~ /DateTime/
!ll&&!rl&&r
000
MC/DC0%0/2
$attribute->type_constraint and $attribute->type_constraint =~ /DateTime/
$attribute->type_constraint $attribute->type_constraint =~ /DateTime/
Truth table0%0/3
A and B
A= $attribute->type_constraintB= $attribute->type_constraint =~ /DateTime/
ABresult
0X0
100
111
377 {
378 0 $object->{$attribute->name} = $self->_parse_datetime($object->{$attribute->name});
379 }
380 }
381 0 return $object_class->new($object);
382 }
383  
384 sub _do_strptime {
385 0 ▶ my ($self, $string, $format) = @_;
Subroutine _do_strptime: not called CC=1 SCAR=6.9
386 0 my $parser = DateTime::Format::Strptime->new(pattern => $format);
387 0 return $parser->parse_datetime($string);
388 }
389  
390 sub _parse_datetime {
391 0 ▶ my ($self, $string) = @_;
Subroutine _parse_datetime: not called CC=2 SCAR=17.9
392 0 ▶ return $self->_do_strptime($string, '%Y-%m-%d %H:%M:%S') // $self->_do_strptime($string, '%Y-%m-%d');
Condition0%0/3
$self->_do_strptime($string, "%Y-%m-%d %H:%M:%S") // $self->_do_strptime($string, "%Y-%m-%d")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_do_strptime($string, "%Y-%m-%d %H:%M:%S") // $self->_do_strptime($string, "%Y-%m-%d")
$self->_do_strptime($string, "%Y-%m-%d %H:%M:%S") $self->_do_strptime($string, "%Y-%m-%d")
Truth table0%0/3
A // B
A= $self->_do_strptime($string, "%Y-%m-%d %H:%M:%S")B= $self->_do_strptime($string, "%Y-%m-%d")
ABresult
1X1
011
000
393 }
394  
395 =head2 default_values
396  
397 Returns an instance of the object filled in with the default values suggested by OpenERP.
398  
399 =cut
400 sub default_values
401 {
402 0 ▶ my $self = shift;
Subroutine default_values: not called CC=1 SCAR=6.9
Pod documented
403 0 my $context = shift;
404 # do a default_get
405  
406 0 my @fields = map { $_->name } $self->object_class->meta->get_all_attributes;
407 0 my $object = $self->schema->client->get_defaults($self->object_class->model, \@fields, $context);
408 0 my $class = MooseX::NotRequired::make_optional_subclass($self->object);
409 0 return $self->_inflate_object($class, $object);
410 }
411  
412 =head2 create_related_object_for_DBIC
413  
414 Creates a related DBIC object for an object of this class (before the object
415 is created).
416  
417 It returns a transaction guard alongside the id so that if the corresponding
418 object fails to create it can be aborted.
419  
420 This can make the link up smoother as you know the id of the object to refer
421 to in OpenERP before creating the OpenERP object. It also allows for failures
422 to be dealt with more reliably.
423  
424 my ($id, $guard) = $self->create_related_object_for_DBIC('details', $details);
425 # Create the object
426 $object->{x_dbic_link_id} = $id;
427 $object->{default_code} = sprintf("OBJ%06d", $id);
428  
429 my $prod = $self->$orig($object);
430 $guard->commit;
431  
432 =cut
433  
434 sub create_related_object_for_DBIC
435 {
436 0 ▶ my ($self, $relation_name, $data) = @_;
Subroutine create_related_object_for_DBIC: not called CC=3 SCAR=24.8
Pod documented
437 0 my $object = $self->object_class;
438 0 my $relation = $object->meta->link->{$relation_name};
439 0 ▶ if($relation)
Branch: 1 branch
BranchTrueFalseTotal
if ($relation) { } 0 0 0
440 {
441 0 ▶ die 'Wrong type of relation' unless $relation->{class} eq 'DBIC';
Branch: 1 branch
BranchTrueFalseTotal
unless $relation->{"class"} eq "DBIC" 0 0 0
442 0 my $link = $self->schema->link($relation->{class});
443 0 my $guard = $link->dbic_schema->storage->txn_scope_guard;
444 0 my $id = $link->create($relation->{args}, $data);
445 0 return ($id, $guard);
446 }
447 else
448 {
449 0 die 'Unable to find relation';
450 }
451 }
452 #-------------------------------------------------------------------------------
453  
454 =head2 retrieve_list
455  
456 Takes a reference to a list of object IDs and returns a list of objects.
457  
458 my @list = $schema->class('Name')->retrieve_list([32, 15, 60]);
459  
460 =cut
461  
462 sub retrieve_list {
463 0 ▶ my $self = shift;
Subroutine retrieve_list: not called CC=1 SCAR=6.9
Pod documented
464 0 return $self->_retrieve_list(1, @_);
465 }
466  
467 sub _retrieve_list {
468 0 ▶ my ($self, $inflate_objects, $ids, @args) = @_;
Subroutine _retrieve_list: not called CC=6 SCAR=37.4
469
470 0 my $context = $self->_get_context(shift @args);
471 0 $self->_ensure_object_fields(\@args);
472 0 ▶ if (my $objects = $self->schema->client->read($self->object_class->model, $ids, $context, @args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $objects = $self->schema->client->read($self->object_class->model, $ids, $context, @args)) 0 0 0
473 0 foreach my $attribute ($self->object_class->meta->get_all_attributes) {
474 0 ▶ if($attribute->type_constraint && $attribute->type_constraint =~ /DateTime/)
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->type_constraint and $attribute->type_constraint =~ /DateTime/) 0 0 0
Condition0%0/3
$attribute->type_constraint and $attribute->type_constraint =~ /DateTime/
!ll&&!rl&&r
000
MC/DC0%0/2
$attribute->type_constraint and $attribute->type_constraint =~ /DateTime/
$attribute->type_constraint $attribute->type_constraint =~ /DateTime/
Truth table0%0/3
A and B
A= $attribute->type_constraintB= $attribute->type_constraint =~ /DateTime/
ABresult
0X0
100
111
475 {
476 0 map { $_->{$attribute->name} = $self->_parse_datetime($_->{$attribute->name}) } @$objects;
477 }
478 }
479 0 my %id_map = map { $_->{id} => $_ } @$objects;
480 0 my @sorted = map { $id_map{$_} } @$ids;
481 0 ▶ return map {$self->object_class->new($_)} @sorted if $inflate_objects;
Branch: 1 branch
BranchTrueFalseTotal
if $inflate_objects 0 0 0
482 0 return @sorted;
483 }
484 }
485  
486  
487 #-------------------------------------------------------------------------------
488  
489 sub _collapse_data_to_ids
490 {
491 0 ▶ my ($self, $object_data) = @_;
Subroutine _collapse_data_to_ids: not called CC=14 SCAR=53.5
492  
493 0 my $relationships = $self->object_class->meta->relationship;
494 0 ▶ while (my ($name, $rel) = each %$relationships) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$relationships) 0 0 0
495 0 ▶ if ($rel->{type} eq 'one2many') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->{"type"} eq "one2many") 0 0 0
496 0 ▶ if ($object_data->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_data->{$name}) 0 0 0
497 0 $object_data->{$rel->{key}} = $self->_id($rel, $object_data->{$name});
498 0 ▶ delete $object_data->{$name} if $name ne $rel->{key};
Branch: 1 branch
BranchTrueFalseTotal
if $name ne $rel->{"key"} 0 0 0
499 }
500 }
501
502 0 ▶ if ($rel->{type} eq 'many2one') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->{"type"} eq "many2one") 0 0 0
503 0 ▶ if ($object_data->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_data->{$name}) 0 0 0
504 0 $object_data->{$rel->{key}} = $self->_id($rel, $object_data->{$name});
505 0 ▶ delete $object_data->{$name} if $name ne $rel->{key};
Branch: 1 branch
BranchTrueFalseTotal
if $name ne $rel->{"key"} 0 0 0
506 }
507 }
508 0 ▶ if ($rel->{type} eq 'many2many') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->{"type"} eq "many2many") 0 0 0
509 0 ▶ if ($object_data->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_data->{$name}) 0 0 0
510 0 my $val = $object_data->{$name};
511 0 my @ids;
512 0 ▶ if(ref $val eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 0 0
513 {
514 # they passed in an arrayref.
515 0 my $objects = $val;
516 0 @ids = map { $self->_id($rel, $_) } @$objects;
517 }
518 else
519 {
520 # assume it's a single object.
521 0 push @ids, $self->_id($rel, $val);
522 }
523 0 $object_data->{$rel->{key}} = [[ 6, 0, \@ids ]];
524 0 ▶ delete $object_data->{$name} if $name ne $rel->{key};
Branch: 1 branch
BranchTrueFalseTotal
if $name ne $rel->{"key"} 0 0 0
525 }
526 }
527 }
528 # Force Str parameters to be object type RPC::XML::string
529 0 foreach my $attribute ($self->object_class->meta->get_all_attributes) {
530 0 ▶ if (exists $object_data->{$attribute->name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$object_data{$attribute->name}) 0 0 0
531 0 $object_data->{$attribute->name} = $self->prepare_attribute_for_send($attribute->type_constraint, $object_data->{$attribute->name});
532 }
533 }
534 0 return $object_data;
535 }
536  
537 sub _id
538 {
539 0 ▶ my $self = shift;
Subroutine _id: not called CC=4 SCAR=30.0
540 0 my $rel = shift;
541 0 my $val = shift;
542 0 my $ref = ref $val;
543 0 ▶ if($ref)
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) 0 0 0
544 {
545 # FIXME: this is close to what I want but I need to be doing it with the class
546 # that corresponds to the relation we're delving into.
547 0 ▶ if($ref eq 'HASH')
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
548 {
549 0 my $class = $self->schema->class($rel->{class});
550 0 return [[ 0, 0, $class->_collapse_data_to_ids($val) ]];
551 }
552 elsif($ref eq 'ARRAY')
553 {
554 # this should allow us to do child objects too.
555 0 my $class = $self->schema->class($rel->{class});
556 0 my @expanded = map { [ 0, 0, $class->_collapse_data_to_ids($_) ] } @$val;
557 0 return \@expanded;
558 }
559 else
560 {
561 0 return $val->id;
562 }
563 }
564 0 return $val;
565 }
566  
567 =head2 create
568  
569 Creates a new instance of an object in OpenERP.
570  
571 my $obj = $schema->class('Name')->create({
572 name => 'OpusVL',
573 active => 1,
574 });
575  
576 Takes a hashref of object parameters.
577  
578 Returns the new object or C<undef> if it could not be created.
579  
580 =cut
581  
582 sub create {
583 0 ▶ my ($self, $object_data, @args) = @_;
Subroutine create: not called CC=2 SCAR=17.9
Pod documented
584  
585 ### Create called with initial object data:
586 ### $object_data;
587
588 0 $object_data = $self->_collapse_data_to_ids($object_data);
589  
590 ### To
591 ### $object_data;
592 0 my $id;
593 $self->_with_retries(sub {
594 0 ▶ $id = $self->schema->client->create($self->object_class->model, $object_data, @args);
Subroutine __ANON__: not called CC=1 SCAR=6.9
595 0 });
596 0 ▶ if ($id)
Branch: 1 branch
BranchTrueFalseTotal
if ($id) 0 0 0
597 {
598 0 return $self->retrieve($id);
599 }
600 }
601  
602 sub _with_retries
603 {
604 4 ▶ my $self = shift;
Subroutine _with_retries: called CC=1 SCAR=0.0
605 4 my $call = shift;
606 retry
607 {
608 17 ▶ $call->();
Subroutine __ANON__: called CC=1 SCAR=0.0
609 }
610 16 ▶ retry_if {/current transaction is aborted, commands ignored until end of transaction block/}
Subroutine __ANON__: called CC=1 SCAR=0.0
611 catch
612 {
613 3 ▶ die $_; # rethrow the unhandled exception
Subroutine __ANON__: called CC=1 SCAR=0.0
614 4 };
615 }
616  
617  
618 #-------------------------------------------------------------------------------
619  
620 =head2 execute
621  
622 Performs an execute in OpenERP on the class level.
623  
624 $c->model('OpenERP')->class('Invoice')->execute('build_invoice', $args);
625  
626 Please look at L<OpenERP::OOM::Object::Base> for more information on C<execute>
627  
628 =cut
629  
630 sub execute {
631 0 ▶ my $self = shift;
Subroutine execute: not called CC=1 SCAR=6.9
Pod documented
632 0 my $action = shift;
633 0 my @params = @_;
634 0 my @args = ($action, $self->object_class->model, @params);
635 0 my $retval;
636 $self->_with_retries(sub {
637 0 ▶ $retval = $self->schema->client->object_execute(@args);
Subroutine __ANON__: not called CC=1 SCAR=6.9
638 0 });
639 0 return $retval;
640 }
641  
642 #-------------------------------------------------------------------------------
643  
644 =head1 AUTHOR
645  
646 Jon Allen (JJ) - L<jj@opusvl.com>
647  
648 =head1 COPYRIGHT and LICENSE
649  
650 Copyright (C) 2010 Opus Vision Limited
651  
652 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
653  
654 =cut
655  
656 3 1;
↑ Summary OOM/DynamicUtils.pm »