3 CREATE OR REPLACE FUNCTION asset.opac_ou_metarecord_copy_count (org INT, rid BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
8 SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) JOIN metabib.metarecord_source_map m ON (m.source = b.id) WHERE src.transcendant AND m.metarecord = rid;
10 FOR ans IN SELECT u.id, t.depth FROM actor.org_unit_ancestors(org) AS u JOIN actor.org_unit_type t ON (u.ou_type = t.id) LOOP
15 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
19 actor.org_unit_descendants(ans.id) d
20 JOIN asset.opac_visible_copies av ON (av.circ_lib = d.id)
21 JOIN asset.copy cp ON (cp.id = av.copy_id)
22 JOIN metabib.metarecord_source_map m ON (m.metarecord = rid AND m.source = av.record)
26 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
35 CREATE OR REPLACE FUNCTION asset.opac_lasso_metarecord_copy_count (i_lasso INT, rid BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
40 SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) JOIN metabib.metarecord_source_map m ON (m.source = b.id) WHERE src.transcendant AND m.metarecord = rid;
42 FOR ans IN SELECT u.org_unit AS id FROM actor.org_lasso_map AS u WHERE lasso = i_lasso LOOP
47 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
51 actor.org_unit_descendants(ans.id) d
52 JOIN asset.opac_visible_copies av ON (av.circ_lib = d.id)
53 JOIN asset.copy cp ON (cp.id = av.copy_id)
54 JOIN metabib.metarecord_source_map m ON (m.metarecord = rid AND m.source = av.record)
58 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
67 CREATE OR REPLACE FUNCTION asset.staff_ou_metarecord_copy_count (org INT, rid BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
72 SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) JOIN metabib.metarecord_source_map m ON (m.source = b.id) WHERE src.transcendant AND m.metarecord = rid;
74 FOR ans IN SELECT u.id, t.depth FROM actor.org_unit_ancestors(org) AS u JOIN actor.org_unit_type t ON (u.ou_type = t.id) LOOP
79 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
83 actor.org_unit_descendants(ans.id) d
84 JOIN asset.copy cp ON (cp.circ_lib = d.id AND NOT cp.deleted)
85 JOIN asset.call_number cn ON (cn.id = cp.call_number AND NOT cn.deleted)
86 JOIN metabib.metarecord_source_map m ON (m.metarecord = rid AND m.source = cn.record)
90 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
99 CREATE OR REPLACE FUNCTION asset.staff_lasso_metarecord_copy_count (i_lasso INT, rid BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
104 SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) JOIN metabib.metarecord_source_map m ON (m.source = b.id) WHERE src.transcendant AND m.metarecord = rid;
106 FOR ans IN SELECT u.org_unit AS id FROM actor.org_lasso_map AS u WHERE lasso = i_lasso LOOP
111 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
115 actor.org_unit_descendants(ans.id) d
116 JOIN asset.copy cp ON (cp.circ_lib = d.id AND NOT cp.deleted)
117 JOIN asset.call_number cn ON (cn.id = cp.call_number AND NOT cn.deleted)
118 JOIN metabib.metarecord_source_map m ON (m.metarecord = rid AND m.source = cn.record)
122 RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
129 $f$ LANGUAGE PLPGSQL;
131 CREATE OR REPLACE FUNCTION unapi.mmr_mra (
137 depth INT DEFAULT NULL,
138 slimit HSTORE DEFAULT NULL,
139 soffset HSTORE DEFAULT NULL,
140 include_xmlns BOOL DEFAULT TRUE,
141 pref_lib INT DEFAULT NULL
146 CASE WHEN $9 THEN 'http://open-ils.org/spec/indexing/v1' ELSE NULL END AS xmlns,
147 'tag:open-ils.org:U2@mmr/' || $1 AS metarecord
149 (SELECT XMLAGG(foo.y)
151 SELECT DISTINCT ON (COALESCE(cvm.id,uvm.id))
152 COALESCE(cvm.id,uvm.id),
157 cvm.value AS "coded-value",
166 FROM metabib.record_attr_flat mra
167 JOIN config.record_attr_definition rad ON (mra.attr = rad.name)
168 LEFT JOIN config.coded_value_map cvm ON (cvm.ctype = mra.attr AND code = mra.value)
169 LEFT JOIN metabib.uncontrolled_record_attr_value uvm ON (uvm.attr = mra.attr AND uvm.value = mra.value)
171 WITH aou AS (SELECT COALESCE(id, (evergreen.org_top()).id) AS id
172 FROM actor.org_unit WHERE shortname = $5 LIMIT 1)
174 FROM metabib.metarecord_source_map, aou
175 WHERE metarecord = $1 AND (
177 SELECT 1 FROM asset.opac_visible_copies
178 WHERE record = source AND circ_lib IN (
179 SELECT id FROM actor.org_unit_descendants(aou.id, $6))
182 OR EXISTS (SELECT 1 FROM located_uris(source, aou.id, $10) LIMIT 1)
189 $F$ LANGUAGE SQL STABLE;
191 CREATE OR REPLACE FUNCTION evergreen.ranked_volumes(
194 depth INT DEFAULT NULL,
195 slimit HSTORE DEFAULT NULL,
196 soffset HSTORE DEFAULT NULL,
197 pref_lib INT DEFAULT NULL,
198 includes TEXT[] DEFAULT NULL::TEXT[]
199 ) RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank BIGINT) AS $$
200 SELECT ua.id, ua.name, ua.label_sortkey, MIN(ua.rank) AS rank FROM (
201 SELECT acn.id, aou.name, acn.label_sortkey,
202 evergreen.rank_ou(aou.id, $2, $6), evergreen.rank_cp_status(acp.status),
204 FROM asset.call_number acn
205 JOIN asset.copy acp ON (acn.id = acp.call_number)
206 JOIN actor.org_unit_descendants( $2, COALESCE(
209 FROM actor.org_unit_type aout
210 INNER JOIN actor.org_unit ou ON ou_type = aout.id
213 ) AS aou ON (acp.circ_lib = aou.id)
214 WHERE acn.record = ANY ($1)
215 AND acn.deleted IS FALSE
216 AND acp.deleted IS FALSE
217 AND CASE WHEN ('exclude_invisible_acn' = ANY($7)) THEN
220 FROM asset.opac_visible_copies
221 WHERE copy_id = acp.id AND record = acn.record
223 GROUP BY acn.id, acp.status, aou.name, acn.label_sortkey, aou.id
225 ORDER BY evergreen.rank_ou(aou.id, $2, $6), evergreen.rank_cp_status(acp.status)
228 GROUP BY ua.id, ua.name, ua.label_sortkey
229 ORDER BY rank, ua.name, ua.label_sortkey
230 LIMIT ($4 -> 'acn')::INT
231 OFFSET ($5 -> 'acn')::INT;
235 CREATE OR REPLACE FUNCTION evergreen.ranked_volumes
236 ( 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[] )
237 RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank BIGINT)
238 AS $$ SELECT * FROM evergreen.ranked_volumes(ARRAY[$1],$2,$3,$4,$5,$6,$7) $$ LANGUAGE SQL STABLE;
241 CREATE OR REPLACE FUNCTION evergreen.located_uris (
244 pref_lib INT DEFAULT NULL
245 ) RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank INT) AS $$
246 WITH all_orgs AS (SELECT COALESCE( enabled, FALSE ) AS flag FROM config.global_flag WHERE name = 'opac.located_uri.act_as_copy')
247 SELECT DISTINCT ON (id) * FROM (
248 SELECT acn.id, COALESCE(aou.name,aoud.name), acn.label_sortkey, evergreen.rank_ou(aou.id, $2, $3) AS pref_ou
249 FROM asset.call_number acn
250 INNER JOIN asset.uri_call_number_map auricnm ON acn.id = auricnm.call_number
251 INNER JOIN asset.uri auri ON auri.id = auricnm.uri
252 LEFT JOIN actor.org_unit_ancestors( COALESCE($3, $2) ) aou ON (acn.owning_lib = aou.id)
253 LEFT JOIN actor.org_unit_descendants( COALESCE($3, $2) ) aoud ON (acn.owning_lib = aoud.id),
255 WHERE acn.record = ANY ($1)
256 AND acn.deleted IS FALSE
257 AND auri.active IS TRUE
258 AND ((NOT all_orgs.flag AND aou.id IS NOT NULL) OR COALESCE(aou.id,aoud.id) IS NOT NULL)
260 SELECT acn.id, COALESCE(aou.name,aoud.name) AS name, acn.label_sortkey, evergreen.rank_ou(aou.id, $2, $3) AS pref_ou
261 FROM asset.call_number acn
262 INNER JOIN asset.uri_call_number_map auricnm ON acn.id = auricnm.call_number
263 INNER JOIN asset.uri auri ON auri.id = auricnm.uri
264 LEFT JOIN actor.org_unit_ancestors( $2 ) aou ON (acn.owning_lib = aou.id)
265 LEFT JOIN actor.org_unit_descendants( $2 ) aoud ON (acn.owning_lib = aoud.id),
267 WHERE acn.record = ANY ($1)
268 AND acn.deleted IS FALSE
269 AND auri.active IS TRUE
270 AND ((NOT all_orgs.flag AND aou.id IS NOT NULL) OR COALESCE(aou.id,aoud.id) IS NOT NULL))x
271 ORDER BY id, pref_ou DESC;
275 CREATE OR REPLACE FUNCTION evergreen.located_uris ( bibid BIGINT, ouid INT, pref_lib INT DEFAULT NULL)
276 RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank INT)
277 AS $$ SELECT * FROM evergreen.located_uris(ARRAY[$1],$2,$3) $$ LANGUAGE SQL STABLE;
280 CREATE OR REPLACE FUNCTION unapi.mmr_holdings_xml (
284 depth INT DEFAULT NULL,
285 includes TEXT[] DEFAULT NULL::TEXT[],
286 slimit HSTORE DEFAULT NULL,
287 soffset HSTORE DEFAULT NULL,
288 include_xmlns BOOL DEFAULT TRUE,
289 pref_lib INT DEFAULT NULL
295 CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
296 CASE WHEN ('mmr' = ANY ($5)) THEN 'tag:open-ils.org:U2@mmr/' || $1 || '/' || $3 ELSE NULL END AS id,
297 (SELECT metarecord_has_holdable_copy FROM asset.metarecord_has_holdable_copy($1)) AS has_holdable
301 (SELECT XMLAGG(XMLELEMENT::XML) FROM (
304 XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
306 FROM asset.opac_ou_metarecord_copy_count($2, $1)
310 XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
312 FROM asset.staff_ou_metarecord_copy_count($2, $1)
316 XMLATTRIBUTES('pref_lib' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
318 FROM asset.opac_ou_metarecord_copy_count($9, $1)
322 -- XXX monograph_parts and foreign_copies are skipped in MRs ... put them back some day?
325 (SELECT XMLAGG(acn ORDER BY rank, name, label_sortkey) FROM (
327 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
328 FROM evergreen.ranked_volumes((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $4, $6, $7, $9, $5) AS y
331 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
332 FROM evergreen.located_uris((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $9) AS uris
335 CASE WHEN ('ssub' = ANY ($5)) THEN
338 (SELECT XMLAGG(ssub) FROM (
339 SELECT unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
340 FROM serial.subscription
341 WHERE record_entry IN (SELECT source FROM metabib.metarecord_source_map WHERE metarecord = $1)
346 $F$ LANGUAGE SQL STABLE;
348 CREATE OR REPLACE FUNCTION unapi.mmr (
354 depth INT DEFAULT NULL,
355 slimit HSTORE DEFAULT NULL,
356 soffset HSTORE DEFAULT NULL,
357 include_xmlns BOOL DEFAULT TRUE,
358 pref_lib INT DEFAULT NULL
362 mmrec metabib.metarecord%ROWTYPE;
363 leadrec biblio.record_entry%ROWTYPE;
364 subrec biblio.record_entry%ROWTYPE;
365 layout unapi.bre_output_layout%ROWTYPE;
366 xfrm config.xml_transform%ROWTYPE;
368 xml_buf TEXT; -- growing XML document
369 tmp_xml TEXT; -- single-use XML string
370 xml_frag TEXT; -- single-use XML fragment
375 subxml XML; -- subordinate records elements
380 -- xpath for extracting bre.marc values from subordinate records
381 -- so they may be appended to the MARC of the master record prior
382 -- to XSLT processing.
383 -- subjects, isbn, issn, upc -- anything else?
385 '//*[starts-with(@tag, "6") or @tag="020" or @tag="022" or @tag="024"]';
387 IF org = '-' OR org IS NULL THEN
388 SELECT shortname INTO org FROM evergreen.org_top();
391 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
397 SELECT INTO mmrec * FROM metabib.metarecord WHERE id = obj_id;
402 -- TODO: aggregate holdings from constituent records
403 IF format = 'holdings_xml' THEN -- the special case
404 output := unapi.mmr_holdings_xml(
405 obj_id, ouid, org, depth,
406 evergreen.array_remove_item_by_value(includes,'holdings_xml'),
407 slimit, soffset, include_xmlns);
411 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
413 IF layout.name IS NULL THEN
417 SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform;
419 SELECT INTO leadrec * FROM biblio.record_entry WHERE id = mmrec.master_record;
421 -- Grab distinct MVF for all records if requested
422 IF ('mra' = ANY (includes)) THEN
423 axml := unapi.mmr_mra(obj_id,NULL,NULL,NULL,org,depth,NULL,NULL,TRUE,pref_lib);
428 xml_buf = leadrec.marc;
431 IF ('holdings_xml' = ANY (includes)) THEN
432 hxml := unapi.mmr_holdings_xml(
433 obj_id, ouid, org, depth,
434 evergreen.array_remove_item_by_value(includes,'holdings_xml'),
435 slimit, soffset, include_xmlns, pref_lib);
439 parts := '{}'::TEXT[];
440 FOR subrec IN SELECT bre.* FROM biblio.record_entry bre
441 JOIN metabib.metarecord_source_map mmsm ON (mmsm.source = bre.id)
442 JOIN metabib.metarecord mmr ON (mmr.id = mmsm.metarecord)
443 WHERE mmr.id = obj_id
444 ORDER BY CASE WHEN bre.id = mmr.master_record THEN 0 ELSE bre.id END
445 LIMIT COALESCE((slimit->'bre')::INT, 5) LOOP
447 IF subrec.id = leadrec.id THEN CONTINUE; END IF;
448 -- Append choice data from the the non-lead records to the
449 -- the lead record document
451 parts := parts || xpath(sub_xpath, subrec.marc::XML)::TEXT[];
454 SELECT ARRAY_TO_STRING( ARRAY_AGG( DISTINCT p ), '' )::XML INTO subxml FROM UNNEST(parts) p;
456 -- append data from the subordinate records to the
457 -- main record document before applying the XSLT
459 IF subxml IS NOT NULL THEN
460 xml_buf := REGEXP_REPLACE(xml_buf,
461 '</record>(.*?)$', subxml || '</record>' || E'\\1');
464 IF format = 'marcxml' THEN
465 -- If we're not using the prefixed namespace in
466 -- this record, then remove all declarations of it
467 IF xml_buf !~ E'<marc:' THEN
468 xml_buf := REGEXP_REPLACE(xml_buf,
469 ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g');
472 xml_buf := oils_xslt_process(xml_buf, xfrm.xslt)::XML;
475 -- update top_el to reflect the change in xml_buf, which may
476 -- now be a different type of document (e.g. record -> mods)
477 top_el := REGEXP_REPLACE(xml_buf, E'^.*?<((?:\\S+:)?' ||
478 layout.holdings_element || ').*$', E'\\1');
480 IF axml IS NOT NULL THEN
481 xml_buf := REGEXP_REPLACE(xml_buf,
482 '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1');
485 IF hxml IS NOT NULL THEN
486 xml_buf := REGEXP_REPLACE(xml_buf,
487 '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1');
490 IF ('mmr.unapi' = ANY (includes)) THEN
491 output := REGEXP_REPLACE(
493 '</' || top_el || '>(.*?)',
497 'http://www.w3.org/1999/xhtml' AS xmlns,
499 'tag:open-ils.org:U2@mmr/' || obj_id || '/' || org AS title
501 )::TEXT || '</' || top_el || E'>\\1'
507 -- remove ignorable whitesace
508 output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML;
511 $F$ LANGUAGE PLPGSQL STABLE;