From 94813cbe6a061b95079a7a40375473e9c7914eaf Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Thu, 7 Feb 2013 13:43:19 -0500 Subject: [PATCH] rank_ou() honors pref-lib in non-global context When determining how to sort an org unit (e.g. sorting copies by circ lib for display in the catalog), allow the pref-lib to affect the sort order in global and non-global searches. Org units are now sorted with the following criteria in the following order. For example, assume we are sorting a copy circ_lib: 1. circ_lib matches the search_lib 2. circ_lib matches pref_lib 3. distance of circ_lib from pref_lib when pref_lib is a child of search_lib, if circ_lib is a child of pref_lib. (For example, searching CONS with pref_lib SYS1, items at BR1 will sort ahead of items at BR3, since BR1 is a child of the pref_lib). 4. proximity of circ_lib to search_lib, when circ_lib is a child of search_lib. 5. In all other cases, circ_lib is sorted to the bottom with the rest of the riffraff. Signed-off-by: Bill Erickson Signed-off-by: Ben Shum --- Open-ILS/src/sql/Pg/990.schema.unapi.sql | 31 +++++++++++++------ .../Pg/upgrade/XXXX.schema.relax_rank_ou.sql | 29 +++++++++++++++++ 2 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 Open-ILS/src/sql/Pg/upgrade/XXXX.schema.relax_rank_ou.sql diff --git a/Open-ILS/src/sql/Pg/990.schema.unapi.sql b/Open-ILS/src/sql/Pg/990.schema.unapi.sql index 05eafcf385..44e1c4db33 100644 --- a/Open-ILS/src/sql/Pg/990.schema.unapi.sql +++ b/Open-ILS/src/sql/Pg/990.schema.unapi.sql @@ -16,17 +16,28 @@ $$ LANGUAGE SQL STABLE; CREATE OR REPLACE FUNCTION evergreen.rank_ou(lib INT, search_lib INT, pref_lib INT DEFAULT NULL) RETURNS INTEGER AS $$ - WITH search_libs AS ( - SELECT id, distance FROM actor.org_unit_descendants_distance($2) - ) SELECT COALESCE( - (SELECT -10000 FROM actor.org_unit - WHERE $1 = $3 AND id = $3 AND $2 IN ( - SELECT id FROM actor.org_unit WHERE parent_ou IS NULL - ) - ), - (SELECT distance FROM search_libs WHERE id = $1), - 10000 + + -- lib matches search_lib + (SELECT CASE WHEN $1 = $2 THEN -20000 END), + + -- lib matches pref_lib + (SELECT CASE WHEN $1 = $3 THEN -10000 END), + + + -- pref_lib is a child of search_lib and lib is a child of pref lib. + -- For example, searching CONS, pref lib is SYS1, + -- copies at BR1 and BR2 sort to the front. + (SELECT distance - 5000 + FROM actor.org_unit_descendants_distance($3) + WHERE id = $1 AND $3 IN ( + SELECT id FROM actor.org_unit_descendants($2))), + + -- lib is a child of search_lib + (SELECT distance FROM actor.org_unit_descendants_distance($2) WHERE id = $1), + + -- all others pay cash + 1000 ); $$ LANGUAGE SQL STABLE; diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.relax_rank_ou.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.relax_rank_ou.sql new file mode 100644 index 0000000000..626d40b869 --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.relax_rank_ou.sql @@ -0,0 +1,29 @@ +BEGIN; + +CREATE OR REPLACE FUNCTION evergreen.rank_ou(lib INT, search_lib INT, pref_lib INT DEFAULT NULL) +RETURNS INTEGER AS $$ + SELECT COALESCE( + + -- lib matches search_lib + (SELECT CASE WHEN $1 = $2 THEN -20000 END), + + -- lib matches pref_lib + (SELECT CASE WHEN $1 = $3 THEN -10000 END), + + + -- pref_lib is a child of search_lib and lib is a child of pref lib. + (SELECT distance - 5000 + FROM actor.org_unit_descendants_distance($3) + WHERE id = $1 AND $3 IN ( + SELECT id FROM actor.org_unit_descendants($2))), + + -- lib is a child of search_lib + (SELECT distance FROM actor.org_unit_descendants_distance($2) WHERE id = $1), + + -- all others pay cash + 1000 + ); +$$ LANGUAGE SQL STABLE; + +COMMIT; + -- 2.43.2