From 68a4e3b1c8133c9c98503f5d2128e054bf1d2114 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Mon, 12 Mar 2012 15:05:50 -0400 Subject: [PATCH] Org unit retrieval cleanup Clean up and consolidate the various org unit retrieval method (in Perl) to reduce code/confusion and make it easier to integrate changes to the org unit tree in the future. Signed-off-by: Bill Erickson Signed-off-by: Jason Stephenson --- .../perlmods/lib/OpenILS/Application/Actor.pm | 40 ------- .../lib/OpenILS/Application/AppUtils.pm | 108 +----------------- .../perlmods/lib/OpenILS/Application/Cat.pm | 4 +- .../OpenILS/Application/Circ/ScriptBuilder.pm | 11 +- .../lib/OpenILS/Application/Collections.pm | 2 +- .../lib/OpenILS/Application/Search/Biblio.pm | 2 +- .../OpenILS/Application/Search/CNBrowse.pm | 2 +- 7 files changed, 10 insertions(+), 159 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Actor.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Actor.pm index 77277e48f3..966a991602 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Actor.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Actor.pm @@ -2789,46 +2789,6 @@ sub safe_token_home_lib { } - -__PACKAGE__->register_method( - method => 'slim_tree', - api_name => "open-ils.actor.org_tree.slim_hash.retrieve", -); -sub slim_tree { - my $tree = new_editor()->search_actor_org_unit( - [ - {"parent_ou" => undef }, - { - flesh => -1, - flesh_fields => { aou => ['children'] }, - order_by => { aou => 'name'}, - select => { aou => ["id","shortname", "name"]}, - } - ] - )->[0]; - - return trim_tree($tree); -} - - -sub trim_tree { - my $tree = shift; - return undef unless $tree; - my $htree = { - code => $tree->shortname, - name => $tree->name, - }; - if( $tree->children and @{$tree->children} ) { - $htree->{children} = []; - for my $c (@{$tree->children}) { - push( @{$htree->{children}}, trim_tree($c) ); - } - } - - return $htree; -} - - __PACKAGE__->register_method( method => "update_penalties", api_name => "open-ils.actor.user.penalties.update" diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm index 792f53bb6f..2529d6b6a8 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm @@ -189,89 +189,8 @@ sub simple_scalar_request { return $val; } - - - - -my $tree = undef; -my $orglist = undef; -my $org_typelist = undef; -my $org_typelist_hash = {}; - -sub __get_org_tree { - - # can we throw this version away?? - - my $self = shift; - if($tree) { return $tree; } - - # see if it's in the cache - $tree = $cache_client->new()->get_cache('_orgtree'); - if($tree) { return $tree; } - - if(!$orglist) { - warn "Retrieving Org Tree\n"; - $orglist = $self->simple_scalar_request( - "open-ils.cstore", - "open-ils.cstore.direct.actor.org_unit.search.atomic", - { id => { '!=' => undef } } - ); - } - - if( ! $org_typelist ) { - warn "Retrieving org types\n"; - $org_typelist = $self->simple_scalar_request( - "open-ils.cstore", - "open-ils.cstore.direct.actor.org_unit_type.search.atomic", - { id => { '!=' => undef } } - ); - $self->build_org_type($org_typelist); - } - - $tree = $self->build_org_tree($orglist,1); - $cache_client->new()->put_cache('_orgtree', $tree); - return $tree; - -} - -my $slimtree = undef; -sub get_slim_org_tree { - - my $self = shift; - if($slimtree) { return $slimtree; } - - # see if it's in the cache - $slimtree = $cache_client->new()->get_cache('slimorgtree'); - if($slimtree) { return $slimtree; } - - if(!$orglist) { - warn "Retrieving Org Tree\n"; - $orglist = $self->simple_scalar_request( - "open-ils.cstore", - "open-ils.cstore.direct.actor.org_unit.search.atomic", - { id => { '!=' => undef } } - ); - } - - $slimtree = $self->build_org_tree($orglist); - $cache_client->new->put_cache('slimorgtree', $slimtree); - return $slimtree; - -} - - -sub build_org_type { - my($self, $org_typelist) = @_; - for my $type (@$org_typelist) { - $org_typelist_hash->{$type->id()} = $type; - } -} - - - sub build_org_tree { - - my( $self, $orglist, $add_types ) = @_; + my( $self, $orglist ) = @_; return $orglist unless ref $orglist; return $$orglist[0] if @$orglist == 1; @@ -283,11 +202,6 @@ sub build_org_tree { for my $org (@list) { next unless ($org); - - if(!ref($org->ou_type()) and $add_types) { - $org->ou_type( $org_typelist_hash->{$org->ou_type()}); - } - next if (!defined($org->parent_ou) || $org->parent_ou eq ""); my ($parent) = grep { $_->id == $org->parent_ou } @list; @@ -1220,22 +1134,6 @@ sub fetch_bill { return($bill, $evt); } -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( - [ - {"parent_ou" => undef }, - { - flesh => -1, - flesh_fields => { aou => ['children'] }, - order_by => { aou => 'name'} - } - ] - )->[0]; -} - sub walk_org_tree { my( $self, $node, $callback ) = @_; return unless $node; @@ -1456,11 +1354,12 @@ sub get_org_types { return $org_types = OpenILS::Utils::CStoreEditor->new->retrieve_all_actor_org_unit_type(); } +my %ORG_TREE; 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"); + my $tree = $ORG_TREE{$locale} || $cache->get_cache("orgtree.$locale"); return $tree if $tree; my $ses = OpenILS::Utils::CStoreEditor->new; @@ -1476,6 +1375,7 @@ sub get_org_tree { ] )->[0]; + $ORG_TREE{$locale} = $tree; $cache->put_cache("orgtree.$locale", $tree); return $tree; } diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat.pm index b9b79bee32..1c397e1e1b 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat.pm @@ -1286,7 +1286,7 @@ sub acn_sms_msg { my($self, $conn, $auth, $org_id, $carrier, $number, $target_ids) = @_; my $sms_enable = $U->ou_ancestor_setting_value( - $org_id || $U->fetch_org_tree->id, + $org_id || $U->get_org_tree->id, 'sms.enable' ); # We could maybe make a Validator for this on the templates @@ -1295,7 +1295,7 @@ sub acn_sms_msg { } my $disable_auth = $U->ou_ancestor_setting_value( - $org_id || $U->fetch_org_tree->id, + $org_id || $U->get_org_tree->id, 'sms.disable_authentication_requirement.callnumbers' ); diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/ScriptBuilder.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/ScriptBuilder.pm index 7589cab9ef..f042665ed9 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/ScriptBuilder.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/ScriptBuilder.pm @@ -275,16 +275,7 @@ sub insert_org_methods { my ( $editor, $runner ) = @_; if(!$ORG_TREE) { - $ORG_TREE = $editor->search_actor_org_unit( - [ - {"parent_ou" => undef }, - { - flesh => -1, - flesh_fields => { aou => ['children'] }, - order_by => { aou => 'name'} - } - ] - )->[0]; + $ORG_TREE = $U->get_org_tree; flatten_org_tree($ORG_TREE); } diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Collections.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Collections.pm index e5da73cbc1..97c07f7361 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Collections.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Collections.pm @@ -727,7 +727,7 @@ sub transaction_details { or return $e->event; $org = $org->[0]; # get a reference to the org inside of the tree - $org = $U->find_org($U->fetch_org_tree(), $org->id); + $org = $U->find_org($U->get_org_tree(), $org->id); my @data; for my $uid (@$user_list) { diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm index 82ad1af73a..0933acc769 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm @@ -1928,7 +1928,7 @@ sub format_biblio_record_entry { if ($for_print) { $bib_id = $arg1; - $context_org = $arg2 || $U->fetch_org_tree->id; + $context_org = $arg2 || $U->get_org_tree->id; $e = new_editor(xact => 1); } elsif ($for_email) { $auth = $arg1; diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/CNBrowse.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/CNBrowse.pm index eade47a727..4e4d9c865d 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/CNBrowse.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/CNBrowse.pm @@ -80,7 +80,7 @@ sub cn_browse { my( $self, $conn, $cn, $orgid, $size, $offset, $copy_statuses, $copy_locations ) = @_; my $ses = OpenSRF::AppSession->create('open-ils.supercat'); - my $tree = $U->get_slim_org_tree; + my $tree = $U->get_org_tree; my $name = _find_shortname($orgid, $tree); $logger->debug("cn browse found or name $name"); -- 2.43.2