]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0340.schema.serials_remove_owner.sql
serial.record_entry already had an owner column spelled "owning_lib"
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0340.schema.serials_remove_owner.sql
1 -- serial.record_entry already had an owner column spelled "owning_lib"
2 -- Adjust the table and affected functions accordingly
3 BEGIN;
4
5 ALTER TABLE serial.record_entry DROP COLUMN owner;
6
7 CREATE OR REPLACE FUNCTION maintain_control_numbers() RETURNS TRIGGER AS $func$
8 use strict;
9 use MARC::Record;
10 use MARC::File::XML;
11 use Encode;
12 use Unicode::Normalize;
13
14 my $record = MARC::Record->new_from_xml($_TD->{new}{marc});
15 my $schema = $_TD->{table_schema};
16 my $rec_id = $_TD->{new}{id};
17
18 # Short-circuit if maintaining control numbers per MARC21 spec is not enabled
19 my $enable = spi_exec_query("SELECT enabled FROM config.global_flag WHERE name = 'cat.maintain_control_numbers'");
20 if (!($enable->{processed}) or $enable->{rows}[0]->{enabled} eq 'f') {
21     return;
22 }
23
24 # Get the control number identifier from an OU setting based on $_TD->{new}{owner}
25 my $ou_cni = 'EVRGRN';
26
27 my $owner;
28 if ($schema eq 'serial') {
29     $owner = $_TD->{new}{owning_lib};
30 } else {
31     # are.owner and bre.owner can be null, so fall back to the consortial setting
32     $owner = $_TD->{new}{owner} || 1;
33 }
34
35 my $ous_rv = spi_exec_query("SELECT value FROM actor.org_unit_ancestor_setting('cat.marc_control_number_identifier', $owner)");
36 if ($ous_rv->{processed}) {
37     $ou_cni = $ous_rv->{rows}[0]->{value};
38     $ou_cni =~ s/"//g; # Stupid VIM syntax highlighting"
39 } else {
40     # Fall back to the shortname of the OU if there was no OU setting
41     $ous_rv = spi_exec_query("SELECT shortname FROM actor.org_unit WHERE id = $owner");
42     if ($ous_rv->{processed}) {
43         $ou_cni = $ous_rv->{rows}[0]->{shortname};
44     }
45 }
46
47 my ($create, $munge) = (0, 0);
48 my ($orig_001, $orig_003) = ('', '');
49
50 # Incoming MARC records may have multiple 001s or 003s, despite the spec
51 my @control_ids = $record->field('003');
52 my @scns = $record->field('035');
53
54 foreach my $id_field ('001', '003') {
55     my $spec_value;
56     my @controls = $record->field($id_field);
57
58     if ($id_field eq '001') {
59         $spec_value = $rec_id;
60     } else {
61         $spec_value = $ou_cni;
62     }
63
64     # Create the 001/003 if none exist
65     if (scalar(@controls) == 0) {
66         $record->insert_fields_ordered(MARC::Field->new($id_field, $spec_value));
67         $create = 1;
68     } elsif (scalar(@controls) > 1) {
69         # Do we already have the right 001/003 value in the existing set?
70         unless (grep $_->data() eq $spec_value, @controls) {
71             $munge = 1;
72         }
73
74         # Delete the other fields, as with more than 1 001/003 we do not know which 003/001 to match
75         foreach my $control (@controls) {
76             unless ($control->data() eq $spec_value) {
77                 $record->delete_field($control);
78             }
79         }
80     } else {
81         # Only one field; check to see if we need to munge it
82         unless (grep $_->data() eq $spec_value, @controls) {
83             $munge = 1;
84         }
85     }
86 }
87
88 # Now, if we need to munge the 001, we will first push the existing 001/003 into the 035
89 if ($munge) {
90     my $scn = "(" . $record->field('003')->data() . ")" . $record->field('001')->data();
91
92     # Do not create duplicate 035 fields
93     unless (grep $_->subfield('a') eq $scn, @scns) {
94         $record->insert_fields_ordered(MARC::Field->new('035', '', '', 'a' => $scn));
95     }
96 }
97
98 # Set the 001/003 and update the MARC
99 if ($create or $munge) {
100     $record->field('001')->data($rec_id);
101     $record->field('003')->data($ou_cni);
102
103     my $xml = $record->as_xml_record();
104     $xml =~ s/\n//sgo;
105     $xml =~ s/^<\?xml.+\?\s*>//go;
106     $xml =~ s/>\s+</></go;
107     $xml =~ s/\p{Cc}//go;
108
109     # Embed a version of OpenILS::Application::AppUtils->entityize()
110     # to avoid having to set PERL5LIB for PostgreSQL as well
111
112     # If we are going to convert non-ASCII characters to XML entities,
113     # we had better be dealing with a UTF8 string to begin with
114     $xml = decode_utf8($xml);
115
116     $xml = NFC($xml);
117
118     # Convert raw ampersands to entities
119     $xml =~ s/&(?!\S+;)/&amp;/gso;
120
121     # Convert Unicode characters to entities
122     $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
123
124     $xml =~ s/[\x00-\x1f]//go;
125     $_TD->{new}{marc} = $xml;
126
127     return "MODIFY";
128 }
129
130 return;
131 $func$ LANGUAGE PLPERLU;
132
133 COMMIT;