1 DROP SCHEMA IF EXISTS unapi CASCADE;
6 CREATE OR REPLACE FUNCTION evergreen.org_top()
7 RETURNS SETOF actor.org_unit AS $$
8 SELECT * FROM actor.org_unit WHERE parent_ou IS NULL LIMIT 1;
12 CREATE OR REPLACE FUNCTION evergreen.array_remove_item_by_value(inp ANYARRAY, el ANYELEMENT)
13 RETURNS anyarray AS $$
14 SELECT ARRAY_AGG(x.e) FROM UNNEST( $1 ) x(e) WHERE x.e <> $2;
15 $$ LANGUAGE SQL STABLE;
17 CREATE OR REPLACE FUNCTION evergreen.rank_ou(lib INT, search_lib INT, pref_lib INT DEFAULT NULL)
21 -- lib matches search_lib
22 (SELECT CASE WHEN $1 = $2 THEN -20000 END),
24 -- lib matches pref_lib
25 (SELECT CASE WHEN $1 = $3 THEN -10000 END),
28 -- pref_lib is a child of search_lib and lib is a child of pref lib.
29 -- For example, searching CONS, pref lib is SYS1,
30 -- copies at BR1 and BR2 sort to the front.
31 (SELECT distance - 5000
32 FROM actor.org_unit_descendants_distance($3)
33 WHERE id = $1 AND $3 IN (
34 SELECT id FROM actor.org_unit_descendants($2))),
36 -- lib is a child of search_lib
37 (SELECT distance FROM actor.org_unit_descendants_distance($2) WHERE id = $1),
39 -- all others pay cash
42 $$ LANGUAGE SQL STABLE;
44 CREATE OR REPLACE FUNCTION evergreen.rank_cp_status(status INT)
46 WITH totally_available AS (
47 SELECT id, 0 AS avail_rank
48 FROM config.copy_status
49 WHERE opac_visible IS TRUE
50 AND copy_active IS TRUE
51 AND id != 1 -- "Checked out"
52 ), almost_available AS (
53 SELECT id, 10 AS avail_rank
54 FROM config.copy_status
55 WHERE holdable IS TRUE
56 AND opac_visible IS TRUE
57 AND copy_active IS FALSE
58 OR id = 1 -- "Checked out"
61 (SELECT avail_rank FROM totally_available WHERE $1 IN (id)),
62 (SELECT avail_rank FROM almost_available WHERE $1 IN (id)),
65 $$ LANGUAGE SQL STABLE;
67 CREATE OR REPLACE FUNCTION evergreen.ranked_volumes(
70 depth INT DEFAULT NULL,
71 slimit HSTORE DEFAULT NULL,
72 soffset HSTORE DEFAULT NULL,
73 pref_lib INT DEFAULT NULL,
74 includes TEXT[] DEFAULT NULL::TEXT[]
75 ) RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank BIGINT) AS $$
76 SELECT ua.id, ua.name, ua.label_sortkey, MIN(ua.rank) AS rank FROM (
77 SELECT acn.id, aou.name, acn.label_sortkey,
78 evergreen.rank_ou(aou.id, $2, $6), evergreen.rank_cp_status(acp.status),
80 FROM asset.call_number acn
81 JOIN asset.copy acp ON (acn.id = acp.call_number)
82 JOIN actor.org_unit_descendants( $2, COALESCE(
85 FROM actor.org_unit_type aout
86 INNER JOIN actor.org_unit ou ON ou_type = aout.id
89 ) AS aou ON (acp.circ_lib = aou.id)
90 WHERE acn.record = ANY ($1)
91 AND acn.deleted IS FALSE
92 AND acp.deleted IS FALSE
93 AND CASE WHEN ('exclude_invisible_acn' = ANY($7)) THEN
96 FROM asset.opac_visible_copies
97 WHERE copy_id = acp.id AND record = acn.record
99 GROUP BY acn.id, acp.status, aou.name, acn.label_sortkey, aou.id
101 ORDER BY evergreen.rank_ou(aou.id, $2, $6), evergreen.rank_cp_status(acp.status)
104 GROUP BY ua.id, ua.name, ua.label_sortkey
105 ORDER BY rank, ua.name, ua.label_sortkey
106 LIMIT ($4 -> 'acn')::INT
107 OFFSET ($5 -> 'acn')::INT;
111 CREATE OR REPLACE FUNCTION evergreen.ranked_volumes
112 ( bibid BIGINT, ouid INT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, pref_lib INT DEFAULT NULL, includes TEXT[] DEFAULT NULL::TEXT[] )
113 RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank BIGINT)
114 AS $$ SELECT * FROM evergreen.ranked_volumes(ARRAY[$1],$2,$3,$4,$5,$6,$7) $$ LANGUAGE SQL STABLE;
117 CREATE OR REPLACE FUNCTION evergreen.located_uris (
120 pref_lib INT DEFAULT NULL
121 ) RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank INT) AS $$
122 WITH all_orgs AS (SELECT COALESCE( enabled, FALSE ) AS flag FROM config.global_flag WHERE name = 'opac.located_uri.act_as_copy')
123 SELECT DISTINCT ON (id) * FROM (
124 SELECT acn.id, COALESCE(aou.name,aoud.name), acn.label_sortkey, evergreen.rank_ou(aou.id, $2, $3) AS pref_ou
125 FROM asset.call_number acn
126 INNER JOIN asset.uri_call_number_map auricnm ON acn.id = auricnm.call_number
127 INNER JOIN asset.uri auri ON auri.id = auricnm.uri
128 LEFT JOIN actor.org_unit_ancestors( COALESCE($3, $2) ) aou ON (acn.owning_lib = aou.id)
129 LEFT JOIN actor.org_unit_descendants( COALESCE($3, $2) ) aoud ON (acn.owning_lib = aoud.id),
131 WHERE acn.record = ANY ($1)
132 AND acn.deleted IS FALSE
133 AND auri.active IS TRUE
134 AND ((NOT all_orgs.flag AND aou.id IS NOT NULL) OR COALESCE(aou.id,aoud.id) IS NOT NULL)
136 SELECT acn.id, COALESCE(aou.name,aoud.name) AS name, acn.label_sortkey, evergreen.rank_ou(aou.id, $2, $3) AS pref_ou
137 FROM asset.call_number acn
138 INNER JOIN asset.uri_call_number_map auricnm ON acn.id = auricnm.call_number
139 INNER JOIN asset.uri auri ON auri.id = auricnm.uri
140 LEFT JOIN actor.org_unit_ancestors( $2 ) aou ON (acn.owning_lib = aou.id)
141 LEFT JOIN actor.org_unit_descendants( $2 ) aoud ON (acn.owning_lib = aoud.id),
143 WHERE acn.record = ANY ($1)
144 AND acn.deleted IS FALSE
145 AND auri.active IS TRUE
146 AND ((NOT all_orgs.flag AND aou.id IS NOT NULL) OR COALESCE(aou.id,aoud.id) IS NOT NULL))x
147 ORDER BY id, pref_ou DESC;
151 CREATE OR REPLACE FUNCTION evergreen.located_uris ( bibid BIGINT, ouid INT, pref_lib INT DEFAULT NULL)
152 RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank INT)
153 AS $$ SELECT * FROM evergreen.located_uris(ARRAY[$1],$2,$3) $$ LANGUAGE SQL STABLE;
155 CREATE TABLE unapi.bre_output_layout (
156 name TEXT PRIMARY KEY,
157 transform TEXT REFERENCES config.xml_transform (name) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,
158 mime_type TEXT NOT NULL,
159 feed_top TEXT NOT NULL,
160 holdings_element TEXT,
162 description_element TEXT,
163 creator_element TEXT,
164 update_ts_element TEXT
167 INSERT INTO unapi.bre_output_layout
168 (name, transform, mime_type, holdings_element, feed_top, title_element, description_element, creator_element, update_ts_element)
170 ('holdings_xml', NULL, 'application/xml', NULL, 'hxml', NULL, NULL, NULL, NULL),
171 ('marcxml', 'marcxml', 'application/marc+xml', 'record', 'collection', NULL, NULL, NULL, NULL),
172 ('mods32', 'mods32', 'application/mods+xml', 'mods', 'modsCollection', NULL, NULL, NULL, NULL)
175 -- Dummy functions, so we can create the real ones out of order
176 CREATE OR REPLACE FUNCTION unapi.aou ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
177 CREATE OR REPLACE FUNCTION unapi.acnp ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
178 CREATE OR REPLACE FUNCTION unapi.acns ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
179 CREATE OR REPLACE FUNCTION unapi.acn ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
180 CREATE OR REPLACE FUNCTION unapi.ssub ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
181 CREATE OR REPLACE FUNCTION unapi.sdist ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
182 CREATE OR REPLACE FUNCTION unapi.sstr ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
183 CREATE OR REPLACE FUNCTION unapi.sitem ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
184 CREATE OR REPLACE FUNCTION unapi.sunit ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
185 CREATE OR REPLACE FUNCTION unapi.sisum ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
186 CREATE OR REPLACE FUNCTION unapi.sbsum ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
187 CREATE OR REPLACE FUNCTION unapi.sssum ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
188 CREATE OR REPLACE FUNCTION unapi.siss ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
189 CREATE OR REPLACE FUNCTION unapi.auri ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
190 CREATE OR REPLACE FUNCTION unapi.acp ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
191 CREATE OR REPLACE FUNCTION unapi.acpn ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
192 CREATE OR REPLACE FUNCTION unapi.acl ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
193 CREATE OR REPLACE FUNCTION unapi.ccs ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
194 CREATE OR REPLACE FUNCTION unapi.ascecm ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
195 CREATE OR REPLACE FUNCTION unapi.bre (
201 depth INT DEFAULT NULL,
202 slimit HSTORE DEFAULT NULL,
203 soffset HSTORE DEFAULT NULL,
204 include_xmlns BOOL DEFAULT TRUE,
205 pref_lib INT DEFAULT NULL
207 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
208 CREATE OR REPLACE FUNCTION unapi.mmr (
214 depth INT DEFAULT NULL,
215 slimit HSTORE DEFAULT NULL,
216 soffset HSTORE DEFAULT NULL,
217 include_xmlns BOOL DEFAULT TRUE,
218 pref_lib INT DEFAULT NULL
220 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
221 CREATE OR REPLACE FUNCTION unapi.bmp ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
222 CREATE OR REPLACE FUNCTION unapi.mra ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
223 CREATE OR REPLACE FUNCTION unapi.mmr_mra ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
224 CREATE OR REPLACE FUNCTION unapi.circ ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT DEFAULT '-', depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
226 CREATE OR REPLACE FUNCTION unapi.holdings_xml (
230 depth INT DEFAULT NULL,
231 includes TEXT[] DEFAULT NULL::TEXT[],
232 slimit HSTORE DEFAULT NULL,
233 soffset HSTORE DEFAULT NULL,
234 include_xmlns BOOL DEFAULT TRUE,
235 pref_lib INT DEFAULT NULL
237 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
239 CREATE OR REPLACE FUNCTION unapi.mmr_holdings_xml (
243 depth INT DEFAULT NULL,
244 includes TEXT[] DEFAULT NULL::TEXT[],
245 slimit HSTORE DEFAULT NULL,
246 soffset HSTORE DEFAULT NULL,
247 include_xmlns BOOL DEFAULT TRUE,
248 pref_lib INT DEFAULT NULL
250 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
252 CREATE OR REPLACE FUNCTION unapi.biblio_record_entry_feed ( id_list BIGINT[], format TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE, title TEXT DEFAULT NULL, description TEXT DEFAULT NULL, creator TEXT DEFAULT NULL, update_ts TEXT DEFAULT NULL, unapi_url TEXT DEFAULT NULL, header_xml XML DEFAULT NULL ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
254 CREATE OR REPLACE FUNCTION unapi.memoize (classname TEXT, obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
260 'id' || COALESCE(obj_id::TEXT,'') ||
261 'format' || COALESCE(format::TEXT,'') ||
262 'ename' || COALESCE(ename::TEXT,'') ||
263 'includes' || COALESCE(includes::TEXT,'{}'::TEXT[]::TEXT) ||
264 'org' || COALESCE(org::TEXT,'') ||
265 'depth' || COALESCE(depth::TEXT,'') ||
266 'slimit' || COALESCE(slimit::TEXT,'') ||
267 'soffset' || COALESCE(soffset::TEXT,'') ||
268 'include_xmlns' || COALESCE(include_xmlns::TEXT,'');
269 -- RAISE NOTICE 'memoize key: %', key;
272 -- RAISE NOTICE 'memoize hash: %', key;
274 -- XXX cache logic ... memcached? table?
276 EXECUTE $$SELECT unapi.$$ || classname || $$( $1, $2, $3, $4, $5, $6, $7, $8, $9);$$ INTO output USING obj_id, format, ename, includes, org, depth, slimit, soffset, include_xmlns;
279 $F$ LANGUAGE PLPGSQL STABLE;
281 CREATE OR REPLACE FUNCTION unapi.biblio_record_entry_feed ( id_list BIGINT[], format TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE, title TEXT DEFAULT NULL, description TEXT DEFAULT NULL, creator TEXT DEFAULT NULL, update_ts TEXT DEFAULT NULL, unapi_url TEXT DEFAULT NULL, header_xml XML DEFAULT NULL ) RETURNS XML AS $F$
283 layout unapi.bre_output_layout%ROWTYPE;
284 transform config.xml_transform%ROWTYPE;
287 xmlns_uri TEXT := 'http://open-ils.org/spec/feed-xml/v1';
292 IF org = '-' OR org IS NULL THEN
293 SELECT shortname INTO org FROM evergreen.org_top();
296 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
297 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
299 IF layout.name IS NULL THEN
303 SELECT * INTO transform FROM config.xml_transform WHERE name = layout.transform;
304 xmlns_uri := COALESCE(transform.namespace_uri,xmlns_uri);
306 -- Gather the bib xml
307 SELECT XMLAGG( unapi.bre(i, format, '', includes, org, depth, slimit, soffset, include_xmlns)) INTO tmp_xml FROM UNNEST( id_list ) i;
309 IF layout.title_element IS NOT NULL THEN
310 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.title_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, title;
313 IF layout.description_element IS NOT NULL THEN
314 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.description_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, description;
317 IF layout.creator_element IS NOT NULL THEN
318 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.creator_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, creator;
321 IF layout.update_ts_element IS NOT NULL THEN
322 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.update_ts_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, update_ts;
325 IF unapi_url IS NOT NULL THEN
326 EXECUTE $$SELECT XMLCONCAT( XMLELEMENT( name link, XMLATTRIBUTES( 'http://www.w3.org/1999/xhtml' AS xmlns, 'unapi-server' AS rel, $1 AS href, 'unapi' AS title)), $2)$$ INTO tmp_xml USING unapi_url, tmp_xml::XML;
329 IF header_xml IS NOT NULL THEN tmp_xml := XMLCONCAT(header_xml,tmp_xml::XML); END IF;
331 element_list := regexp_split_to_array(layout.feed_top,E'\\.');
332 FOR i IN REVERSE ARRAY_UPPER(element_list, 1) .. 1 LOOP
333 EXECUTE 'SELECT XMLELEMENT( name '|| quote_ident(element_list[i]) ||', XMLATTRIBUTES( $1 AS xmlns), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML;
338 $F$ LANGUAGE PLPGSQL STABLE;
340 CREATE OR REPLACE FUNCTION unapi.bre (
346 depth INT DEFAULT NULL,
347 slimit HSTORE DEFAULT NULL,
348 soffset HSTORE DEFAULT NULL,
349 include_xmlns BOOL DEFAULT TRUE,
350 pref_lib INT DEFAULT NULL
354 me biblio.record_entry%ROWTYPE;
355 layout unapi.bre_output_layout%ROWTYPE;
356 xfrm config.xml_transform%ROWTYPE;
365 IF org = '-' OR org IS NULL THEN
366 SELECT shortname INTO org FROM evergreen.org_top();
369 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
375 IF format = 'holdings_xml' THEN -- the special case
376 output := unapi.holdings_xml( obj_id, ouid, org, depth, includes, slimit, soffset, include_xmlns);
380 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
382 IF layout.name IS NULL THEN
386 SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform;
388 SELECT * INTO me FROM biblio.record_entry WHERE id = obj_id;
390 -- grab SVF if we need them
391 IF ('mra' = ANY (includes)) THEN
392 axml := unapi.mra(obj_id,NULL,NULL,NULL,NULL);
397 -- grab holdings if we need them
398 IF ('holdings_xml' = ANY (includes)) THEN
399 hxml := unapi.holdings_xml(obj_id, ouid, org, depth, evergreen.array_remove_item_by_value(includes,'holdings_xml'), slimit, soffset, include_xmlns, pref_lib);
405 -- generate our item node
408 IF format = 'marcxml' THEN
410 IF tmp_xml !~ E'<marc:' THEN -- If we're not using the prefixed namespace in this record, then remove all declarations of it
411 tmp_xml := REGEXP_REPLACE(tmp_xml, ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g');
414 tmp_xml := oils_xslt_process(me.marc, xfrm.xslt)::XML;
417 top_el := REGEXP_REPLACE(tmp_xml, E'^.*?<((?:\\S+:)?' || layout.holdings_element || ').*$', E'\\1');
419 IF axml IS NOT NULL THEN
420 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1');
423 IF hxml IS NOT NULL THEN -- XXX how do we configure the holdings position?
424 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1');
427 IF ('bre.unapi' = ANY (includes)) THEN
428 output := REGEXP_REPLACE(
430 '</' || top_el || '>(.*?)',
434 'http://www.w3.org/1999/xhtml' AS xmlns,
436 'tag:open-ils.org:U2@bre/' || obj_id || '/' || org AS title
438 )::TEXT || '</' || top_el || E'>\\1'
444 output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML;
447 $F$ LANGUAGE PLPGSQL STABLE;
449 CREATE OR REPLACE FUNCTION unapi.holdings_xml (
453 depth INT DEFAULT NULL,
454 includes TEXT[] DEFAULT NULL::TEXT[],
455 slimit HSTORE DEFAULT NULL,
456 soffset HSTORE DEFAULT NULL,
457 include_xmlns BOOL DEFAULT TRUE,
458 pref_lib INT DEFAULT NULL
464 CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
465 CASE WHEN ('bre' = ANY ($5)) THEN 'tag:open-ils.org:U2@bre/' || $1 || '/' || $3 ELSE NULL END AS id,
466 (SELECT record_has_holdable_copy FROM asset.record_has_holdable_copy($1)) AS has_holdable
470 (SELECT XMLAGG(XMLELEMENT::XML) FROM (
473 XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
475 FROM asset.opac_ou_record_copy_count($2, $1)
479 XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
481 FROM asset.staff_ou_record_copy_count($2, $1)
485 XMLATTRIBUTES('pref_lib' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
487 FROM asset.opac_ou_record_copy_count($9, $1)
492 WHEN ('bmp' = ANY ($5)) THEN
494 name monograph_parts,
495 (SELECT XMLAGG(bmp) FROM (
496 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)
497 FROM biblio.monograph_part
505 (SELECT XMLAGG(acn ORDER BY rank, name, label_sortkey) FROM (
507 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
508 FROM evergreen.ranked_volumes($1, $2, $4, $6, $7, $9, $5) AS y
511 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), uris.rank, name, label_sortkey
512 FROM evergreen.located_uris($1, $2, $9) AS uris
515 CASE WHEN ('ssub' = ANY ($5)) THEN
518 (SELECT XMLAGG(ssub) FROM (
519 SELECT unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
520 FROM serial.subscription
521 WHERE record_entry = $1
525 CASE WHEN ('acp' = ANY ($5)) THEN
528 (SELECT XMLAGG(acp) FROM (
529 SELECT unapi.acp(p.target_copy,'xml','copy',evergreen.array_remove_item_by_value($5,'acp'), $3, $4, $6, $7, FALSE)
530 FROM biblio.peer_bib_copy_map p
531 JOIN asset.copy c ON (p.target_copy = c.id)
532 WHERE NOT c.deleted AND p.peer_record = $1
533 LIMIT ($6 -> 'acp')::INT
534 OFFSET ($7 -> 'acp')::INT
539 $F$ LANGUAGE SQL STABLE;
541 CREATE OR REPLACE FUNCTION unapi.ssub ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
545 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
546 'tag:open-ils.org:U2@ssub/' || id AS id,
547 'tag:open-ils.org:U2@aou/' || owning_lib AS owning_lib,
548 start_date AS start, end_date AS end, expected_date_offset
551 WHEN ('sdist' = ANY ($4)) THEN
552 XMLELEMENT( name distributions,
553 (SELECT XMLAGG(sdist) FROM (
554 SELECT unapi.sdist( id, 'xml', 'distribution', evergreen.array_remove_item_by_value($4,'ssub'), $5, $6, $7, $8, FALSE)
555 FROM serial.distribution
556 WHERE subscription = ssub.id
562 FROM serial.subscription ssub
564 GROUP BY id, start_date, end_date, expected_date_offset, owning_lib;
565 $F$ LANGUAGE SQL STABLE;
567 CREATE OR REPLACE FUNCTION unapi.sdist ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
571 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
572 'tag:open-ils.org:U2@sdist/' || id AS id,
573 'tag:open-ils.org:U2@acn/' || receive_call_number AS receive_call_number,
574 'tag:open-ils.org:U2@acn/' || bind_call_number AS bind_call_number,
575 unit_label_prefix, label, unit_label_suffix, summary_method
577 unapi.aou( holding_lib, $2, 'holding_lib', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8),
578 CASE WHEN subscription IS NOT NULL AND ('ssub' = ANY ($4)) THEN unapi.ssub( subscription, 'xml', 'subscription', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE) ELSE NULL END,
580 WHEN ('sstr' = ANY ($4)) THEN
581 XMLELEMENT( name streams,
582 (SELECT XMLAGG(sstr) FROM (
583 SELECT unapi.sstr( id, 'xml', 'stream', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE)
585 WHERE distribution = sdist.id
590 XMLELEMENT( name summaries,
592 WHEN ('sbsum' = ANY ($4)) THEN
593 (SELECT XMLAGG(sbsum) FROM (
594 SELECT unapi.sbsum( id, 'xml', 'serial_summary', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE)
595 FROM serial.basic_summary
596 WHERE distribution = sdist.id
601 WHEN ('sisum' = ANY ($4)) THEN
602 (SELECT XMLAGG(sisum) FROM (
603 SELECT unapi.sisum( id, 'xml', 'serial_summary', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE)
604 FROM serial.index_summary
605 WHERE distribution = sdist.id
610 WHEN ('sssum' = ANY ($4)) THEN
611 (SELECT XMLAGG(sssum) FROM (
612 SELECT unapi.sssum( id, 'xml', 'serial_summary', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE)
613 FROM serial.supplement_summary
614 WHERE distribution = sdist.id
620 FROM serial.distribution sdist
622 GROUP BY id, label, unit_label_prefix, unit_label_suffix, holding_lib, summary_method, subscription, receive_call_number, bind_call_number;
623 $F$ LANGUAGE SQL STABLE;
625 CREATE OR REPLACE FUNCTION unapi.sstr ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
629 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
630 'tag:open-ils.org:U2@sstr/' || id AS id,
633 CASE WHEN distribution IS NOT NULL AND ('sdist' = ANY ($4)) THEN unapi.sssum( distribution, 'xml', 'distribtion', evergreen.array_remove_item_by_value($4,'sstr'), $5, $6, $7, $8, FALSE) ELSE NULL END,
635 WHEN ('sitem' = ANY ($4)) THEN
636 XMLELEMENT( name items,
637 (SELECT XMLAGG(sitem) FROM (
638 SELECT unapi.sitem( id, 'xml', 'serial_item', evergreen.array_remove_item_by_value($4,'sstr'), $5, $6, $7, $8, FALSE)
640 WHERE stream = sstr.id
646 FROM serial.stream sstr
648 GROUP BY id, routing_label, distribution;
649 $F$ LANGUAGE SQL STABLE;
651 CREATE OR REPLACE FUNCTION unapi.siss ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
655 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
656 'tag:open-ils.org:U2@siss/' || id AS id,
657 create_date, edit_date, label, date_published,
658 holding_code, holding_type, holding_link_id
660 CASE WHEN subscription IS NOT NULL AND ('ssub' = ANY ($4)) THEN unapi.ssub( subscription, 'xml', 'subscription', evergreen.array_remove_item_by_value($4,'siss'), $5, $6, $7, $8, FALSE) ELSE NULL END,
662 WHEN ('sitem' = ANY ($4)) THEN
663 XMLELEMENT( name items,
664 (SELECT XMLAGG(sitem) FROM (
665 SELECT unapi.sitem( id, 'xml', 'serial_item', evergreen.array_remove_item_by_value($4,'siss'), $5, $6, $7, $8, FALSE)
667 WHERE issuance = sstr.id
673 FROM serial.issuance sstr
675 GROUP BY id, create_date, edit_date, label, date_published, holding_code, holding_type, holding_link_id, subscription;
676 $F$ LANGUAGE SQL STABLE;
678 CREATE OR REPLACE FUNCTION unapi.sitem ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
682 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
683 'tag:open-ils.org:U2@sitem/' || id AS id,
684 'tag:open-ils.org:U2@siss/' || issuance AS issuance,
685 date_expected, date_received
687 CASE WHEN issuance IS NOT NULL AND ('siss' = ANY ($4)) THEN unapi.siss( issuance, $2, 'issuance', evergreen.array_remove_item_by_value($4,'sitem'), $5, $6, $7, $8, FALSE) ELSE NULL END,
688 CASE WHEN stream IS NOT NULL AND ('sstr' = ANY ($4)) THEN unapi.sstr( stream, $2, 'stream', evergreen.array_remove_item_by_value($4,'sitem'), $5, $6, $7, $8, FALSE) ELSE NULL END,
689 CASE WHEN unit IS NOT NULL AND ('sunit' = ANY ($4)) THEN unapi.sunit( unit, $2, 'serial_unit', evergreen.array_remove_item_by_value($4,'sitem'), $5, $6, $7, $8, FALSE) ELSE NULL END,
690 CASE WHEN uri IS NOT NULL AND ('auri' = ANY ($4)) THEN unapi.auri( uri, $2, 'uri', evergreen.array_remove_item_by_value($4,'sitem'), $5, $6, $7, $8, FALSE) ELSE NULL END
691 -- XMLELEMENT( name notes,
693 -- WHEN ('acpn' = ANY ($4)) THEN
694 -- (SELECT XMLAGG(acpn) FROM (
695 -- SELECT unapi.acpn( id, 'xml', 'copy_note', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8)
696 -- FROM asset.copy_note
697 -- WHERE owning_copy = cp.id AND pub
703 FROM serial.item sitem
705 $F$ LANGUAGE SQL STABLE;
708 CREATE OR REPLACE FUNCTION unapi.sssum ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
712 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
713 'tag:open-ils.org:U2@sbsum/' || id AS id,
714 'sssum' AS type, generated_coverage, textual_holdings, show_generated
716 CASE WHEN ('sdist' = ANY ($4)) THEN unapi.sdist( distribution, 'xml', 'distribtion', evergreen.array_remove_item_by_value($4,'ssum'), $5, $6, $7, $8, FALSE) ELSE NULL END
718 FROM serial.supplement_summary ssum
720 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
721 $F$ LANGUAGE SQL STABLE;
723 CREATE OR REPLACE FUNCTION unapi.sbsum ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
727 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
728 'tag:open-ils.org:U2@sbsum/' || id AS id,
729 'sbsum' AS type, generated_coverage, textual_holdings, show_generated
731 CASE WHEN ('sdist' = ANY ($4)) THEN unapi.sdist( distribution, 'xml', 'distribtion', evergreen.array_remove_item_by_value($4,'ssum'), $5, $6, $7, $8, FALSE) ELSE NULL END
733 FROM serial.basic_summary ssum
735 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
736 $F$ LANGUAGE SQL STABLE;
738 CREATE OR REPLACE FUNCTION unapi.sisum ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
742 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
743 'tag:open-ils.org:U2@sbsum/' || id AS id,
744 'sisum' AS type, generated_coverage, textual_holdings, show_generated
746 CASE WHEN ('sdist' = ANY ($4)) THEN unapi.sdist( distribution, 'xml', 'distribtion', evergreen.array_remove_item_by_value($4,'ssum'), $5, $6, $7, $8, FALSE) ELSE NULL END
748 FROM serial.index_summary ssum
750 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
751 $F$ LANGUAGE SQL STABLE;
754 CREATE OR REPLACE FUNCTION unapi.aou ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
758 IF ename = 'circlib' THEN
762 'http://open-ils.org/spec/actors/v1' AS xmlns,
767 FROM actor.org_unit aou
770 EXECUTE $$SELECT XMLELEMENT(
771 name $$ || ename || $$,
773 'http://open-ils.org/spec/actors/v1' AS xmlns,
774 'tag:open-ils.org:U2@aou/' || id AS id,
775 shortname, name, opac_visible
778 FROM actor.org_unit aou
779 WHERE id = $1 $$ INTO output USING obj_id;
785 $F$ LANGUAGE PLPGSQL STABLE;
787 CREATE OR REPLACE FUNCTION unapi.acl ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
791 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
795 label_prefix AS prefix,
796 label_suffix AS suffix
800 FROM asset.copy_location
802 $F$ LANGUAGE SQL STABLE;
804 CREATE OR REPLACE FUNCTION unapi.ccs ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
808 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
815 FROM config.copy_status
817 $F$ LANGUAGE SQL STABLE;
819 CREATE OR REPLACE FUNCTION unapi.acpn ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
823 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
831 $F$ LANGUAGE SQL STABLE;
833 CREATE OR REPLACE FUNCTION unapi.ascecm ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
837 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
843 FROM asset.stat_cat_entry asce
844 JOIN asset.stat_cat sc ON (sc.id = asce.stat_cat)
846 $F$ LANGUAGE SQL STABLE;
848 CREATE OR REPLACE FUNCTION unapi.bmp ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
852 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
853 'tag:open-ils.org:U2@bmp/' || id AS id,
857 'tag:open-ils.org:U2@bre/' || record AS record
860 WHEN ('acp' = ANY ($4)) THEN
861 XMLELEMENT( name copies,
862 (SELECT XMLAGG(acp) FROM (
863 SELECT unapi.acp( cp.id, 'xml', 'copy', evergreen.array_remove_item_by_value($4,'bmp'), $5, $6, $7, $8, FALSE)
865 JOIN asset.copy_part_map cpm ON (cpm.target_copy = cp.id)
867 AND cp.deleted IS FALSE
868 ORDER BY COALESCE(cp.copy_number,0), cp.barcode
869 LIMIT ($7 -> 'acp')::INT
870 OFFSET ($8 -> 'acp')::INT
876 CASE WHEN ('bre' = ANY ($4)) THEN unapi.bre( record, 'marcxml', 'record', evergreen.array_remove_item_by_value($4,'bmp'), $5, $6, $7, $8, FALSE) ELSE NULL END
878 FROM biblio.monograph_part
880 GROUP BY id, label, label_sortkey, record;
881 $F$ LANGUAGE SQL STABLE;
883 CREATE OR REPLACE FUNCTION unapi.acp ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
887 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
888 'tag:open-ils.org:U2@acp/' || id AS id, id AS copy_id,
889 create_date, edit_date, copy_number, circulate, deposit,
890 ref, holdable, deleted, deposit_amount, price, barcode,
891 circ_modifier, circ_as_type, opac_visible, age_protect
893 unapi.ccs( status, $2, 'status', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE),
894 unapi.acl( location, $2, 'location', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE),
895 unapi.aou( circ_lib, $2, 'circ_lib', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8),
896 unapi.aou( circ_lib, $2, 'circlib', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8),
897 CASE WHEN ('acn' = ANY ($4)) THEN unapi.acn( call_number, $2, 'call_number', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE) ELSE NULL END,
899 WHEN ('acpn' = ANY ($4)) THEN
900 XMLELEMENT( name copy_notes,
901 (SELECT XMLAGG(acpn) FROM (
902 SELECT unapi.acpn( id, 'xml', 'copy_note', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
904 WHERE owning_copy = cp.id AND pub
910 WHEN ('ascecm' = ANY ($4)) THEN
911 XMLELEMENT( name statcats,
912 (SELECT XMLAGG(ascecm) FROM (
913 SELECT unapi.ascecm( stat_cat_entry, 'xml', 'statcat', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
914 FROM asset.stat_cat_entry_copy_map
915 WHERE owning_copy = cp.id
921 WHEN ('bre' = ANY ($4)) THEN
922 XMLELEMENT( name foreign_records,
923 (SELECT XMLAGG(bre) FROM (
924 SELECT unapi.bre(peer_record,'marcxml','record','{}'::TEXT[], $5, $6, $7, $8, FALSE)
925 FROM biblio.peer_bib_copy_map
926 WHERE target_copy = cp.id
933 WHEN ('bmp' = ANY ($4)) THEN
934 XMLELEMENT( name monograph_parts,
935 (SELECT XMLAGG(bmp) FROM (
936 SELECT unapi.bmp( part, 'xml', 'monograph_part', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
937 FROM asset.copy_part_map
938 WHERE target_copy = cp.id
944 WHEN ('circ' = ANY ($4)) THEN
945 XMLELEMENT( name current_circulation,
946 (SELECT XMLAGG(circ) FROM (
947 SELECT unapi.circ( id, 'xml', 'circ', evergreen.array_remove_item_by_value($4,'circ'), $5, $6, $7, $8, FALSE)
948 FROM action.circulation
949 WHERE target_copy = cp.id
950 AND checkin_time IS NULL
958 AND cp.deleted IS FALSE
959 GROUP BY id, status, location, circ_lib, call_number, create_date,
960 edit_date, copy_number, circulate, deposit, ref, holdable,
961 deleted, deposit_amount, price, barcode, circ_modifier,
962 circ_as_type, opac_visible, age_protect;
963 $F$ LANGUAGE SQL STABLE;
965 CREATE OR REPLACE FUNCTION unapi.sunit ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
969 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
970 'tag:open-ils.org:U2@acp/' || id AS id, id AS copy_id,
971 create_date, edit_date, copy_number, circulate, deposit,
972 ref, holdable, deleted, deposit_amount, price, barcode,
973 circ_modifier, circ_as_type, opac_visible, age_protect,
974 status_changed_time, floating, mint_condition,
975 detailed_contents, sort_key, summary_contents, cost
977 unapi.ccs( status, $2, 'status', evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($4,'acp'),'sunit'), $5, $6, $7, $8, FALSE),
978 unapi.acl( location, $2, 'location', evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($4,'acp'),'sunit'), $5, $6, $7, $8, FALSE),
979 unapi.aou( circ_lib, $2, 'circ_lib', evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($4,'acp'),'sunit'), $5, $6, $7, $8),
980 unapi.aou( circ_lib, $2, 'circlib', evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($4,'acp'),'sunit'), $5, $6, $7, $8),
981 CASE WHEN ('acn' = ANY ($4)) THEN unapi.acn( call_number, $2, 'call_number', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE) ELSE NULL END,
982 XMLELEMENT( name copy_notes,
984 WHEN ('acpn' = ANY ($4)) THEN
985 (SELECT XMLAGG(acpn) FROM (
986 SELECT unapi.acpn( id, 'xml', 'copy_note', evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($4,'acp'),'sunit'), $5, $6, $7, $8, FALSE)
988 WHERE owning_copy = cp.id AND pub
993 XMLELEMENT( name statcats,
995 WHEN ('ascecm' = ANY ($4)) THEN
996 (SELECT XMLAGG(ascecm) FROM (
997 SELECT unapi.ascecm( stat_cat_entry, 'xml', 'statcat', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
998 FROM asset.stat_cat_entry_copy_map
999 WHERE owning_copy = cp.id
1004 XMLELEMENT( name foreign_records,
1006 WHEN ('bre' = ANY ($4)) THEN
1007 (SELECT XMLAGG(bre) FROM (
1008 SELECT unapi.bre(peer_record,'marcxml','record','{}'::TEXT[], $5, $6, $7, $8, FALSE)
1009 FROM biblio.peer_bib_copy_map
1010 WHERE target_copy = cp.id
1016 WHEN ('bmp' = ANY ($4)) THEN
1017 XMLELEMENT( name monograph_parts,
1018 (SELECT XMLAGG(bmp) FROM (
1019 SELECT unapi.bmp( part, 'xml', 'monograph_part', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
1020 FROM asset.copy_part_map
1021 WHERE target_copy = cp.id
1027 WHEN ('circ' = ANY ($4)) THEN
1028 XMLELEMENT( name current_circulation,
1029 (SELECT XMLAGG(circ) FROM (
1030 SELECT unapi.circ( id, 'xml', 'circ', evergreen.array_remove_item_by_value($4,'circ'), $5, $6, $7, $8, FALSE)
1031 FROM action.circulation
1032 WHERE target_copy = cp.id
1033 AND checkin_time IS NULL
1041 AND cp.deleted IS FALSE
1042 GROUP BY id, status, location, circ_lib, call_number, create_date,
1043 edit_date, copy_number, circulate, floating, mint_condition,
1044 deposit, ref, holdable, deleted, deposit_amount, price,
1045 barcode, circ_modifier, circ_as_type, opac_visible,
1046 status_changed_time, detailed_contents, sort_key,
1047 summary_contents, cost, age_protect;
1048 $F$ LANGUAGE SQL STABLE;
1050 CREATE OR REPLACE FUNCTION unapi.acn ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
1054 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1055 'tag:open-ils.org:U2@acn/' || acn.id AS id,
1056 acn.id AS vol_id, o.shortname AS lib,
1057 o.opac_visible AS opac_visible,
1058 deleted, label, label_sortkey, label_class, record
1060 unapi.aou( owning_lib, $2, 'owning_lib', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8),
1062 WHEN ('acp' = ANY ($4)) THEN
1063 CASE WHEN $6 IS NOT NULL THEN
1064 XMLELEMENT( name copies,
1065 (SELECT XMLAGG(acp ORDER BY rank_avail) FROM (
1066 SELECT unapi.acp( cp.id, 'xml', 'copy', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE),
1067 evergreen.rank_cp_status(cp.status) AS rank_avail
1069 JOIN actor.org_unit_descendants( (SELECT id FROM actor.org_unit WHERE shortname = $5), $6) aoud ON (cp.circ_lib = aoud.id)
1070 WHERE cp.call_number = acn.id
1071 AND cp.deleted IS FALSE
1072 ORDER BY rank_avail, COALESCE(cp.copy_number,0), cp.barcode
1073 LIMIT ($7 -> 'acp')::INT
1074 OFFSET ($8 -> 'acp')::INT
1078 XMLELEMENT( name copies,
1079 (SELECT XMLAGG(acp ORDER BY rank_avail) FROM (
1080 SELECT unapi.acp( cp.id, 'xml', 'copy', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE),
1081 evergreen.rank_cp_status(cp.status) AS rank_avail
1083 JOIN actor.org_unit_descendants( (SELECT id FROM actor.org_unit WHERE shortname = $5) ) aoud ON (cp.circ_lib = aoud.id)
1084 WHERE cp.call_number = acn.id
1085 AND cp.deleted IS FALSE
1086 ORDER BY rank_avail, COALESCE(cp.copy_number,0), cp.barcode
1087 LIMIT ($7 -> 'acp')::INT
1088 OFFSET ($8 -> 'acp')::INT
1096 (SELECT XMLAGG(auri) FROM (SELECT unapi.auri(uri,'xml','uri', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE) FROM asset.uri_call_number_map WHERE call_number = acn.id)x)
1098 unapi.acnp( acn.prefix, 'marcxml', 'prefix', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE),
1099 unapi.acns( acn.suffix, 'marcxml', 'suffix', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE),
1100 CASE WHEN ('bre' = ANY ($4)) THEN unapi.bre( acn.record, 'marcxml', 'record', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE) ELSE NULL END
1102 FROM asset.call_number acn
1103 JOIN actor.org_unit o ON (o.id = acn.owning_lib)
1105 AND acn.deleted IS FALSE
1106 GROUP BY acn.id, o.shortname, o.opac_visible, deleted, label, label_sortkey, label_class, owning_lib, record, acn.prefix, acn.suffix;
1107 $F$ LANGUAGE SQL STABLE;
1109 CREATE OR REPLACE FUNCTION unapi.acnp ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
1111 name call_number_prefix,
1113 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1116 'tag:open-ils.org:U2@aou/' || owning_lib AS owning_lib,
1120 FROM asset.call_number_prefix
1122 $F$ LANGUAGE SQL STABLE;
1124 CREATE OR REPLACE FUNCTION unapi.acns ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
1126 name call_number_suffix,
1128 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1131 'tag:open-ils.org:U2@aou/' || owning_lib AS owning_lib,
1135 FROM asset.call_number_suffix
1137 $F$ LANGUAGE SQL STABLE;
1139 CREATE OR REPLACE FUNCTION unapi.auri ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
1143 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1144 'tag:open-ils.org:U2@auri/' || uri.id AS id,
1150 WHEN ('acn' = ANY ($4)) THEN
1151 XMLELEMENT( name copies,
1152 (SELECT XMLAGG(acn) FROM (SELECT unapi.acn( call_number, 'xml', 'copy', evergreen.array_remove_item_by_value($4,'auri'), $5, $6, $7, $8, FALSE) FROM asset.uri_call_number_map WHERE uri = uri.id)x)
1159 GROUP BY uri.id, use_restriction, href, label;
1160 $F$ LANGUAGE SQL STABLE;
1162 CREATE OR REPLACE FUNCTION unapi.mra (
1168 depth INT DEFAULT NULL,
1169 slimit HSTORE DEFAULT NULL,
1170 soffset HSTORE DEFAULT NULL,
1171 include_xmlns BOOL DEFAULT TRUE
1172 ) RETURNS XML AS $F$
1176 CASE WHEN $9 THEN 'http://open-ils.org/spec/indexing/v1' ELSE NULL END AS xmlns,
1177 'tag:open-ils.org:U2@mra/' || $1 AS id,
1178 'tag:open-ils.org:U2@bre/' || $1 AS record
1180 (SELECT XMLAGG(foo.y)
1186 cvm.value AS "coded-value",
1195 FROM metabib.record_attr_flat mra
1196 JOIN config.record_attr_definition rad ON (mra.attr = rad.name)
1197 LEFT JOIN config.coded_value_map cvm ON (cvm.ctype = mra.attr AND code = mra.value)
1202 $F$ LANGUAGE SQL STABLE;
1204 CREATE OR REPLACE FUNCTION unapi.circ (obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT DEFAULT '-', depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
1208 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1209 'tag:open-ils.org:U2@circ/' || id AS id,
1213 CASE WHEN ('aou' = ANY ($4)) THEN unapi.aou( circ_lib, $2, 'circ_lib', evergreen.array_remove_item_by_value($4,'circ'), $5, $6, $7, $8, FALSE) ELSE NULL END,
1214 CASE WHEN ('acp' = ANY ($4)) THEN unapi.acp( circ_lib, $2, 'target_copy', evergreen.array_remove_item_by_value($4,'circ'), $5, $6, $7, $8, FALSE) ELSE NULL END
1216 FROM action.circulation
1218 $F$ LANGUAGE SQL STABLE;
1220 CREATE OR REPLACE FUNCTION unapi.mmr_mra (
1226 depth INT DEFAULT NULL,
1227 slimit HSTORE DEFAULT NULL,
1228 soffset HSTORE DEFAULT NULL,
1229 include_xmlns BOOL DEFAULT TRUE
1230 ) RETURNS XML AS $F$
1234 CASE WHEN $9 THEN 'http://open-ils.org/spec/indexing/v1' ELSE NULL END AS xmlns,
1235 'tag:open-ils.org:U2@mmr/' || $1 AS metarecord
1237 (SELECT XMLAGG(foo.y)
1239 SELECT DISTINCT ON (COALESCE(cvm.id,uvm.id))
1240 COALESCE(cvm.id,uvm.id),
1245 cvm.value AS "coded-value",
1254 FROM metabib.record_attr_flat mra
1255 JOIN config.record_attr_definition rad ON (mra.attr = rad.name)
1256 LEFT JOIN config.coded_value_map cvm ON (cvm.ctype = mra.attr AND code = mra.value)
1257 LEFT JOIN metabib.uncontrolled_record_attr_value uvm ON (uvm.attr = mra.attr AND uvm.value = mra.value)
1258 WHERE mra.id IN (SELECT source FROM metabib.metarecord_source_map WHERE metarecord = $1)
1263 $F$ LANGUAGE SQL STABLE;
1265 CREATE OR REPLACE FUNCTION unapi.mmr_holdings_xml (
1269 depth INT DEFAULT NULL,
1270 includes TEXT[] DEFAULT NULL::TEXT[],
1271 slimit HSTORE DEFAULT NULL,
1272 soffset HSTORE DEFAULT NULL,
1273 include_xmlns BOOL DEFAULT TRUE,
1274 pref_lib INT DEFAULT NULL
1280 CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1281 CASE WHEN ('mmr' = ANY ($5)) THEN 'tag:open-ils.org:U2@mmr/' || $1 || '/' || $3 ELSE NULL END AS id,
1282 (SELECT metarecord_has_holdable_copy FROM asset.metarecord_has_holdable_copy($1)) AS has_holdable
1286 (SELECT XMLAGG(XMLELEMENT::XML) FROM (
1289 XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
1291 FROM asset.opac_ou_metarecord_copy_count($2, $1)
1295 XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
1297 FROM asset.staff_ou_metarecord_copy_count($2, $1)
1301 XMLATTRIBUTES('pref_lib' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
1303 FROM asset.opac_ou_metarecord_copy_count($9, $1)
1307 -- XXX monograph_parts and foreign_copies are skipped in MRs ... put them back some day?
1310 (SELECT XMLAGG(acn ORDER BY rank, name, label_sortkey) FROM (
1312 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
1313 FROM evergreen.ranked_volumes((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $4, $6, $7, $9, $5) AS y
1316 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), uris.rank, name, label_sortkey
1317 FROM evergreen.located_uris((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $9) AS uris
1320 CASE WHEN ('ssub' = ANY ($5)) THEN
1323 (SELECT XMLAGG(ssub) FROM (
1324 SELECT unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
1325 FROM serial.subscription
1326 WHERE record_entry IN (SELECT source FROM metabib.metarecord_source_map WHERE metarecord = $1)
1331 $F$ LANGUAGE SQL STABLE;
1333 CREATE OR REPLACE FUNCTION unapi.mmr (
1339 depth INT DEFAULT NULL,
1340 slimit HSTORE DEFAULT NULL,
1341 soffset HSTORE DEFAULT NULL,
1342 include_xmlns BOOL DEFAULT TRUE,
1343 pref_lib INT DEFAULT NULL
1347 mmrec metabib.metarecord%ROWTYPE;
1348 leadrec biblio.record_entry%ROWTYPE;
1349 subrec biblio.record_entry%ROWTYPE;
1350 layout unapi.bre_output_layout%ROWTYPE;
1351 xfrm config.xml_transform%ROWTYPE;
1353 xml_buf TEXT; -- growing XML document
1354 tmp_xml TEXT; -- single-use XML string
1355 xml_frag TEXT; -- single-use XML fragment
1360 subxml XML; -- subordinate records elements
1365 -- xpath for extracting bre.marc values from subordinate records
1366 -- so they may be appended to the MARC of the master record prior
1367 -- to XSLT processing.
1368 -- subjects, isbn, issn, upc -- anything else?
1370 '//*[starts-with(@tag, "6") or @tag="020" or @tag="022" or @tag="024"]';
1372 IF org = '-' OR org IS NULL THEN
1373 SELECT shortname INTO org FROM evergreen.org_top();
1376 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
1378 IF ouid IS NULL THEN
1382 SELECT INTO mmrec * FROM metabib.metarecord WHERE id = obj_id;
1387 -- TODO: aggregate holdings from constituent records
1388 IF format = 'holdings_xml' THEN -- the special case
1389 output := unapi.mmr_holdings_xml(
1390 obj_id, ouid, org, depth,
1391 evergreen.array_remove_item_by_value(includes,'holdings_xml'),
1392 slimit, soffset, include_xmlns, pref_lib);
1396 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
1398 IF layout.name IS NULL THEN
1402 SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform;
1404 SELECT INTO leadrec * FROM biblio.record_entry WHERE id = mmrec.master_record;
1406 -- Grab distinct MVF for all records if requested
1407 IF ('mra' = ANY (includes)) THEN
1408 axml := unapi.mmr_mra(obj_id,NULL,NULL,NULL,NULL,NULL,NULL,NULL,TRUE);
1413 xml_buf = leadrec.marc;
1416 IF ('holdings_xml' = ANY (includes)) THEN
1417 hxml := unapi.mmr_holdings_xml(
1418 obj_id, ouid, org, depth,
1419 evergreen.array_remove_item_by_value(includes,'holdings_xml'),
1420 slimit, soffset, include_xmlns, pref_lib);
1423 subxml := NULL::XML;
1424 parts := '{}'::TEXT[];
1425 FOR subrec IN SELECT bre.* FROM biblio.record_entry bre
1426 JOIN metabib.metarecord_source_map mmsm ON (mmsm.source = bre.id)
1427 JOIN metabib.metarecord mmr ON (mmr.id = mmsm.metarecord)
1428 WHERE mmr.id = obj_id
1429 ORDER BY CASE WHEN bre.id = mmr.master_record THEN 0 ELSE bre.id END
1430 LIMIT COALESCE((slimit->'bre')::INT, 5) LOOP
1432 IF subrec.id = leadrec.id THEN CONTINUE; END IF;
1433 -- Append choice data from the the non-lead records to the
1434 -- the lead record document
1436 parts := parts || xpath(sub_xpath, subrec.marc::XML)::TEXT[];
1439 SELECT ARRAY_TO_STRING( ARRAY_AGG( DISTINCT p ), '' )::XML INTO subxml FROM UNNEST(parts) p;
1441 -- append data from the subordinate records to the
1442 -- main record document before applying the XSLT
1444 IF subxml IS NOT NULL THEN
1445 xml_buf := REGEXP_REPLACE(xml_buf,
1446 '</record>(.*?)$', subxml || '</record>' || E'\\1');
1449 IF format = 'marcxml' THEN
1450 -- If we're not using the prefixed namespace in
1451 -- this record, then remove all declarations of it
1452 IF xml_buf !~ E'<marc:' THEN
1453 xml_buf := REGEXP_REPLACE(xml_buf,
1454 ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g');
1457 xml_buf := oils_xslt_process(xml_buf, xfrm.xslt)::XML;
1460 -- update top_el to reflect the change in xml_buf, which may
1461 -- now be a different type of document (e.g. record -> mods)
1462 top_el := REGEXP_REPLACE(xml_buf, E'^.*?<((?:\\S+:)?' ||
1463 layout.holdings_element || ').*$', E'\\1');
1465 IF axml IS NOT NULL THEN
1466 xml_buf := REGEXP_REPLACE(xml_buf,
1467 '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1');
1470 IF hxml IS NOT NULL THEN
1471 xml_buf := REGEXP_REPLACE(xml_buf,
1472 '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1');
1475 IF ('mmr.unapi' = ANY (includes)) THEN
1476 output := REGEXP_REPLACE(
1478 '</' || top_el || '>(.*?)',
1482 'http://www.w3.org/1999/xhtml' AS xmlns,
1483 'unapi-id' AS class,
1484 'tag:open-ils.org:U2@mmr/' || obj_id || '/' || org AS title
1486 )::TEXT || '</' || top_el || E'>\\1'
1492 -- remove ignorable whitesace
1493 output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML;
1496 $F$ LANGUAGE PLPGSQL STABLE;
1501 -- Some test queries
1503 SELECT unapi.memoize( 'bre', 1,'mods32','','{holdings_xml,acp}'::TEXT[], 'SYS1');
1504 SELECT unapi.memoize( 'bre', 1,'marcxml','','{holdings_xml,acp}'::TEXT[], 'SYS1');
1505 SELECT unapi.memoize( 'bre', 1,'holdings_xml','','{holdings_xml,acp}'::TEXT[], 'SYS1');
1507 SELECT unapi.biblio_record_entry_feed('{1}'::BIGINT[],'mods32','{holdings_xml,acp}'::TEXT[],'SYS1',NULL,'acn=>1',NULL, NULL,NULL,NULL,NULL,'http://c64/opac/extras/unapi', '<totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/">2</totalResults><startIndex xmlns="http://a9.com/-/spec/opensearch/1.1/">1</startIndex><itemsPerPage xmlns="http://a9.com/-/spec/opensearch/1.1/">10</itemsPerPage>');
1509 SELECT unapi.biblio_record_entry_feed('{7209,7394}'::BIGINT[],'marcxml','{}'::TEXT[],'SYS1',NULL,'acn=>1',NULL, NULL,NULL,NULL,NULL,'http://fulfillment2.esilibrary.com/opac/extras/unapi', '<totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/">2</totalResults><startIndex xmlns="http://a9.com/-/spec/opensearch/1.1/">1</startIndex><itemsPerPage xmlns="http://a9.com/-/spec/opensearch/1.1/">10</itemsPerPage>');
1510 EXPLAIN ANALYZE SELECT unapi.biblio_record_entry_feed('{7209,7394}'::BIGINT[],'marcxml','{}'::TEXT[],'SYS1',NULL,'acn=>1',NULL, NULL,NULL,NULL,NULL,'http://fulfillment2.esilibrary.com/opac/extras/unapi', '<totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/">2</totalResults><startIndex xmlns="http://a9.com/-/spec/opensearch/1.1/">1</startIndex><itemsPerPage xmlns="http://a9.com/-/spec/opensearch/1.1/">10</itemsPerPage>');
1511 EXPLAIN ANALYZE SELECT unapi.biblio_record_entry_feed('{7209,7394}'::BIGINT[],'marcxml','{holdings_xml}'::TEXT[],'SYS1',NULL,'acn=>1',NULL, NULL,NULL,NULL,NULL,'http://fulfillment2.esilibrary.com/opac/extras/unapi', '<totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/">2</totalResults><startIndex xmlns="http://a9.com/-/spec/opensearch/1.1/">1</startIndex><itemsPerPage xmlns="http://a9.com/-/spec/opensearch/1.1/">10</itemsPerPage>');
1512 EXPLAIN ANALYZE SELECT unapi.biblio_record_entry_feed('{7209,7394}'::BIGINT[],'mods32','{holdings_xml}'::TEXT[],'SYS1',NULL,'acn=>1',NULL, NULL,NULL,NULL,NULL,'http://fulfillment2.esilibrary.com/opac/extras/unapi', '<totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/">2</totalResults><startIndex xmlns="http://a9.com/-/spec/opensearch/1.1/">1</startIndex><itemsPerPage xmlns="http://a9.com/-/spec/opensearch/1.1/">10</itemsPerPage>');
1514 SELECT unapi.biblio_record_entry_feed('{216}'::BIGINT[],'marcxml','{}'::TEXT[], 'BR1');
1515 EXPLAIN ANALYZE SELECT unapi.bre(216,'marcxml','record','{holdings_xml,bre.unapi}'::TEXT[], 'BR1');
1516 EXPLAIN ANALYZE SELECT unapi.bre(216,'holdings_xml','record','{}'::TEXT[], 'BR1');
1517 EXPLAIN ANALYZE SELECT unapi.holdings_xml(216,4,'BR1',2,'{bre}'::TEXT[]);
1518 EXPLAIN ANALYZE SELECT unapi.bre(216,'mods32','record','{}'::TEXT[], 'BR1');
1520 -- Limit to 5 call numbers, 5 copies, with a preferred library of 4 (BR1), in SYS2 at a depth of 0
1521 EXPLAIN ANALYZE SELECT unapi.bre(36,'marcxml','record','{holdings_xml,mra,acp,acnp,acns,bmp}','SYS2',0,'acn=>5,acp=>5',NULL,TRUE,4);