add identifier function
[sgn.git] / lib / SGN / Controller / AJAX / Transformation.pm
blob65b6538ec58ad24f7052edf12774b7d42c0ef9c9
1 package SGN::Controller::AJAX::Transformation;
3 use Moose;
4 use Data::Dumper;
5 use JSON;
6 use CXGN::People::Person;
7 use SGN::Image;
8 use CXGN::Stock::StockLookup;
9 use CXGN::Location::LocationLookup;
10 use SGN::Model::Cvterm;
11 use CXGN::List::Validate;
12 use CXGN::List;
13 use CXGN::Transformation::AddTransformationProject;
14 use CXGN::Transformation::AddTransformationIdentifier;
15 use List::MoreUtils qw /any /;
18 BEGIN { extends 'Catalyst::Controller::REST' }
20 __PACKAGE__->config(
21 default => 'application/json',
22 stash_key => 'rest',
23 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
27 sub add_transformation_project : Path('/ajax/transformation/add_transformation_project') : ActionClass('REST') {}
29 sub add_transformation_project_POST :Args(0){
30 my ($self, $c) = @_;
31 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
32 my $dbh = $c->dbc->dbh;
33 my $project_name = $c->req->param('project_name');
34 my $breeding_program_id = $c->req->param('project_program_id');
35 my $location = $c->req->param('project_location');
36 my $year = $c->req->param('year');
37 my $project_description = $c->req->param('project_description');
38 $project_name =~ s/^\s+|\s+$//g;
40 print STDERR "PROJECT NAME =".Dumper($project_name)."\n";
41 if (!$c->user()){
42 $c->stash->{rest} = {error => "You need to be logged in to add a transformation project."};
43 return;
46 if (!any { $_ eq "curator" || $_ eq "submitter" } ($c->user()->roles)){
47 $c->stash->{rest} = {error => "you have insufficient privileges to add a transformation project." };
48 return;
51 my $user_id = $c->user()->get_object()->get_sp_person_id();
53 my $geolocation_lookup = CXGN::Location::LocationLookup->new(schema =>$schema);
54 $geolocation_lookup->set_location_name($location);
55 if(!$geolocation_lookup->get_geolocation()){
56 $c->stash->{rest}={error => "Location not found"};
57 return;
60 my $error;
61 eval{
62 my $add_transformation_project = CXGN::Transformation::AddTransformationProject->new({
63 chado_schema => $schema,
64 dbh => $dbh,
65 breeding_program_id => $breeding_program_id,
66 year => $year,
67 project_description => $project_description,
68 transformation_project_name => $project_name,
69 nd_geolocation_id => $geolocation_lookup->get_geolocation()->nd_geolocation_id(),
70 owner_id => $user_id
71 });
73 my $return = $add_transformation_project->save_transformation_project();
74 if ($return->{error}){
75 $error = $return->{error};
79 if ($@) {
80 $c->stash->{rest} = {error => $@};
81 return;
84 if ($error){
85 $c->stash->{rest} = {error => $error};
86 } else {
87 $c->stash->{rest} = {success => 1};
93 sub add_transformation_identifier : Path('/ajax/transformation/add_transformation_identifier') : ActionClass('REST') {}
95 sub add_transformation_identifier_POST :Args(0){
96 my ($self, $c) = @_;
97 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
98 my $phenome_schema = $c->dbic_schema("CXGN::Phenome::Schema");
99 my $dbh = $c->dbc->dbh;
100 my $transformation_identifier = $c->req->param('transformation_identifier');
101 my $plant_material = $c->req->param('plant_material');
102 my $vector_construct = $c->req->param('vector_construct');
103 my $notes = $c->req->param('notes');
104 my $transformation_project_id = $c->req->param('transformation_project_id');
105 $transformation_identifier =~ s/^\s+|\s+$//g;
107 print STDERR "TRANSFORMATION IDENTIFIER =".Dumper($transformation_identifier)."\n";
109 if (!$c->user()){
110 $c->stash->{rest} = {error => "You need to be logged in to add a transformation transformation identifier."};
111 return;
114 if (!any { $_ eq "curator" || $_ eq "submitter" } ($c->user()->roles)){
115 $c->stash->{rest} = {error => "you have insufficient privileges to add a transformation identifier." };
116 return;
119 my $user_id = $c->user()->get_object()->get_sp_person_id();
121 my $accession_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema,'accession', 'stock_type')->cvterm_id();
122 my $vector_construct_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema,'vector_construct', 'stock_type')->cvterm_id();
124 if ($schema->resultset("Stock::Stock")->find({uniquename => $transformation_identifier})){
125 $c->stash->{rest} = {error => "Transformation identifier already exists." };
126 return 0;
129 if (! $schema->resultset("Stock::Stock")->find({uniquename => $plant_material, type_id => $accession_cvterm_id })){
130 $c->stash->{rest} = {error => "Plant material does not exist or does not exist as accession uniquename." };
131 return;
134 if (! $schema->resultset("Stock::Stock")->find({uniquename => $vector_construct, type_id => $vector_construct_cvterm_id })){
135 $c->stash->{rest} = {error => "vector construct does not exist or does not exist as vector construct uniquename." };
136 return;
139 eval {
140 my $add_transformation = CXGN::Transformation::AddTransformationIdentifier->new({
141 chado_schema => $schema,
142 phenome_schema => $phenome_schema,
143 dbh => $dbh,
144 transformation_project_id => $transformation_project_id,
145 transformation_identifier => $transformation_identifier,
146 plant_material => $plant_material,
147 vector_construct => $vector_construct,
148 notes => $notes,
149 owner_id => $user_id,
152 $add_transformation->add_transformation_identifier();
155 if ($@) {
156 $c->stash->{rest} = { error => "An error occurred: $@"};
157 return 0;
159 return 1;