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