Merge branch 'master' into topic/parent_string
[cxgn-corelibs.git] / lib / MOBY / secondary_input.pm
blob52e4b79a563a08c65314a7f36e1299024cd7c662
1 #!/usr/bin/perl -w
2 package MOBY::secondary_input;
3 use strict;
4 use Carp;
5 use MOBY::Config;
6 use vars qw($AUTOLOAD @ISA);
8 =head1 NAME
10 MOBY::secondary_input - a lightweight connection to the
11 secondary_input table in the database
13 =head1 SYNOPSIS
15 NON FUNCTIONAL AT THIS TIME
16 use MOBY::secondary_input;
17 my $Instance = MOBY::secondary_input->new(
18 object_type => "Sequence",
19 namespaces => ["genbank/gi", "genbank/Acc"],
20 article_name => "InputSequenceThingy",
24 =cut
26 =head1 DESCRIPTION
28 representation of the secondary_input table. Can write to the database
30 =head1 AUTHORS
32 Mark Wilkinson (mwilkinson@gene.pbi.nrc.ca)
35 =cut
39 # Encapsulated:
40 # DATA
41 #___________________________________________________________
42 #ATTRIBUTES
43 my %_attr_data = # DEFAULT ACCESSIBILITY
45 secondary_input_id => [ undef, 'read/write' ],
46 default_value => [ undef, 'read/write' ],
47 maximum_value => [ undef, 'read/write' ],
48 minimum_value => [ undef, 'read/write' ],
49 enum_value => [ undef, 'read/write' ],
50 datatype => [ undef, 'read/write' ],
51 article_name => [ undef, 'read/write' ],
52 service_instance_id => [ undef, 'read/write' ],
53 service_instance_lsid => [ undef, 'read/write' ],
54 dbh => [ undef, 'read/write' ],
57 #_____________________________________________________________
58 # METHODS, to operate on encapsulated class data
59 # Is a specified object attribute accessible in a given mode
60 sub _accessible {
61 my ( $self, $attr, $mode ) = @_;
62 $_attr_data{$attr}[1] =~ /$mode/;
65 # Classwide default value for a specified object attribute
66 sub _default_for {
67 my ( $self, $attr ) = @_;
68 $_attr_data{$attr}[0];
71 # List of names of all specified object attributes
72 sub _standard_keys {
73 keys %_attr_data;
77 sub _dbh {
78 my ($self) = @_;
79 my $central_connect = MOBY::central_db_connection->new();
80 $self->dbh( $central_connect->dbh );
81 return $central_connect->dbh;
84 sub new {
85 my ( $caller, %args ) = @_;
86 my $caller_is_obj = ref($caller);
87 return $caller if $caller_is_obj;
88 my $class = $caller_is_obj || $caller;
89 my $proxy;
90 my $self = bless {}, $class;
91 foreach my $attrname ( $self->_standard_keys ) {
92 if ( exists $args{$attrname} ) {
93 $self->{$attrname} = $args{$attrname};
94 } elsif ($caller_is_obj) {
95 $self->{$attrname} = $caller->{$attrname};
96 } else {
97 $self->{$attrname} = $self->_default_for($attrname);
100 my $datatype = $self->datatype;
101 unless ( ( $datatype =~ /Integer/ )
102 || ( $datatype =~ /Float/ )
103 || ( $datatype =~ /String/ )
104 || ( $datatype =~ /DateTime/ ) )
106 return undef;
108 my $id = $self->WRITE;
109 $self->secondary_input_id($id) if defined $id;
110 return $self;
113 sub WRITE {
114 my ($self) = @_;
115 $CONFIG ||= MOBY::Config->new; # exported by Config.pm
116 my $adaptor = $CONFIG->getDataAdaptor( datasource => 'mobycentral' );
117 my $dbh = $self->dbh;
118 my $insertid = $adaptor->insert_secondary_input(default_value => $self->default_value,
119 maximum_value => $self->maximum_value,
120 minimum_value => $self->minimum_value,
121 enum_value => $self->enum_value,
122 datatype => $self->datatype,
123 article_name => $self->article_name,
124 service_instance_lsid => $self->service_instance_lsid);
126 return $insertid;
129 sub AUTOLOAD {
130 no strict "refs";
131 my ( $self, $newval ) = @_;
132 $AUTOLOAD =~ /.*::(\w+)/;
133 my $attr = $1;
134 if ( $self->_accessible( $attr, 'write' ) ) {
135 *{$AUTOLOAD} = sub {
136 if ( defined $_[1] ) { $_[0]->{$attr} = $_[1] }
137 return $_[0]->{$attr};
138 }; ### end of created subroutine
139 ### this is called first time only
140 if ( defined $newval ) {
141 $self->{$attr} = $newval;
143 return $self->{$attr};
144 } elsif ( $self->_accessible( $attr, 'read' ) ) {
145 *{$AUTOLOAD} = sub {
146 return $_[0]->{$attr};
147 }; ### end of created subroutine
148 return $self->{$attr};
151 # Must have been a mistake then...
152 croak "No such method: $AUTOLOAD";
154 sub DESTROY { }