3 CXGN::Chado::Stock - a second-level DBIC Bio::Chado::Schema::Stock::Stock object
9 Created to work with CXGN::Page::Form::AjaxFormPage
10 for eliminating the need to refactor the AjaxFormPage and Editable to work with DBIC objects.
11 Functions such as 'get_obsolete' , 'store' , and 'exists_in_database' are required , and do not use standard DBIC syntax.
15 Naama Menda <nm249@cornell.edu>
19 package CXGN
::Chado
::Stock
;
23 use Bio
::Chado
::Schema
;
25 use base qw
/ CXGN::DB::Object / ;
29 Usage: my $stock = CXGN::Chado::Stock->new($schema, $stock_id);
31 Ret: a CXGN::Chado::Stock object
32 Args: a $schema a schema object,
33 $stock_id, if omitted, an empty stock object is created.
34 Side_Effects: accesses the database, check if exists the database columns that this object use. die if the id is not an integer.
43 ### First, bless the class to create the object and set the schema into the object.
44 #my $self = $class->SUPER::new($schema);
45 my $self = bless {}, $class;
46 $self->set_schema($schema);
49 $stock = $self->get_resultset('Stock::Stock')->find({ stock_id
=> $id });
51 ### Create an empty resultset object;
52 $stock = $self->get_resultset('Stock::Stock')->new( {} );
54 ###It's important to set the object row for using the accesor in other class functions
55 $self->set_object_row($stock);
64 Desc: store a new stock
67 Side Effects: checks if the stock exists in the database, and if does, will attempt to update
74 my $id = $self->get_stock_id();
75 my $schema=$self->get_schema();
76 #no stock id . Check first if the name exists in te database
78 my $exists= $self->exists_in_database();
80 my $new_row = $self->get_object_row();
83 $id=$new_row->stock_id();
85 my $existing_stock=$self->get_resultset('Stock::Stock')->find($exists);
86 #don't update here if stock already exist. User should call from the code exist_in_database
87 #and instantiate a new stock object with the database id
88 #updating here is not a good idea, since it might not be what the user intended to do
89 #and it can mess up the database.
92 $self->get_object_row()->update();
97 ########################
100 =head2 exists_in_database
102 Usage: $self->exists_in_database()
103 Desc: check if the uniquename exists in the stock table
111 sub exists_in_database
{
113 my $stock_id = $self->get_stock_id();
114 my $uniquename = $self->get_uniquename || '' ;
115 my ($s) = $self->get_resultset('Stock::Stock')->search(
117 uniquename
=> { 'ilike' => $uniquename },
119 #loading new stock - $stock_id is undef
120 if (defined($s) && !$stock_id ) { return $s->stock_id ; }
122 #updating an existing stock
123 elsif ($stock_id && defined($s) ) {
124 if ( ($s->stock_id == $stock_id) ) {
126 #trying to update the uniquename
127 } elsif ( $s->stock_id != $stock_id ) {
128 return " Can't update an existing stock $stock_id uniquename:$uniquename.";
129 # if the new name we're trying to update/insert does not exist in the stock table..
130 } elsif ($stock_id && !$s->stock_id) {
139 Usage: $self->get_organism
140 Desc: find the organism object of this stock
141 Ret: L<Bio::Chado::Schema::Organism::Organism> object
150 if (my $bcs_stock = $self->get_object_row) {
151 return $bcs_stock->organism;
158 Usage: $self->get_type
159 Desc: find the cvterm type of this stock
160 Ret: L<Bio::Chado::Schema::Cv::Cvterm> object
170 if (my $bcs_stock = $self->get_object_row ) {
171 return $bcs_stock->type;
181 return $self->{object_row
};
186 $self->{object_row
} = shift;
191 Usage: $self->get_resultset(ModuleName::TableName)
192 Desc: Get a ResultSet object for source_name
193 Ret: a ResultSet object
203 return $self->get_schema()->resultset("$source");
206 =head2 accessors get_schema, set_schema
218 return $self->{schema
};
223 $self->{schema
} = shift;
227 ###mapping accessors to DBIC
229 =head2 accessors get_name, set_name
241 return $self->get_object_row()->get_column("name");
246 $self->get_object_row()->set_column(name
=> shift);
249 =head2 accessors get_uniquename, set_uniquename
261 return $self->get_object_row()->get_column("uniquename");
266 $self->get_object_row()->set_column(uniquename
=> shift);
269 =head2 accessors get_organism_id, set_organism_id
279 sub get_organism_id
{
281 if (my $bcs_stock = $self->get_object_row ) {
282 return $bcs_stock->get_column("organism_id");
287 sub set_organism_id
{
289 $self->get_object_row()->set_column(organism_id
=> shift);
292 =head2 accessors get_type_id, set_type_id
304 if (my $bcs_stock = $self->get_object_row ) {
305 return $bcs_stock->get_column("type_id");
311 $self->get_object_row()->set_column(type_id
=> shift);
314 =head2 accessors get_description, set_description
324 sub get_description
{
326 return $self->get_object_row()->get_column("description");
329 sub set_description
{
331 $self->get_object_row()->set_column(description
=> shift);
334 =head2 accessors get_stock_id, set_stock_id
346 if ( my $bcs_stock = $self->get_object_row ) {
347 return $bcs_stock->get_column("stock_id");
354 $self->get_object_row()->set_column(stock_id
=> shift);
357 =head2 accessors get_is_obsolete, set_is_obsolete
367 sub get_is_obsolete
{
369 my $stock = $self->get_object_row();
370 return $stock->get_column("is_obsolete") if $stock;
373 sub set_is_obsolete
{
375 $self->get_object_row()->set_column(is_obsolete
=> shift);
378 =head2 function get_image_ids
380 Synopsis: my @images = $self->get_image_ids()
382 Returns: a list of image ids
384 Description: a method for fetching all images associated with a stock
390 my $ids = $self->get_schema->storage->dbh->selectcol_arrayref
391 ( "SELECT value FROM stockprop JOIN cvterm on cvterm.cvterm_id = stockprop.type_id WHERE stock_id=? AND cvterm.name = 'sgn image_id' ",