Merge branch 'topic/related_stock_datatables' into topic/trials_from_seedlots
[sgn.git] / lib / CXGN / BrAPI / v1 / Trials.pm
blob91197976034dcc804424e2ea7de24a6c513c4bb4
1 package CXGN::BrAPI::v1::Trials;
3 use Moose;
4 use Data::Dumper;
5 use SGN::Model::Cvterm;
6 use CXGN::Trial::Folder;
7 use CXGN::BrAPI::Pagination;
8 use CXGN::BrAPI::JSONResponse;
10 has 'bcs_schema' => (
11 isa => 'Bio::Chado::Schema',
12 is => 'rw',
13 required => 1,
16 has 'metadata_schema' => (
17 isa => 'CXGN::Metadata::Schema',
18 is => 'rw',
19 required => 1,
22 has 'phenome_schema' => (
23 isa => 'CXGN::Phenome::Schema',
24 is => 'rw',
25 required => 1,
28 has 'page_size' => (
29 isa => 'Int',
30 is => 'rw',
31 required => 1,
34 has 'page' => (
35 isa => 'Int',
36 is => 'rw',
37 required => 1,
40 has 'status' => (
41 isa => 'ArrayRef[Maybe[HashRef]]',
42 is => 'rw',
43 required => 1,
46 sub trials_search {
47 my $self = shift;
48 my $search_params = shift;
49 my $page_size = $self->page_size;
50 my $page = $self->page;
51 my $status = $self->status;
52 my $schema = $self->bcs_schema;
53 #my $auth = _authenticate_user($c);
55 my @location_dbids = $search_params->{locationDbIds} ? @{$search_params->{locationDbIds}} : ();
56 my @program_dbids = $search_params->{programDbIds} ? @{$search_params->{programDbIds}} : ();
58 my %location_id_list;
59 if (scalar(@location_dbids)>0){
60 %location_id_list = map { $_ => 1} @location_dbids;
62 my %program_id_list;
63 if (scalar(@program_dbids)>0){
64 %program_id_list = map { $_ => 1} @program_dbids;
67 my $total_count = 0;
69 my $folder_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema,'trial_folder', 'project_property')->cvterm_id();
70 my $folder_rs = $self->bcs_schema()->resultset("Project::Project")->search_related('projectprops', {'projectprops.type_id'=>$folder_cvterm_id});
72 my @folder_studies;
73 my %additional_info;
74 my @data;
75 if ($folder_rs) {
76 $total_count = $folder_rs->count();
77 my $rs_slice = $folder_rs->slice($page_size*$page, $page_size*($page+1)-1);
78 while (my $p = $rs_slice->next()) {
79 my $folder = CXGN::Trial::Folder->new({bcs_schema=>$self->bcs_schema, folder_id=>$p->project_id});
80 if ($folder->is_folder) {
81 my $children = $folder->children();
82 foreach (@$children) {
83 my $passes_search = 1;
84 if (%location_id_list) {
85 if (!exists($location_id_list{$_->location_id})) {
86 $passes_search = 0;
89 if ($passes_search){
90 push @folder_studies, {
91 studyDbId=>$_->folder_id,
92 studyName=>$_->name,
93 locationDbId=>$_->location_id
98 my $passes_search = 1;
99 if (%program_id_list) {
100 if (!exists($program_id_list{$folder->breeding_program->project_id})) {
101 $passes_search = 0;
104 if ($passes_search){
105 push @data, {
106 trialDbId=>$folder->folder_id,
107 trialName=>$folder->name,
108 programDbId=>$folder->breeding_program->project_id(),
109 programName=>$folder->breeding_program->name(),
110 startDate=>'',
111 endDate=>'',
112 active=>'',
113 studies=>\@folder_studies,
114 additionalInfo=>\%additional_info
120 my %result = (data => \@data);
121 my @data_files;
122 my $pagination = CXGN::BrAPI::Pagination->pagination_response($total_count,$page_size,$page);
123 return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'Trials-search result constructed');
126 sub trial_details {
127 my $self = shift;
128 my $folder_id = shift;
130 my $page_size = $self->page_size;
131 my $page = $self->page;
132 my $status = $self->status;
133 my $schema = $self->bcs_schema;
134 my $trial_check = $schema->resultset('Project::Project')->find({project_id=>$folder_id});
135 if ($trial_check){
136 my $folder = CXGN::Trial::Folder->new(bcs_schema=>$self->bcs_schema(), folder_id=>$folder_id);
137 if ($folder->is_folder) {
138 my $total_count = 1;
139 my @folder_studies;
140 my %additional_info;
141 my $children = $folder->children();
142 foreach (@$children) {
143 push @folder_studies, {
144 studyDbId=>$_->folder_id,
145 studyName=>$_->name,
146 locationDbId=>$_->location_id
149 my %result = (
150 trialDbId=>$folder->folder_id,
151 trialName=>$folder->name,
152 programDbId=>$folder->breeding_program->project_id(),
153 programName=>$folder->breeding_program->name(),
154 startDate=>'',
155 endDate=>'',
156 active=>'',
157 studies=>\@folder_studies,
158 additionalInfo=>\%additional_info
160 my @data_files;
161 my $pagination = CXGN::BrAPI::Pagination->pagination_response($total_count,$page_size,$page);
162 return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'Trial detail result constructed');
163 } else {
164 return CXGN::BrAPI::JSONResponse->return_error($status, 'The given trialDbId does not match an actual trial.');
166 } else {
167 return CXGN::BrAPI::JSONResponse->return_error($status, 'The given trialDbId not found.');