moved creation of trial fieldbook file from the AJAX module to CXGN::Fieldbook::Downl...
[sgn.git] / lib / CXGN / Fieldbook / DownloadTrial.pm
blob65cce5de84968a469495e12abbf35b2bb4536573
1 package CXGN::Fieldbook::DownloadTrial;
3 use Moose;
4 use Moose::Util::TypeConstraints;
5 use Try::Tiny;
6 use File::Basename qw | basename dirname|;
7 use File::Copy;
8 use File::Spec::Functions;
9 use Digest::MD5;
10 use CXGN::List::Validate;
11 use Data::Dumper;
12 use CXGN::Trial::TrialLayout;
13 use Spreadsheet::WriteExcel;
14 use CXGN::Trait;
15 use CXGN::List::Transform;
16 use CXGN::People::Person;
17 use DateTime;
19 has 'bcs_schema' => (
20 isa => "Bio::Chado::Schema",
21 is => 'ro',
22 required => 1,
25 has 'metadata_schema' => (
26 isa => "CXGN::Metadata::Schema",
27 is => 'ro',
28 required => 1,
31 has 'phenome_schema' => (
32 isa => "CXGN::Phenome::Schema",
33 is => 'ro',
34 required => 1,
37 has 'trial_id' => (
38 isa => "Int",
39 is => 'ro',
40 required => 1,
43 has 'tempfile' => (isa => 'Str', is => 'ro',
44 predicate => 'has_tempfile',
45 required => 1,
48 has 'archive_path' => (isa => 'Str', is => 'ro',
49 predicate => 'has_archive_path',
50 required => 1,
53 has 'user_id' => (
54 is => 'ro',
55 isa => 'Int',
56 required => 1,
59 has 'user_name' => (
60 is => 'ro',
61 isa => 'Str',
62 required => 1,
65 has 'file_metadata' => (isa => 'Str', is => 'rw', predicate => 'has_file_metadata');
68 sub download {
69 my $self = shift;
70 my %errors;
71 my @error_messages;
73 my $schema = $self->bcs_schema();
74 my $trial_id = $self->trial_id();
75 my $tempfile = $self->tempfile();
76 my $wb = Spreadsheet::WriteExcel->new($tempfile);
77 if (!$wb) {
78 push @error_messages, "Could not create file.";
79 $errors{'error_messages'} = \@error_messages;
80 return \%errors;
83 my $ws = $wb->add_worksheet();
84 my $trial_layout;
85 print STDERR "\n\nTrial id: ($trial_id)\n\n";
86 try {
87 $trial_layout = CXGN::Trial::TrialLayout->new({schema => $schema, trial_id => $trial_id} );
89 if (!$trial_layout) {
90 push @error_messages, "Trial does not have valid field design.";
91 $errors{'error_messages'} = \@error_messages;
92 return \%errors;
95 my $trial_name = $trial_layout->get_trial_name();
96 $ws->write(0, 0, 'plot_id');
97 $ws->write(0, 1, 'range');
98 $ws->write(0, 2, 'plot');
99 $ws->write(0, 3, 'rep');
100 $ws->write(0, 4, 'accession');
101 $ws->write(0, 5, 'is_a_control');
103 my %design = %{$trial_layout->get_design()};
104 my $row_num = 1;
105 foreach my $key (sort { $a <=> $b} keys %design) {
106 my %design_info = %{$design{$key}};
107 $ws->write($row_num,0,$design_info{'plot_name'});
108 $ws->write($row_num,1,$design_info{'block_number'});
109 $ws->write($row_num,2,$design_info{'plot_number'});
110 $ws->write($row_num,3,$design_info{'rep_number'});
111 $ws->write($row_num,4,$design_info{'accession_name'});
112 $ws->write($row_num,5,$design_info{'is_a_control'});
113 $row_num++;
115 $wb->close();
117 my $user_id = $self->user_id();
118 open(my $F, "<", $tempfile) || die "Can't open file ".$self->tempfile();
119 binmode $F;
120 my $md5 = Digest::MD5->new();
121 $md5->addfile($F);
122 close($F);
124 my $project = $trial_layout->get_project;
126 my $time = DateTime->now();
127 my $timestamp = $time->ymd()."_".$time->hms();
128 my $user_name = $self->user_name();
129 my $subdirectory_name = "tablet_field_layout";
130 my $archived_file_name = catfile($user_id, $subdirectory_name,$timestamp."_".$project->name.".xls");
131 my $archive_path = $self->archive_path();
132 my $file_destination = catfile($archive_path, $archived_file_name);
134 if (!-d $archive_path) {
135 mkdir $archive_path;
138 if (! -d catfile($archive_path, $user_id)) {
139 mkdir (catfile($archive_path, $user_id));
142 if (! -d catfile($archive_path, $user_id,$subdirectory_name)) {
143 mkdir (catfile($archive_path, $user_id, $subdirectory_name));
146 my $metadata_schema = $self->metadata_schema();
147 my $md_row = $metadata_schema->resultset("MdMetadata")->create({
148 create_person_id => $user_id,
150 $md_row->insert();
152 my $file_row = $metadata_schema->resultset("MdFiles")->create({
153 basename => basename($file_destination),
154 dirname => dirname($file_destination),
155 filetype => 'tablet field layout xls',
156 md5checksum => $md5->hexdigest(),
157 metadata_id => $md_row->metadata_id(),
159 $file_row->insert();
161 my $field_layout_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'field_layout', 'experiment_type' );
163 my $experiment = $schema->resultset('NaturalDiversity::NdExperiment')->find(
165 'nd_experiment_projects.project_id' => $project->project_id,
166 type_id => $field_layout_cvterm->cvterm_id(),
169 join => 'nd_experiment_projects',
173 my $phenome_schema = $self->phenome_schema();
174 my $experiment_files = $phenome_schema->resultset("NdExperimentMdFiles")->create({
175 nd_experiment_id => $experiment->nd_experiment_id(),
176 file_id => $file_row->file_id(),
178 $experiment_files->insert();
180 move($tempfile,$file_destination);
181 unlink $tempfile;
183 my $result = $file_row->file_id;
184 return {result => $result, file => $file_destination};