Skip to content

Commit

Permalink
fixed merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
titima15 committed Oct 25, 2023
2 parents efb9a9e + 4cffd0d commit 0d59bc8
Show file tree
Hide file tree
Showing 15 changed files with 815 additions and 34 deletions.
74 changes: 74 additions & 0 deletions db/00181/AddDiscardedMetadataCvterm.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env perl


=head1 NAME
AddDiscardedMetadataCvterm
=head1 SYNOPSIS
mx-run AddDiscardedMetadataCvterm [options] -H hostname -D dbname -u username [-F]
this is a subclass of L<CXGN::Metadata::Dbpatch>
see the perldoc of parent class for more details.
=head1 DESCRIPTION
This patch adds discarded_metadata stock_property cvterm
This subclass uses L<Moose>. The parent class uses L<MooseX::Runnable>
=head1 AUTHOR
Titima Tantikanjana <[email protected]>
=head1 COPYRIGHT & LICENSE
Copyright 2010 Boyce Thompson Institute for Plant Research
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut


package AddDiscardedMetadataCvterm;

use Moose;
use Bio::Chado::Schema;
use Try::Tiny;
extends 'CXGN::Metadata::Dbpatch';


has '+description' => ( default => <<'' );
This patch adds the 'discarded_metadata' stock_property cvterm
has '+prereq' => (
default => sub {
[],
},

);

sub patch {
my $self=shift;

print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";

print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";

print STDOUT "\nExecuting the SQL commands.\n";
my $schema = Bio::Chado::Schema->connect( sub { $self->dbh->clone } );

print STDERR "INSERTING CV TERMS...\n";

$schema->resultset("Cv::Cvterm")->create_with({
name => 'discarded_metadata',
cv => 'stock_property'
});

print "You're done!\n";
}


####
1; #
####
2 changes: 1 addition & 1 deletion lib/CXGN/List.pm
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ sub seedlot_list_details {
$content_type = 'cross';
}

push @seedlot_details, [$id, $seedlot_obj->uniquename(), $content_id, $content_name, $content_type, $seedlot_obj->box_name(), $seedlot_obj->get_current_count_property(), $seedlot_obj->get_current_weight_property(), $seedlot_obj->quality()];
push @seedlot_details, [$id, $seedlot_obj->uniquename(), $content_id, $content_name, $content_type, $seedlot_obj->description(), $seedlot_obj->box_name(), $seedlot_obj->get_current_count_property(), $seedlot_obj->get_current_weight_property(), $seedlot_obj->quality()];

}

Expand Down
75 changes: 75 additions & 0 deletions lib/CXGN/Stock/Seedlot/Discard.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

package CXGN::Stock::Seedlot::Discard;

=head1 NAME
CXGN::Stock::Seedlot::Discard
=head1 DESCRIPTION
Store and manage discarded seedlot metadata
=head1 USAGE
=head1 AUTHOR
Titima Tantikanjana <[email protected]>
=cut


use Moose;
use Data::Dumper;


extends 'CXGN::JSONProp';

has 'person_id' => (isa => 'Int', is => 'rw');
has 'discard_date' => (isa => 'Str', is => 'rw');
has 'reason' => (isa => 'Str', is => 'rw');


sub BUILD {
my $self = shift;
my $args = shift;

$self->prop_table('stockprop');
$self->prop_namespace('Stock::Stockprop');
$self->prop_primary_key('stockprop_id');
$self->prop_type('discarded_metadata');
$self->cv_name('stock_property');
$self->allowed_fields([ qw | person_id discard_date reason | ]);
$self->parent_table('stock');
$self->parent_primary_key('stock_id');

$self->load();
}


sub get_discard_details {
my $self = shift;
my $args = shift;
my $schema = $self->bcs_schema();
my $seedlot_id = $self->parent_id();
my $type = $self->prop_type();
my $type_id = $self->_prop_type_id();
my $key_ref = $self->allowed_fields();
my @fields = @$key_ref;
my @discard_details;
my $discard_details_rs = $schema->resultset("Stock::Stockprop")->find({stock_id => $seedlot_id, type_id => $type_id});
if ($discard_details_rs) {
my $details_json = $discard_details_rs->value();
my $detail_hash = JSON::Any->jsonToObj($details_json);
foreach my $field (@fields){
push @discard_details, $detail_hash->{$field};
}
print STDERR "DISCARDED DETAILS =".Dumper(\@discard_details)."\n";
}

return \@discard_details;
}



1;
11 changes: 9 additions & 2 deletions lib/SGN/Controller/AJAX/HTMLSelect.pm
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ sub get_label_data_source_types_select : Path('/ajax/html/select/label_data_sour
my @types = (
["any", "Any Data Type..."],
["field_trials", "Field Trials"],
["genotyping_plates", "Genotyping Plates"],
["genotyping_plates", "Genotyping Plates"],
["crossing_experiments", "Crossing Experiments"],
["lists", "Lists"],
["public_lists", "Public Lists"]
Expand Down Expand Up @@ -720,6 +720,7 @@ sub get_seedlots_select : Path('/ajax/html/select/seedlots') Args(0) {
# my $search_location = $c->req->param('seedlot_location') ? $c->req->param('seedlot_location') : '';
# my $search_amount = $c->req->param('seedlot_amount') ? $c->req->param('seedlot_amount') : '';
# my $search_weight = $c->req->param('seedlot_weight') ? $c->req->param('seedlot_weight') : '';
my $exclude_discarded = $c->req->param('exclude_discarded') ? $c->req->param('exclude_discarded') : '';
my ($list, $records_total) = CXGN::Stock::Seedlot->list_seedlots(
$c->dbic_schema("Bio::Chado::Schema", "sgn_chado"),
$c->dbic_schema("CXGN::People::Schema"),
Expand Down Expand Up @@ -757,7 +758,13 @@ sub get_seedlots_select : Path('/ajax/html/select/seedlots') Args(0) {
my $data_related = $c->req->param("data-related") || "";
my @stocks;
foreach my $r (@seedlots) {
push @stocks, [ $r->{seedlot_stock_id}, $r->{seedlot_stock_uniquename} ];
if ($exclude_discarded == 1) {
if ($r->{count} ne 'DISCARDED') {
push @stocks, [ $r->{seedlot_stock_id}, $r->{seedlot_stock_uniquename} ];
}
} else {
push @stocks, [ $r->{seedlot_stock_id}, $r->{seedlot_stock_uniquename} ];
}
}
@stocks = sort { $a->[1] cmp $b->[1] } @stocks;

Expand Down
13 changes: 7 additions & 6 deletions lib/SGN/Controller/AJAX/List.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1313,10 +1313,11 @@ sub get_list_details :Path('/ajax/list/details') :Args(1) {
content_id => $seedlot->[2],
content_name => $seedlot->[3],
content_type => $seedlot->[4],
box_name => $seedlot->[5],
current_count => $seedlot->[6],
current_weight => $seedlot->[7],
quality => $seedlot->[8],
description => $seedlot->[5],
box_name => $seedlot->[6],
current_count => $seedlot->[7],
current_weight => $seedlot->[8],
quality => $seedlot->[9],
}
}
}
Expand Down Expand Up @@ -1345,9 +1346,9 @@ sub download_list_details : Path('/list/download_details') {
my @details = @$result;
foreach my $seedlot_ref (@details) {
my @seedlot = @$seedlot_ref;
push @list_details, "$seedlot[1]\t$seedlot[3]\t$seedlot[4]\t$seedlot[5]\t$seedlot[6]\t$seedlot[7]\t$seedlot[8]\n";
push @list_details, "$seedlot[1]\t$seedlot[3]\t$seedlot[4]\t$seedlot[5]\t$seedlot[6]\t$seedlot[7]\t$seedlot[8]\t$seedlot[9]\n";
}
$header = "Seedlot_Name\tContent_Name\tContent_type\tBox_Name\tCurrent_Count\tCurrent_Weight\tQuality";
$header = "Seedlot_Name\tContent_Name\tContent_type\tDescription\tBox_Name\tCurrent_Count\tCurrent_Weight\tQuality";
}

my $dl_token = $c->req->param("list_download_token") || "no_token";
Expand Down
124 changes: 124 additions & 0 deletions lib/SGN/Controller/AJAX/Seedlot.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ use CXGN::Login;
use JSON;
use CXGN::BreederSearch;
use CXGN::Onto;
use CXGN::List;
use CXGN::List::Validate;
use CXGN::Stock::Seedlot::Discard;

__PACKAGE__->config(
default => 'application/json',
Expand Down Expand Up @@ -1616,6 +1619,127 @@ sub seedlot_maintenance_event_upload_POST : Args(0) {
$c->stash->{rest} = { success => 1 };
}


sub discard_seedlots : Path('/ajax/breeders/seedlot/discard') :Args(0) {
my $self = shift;
my $c = shift;
my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
my $dbh = $c->dbc->dbh();
my $seedlot_list_id = $c->req->param("seedlot_list_id");
my $seedlot_name = $c->req->param("seedlot_name");
my $discard_reason = $c->req->param("discard_reason");
my @seedlots_to_discard;

my $time = DateTime->now();
my $discard_date = $time->ymd();

if (!$c->user()){
$c->stash->{rest} = { error_string => "You must be logged in to discard seedlot" };
return;
}
if (!$c->user()->check_roles("curator")) {
$c->stash->{rest} = { error_string => "You do not have the correct role to discard seedlot. Please contact us." };
return;
}

my $user_id = $c->user()->get_object()->get_sp_person_id();

if (defined $seedlot_list_id) {
my $list = CXGN::List->new( { dbh=>$dbh, list_id=>$seedlot_list_id });
my $seedlots = $list->elements();
@seedlots_to_discard = @$seedlots;

my $seedlot_validator = CXGN::List::Validate->new();
my @seedlots_missing = @{$seedlot_validator->validate($schema,'seedlots',\@seedlots_to_discard)->{'missing'}};

if (scalar(@seedlots_missing) > 0){
$c->stash->{rest} = { error_string => "The following seedlots are not in the database : ".join(',',@seedlots_missing) };
return;
}
} elsif (defined $seedlot_name) {
@seedlots_to_discard = ($seedlot_name);
}

my $seedlot_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "seedlot", 'stock_type')->cvterm_id();
my $current_count_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "current_count", 'stock_property')->cvterm_id();
my $current_weight_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "current_weight_gram", 'stock_property')->cvterm_id();

foreach my $seedlot_name (@seedlots_to_discard) {
my $seedlot_rs = $schema->resultset("Stock::Stock")->find( { uniquename => $seedlot_name, type_id => $seedlot_type_id });
my $seedlot_id = $seedlot_rs->stock_id();

my $current_count_rs = $seedlot_rs->stockprops({type_id=>$current_count_type_id});
if ($current_count_rs->count == 1){
$current_count_rs->first->update({value=>'DISCARDED'});
}

my $current_weight_rs = $seedlot_rs->stockprops({type_id=>$current_weight_type_id});
if ($current_weight_rs->count == 1){
$current_weight_rs->first->update({value=>"DISCARDED"});
}

my $seedlot_to_discard = CXGN::Stock::Seedlot::Discard->new({
bcs_schema => $schema,
parent_id => $seedlot_id,
});

$seedlot_to_discard->person_id($user_id);
$seedlot_to_discard->discard_date($discard_date);
$seedlot_to_discard->reason($discard_reason);

$seedlot_to_discard->store();

if (!$seedlot_to_discard->store()){
$c->stash->{rest} = {error_string => "Error discarding seedlot",};
return;
}
}

my $bs = CXGN::BreederSearch->new( { dbh=>$dbh, dbname=>$c->config->{dbname}, } );
my $refresh = $bs->refresh_matviews($c->config->{dbhost}, $c->config->{dbname}, $c->config->{dbuser}, $c->config->{dbpass}, 'stockprop', 'concurrent', $c->config->{basepath});

$c->stash->{rest} = {success => "1",};

}


sub undo_discarded_seedlots : Path('/ajax/breeders/seedlot/undo_discard') :Args(0) {
my $self = shift;
my $c = shift;
my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
my $dbh = $c->dbc->dbh();
my $seedlot_id = $c->req->param("seedlot_id");

if (!$c->user()){
$c->stash->{rest} = { error_string => "You must be logged in to undo discading this seedlot" };
return;
}
if (!$c->user()->check_roles("curator")) {
$c->stash->{rest} = { error_string => "You do not have the correct role to undo discarding this seedlot. Please contact us." };
return;
}

my $user_id = $c->user()->get_object()->get_sp_person_id();

my $discarded_metadata_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'discarded_metadata', 'stock_property')->cvterm_id();
my $discarded_rs = $schema->resultset("Stock::Stockprop")->find({stock_id => $seedlot_id, type_id => $discarded_metadata_type_id});

if (defined $discarded_rs->stockprop_id) {
$discarded_rs->delete();
}

my $restored_seedlot = CXGN::Stock::Seedlot->new(schema => $schema, seedlot_id => $seedlot_id);
$restored_seedlot->set_current_count_property();
$restored_seedlot->set_current_weight_property();

my $bs = CXGN::BreederSearch->new( { dbh=>$dbh, dbname=>$c->config->{dbname}, } );
my $refresh = $bs->refresh_matviews($c->config->{dbhost}, $c->config->{dbname}, $c->config->{dbuser}, $c->config->{dbpass}, 'stockprop', 'concurrent', $c->config->{basepath});

$c->stash->{rest} = { success => 1 };

}


1;

no Moose;
Expand Down
Loading

0 comments on commit 0d59bc8

Please sign in to comment.