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
145 CASE WHEN $9 THEN 'http://open-ils.org/spec/indexing/v1' ELSE NULL END AS xmlns,
146 'tag:open-ils.org:U2@mmr/' || $1 AS metarecord
148 (SELECT XMLAGG(foo.y)
150 SELECT DISTINCT ON (COALESCE(cvm.id,uvm.id))
151 COALESCE(cvm.id,uvm.id),
156 cvm.value AS "coded-value",
165 FROM metabib.record_attr_flat mra
166 JOIN config.record_attr_definition rad ON (mra.attr = rad.name)
167 LEFT JOIN config.coded_value_map cvm ON (cvm.ctype = mra.attr AND code = mra.value)
168 LEFT JOIN metabib.uncontrolled_record_attr_value uvm ON (uvm.attr = mra.attr AND uvm.value = mra.value)
169 WHERE mra.id IN (SELECT source FROM metabib.metarecord_source_map WHERE metarecord = $1)
174 $F$ LANGUAGE SQL STABLE;
176 CREATE OR REPLACE FUNCTION evergreen.ranked_volumes(
179 depth INT DEFAULT NULL,
180 slimit HSTORE DEFAULT NULL,
181 soffset HSTORE DEFAULT NULL,
182 pref_lib INT DEFAULT NULL,
183 includes TEXT[] DEFAULT NULL::TEXT[]
184 ) RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank BIGINT) AS $$
185 SELECT ua.id, ua.name, ua.label_sortkey, MIN(ua.rank) AS rank FROM (
186 SELECT acn.id, aou.name, acn.label_sortkey,
187 evergreen.rank_ou(aou.id, $2, $6), evergreen.rank_cp_status(acp.status),
189 FROM asset.call_number acn
190 JOIN asset.copy acp ON (acn.id = acp.call_number)
191 JOIN actor.org_unit_descendants( $2, COALESCE(
194 FROM actor.org_unit_type aout
195 INNER JOIN actor.org_unit ou ON ou_type = aout.id
198 ) AS aou ON (acp.circ_lib = aou.id)
199 WHERE acn.record = ANY ($1)
200 AND acn.deleted IS FALSE
201 AND acp.deleted IS FALSE
202 AND CASE WHEN ('exclude_invisible_acn' = ANY($7)) THEN
205 FROM asset.opac_visible_copies
206 WHERE copy_id = acp.id AND record = acn.record
208 GROUP BY acn.id, acp.status, aou.name, acn.label_sortkey, aou.id
210 ORDER BY evergreen.rank_ou(aou.id, $2, $6), evergreen.rank_cp_status(acp.status)
213 GROUP BY ua.id, ua.name, ua.label_sortkey
214 ORDER BY rank, ua.name, ua.label_sortkey
215 LIMIT ($4 -> 'acn')::INT
216 OFFSET ($5 -> 'acn')::INT;
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 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 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;
265 CREATE OR REPLACE FUNCTION unapi.mmr_holdings_xml (
269 depth INT DEFAULT NULL,
270 includes TEXT[] DEFAULT NULL::TEXT[],
271 slimit HSTORE DEFAULT NULL,
272 soffset HSTORE DEFAULT NULL,
273 include_xmlns BOOL DEFAULT TRUE,
274 pref_lib INT DEFAULT NULL
280 CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
281 CASE WHEN ('mmr' = ANY ($5)) THEN 'tag:open-ils.org:U2@mmr/' || $1 || '/' || $3 ELSE NULL END AS id,
282 (SELECT metarecord_has_holdable_copy FROM asset.metarecord_has_holdable_copy($1)) AS has_holdable
286 (SELECT XMLAGG(XMLELEMENT::XML) FROM (
289 XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
291 FROM asset.opac_ou_metarecord_copy_count($2, $1)
295 XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
297 FROM asset.staff_ou_metarecord_copy_count($2, $1)
301 XMLATTRIBUTES('pref_lib' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
303 FROM asset.opac_ou_metarecord_copy_count($9, $1)
307 -- XXX monograph_parts and foreign_copies are skipped in MRs ... put them back some day?
310 (SELECT XMLAGG(acn ORDER BY rank, name, label_sortkey) FROM (
312 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
313 FROM evergreen.ranked_volumes((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $4, $6, $7, $9, $5) AS y
316 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
317 FROM evergreen.located_uris((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $9) AS uris
320 CASE WHEN ('ssub' = ANY ($5)) THEN
323 (SELECT XMLAGG(ssub) FROM (
324 SELECT unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
325 FROM serial.subscription
326 WHERE record_entry IN (SELECT source FROM metabib.metarecord_source_map WHERE metarecord = $1)
331 $F$ LANGUAGE SQL STABLE;
333 CREATE OR REPLACE FUNCTION unapi.mmr (
339 depth INT DEFAULT NULL,
340 slimit HSTORE DEFAULT NULL,
341 soffset HSTORE DEFAULT NULL,
342 include_xmlns BOOL DEFAULT TRUE,
343 pref_lib INT DEFAULT NULL
347 mmrec metabib.metarecord%ROWTYPE;
348 leadrec biblio.record_entry%ROWTYPE;
349 subrec biblio.record_entry%ROWTYPE;
350 layout unapi.bre_output_layout%ROWTYPE;
351 xfrm config.xml_transform%ROWTYPE;
353 xml_buf TEXT; -- growing XML document
354 tmp_xml TEXT; -- single-use XML string
355 xml_frag TEXT; -- single-use XML fragment
360 subxml XML; -- subordinate records elements
365 -- xpath for extracting bre.marc values from subordinate records
366 -- so they may be appended to the MARC of the master record prior
367 -- to XSLT processing.
368 -- subjects, isbn, issn, upc -- anything else?
370 '//*[starts-with(@tag, "6") or @tag="020" or @tag="022" or @tag="024"]';
372 IF org = '-' OR org IS NULL THEN
373 SELECT shortname INTO org FROM evergreen.org_top();
376 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
382 SELECT INTO mmrec * FROM metabib.metarecord WHERE id = obj_id;
387 -- TODO: aggregate holdings from constituent records
388 IF format = 'holdings_xml' THEN -- the special case
389 output := unapi.mmr_holdings_xml(
390 obj_id, ouid, org, depth,
391 evergreen.array_remove_item_by_value(includes,'holdings_xml'),
392 slimit, soffset, include_xmlns);
396 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
398 IF layout.name IS NULL THEN
402 SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform;
404 SELECT INTO leadrec * FROM biblio.record_entry WHERE id = mmrec.master_record;
406 -- Grab distinct MVF for all records if requested
407 IF ('mra' = ANY (includes)) THEN
408 axml := unapi.mmr_mra(obj_id,NULL,NULL,NULL,NULL,NULL,NULL,NULL,TRUE);
413 xml_buf = leadrec.marc;
416 IF ('holdings_xml' = ANY (includes)) THEN
417 hxml := unapi.mmr_holdings_xml(
418 obj_id, ouid, org, depth,
419 evergreen.array_remove_item_by_value(includes,'holdings_xml'),
420 slimit, soffset, include_xmlns, pref_lib);
424 parts := '{}'::TEXT[];
425 FOR subrec IN SELECT bre.* FROM biblio.record_entry bre
426 JOIN metabib.metarecord_source_map mmsm ON (mmsm.source = bre.id)
427 JOIN metabib.metarecord mmr ON (mmr.id = mmsm.metarecord)
428 WHERE mmr.id = obj_id
429 ORDER BY CASE WHEN bre.id = mmr.master_record THEN 0 ELSE bre.id END
430 LIMIT COALESCE((slimit->'bre')::INT, 5) LOOP
432 IF subrec.id = leadrec.id THEN CONTINUE; END IF;
433 -- Append choice data from the the non-lead records to the
434 -- the lead record document
436 parts := parts || xpath(sub_xpath, subrec.marc::XML)::TEXT[];
439 SELECT ARRAY_TO_STRING( ARRAY_AGG( DISTINCT p ), '' )::XML INTO subxml FROM UNNEST(parts) p;
441 -- append data from the subordinate records to the
442 -- main record document before applying the XSLT
444 IF subxml IS NOT NULL THEN
445 xml_buf := REGEXP_REPLACE(xml_buf,
446 '</record>(.*?)$', subxml || '</record>' || E'\\1');
449 IF format = 'marcxml' THEN
450 -- If we're not using the prefixed namespace in
451 -- this record, then remove all declarations of it
452 IF xml_buf !~ E'<marc:' THEN
453 xml_buf := REGEXP_REPLACE(xml_buf,
454 ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g');
457 xml_buf := oils_xslt_process(xml_buf, xfrm.xslt)::XML;
460 -- update top_el to reflect the change in xml_buf, which may
461 -- now be a different type of document (e.g. record -> mods)
462 top_el := REGEXP_REPLACE(xml_buf, E'^.*?<((?:\\S+:)?' ||
463 layout.holdings_element || ').*$', E'\\1');
465 IF axml IS NOT NULL THEN
466 xml_buf := REGEXP_REPLACE(xml_buf,
467 '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1');
470 IF hxml IS NOT NULL THEN
471 xml_buf := REGEXP_REPLACE(xml_buf,
472 '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1');
475 IF ('mmr.unapi' = ANY (includes)) THEN
476 output := REGEXP_REPLACE(
478 '</' || top_el || '>(.*?)',
482 'http://www.w3.org/1999/xhtml' AS xmlns,
484 'tag:open-ils.org:U2@mmr/' || obj_id || '/' || org AS title
486 )::TEXT || '</' || top_el || E'>\\1'
492 -- remove ignorable whitesace
493 output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML;
496 $F$ LANGUAGE PLPGSQL STABLE;