seedlot upload with accession synonyms. seedlot upload works to update existing seedlots
[sgn.git] / lib / SGN / Controller / AJAX / Dataset.pm
blob29868abe01631e5db5ad6fc2d46b7c44b7b24bf3
2 package SGN::Controller::AJAX::Dataset;
4 use Moose;
6 BEGIN { extends 'Catalyst::Controller::REST' }
8 use Data::Dumper;
9 use JSON::Any;
10 use CXGN::Dataset;
13 __PACKAGE__->config(
14 default => 'application/json',
15 stash_key => 'rest',
16 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
19 sub store_dataset :Path('/ajax/dataset/save') Args(0) {
20 my $self = shift;
21 my $c = shift;
23 my $user;
24 if (!$c->user()) {
25 $c->stash->{rest} = { error => "Login required to perform requested action." };
26 return;
28 else {
29 if (! ($c->user->check_roles('submitter') || $c->user->check_roles('curator'))) {
30 $c->stash->{rest} = { error => "Insufficient privileges to perform requested action." };
31 return;
35 my %data;
37 my $dataset_name = $c->req->param("name");
38 my $dataset_description = $c->req->param("description");
40 my $people_schema = $c->dbic_schema("CXGN::People::Schema");
41 if (CXGN::Dataset->exists_dataset_name($people_schema, $dataset_name)) {
42 $c->stash->{rest} = { error => "The dataset with name $dataset_name already exists. Please chose another name." };
43 return;
46 my $user_id = $c->user()->get_object()->get_sp_person_id();
48 my $dataset = CXGN::Dataset->new( {
49 schema => $c->dbic_schema("Bio::Chado::Schema"),
50 people_schema => $people_schema,
51 });
53 $dataset->sp_person_id($user_id);
54 $dataset->name($dataset_name);
55 $dataset->description($dataset_description);
57 foreach my $type (qw | trials accessions years locations plots traits breeding_programs genotyping_protocols trial_types trial_designs category_order |) {
58 print STDERR "Storing data: $type. $data{$type}\n";
60 my $json = $c->req->param($type);
61 if ($json) {
62 my $obj = JSON::Any->jsonToObj($json);
63 $dataset->$type($obj);
67 $dataset->store();
69 $c->stash->{rest} = { message => "Stored Dataset Successfully!" };
72 sub get_datasets_by_user :Path('/ajax/dataset/by_user') Args(0) {
73 my $self = shift;
74 my $c = shift;
76 my $user = $c->user();
77 if (!$user) {
78 $c->stash->{rest} = { error => "No logged in user to display dataset information for." };
79 return;
82 my $datasets = CXGN::Dataset->get_datasets_by_user(
83 $c->dbic_schema("CXGN::People::Schema"),
84 $user->get_object()->get_sp_person_id()
87 $c->stash->{rest} = $datasets;
90 sub get_dataset :Path('/ajax/dataset/get') Args(1) {
91 my $self = shift;
92 my $c = shift;
93 my $dataset_id = shift;
95 my $dataset = CXGN::Dataset->new(
97 schema => $c->dbic_schema("Bio::Chado::Schema"),
98 people_schema => $c->dbic_schema("CXGN::People::Schema"),
99 sp_dataset_id=> $dataset_id,
102 my $dataset_data = $dataset->get_dataset_data();
104 $c->stash->{rest} = { dataset => $dataset_data };
107 sub delete_dataset :Path('/ajax/dataset/delete') Args(1) {
108 my $self = shift;
109 my $c = shift;
110 my $dataset_id = shift;
112 if (!$c->user()) {
113 $c->stash->{rest} = { error => "Deleting datasets requires login" };
114 return;
117 my $logged_in_user = $c->user()->get_object()->get_sp_person_id();
119 my $dataset = CXGN::Dataset->new(
121 schema => $c->dbic_schema("Bio::Chado::Schema"),
122 people_schema => $c->dbic_schema("CXGN::People::Schema"),
123 sp_dataset_id=> $dataset_id,
126 print STDERR "Dataset owner: ".$dataset->sp_person_id.", logged in: $logged_in_user\n";
127 if ($dataset->sp_person_id() != $logged_in_user) {
128 $c->stash->{rest} = { error => "Only the owner can delete a dataset" };
129 return;
132 my $error = $dataset->delete();
134 if ($error) {
135 $c->stash->{rest} = { error => $error };
137 else {
138 $c->stash->{rest} = { success => 1 };