]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0512.schema.multi-home-items.sql
LP#1917826: tweaks to data update
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0512.schema.multi-home-items.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version) VALUES ('0512'); -- miker
4
5 CREATE TABLE biblio.peer_type (
6     id      SERIAL  PRIMARY KEY,
7     name        TEXT        NOT NULL UNIQUE -- i18n
8 );
9
10 CREATE TABLE biblio.peer_bib_copy_map (
11     id      SERIAL  PRIMARY KEY,
12     peer_type   INT     NOT NULL REFERENCES biblio.peer_type (id),
13     peer_record BIGINT      NOT NULL REFERENCES biblio.record_entry (id),
14     target_copy BIGINT      NOT NULL -- can't use fkey because of acp subtables
15 );
16 CREATE INDEX peer_bib_copy_map_record_idx ON biblio.peer_bib_copy_map (peer_record);
17 CREATE INDEX peer_bib_copy_map_copy_idx ON biblio.peer_bib_copy_map (target_copy);
18
19 DROP TABLE asset.opac_visible_copies;
20 CREATE TABLE asset.opac_visible_copies (
21   id        BIGSERIAL primary key,
22   copy_id   BIGINT,
23   record    BIGINT,
24   circ_lib  INTEGER
25 );
26
27 INSERT INTO biblio.peer_type (id,name) VALUES
28     (1,oils_i18n_gettext(1,'Bound Volume','bpt','name')),
29     (2,oils_i18n_gettext(2,'Bilingual','bpt','name')),
30     (3,oils_i18n_gettext(3,'Back-to-back','bpt','name')),
31     (4,oils_i18n_gettext(4,'Set','bpt','name')),
32     (5,oils_i18n_gettext(5,'e-Reader Preload','bpt','name')); 
33
34 SELECT SETVAL('biblio.peer_type_id_seq'::TEXT, 100);
35
36 CREATE OR REPLACE FUNCTION search.query_parser_fts (
37
38     param_search_ou INT,
39     param_depth     INT,
40     param_query     TEXT,
41     param_statuses  INT[],
42     param_locations INT[],
43     param_offset    INT,
44     param_check     INT,
45     param_limit     INT,
46     metarecord      BOOL,
47     staff           BOOL
48  
49 ) RETURNS SETOF search.search_result AS $func$
50 DECLARE
51
52     current_res         search.search_result%ROWTYPE;
53     search_org_list     INT[];
54
55     check_limit         INT;
56     core_limit          INT;
57     core_offset         INT;
58     tmp_int             INT;
59
60     core_result         RECORD;
61     core_cursor         REFCURSOR;
62     core_rel_query      TEXT;
63
64     total_count         INT := 0;
65     check_count         INT := 0;
66     deleted_count       INT := 0;
67     visible_count       INT := 0;
68     excluded_count      INT := 0;
69
70 BEGIN
71
72     check_limit := COALESCE( param_check, 1000 );
73     core_limit  := COALESCE( param_limit, 25000 );
74     core_offset := COALESCE( param_offset, 0 );
75
76     -- core_skip_chk := COALESCE( param_skip_chk, 1 );
77
78     IF param_search_ou > 0 THEN
79         IF param_depth IS NOT NULL THEN
80             SELECT array_accum(distinct id) INTO search_org_list FROM actor.org_unit_descendants( param_search_ou, param_depth );
81         ELSE
82             SELECT array_accum(distinct id) INTO search_org_list FROM actor.org_unit_descendants( param_search_ou );
83         END IF;
84     ELSIF param_search_ou < 0 THEN
85         SELECT array_accum(distinct org_unit) INTO search_org_list FROM actor.org_lasso_map WHERE lasso = -param_search_ou;
86     ELSIF param_search_ou = 0 THEN
87         -- reserved for user lassos (ou_buckets/type='lasso') with ID passed in depth ... hack? sure.
88     END IF;
89
90     OPEN core_cursor FOR EXECUTE param_query;
91
92     LOOP
93
94         FETCH core_cursor INTO core_result;
95         EXIT WHEN NOT FOUND;
96         EXIT WHEN total_count >= core_limit;
97
98         total_count := total_count + 1;
99
100         CONTINUE WHEN total_count NOT BETWEEN  core_offset + 1 AND check_limit + core_offset;
101
102         check_count := check_count + 1;
103
104         PERFORM 1 FROM biblio.record_entry b WHERE NOT b.deleted AND b.id IN ( SELECT * FROM search.explode_array( core_result.records ) );
105         IF NOT FOUND THEN
106             -- RAISE NOTICE ' % were all deleted ... ', core_result.records;
107             deleted_count := deleted_count + 1;
108             CONTINUE;
109         END IF;
110
111         PERFORM 1
112           FROM  biblio.record_entry b
113                 JOIN config.bib_source s ON (b.source = s.id)
114           WHERE s.transcendant
115                 AND b.id IN ( SELECT * FROM search.explode_array( core_result.records ) );
116
117         IF FOUND THEN
118             -- RAISE NOTICE ' % were all transcendant ... ', core_result.records;
119             visible_count := visible_count + 1;
120
121             current_res.id = core_result.id;
122             current_res.rel = core_result.rel;
123
124             tmp_int := 1;
125             IF metarecord THEN
126                 SELECT COUNT(DISTINCT s.source) INTO tmp_int FROM metabib.metarecord_source_map s WHERE s.metarecord = core_result.id;
127             END IF;
128
129             IF tmp_int = 1 THEN
130                 current_res.record = core_result.records[1];
131             ELSE
132                 current_res.record = NULL;
133             END IF;
134
135             RETURN NEXT current_res;
136
137             CONTINUE;
138         END IF;
139
140         PERFORM 1
141           FROM  asset.call_number cn
142                 JOIN asset.uri_call_number_map map ON (map.call_number = cn.id)
143                 JOIN asset.uri uri ON (map.uri = uri.id)
144           WHERE NOT cn.deleted
145                 AND cn.label = '##URI##'
146                 AND uri.active
147                 AND ( param_locations IS NULL OR array_upper(param_locations, 1) IS NULL )
148                 AND cn.record IN ( SELECT * FROM search.explode_array( core_result.records ) )
149                 AND cn.owning_lib IN ( SELECT * FROM search.explode_array( search_org_list ) )
150           LIMIT 1;
151
152         IF FOUND THEN
153             -- RAISE NOTICE ' % have at least one URI ... ', core_result.records;
154             visible_count := visible_count + 1;
155
156             current_res.id = core_result.id;
157             current_res.rel = core_result.rel;
158
159             tmp_int := 1;
160             IF metarecord THEN
161                 SELECT COUNT(DISTINCT s.source) INTO tmp_int FROM metabib.metarecord_source_map s WHERE s.metarecord = core_result.id;
162             END IF;
163
164             IF tmp_int = 1 THEN
165                 current_res.record = core_result.records[1];
166             ELSE
167                 current_res.record = NULL;
168             END IF;
169
170             RETURN NEXT current_res;
171
172             CONTINUE;
173         END IF;
174
175         IF param_statuses IS NOT NULL AND array_upper(param_statuses, 1) > 0 THEN
176
177             PERFORM 1
178               FROM  asset.call_number cn
179                     JOIN asset.copy cp ON (cp.call_number = cn.id)
180               WHERE NOT cn.deleted
181                     AND NOT cp.deleted
182                     AND cp.status IN ( SELECT * FROM search.explode_array( param_statuses ) )
183                     AND cn.record IN ( SELECT * FROM search.explode_array( core_result.records ) )
184                     AND cp.circ_lib IN ( SELECT * FROM search.explode_array( search_org_list ) )
185               LIMIT 1;
186
187             IF NOT FOUND THEN
188                 PERFORM 1
189                   FROM  biblio.peer_bib_copy_map pr
190                         JOIN asset.copy cp ON (cp.id = pr.target_copy)
191                   WHERE NOT cp.deleted
192                         AND cp.status IN ( SELECT * FROM search.explode_array( param_statuses ) )
193                         AND pr.peer_record IN ( SELECT * FROM search.explode_array( core_result.records ) )
194                         AND cp.circ_lib IN ( SELECT * FROM search.explode_array( search_org_list ) )
195                   LIMIT 1;
196
197                 IF NOT FOUND THEN
198                 -- RAISE NOTICE ' % and multi-home linked records were all status-excluded ... ', core_result.records;
199                     excluded_count := excluded_count + 1;
200                     CONTINUE;
201                 END IF;
202             END IF;
203
204         END IF;
205
206         IF param_locations IS NOT NULL AND array_upper(param_locations, 1) > 0 THEN
207
208             PERFORM 1
209               FROM  asset.call_number cn
210                     JOIN asset.copy cp ON (cp.call_number = cn.id)
211               WHERE NOT cn.deleted
212                     AND NOT cp.deleted
213                     AND cp.location IN ( SELECT * FROM search.explode_array( param_locations ) )
214                     AND cn.record IN ( SELECT * FROM search.explode_array( core_result.records ) )
215                     AND cp.circ_lib IN ( SELECT * FROM search.explode_array( search_org_list ) )
216               LIMIT 1;
217
218             IF NOT FOUND THEN
219                 PERFORM 1
220                   FROM  biblio.peer_bib_copy_map pr
221                         JOIN asset.copy cp ON (cp.id = pr.target_copy)
222                   WHERE NOT cp.deleted
223                         AND cp.location IN ( SELECT * FROM search.explode_array( param_locations ) )
224                         AND pr.peer_record IN ( SELECT * FROM search.explode_array( core_result.records ) )
225                         AND cp.circ_lib IN ( SELECT * FROM search.explode_array( search_org_list ) )
226                   LIMIT 1;
227
228                 IF NOT FOUND THEN
229                     -- RAISE NOTICE ' % and multi-home linked records were all copy_location-excluded ... ', core_result.records;
230                     excluded_count := excluded_count + 1;
231                     CONTINUE;
232                 END IF;
233             END IF;
234
235         END IF;
236
237         IF staff IS NULL OR NOT staff THEN
238
239             PERFORM 1
240               FROM  asset.opac_visible_copies
241               WHERE circ_lib IN ( SELECT * FROM search.explode_array( search_org_list ) )
242                     AND record IN ( SELECT * FROM search.explode_array( core_result.records ) )
243               LIMIT 1;
244
245             IF NOT FOUND THEN
246                 PERFORM 1
247                   FROM  biblio.peer_bib_copy_map pr
248                         JOIN asset.opac_visible_copies cp ON (cp.copy_id = pr.target_copy)
249                   WHERE cp.circ_lib IN ( SELECT * FROM search.explode_array( search_org_list ) )
250                         AND pr.peer_record IN ( SELECT * FROM search.explode_array( core_result.records ) )
251                   LIMIT 1;
252
253                 IF NOT FOUND THEN
254
255                     -- RAISE NOTICE ' % and multi-home linked records were all visibility-excluded ... ', core_result.records;
256                     excluded_count := excluded_count + 1;
257                     CONTINUE;
258                 END IF;
259             END IF;
260
261         ELSE
262
263             PERFORM 1
264               FROM  asset.call_number cn
265                     JOIN asset.copy cp ON (cp.call_number = cn.id)
266               WHERE NOT cn.deleted
267                     AND NOT cp.deleted
268                     AND cp.circ_lib IN ( SELECT * FROM search.explode_array( search_org_list ) )
269                     AND cn.record IN ( SELECT * FROM search.explode_array( core_result.records ) )
270               LIMIT 1;
271
272             IF NOT FOUND THEN
273
274                 PERFORM 1
275                   FROM  biblio.peer_bib_copy_map pr
276                         JOIN asset.copy cp ON (cp.id = pr.target_copy)
277                   WHERE NOT cp.deleted
278                         AND cp.circ_lib IN ( SELECT * FROM search.explode_array( search_org_list ) )
279                         AND pr.peer_record IN ( SELECT * FROM search.explode_array( core_result.records ) )
280                   LIMIT 1;
281
282                 IF NOT FOUND THEN
283
284                     PERFORM 1
285                       FROM  asset.call_number cn
286                       WHERE cn.record IN ( SELECT * FROM search.explode_array( core_result.records ) )
287                       LIMIT 1;
288
289                     IF FOUND THEN
290                         -- RAISE NOTICE ' % and multi-home linked records were all visibility-excluded ... ', core_result.records;
291                         excluded_count := excluded_count + 1;
292                         CONTINUE;
293                     END IF;
294                 END IF;
295
296             END IF;
297
298         END IF;
299
300         visible_count := visible_count + 1;
301
302         current_res.id = core_result.id;
303         current_res.rel = core_result.rel;
304
305         tmp_int := 1;
306         IF metarecord THEN
307             SELECT COUNT(DISTINCT s.source) INTO tmp_int FROM metabib.metarecord_source_map s WHERE s.metarecord = core_result.id;
308         END IF;
309
310         IF tmp_int = 1 THEN
311             current_res.record = core_result.records[1];
312         ELSE
313             current_res.record = NULL;
314         END IF;
315
316         RETURN NEXT current_res;
317
318         IF visible_count % 1000 = 0 THEN
319             -- RAISE NOTICE ' % visible so far ... ', visible_count;
320         END IF;
321
322     END LOOP;
323
324     current_res.id = NULL;
325     current_res.rel = NULL;
326     current_res.record = NULL;
327     current_res.total = total_count;
328     current_res.checked = check_count;
329     current_res.deleted = deleted_count;
330     current_res.visible = visible_count;
331     current_res.excluded = excluded_count;
332
333     CLOSE core_cursor;
334
335     RETURN NEXT current_res;
336
337 END;
338 $func$ LANGUAGE PLPGSQL;
339
340 CREATE OR REPLACE FUNCTION unapi.holdings_xml (bid BIGINT, ouid INT, org TEXT, depth INT DEFAULT NULL, includes TEXT[] DEFAULT NULL::TEXT[], slimit INT DEFAULT NULL, soffset INT DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE) RETURNS XML AS $F$
341      SELECT  XMLELEMENT(
342                  name holdings,
343                  XMLATTRIBUTES(
344                     CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
345                     CASE WHEN ('bre' = ANY ('{acn,auri}'::TEXT[] || $5)) THEN 'tag:open-ils.org:U2@bre/' || $1 || '/' || $3 ELSE NULL END AS id
346                  ),
347                  XMLELEMENT(
348                      name counts,
349                      (SELECT  XMLAGG(XMLELEMENT::XML) FROM (
350                          SELECT  XMLELEMENT(
351                                      name count,
352                                      XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
353                                  )::text
354                            FROM  asset.opac_ou_record_copy_count($2,  $1)
355                                      UNION
356                          SELECT  XMLELEMENT(
357                                      name count,
358                                      XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
359                                  )::text
360                            FROM  asset.staff_ou_record_copy_count($2, $1)
361                                      ORDER BY 1
362                      )x)
363                  ),
364                  CASE
365                      WHEN ('bmp' = ANY ($5)) THEN
366                         XMLELEMENT( name monograph_parts,
367                             XMLAGG((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) FROM biblio.monograph_part WHERE record = $1))
368                         )
369                      ELSE NULL
370                  END,
371                  CASE WHEN ('acn' = ANY ('{acn,auri}'::TEXT[] || $5)) THEN
372                      XMLELEMENT(
373                          name volumes,
374                          (SELECT XMLAGG(acn) FROM (
375                             SELECT  unapi.acn(acn.id,'xml','volume',evergreen.array_remove_item_by_value(evergreen.array_remove_item_by_value('{acn,auri}'::TEXT[] || $5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE)
376                               FROM  asset.call_number acn
377                               WHERE acn.record = $1
378                                     AND EXISTS (
379                                         SELECT  1
380                                           FROM  asset.copy acp
381                                                 JOIN actor.org_unit_descendants(
382                                                     $2,
383                                                     (COALESCE(
384                                                         $4,
385                                                         (SELECT aout.depth
386                                                           FROM  actor.org_unit_type aout
387                                                                 JOIN actor.org_unit aou ON (aou.ou_type = aout.id AND aou.id = $2)
388                                                         )
389                                                     ))
390                                                 ) aoud ON (acp.circ_lib = aoud.id)
391                                           LIMIT 1
392                                     )
393                               ORDER BY label_sortkey
394                               LIMIT $6
395                               OFFSET $7
396                          )x)
397                      )
398                  ELSE NULL END,
399                  CASE WHEN ('ssub' = ANY ('{acn,auri}'::TEXT[] || $5)) THEN
400                      XMLELEMENT(
401                          name subscriptions,
402                          (SELECT XMLAGG(ssub) FROM (
403                             SELECT  unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
404                               FROM  serial.subscription
405                               WHERE record_entry = $1
406                         )x)
407                      )
408                  ELSE NULL END,
409                  CASE WHEN ('acp' = ANY ($5)) THEN
410                      XMLELEMENT(
411                          name foreign_copies,
412                          (SELECT XMLAGG(acp) FROM (
413                             SELECT  unapi.acp(p.target_copy,'xml','copy','{}'::TEXT[], $3, $4, $6, $7, FALSE)
414                               FROM  biblio.peer_bib_copy_map p
415                                     JOIN asset.copy c ON (p.target_copy = c.id)
416                               WHERE NOT c.deleted AND peer_record = $1
417                         )x)
418                      )
419                  ELSE NULL END
420              );
421 $F$ LANGUAGE SQL;
422
423 CREATE OR REPLACE FUNCTION unapi.acp ( obj_id BIGINT, format TEXT,  ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit INT DEFAULT NULL, soffset INT DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
424         SELECT  XMLELEMENT(
425                     name copy,
426                     XMLATTRIBUTES(
427                         CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
428                         'tag:open-ils.org:U2@acp/' || id AS id,
429                         create_date, edit_date, copy_number, circulate, deposit,
430                         ref, holdable, deleted, deposit_amount, price, barcode,
431                         circ_modifier, circ_as_type, opac_visible
432                     ),
433                     unapi.ccs( status, $2, 'status', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE),
434                     unapi.acl( location, $2, 'location', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE),
435                     unapi.aou( circ_lib, $2, 'circ_lib', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8),
436                     unapi.aou( circ_lib, $2, 'circlib', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8),
437                     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,
438                     XMLELEMENT( name copy_notes,
439                         CASE
440                             WHEN ('acpn' = ANY ($4)) THEN
441                                 XMLAGG((SELECT unapi.acpn( id, 'xml', 'copy_note', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE) FROM asset.copy_note WHERE owning_copy = cp.id AND pub))
442                             ELSE NULL
443                         END
444                     ),
445                     XMLELEMENT( name statcats,
446                         CASE
447                             WHEN ('ascecm' = ANY ($4)) THEN
448                                 XMLAGG((SELECT unapi.ascecm( stat_cat_entry, 'xml', 'statcat', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE) FROM asset.stat_cat_entry_copy_map WHERE owning_copy = cp.id))
449                             ELSE NULL
450                         END
451                     ),
452                     XMLELEMENT( name foreign_records,
453                         CASE
454                             WHEN ('bre' = ANY ($4)) THEN
455                                 XMLAGG((SELECT unapi.bre(peer_record,'marcxml','record','{}'::TEXT[], $5, $6, $7, $8, FALSE) FROM biblio.peer_bib_copy_map WHERE target_copy = cp.id))
456                             ELSE NULL
457                         END
458
459                     ),
460                     CASE
461                         WHEN ('bmp' = ANY ($4)) THEN
462                             XMLELEMENT( name monograph_parts,
463                                 XMLAGG((SELECT unapi.bmp( part, 'xml', 'monograph_part', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE) FROM asset.copy_part_map WHERE target_copy = cp.id))
464                             )
465                         ELSE NULL
466                     END
467                 )
468           FROM  asset.copy cp
469           WHERE id = $1
470           GROUP BY id, status, location, circ_lib, call_number, create_date, edit_date, copy_number, circulate, deposit, ref, holdable, deleted, deposit_amount, price, barcode, circ_modifier, circ_as_type, opac_visible;
471 $F$ LANGUAGE SQL;
472
473 CREATE OR REPLACE FUNCTION asset.refresh_opac_visible_copies_mat_view () RETURNS VOID AS $$
474
475     TRUNCATE TABLE asset.opac_visible_copies;
476
477     INSERT INTO asset.opac_visible_copies (copy_id, circ_lib, record)
478     SELECT  cp.id, cp.circ_lib, cn.record
479     FROM  asset.copy cp
480         JOIN asset.call_number cn ON (cn.id = cp.call_number)
481         JOIN actor.org_unit a ON (cp.circ_lib = a.id)
482         JOIN asset.copy_location cl ON (cp.location = cl.id)
483         JOIN config.copy_status cs ON (cp.status = cs.id)
484         JOIN biblio.record_entry b ON (cn.record = b.id)
485     WHERE NOT cp.deleted
486         AND NOT cn.deleted
487         AND NOT b.deleted
488         AND cs.opac_visible
489         AND cl.opac_visible
490         AND cp.opac_visible
491         AND a.opac_visible
492             UNION
493     SELECT  cp.id, cp.circ_lib, pbcm.peer_record AS record
494     FROM  asset.copy cp
495         JOIN biblio.peer_bib_copy_map pbcm ON (pbcm.target_copy = cp.id)
496         JOIN actor.org_unit a ON (cp.circ_lib = a.id)
497         JOIN asset.copy_location cl ON (cp.location = cl.id)
498         JOIN config.copy_status cs ON (cp.status = cs.id)
499     WHERE NOT cp.deleted
500         AND cs.opac_visible
501         AND cl.opac_visible
502         AND cp.opac_visible
503         AND a.opac_visible;
504
505 $$ LANGUAGE SQL;
506 COMMENT ON FUNCTION asset.refresh_opac_visible_copies_mat_view() IS $$
507 Rebuild the copy OPAC visibility cache.  Useful during migrations.
508 $$;
509
510 SELECT asset.refresh_opac_visible_copies_mat_view();
511 CREATE INDEX opac_visible_copies_idx1 on asset.opac_visible_copies (record, circ_lib);
512 CREATE INDEX opac_visible_copies_copy_id_idx on asset.opac_visible_copies (copy_id);
513 CREATE UNIQUE INDEX opac_visible_copies_once_per_record_idx on asset.opac_visible_copies (copy_id, record);
514  
515 CREATE OR REPLACE FUNCTION asset.cache_copy_visibility () RETURNS TRIGGER as $func$
516 DECLARE
517     add_query       TEXT;
518     remove_query    TEXT;
519     do_add          BOOLEAN := false;
520     do_remove       BOOLEAN := false;
521 BEGIN
522     add_query := $$
523             INSERT INTO asset.opac_visible_copies (copy_id, circ_lib, record)
524               SELECT id, circ_lib, record FROM (
525                 SELECT  cp.id, cp.circ_lib, cn.record, cn.id AS call_number
526                   FROM  asset.copy cp
527                         JOIN asset.call_number cn ON (cn.id = cp.call_number)
528                         JOIN actor.org_unit a ON (cp.circ_lib = a.id)
529                         JOIN asset.copy_location cl ON (cp.location = cl.id)
530                         JOIN config.copy_status cs ON (cp.status = cs.id)
531                         JOIN biblio.record_entry b ON (cn.record = b.id)
532                   WHERE NOT cp.deleted
533                         AND NOT cn.deleted
534                         AND NOT b.deleted
535                         AND cs.opac_visible
536                         AND cl.opac_visible
537                         AND cp.opac_visible
538                         AND a.opac_visible
539                             UNION
540                 SELECT  cp.id, cp.circ_lib, pbcm.peer_record AS record, NULL AS call_number
541                   FROM  asset.copy cp
542                         JOIN biblio.peer_bib_copy_map pbcm ON (pbcm.target_copy = cp.id)
543                         JOIN actor.org_unit a ON (cp.circ_lib = a.id)
544                         JOIN asset.copy_location cl ON (cp.location = cl.id)
545                         JOIN config.copy_status cs ON (cp.status = cs.id)
546                   WHERE NOT cp.deleted
547                         AND cs.opac_visible
548                         AND cl.opac_visible
549                         AND cp.opac_visible
550                         AND a.opac_visible
551                     ) AS x 
552
553     $$;
554  
555     remove_query := $$ DELETE FROM asset.opac_visible_copies WHERE copy_id IN ( SELECT id FROM asset.copy WHERE $$;
556
557     IF TG_TABLE_NAME = 'peer_bib_copy_map' THEN
558         IF TG_OP = 'INSERT' THEN
559             add_query := add_query || 'WHERE x.id = ' || NEW.target_copy || ' AND x.record = ' || NEW.peer_record || ';';
560             EXECUTE add_query;
561             RETURN NEW;
562         ELSE
563             remove_query := 'DELETE FROM asset.opac_visible_copies WHERE copy_id = ' || OLD.target_copy || ' AND record = ' || OLD.peer_record || ';';
564             EXECUTE remove_query;
565             RETURN OLD;
566         END IF;
567     END IF;
568
569     IF TG_OP = 'INSERT' THEN
570
571         IF TG_TABLE_NAME IN ('copy', 'unit') THEN
572             add_query := add_query || 'WHERE x.id = ' || NEW.id || ';';
573             EXECUTE add_query;
574         END IF;
575
576         RETURN NEW;
577
578     END IF;
579
580     -- handle items first, since with circulation activity
581     -- their statuses change frequently
582     IF TG_TABLE_NAME IN ('copy', 'unit') THEN
583
584         IF OLD.location    <> NEW.location OR
585            OLD.call_number <> NEW.call_number OR
586            OLD.status      <> NEW.status OR
587            OLD.circ_lib    <> NEW.circ_lib THEN
588             -- any of these could change visibility, but
589             -- we'll save some queries and not try to calculate
590             -- the change directly
591             do_remove := true;
592             do_add := true;
593         ELSE
594
595             IF OLD.deleted <> NEW.deleted THEN
596                 IF NEW.deleted THEN
597                     do_remove := true;
598                 ELSE
599                     do_add := true;
600                 END IF;
601             END IF;
602
603             IF OLD.opac_visible <> NEW.opac_visible THEN
604                 IF OLD.opac_visible THEN
605                     do_remove := true;
606                 ELSIF NOT do_remove THEN -- handle edge case where deleted item
607                                         -- is also marked opac_visible
608                     do_add := true;
609                 END IF;
610             END IF;
611
612         END IF;
613
614         IF do_remove THEN
615             DELETE FROM asset.opac_visible_copies WHERE id = NEW.id;
616         END IF;
617         IF do_add THEN
618             add_query := add_query || 'WHERE x.id = ' || NEW.id || ';';
619             EXECUTE add_query;
620         END IF;
621
622         RETURN NEW;
623
624     END IF;
625
626     IF TG_TABLE_NAME IN ('call_number', 'record_entry') THEN -- these have a 'deleted' column
627  
628         IF OLD.deleted AND NEW.deleted THEN -- do nothing
629
630             RETURN NEW;
631  
632         ELSIF NEW.deleted THEN -- remove rows
633  
634             IF TG_TABLE_NAME = 'call_number' THEN
635                 DELETE FROM asset.opac_visible_copies WHERE id IN (SELECT id FROM asset.copy WHERE call_number = NEW.id);
636             ELSIF TG_TABLE_NAME = 'record_entry' THEN
637                 DELETE FROM asset.opac_visible_copies WHERE record = NEW.id;
638             END IF;
639  
640             RETURN NEW;
641  
642         ELSIF OLD.deleted THEN -- add rows
643  
644             IF TG_TABLE_NAME IN ('copy','unit') THEN
645                 add_query := add_query || 'WHERE x.id = ' || NEW.id || ';';
646             ELSIF TG_TABLE_NAME = 'call_number' THEN
647                 add_query := add_query || 'WHERE x.call_number = ' || NEW.id || ';';
648             ELSIF TG_TABLE_NAME = 'record_entry' THEN
649                 add_query := add_query || 'WHERE x.record = ' || NEW.id || ';';
650             END IF;
651  
652             EXECUTE add_query;
653             RETURN NEW;
654  
655         END IF;
656  
657     END IF;
658
659     IF TG_TABLE_NAME = 'call_number' THEN
660
661         IF OLD.record <> NEW.record THEN
662             -- call number is linked to different bib
663             remove_query := remove_query || 'call_number = ' || NEW.id || ');';
664             EXECUTE remove_query;
665             add_query := add_query || 'WHERE x.call_number = ' || NEW.id || ';';
666             EXECUTE add_query;
667         END IF;
668
669         RETURN NEW;
670
671     END IF;
672
673     IF TG_TABLE_NAME IN ('record_entry') THEN
674         RETURN NEW; -- don't have 'opac_visible'
675     END IF;
676
677     -- actor.org_unit, asset.copy_location, asset.copy_status
678     IF NEW.opac_visible = OLD.opac_visible THEN -- do nothing
679
680         RETURN NEW;
681
682     ELSIF NEW.opac_visible THEN -- add rows
683
684         IF TG_TABLE_NAME = 'org_unit' THEN
685             add_query := add_query || 'AND cp.circ_lib = ' || NEW.id || ';';
686         ELSIF TG_TABLE_NAME = 'copy_location' THEN
687             add_query := add_query || 'AND cp.location = ' || NEW.id || ';';
688         ELSIF TG_TABLE_NAME = 'copy_status' THEN
689             add_query := add_query || 'AND cp.status = ' || NEW.id || ';';
690         END IF;
691  
692         EXECUTE add_query;
693  
694     ELSE -- delete rows
695
696         IF TG_TABLE_NAME = 'org_unit' THEN
697             remove_query := 'DELETE FROM asset.opac_visible_copies WHERE circ_lib = ' || NEW.id || ';';
698         ELSIF TG_TABLE_NAME = 'copy_location' THEN
699             remove_query := remove_query || 'location = ' || NEW.id || ');';
700         ELSIF TG_TABLE_NAME = 'copy_status' THEN
701             remove_query := remove_query || 'status = ' || NEW.id || ');';
702         END IF;
703  
704         EXECUTE remove_query;
705  
706     END IF;
707  
708     RETURN NEW;
709 END;
710 $func$ LANGUAGE PLPGSQL;
711 COMMENT ON FUNCTION asset.cache_copy_visibility() IS $$
712 Trigger function to update the copy OPAC visiblity cache.
713 $$;
714
715 CREATE TRIGGER a_opac_vis_mat_view_tgr AFTER INSERT OR DELETE ON biblio.peer_bib_copy_map FOR EACH ROW EXECUTE PROCEDURE asset.cache_copy_visibility();
716
717 CREATE OR REPLACE FUNCTION biblio.indexing_ingest_or_delete () RETURNS TRIGGER AS $func$
718 DECLARE
719     transformed_xml TEXT;
720     prev_xfrm       TEXT;
721     normalizer      RECORD;
722     xfrm            config.xml_transform%ROWTYPE;
723     attr_value      TEXT;
724     new_attrs       HSTORE := ''::HSTORE;
725     attr_def        config.record_attr_definition%ROWTYPE;
726 BEGIN
727
728     IF NEW.deleted IS TRUE THEN -- If this bib is deleted
729         DELETE FROM metabib.metarecord_source_map WHERE source = NEW.id; -- Rid ourselves of the search-estimate-killing linkage
730         DELETE FROM metabib.record_attr WHERE id = NEW.id; -- Kill the attrs hash, useless on deleted records
731         DELETE FROM authority.bib_linking WHERE bib = NEW.id; -- Avoid updating fields in bibs that are no longer visible
732         DELETE FROM biblio.peer_bib_copy_map WHERE peer_record = NEW.id; -- Separate any multi-homed items
733         RETURN NEW; -- and we're done
734     END IF;
735
736     IF TG_OP = 'UPDATE' THEN -- re-ingest?
737         PERFORM * FROM config.internal_flag WHERE name = 'ingest.reingest.force_on_same_marc' AND enabled;
738
739         IF NOT FOUND AND OLD.marc = NEW.marc THEN -- don't do anything if the MARC didn't change
740             RETURN NEW;
741         END IF;
742     END IF;
743
744     -- Record authority linking
745     PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_linking' AND enabled;
746     IF NOT FOUND THEN
747         PERFORM biblio.map_authority_linking( NEW.id, NEW.marc );
748     END IF;
749
750     -- Flatten and insert the mfr data
751     PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_metabib_full_rec' AND enabled;
752     IF NOT FOUND THEN
753         PERFORM metabib.reingest_metabib_full_rec(NEW.id);
754
755         -- Now we pull out attribute data, which is dependent on the mfr for all but XPath-based fields
756         PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_metabib_rec_descriptor' AND enabled;
757         IF NOT FOUND THEN
758             FOR attr_def IN SELECT * FROM config.record_attr_definition ORDER BY format LOOP
759
760                 IF attr_def.tag IS NOT NULL THEN -- tag (and optional subfield list) selection
761                     SELECT  ARRAY_TO_STRING(ARRAY_ACCUM(value), COALESCE(attr_def.joiner,' ')) INTO attr_value
762                       FROM  (SELECT * FROM metabib.full_rec ORDER BY tag, subfield) AS x
763                       WHERE record = NEW.id
764                             AND tag LIKE attr_def.tag
765                             AND CASE
766                                 WHEN attr_def.sf_list IS NOT NULL 
767                                     THEN POSITION(subfield IN attr_def.sf_list) > 0
768                                 ELSE TRUE
769                                 END
770                       GROUP BY tag
771                       ORDER BY tag
772                       LIMIT 1;
773
774                 ELSIF attr_def.fixed_field IS NOT NULL THEN -- a named fixed field, see config.marc21_ff_pos_map.fixed_field
775                     attr_value := biblio.marc21_extract_fixed_field(NEW.id, attr_def.fixed_field);
776
777                 ELSIF attr_def.xpath IS NOT NULL THEN -- and xpath expression
778
779                     SELECT INTO xfrm * FROM config.xml_transform WHERE name = attr_def.format;
780             
781                     -- See if we can skip the XSLT ... it's expensive
782                     IF prev_xfrm IS NULL OR prev_xfrm <> xfrm.name THEN
783                         -- Can't skip the transform
784                         IF xfrm.xslt <> '---' THEN
785                             transformed_xml := oils_xslt_process(NEW.marc,xfrm.xslt);
786                         ELSE
787                             transformed_xml := NEW.marc;
788                         END IF;
789             
790                         prev_xfrm := xfrm.name;
791                     END IF;
792
793                     IF xfrm.name IS NULL THEN
794                         -- just grab the marcxml (empty) transform
795                         SELECT INTO xfrm * FROM config.xml_transform WHERE xslt = '---' LIMIT 1;
796                         prev_xfrm := xfrm.name;
797                     END IF;
798
799                     attr_value := oils_xpath_string(attr_def.xpath, transformed_xml, COALESCE(attr_def.joiner,' '), ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]]);
800
801                 ELSIF attr_def.phys_char_sf IS NOT NULL THEN -- a named Physical Characteristic, see config.marc21_physical_characteristic_*_map
802                     SELECT  value::TEXT INTO attr_value
803                       FROM  biblio.marc21_physical_characteristics(NEW.id)
804                       WHERE subfield = attr_def.phys_char_sf
805                       LIMIT 1; -- Just in case ...
806
807                 END IF;
808
809                 -- apply index normalizers to attr_value
810                 FOR normalizer IN
811                     SELECT  n.func AS func,
812                             n.param_count AS param_count,
813                             m.params AS params
814                       FROM  config.index_normalizer n
815                             JOIN config.record_attr_index_norm_map m ON (m.norm = n.id)
816                       WHERE attr = attr_def.name
817                       ORDER BY m.pos LOOP
818                         EXECUTE 'SELECT ' || normalizer.func || '(' ||
819                             quote_literal( attr_value ) ||
820                             CASE
821                                 WHEN normalizer.param_count > 0
822                                     THEN ',' || REPLACE(REPLACE(BTRIM(normalizer.params,'[]'),E'\'',E'\\\''),E'"',E'\'')
823                                     ELSE ''
824                                 END ||
825                             ')' INTO attr_value;
826         
827                 END LOOP;
828
829                 -- Add the new value to the hstore
830                 new_attrs := new_attrs || hstore( attr_def.name, attr_value );
831
832             END LOOP;
833
834             IF TG_OP = 'INSERT' OR OLD.deleted THEN -- initial insert OR revivication
835                 INSERT INTO metabib.record_attr (id, attrs) VALUES (NEW.id, new_attrs);
836             ELSE
837                 UPDATE metabib.record_attr SET attrs = attrs || new_attrs WHERE id = NEW.id;
838             END IF;
839
840         END IF;
841     END IF;
842
843     -- Gather and insert the field entry data
844     PERFORM metabib.reingest_metabib_field_entries(NEW.id);
845
846     -- Located URI magic
847     IF TG_OP = 'INSERT' THEN
848         PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_located_uri' AND enabled;
849         IF NOT FOUND THEN
850             PERFORM biblio.extract_located_uris( NEW.id, NEW.marc, NEW.editor );
851         END IF;
852     ELSE
853         PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_located_uri' AND enabled;
854         IF NOT FOUND THEN
855             PERFORM biblio.extract_located_uris( NEW.id, NEW.marc, NEW.editor );
856         END IF;
857     END IF;
858
859     -- (re)map metarecord-bib linking
860     IF TG_OP = 'INSERT' THEN -- if not deleted and performing an insert, check for the flag
861         PERFORM * FROM config.internal_flag WHERE name = 'ingest.metarecord_mapping.skip_on_insert' AND enabled;
862         IF NOT FOUND THEN
863             PERFORM metabib.remap_metarecord_for_bib( NEW.id, NEW.fingerprint );
864         END IF;
865     ELSE -- we're doing an update, and we're not deleted, remap
866         PERFORM * FROM config.internal_flag WHERE name = 'ingest.metarecord_mapping.skip_on_update' AND enabled;
867         IF NOT FOUND THEN
868             PERFORM metabib.remap_metarecord_for_bib( NEW.id, NEW.fingerprint );
869         END IF;
870     END IF;
871
872     RETURN NEW;
873 END;
874 $func$ LANGUAGE PLPGSQL;
875
876 COMMIT;