fixed recursive_children cvterm function, and added tests for parents and children
[cxgn-corelibs.git] / lib / CXGN / Chado / Stock.pm
blob221a04d4cd4d20ad7bd9b688b07451859df914ea
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 hte 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
12 use standard DBIC syntax.
14 =head1 AUTHOR
16 Naama Menda <nm249@cornell.edu>
18 =cut
20 package CXGN::Chado::Stock ;
21 use strict;
22 use warnings;
23 use Carp;
24 use Bio::Chado::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;
45 ### First, bless the class to create the object and set the schema into the object.
46 #my $self = $class->SUPER::new($schema);
47 my $self = bless {}, $class;
48 $self->set_schema($schema);
49 my $stock;
50 if (defined $id) {
51 $stock = $self->get_resultset('Stock::Stock')->find({ stock_id => $id });
52 } else {
53 ### Create an empty resultset object;
54 $stock = $self->get_resultset('Stock::Stock')->new( {} );
56 ###It's important to set the object row for using the accesor in other class functions
57 $self->set_object_row($stock);
58 return $self;
63 =head2 store
65 Usage: $self->store
66 Desc: store a new stock
67 Ret: a database id
68 Args: none
69 Side Effects: checks if the stock exists in the database, and if does, will attempt to update
70 Example:
72 =cut
74 sub store {
75 my $self=shift;
76 my $id = $self->get_stock_id();
77 my $schema=$self->get_schema();
78 #no stock id . Check first if the name exists in te database
79 if (!$id) {
80 my $exists= $self->exists_in_database();
81 if (!$exists) {
82 my $new_row = $self->get_object_row();
83 $new_row->insert();
85 $id=$new_row->stock_id();
86 }else {
87 my $existing_stock=$self->get_resultset('Stock::Stock')->find($exists);
88 #don't update here if stock already exist. User should call from the code exist_in_database
89 #and instantiate a new stock object with the database id
90 #updating here is not a good idea, since it might not be what the user intended to do
91 #and it can mess up the database.
93 }else { # id exists
94 $self->get_object_row()->update();
96 return $id
99 ########################
102 =head2 exists_in_database
104 Usage: $self->exists_in_database()
105 Desc: check if the uniquename exists in the stock table
106 Ret:
107 Args:
108 Side Effects:
109 Example:
111 =cut
113 sub exists_in_database {
114 my $self=shift;
115 my $stock_id = $self->get_stock_id();
116 my $uniquename = $self->get_uniquename || '' ;
117 my ($s) = $self->get_resultset('Stock::Stock')->search(
119 uniquename => { 'ilike' => $uniquename },
121 #loading new stock - $stock_id is undef
122 if (defined($s) && !$stock_id ) { return $s->stock_id ; }
124 #updating an existing stock
125 elsif ($stock_id && defined($s) ) {
126 if ( ($s->stock_id == $stock_id) ) {
127 return 0;
128 #trying to update the uniquename
129 } elsif ( $s->stock_id != $stock_id ) {
130 return " Can't update an existing stock $stock_id uniquename:$uniquename.";
131 # if the new name we're trying to update/insert does not exist in the stock table..
132 } elsif ($stock_id && !$s->stock_id) {
133 return 0;
136 return undef;
139 =head2 get_organism
141 Usage: $self->get_organism
142 Desc: find the organism object of this stock
143 Ret: L<Bio::Chado::Schema::Organism::Organism> object
144 Args: none
145 Side Effects: none
146 Example:
148 =cut
150 sub get_organism {
151 my $self = shift;
152 if ($self->get_organism_id) {
153 return $self->get_object_row()->find_related('organism');
155 return undef;
158 =head2 get_type
160 Usage: $self->get_type
161 Desc: find the cvterm type of this stock
162 Ret: L<Bio::Chado::Schema::Cv::Cvterm> object
163 Args: none
164 Side Effects: none
165 Example:
167 =cut
169 sub get_type {
170 my $self = shift;
171 if ($self->get_type_id) {
172 return $self->get_object_row()->find_related('type');
174 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 return $self->get_object_row()->get_column("organism_id");
284 sub set_organism_id {
285 my $self = shift;
286 $self->get_object_row()->set_column(organism_id => shift);
289 =head2 accessors get_type_id, set_type_id
291 Usage:
292 Desc:
293 Property
294 Side Effects:
295 Example:
297 =cut
299 sub get_type_id {
300 my $self = shift;
301 return $self->get_object_row()->get_column("type_id");
304 sub set_type_id {
305 my $self = shift;
306 $self->get_object_row()->set_column(type_id => shift);
309 =head2 accessors get_description, set_description
311 Usage:
312 Desc:
313 Property
314 Side Effects:
315 Example:
317 =cut
319 sub get_description {
320 my $self = shift;
321 return $self->get_object_row()->get_column("description");
324 sub set_description {
325 my $self = shift;
326 $self->get_object_row()->set_column(description => shift);
329 =head2 accessors get_stock_id, set_stock_id
331 Usage:
332 Desc:
333 Property
334 Side Effects:
335 Example:
337 =cut
339 sub get_stock_id {
340 my $self = shift;
341 return $self->get_object_row()->get_column("stock_id");
344 sub set_stock_id {
345 my $self = shift;
346 $self->get_object_row()->set_column(stock_id => shift);
349 =head2 accessors get_is_obsolete, set_is_obsolete
351 Usage:
352 Desc:
353 Property
354 Side Effects:
355 Example:
357 =cut
359 sub get_is_obsolete {
360 my $self = shift;
361 return $self->get_object_row()->get_column("is_obsolete");
364 sub set_is_obsolete {
365 my $self = shift;
366 $self->get_object_row()->set_column(is_obsolete => shift);
371 ##########
372 1;########
373 ##########