function for finding images linked with the stock
[cxgn-corelibs.git] / lib / CXGN / Chado / Stock.pm
blob1f196233b190194eb959543ed7794ae22b69dd44
1 =head1 NAME
3 CXGN::Chado::Stock - a second-level DBIC Bio::Chado::Schema::Stock::Stock object
5 Version:1.0
7 =head1 DESCRIPTION
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.
13 =head1 AUTHOR
15 Naama Menda <nm249@cornell.edu>
17 =cut
19 package CXGN::Chado::Stock ;
20 use strict;
21 use warnings;
22 use Carp;
23 use Bio::Chado::Schema;
25 use base qw / CXGN::DB::Object / ;
27 =head2 new
29 Usage: my $stock = CXGN::Chado::Stock->new($schema, $stock_id);
30 Desc:
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.
36 =cut
38 sub new {
39 my $class = shift;
40 my $schema = shift;
41 my $id = shift;
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);
47 my $stock;
48 if (defined $id) {
49 $stock = $self->get_resultset('Stock::Stock')->find({ stock_id => $id });
50 } else {
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);
56 return $self;
61 =head2 store
63 Usage: $self->store
64 Desc: store a new stock
65 Ret: a database id
66 Args: none
67 Side Effects: checks if the stock exists in the database, and if does, will attempt to update
68 Example:
70 =cut
72 sub store {
73 my $self=shift;
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
77 if (!$id) {
78 my $exists= $self->exists_in_database();
79 if (!$exists) {
80 my $new_row = $self->get_object_row();
81 $new_row->insert();
83 $id=$new_row->stock_id();
84 }else {
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.
91 }else { # id exists
92 $self->get_object_row()->update();
94 return $id
97 ########################
100 =head2 exists_in_database
102 Usage: $self->exists_in_database()
103 Desc: check if the uniquename exists in the stock table
104 Ret:
105 Args:
106 Side Effects:
107 Example:
109 =cut
111 sub exists_in_database {
112 my $self=shift;
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) ) {
125 return 0;
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) {
131 return 0;
134 return undef;
137 =head2 get_organism
139 Usage: $self->get_organism
140 Desc: find the organism object of this stock
141 Ret: L<Bio::Chado::Schema::Organism::Organism> object
142 Args: none
143 Side Effects: none
144 Example:
146 =cut
148 sub get_organism {
149 my $self = shift;
150 if (my $bcs_stock = $self->get_object_row) {
151 return $bcs_stock->organism;
153 return undef;
156 =head2 get_type
158 Usage: $self->get_type
159 Desc: find the cvterm type of this stock
160 Ret: L<Bio::Chado::Schema::Cv::Cvterm> object
161 Args: none
162 Side Effects: none
163 Example:
165 =cut
167 sub get_type {
168 my $self = shift;
170 if (my $bcs_stock = $self->get_object_row ) {
171 return $bcs_stock->type;
173 return undef;
179 sub get_object_row {
180 my $self = shift;
181 return $self->{object_row};
184 sub set_object_row {
185 my $self = shift;
186 $self->{object_row} = shift;
189 =head2 get_resultset
191 Usage: $self->get_resultset(ModuleName::TableName)
192 Desc: Get a ResultSet object for source_name
193 Ret: a ResultSet object
194 Args: a source name
195 Side Effects: none
196 Example:
198 =cut
200 sub get_resultset {
201 my $self=shift;
202 my $source = shift;
203 return $self->get_schema()->resultset("$source");
206 =head2 accessors get_schema, set_schema
208 Usage:
209 Desc:
210 Property
211 Side Effects:
212 Example:
214 =cut
216 sub get_schema {
217 my $self = shift;
218 return $self->{schema};
221 sub set_schema {
222 my $self = shift;
223 $self->{schema} = shift;
227 ###mapping accessors to DBIC
229 =head2 accessors get_name, set_name
231 Usage:
232 Desc:
233 Property
234 Side Effects:
235 Example:
237 =cut
239 sub get_name {
240 my $self = shift;
241 return $self->get_object_row()->get_column("name");
244 sub set_name {
245 my $self = shift;
246 $self->get_object_row()->set_column(name => shift);
249 =head2 accessors get_uniquename, set_uniquename
251 Usage:
252 Desc:
253 Property
254 Side Effects:
255 Example:
257 =cut
259 sub get_uniquename {
260 my $self = shift;
261 return $self->get_object_row()->get_column("uniquename");
264 sub set_uniquename {
265 my $self = shift;
266 $self->get_object_row()->set_column(uniquename => shift);
269 =head2 accessors get_organism_id, set_organism_id
271 Usage:
272 Desc:
273 Property
274 Side Effects:
275 Example:
277 =cut
279 sub get_organism_id {
280 my $self = shift;
281 if (my $bcs_stock = $self->get_object_row ) {
282 return $bcs_stock->get_column("organism_id");
284 return undef;
287 sub set_organism_id {
288 my $self = shift;
289 $self->get_object_row()->set_column(organism_id => shift);
292 =head2 accessors get_type_id, set_type_id
294 Usage:
295 Desc:
296 Property
297 Side Effects:
298 Example:
300 =cut
302 sub get_type_id {
303 my $self = shift;
304 if (my $bcs_stock = $self->get_object_row ) {
305 return $bcs_stock->get_column("type_id");
309 sub set_type_id {
310 my $self = shift;
311 $self->get_object_row()->set_column(type_id => shift);
314 =head2 accessors get_description, set_description
316 Usage:
317 Desc:
318 Property
319 Side Effects:
320 Example:
322 =cut
324 sub get_description {
325 my $self = shift;
326 return $self->get_object_row()->get_column("description");
329 sub set_description {
330 my $self = shift;
331 $self->get_object_row()->set_column(description => shift);
334 =head2 accessors get_stock_id, set_stock_id
336 Usage:
337 Desc:
338 Property
339 Side Effects:
340 Example:
342 =cut
344 sub get_stock_id {
345 my $self = shift;
346 if ( my $bcs_stock = $self->get_object_row ) {
347 return $bcs_stock->get_column("stock_id");
349 return undef;
352 sub set_stock_id {
353 my $self = shift;
354 $self->get_object_row()->set_column(stock_id => shift);
357 =head2 accessors get_is_obsolete, set_is_obsolete
359 Usage:
360 Desc:
361 Property
362 Side Effects:
363 Example:
365 =cut
367 sub get_is_obsolete {
368 my $self = shift;
369 my $stock = $self->get_object_row();
370 return $stock->get_column("is_obsolete") if $stock;
373 sub set_is_obsolete {
374 my $self = shift;
375 $self->get_object_row()->set_column(is_obsolete => shift);
378 =head2 function get_image_ids
380 Synopsis: my @images = $self->get_image_ids()
381 Arguments: none
382 Returns: a list of image ids
383 Side effects: none
384 Description: a method for fetching all images associated with a stock
386 =cut
388 sub get_image_ids {
389 my $self = shift;
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' ",
392 undef,
393 $self->get_stock_id
395 return @$ids;
398 ##########
399 1;########
400 ##########