OOM/Class/Base.pm

filter: Statementstmt 17.2% 40 / 232 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 33.3% 14 / 42 Podpod 100% 14 / 14 totaltotal 16.6% 68 / 408 CC 57 SCAR 42.3
CC
57
Cov
12%
_raw_search
210.0
_collapse_data_to_ids
210.0
_retrieve_list
42.0
CRAP 68.6
« OOM/Object/Base.pm ↑ 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 0 ▶sub _build_object_class {
Subroutine _build_object_class: not called CC=2 SCAR=17.9
46 0 my $self = shift;
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 0 ▶sub raw_search {
Subroutine raw_search: not called CC=1 SCAR=6.9
Pod documented
123 0 my $self = shift;
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 0 ▶sub search_limited_fields {
Subroutine search_limited_fields: not called CC=1 SCAR=6.9
Pod documented
148 0 my $self = shift;
149 0 return $self->_search_limited_fields(1, @_);
150 }
151  
152 0 ▶sub _search_limited_fields {
Subroutine _search_limited_fields: not called CC=5 SCAR=34.0
153 0 my $self = shift;
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 0 ▶sub _raw_search {
Subroutine _raw_search: not called CC=14 SCAR=53.5
164 0 my ($self, $ids_only, @args) = @_;
165 ### Initial search args: @args
166 0 my @search;
167 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
168
169 # Loop through each search criteria, and if it is a linked object
170 # search, replace it with a translated OpenERP search parameter.
171 0 foreach my $criteria (@search) {
172 0 ▶ if(ref $criteria eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $criteria eq "ARRAY") 0 0 0
173 0 my $search_field = $criteria->[0];
174  
175 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
176 0 ▶ if ($self->schema->link($link->{class})->can('search')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->schema->link($link->{'class'})->can('search')) { } 0 0 0
177 0 my @results = $self->schema->link($link->{class})->search($link->{args}, @$criteria[1 .. @$criteria-1]);
178  
179 0 ▶ if (@results) {
Branch: 1 branch
BranchTrueFalseTotal
if (@results) { } 0 0 0
180 ### Adding to OpenERP search:
181 ### $link->{key}
182 ### IN
183 ### join(', ', @results)
184 0 $criteria = [$link->{key}, 'in', \@results];
185 } else {
186 0 return; # No results found, so no point searching in OpenERP
187 }
188 } else {
189 0 carp "Cannot search for link type " . $link->{class};
190 }
191 }
192 }
193 }
194 0 my $context = $self->_get_context(shift @args);
195 0 my $options = shift @args;
196 0 ▶ $options = {} unless $options;
Branch: 1 branch
BranchTrueFalseTotal
unless $options 0 0 0
197 ### Search: @search
198 ### Search context: $context
199 ### Search options: $options
200 0 ▶ if($ids_only)
Branch: 1 branch
BranchTrueFalseTotal
if ($ids_only) 0 0 0
201 {
202 0 return $self->schema->client->search($self->object_class->model,[@search], $context, $options->{offset}, $options->{limit}, $options->{order});
203 }
204  
205 0 my $objects = $self->schema->client->search_detail($self->object_class->model,[@search], $context, $options->{offset}, $options->{limit}, $options->{order});
206  
207 0 ▶ if ($objects) {
Branch: 1 branch
BranchTrueFalseTotal
if ($objects) { } 0 0 0
208 0 foreach my $attribute ($self->object_class->meta->get_all_attributes) {
209 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
210 {
211 0 map { $_->{$attribute->name} = $self->_parse_datetime($_->{$attribute->name}) } @$objects;
212 }
213 }
214 0 return $objects;
215 } else {
216 0 return undef;
217 }
218 }
219  
220 sub search
221 0 ▶{
Subroutine search: not called CC=3 SCAR=24.8
Pod documented
222 0 my $self = shift;
223 0 my $objects = $self->raw_search(@_);
224 0 ▶ if($objects) {
Branch: 1 branch
BranchTrueFalseTotal
if ($objects) { } 0 0 0
225 0 return map {$self->object_class->new($_)} @$objects;
226 } else {
227 0 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
228 }
229 }
230  
231 =head2 is_not_null
232  
233 Returns search criteria for a not null search. i.e. equivalend to $field is not null in SQL.
234  
235 $self->search($self->is_not_null('x_department'), [ 'other_field', '=', 3 ]);
236  
237 =cut
238  
239 sub is_not_null
240 0 ▶{
Subroutine is_not_null: not called CC=1 SCAR=6.9
Pod documented
241 0 my $self = shift;
242 0 my $field = shift;
243 0 return [ $field, '!=', RPC::XML::boolean->new(0) ];
244 }
245  
246 =head2 null
247  
248 Returns a 'null' for use in OpenERP calls and objects. (Actually this is a False value).
249  
250 =cut
251  
252 0 ▶sub null { RPC::XML::boolean->new(0) }
Subroutine null: not called CC=1 SCAR=6.9
Pod documented
253  
254 =head2 is_null
255  
256 Returns search criteria for an is null search. i.e. equivalend to $field is null in SQL.
257  
258 $self->search($self->is_null('x_department'), [ 'other_field', '=', 3 ]);
259  
260 =cut
261  
262 sub is_null
263 0 ▶{
Subroutine is_null: not called CC=1 SCAR=6.9
Pod documented
264 0 my $self = shift;
265 0 my $field = shift;
266 0 return [ $field, '=', RPC::XML::boolean->new(0) ];
267 }
268  
269 #-------------------------------------------------------------------------------
270  
271 =head2 find
272  
273 Returns the first object matching a given query.
274  
275 my $obj = $schema->class('Name')->find(['id', '=', 32]);
276  
277 Will return C<undef> if no objects matching the query are found.
278  
279 =cut
280  
281 0 ▶sub find {
Subroutine find: not called CC=2 SCAR=17.9
Pod documented
282 0 my $self = shift;
283
284 #my $ids = $self->schema->client->search($self->object_class->model,[@_]);
285 0 my $ids = $self->raw_search(@_);
286
287 0 ▶ if ($ids->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ids->[0]) 0 0 0
288 #return $self->retrieve($ids->[0]);
289 0 return $self->object_class->new($ids->[0]);
290 }
291 }
292  
293  
294 =head2 get_options
295  
296 This returns the options for available for a selection field. It will croak if you
297 try to give it a field that isn't an option.
298  
299 =cut
300  
301 sub get_options
302 0 ▶{
Subroutine get_options: not called CC=2 SCAR=17.9
Pod documented
303 0 my $self = shift;
304 0 my $field = shift;
305  
306 0 my $model_info = $self->schema->client->model_fields($self->object_class->model);
307 0 my $field_info = $model_info->{$field};
308 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
309 0 my $options = $field_info->{selection};
310 0 return $options;
311 }
312  
313 #-------------------------------------------------------------------------------
314  
315 =head2 retrieve
316  
317 Returns an object by ID.
318  
319 my $obj = $schema->class('Name')->retrieve(32);
320  
321 =cut
322  
323 0 ▶sub retrieve {
Subroutine retrieve: not called CC=2 SCAR=17.9
Pod documented
324 0 my ($self, $id, @args) = @_;
325
326 # FIXME - This should probably be in a try/catch block
327 0 my $context = $self->_get_context(shift @args);
328 0 $self->_ensure_object_fields(\@args);
329 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
330 {
331 0 return $self->_inflate_object($self->object, $object);
332 }
333 }
334  
335 sub _ensure_object_fields
336 0 ▶{
Subroutine _ensure_object_fields: not called CC=4 SCAR=30.0
337 0 my $self = shift;
338 0 my $args = shift;
339  
340 0 ▶ unless(@$args)
Branch: 1 branch
BranchTrueFalseTotal
unless (@$args) 0 0 0
341 {
342 0 my @fields;
343 0 foreach my $attribute ($self->object_class->meta->get_all_attributes)
344 {
345 0 my $name = $attribute->name;
346 0 ▶ push @fields, $name unless $name =~ /^_/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^_/ 0 0 0
347 }
348 0 push @$args, \@fields;
349 }
350 }
351  
352 sub _get_context
353 0 ▶{
Subroutine _get_context: not called CC=2 SCAR=17.9
354 0 my $self = shift;
355 0 my $context = shift;
356  
357 0 my %translation = ( lang => $self->schema->lang );
358 0 ▶ if($context)
Branch: 1 branch
BranchTrueFalseTotal
if ($context) 0 0 0
359 {
360 # merge the context with our language for translation.
361 0 @translation{keys %$context} = values %$context;
362 }
363 0 $context = \%translation;
364 0 return $context;
365 }
366  
367 sub _inflate_object
368 0 ▶{
Subroutine _inflate_object: not called CC=4 SCAR=30.0
369 0 my $self = shift;
370 0 my $object_class = shift;
371 0 my $object = shift;
372  
373 0 foreach my $attribute ($self->object_class->meta->get_all_attributes) {
374 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
375 {
376 0 $object->{$attribute->name} = $self->_parse_datetime($object->{$attribute->name});
377 }
378 }
379 0 return $object_class->new($object);
380 }
381  
382 0 ▶sub _do_strptime {
Subroutine _do_strptime: not called CC=2 SCAR=17.9
383 0 my ($self, $string, $format) = @_;
384 0 ▶ return unless $string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 0 0 0
385 0 my $parser = DateTime::Format::Strptime->new(pattern => $format, time_zone => 'UTC');
386 0 return $parser->parse_datetime($string);
387 }
388  
389 0 ▶sub _parse_datetime {
Subroutine _parse_datetime: not called CC=2 SCAR=17.9
390 0 my ($self, $string) = @_;
391 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
392 }
393  
394 =head2 default_values
395  
396 Returns an instance of the object filled in with the default values suggested by OpenERP.
397  
398 =cut
399 sub default_values
400 0 ▶{
Subroutine default_values: not called CC=1 SCAR=6.9
Pod documented
401 0 my $self = shift;
402 0 my $context = shift;
403 # do a default_get
404  
405 0 my @fields = map { $_->name } $self->object_class->meta->get_all_attributes;
406 0 my $object = $self->schema->client->get_defaults($self->object_class->model, \@fields, $context);
407 0 my $class = MooseX::NotRequired::make_optional_subclass($self->object);
408 0 return $self->_inflate_object($class, $object);
409 }
410  
411 =head2 create_related_object_for_DBIC
412  
413 Creates a related DBIC object for an object of this class (before the object
414 is created).
415  
416 It returns a transaction guard alongside the id so that if the corresponding
417 object fails to create it can be aborted.
418  
419 This can make the link up smoother as you know the id of the object to refer
420 to in OpenERP before creating the OpenERP object. It also allows for failures
421 to be dealt with more reliably.
422  
423 my ($id, $guard) = $self->create_related_object_for_DBIC('details', $details);
424 # Create the object
425 $object->{x_dbic_link_id} = $id;
426 $object->{default_code} = sprintf("OBJ%06d", $id);
427  
428 my $prod = $self->$orig($object);
429 $guard->commit;
430  
431 =cut
432  
433 sub create_related_object_for_DBIC
434 0 ▶{
Subroutine create_related_object_for_DBIC: not called CC=3 SCAR=24.8
Pod documented
435 0 my ($self, $relation_name, $data) = @_;
436 0 my $object = $self->object_class;
437 0 my $relation = $object->meta->link->{$relation_name};
438 0 ▶ if($relation)
Branch: 1 branch
BranchTrueFalseTotal
if ($relation) { } 0 0 0
439 {
440 0 ▶ die 'Wrong type of relation' unless $relation->{class} eq 'DBIC';
Branch: 1 branch
BranchTrueFalseTotal
unless $relation->{"class"} eq "DBIC" 0 0 0
441 0 my $link = $self->schema->link($relation->{class});
442 0 my $guard = $link->dbic_schema->storage->txn_scope_guard;
443 0 my $id = $link->create($relation->{args}, $data);
444 0 return ($id, $guard);
445 }
446 else
447 {
448 0 die 'Unable to find relation';
449 }
450 }
451 #-------------------------------------------------------------------------------
452  
453 =head2 retrieve_list
454  
455 Takes a reference to a list of object IDs and returns a list of objects.
456  
457 my @list = $schema->class('Name')->retrieve_list([32, 15, 60]);
458  
459 =cut
460  
461 0 ▶sub retrieve_list {
Subroutine retrieve_list: not called CC=1 SCAR=6.9
Pod documented
462 0 my $self = shift;
463 0 return $self->_retrieve_list(1, @_);
464 }
465  
466 0 ▶sub _retrieve_list {
Subroutine _retrieve_list: not called CC=6 SCAR=37.4
467 0 my ($self, $inflate_objects, $ids, @args) = @_;
468
469 0 my $context = $self->_get_context(shift @args);
470 0 $self->_ensure_object_fields(\@args);
471 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
472 0 foreach my $attribute ($self->object_class->meta->get_all_attributes) {
473 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
474 {
475 0 map { $_->{$attribute->name} = $self->_parse_datetime($_->{$attribute->name}) } @$objects;
476 }
477 }
478 0 my %id_map = map { $_->{id} => $_ } @$objects;
479 0 my @sorted = map { $id_map{$_} } @$ids;
480 0 ▶ return map {$self->object_class->new($_)} @sorted if $inflate_objects;
Branch: 1 branch
BranchTrueFalseTotal
if $inflate_objects 0 0 0
481 0 return @sorted;
482 }
483 }
484  
485  
486 #-------------------------------------------------------------------------------
487  
488 sub _collapse_data_to_ids
489 0 ▶{
Subroutine _collapse_data_to_ids: not called CC=14 SCAR=53.5
490 0 my ($self, $object_data) = @_;
491  
492 0 my $relationships = $self->object_class->meta->relationship;
493 0 ▶ while (my ($name, $rel) = each %$relationships) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$relationships) 0 0 0
494 0 ▶ if ($rel->{type} eq 'one2many') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->{"type"} eq "one2many") 0 0 0
495 0 ▶ if ($object_data->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_data->{$name}) 0 0 0
496 0 $object_data->{$rel->{key}} = $self->_id($rel, $object_data->{$name});
497 0 ▶ delete $object_data->{$name} if $name ne $rel->{key};
Branch: 1 branch
BranchTrueFalseTotal
if $name ne $rel->{"key"} 0 0 0
498 }
499 }
500
501 0 ▶ if ($rel->{type} eq 'many2one') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->{"type"} eq "many2one") 0 0 0
502 0 ▶ if ($object_data->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_data->{$name}) 0 0 0
503 0 $object_data->{$rel->{key}} = $self->_id($rel, $object_data->{$name});
504 }
505 0 ▶ if ($name ne $rel->{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name ne $rel->{"key"}) 0 0 0
506 0 delete $object_data->{$name};
507 }
508 }
509 0 ▶ if ($rel->{type} eq 'many2many') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->{"type"} eq "many2many") 0 0 0
510 0 ▶ if ($object_data->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_data->{$name}) 0 0 0
511 0 my $val = $object_data->{$name};
512 0 my @ids;
513 0 ▶ if(ref $val eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 0 0
514 {
515 # they passed in an arrayref.
516 0 my $objects = $val;
517 0 @ids = map { $self->_id($rel, $_) } @$objects;
518 }
519 else
520 {
521 # assume it's a single object.
522 0 push @ids, $self->_id($rel, $val);
523 }
524 0 $object_data->{$rel->{key}} = [[ 6, 0, \@ids ]];
525 0 ▶ delete $object_data->{$name} if $name ne $rel->{key};
Branch: 1 branch
BranchTrueFalseTotal
if $name ne $rel->{"key"} 0 0 0
526 }
527 }
528 }
529 # Force Str parameters to be object type RPC::XML::string
530 0 foreach my $attribute ($self->object_class->meta->get_all_attributes) {
531 0 ▶ if (exists $object_data->{$attribute->name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$object_data{$attribute->name}) 0 0 0
532 0 $object_data->{$attribute->name} = $self->prepare_attribute_for_send($attribute->type_constraint, $object_data->{$attribute->name});
533 }
534 }
535 0 return $object_data;
536 }
537  
538 sub _id
539 0 ▶{
Subroutine _id: not called CC=4 SCAR=30.0
540 0 my $self = shift;
541 0 my $rel = shift;
542 0 my $val = shift;
543 0 my $ref = ref $val;
544 0 ▶ if($ref)
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) 0 0 0
545 {
546 # FIXME: this is close to what I want but I need to be doing it with the class
547 # that corresponds to the relation we're delving into.
548 0 ▶ if($ref eq 'HASH')
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
549 {
550 0 my $class = $self->schema->class($rel->{class});
551 0 return [[ 0, 0, $class->_collapse_data_to_ids($val) ]];
552 }
553 elsif($ref eq 'ARRAY')
554 {
555 # this should allow us to do child objects too.
556 0 my $class = $self->schema->class($rel->{class});
557 0 my @expanded = map { [ 0, 0, $class->_collapse_data_to_ids($_) ] } @$val;
558 0 return \@expanded;
559 }
560 else
561 {
562 0 return $val->id;
563 }
564 }
565 0 return $val;
566 }
567  
568 =head2 create
569  
570 Creates a new instance of an object in OpenERP.
571  
572 my $obj = $schema->class('Name')->create({
573 name => 'OpusVL',
574 active => 1,
575 });
576  
577 Takes a hashref of object parameters.
578  
579 Returns the new object or C<undef> if it could not be created.
580  
581 =cut
582  
583 0 ▶sub create {
Subroutine create: not called CC=2 SCAR=17.9
Pod documented
584 0 my ($self, $object_data, @args) = @_;
585  
586 ### Create called with initial object data:
587 ### $object_data;
588
589 0 $object_data = $self->_collapse_data_to_ids($object_data);
590  
591 ### To
592 ### $object_data;
593 0 my $id;
594 0 ▶ $self->_with_retries(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
595 0 $id = $self->schema->client->create($self->object_class->model, $object_data, @args);
596 0 });
597 0 ▶ if ($id)
Branch: 1 branch
BranchTrueFalseTotal
if ($id) 0 0 0
598 {
599 0 return $self->retrieve($id);
600 }
601 }
602  
603 sub _with_retries
604 4 ▶{
Subroutine _with_retries: called CC=1 SCAR=0.0
605 4 my $self = shift;
606 4 my $call = shift;
607 retry
608 17 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
609 17 $call->();
610 }
611 16 ▶ retry_if {/current transaction is aborted, commands ignored until end of transaction block/}
Subroutine __ANON__: called CC=1 SCAR=0.0
612 catch
613 3 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
614 3 die $_; # rethrow the unhandled exception
615 4 };
616 }
617  
618  
619 #-------------------------------------------------------------------------------
620  
621 =head2 execute
622  
623 Performs an execute in OpenERP on the class level.
624  
625 $c->model('OpenERP')->class('Invoice')->execute('build_invoice', $args);
626  
627 Please look at L<OpenERP::OOM::Object::Base> for more information on C<execute>
628  
629 =cut
630  
631 0 ▶sub execute {
Subroutine execute: not called CC=1 SCAR=6.9
Pod documented
632 0 my $self = shift;
633 0 my $action = shift;
634 0 my @params = @_;
635 0 my @args = ($action, $self->object_class->model, @params);
636 0 my $retval;
637 0 ▶ $self->_with_retries(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
638 0 $retval = $self->schema->client->object_execute(@args);
639 0 });
640 0 return $retval;
641 }
642  
643 #-------------------------------------------------------------------------------
644  
645  
646 3 1;
« OOM/Object/Base.pm ↑ Summary OOM/DynamicUtils.pm »