any user with db permissions should be able to call $metadata->store
[cxgn-corelibs.git] / lib / CXGN / Chado / Stock.pm
blob48a1886d639e22bc2833a4e4e5761631fa1d06d5
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;
24 use CXGN::Metadata::Schema;
26 use base qw / CXGN::DB::Object / ;
28 =head2 new
30 Usage: my $stock = CXGN::Chado::Stock->new($schema, $stock_id);
31 Desc:
32 Ret: a CXGN::Chado::Stock object
33 Args: a $schema a schema object,
34 $stock_id, if omitted, an empty stock object is created.
35 Side_Effects: accesses the database, check if exists the database columns that this object use. die if the id is not an integer.
37 =cut
39 sub new {
40 my $class = shift;
41 my $schema = shift;
42 my $id = shift;
44 ### First, bless the class to create the object and set the schema into the object.
45 #my $self = $class->SUPER::new($schema);
46 my $self = bless {}, $class;
47 $self->set_schema($schema);
48 my $stock;
49 if (defined $id) {
50 $stock = $self->get_resultset('Stock::Stock')->find({ stock_id => $id });
51 } else {
52 ### Create an empty resultset object;
53 $stock = $self->get_resultset('Stock::Stock')->new( {} );
55 ###It's important to set the object row for using the accesor in other class functions
56 $self->set_object_row($stock);
57 return $self;
62 =head2 store
64 Usage: $self->store
65 Desc: store a new stock
66 Ret: a database id
67 Args: none
68 Side Effects: checks if the stock exists in the database, and if does, will attempt to update
69 Example:
71 =cut
73 sub store {
74 my $self=shift;
75 my $id = $self->get_stock_id();
76 my $schema=$self->get_schema();
77 #no stock id . Check first if the name exists in te database
78 if (!$id) {
79 my $exists= $self->exists_in_database();
80 if (!$exists) {
81 my $new_row = $self->get_object_row();
82 $new_row->insert();
84 $id=$new_row->stock_id();
85 }else {
86 my $existing_stock=$self->get_resultset('Stock::Stock')->find($exists);
87 #don't update here if stock already exist. User should call from the code exist_in_database
88 #and instantiate a new stock object with the database id
89 #updating here is not a good idea, since it might not be what the user intended to do
90 #and it can mess up the database.
92 }else { # id exists
93 $self->get_object_row()->update();
95 return $id
98 ########################
101 =head2 exists_in_database
103 Usage: $self->exists_in_database()
104 Desc: check if the uniquename exists in the stock table
105 Ret:
106 Args:
107 Side Effects:
108 Example:
110 =cut
112 sub exists_in_database {
113 my $self=shift;
114 my $stock_id = $self->get_stock_id();
115 my $uniquename = $self->get_uniquename || '' ;
116 my ($s) = $self->get_resultset('Stock::Stock')->search(
118 uniquename => { 'ilike' => $uniquename },
120 #loading new stock - $stock_id is undef
121 if (defined($s) && !$stock_id ) { return $s->stock_id ; }
123 #updating an existing stock
124 elsif ($stock_id && defined($s) ) {
125 if ( ($s->stock_id == $stock_id) ) {
126 return 0;
127 #trying to update the uniquename
128 } elsif ( $s->stock_id != $stock_id ) {
129 return " Can't update an existing stock $stock_id uniquename:$uniquename.";
130 # if the new name we're trying to update/insert does not exist in the stock table..
131 } elsif ($stock_id && !$s->stock_id) {
132 return 0;
135 return undef;
138 =head2 get_organism
140 Usage: $self->get_organism
141 Desc: find the organism object of this stock
142 Ret: L<Bio::Chado::Schema::Organism::Organism> object
143 Args: none
144 Side Effects: none
145 Example:
147 =cut
149 sub get_organism {
150 my $self = shift;
151 if (my $bcs_stock = $self->get_object_row) {
152 return $bcs_stock->organism;
154 return undef;
157 =head2 get_type
159 Usage: $self->get_type
160 Desc: find the cvterm type of this stock
161 Ret: L<Bio::Chado::Schema::Cv::Cvterm> object
162 Args: none
163 Side Effects: none
164 Example:
166 =cut
168 sub get_type {
169 my $self = shift;
171 if (my $bcs_stock = $self->get_object_row ) {
172 return $bcs_stock->type;
174 return undef;
180 sub get_object_row {
181 my $self = shift;
182 return $self->{object_row};
185 sub set_object_row {
186 my $self = shift;
187 $self->{object_row} = shift;
190 =head2 get_resultset
192 Usage: $self->get_resultset(ModuleName::TableName)
193 Desc: Get a ResultSet object for source_name
194 Ret: a ResultSet object
195 Args: a source name
196 Side Effects: none
197 Example:
199 =cut
201 sub get_resultset {
202 my $self=shift;
203 my $source = shift;
204 return $self->get_schema()->resultset("$source");
207 =head2 accessors get_schema, set_schema
209 Usage:
210 Desc:
211 Property
212 Side Effects:
213 Example:
215 =cut
217 sub get_schema {
218 my $self = shift;
219 return $self->{schema};
222 sub set_schema {
223 my $self = shift;
224 $self->{schema} = shift;
228 ###mapping accessors to DBIC
230 =head2 accessors get_name, set_name
232 Usage:
233 Desc:
234 Property
235 Side Effects:
236 Example:
238 =cut
240 sub get_name {
241 my $self = shift;
242 return $self->get_object_row()->get_column("name");
245 sub set_name {
246 my $self = shift;
247 $self->get_object_row()->set_column(name => shift);
250 =head2 accessors get_uniquename, set_uniquename
252 Usage:
253 Desc:
254 Property
255 Side Effects:
256 Example:
258 =cut
260 sub get_uniquename {
261 my $self = shift;
262 return $self->get_object_row()->get_column("uniquename");
265 sub set_uniquename {
266 my $self = shift;
267 $self->get_object_row()->set_column(uniquename => shift);
270 =head2 accessors get_organism_id, set_organism_id
272 Usage:
273 Desc:
274 Property
275 Side Effects:
276 Example:
278 =cut
280 sub get_organism_id {
281 my $self = shift;
282 if (my $bcs_stock = $self->get_object_row ) {
283 return $bcs_stock->get_column("organism_id");
285 return undef;
288 sub set_organism_id {
289 my $self = shift;
290 $self->get_object_row()->set_column(organism_id => shift);
293 =head2 accessors get_type_id, set_type_id
295 Usage:
296 Desc:
297 Property
298 Side Effects:
299 Example:
301 =cut
303 sub get_type_id {
304 my $self = shift;
305 if (my $bcs_stock = $self->get_object_row ) {
306 return $bcs_stock->get_column("type_id");
310 sub set_type_id {
311 my $self = shift;
312 $self->get_object_row()->set_column(type_id => shift);
315 =head2 accessors get_description, set_description
317 Usage:
318 Desc:
319 Property
320 Side Effects:
321 Example:
323 =cut
325 sub get_description {
326 my $self = shift;
327 return $self->get_object_row()->get_column("description");
330 sub set_description {
331 my $self = shift;
332 $self->get_object_row()->set_column(description => shift);
335 =head2 accessors get_stock_id, set_stock_id
337 Usage:
338 Desc:
339 Property
340 Side Effects:
341 Example:
343 =cut
345 sub get_stock_id {
346 my $self = shift;
347 if ( my $bcs_stock = $self->get_object_row ) {
348 return $bcs_stock->get_column("stock_id");
350 return undef;
353 sub set_stock_id {
354 my $self = shift;
355 $self->get_object_row()->set_column(stock_id => shift);
358 =head2 accessors get_is_obsolete, set_is_obsolete
360 Usage:
361 Desc:
362 Property
363 Side Effects:
364 Example:
366 =cut
368 sub get_is_obsolete {
369 my $self = shift;
370 my $stock = $self->get_object_row();
371 return $stock->get_column("is_obsolete") if $stock;
374 sub set_is_obsolete {
375 my $self = shift;
376 $self->get_object_row()->set_column(is_obsolete => shift);
379 =head2 function get_image_ids
381 Synopsis: my @images = $self->get_image_ids()
382 Arguments: none
383 Returns: a list of image ids
384 Side effects: none
385 Description: a method for fetching all images associated with a stock
387 =cut
389 sub get_image_ids {
390 my $self = shift;
391 my $ids = $self->get_schema->storage->dbh->selectcol_arrayref
392 ( "SELECT image_id FROM phenome.stock_image WHERE stock_id=? ",
393 undef,
394 $self->get_stock_id
396 return @$ids;
399 =head2 associate_allele
401 Usage: $self->associate_allele($allele_id, $sp_person_id)
402 Desc: store a stock-allele link in phenome.stock_allele
403 Ret: a database id
404 Args: allele_id, sp_person_id
405 Side Effects: store a metadata row
406 Example:
408 =cut
410 sub associate_allele {
411 my $self = shift;
412 my $allele_id = shift;
413 my $sp_person_id = shift;
414 if (!$allele_id || !$sp_person_id) {
415 warn "Need both allele_id and person_id for linking the stock with an allele!";
416 return
418 my $metadata_schema = CXGN::Metadata::Schema->connect(
419 sub { $self->get_schema->storage->dbh },
420 { on_connect_do => ['SET search_path TO metadata;'] },
422 my $metadata = CXGN::Metadata::Metadbdata->new($metadata_schema);
423 $metadata->set_create_person_id($sp_person_id);
424 my $metadata_id = $metadata->store()->get_metadata_id();
425 #check if the allele is already linked
426 my $ids = $self->get_schema->storage->dbh->selectcol_arrayref
427 ( "SELECT stock_allele_id FROM phenome.stock_allele WHERE stock_id = ? AND allele_id = ?",
428 undef,
429 $self->get_stock_id,
430 $allele_id
432 if ($ids) { warn "Allele $allele_id is already linked with stock " . $self->get_stock_id ; }
433 #store the allele_id - stock_id link
434 my $q = "INSERT INTO phenome.stock_allele (stock_id, allele_id, metadata_id) VALUES (?,?,?) RETURNING stock_allele_id";
435 my $sth = $self->get_schema->storage->dbh->prepare($q);
436 $sth->execute($self->get_stock_id, $allele_id, $metadata_id);
437 my ($id) = $sth->fetchrow_array;
438 return $id;
442 ##########
443 1;########
444 ##########