3 SELECT evergreen.upgrade_deps_block_check('0962', :eg_version);
5 ALTER TABLE vandelay.import_item_attr_definition
6 ADD COLUMN parts_data TEXT;
8 ALTER TABLE vandelay.import_item
9 ADD COLUMN parts_data TEXT;
11 CREATE OR REPLACE FUNCTION vandelay.ingest_items ( import_id BIGINT, attr_def_id BIGINT ) RETURNS SETOF vandelay.import_item AS $$
39 attr_set vandelay.import_item%ROWTYPE;
46 SELECT * INTO attr_def FROM vandelay.import_item_attr_definition WHERE id = attr_def_id;
50 attr_set.definition := attr_def.id;
52 -- Build the combined XPath
56 WHEN attr_def.owning_lib IS NULL THEN 'null()'
57 WHEN LENGTH( attr_def.owning_lib ) = 1 THEN '*[@code="' || attr_def.owning_lib || '"]'
58 ELSE '*' || attr_def.owning_lib
63 WHEN attr_def.circ_lib IS NULL THEN 'null()'
64 WHEN LENGTH( attr_def.circ_lib ) = 1 THEN '*[@code="' || attr_def.circ_lib || '"]'
65 ELSE '*' || attr_def.circ_lib
70 WHEN attr_def.call_number IS NULL THEN 'null()'
71 WHEN LENGTH( attr_def.call_number ) = 1 THEN '*[@code="' || attr_def.call_number || '"]'
72 ELSE '*' || attr_def.call_number
77 WHEN attr_def.copy_number IS NULL THEN 'null()'
78 WHEN LENGTH( attr_def.copy_number ) = 1 THEN '*[@code="' || attr_def.copy_number || '"]'
79 ELSE '*' || attr_def.copy_number
84 WHEN attr_def.status IS NULL THEN 'null()'
85 WHEN LENGTH( attr_def.status ) = 1 THEN '*[@code="' || attr_def.status || '"]'
86 ELSE '*' || attr_def.status
91 WHEN attr_def.location IS NULL THEN 'null()'
92 WHEN LENGTH( attr_def.location ) = 1 THEN '*[@code="' || attr_def.location || '"]'
93 ELSE '*' || attr_def.location
98 WHEN attr_def.circulate IS NULL THEN 'null()'
99 WHEN LENGTH( attr_def.circulate ) = 1 THEN '*[@code="' || attr_def.circulate || '"]'
100 ELSE '*' || attr_def.circulate
105 WHEN attr_def.deposit IS NULL THEN 'null()'
106 WHEN LENGTH( attr_def.deposit ) = 1 THEN '*[@code="' || attr_def.deposit || '"]'
107 ELSE '*' || attr_def.deposit
112 WHEN attr_def.deposit_amount IS NULL THEN 'null()'
113 WHEN LENGTH( attr_def.deposit_amount ) = 1 THEN '*[@code="' || attr_def.deposit_amount || '"]'
114 ELSE '*' || attr_def.deposit_amount
119 WHEN attr_def.ref IS NULL THEN 'null()'
120 WHEN LENGTH( attr_def.ref ) = 1 THEN '*[@code="' || attr_def.ref || '"]'
121 ELSE '*' || attr_def.ref
126 WHEN attr_def.holdable IS NULL THEN 'null()'
127 WHEN LENGTH( attr_def.holdable ) = 1 THEN '*[@code="' || attr_def.holdable || '"]'
128 ELSE '*' || attr_def.holdable
133 WHEN attr_def.price IS NULL THEN 'null()'
134 WHEN LENGTH( attr_def.price ) = 1 THEN '*[@code="' || attr_def.price || '"]'
135 ELSE '*' || attr_def.price
140 WHEN attr_def.barcode IS NULL THEN 'null()'
141 WHEN LENGTH( attr_def.barcode ) = 1 THEN '*[@code="' || attr_def.barcode || '"]'
142 ELSE '*' || attr_def.barcode
147 WHEN attr_def.circ_modifier IS NULL THEN 'null()'
148 WHEN LENGTH( attr_def.circ_modifier ) = 1 THEN '*[@code="' || attr_def.circ_modifier || '"]'
149 ELSE '*' || attr_def.circ_modifier
154 WHEN attr_def.circ_as_type IS NULL THEN 'null()'
155 WHEN LENGTH( attr_def.circ_as_type ) = 1 THEN '*[@code="' || attr_def.circ_as_type || '"]'
156 ELSE '*' || attr_def.circ_as_type
161 WHEN attr_def.alert_message IS NULL THEN 'null()'
162 WHEN LENGTH( attr_def.alert_message ) = 1 THEN '*[@code="' || attr_def.alert_message || '"]'
163 ELSE '*' || attr_def.alert_message
168 WHEN attr_def.opac_visible IS NULL THEN 'null()'
169 WHEN LENGTH( attr_def.opac_visible ) = 1 THEN '*[@code="' || attr_def.opac_visible || '"]'
170 ELSE '*' || attr_def.opac_visible
175 WHEN attr_def.pub_note IS NULL THEN 'null()'
176 WHEN LENGTH( attr_def.pub_note ) = 1 THEN '*[@code="' || attr_def.pub_note || '"]'
177 ELSE '*' || attr_def.pub_note
181 WHEN attr_def.priv_note IS NULL THEN 'null()'
182 WHEN LENGTH( attr_def.priv_note ) = 1 THEN '*[@code="' || attr_def.priv_note || '"]'
183 ELSE '*' || attr_def.priv_note
188 WHEN attr_def.internal_id IS NULL THEN 'null()'
189 WHEN LENGTH( attr_def.internal_id ) = 1 THEN '*[@code="' || attr_def.internal_id || '"]'
190 ELSE '*' || attr_def.internal_id
195 WHEN attr_def.stat_cat_data IS NULL THEN 'null()'
196 WHEN LENGTH( attr_def.stat_cat_data ) = 1 THEN '*[@code="' || attr_def.stat_cat_data || '"]'
197 ELSE '*' || attr_def.stat_cat_data
202 WHEN attr_def.parts_data IS NULL THEN 'null()'
203 WHEN LENGTH( attr_def.parts_data ) = 1 THEN '*[@code="' || attr
204 ELSE '*' || attr_def.parts_data
209 xpaths := ARRAY[owning_lib, circ_lib, call_number, copy_number, status, location, circulate,
210 deposit, deposit_amount, ref, holdable, price, barcode, circ_modifier, circ_as_type,
211 alert_message, pub_note, priv_note, internal_id, stat_cat_data, parts_data, opac_visible];
215 FROM oils_xpath_tag_to_table( (SELECT marc FROM vandelay.queued_bib_record WHERE id = import_id), attr_def.tag, xpaths)
216 AS t( ol TEXT, clib TEXT, cn TEXT, cnum TEXT, cs TEXT, cl TEXT, circ TEXT,
217 dep TEXT, dep_amount TEXT, r TEXT, hold TEXT, pr TEXT, bc TEXT, circ_mod TEXT,
218 circ_as TEXT, amessage TEXT, note TEXT, pnote TEXT, internal_id TEXT,
219 stat_cat_data TEXT, parts_data TEXT, opac_vis TEXT )
222 attr_set.import_error := NULL;
223 attr_set.error_detail := NULL;
224 attr_set.deposit_amount := NULL;
225 attr_set.copy_number := NULL;
226 attr_set.price := NULL;
227 attr_set.circ_modifier := NULL;
228 attr_set.location := NULL;
229 attr_set.barcode := NULL;
230 attr_set.call_number := NULL;
232 IF tmp_attr_set.pr != '' THEN
233 tmp_str = REGEXP_REPLACE(tmp_attr_set.pr, E'[^0-9\\.]', '', 'g');
235 attr_set.import_error := 'import.item.invalid.price';
236 attr_set.error_detail := tmp_attr_set.pr; -- original value
237 RETURN NEXT attr_set; CONTINUE;
239 attr_set.price := tmp_str::NUMERIC(8,2);
242 IF tmp_attr_set.dep_amount != '' THEN
243 tmp_str = REGEXP_REPLACE(tmp_attr_set.dep_amount, E'[^0-9\\.]', '', 'g');
245 attr_set.import_error := 'import.item.invalid.deposit_amount';
246 attr_set.error_detail := tmp_attr_set.dep_amount;
247 RETURN NEXT attr_set; CONTINUE;
249 attr_set.deposit_amount := tmp_str::NUMERIC(8,2);
252 IF tmp_attr_set.cnum != '' THEN
253 tmp_str = REGEXP_REPLACE(tmp_attr_set.cnum, E'[^0-9]', '', 'g');
255 attr_set.import_error := 'import.item.invalid.copy_number';
256 attr_set.error_detail := tmp_attr_set.cnum;
257 RETURN NEXT attr_set; CONTINUE;
259 attr_set.copy_number := tmp_str::INT;
262 IF tmp_attr_set.ol != '' THEN
263 SELECT id INTO attr_set.owning_lib FROM actor.org_unit WHERE shortname = UPPER(tmp_attr_set.ol); -- INT
265 attr_set.import_error := 'import.item.invalid.owning_lib';
266 attr_set.error_detail := tmp_attr_set.ol;
267 RETURN NEXT attr_set; CONTINUE;
271 IF tmp_attr_set.clib != '' THEN
272 SELECT id INTO attr_set.circ_lib FROM actor.org_unit WHERE shortname = UPPER(tmp_attr_set.clib); -- INT
274 attr_set.import_error := 'import.item.invalid.circ_lib';
275 attr_set.error_detail := tmp_attr_set.clib;
276 RETURN NEXT attr_set; CONTINUE;
280 IF tmp_attr_set.cs != '' THEN
281 SELECT id INTO attr_set.status FROM config.copy_status WHERE LOWER(name) = LOWER(tmp_attr_set.cs); -- INT
283 attr_set.import_error := 'import.item.invalid.status';
284 attr_set.error_detail := tmp_attr_set.cs;
285 RETURN NEXT attr_set; CONTINUE;
289 IF COALESCE(tmp_attr_set.circ_mod, '') = '' THEN
291 -- no circ mod defined, see if we should apply a default
292 SELECT INTO attr_set.circ_modifier TRIM(BOTH '"' FROM value)
293 FROM actor.org_unit_ancestor_setting(
294 'vandelay.item.circ_modifier.default',
298 -- make sure the value from the org setting is still valid
299 PERFORM 1 FROM config.circ_modifier WHERE code = attr_set.circ_modifier;
301 attr_set.import_error := 'import.item.invalid.circ_modifier';
302 attr_set.error_detail := tmp_attr_set.circ_mod;
303 RETURN NEXT attr_set; CONTINUE;
308 SELECT code INTO attr_set.circ_modifier FROM config.circ_modifier WHERE code = tmp_attr_set.circ_mod;
310 attr_set.import_error := 'import.item.invalid.circ_modifier';
311 attr_set.error_detail := tmp_attr_set.circ_mod;
312 RETURN NEXT attr_set; CONTINUE;
316 IF tmp_attr_set.circ_as != '' THEN
317 SELECT code INTO attr_set.circ_as_type FROM config.coded_value_map WHERE ctype = 'item_type' AND code = tmp_attr_set.circ_as;
319 attr_set.import_error := 'import.item.invalid.circ_as_type';
320 attr_set.error_detail := tmp_attr_set.circ_as;
321 RETURN NEXT attr_set; CONTINUE;
325 IF COALESCE(tmp_attr_set.cl, '') = '' THEN
326 -- no location specified, see if we should apply a default
328 SELECT INTO attr_set.location TRIM(BOTH '"' FROM value)
329 FROM actor.org_unit_ancestor_setting(
330 'vandelay.item.copy_location.default',
334 -- make sure the value from the org setting is still valid
335 PERFORM 1 FROM asset.copy_location WHERE id = attr_set.location;
337 attr_set.import_error := 'import.item.invalid.location';
338 attr_set.error_detail := tmp_attr_set.cs;
339 RETURN NEXT attr_set; CONTINUE;
343 -- search up the org unit tree for a matching copy location
344 WITH RECURSIVE anscestor_depth AS (
348 FROM actor.org_unit ou
349 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
350 WHERE ou.id = COALESCE(attr_set.owning_lib, attr_set.circ_lib)
355 FROM actor.org_unit ou
356 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
357 JOIN anscestor_depth ot ON (ot.parent_ou = ou.id)
358 ) SELECT cpl.id INTO attr_set.location
359 FROM anscestor_depth a
360 JOIN asset.copy_location cpl ON (cpl.owning_lib = a.id)
361 WHERE LOWER(cpl.name) = LOWER(tmp_attr_set.cl)
362 ORDER BY a.depth DESC
366 attr_set.import_error := 'import.item.invalid.location';
367 attr_set.error_detail := tmp_attr_set.cs;
368 RETURN NEXT attr_set; CONTINUE;
372 attr_set.circulate :=
373 LOWER( SUBSTRING( tmp_attr_set.circ, 1, 1)) IN ('t','y','1')
374 OR LOWER(tmp_attr_set.circ) = 'circulating'; -- BOOL
377 LOWER( SUBSTRING( tmp_attr_set.dep, 1, 1 ) ) IN ('t','y','1')
378 OR LOWER(tmp_attr_set.dep) = 'deposit'; -- BOOL
381 LOWER( SUBSTRING( tmp_attr_set.hold, 1, 1 ) ) IN ('t','y','1')
382 OR LOWER(tmp_attr_set.hold) = 'holdable'; -- BOOL
384 attr_set.opac_visible :=
385 LOWER( SUBSTRING( tmp_attr_set.opac_vis, 1, 1 ) ) IN ('t','y','1')
386 OR LOWER(tmp_attr_set.opac_vis) = 'visible'; -- BOOL
389 LOWER( SUBSTRING( tmp_attr_set.r, 1, 1 ) ) IN ('t','y','1')
390 OR LOWER(tmp_attr_set.r) = 'reference'; -- BOOL
392 attr_set.call_number := tmp_attr_set.cn; -- TEXT
393 attr_set.barcode := tmp_attr_set.bc; -- TEXT,
394 attr_set.alert_message := tmp_attr_set.amessage; -- TEXT,
395 attr_set.pub_note := tmp_attr_set.note; -- TEXT,
396 attr_set.priv_note := tmp_attr_set.pnote; -- TEXT,
397 attr_set.alert_message := tmp_attr_set.amessage; -- TEXT,
398 attr_set.internal_id := tmp_attr_set.internal_id::BIGINT;
399 attr_set.stat_cat_data := tmp_attr_set.stat_cat_data; -- TEXT,
400 attr_set.parts_data := tmp_attr_set.parts_data; -- TEXT,
402 RETURN NEXT attr_set;
413 CREATE OR REPLACE FUNCTION vandelay.ingest_bib_items ( ) RETURNS TRIGGER AS $func$
416 item_data vandelay.import_item%ROWTYPE;
419 IF TG_OP IN ('INSERT','UPDATE') AND NEW.imported_as IS NOT NULL THEN
423 SELECT item_attr_def INTO attr_def FROM vandelay.bib_queue WHERE id = NEW.queue;
425 FOR item_data IN SELECT * FROM vandelay.ingest_items( NEW.id::BIGINT, attr_def ) LOOP
426 INSERT INTO vandelay.import_item (
455 item_data.definition,
456 item_data.owning_lib,
458 item_data.call_number,
459 item_data.copy_number,
464 item_data.deposit_amount,
469 item_data.circ_modifier,
470 item_data.circ_as_type,
471 item_data.alert_message,
474 item_data.internal_id,
475 item_data.opac_visible,
476 item_data.stat_cat_data,
477 item_data.parts_data,
478 item_data.import_error,
479 item_data.error_detail
485 $func$ LANGUAGE PLPGSQL;