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 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.
16 Naama Menda <nm249@cornell.edu>
20 package CXGN
::Chado
::Stock
;
24 use Bio
::Chado
::Schema
;
26 use base qw
/ CXGN::DB::Object / ;
30 Usage: my $stock = CXGN::Chado::Stock->new($schema, $stock_id);
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.
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);
51 $stock = $self->get_resultset('Stock::Stock')->find({ stock_id
=> $id });
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);
66 Desc: store a new stock
69 Side Effects: checks if the stock exists in the database, and if does, will attempt to update
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
80 my $exists= $self->exists_in_database();
82 my $new_row = $self->get_object_row();
85 $id=$new_row->stock_id();
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.
94 $self->get_object_row()->update();
99 ########################
102 =head2 exists_in_database
104 Usage: $self->exists_in_database()
105 Desc: check if the uniquename exists in the stock table
113 sub exists_in_database
{
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) ) {
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) {
141 Usage: $self->get_organism
142 Desc: find the organism object of this stock
143 Ret: L<Bio::Chado::Schema::Organism::Organism> object
152 if ($self->get_organism_id) {
153 return $self->get_object_row()->find_related('organism');
160 Usage: $self->get_type
161 Desc: find the cvterm type of this stock
162 Ret: L<Bio::Chado::Schema::Cv::Cvterm> object
171 if ($self->get_type_id) {
172 return $self->get_object_row()->find_related('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 return $self->get_object_row()->get_column("organism_id");
284 sub set_organism_id
{
286 $self->get_object_row()->set_column(organism_id
=> shift);
289 =head2 accessors get_type_id, set_type_id
301 return $self->get_object_row()->get_column("type_id");
306 $self->get_object_row()->set_column(type_id
=> shift);
309 =head2 accessors get_description, set_description
319 sub get_description
{
321 return $self->get_object_row()->get_column("description");
324 sub set_description
{
326 $self->get_object_row()->set_column(description
=> shift);
329 =head2 accessors get_stock_id, set_stock_id
341 return $self->get_object_row()->get_column("stock_id");
346 $self->get_object_row()->set_column(stock_id
=> shift);
349 =head2 accessors get_is_obsolete, set_is_obsolete
359 sub get_is_obsolete
{
361 return $self->get_object_row()->get_column("is_obsolete");
364 sub set_is_obsolete
{
366 $self->get_object_row()->set_column(is_obsolete
=> shift);