| 1 | #!/usr/bin/perl -w | ||
| 2 | package MOBY::authority; | ||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | use MOBY::Config; | ||
| 6 | use vars qw($AUTOLOAD @ISA); | ||
| 7 | |||
| 8 | use vars qw /$VERSION/; | ||
| 9 | $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | MOBY::authority - a lightweight connection to the | ||
| 14 | authority table in the database | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use MOBY::authority; | ||
| 19 | my $Instance = MOBY::authority->new( | ||
| 20 | authority_common_name => "genbank", | ||
| 21 | authority_uri => "ncbi.nlm.nih.gov", | ||
| 22 | contact_email => "mr.BIG@ncbi.nlm.nih.gov", | ||
| 23 | |||
| 24 | ); | ||
| 25 | print $Instance->authority_id; | ||
| 26 | |||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | representation of the authority table. Can write to the database | ||
| 33 | |||
| 34 | =head1 AUTHORS | ||
| 35 | |||
| 36 | Mark Wilkinson (mwilkinson@gene.pbi.nrc.ca) | ||
| 37 | |||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | { | ||
| 42 | |||
| 43 | # Encapsulated: | ||
| 44 | # DATA | ||
| 45 | #___________________________________________________________ | ||
| 46 | #ATTRIBUTES | ||
| 47 | my %_attr_data = # DEFAULT ACCESSIBILITY | ||
| 48 | ( | ||
| 49 | authority_common_name => [ undef, 'read/write' ], | ||
| 50 | authority_uri => [ undef, 'read/write' ], | ||
| 51 | contact_email => [ undef, 'read/write' ], | ||
| 52 | dbh => [ undef, 'read/write' ], | ||
| 53 | ); | ||
| 54 | |||
| 55 | #_____________________________________________________________ | ||
| 56 | # METHODS, to operate on encapsulated class data | ||
| 57 | # Is a specified object attribute accessible in a given mode | ||
| 58 | sub _accessible { | ||
| 59 | my ( $self, $attr, $mode ) = @_; | ||
| 60 | $_attr_data{$attr}[1] =~ /$mode/; | ||
| 61 | } | ||
| 62 | |||
| 63 | # Classwide default value for a specified object attribute | ||
| 64 | sub _default_for { | ||
| 65 | my ( $self, $attr ) = @_; | ||
| 66 | $_attr_data{$attr}[0]; | ||
| 67 | } | ||
| 68 | |||
| 69 | # List of names of all specified object attributes | ||
| 70 | sub _standard_keys { | ||
| 71 | keys %_attr_data; | ||
| 72 | } | ||
| 73 | sub authority_id { | ||
| 74 | die "AUTHORITY_ID is deprecated. fix your code!\n"; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | sub new { | ||
| 79 | my ( $caller, %args ) = @_; | ||
| 80 | my $caller_is_obj = ref($caller); | ||
| 81 | return $caller if $caller_is_obj; | ||
| 82 | my $class = $caller_is_obj || $caller; | ||
| 83 | my $proxy; | ||
| 84 | my $self = bless {}, $class; | ||
| 85 | foreach my $attrname ( $self->_standard_keys ) { | ||
| 86 | if ( exists $args{$attrname} ) { | ||
| 87 | $self->{$attrname} = $args{$attrname}; | ||
| 88 | } elsif ($caller_is_obj) { | ||
| 89 | $self->{$attrname} = $caller->{$attrname}; | ||
| 90 | } else { | ||
| 91 | $self->{$attrname} = $self->_default_for($attrname); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | my $dbh = $self->dbh; | ||
| 95 | $CONFIG ||= MOBY::Config->new; # exported by Config.pm | ||
| 96 | my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' ); | ||
| 97 | |||
| 98 | my $result = $adaptor->query_authority(authority_uri => $self->authority_uri); | ||
| 99 | my $row = shift(@$result); | ||
| 100 | unless ($row) { | ||
| 101 | my $insertid = $adaptor->insert_authority( | ||
| 102 | authority_common_name => $self->authority_common_name, | ||
| 103 | authority_uri => $self->authority_uri, | ||
| 104 | contact_email => $self->contact_email); | ||
| 105 | } else { | ||
| 106 | $self->authority_common_name($row->{authority_common_name}); | ||
| 107 | $self->authority_uri($row->{authority_uri}); | ||
| 108 | $self->contact_email($row->{contact_email}); | ||
| 109 | } | ||
| 110 | return $self; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub AUTOLOAD { | ||
| 114 | no strict "refs"; | ||
| 115 | my ( $self, $newval ) = @_; | ||
| 116 | $AUTOLOAD =~ /.*::(\w+)/; | ||
| 117 | my $attr = $1; | ||
| 118 | if ( $self->_accessible( $attr, 'write' ) ) { | ||
| 119 | *{$AUTOLOAD} = sub { | ||
| 120 | if ( defined $_[1] ) { $_[0]->{$attr} = $_[1] } | ||
| 121 | return $_[0]->{$attr}; | ||
| 122 | }; ### end of created subroutine | ||
| 123 | ### this is called first time only | ||
| 124 | if ( defined $newval ) { | ||
| 125 | $self->{$attr} = $newval; | ||
| 126 | } | ||
| 127 | return $self->{$attr}; | ||
| 128 | } elsif ( $self->_accessible( $attr, 'read' ) ) { | ||
| 129 | *{$AUTOLOAD} = sub { | ||
| 130 | return $_[0]->{$attr}; | ||
| 131 | }; ### end of created subroutine | ||
| 132 | return $self->{$attr}; | ||
| 133 | } | ||
| 134 | |||
| 135 | # Must have been a mistake then... | ||
| 136 | croak "No such method: $AUTOLOAD"; | ||
| 137 | } | ||
| 138 | sub DESTROY { } | ||
| 139 | 1; |