minor fixes
[sgn.git] / lib / CXGN / Trial / Download.pm
blob0c7a325c6c5e7ce0b6a5dbb44213de223d53b9e3
2 package CXGN::Trial::Download;
4 use Moose;
6 use Moose::Util::TypeConstraints;
7 use Try::Tiny;
8 use File::Basename qw | basename dirname|;
9 use Digest::MD5;
10 use CXGN::List::Validate;
11 use Data::Dumper;
12 use CXGN::Trial;
13 use CXGN::Trial::TrialLayout;
14 use Spreadsheet::WriteExcel;
15 use CXGN::Trait;
16 use CXGN::List::Transform;
17 use CXGN::People::Person;
18 use DateTime;
20 with 'MooseX::Object::Pluggable';
23 has 'bcs_schema' => (
24 isa => "Bio::Chado::Schema",
25 is => 'ro',
26 required => 1,
29 has 'trial_id' => (
30 isa => "Int",
31 is => 'ro',
32 required => 1,
36 has 'bcs_schema' => (
37 is => 'ro',
38 isa => 'DBIx::Class::Schema',
39 required => 1,
42 # can be provided for logging purposes
43 has 'user_id' => (
44 is => 'ro',
45 isa => 'Int',
48 has 'trial_download_logfile' => (
49 is => 'ro',
50 isa => 'Str',
53 ## defines the plugin with which the download will be processed
54 has 'format' => (isa => 'Str', is => 'ro', required => 1);
56 has 'data_level' => (isa => 'Str | Undef', is => 'ro', default => 'plots');
58 has 'sample_number' => (isa => 'Int | Undef', is => 'ro', default => 0);
60 has 'predefined_columns' => (isa => 'ArrayRef[HashRef] | Undef', is => 'ro');
62 has 'trait_list' => (isa => 'ArrayRef', is => 'rw', predicate => 'has_trait_list' );
64 has 'filename' => (isa => 'Str', is => 'ro',
65 predicate => 'has_filename',
66 required => 1,
69 has 'file_metadata' => (isa => 'Str', is => 'rw', predicate => 'has_file_metadata');
71 ## used when downloading phenotypes as either csv or xls.
72 has 'include_timestamp' => (isa => 'Bool', is => 'ro', default => 0);
75 sub BUILD {
76 my $self = shift;
77 $self->load_plugin($self->format());
80 # sub verify {
81 # my $self = shift;
83 # $self->load_plugin($self->format());
85 # return $self->plugin_verify();
86 # }
88 # sub download {
89 # my $self = shift;
91 # print STDERR "Format: ".$self->format()."\n";
92 # eval {
93 # $self->load_plugin($self->format());
94 # };
95 # if ($@) {
96 # die "The plugin specified (".$self->format().") for the download does not exist";
97 # }
99 # my $error = $self->plugin_download();
101 # return $error;
104 sub trial_download_log {
105 my $self = shift;
106 my $trial_id = shift;
107 my $message = shift;
109 if (! $self->user_id && !$self->trial_download_logfile()) {
110 return;
112 else {
113 print STDERR "Note: set config variable trial_download_logfile to obtain a log of downloaded trials.\n";
116 my $now = DateTime->now();
118 open (my $F, ">>", $self->trial_download_logfile()) || die "Can't open ".$self->trial_download_logfile();
119 my $username = CXGN::People::Person->new($self->bcs_schema->storage->dbh(), $self->user_id())->get_username();
120 print $F join("\t", (
121 $username,
122 $trial_id,
123 $message,
124 $now->year()."-".$now->month()."-".$now->day()." ".$now->hour().":".$now->minute()));
125 print $F "\n";
127 close($F);