]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/XXX.schema.unapi_exclude_invisible_acn.sql
cae52166c0634318b2049748791204c6cbb7b9fd
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / XXX.schema.unapi_exclude_invisible_acn.sql
1
2 BEGIN;
3
4 DROP FUNCTION IF EXISTS 
5     evergreen.ranked_volumes(BIGINT, INT, INT, HSTORE, HSTORE, INT);
6
7 CREATE OR REPLACE FUNCTION evergreen.ranked_volumes(
8     bibid BIGINT, 
9     ouid INT,
10     depth INT DEFAULT NULL,
11     slimit HSTORE DEFAULT NULL,
12     soffset HSTORE DEFAULT NULL,
13     pref_lib INT DEFAULT NULL,
14     includes TEXT[] DEFAULT NULL::TEXT[]
15 ) RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank BIGINT) AS $$
16     SELECT ua.id, ua.name, ua.label_sortkey, MIN(ua.rank) AS rank FROM (
17         SELECT acn.id, aou.name, acn.label_sortkey,
18             evergreen.rank_ou(aou.id, $2, $6), evergreen.rank_cp_status(acp.status),
19             RANK() OVER w
20         FROM asset.call_number acn
21             JOIN asset.copy acp ON (acn.id = acp.call_number)
22             JOIN actor.org_unit_descendants( $2, COALESCE(
23                 $3, (
24                     SELECT depth
25                     FROM actor.org_unit_type aout
26                         INNER JOIN actor.org_unit ou ON ou_type = aout.id
27                     WHERE ou.id = $2
28                 ), $6)
29             ) AS aou ON (acp.circ_lib = aou.id)
30         WHERE acn.record = $1
31             AND acn.deleted IS FALSE
32             AND acp.deleted IS FALSE
33             AND CASE WHEN ('exclude_invisible_acn' = ANY($7)) THEN 
34                 EXISTS (
35                     SELECT 1 
36                     FROM asset.opac_visible_copies 
37                     WHERE copy_id = acp.id AND record = acn.record
38                 ) ELSE TRUE END
39         GROUP BY acn.id, acp.status, aou.name, acn.label_sortkey, aou.id
40         WINDOW w AS (
41             ORDER BY evergreen.rank_ou(aou.id, $2, $6), evergreen.rank_cp_status(acp.status)
42         )
43     ) AS ua
44     GROUP BY ua.id, ua.name, ua.label_sortkey
45     ORDER BY rank, ua.name, ua.label_sortkey
46     LIMIT ($4 -> 'acn')::INT
47     OFFSET ($5 -> 'acn')::INT;
48 $$
49 LANGUAGE SQL STABLE;
50
51 CREATE OR REPLACE FUNCTION unapi.holdings_xml (
52     bid BIGINT,
53     ouid INT,
54     org TEXT,
55     depth INT DEFAULT NULL,
56     includes TEXT[] DEFAULT NULL::TEXT[],
57     slimit HSTORE DEFAULT NULL,
58     soffset HSTORE DEFAULT NULL,
59     include_xmlns BOOL DEFAULT TRUE,
60     pref_lib INT DEFAULT NULL
61 )
62 RETURNS XML AS $F$
63      SELECT  XMLELEMENT(
64                  name holdings,
65                  XMLATTRIBUTES(
66                     CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
67                     CASE WHEN ('bre' = ANY ($5)) THEN 'tag:open-ils.org:U2@bre/' || $1 || '/' || $3 ELSE NULL END AS id,
68                     (SELECT record_has_holdable_copy FROM asset.record_has_holdable_copy($1)) AS has_holdable
69                  ),
70                  XMLELEMENT(
71                      name counts,
72                      (SELECT  XMLAGG(XMLELEMENT::XML) FROM (
73                          SELECT  XMLELEMENT(
74                                      name count,
75                                      XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
76                                  )::text
77                            FROM  asset.opac_ou_record_copy_count($2,  $1)
78                                      UNION
79                          SELECT  XMLELEMENT(
80                                      name count,
81                                      XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
82                                  )::text
83                            FROM  asset.staff_ou_record_copy_count($2, $1)
84                                      UNION
85                          SELECT  XMLELEMENT(
86                                      name count,
87                                      XMLATTRIBUTES('pref_lib' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
88                                  )::text
89                            FROM  asset.opac_ou_record_copy_count($9,  $1)
90                                      ORDER BY 1
91                      )x)
92                  ),
93                  CASE 
94                      WHEN ('bmp' = ANY ($5)) THEN
95                         XMLELEMENT(
96                             name monograph_parts,
97                             (SELECT XMLAGG(bmp) FROM (
98                                 SELECT  unapi.bmp( id, 'xml', 'monograph_part', evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($5,'bre'), 'holdings_xml'), $3, $4, $6, $7, FALSE)
99                                   FROM  biblio.monograph_part
100                                   WHERE record = $1
101                             )x)
102                         )
103                      ELSE NULL
104                  END,
105                  XMLELEMENT(
106                      name volumes,
107                      (SELECT XMLAGG(acn ORDER BY rank, name, label_sortkey) FROM (
108                         -- Physical copies
109                         SELECT  unapi.acn(y.id,'xml','volume',evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE), y.rank, name, label_sortkey
110                         FROM evergreen.ranked_volumes($1, $2, $4, $6, $7, $9, $5) AS y
111                         UNION ALL
112                         -- Located URIs
113                         SELECT unapi.acn(uris.id,'xml','volume',evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE), 0, name, label_sortkey
114                         FROM evergreen.located_uris($1, $2, $9) AS uris
115                      )x)
116                  ),
117                  CASE WHEN ('ssub' = ANY ($5)) THEN 
118                      XMLELEMENT(
119                          name subscriptions,
120                          (SELECT XMLAGG(ssub) FROM (
121                             SELECT  unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
122                               FROM  serial.subscription
123                               WHERE record_entry = $1
124                         )x)
125                      )
126                  ELSE NULL END,
127                  CASE WHEN ('acp' = ANY ($5)) THEN 
128                      XMLELEMENT(
129                          name foreign_copies,
130                          (SELECT XMLAGG(acp) FROM (
131                             SELECT  unapi.acp(p.target_copy,'xml','copy',evergreen.array_remove_item_by_value($5,'acp'), $3, $4, $6, $7, FALSE)
132                               FROM  biblio.peer_bib_copy_map p
133                                     JOIN asset.copy c ON (p.target_copy = c.id)
134                               WHERE NOT c.deleted AND p.peer_record = $1
135                             LIMIT ($6 -> 'acp')::INT
136                             OFFSET ($7 -> 'acp')::INT
137                         )x)
138                      )
139                  ELSE NULL END
140              );
141 $F$ LANGUAGE SQL STABLE;
142
143 COMMIT;