4 SGN::Controller::AJAX::FieldBook - a REST controller class to provide the
5 backend for field book operations
9 Creating and viewing trials
13 Jeremy Edwards <jde22@cornell.edu>
17 package SGN
::Controller
::AJAX
::FieldBook
;
20 use List
::MoreUtils qw
/any /;
21 use Scalar
::Util
qw(looks_like_number);
24 use File
::Basename qw
| basename dirname
|;
27 use File
::Spec
::Functions
;
29 use JSON
-support_by_pp
;
30 use Spreadsheet
::WriteExcel
;
31 use SGN
::View
::Trial qw
/design_layout_view design_info_view/;
32 use CXGN
::Phenotypes
::ParseUpload
;
33 use CXGN
::Phenotypes
::StorePhenotypes
;
34 use CXGN
::Trial
::TrialLayout
;
35 use CXGN
::Location
::LocationLookup
;
36 use CXGN
::Stock
::StockLookup
;
38 use CXGN
::Fieldbook
::TraitInfo
;
39 use CXGN
::Fieldbook
::DownloadTrial
;
40 use SGN
::Model
::Cvterm
;
44 BEGIN { extends
'Catalyst::Controller::REST' }
47 default => 'application/json',
49 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
53 sub create_fieldbook_from_trial
: Path
('/ajax/fieldbook/create') : ActionClass
('REST') { }
55 sub create_fieldbook_from_trial_POST
: Args
(0) {
57 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
58 my $trial_id = $c->req->param('trial_id');
59 my $data_level = $c->req->param('data_level') || 'plots';
60 my $metadata_schema = $c->dbic_schema('CXGN::Metadata::Schema');
61 my $phenome_schema = $c->dbic_schema('CXGN::Phenome::Schema');
65 $c->stash->{rest
} = {error
=> "You need to be logged in to create a field book" };
68 if (!any
{ $_ eq "curator" || $_ eq "submitter" } ($c->user()->roles) ) {
69 $c->stash->{rest
} = {error
=> "You have insufficient privileges to create a field book." };
73 $c->stash->{rest
} = {error
=> "No trial ID supplied." };
76 my $trial = $schema->resultset('Project::Project')->find({project_id
=> $trial_id});
78 $c->stash->{rest
} = {error
=> "Trial does not exist with id $trial_id." };
81 if ($data_level eq 'plants') {
82 my $trial = CXGN
::Trial
->new( { bcs_schema
=> $schema, trial_id
=> $trial_id });
83 if (!$trial->has_plant_entries()){
84 $c->stash->{rest
} = {error
=> "Trial does not have plant entries. You must first create plant entries." };
89 my $dir = $c->tempfiles_subdir('/other');
90 my $tempfile = $c->config->{basepath
}."/".$c->tempfile( TEMPLATE
=> 'other/excelXXXX');
92 my $create_fieldbook = CXGN
::Fieldbook
::DownloadTrial
->new({
93 bcs_schema
=> $schema,
94 metadata_schema
=> $metadata_schema,
95 phenome_schema
=> $phenome_schema,
96 trial_id
=> $trial_id,
97 tempfile
=> $tempfile,
98 archive_path
=> $c->config->{archive_path
},
99 user_id
=> $c->user()->get_object()->get_sp_person_id(),
100 user_name
=> $c->user()->get_object()->get_username(),
101 data_level
=> $data_level,
104 my $create_fieldbook_return = $create_fieldbook->download();
106 $c->stash->{rest
} = {
107 error_string
=> $create_fieldbook_return->{'error_messages'},
109 result
=> $create_fieldbook_return->{'result'},
110 file
=> $create_fieldbook_return->{'file'},
114 sub create_trait_file_for_field_book
: Path
('/ajax/fieldbook/traitfile/create') : ActionClass
('REST') { }
116 sub create_trait_file_for_field_book_POST
: Args
(0) {
120 $c->stash->{rest
} = {error
=> "You need to be logged in to create a field book" };
123 if (!any
{ $_ eq "curator" || $_ eq "submitter" } ($c->user()->roles) ) {
124 $c->stash->{rest
} = {error
=> "You have insufficient privileges to create a field book." };
129 my $trait_file_name = $c->req->param('trait_file_name');
130 my $user_id = $c->user()->get_object()->get_sp_person_id();
131 my $user_name = $c->user()->get_object()->get_username();
132 my $time = DateTime
->now();
133 my $timestamp = $time->ymd()."_".$time->hms();
134 my $subdirectory_name = "tablet_trait_files";
135 my $archived_file_name = catfile
($user_id, $subdirectory_name,$timestamp."_".$trait_file_name.".trt");
136 my $archive_path = $c->config->{archive_path
};
137 my $file_destination = catfile
($archive_path, $archived_file_name);
139 if ($c->req->param('trait_list')) {
140 @trait_list = @
{_parse_list_from_json
($c->req->param('trait_list'))};
143 if (!-d
$archive_path) {
147 if (! -d catfile
($archive_path, $user_id)) {
148 mkdir (catfile
($archive_path, $user_id));
151 if (! -d catfile
($archive_path, $user_id,$subdirectory_name)) {
152 mkdir (catfile
($archive_path, $user_id, $subdirectory_name));
155 open FILE
, ">$file_destination" or die $!;
156 my $chado_schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
157 print FILE
"trait,format,defaultValue,minimum,maximum,details,categories,isVisible,realPosition\n";
160 foreach my $term (@trait_list) {
162 my ($trait_name, $full_cvterm_accession) = split (/\|/, $term);
163 my ( $db_name , $accession ) = split (/:/ , $full_cvterm_accession);
165 $accession =~ s/\s+$//;
166 $accession =~ s/^\s+//;
167 $db_name =~ s/\s+$//;
168 $db_name =~ s/^\s+//;
170 print STDERR
"traitname: $term | accession: $accession \n";
174 my $trait_info_lookup = CXGN
::Fieldbook
::TraitInfo
176 chado_schema
=> $chado_schema,
178 trait_accession
=> $accession,
180 my $trait_info_string = $trait_info_lookup->get_trait_info($trait_name);
182 #return error if not $trait_info_string;
183 #print line with trait info
184 #print FILE "$trait_name:$db_name:$accession,text,,,,,,TRUE,$order\n";
185 print FILE
"\"$trait_name|$db_name:$accession\",$trait_info_string,\"TRUE\",\"$order\"\n";
191 open(my $F, "<", $file_destination) || die "Can't open file ";
193 my $md5 = Digest
::MD5
->new();
197 my $metadata_schema = $c->dbic_schema('CXGN::Metadata::Schema');
199 my $md_row = $metadata_schema->resultset("MdMetadata")->create({
200 create_person_id
=> $user_id,
204 my $file_row = $metadata_schema->resultset("MdFiles")->create({
205 basename
=> basename
($file_destination),
206 dirname
=> dirname
($file_destination),
207 filetype
=> 'tablet trait file',
208 md5checksum
=> $md5->hexdigest(),
209 metadata_id
=> $md_row->metadata_id(),
213 $c->stash->{rest
} = {success
=> "1",};
218 sub _parse_list_from_json
{
219 my $list_json = shift;
222 my $decoded_list = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($list_json);
223 #my $decoded_list = decode_json($list_json);
224 my @array_of_list_items = @
{$decoded_list};
225 return \
@array_of_list_items;