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