This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Namespaces.pm
210 lines (146 loc) · 5.34 KB
/
Namespaces.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package Namespaces;
=head1 NAME
Namespaces
=head1 DESCRIPTION
This package provides the interface and access logic to
namespace-specific data.
CREATE TABLE `ht_namespaces` (
`namespace` varchar(8) NOT NULL,
`institution` varchar(255) DEFAULT NULL,
`grin_instance` varchar(32) DEFAULT NULL,
`default_source` varchar(32) DEFAULT NULL,
PRIMARY KEY (`namespace`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
=head1 SYNOPSIS
Coding example
=head1 METHODS
=over 8
=cut
use strict;
use warnings;
use Context;
use Database;
use DbUtils;
use Identifier;
# ---------------------------------------------------------------------
=item ___get_N_ref, ___set_N_ref
Needed for persistent clients, e.g. imgsrv.
=cut
# ---------------------------------------------------------------------
sub ___get_N_ref {
my $C = shift;
my $Namespace_Hash_ref = ( $C->has_object('Namespaces') ? $C->get_object('Namespaces') : {} );
return $Namespace_Hash_ref;
}
sub ___set_N_ref {
my ($C, $n_ref) = @_;
bless $n_ref, 'Namespaces';
$C->set_object('Namespaces', $n_ref);
}
# ---------------------------------------------------------------------
=item __load_namespace_hash
Description
=cut
# ---------------------------------------------------------------------
sub __load_namespace_hash {
my $C = shift;
my $Namespace_Hash_ref = ___get_N_ref($C);
return if (scalar keys %$Namespace_Hash_ref);
my $dbh = $C->get_object('Database')->get_DBH;
my $statement = qq{SELECT * FROM ht_namespaces};
my $sth = DbUtils::prep_n_execute($dbh, $statement);
my $ref_to_arr_of_hashref = $sth->fetchall_arrayref({});
my %Namespace_Hash = map { $_->{namespace},
{
institution => $_->{institution},
grin_instance => $_->{grin_instance},
}
} @$ref_to_arr_of_hashref;
___set_N_ref($C, \%Namespace_Hash);
}
# ---------------------------------------------------------------------
=item get_institution_by_namespace
Description
=cut
# ---------------------------------------------------------------------
sub get_institution_by_namespace {
my ($C, $id) = @_;
__load_namespace_hash($C);
my ($namespace, $barcode) = Identifier::split_id($id);
my $Namespace_Hash_ref = ___get_N_ref($C);
return $Namespace_Hash_ref->{$namespace}->{institution};
}
# ---------------------------------------------------------------------
=item get_google_id_by_namespace
Description
=cut
# ---------------------------------------------------------------------
sub get_google_id_by_namespace {
my ($C, $id) = @_;
__load_namespace_hash($C);
my ($namespace, $barcode) = Identifier::split_id($id);
my $Namespace_Hash_ref = ___get_N_ref($C);
my $grin_prefix =
__map_UCAL_GRIN_prefix(
$Namespace_Hash_ref->{$namespace}{grin_instance},
$barcode
);
return (defined $grin_prefix) ? $grin_prefix . ':' . ( uc $barcode ) : undef;
}
# ---------------------------------------------------------------------
=item __map_uc1_2_GRIN_prefix
Description
=cut
# ---------------------------------------------------------------------
sub __map_UCAL_GRIN_prefix {
my ($grin_prefix, $barcode) = @_;
return $grin_prefix
unless (defined $grin_prefix && $grin_prefix eq 'UCAL');
# From ht_to_grin.rb aelkiss
return 'UCSC'
if ( $barcode =~ m/^32106\d{9}$/ );
return 'UCSD'
if ( $barcode =~ m/^31822\d{9}$/ );
return 'UCSF'
if ( $barcode =~ m/^31378\d{9}$/ );
return 'UCD'
if ( $barcode =~ m/^31175\d{9}$/ );
return 'UCR'
if ( $barcode =~ m/^31210\d{9}$/ );
return 'UCLA'
if ( $barcode =~ m/^l\d{10}|31158\d{9}$/ );
return 'SRLF'
if ( $barcode =~ m/^a{1,3}\d{9}/ );
# Nothing from these campuses?
return 'UCI'
if ( $barcode =~ m/^.1970\d{9}$/ );
return 'UCSB'
if ( $barcode =~ m/^.1205\d{9}$/ );
return 'UCBK'
if ( length($barcode) == 10 );
# No matches
return 'UCAL';
}
1;
__END__
=head1 AUTHOR
Phillip Farber, University of Michigan, [email protected]
=head1 COPYRIGHT
Copyright 2012-14 ©, The Regents of The University of Michigan, All Rights Reserved
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
=cut