1 -- serial.record_entry already had an owner column spelled "owning_lib"
2 -- Adjust the table and affected functions accordingly
5 ALTER TABLE serial.record_entry DROP COLUMN owner;
7 CREATE OR REPLACE FUNCTION maintain_control_numbers() RETURNS TRIGGER AS $func$
12 use Unicode::Normalize;
14 my $record = MARC::Record->new_from_xml($_TD->{new}{marc});
15 my $schema = $_TD->{table_schema};
16 my $rec_id = $_TD->{new}{id};
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') {
24 # Get the control number identifier from an OU setting based on $_TD->{new}{owner}
25 my $ou_cni = 'EVRGRN';
28 if ($schema eq 'serial') {
29 $owner = $_TD->{new}{owning_lib};
31 # are.owner and bre.owner can be null, so fall back to the consortial setting
32 $owner = $_TD->{new}{owner} || 1;
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"
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};
47 my ($create, $munge) = (0, 0);
48 my ($orig_001, $orig_003) = ('', '');
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');
54 foreach my $id_field ('001', '003') {
56 my @controls = $record->field($id_field);
58 if ($id_field eq '001') {
59 $spec_value = $rec_id;
61 $spec_value = $ou_cni;
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));
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) {
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);
81 # Only one field; check to see if we need to munge it
82 unless (grep $_->data() eq $spec_value, @controls) {
88 # Now, if we need to munge the 001, we will first push the existing 001/003 into the 035
90 my $scn = "(" . $record->field('003')->data() . ")" . $record->field('001')->data();
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));
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);
103 my $xml = $record->as_xml_record();
105 $xml =~ s/^<\?xml.+\?\s*>//go;
106 $xml =~ s/>\s+</></go;
107 $xml =~ s/\p{Cc}//go;
109 # Embed a version of OpenILS::Application::AppUtils->entityize()
110 # to avoid having to set PERL5LIB for PostgreSQL as well
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);
118 # Convert raw ampersands to entities
119 $xml =~ s/&(?!\S+;)/&/gso;
121 # Convert Unicode characters to entities
122 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
124 $xml =~ s/[\x00-\x1f]//go;
125 $_TD->{new}{marc} = $xml;
131 $func$ LANGUAGE PLPERLU;