4 package CXGN
::Phenome
::Stock
::StockForm
;
5 my $stock_form = CXGN
::Phenome
::Stock
::StockForm
->new();
7 use base qw
/CXGN::Page::Form::AjaxFormPage /;
10 use CXGN
::Tools
::Organism
;
11 use Bio
::Chado
::Schema
;
14 use CXGN
::People
::Person
;
21 use CatalystX
::GlobalContext
qw( $c );
26 my $self = $class->SUPER::new(@_);
32 my %args = $self->get_args();
33 my $stock_id = $args{stock_id} || $args{object_id};
34 my $user_type = $self->get_user()->get_user_type();
35 my %json_hash= $self->get_json_hash();
36 my $sp_person_id = $self->get_user()->get_sp_person_id();
38 print STDERR "this is sp_person_id before dbic_schema: ".$sp_person_id."\n";
40 my $schema = $c->dbic_schema( 'Bio::Chado::Schema', 'sgn_chado', $sp_person_id);
42 $self->set_object_id($stock_id);
43 $self->set_object_name('Stock'); #this is useful for email messages
44 $self->set_object( CXGN::Stock->new({schema=>$schema, stock_id=>$stock_id}) );
46 if ( $self->get_object()->is_obsolete() == 1 && $user_type ne 'curator' )
48 $json_hash{error}="Stock $stock_id is obsolete!";
50 unless ( ( $stock_id =~ m /^\d+$/ || !$stock_id ) ) {
51 $json_hash{error}="No stock exists for identifier $stock_id";
53 $self->set_json_hash(%json_hash);
54 $self->set_primary_key("stock_id");
56 #my @owners = $self->get_object()->search_related("stockprops", {
57 #type_id => $sp_person_cvterm_id } );
59 $self->set_owners( @owners );
61 $self->print_json() if $json_hash{error};
68 my $stock = $self->get_object();
69 my $stock_id = $self->get_object_id();
70 my %args = $self->get_args();
71 my %json_hash = $self->get_json_hash();
72 my $initial_stock_id = $stock_id;
75 $stock->species($args{organism});
76 $stock->type_id($args{type_id});
77 $stock->name($args{uniquename});
78 $stock->uniquename($args{uniquename});
79 $stock->description($args{description});
82 my $message = $stock->exists_in_database();
85 $error = " Stock $args{uniquename} already exists in the database ";
88 $self->SUPER::store(); #this sets $json_hash{validate} if the form validation failed.
89 $stock_id = $stock->stock_id() ;
91 $error = " An error occurred. Cannot store to the database\n An email message has been sent to the SGN development team";
92 CXGN::Contact::send_email('stock_ajax_form.pl died', $error . "\n" . $_ , 'sgn-bugs@sgn.cornell.edu');
95 #the validate field is false is validation passed for all fields, true if did not pass and the form is re-printed
97 %json_hash= $self->get_json_hash();
98 $validate= $json_hash{validate};
99 $json_hash{error} = $error if $error;
101 my $refering_page="/stock/$stock_id/view";
102 $self->send_form_email({subject=>"[New stock details stored] stock $stock_id", mailing_list=>'sgn-db-curation@sgn.cornell.edu', refering_page=>"www.solgenomics.net".$refering_page}) if (!$validate && !$json_hash{error});
103 $json_hash{refering_page}=$refering_page if !$initial_stock_id && !$validate && !$error;
105 $self->set_json_hash(%json_hash);
111 ####################################
113 ##Delete the stock (actually set obsolete = 't')
115 my $check = $self->check_modify_privileges();
116 $self->print_json() if $check ; #error or no user privileges
118 my $stock = $self->get_object();
119 my $stock_name = $stock->uniquename();
120 my $stock_id = $stock->stock_id();
121 my %json_hash= $self->get_json_hash();
122 my $refering_page="/stock/$stock_id/view";
124 if (!$json_hash{error} ) {
126 $stock->is_obsolete(1) ;
129 $json_hash{error} = " An error occurred. Cannot delete stock\n An email message has been sent to the SGN development team";
130 $self->send_form_email({subject=>"Stock delete failed! ($stock_name) $_", mailing_list=>'sgn-db-curation@sgn.cornell.edu', refering_page=>"www.solgenomics.net".$refering_page, action=>'delete'});
132 $json_hash{reload} = 1;
134 $self->send_form_email({subject=>"Stock obsoleted ($stock_name)", mailing_list=>'sgn-db-curation@sgn.cornell.edu', refering_page=>"www.solgenomics.net".$refering_page, action=>'delete'}) if (!$json_hash{error});
135 $self->set_json_hash(%json_hash);
143 my $form_id = 'edit_stock'; # a form_id is required for ajax forms
145 $self->init_form($form_id) ; ## instantiate static/editable/confirmStore form
146 my $stock = $self->get_object();
147 my %args = $self->get_args();
148 my $form = $self->get_form();
151 my $species = $stock->get_species() ;
152 my $organism_id = $species ? $stock->stock->organism_id : undef ;
153 my ($stock_type) = $stock->get_schema->resultset("Cv::Cv")->search(
154 { name => 'stock_type' , }
159 my $cvterm_res = $stock_type->
160 search_related('cvterms');
161 while (my $cvterm = $cvterm_res->next() ) {
162 push @types, $cvterm->name() ;
163 push @type_ids, $cvterm->cvterm_id();
168 if ( $self->get_action =~ /new|store|edit/ ) {
170 display_name => "Organism",
171 field_name => "Organism",
172 id => "species_name",
174 getter => "get_species",
175 setter => "set_species",
176 autocomplete => '/ajax/organism/autocomplete',
178 $self->get_form->add_select(
179 display_name => "Stock type",
180 field_name => "type_id",
181 contents => $stock->type_id(),
186 select_list_ref => \@types,
187 select_id_list_ref => \@type_ids,
190 if ( $stock->is_obsolete() ) {
192 display_name => "Status",
193 field_name => "obsolete_stat",
194 contents => 'OBSOLETE',
197 if ( $self->get_action =~ /view/ ) {
199 display_name => "Organism",
200 field_name => "stock_organism",
201 contents => $species ? qq|<a href="/organism/$organism_id/view"> | . $species . "</a>" : '',
204 display_name => "Stock type",
205 field_name => "stock_type",
206 contents => $stock->type() ,
211 display_name => "Uniquename ",
212 field_name => "uniquename",
214 getter => "uniquename",
215 setter => "uniquename",
216 validate => 'string',
220 display_name => "Description",
221 field_name => "description",
223 getter => "description",
224 setter => "description",
230 field_name => "stock_id",
231 contents => $stock->stock_id(),
234 field_name => "sp_person_id",
235 contents => $self->get_user()->get_sp_person_id(),
237 setter => "sp_person_id",
240 field_name => "action",
243 if ($self->get_action =~ /view/ ) {
245 field_name => "type_id",
246 contents => $stock->type_id,
249 if ( $self->get_action() =~ /view|edit/ ) {
250 $form->from_database();
252 elsif ( $self->get_action() =~ /store/ ) {
253 my %json_hash = $self->get_json_hash() ;
254 print $json_hash{html} ;
255 $form->from_request( %args );