From 127377bdf3be2b1ef350088e6e5693726e6d172e Mon Sep 17 00:00:00 2001 From: Jason Stephenson Date: Wed, 10 Aug 2011 10:55:34 -0400 Subject: [PATCH] Add some return guards in OpenILS::Application::AppUtils. Signed-off-by: Jason Stephenson --- .../perlmods/lib/OpenILS/Application/AppUtils.pm | 192 +++++++++++---------- 1 file changed, 104 insertions(+), 88 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm index 8b297015bd..34782d794c 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm @@ -62,15 +62,16 @@ my $PERM_QUERY = { # returns undef if user has all of the perms provided # returns the first failed perm on failure sub check_user_perms { - my($self, $user_id, $org_id, @perm_types ) = @_; - $logger->debug("Checking perms with user : $user_id , org: $org_id, @perm_types"); - - for my $type (@perm_types) { - $PERM_QUERY->{select}->{au}->[0]->{params} = [$type, $org_id]; - $PERM_QUERY->{where}->{id} = $user_id; - return $type unless $self->is_true(OpenILS::Utils::CStoreEditor->new->json_query($PERM_QUERY)->[0]->{has_perm}); - } - return undef; + my($self, $user_id, $org_id, @perm_types ) = @_; + $logger->debug("Checking perms with user : $user_id , org: $org_id, @perm_types"); + + for my $type (@perm_types) { + $PERM_QUERY->{select}->{au}->[0]->{params} = [$type, $org_id]; + $PERM_QUERY->{where}->{id} = $user_id; + my $r = OpenILS::Utils::CStoreEditor->new->json_query($PERM_QUERY); + return $type unless (ref($r) eq 'ARRAY' and $self->is_true($r->[0]->{has_perm})); + } + return undef; } # checks the list of user perms. The first one that fails returns a new @@ -980,16 +981,16 @@ sub logmark { # takes a copy id sub fetch_open_circulation { - my( $self, $cid ) = @_; - $self->logmark; + my( $self, $cid ) = @_; + $self->logmark; - my $e = OpenILS::Utils::CStoreEditor->new; + my $e = OpenILS::Utils::CStoreEditor->new; my $circ = $e->search_action_circulation({ - target_copy => $cid, - stop_fines_time => undef, - checkin_time => undef - })->[0]; - + target_copy => $cid, + stop_fines_time => undef, + checkin_time => undef + }); + $circ = $circ->[0] if (ref($circ) eq 'ARRAY'); return ($circ, $e->event); } @@ -1199,18 +1200,20 @@ sub fetch_bill { my $ORG_TREE; sub fetch_org_tree { - my $self = shift; - return $ORG_TREE if $ORG_TREE; - return $ORG_TREE = OpenILS::Utils::CStoreEditor->new->search_actor_org_unit( + my $self = shift; + return $ORG_TREE if $ORG_TREE; + $ORG_TREE = OpenILS::Utils::CStoreEditor->new->search_actor_org_unit( [ {"parent_ou" => undef }, { - flesh => -1, - flesh_fields => { aou => ['children'] }, + flesh => -1, + flesh_fields => { aou => ['children'] }, order_by => { aou => 'name'} } ] - )->[0]; + ); + $ORG_TREE = $ORG_TREE->[0] if (ref($ORG_TREE) eq 'ARRAY'); + return $ORG_TREE; } sub walk_org_tree { @@ -1362,11 +1365,11 @@ sub ou_ancestor_setting { } my $query = {from => ['actor.org_unit_ancestor_setting', $name, $orgid]}; - my $setting = $e->json_query($query)->[0]; - return undef unless $setting; + my $setting = $e->json_query($query); + return undef unless (ref($setting) eq 'ARRAY'); + $setting = $setting->[0] if (ref($setting) eq 'ARRAY'); return {org => $setting->{org_unit}, value => OpenSRF::Utils::JSON->JSON2perl($setting->{value})}; -} - +} # returns the ISO8601 string representation of the requested epoch in GMT sub epoch2ISO8601 { @@ -1434,53 +1437,56 @@ sub get_org_types { } sub get_org_tree { - my $self = shift; - my $locale = shift || ''; - my $cache = OpenSRF::Utils::Cache->new("global", 0); - my $tree = $cache->get_cache("orgtree.$locale"); - return $tree if $tree; - - my $ses = OpenILS::Utils::CStoreEditor->new; - $ses->session->session_locale($locale); - $tree = $ses->search_actor_org_unit( - [ + my $self = shift; + my $locale = shift || ''; + my $cache = OpenSRF::Utils::Cache->new("global", 0); + my $tree = $cache->get_cache("orgtree.$locale"); + return $tree if $tree; + + my $ses = OpenILS::Utils::CStoreEditor->new; + $ses->session->session_locale($locale); + my $r = $ses->search_actor_org_unit( + [ {"parent_ou" => undef }, { - flesh => -1, - flesh_fields => { aou => ['children'] }, - order_by => { aou => 'name'} + flesh => -1, + flesh_fields => { aou => ['children'] }, + order_by => { aou => 'name'} } ] - )->[0]; + ); + $tree = $r->[0] if (ref($r) eq 'ARRAY'); - $cache->put_cache("orgtree.$locale", $tree); - return $tree; + $cache->put_cache("orgtree.$locale", $tree); + return $tree; } sub get_org_descendants { - my($self, $org_id, $depth) = @_; + my($self, $org_id, $depth) = @_; - my $select = { - transform => 'actor.org_unit_descendants', - column => 'id', - result_field => 'id', - }; - $select->{params} = [$depth] if defined $depth; + my $select = { + transform => 'actor.org_unit_descendants', + column => 'id', + result_field => 'id', + }; + $select->{params} = [$depth] if defined $depth; - my $org_list = OpenILS::Utils::CStoreEditor->new->json_query({ + my $org_list = OpenILS::Utils::CStoreEditor->new->json_query({ select => {aou => [$select]}, from => 'aou', where => {id => $org_id} }); - my @orgs; - push(@orgs, $_->{id}) for @$org_list; - return \@orgs; + my @orgs; + if (ref($org_list) eq 'ARRAY') { + push(@orgs, $_->{id}) for @$org_list; + } + return \@orgs; } sub get_org_ancestors { - my($self, $org_id) = @_; + my($self, $org_id) = @_; - my $org_list = OpenILS::Utils::CStoreEditor->new->json_query({ + my $org_list = OpenILS::Utils::CStoreEditor->new->json_query({ select => { aou => [{ transform => 'actor.org_unit_ancestors', @@ -1493,51 +1499,55 @@ sub get_org_ancestors { where => {id => $org_id} }); - my @orgs; - push(@orgs, $_->{id}) for @$org_list; - return \@orgs; + my @orgs; + if (ref($org_list) eq 'ARRAY') { + push(@orgs, $_->{id}) for @$org_list; + } + return \@orgs; } sub get_org_full_path { - my($self, $org_id, $depth) = @_; + my($self, $org_id, $depth) = @_; my $query = { - select => { - aou => [{ - transform => 'actor.org_unit_full_path', - column => 'id', - result_field => 'id', - }], - }, - from => 'aou', - where => {id => $org_id} - }; + select => { + aou => [{ + transform => 'actor.org_unit_full_path', + column => 'id', + result_field => 'id', + }], + }, + from => 'aou', + where => {id => $org_id} + }; $query->{select}->{aou}->[0]->{params} = [$depth] if defined $depth; - my $org_list = OpenILS::Utils::CStoreEditor->new->json_query($query); - return [ map {$_->{id}} @$org_list ]; + my $org_list = OpenILS::Utils::CStoreEditor->new->json_query($query); + return [ map {$_->{id}} @$org_list ] if (ref($org_list) eq 'ARRAY'); + return []; } # returns the ID of the org unit ancestor at the specified depth sub org_unit_ancestor_at_depth { my($class, $org_id, $depth) = @_; my $resp = OpenILS::Utils::CStoreEditor->new->json_query( - {from => ['actor.org_unit_ancestor_at_depth', $org_id, $depth]})->[0]; - return ($resp) ? $resp->{id} : undef; + {from => ['actor.org_unit_ancestor_at_depth', $org_id, $depth]}); + return (ref($resp) eq 'ARRAY') ? $resp->[0]->{id} : undef; } # returns the user's configured locale as a string. Defaults to en-US if none is configured. sub get_user_locale { - my($self, $user_id, $e) = @_; - $e ||= OpenILS::Utils::CStoreEditor->new; + my($self, $user_id, $e) = @_; + $e ||= OpenILS::Utils::CStoreEditor->new; - # first, see if the user has an explicit locale set - my $setting = $e->search_actor_user_setting( - {usr => $user_id, name => 'global.locale'})->[0]; - return OpenSRF::Utils::JSON->JSON2perl($setting->value) if $setting; + # first, see if the user has an explicit locale set + my $setting = $e->search_actor_user_setting( + {usr => $user_id, name => 'global.locale'}); + $setting = $setting->[0] if (ref($setting) eq 'ARRAY'); + return OpenSRF::Utils::JSON->JSON2perl($setting->value) if $setting; - my $user = $e->retrieve_actor_user($user_id) or return $e->event; - return $self->get_org_locale($user->home_ou, $e); + my $user = $e->retrieve_actor_user($user_id) or return $e->event; + return $self->get_org_locale($user->home_ou, $e); } # returns org locale setting @@ -1641,7 +1651,7 @@ sub xact_org { "where" => {id => $xact_id}, }); - return $loc->[0]->{circ_lib} if @$loc; + return $loc->[0]->{circ_lib} if (ref($loc) eq 'ARRAY' and @$loc); $loc = $e->json_query({ "select" => {bresv => ["request_lib"]}, @@ -1649,7 +1659,7 @@ sub xact_org { "where" => {id => $xact_id}, }); - return $loc->[0]->{request_lib} if @$loc; + return $loc->[0]->{request_lib} if (ref($loc) eq 'ARRAY' and @$loc); $loc = $e->json_query({ "select" => {mg => ["billing_location"]}, @@ -1657,7 +1667,8 @@ sub xact_org { "where" => {id => $xact_id}, }); - return $loc->[0]->{billing_location}; + return $loc->[0]->{billing_location} if (ref($loc) eq 'ARRAY' and @$loc); + return undef; } @@ -1672,9 +1683,9 @@ sub find_event_def_by_hook { for my $org_id (reverse @$orgs) { my $def = $e->search_action_trigger_event_definition( - {hook => $hook, owner => $org_id})->[0]; + {hook => $hook, owner => $org_id}); - return $def if $def; + return $def->[0] if (ref($def) eq 'ARRAY' and @$def); } return undef; @@ -1811,7 +1822,12 @@ sub create_uuid_string { sub create_circ_chain_summary { my($class, $e, $circ_id) = @_; - my $sum = $e->json_query({from => ['action.summarize_circ_chain', $circ_id]})->[0]; + my $sum = $e->json_query({from => ['action.summarize_circ_chain', $circ_id]}); + if (ref($sum) eq 'ARRAY' and @$sum) { + $sum = $sum->[0]; + } else { + $sum = undef; + } return undef unless $sum; my $obj = Fieldmapper::action::circ_chain_summary->new; $obj->$_($sum->{$_}) for keys %$sum; -- 2.11.0