1 DROP SCHEMA IF EXISTS unapi CASCADE;
6 CREATE OR REPLACE FUNCTION evergreen.org_top()
7 RETURNS actor.org_unit AS $$
8 SELECT * FROM actor.org_unit WHERE parent_ou IS NULL LIMIT 1;
9 $$ LANGUAGE SQL STABLE;
11 CREATE OR REPLACE FUNCTION evergreen.array_remove_item_by_value(inp ANYARRAY, el ANYELEMENT)
12 RETURNS anyarray AS $$
13 SELECT ARRAY_AGG(x.e) FROM UNNEST( $1 ) x(e) WHERE x.e <> $2;
14 $$ LANGUAGE SQL STABLE;
16 CREATE OR REPLACE FUNCTION evergreen.rank_ou(lib INT, search_lib INT, pref_lib INT DEFAULT NULL)
20 -- lib matches search_lib
21 (SELECT CASE WHEN $1 = $2 THEN -20000 END),
23 -- lib matches pref_lib
24 (SELECT CASE WHEN $1 = $3 THEN -10000 END),
27 -- pref_lib is a child of search_lib and lib is a child of pref lib.
28 -- For example, searching CONS, pref lib is SYS1,
29 -- copies at BR1 and BR2 sort to the front.
30 (SELECT distance - 5000
31 FROM actor.org_unit_descendants_distance($3)
32 WHERE id = $1 AND $3 IN (
33 SELECT id FROM actor.org_unit_descendants($2))),
35 -- lib is a child of search_lib
36 (SELECT distance FROM actor.org_unit_descendants_distance($2) WHERE id = $1),
38 -- all others pay cash
41 $$ LANGUAGE SQL STABLE;
43 -- geolocation-aware variant
44 CREATE OR REPLACE FUNCTION evergreen.rank_ou(lib INT, search_lib INT, pref_lib INT, plat FLOAT, plon FLOAT)
48 -- lib matches search_lib
49 (SELECT CASE WHEN $1 = $2 THEN -20000 END),
51 -- lib matches pref_lib
52 (SELECT CASE WHEN $1 = $3 THEN -10000 END),
55 -- pref_lib is a child of search_lib and lib is a child of pref lib.
56 -- For example, searching CONS, pref lib is SYS1,
57 -- copies at BR1 and BR2 sort to the front.
58 (SELECT distance - 5000
59 FROM actor.org_unit_descendants_distance($3)
60 WHERE id = $1 AND $3 IN (
61 SELECT id FROM actor.org_unit_descendants($2))),
63 -- lib is a child of search_lib
64 (SELECT distance FROM actor.org_unit_descendants_distance($2) WHERE id = $1),
66 -- all others pay cash
68 ) + ((SELECT CASE WHEN addr.latitude IS NULL THEN 0 ELSE -20038 END) + (earth_distance( -- shortest GC distance is returned, only half the circumfrence is needed
70 COALESCE(addr.latitude,plat), -- if the org has no coords, we just
71 COALESCE(addr.longitude,plon) -- force 0 distance and let the above tie-break
72 ),ll_to_earth(plat,plon)
73 ) / 1000)::INT ) -- earth_distance is in meters, convert to kilometers and subtract from largest distance
74 FROM actor.org_unit org
75 LEFT JOIN actor.org_address addr ON (org.billing_address = addr.id)
77 $$ LANGUAGE SQL STABLE;
79 -- this version exists mainly to accommodate JSON query transform limitations
80 -- (the transform argument must be an IDL field, not an entire row/object)
81 -- XXX is there another way?
82 CREATE OR REPLACE FUNCTION evergreen.rank_cp(copy_id BIGINT)
85 copy asset.copy%ROWTYPE;
87 SELECT * INTO copy FROM asset.copy WHERE id = copy_id;
88 RETURN evergreen.rank_cp(copy);
90 $$ LANGUAGE PLPGSQL STABLE;
92 CREATE OR REPLACE FUNCTION evergreen.rank_cp(copy asset.copy)
97 WITH totally_available AS (
98 SELECT id, 0 AS avail_rank
99 FROM config.copy_status
100 WHERE opac_visible IS TRUE
101 AND copy_active IS TRUE
102 AND id != 1 -- "Checked out"
103 ), almost_available AS (
104 SELECT id, 10 AS avail_rank
105 FROM config.copy_status
106 WHERE holdable IS TRUE
107 AND opac_visible IS TRUE
108 AND copy_active IS FALSE
109 OR id = 1 -- "Checked out"
112 CASE WHEN NOT copy.opac_visible THEN 100 END,
113 (SELECT avail_rank FROM totally_available WHERE copy.status IN (id)),
114 CASE WHEN copy.holdable THEN
115 (SELECT avail_rank FROM almost_available WHERE copy.status IN (id))
122 $$ LANGUAGE PLPGSQL STABLE;
124 CREATE OR REPLACE FUNCTION evergreen.ranked_volumes(
127 depth INT DEFAULT NULL,
128 slimit HSTORE DEFAULT NULL,
129 soffset HSTORE DEFAULT NULL,
130 pref_lib INT DEFAULT NULL,
131 includes TEXT[] DEFAULT NULL::TEXT[]
132 ) RETURNS TABLE(id BIGINT, name TEXT, label_sortkey TEXT, rank BIGINT) AS $$
133 WITH RECURSIVE ou_depth AS (
138 FROM actor.org_unit_type aout
139 INNER JOIN actor.org_unit ou ON ou_type = aout.id
143 ), descendant_depth AS (
147 FROM actor.org_unit ou
148 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
149 JOIN anscestor_depth ad ON (ad.id = ou.id),
151 WHERE ad.depth = ou_depth.depth
156 FROM actor.org_unit ou
157 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
158 JOIN descendant_depth ot ON (ot.id = ou.parent_ou)
159 ), anscestor_depth AS (
163 FROM actor.org_unit ou
164 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
170 FROM actor.org_unit ou
171 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
172 JOIN anscestor_depth ot ON (ot.parent_ou = ou.id)
174 SELECT ou.* FROM actor.org_unit ou JOIN descendant_depth USING (id)
177 SELECT ua.id, ua.name, ua.label_sortkey, MIN(ua.rank) AS rank FROM (
178 SELECT acn.id, owning_lib.name, acn.label_sortkey,
179 evergreen.rank_cp(acp),
181 FROM asset.call_number acn
182 JOIN asset.copy acp ON (acn.id = acp.call_number)
183 JOIN descendants AS aou ON (acp.circ_lib = aou.id)
184 JOIN actor.org_unit AS owning_lib ON (acn.owning_lib = owning_lib.id)
185 WHERE acn.record = ANY ($1)
186 AND acn.deleted IS FALSE
187 AND acp.deleted IS FALSE
188 AND CASE WHEN ('exclude_invisible_acn' = ANY($7)) THEN
190 WITH basevm AS (SELECT c_attrs FROM asset.patron_default_visibility_mask()),
191 circvm AS (SELECT search.calculate_visibility_attribute_test('circ_lib', ARRAY[acp.circ_lib]) AS mask)
193 FROM basevm, circvm, asset.copy_vis_attr_cache acvac
194 WHERE acvac.vis_attr_vector @@ (basevm.c_attrs || '&' || circvm.mask)::query_int
195 AND acvac.target_copy = acp.id
196 AND acvac.record = acn.record
198 GROUP BY acn.id, evergreen.rank_cp(acp), owning_lib.name, acn.label_sortkey, aou.id
202 CASE WHEN aou.id = $2 THEN -20000 END,
203 CASE WHEN aou.id = $6 THEN -10000 END,
204 (SELECT distance - 5000
205 FROM actor.org_unit_descendants_distance($6) as x
206 WHERE x.id = aou.id AND $6 IN (
207 SELECT q.id FROM actor.org_unit_descendants($2) as q)),
208 (SELECT e.distance FROM actor.org_unit_descendants_distance($2) as e WHERE e.id = aou.id),
211 evergreen.rank_cp(acp)
214 GROUP BY ua.id, ua.name, ua.label_sortkey
215 ORDER BY rank, ua.name, ua.label_sortkey
216 LIMIT ($4 -> 'acn')::INT
217 OFFSET ($5 -> 'acn')::INT;
218 $$ LANGUAGE SQL STABLE ROWS 10;
220 CREATE OR REPLACE FUNCTION evergreen.ranked_volumes
221 ( 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[] )
222 RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank BIGINT)
223 AS $$ SELECT * FROM evergreen.ranked_volumes(ARRAY[$1],$2,$3,$4,$5,$6,$7) $$ LANGUAGE SQL STABLE;
226 CREATE OR REPLACE FUNCTION evergreen.located_uris (
229 pref_lib INT DEFAULT NULL
230 ) RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank INT) AS $$
231 WITH all_orgs AS (SELECT COALESCE( enabled, FALSE ) AS flag FROM config.global_flag WHERE name = 'opac.located_uri.act_as_copy')
232 SELECT DISTINCT ON (id) * FROM (
233 SELECT acn.id, COALESCE(aou.name,aoud.name), acn.label_sortkey, evergreen.rank_ou(aou.id, $2, $3) AS pref_ou
234 FROM asset.call_number acn
235 INNER JOIN asset.uri_call_number_map auricnm ON acn.id = auricnm.call_number
236 INNER JOIN asset.uri auri ON auri.id = auricnm.uri
237 LEFT JOIN actor.org_unit_ancestors( COALESCE($3, $2) ) aou ON (acn.owning_lib = aou.id)
238 LEFT JOIN actor.org_unit_descendants( COALESCE($3, $2) ) aoud ON (acn.owning_lib = aoud.id),
240 WHERE acn.record = ANY ($1)
241 AND acn.deleted IS FALSE
242 AND auri.active IS TRUE
243 AND ((NOT all_orgs.flag AND aou.id IS NOT NULL) OR (all_orgs.flag AND COALESCE(aou.id,aoud.id) IS NOT NULL))
245 SELECT acn.id, COALESCE(aou.name,aoud.name) AS name, acn.label_sortkey, evergreen.rank_ou(aou.id, $2, $3) AS pref_ou
246 FROM asset.call_number acn
247 INNER JOIN asset.uri_call_number_map auricnm ON acn.id = auricnm.call_number
248 INNER JOIN asset.uri auri ON auri.id = auricnm.uri
249 LEFT JOIN actor.org_unit_ancestors( $2 ) aou ON (acn.owning_lib = aou.id)
250 LEFT JOIN actor.org_unit_descendants( $2 ) aoud ON (acn.owning_lib = aoud.id),
252 WHERE acn.record = ANY ($1)
253 AND acn.deleted IS FALSE
254 AND auri.active IS TRUE
255 AND ((NOT all_orgs.flag AND aou.id IS NOT NULL) OR (all_orgs.flag AND COALESCE(aou.id,aoud.id) IS NOT NULL)))x
256 ORDER BY id, pref_ou DESC;
260 CREATE OR REPLACE FUNCTION evergreen.located_uris ( bibid BIGINT, ouid INT, pref_lib INT DEFAULT NULL)
261 RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank INT)
262 AS $$ SELECT * FROM evergreen.located_uris(ARRAY[$1],$2,$3) $$ LANGUAGE SQL STABLE;
264 CREATE TABLE unapi.bre_output_layout (
265 name TEXT PRIMARY KEY,
266 transform TEXT REFERENCES config.xml_transform (name) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,
267 mime_type TEXT NOT NULL,
268 feed_top TEXT NOT NULL,
269 holdings_element TEXT,
271 description_element TEXT,
272 creator_element TEXT,
273 update_ts_element TEXT
276 INSERT INTO unapi.bre_output_layout
277 (name, transform, mime_type, holdings_element, feed_top, title_element, description_element, creator_element, update_ts_element)
279 ('holdings_xml', NULL, 'application/xml', NULL, 'hxml', NULL, NULL, NULL, NULL),
280 ('marcxml', 'marcxml', 'application/marc+xml', 'record', 'collection', NULL, NULL, NULL, NULL),
281 ('mods32', 'mods32', 'application/mods+xml', 'mods', 'modsCollection', NULL, NULL, NULL, NULL)
284 -- Dummy functions, so we can create the real ones out of order
285 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;
286 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;
287 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;
288 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;
289 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;
290 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;
291 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;
292 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;
293 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;
294 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;
295 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;
296 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;
297 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;
298 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;
299 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;
300 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;
301 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;
302 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;
303 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;
304 CREATE OR REPLACE FUNCTION unapi.bre (
310 depth INT DEFAULT NULL,
311 slimit HSTORE DEFAULT NULL,
312 soffset HSTORE DEFAULT NULL,
313 include_xmlns BOOL DEFAULT TRUE,
314 pref_lib INT DEFAULT NULL
316 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
317 CREATE OR REPLACE FUNCTION unapi.mmr (
323 depth INT DEFAULT NULL,
324 slimit HSTORE DEFAULT NULL,
325 soffset HSTORE DEFAULT NULL,
326 include_xmlns BOOL DEFAULT TRUE,
327 pref_lib INT DEFAULT NULL
329 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
330 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;
331 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;
332 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, pref_lib INT DEFAULT NULL) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
333 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;
335 CREATE OR REPLACE FUNCTION unapi.holdings_xml (
339 depth INT DEFAULT NULL,
340 includes TEXT[] DEFAULT NULL::TEXT[],
341 slimit HSTORE DEFAULT NULL,
342 soffset HSTORE DEFAULT NULL,
343 include_xmlns BOOL DEFAULT TRUE,
344 pref_lib INT DEFAULT NULL
346 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
348 CREATE OR REPLACE FUNCTION unapi.mmr_holdings_xml (
352 depth INT DEFAULT NULL,
353 includes TEXT[] DEFAULT NULL::TEXT[],
354 slimit HSTORE DEFAULT NULL,
355 soffset HSTORE DEFAULT NULL,
356 include_xmlns BOOL DEFAULT TRUE,
357 pref_lib INT DEFAULT NULL
359 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
361 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, pref_lib INT DEFAULT NULL ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
363 CREATE OR REPLACE FUNCTION unapi.metabib_virtual_record_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, pref_lib INT DEFAULT NULL ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
365 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$
371 'id' || COALESCE(obj_id::TEXT,'') ||
372 'format' || COALESCE(format::TEXT,'') ||
373 'ename' || COALESCE(ename::TEXT,'') ||
374 'includes' || COALESCE(includes::TEXT,'{}'::TEXT[]::TEXT) ||
375 'org' || COALESCE(org::TEXT,'') ||
376 'depth' || COALESCE(depth::TEXT,'') ||
377 'slimit' || COALESCE(slimit::TEXT,'') ||
378 'soffset' || COALESCE(soffset::TEXT,'') ||
379 'include_xmlns' || COALESCE(include_xmlns::TEXT,'');
380 -- RAISE NOTICE 'memoize key: %', key;
383 -- RAISE NOTICE 'memoize hash: %', key;
385 -- XXX cache logic ... memcached? table?
387 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;
390 $F$ LANGUAGE PLPGSQL STABLE;
392 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, pref_lib INT DEFAULT NULL ) RETURNS XML AS $F$
394 layout unapi.bre_output_layout%ROWTYPE;
395 transform config.xml_transform%ROWTYPE;
398 xmlns_uri TEXT := 'http://open-ils.org/spec/feed-xml/v1';
403 IF org = '-' OR org IS NULL THEN
404 SELECT shortname INTO org FROM evergreen.org_top();
407 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
408 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
410 IF layout.name IS NULL THEN
414 SELECT * INTO transform FROM config.xml_transform WHERE name = layout.transform;
415 xmlns_uri := COALESCE(transform.namespace_uri,xmlns_uri);
417 -- Gather the bib xml
418 SELECT XMLAGG( unapi.bre(i, format, '', includes, org, depth, slimit, soffset, include_xmlns, pref_lib)) INTO tmp_xml FROM UNNEST( id_list ) i;
420 IF layout.title_element IS NOT NULL THEN
421 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.title_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, title;
424 IF layout.description_element IS NOT NULL THEN
425 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.description_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, description;
428 IF layout.creator_element IS NOT NULL THEN
429 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.creator_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, creator;
432 IF layout.update_ts_element IS NOT NULL THEN
433 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;
436 IF unapi_url IS NOT NULL THEN
437 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;
440 IF header_xml IS NOT NULL THEN tmp_xml := XMLCONCAT(header_xml,tmp_xml::XML); END IF;
442 element_list := regexp_split_to_array(layout.feed_top,E'\\.');
443 FOR i IN REVERSE ARRAY_UPPER(element_list, 1) .. 1 LOOP
444 EXECUTE 'SELECT XMLELEMENT( name '|| quote_ident(element_list[i]) ||', XMLATTRIBUTES( $1 AS xmlns), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML;
449 $F$ LANGUAGE PLPGSQL STABLE;
451 CREATE OR REPLACE FUNCTION unapi.metabib_virtual_record_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, pref_lib INT DEFAULT NULL ) RETURNS XML AS $F$
453 layout unapi.bre_output_layout%ROWTYPE;
454 transform config.xml_transform%ROWTYPE;
457 xmlns_uri TEXT := 'http://open-ils.org/spec/feed-xml/v1';
462 IF org = '-' OR org IS NULL THEN
463 SELECT shortname INTO org FROM evergreen.org_top();
466 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
467 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
469 IF layout.name IS NULL THEN
473 SELECT * INTO transform FROM config.xml_transform WHERE name = layout.transform;
474 xmlns_uri := COALESCE(transform.namespace_uri,xmlns_uri);
476 -- Gather the bib xml
477 SELECT XMLAGG( unapi.mmr(i, format, '', includes, org, depth, slimit, soffset, include_xmlns, pref_lib)) INTO tmp_xml FROM UNNEST( id_list ) i;
479 IF layout.title_element IS NOT NULL THEN
480 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.title_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, title;
483 IF layout.description_element IS NOT NULL THEN
484 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.description_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, description;
487 IF layout.creator_element IS NOT NULL THEN
488 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.creator_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, creator;
491 IF layout.update_ts_element IS NOT NULL THEN
492 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;
495 IF unapi_url IS NOT NULL THEN
496 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;
499 IF header_xml IS NOT NULL THEN tmp_xml := XMLCONCAT(header_xml,tmp_xml::XML); END IF;
501 element_list := regexp_split_to_array(layout.feed_top,E'\\.');
502 FOR i IN REVERSE ARRAY_UPPER(element_list, 1) .. 1 LOOP
503 EXECUTE 'SELECT XMLELEMENT( name '|| quote_ident(element_list[i]) ||', XMLATTRIBUTES( $1 AS xmlns), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML;
508 $F$ LANGUAGE PLPGSQL STABLE;
510 CREATE OR REPLACE FUNCTION unapi.bre (
516 depth INT DEFAULT NULL,
517 slimit HSTORE DEFAULT NULL,
518 soffset HSTORE DEFAULT NULL,
519 include_xmlns BOOL DEFAULT TRUE,
520 pref_lib INT DEFAULT NULL
524 me biblio.record_entry%ROWTYPE;
525 layout unapi.bre_output_layout%ROWTYPE;
526 xfrm config.xml_transform%ROWTYPE;
536 IF org = '-' OR org IS NULL THEN
537 SELECT shortname INTO org FROM evergreen.org_top();
540 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
546 IF format = 'holdings_xml' THEN -- the special case
547 output := unapi.holdings_xml( obj_id, ouid, org, depth, includes, slimit, soffset, include_xmlns);
551 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
553 IF layout.name IS NULL THEN
557 SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform;
559 SELECT * INTO me FROM biblio.record_entry WHERE id = obj_id;
561 -- grab bib_source, if any
562 IF ('cbs' = ANY (includes) AND me.source IS NOT NULL) THEN
563 source := unapi.cbs(me.source,NULL,NULL,NULL,NULL);
568 -- grab SVF if we need them
569 IF ('mra' = ANY (includes)) THEN
570 axml := unapi.mra(obj_id,NULL,NULL,NULL,NULL);
575 -- grab holdings if we need them
576 IF ('holdings_xml' = ANY (includes)) THEN
577 hxml := unapi.holdings_xml(obj_id, ouid, org, depth, evergreen.array_remove_item_by_value(includes,'holdings_xml'), slimit, soffset, include_xmlns, pref_lib);
583 -- generate our item node
586 IF format = 'marcxml' THEN
588 IF tmp_xml !~ E'<marc:' THEN -- If we're not using the prefixed namespace in this record, then remove all declarations of it
589 tmp_xml := REGEXP_REPLACE(tmp_xml, ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g');
592 tmp_xml := oils_xslt_process(me.marc, xfrm.xslt)::XML;
595 top_el := REGEXP_REPLACE(tmp_xml, E'^.*?<((?:\\S+:)?' || layout.holdings_element || ').*$', E'\\1');
597 IF source IS NOT NULL THEN
598 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', source || '</' || top_el || E'>\\1');
601 IF axml IS NOT NULL THEN
602 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1');
605 IF hxml IS NOT NULL THEN -- XXX how do we configure the holdings position?
606 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1');
609 IF ('bre.unapi' = ANY (includes)) THEN
610 output := REGEXP_REPLACE(
612 '</' || top_el || '>(.*?)',
616 'http://www.w3.org/1999/xhtml' AS xmlns,
618 'tag:open-ils.org:U2@bre/' || obj_id || '/' || org AS title
620 )::TEXT || '</' || top_el || E'>\\1'
626 IF ('bre.extern' = ANY (includes)) THEN
627 output := REGEXP_REPLACE(
629 '</' || top_el || '>(.*?)',
633 'http://open-ils.org/spec/biblio/v1' AS xmlns,
634 me.creator AS creator,
636 me.create_date AS create_date,
637 me.edit_date AS edit_date,
638 me.quality AS quality,
639 me.fingerprint AS fingerprint,
640 me.tcn_source AS tcn_source,
641 me.tcn_value AS tcn_value,
643 me.share_depth AS share_depth,
645 me.deleted AS deleted
647 )::TEXT || '</' || top_el || E'>\\1'
653 output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML;
656 $F$ LANGUAGE PLPGSQL STABLE;
658 CREATE OR REPLACE FUNCTION unapi.holdings_xml (
662 depth INT DEFAULT NULL,
663 includes TEXT[] DEFAULT NULL::TEXT[],
664 slimit HSTORE DEFAULT NULL,
665 soffset HSTORE DEFAULT NULL,
666 include_xmlns BOOL DEFAULT TRUE,
667 pref_lib INT DEFAULT NULL
673 CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
674 CASE WHEN ('bre' = ANY ($5)) THEN 'tag:open-ils.org:U2@bre/' || $1 || '/' || $3 ELSE NULL END AS id,
675 (SELECT record_has_holdable_copy FROM asset.record_has_holdable_copy($1)) AS has_holdable
679 (SELECT XMLAGG(XMLELEMENT::XML) FROM (
682 XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
684 FROM asset.opac_ou_record_copy_count($2, $1)
688 XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
690 FROM asset.staff_ou_record_copy_count($2, $1)
694 XMLATTRIBUTES('pref_lib' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
696 FROM asset.opac_ou_record_copy_count($9, $1)
701 WHEN ('bmp' = ANY ($5)) THEN
703 name monograph_parts,
704 (SELECT XMLAGG(bmp) FROM (
705 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)
706 FROM biblio.monograph_part
707 WHERE NOT deleted AND record = $1
714 (SELECT XMLAGG(acn ORDER BY rank, name, label_sortkey) FROM (
716 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
717 FROM evergreen.ranked_volumes($1, $2, $4, $6, $7, $9, $5) AS y
720 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
721 FROM evergreen.located_uris($1, $2, $9) AS uris
724 CASE WHEN ('ssub' = ANY ($5)) THEN
727 (SELECT XMLAGG(ssub) FROM (
728 SELECT unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
729 FROM serial.subscription
730 WHERE record_entry = $1
734 CASE WHEN ('acp' = ANY ($5)) THEN
737 (SELECT XMLAGG(acp) FROM (
738 SELECT unapi.acp(p.target_copy,'xml','copy',evergreen.array_remove_item_by_value($5,'acp'), $3, $4, $6, $7, FALSE)
739 FROM biblio.peer_bib_copy_map p
740 JOIN asset.copy c ON (p.target_copy = c.id)
741 WHERE NOT c.deleted AND p.peer_record = $1
742 LIMIT ($6 -> 'acp')::INT
743 OFFSET ($7 -> 'acp')::INT
748 $F$ LANGUAGE SQL STABLE;
750 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$
754 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
755 'tag:open-ils.org:U2@ssub/' || id AS id,
756 'tag:open-ils.org:U2@aou/' || owning_lib AS owning_lib,
757 start_date AS start, end_date AS end, expected_date_offset
760 WHEN ('sdist' = ANY ($4)) THEN
761 XMLELEMENT( name distributions,
762 (SELECT XMLAGG(sdist) FROM (
763 SELECT unapi.sdist( id, 'xml', 'distribution', evergreen.array_remove_item_by_value($4,'ssub'), $5, $6, $7, $8, FALSE)
764 FROM serial.distribution
765 WHERE subscription = ssub.id
771 FROM serial.subscription ssub
773 GROUP BY id, start_date, end_date, expected_date_offset, owning_lib;
774 $F$ LANGUAGE SQL STABLE;
776 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$
780 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
781 'tag:open-ils.org:U2@sdist/' || id AS id,
782 'tag:open-ils.org:U2@acn/' || receive_call_number AS receive_call_number,
783 'tag:open-ils.org:U2@acn/' || bind_call_number AS bind_call_number,
784 unit_label_prefix, label, unit_label_suffix, summary_method
786 unapi.aou( holding_lib, $2, 'holding_lib', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8),
787 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,
789 WHEN ('sstr' = ANY ($4)) THEN
790 XMLELEMENT( name streams,
791 (SELECT XMLAGG(sstr) FROM (
792 SELECT unapi.sstr( id, 'xml', 'stream', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE)
794 WHERE distribution = sdist.id
799 XMLELEMENT( name summaries,
801 WHEN ('sbsum' = ANY ($4)) THEN
802 (SELECT XMLAGG(sbsum) FROM (
803 SELECT unapi.sbsum( id, 'xml', 'serial_summary', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE)
804 FROM serial.basic_summary
805 WHERE distribution = sdist.id
810 WHEN ('sisum' = ANY ($4)) THEN
811 (SELECT XMLAGG(sisum) FROM (
812 SELECT unapi.sisum( id, 'xml', 'serial_summary', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE)
813 FROM serial.index_summary
814 WHERE distribution = sdist.id
819 WHEN ('sssum' = ANY ($4)) THEN
820 (SELECT XMLAGG(sssum) FROM (
821 SELECT unapi.sssum( id, 'xml', 'serial_summary', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE)
822 FROM serial.supplement_summary
823 WHERE distribution = sdist.id
829 FROM serial.distribution sdist
831 GROUP BY id, label, unit_label_prefix, unit_label_suffix, holding_lib, summary_method, subscription, receive_call_number, bind_call_number;
832 $F$ LANGUAGE SQL STABLE;
834 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$
838 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
839 'tag:open-ils.org:U2@sstr/' || id AS id,
842 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,
844 WHEN ('sitem' = ANY ($4)) THEN
845 XMLELEMENT( name items,
846 (SELECT XMLAGG(sitem) FROM (
847 SELECT unapi.sitem( id, 'xml', 'serial_item', evergreen.array_remove_item_by_value($4,'sstr'), $5, $6, $7, $8, FALSE)
849 WHERE stream = sstr.id
855 FROM serial.stream sstr
857 GROUP BY id, routing_label, distribution;
858 $F$ LANGUAGE SQL STABLE;
860 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$
864 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
865 'tag:open-ils.org:U2@siss/' || id AS id,
866 create_date, edit_date, label, date_published,
867 holding_code, holding_type, holding_link_id
869 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,
871 WHEN ('sitem' = ANY ($4)) THEN
872 XMLELEMENT( name items,
873 (SELECT XMLAGG(sitem) FROM (
874 SELECT unapi.sitem( id, 'xml', 'serial_item', evergreen.array_remove_item_by_value($4,'siss'), $5, $6, $7, $8, FALSE)
876 WHERE issuance = sstr.id
882 FROM serial.issuance sstr
884 GROUP BY id, create_date, edit_date, label, date_published, holding_code, holding_type, holding_link_id, subscription;
885 $F$ LANGUAGE SQL STABLE;
887 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$
891 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
892 'tag:open-ils.org:U2@sitem/' || id AS id,
893 'tag:open-ils.org:U2@siss/' || issuance AS issuance,
894 date_expected, date_received
896 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,
897 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,
898 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,
899 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
900 -- XMLELEMENT( name notes,
902 -- WHEN ('acpn' = ANY ($4)) THEN
903 -- (SELECT XMLAGG(acpn) FROM (
904 -- SELECT unapi.acpn( id, 'xml', 'copy_note', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8)
905 -- FROM asset.copy_note
906 -- WHERE owning_copy = cp.id AND pub
912 FROM serial.item sitem
914 $F$ LANGUAGE SQL STABLE;
917 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$
921 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
922 'tag:open-ils.org:U2@sbsum/' || id AS id,
923 'sssum' AS type, generated_coverage, textual_holdings, show_generated
925 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
927 FROM serial.supplement_summary ssum
929 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
930 $F$ LANGUAGE SQL STABLE;
932 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$
936 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
937 'tag:open-ils.org:U2@sbsum/' || id AS id,
938 'sbsum' AS type, generated_coverage, textual_holdings, show_generated
940 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
942 FROM serial.basic_summary ssum
944 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
945 $F$ LANGUAGE SQL STABLE;
947 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$
951 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
952 'tag:open-ils.org:U2@sbsum/' || id AS id,
953 'sisum' AS type, generated_coverage, textual_holdings, show_generated
955 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
957 FROM serial.index_summary ssum
959 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
960 $F$ LANGUAGE SQL STABLE;
963 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$
967 IF ename = 'circlib' THEN
971 'http://open-ils.org/spec/actors/v1' AS xmlns,
976 FROM actor.org_unit aou
979 EXECUTE $$SELECT XMLELEMENT(
980 name $$ || ename || $$,
982 'http://open-ils.org/spec/actors/v1' AS xmlns,
983 'tag:open-ils.org:U2@aou/' || id AS id,
984 shortname, name, opac_visible
987 FROM actor.org_unit aou
988 WHERE id = $1 $$ INTO output USING obj_id;
994 $F$ LANGUAGE PLPGSQL STABLE;
996 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$
1000 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1004 label_prefix AS prefix,
1005 label_suffix AS suffix
1009 FROM asset.copy_location
1011 $F$ LANGUAGE SQL STABLE;
1013 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$
1017 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1024 FROM config.copy_status
1026 $F$ LANGUAGE SQL STABLE;
1028 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$
1032 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1033 create_date AS date,
1038 FROM asset.copy_note
1040 $F$ LANGUAGE SQL STABLE;
1042 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$
1046 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1052 FROM asset.stat_cat_entry asce
1053 JOIN asset.stat_cat sc ON (sc.id = asce.stat_cat)
1055 $F$ LANGUAGE SQL STABLE;
1057 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$
1059 name monograph_part,
1061 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1062 'tag:open-ils.org:U2@bmp/' || id AS id,
1066 'tag:open-ils.org:U2@bre/' || record AS record
1069 WHEN ('acp' = ANY ($4)) THEN
1070 XMLELEMENT( name copies,
1071 (SELECT XMLAGG(acp) FROM (
1072 SELECT unapi.acp( cp.id, 'xml', 'copy', evergreen.array_remove_item_by_value($4,'bmp'), $5, $6, $7, $8, FALSE)
1074 JOIN asset.copy_part_map cpm ON (cpm.target_copy = cp.id)
1076 AND cp.deleted IS FALSE
1077 ORDER BY COALESCE(cp.copy_number,0), cp.barcode
1078 LIMIT ($7 -> 'acp')::INT
1079 OFFSET ($8 -> 'acp')::INT
1085 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
1087 FROM biblio.monograph_part
1088 WHERE NOT deleted AND id = $1
1089 GROUP BY id, label, label_sortkey, record;
1090 $F$ LANGUAGE SQL STABLE;
1092 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$
1096 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1097 'tag:open-ils.org:U2@acp/' || id AS id, id AS copy_id,
1098 create_date, edit_date, copy_number, circulate, deposit,
1099 ref, holdable, deleted, deposit_amount, price, barcode,
1100 circ_modifier, circ_as_type, opac_visible, age_protect
1102 unapi.ccs( status, $2, 'status', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE),
1103 unapi.acl( location, $2, 'location', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE),
1104 unapi.aou( circ_lib, $2, 'circ_lib', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8),
1105 unapi.aou( circ_lib, $2, 'circlib', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8),
1106 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,
1108 WHEN ('acpn' = ANY ($4)) THEN
1109 XMLELEMENT( name copy_notes,
1110 (SELECT XMLAGG(acpn) FROM (
1111 SELECT unapi.acpn( id, 'xml', 'copy_note', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
1112 FROM asset.copy_note
1113 WHERE owning_copy = cp.id AND pub
1119 WHEN ('ascecm' = ANY ($4)) THEN
1120 XMLELEMENT( name statcats,
1121 (SELECT XMLAGG(ascecm) FROM (
1122 SELECT unapi.ascecm( stat_cat_entry, 'xml', 'statcat', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
1123 FROM asset.stat_cat_entry_copy_map
1124 WHERE owning_copy = cp.id
1130 WHEN ('bre' = ANY ($4)) THEN
1131 XMLELEMENT( name foreign_records,
1132 (SELECT XMLAGG(bre) FROM (
1133 SELECT unapi.bre(peer_record,'marcxml','record','{}'::TEXT[], $5, $6, $7, $8, FALSE)
1134 FROM biblio.peer_bib_copy_map
1135 WHERE target_copy = cp.id
1142 WHEN ('bmp' = ANY ($4)) THEN
1143 XMLELEMENT( name monograph_parts,
1144 (SELECT XMLAGG(bmp) FROM (
1145 SELECT unapi.bmp( part, 'xml', 'monograph_part', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
1146 FROM asset.copy_part_map
1147 WHERE target_copy = cp.id
1153 WHEN ('circ' = ANY ($4)) THEN
1154 XMLELEMENT( name current_circulation,
1155 (SELECT XMLAGG(circ) FROM (
1156 SELECT unapi.circ( id, 'xml', 'circ', evergreen.array_remove_item_by_value($4,'circ'), $5, $6, $7, $8, FALSE)
1157 FROM action.circulation
1158 WHERE target_copy = cp.id
1159 AND checkin_time IS NULL
1167 AND cp.deleted IS FALSE
1168 GROUP BY id, status, location, circ_lib, call_number, create_date,
1169 edit_date, copy_number, circulate, deposit, ref, holdable,
1170 deleted, deposit_amount, price, barcode, circ_modifier,
1171 circ_as_type, opac_visible, age_protect;
1172 $F$ LANGUAGE SQL STABLE;
1174 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$
1178 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1179 'tag:open-ils.org:U2@acp/' || id AS id, id AS copy_id,
1180 create_date, edit_date, copy_number, circulate, deposit,
1181 ref, holdable, deleted, deposit_amount, price, barcode,
1182 circ_modifier, circ_as_type, opac_visible, age_protect,
1183 status_changed_time, floating, mint_condition,
1184 detailed_contents, sort_key, summary_contents, cost
1186 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),
1187 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),
1188 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),
1189 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),
1190 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,
1191 XMLELEMENT( name copy_notes,
1193 WHEN ('acpn' = ANY ($4)) THEN
1194 (SELECT XMLAGG(acpn) FROM (
1195 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)
1196 FROM asset.copy_note
1197 WHERE owning_copy = cp.id AND pub
1202 XMLELEMENT( name statcats,
1204 WHEN ('ascecm' = ANY ($4)) THEN
1205 (SELECT XMLAGG(ascecm) FROM (
1206 SELECT unapi.ascecm( stat_cat_entry, 'xml', 'statcat', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
1207 FROM asset.stat_cat_entry_copy_map
1208 WHERE owning_copy = cp.id
1213 XMLELEMENT( name foreign_records,
1215 WHEN ('bre' = ANY ($4)) THEN
1216 (SELECT XMLAGG(bre) FROM (
1217 SELECT unapi.bre(peer_record,'marcxml','record','{}'::TEXT[], $5, $6, $7, $8, FALSE)
1218 FROM biblio.peer_bib_copy_map
1219 WHERE target_copy = cp.id
1225 WHEN ('bmp' = ANY ($4)) THEN
1226 XMLELEMENT( name monograph_parts,
1227 (SELECT XMLAGG(bmp) FROM (
1228 SELECT unapi.bmp( part, 'xml', 'monograph_part', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
1229 FROM asset.copy_part_map
1230 WHERE target_copy = cp.id
1236 WHEN ('circ' = ANY ($4)) THEN
1237 XMLELEMENT( name current_circulation,
1238 (SELECT XMLAGG(circ) FROM (
1239 SELECT unapi.circ( id, 'xml', 'circ', evergreen.array_remove_item_by_value($4,'circ'), $5, $6, $7, $8, FALSE)
1240 FROM action.circulation
1241 WHERE target_copy = cp.id
1242 AND checkin_time IS NULL
1250 AND cp.deleted IS FALSE
1251 GROUP BY id, status, location, circ_lib, call_number, create_date,
1252 edit_date, copy_number, circulate, floating, mint_condition,
1253 deposit, ref, holdable, deleted, deposit_amount, price,
1254 barcode, circ_modifier, circ_as_type, opac_visible,
1255 status_changed_time, detailed_contents, sort_key,
1256 summary_contents, cost, age_protect;
1257 $F$ LANGUAGE SQL STABLE;
1259 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$
1263 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1264 'tag:open-ils.org:U2@acn/' || acn.id AS id,
1265 acn.id AS vol_id, o.shortname AS lib,
1266 o.opac_visible AS opac_visible,
1267 deleted, label, label_sortkey, label_class, record
1269 unapi.aou( owning_lib, $2, 'owning_lib', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8),
1271 WHEN ('acp' = ANY ($4)) THEN
1272 CASE WHEN $6 IS NOT NULL THEN
1273 XMLELEMENT( name copies,
1274 (SELECT XMLAGG(acp ORDER BY rank_avail) FROM (
1275 SELECT unapi.acp( cp.id, 'xml', 'copy', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE),
1276 evergreen.rank_cp(cp) AS rank_avail
1278 JOIN actor.org_unit_descendants( (SELECT id FROM actor.org_unit WHERE shortname = $5), $6) aoud ON (cp.circ_lib = aoud.id)
1279 WHERE cp.call_number = acn.id
1280 AND cp.deleted IS FALSE
1281 ORDER BY rank_avail, COALESCE(cp.copy_number,0), cp.barcode
1282 LIMIT ($7 -> 'acp')::INT
1283 OFFSET ($8 -> 'acp')::INT
1287 XMLELEMENT( name copies,
1288 (SELECT XMLAGG(acp ORDER BY rank_avail) FROM (
1289 SELECT unapi.acp( cp.id, 'xml', 'copy', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE),
1290 evergreen.rank_cp(cp) AS rank_avail
1292 JOIN actor.org_unit_descendants( (SELECT id FROM actor.org_unit WHERE shortname = $5) ) aoud ON (cp.circ_lib = aoud.id)
1293 WHERE cp.call_number = acn.id
1294 AND cp.deleted IS FALSE
1295 ORDER BY rank_avail, COALESCE(cp.copy_number,0), cp.barcode
1296 LIMIT ($7 -> 'acp')::INT
1297 OFFSET ($8 -> 'acp')::INT
1305 (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)
1307 unapi.acnp( acn.prefix, 'marcxml', 'prefix', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE),
1308 unapi.acns( acn.suffix, 'marcxml', 'suffix', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE),
1309 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
1311 FROM asset.call_number acn
1312 JOIN actor.org_unit o ON (o.id = acn.owning_lib)
1314 AND acn.deleted IS FALSE
1315 GROUP BY acn.id, o.shortname, o.opac_visible, deleted, label, label_sortkey, label_class, owning_lib, record, acn.prefix, acn.suffix;
1316 $F$ LANGUAGE SQL STABLE;
1318 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$
1320 name call_number_prefix,
1322 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1325 'tag:open-ils.org:U2@aou/' || owning_lib AS owning_lib,
1329 FROM asset.call_number_prefix
1331 $F$ LANGUAGE SQL STABLE;
1333 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$
1335 name call_number_suffix,
1337 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1340 'tag:open-ils.org:U2@aou/' || owning_lib AS owning_lib,
1344 FROM asset.call_number_suffix
1346 $F$ LANGUAGE SQL STABLE;
1348 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$
1352 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1353 'tag:open-ils.org:U2@auri/' || uri.id AS id,
1359 WHEN ('acn' = ANY ($4)) THEN
1360 XMLELEMENT( name copies,
1361 (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)
1368 GROUP BY uri.id, use_restriction, href, label;
1369 $F$ LANGUAGE SQL STABLE;
1371 CREATE OR REPLACE FUNCTION unapi.cbs ( 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$
1375 NULL AS xmlns, -- TODO needs equivalent to http://open-ils.org/spec/holdings/v1
1383 FROM config.bib_source
1385 $F$ LANGUAGE SQL STABLE;
1387 CREATE OR REPLACE FUNCTION unapi.mra (
1393 depth INT DEFAULT NULL,
1394 slimit HSTORE DEFAULT NULL,
1395 soffset HSTORE DEFAULT NULL,
1396 include_xmlns BOOL DEFAULT TRUE
1397 ) RETURNS XML AS $F$
1401 CASE WHEN $9 THEN 'http://open-ils.org/spec/indexing/v1' ELSE NULL END AS xmlns,
1402 'tag:open-ils.org:U2@mra/' || $1 AS id,
1403 'tag:open-ils.org:U2@bre/' || $1 AS record
1405 (SELECT XMLAGG(foo.y)
1411 cvm.value AS "coded-value",
1420 FROM metabib.record_attr_flat mra
1421 JOIN config.record_attr_definition rad ON (mra.attr = rad.name)
1422 LEFT JOIN config.coded_value_map cvm ON (cvm.ctype = mra.attr AND code = mra.value)
1427 $F$ LANGUAGE SQL STABLE;
1429 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$
1433 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1434 'tag:open-ils.org:U2@circ/' || id AS id,
1438 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,
1439 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
1441 FROM action.circulation
1443 $F$ LANGUAGE SQL STABLE;
1445 CREATE OR REPLACE FUNCTION unapi.mmr_mra (
1451 depth INT DEFAULT NULL,
1452 slimit HSTORE DEFAULT NULL,
1453 soffset HSTORE DEFAULT NULL,
1454 include_xmlns BOOL DEFAULT TRUE,
1455 pref_lib INT DEFAULT NULL
1456 ) RETURNS XML AS $F$
1460 CASE WHEN $9 THEN 'http://open-ils.org/spec/indexing/v1' ELSE NULL END AS xmlns,
1461 'tag:open-ils.org:U2@mmr/' || $1 AS metarecord
1463 (SELECT XMLAGG(foo.y)
1465 WITH sourcelist AS (
1466 WITH aou AS (SELECT COALESCE(id, (evergreen.org_top()).id) AS id FROM actor.org_unit WHERE shortname = $5 LIMIT 1),
1467 basevm AS (SELECT c_attrs FROM asset.patron_default_visibility_mask()),
1468 circvm AS (SELECT search.calculate_visibility_attribute_test('circ_lib', ARRAY_AGG(aoud.id)) AS mask
1469 FROM aou, LATERAL actor.org_unit_descendants(aou.id, $6) aoud)
1471 FROM aou, circvm, basevm, metabib.metarecord_source_map mmsm
1472 WHERE mmsm.metarecord = $1 AND (
1475 FROM circvm, basevm, asset.copy_vis_attr_cache acvac
1476 WHERE acvac.vis_attr_vector @@ (basevm.c_attrs || '&' || circvm.mask)::query_int
1477 AND acvac.record = mmsm.source
1479 OR EXISTS (SELECT 1 FROM evergreen.located_uris(source, aou.id, $10) LIMIT 1)
1480 OR EXISTS (SELECT 1 FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) WHERE src.transcendant AND b.id = mmsm.source)
1488 cmra.value AS "coded-value",
1489 cmra.aid AS "cvmid",
1499 SELECT DISTINCT aid, attr, value, STRING_AGG(x.id::TEXT, ',') AS source_list
1501 SELECT v.source AS id,
1505 FROM metabib.record_attr_vector_list v
1506 JOIN config.coded_value_map c ON ( c.id = ANY( v.vlist ) )
1508 JOIN sourcelist ON (x.id = sourcelist.source)
1511 JOIN config.record_attr_definition rad ON (cmra.attr = rad.name)
1526 SELECT DISTINCT aid, attr, value
1528 SELECT v.source AS id,
1532 FROM metabib.record_attr_vector_list v
1533 JOIN metabib.uncontrolled_record_attr_value m ON ( m.id = ANY( v.vlist ) )
1535 JOIN sourcelist ON (x.id = sourcelist.source)
1537 JOIN config.record_attr_definition rad ON (umra.attr = rad.name)
1543 $F$ LANGUAGE SQL STABLE;
1545 CREATE OR REPLACE FUNCTION unapi.mmr_holdings_xml (
1549 depth INT DEFAULT NULL,
1550 includes TEXT[] DEFAULT NULL::TEXT[],
1551 slimit HSTORE DEFAULT NULL,
1552 soffset HSTORE DEFAULT NULL,
1553 include_xmlns BOOL DEFAULT TRUE,
1554 pref_lib INT DEFAULT NULL
1560 CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1561 CASE WHEN ('mmr' = ANY ($5)) THEN 'tag:open-ils.org:U2@mmr/' || $1 || '/' || $3 ELSE NULL END AS id,
1562 (SELECT metarecord_has_holdable_copy FROM asset.metarecord_has_holdable_copy($1)) AS has_holdable
1566 (SELECT XMLAGG(XMLELEMENT::XML) FROM (
1569 XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
1571 FROM asset.opac_ou_metarecord_copy_count($2, $1)
1575 XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
1577 FROM asset.staff_ou_metarecord_copy_count($2, $1)
1581 XMLATTRIBUTES('pref_lib' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
1583 FROM asset.opac_ou_metarecord_copy_count($9, $1)
1587 -- XXX monograph_parts and foreign_copies are skipped in MRs ... put them back some day?
1590 (SELECT XMLAGG(acn ORDER BY rank, name, label_sortkey) FROM (
1592 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
1593 FROM evergreen.ranked_volumes((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $4, $6, $7, $9, $5) AS y
1596 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
1597 FROM evergreen.located_uris((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $9) AS uris
1600 CASE WHEN ('ssub' = ANY ($5)) THEN
1603 (SELECT XMLAGG(ssub) FROM (
1604 SELECT unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
1605 FROM serial.subscription
1606 WHERE record_entry IN (SELECT source FROM metabib.metarecord_source_map WHERE metarecord = $1)
1611 $F$ LANGUAGE SQL STABLE;
1613 CREATE OR REPLACE FUNCTION unapi.mmr (
1619 depth INT DEFAULT NULL,
1620 slimit HSTORE DEFAULT NULL,
1621 soffset HSTORE DEFAULT NULL,
1622 include_xmlns BOOL DEFAULT TRUE,
1623 pref_lib INT DEFAULT NULL
1627 mmrec metabib.metarecord%ROWTYPE;
1628 leadrec biblio.record_entry%ROWTYPE;
1629 subrec biblio.record_entry%ROWTYPE;
1630 layout unapi.bre_output_layout%ROWTYPE;
1631 xfrm config.xml_transform%ROWTYPE;
1633 xml_buf TEXT; -- growing XML document
1634 tmp_xml TEXT; -- single-use XML string
1635 xml_frag TEXT; -- single-use XML fragment
1640 subxml XML; -- subordinate records elements
1645 -- xpath for extracting bre.marc values from subordinate records
1646 -- so they may be appended to the MARC of the master record prior
1647 -- to XSLT processing.
1648 -- subjects, isbn, issn, upc -- anything else?
1650 '//*[starts-with(@tag, "6") or @tag="020" or @tag="022" or @tag="024"]';
1652 IF org = '-' OR org IS NULL THEN
1653 SELECT shortname INTO org FROM evergreen.org_top();
1656 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
1658 IF ouid IS NULL THEN
1662 SELECT INTO mmrec * FROM metabib.metarecord WHERE id = obj_id;
1667 -- TODO: aggregate holdings from constituent records
1668 IF format = 'holdings_xml' THEN -- the special case
1669 output := unapi.mmr_holdings_xml(
1670 obj_id, ouid, org, depth,
1671 evergreen.array_remove_item_by_value(includes,'holdings_xml'),
1672 slimit, soffset, include_xmlns, pref_lib);
1676 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
1678 IF layout.name IS NULL THEN
1682 SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform;
1684 SELECT INTO leadrec * FROM biblio.record_entry WHERE id = mmrec.master_record;
1686 -- Grab distinct MVF for all records if requested
1687 IF ('mra' = ANY (includes)) THEN
1688 axml := unapi.mmr_mra(obj_id,NULL,NULL,NULL,org,depth,NULL,NULL,TRUE,pref_lib);
1693 xml_buf = leadrec.marc;
1696 IF ('holdings_xml' = ANY (includes)) THEN
1697 hxml := unapi.mmr_holdings_xml(
1698 obj_id, ouid, org, depth,
1699 evergreen.array_remove_item_by_value(includes,'holdings_xml'),
1700 slimit, soffset, include_xmlns, pref_lib);
1703 subxml := NULL::XML;
1704 parts := '{}'::TEXT[];
1705 FOR subrec IN SELECT bre.* FROM biblio.record_entry bre
1706 JOIN metabib.metarecord_source_map mmsm ON (mmsm.source = bre.id)
1707 JOIN metabib.metarecord mmr ON (mmr.id = mmsm.metarecord)
1708 WHERE mmr.id = obj_id AND NOT bre.deleted
1709 ORDER BY CASE WHEN bre.id = mmr.master_record THEN 0 ELSE bre.id END
1710 LIMIT COALESCE((slimit->'bre')::INT, 5) LOOP
1712 IF subrec.id = leadrec.id THEN CONTINUE; END IF;
1713 -- Append choice data from the the non-lead records to the
1714 -- the lead record document
1716 parts := parts || xpath(sub_xpath, subrec.marc::XML)::TEXT[];
1719 SELECT ARRAY_TO_STRING( ARRAY_AGG( DISTINCT p ), '' )::XML INTO subxml FROM UNNEST(parts) p;
1721 -- append data from the subordinate records to the
1722 -- main record document before applying the XSLT
1724 IF subxml IS NOT NULL THEN
1725 xml_buf := REGEXP_REPLACE(xml_buf,
1726 '</record>(.*?)$', subxml || '</record>' || E'\\1');
1729 IF format = 'marcxml' THEN
1730 -- If we're not using the prefixed namespace in
1731 -- this record, then remove all declarations of it
1732 IF xml_buf !~ E'<marc:' THEN
1733 xml_buf := REGEXP_REPLACE(xml_buf,
1734 ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g');
1737 xml_buf := oils_xslt_process(xml_buf, xfrm.xslt)::XML;
1740 -- update top_el to reflect the change in xml_buf, which may
1741 -- now be a different type of document (e.g. record -> mods)
1742 top_el := REGEXP_REPLACE(xml_buf, E'^.*?<((?:\\S+:)?' ||
1743 layout.holdings_element || ').*$', E'\\1');
1745 IF axml IS NOT NULL THEN
1746 xml_buf := REGEXP_REPLACE(xml_buf,
1747 '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1');
1750 IF hxml IS NOT NULL THEN
1751 xml_buf := REGEXP_REPLACE(xml_buf,
1752 '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1');
1755 IF ('mmr.unapi' = ANY (includes)) THEN
1756 output := REGEXP_REPLACE(
1758 '</' || top_el || '>(.*?)',
1762 'http://www.w3.org/1999/xhtml' AS xmlns,
1763 'unapi-id' AS class,
1764 'tag:open-ils.org:U2@mmr/' || obj_id || '/' || org AS title
1766 )::TEXT || '</' || top_el || E'>\\1'
1772 -- remove ignorable whitesace
1773 output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML;
1776 $F$ LANGUAGE PLPGSQL STABLE;
1781 -- Some test queries
1783 SELECT unapi.memoize( 'bre', 1,'mods32','','{holdings_xml,acp}'::TEXT[], 'SYS1');
1784 SELECT unapi.memoize( 'bre', 1,'marcxml','','{holdings_xml,acp}'::TEXT[], 'SYS1');
1785 SELECT unapi.memoize( 'bre', 1,'holdings_xml','','{holdings_xml,acp}'::TEXT[], 'SYS1');
1787 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>');
1789 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>');
1790 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>');
1791 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>');
1792 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>');
1794 SELECT unapi.biblio_record_entry_feed('{216}'::BIGINT[],'marcxml','{}'::TEXT[], 'BR1');
1795 EXPLAIN ANALYZE SELECT unapi.bre(216,'marcxml','record','{holdings_xml,bre.unapi}'::TEXT[], 'BR1');
1796 EXPLAIN ANALYZE SELECT unapi.bre(216,'holdings_xml','record','{}'::TEXT[], 'BR1');
1797 EXPLAIN ANALYZE SELECT unapi.holdings_xml(216,4,'BR1',2,'{bre}'::TEXT[]);
1798 EXPLAIN ANALYZE SELECT unapi.bre(216,'mods32','record','{}'::TEXT[], 'BR1');
1800 -- Limit to 5 call numbers, 5 copies, with a preferred library of 4 (BR1), in SYS2 at a depth of 0
1801 EXPLAIN ANALYZE SELECT unapi.bre(36,'marcxml','record','{holdings_xml,mra,acp,acnp,acns,bmp}','SYS2',0,'acn=>5,acp=>5',NULL,TRUE,4);