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