Merge pull request #5205 from solgenomics/topic/generic_trial_upload
[sgn.git] / lib / CXGN / Trial / Folder.pm
blob0d6770308ed522e8541bd4f036c95418d68e9020
2 package CXGN::Trial::Folder;
4 use CXGN::Chado::Cvterm;
5 use CXGN::Location;
6 use Moose;
7 use SGN::Model::Cvterm;
8 use Data::Dumper;
9 use JSON;
11 has 'bcs_schema' => ( isa => 'Bio::Chado::Schema',
12 is => 'rw',
13 required => 1,
16 has 'folder_id' => (isa => "Int",
17 is => 'rw',
20 has 'children' => (is => 'rw',
21 lazy => 1,
22 default => sub {
23 my $self = shift;
24 $self->_get_children();
28 has 'is_folder' => (isa => 'Bool',
29 is => 'rw',
30 default => 0,
33 has 'folder_type' => (isa => 'Str',
34 is => 'rw',
37 has 'name' => (isa => 'Str',
38 is => 'rw',
39 default => 'Untitled',
42 has 'description' => (
43 isa => 'Str',
44 is => 'rw'
47 has 'folder_for_trials' => (isa => 'Bool',
48 is => 'rw',
49 default => 0,
52 has 'folder_for_crosses' => (isa => 'Bool',
53 is => 'rw',
54 default => 0,
57 has 'folder_for_genotyping_trials' => (isa => 'Bool',
58 is => 'rw',
59 default => 0,
62 has 'folder_for_genotyping_projects' => (isa => 'Bool',
63 is => 'rw',
64 default => 0,
67 has 'folder_for_tracking_activities' => (isa => 'Bool',
68 is => 'rw',
69 default => 0,
72 has 'folder_for_transformations' => (isa => 'Bool',
73 is => 'rw',
74 default => 0,
77 has 'location_id' => (isa => 'Int',
78 is => 'rw',
81 has 'location_name' => (isa => 'Str',
82 is => 'rw',
85 has 'breeding_program_trial_relationship_id' => (isa => 'Int',
86 is => 'rw',
89 has 'project_parent' => (isa => 'Bio::Chado::Schema::Result::Project::Project',
90 is => 'rw',
93 has 'breeding_program' => (isa => 'Bio::Chado::Schema::Result::Project::Project',
94 is => 'rw',
97 has 'breeding_program_cvterm_id' => (isa => 'Int',
98 is => 'rw',
101 has 'folder_cvterm_id' => (isa => 'Int',
102 is => 'rw',
105 has 'additional_info' => (
106 is => 'rw'
109 sub BUILD {
110 my $self = shift;
112 my $row = $self->bcs_schema()->resultset('Project::Project')->find( { project_id=>$self->folder_id() });
114 if (!$row) {
115 die "The specified folder with id ".$self->folder_id()." does not exist!";
118 $self->name($row->name());
119 $self->description($row->description());
121 my $breeding_program_type_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema,'breeding_program', 'project_property')->cvterm_id();
122 my $folder_for_trials_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'folder_for_trials', 'project_property')->cvterm_id();
123 my $folder_for_crosses_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'folder_for_crosses', 'project_property')->cvterm_id();
124 my $folder_for_genotyping_trials_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'folder_for_genotyping_trials', 'project_property')->cvterm_id();
125 my $folder_for_genotyping_projects_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'folder_for_genotyping_projects', 'project_property')->cvterm_id();
126 my $folder_for_tracking_activities_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'folder_for_tracking_activities', 'project_property')->cvterm_id();
127 my $folder_for_transformations_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'folder_for_transformations', 'project_property')->cvterm_id();
128 my $folder_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema,'trial_folder', 'project_property')->cvterm_id();
129 my $analyses_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema,'analysis_metadata_json', 'project_property')->cvterm_id();
130 my $breeding_program_trial_relationship_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema,'breeding_program_trial_relationship', 'project_relationship')->cvterm_id();
131 my $additional_info_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema,'project_additional_info', 'project_property')->cvterm_id();
133 $self->breeding_program_cvterm_id($breeding_program_type_id);
134 $self->folder_cvterm_id($folder_cvterm_id);
135 $self->breeding_program_trial_relationship_id($breeding_program_trial_relationship_id);
137 my $folder_type = $self->bcs_schema()->resultset('Project::Projectprop')-> search( { project_id => $self->folder_id() });
138 while (my $folder_type_row = $folder_type->next) {
139 if ($folder_type_row->type_id() == $self->folder_cvterm_id() ) {
140 $self->folder_type("folder");
141 $self->is_folder(1);
142 } elsif ($folder_type_row->type_id() == $self->breeding_program_cvterm_id()) {
143 $self->folder_type("breeding_program");
144 $self->breeding_program($row);
145 } elsif ($folder_type_row->type_id() == $folder_for_trials_cvterm_id) {
146 $self->folder_for_trials(1);
147 } elsif ($folder_type_row->type_id() == $folder_for_crosses_cvterm_id) {
148 $self->folder_for_crosses(1);
149 } elsif ($folder_type_row->type_id() == $folder_for_genotyping_trials_cvterm_id) {
150 $self->folder_for_genotyping_trials(1);
151 } elsif ($folder_type_row->type_id() == $folder_for_genotyping_projects_cvterm_id) {
152 $self->folder_for_genotyping_projects(1);
153 } elsif ($folder_type_row->type_id() == $folder_for_tracking_activities_cvterm_id) {
154 $self->folder_for_tracking_activities(1);
155 } elsif ($folder_type_row->type_id() == $folder_for_transformations_cvterm_id) {
156 $self->folder_for_transformations(1);
157 } elsif ($folder_type_row->type_id() == $additional_info_cvterm_id) {
158 my $additional_info = decode_json($folder_type_row->value);
159 $self->additional_info($additional_info);
163 if (!$self->folder_type) {
164 my $location_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'project location', 'project_property')->cvterm_id;
166 my $trial_type_rs = $self->bcs_schema->resultset("Project::Project")->search({ 'me.project_id' => $self->folder_id })->search_related('projectprops');
167 while (my $tt = $trial_type_rs->next()) {
168 if ($tt->value eq 'crossing_trial') {
169 $self->folder_type("cross");
170 } elsif ($tt->value eq 'genotyping_plate') {
171 $self->folder_type("genotyping_trial");
172 } elsif ($tt->value eq 'genotyping_plate') {
173 $self->folder_type("genotyping_trial");
174 } elsif (($tt->value eq 'genotype_data_project') || ($tt->value eq 'pcr_genotype_data_project')) {
175 $self->folder_type("genotyping_project");
176 } elsif ($tt->value eq 'sampling_trial') {
177 $self->folder_type("sampling_trial");
178 } elsif ($tt->value eq 'activity_record') {
179 $self->folder_type('activity_record');
180 } elsif ($tt->type_id == $analyses_cvterm_id) {
181 $self->folder_type("analyses");
182 } elsif ($tt->type_id == $location_cvterm_id) {
183 $self->location_id($tt->value + 0);
184 my $location = CXGN::Location->new( { bcs_schema => $self->bcs_schema, nd_geolocation_id => $self->location_id } );
185 $self->location_name($location->name());
189 if (!$self->folder_type) {
190 $self->folder_type("trial");
194 my $breeding_program_rel_row = $self->bcs_schema()->resultset('Project::ProjectRelationship')->find( { subject_project_id => $self->folder_id(), type_id => $self->breeding_program_trial_relationship_id() });
195 if ($breeding_program_rel_row) {
196 my $parent_row = $self->bcs_schema()->resultset('Project::Project')->find( { project_id=> $breeding_program_rel_row->object_project_id() });
197 $self->project_parent($parent_row);
198 $self->breeding_program($parent_row);
201 my $folder_rel_row = $self->bcs_schema()->resultset('Project::ProjectRelationship')->find( { subject_project_id => $self->folder_id(), type_id => $self->folder_cvterm_id() });
202 if ($folder_rel_row) {
203 my $parent_row = $self->bcs_schema()->resultset('Project::Project')->find( { project_id=> $folder_rel_row->object_project_id() });
204 $self->project_parent($parent_row);
209 # class methods
211 sub create {
212 my $class = shift;
213 my $args = shift;
214 my $schema = $args->{bcs_schema};
215 my $folder_name = $args->{name};
216 my $description = $args->{description} || "";
217 my $breeding_program_id = $args->{breeding_program_id};
218 my $parent_folder_id = $args->{parent_folder_id};
219 my $folder_for_trials = $args->{folder_for_trials};
220 my $folder_for_crosses = $args->{folder_for_crosses};
221 my $folder_for_genotyping_trials = $args->{folder_for_genotyping_trials};
222 my $folder_for_genotyping_projects = $args->{folder_for_genotyping_projects};
223 my $folder_for_tracking_activities = $args->{folder_for_tracking_activities};
224 my $folder_for_transformations = $args->{folder_for_transformations};
225 my $additional_info = $args->{additional_info} || undef;
227 # check if name is already taken
228 my $check_rs = $schema->resultset('Project::Project')->search( { name => $folder_name } );
229 if ($check_rs->count() > 0) {
230 die "The name $folder_name cannot be used for a new folder because it already exists.";
233 my $folder_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'trial_folder', 'project_property');
235 my $project = $schema->resultset('Project::Project')->create({
236 name => $folder_name,
237 description => $description,
239 $project->create_projectprops({ $folder_cvterm->name() => '1' });
241 if ($folder_for_trials) {
242 my $folder_type_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'folder_for_trials', 'project_property');
243 $project->create_projectprops({ $folder_type_cvterm->name() => '1' });
245 if ($folder_for_crosses) {
246 my $folder_type_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'folder_for_crosses', 'project_property');
247 $project->create_projectprops({ $folder_type_cvterm->name() => '1' });
249 if ($folder_for_genotyping_trials) {
250 my $folder_type_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'folder_for_genotyping_trials', 'project_property');
251 $project->create_projectprops({ $folder_type_cvterm->name() => '1' });
253 if ($folder_for_genotyping_projects) {
254 my $folder_type_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'folder_for_genotyping_projects', 'project_property');
255 $project->create_projectprops({ $folder_type_cvterm->name() => '1' });
257 if ($folder_for_tracking_activities) {
258 my $folder_type_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'folder_for_tracking_activities', 'project_property');
259 $project->create_projectprops({ $folder_type_cvterm->name() => '1' });
261 if ($folder_for_transformations) {
262 my $folder_type_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'folder_for_transformations', 'project_property');
263 $project->create_projectprops({ $folder_type_cvterm->name() => '1' });
265 if ($additional_info){
266 my $additional_info_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'project_additional_info', 'project_property');
267 $project->create_projectprops({ $additional_info_cvterm->name() => encode_json($additional_info) });
270 my $folder = CXGN::Trial::Folder->new({
271 bcs_schema => $schema,
272 folder_id => $project->project_id()
274 $folder->associate_parent($parent_folder_id);
275 $folder->associate_breeding_program($breeding_program_id);
277 return $folder;
280 #CLASS function
281 sub list {
282 my $class = shift;
283 my $args = shift;
284 my $schema = $args->{bcs_schema};
285 my $breeding_program_id = $args->{breeding_program_id};
286 my $folder_for_trials = $args->{folder_for_trials};
287 my $folder_for_crosses = $args->{folder_for_crosses};
288 my $folder_for_genotyping_trials = $args->{folder_for_genotyping_trials};
289 my $folder_for_genotyping_projects = $args->{folder_for_genotyping_projects};
290 my $folder_for_tracking_activities = $args->{folder_for_tracking_activities};
291 my $folder_for_transformations = $args->{folder_for_transformations};
292 my $folder_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema,'trial_folder', 'project_property')->cvterm_id();
293 my $breeding_program_trial_relationship_id = SGN::Model::Cvterm->get_cvterm_row($schema,'breeding_program_trial_relationship', 'project_relationship')->cvterm_id();
295 my %object_project_params;
296 $object_project_params{'me.type_id'} = $breeding_program_trial_relationship_id;
297 if ($breeding_program_id){
298 $object_project_params{'me.object_project_id'} = $breeding_program_id;
301 my %projectprop_params;
302 if (!$folder_for_trials && !$folder_for_crosses && !$folder_for_genotyping_trials && !$folder_for_genotyping_projects && !$folder_for_tracking_activities && !$folder_for_transformations){
303 $projectprop_params{'projectprops.type_id'} = $folder_cvterm_id;
304 } elsif ($folder_for_trials){
305 my $folder_type_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'folder_for_trials', 'project_property')->cvterm_id();
306 $projectprop_params{'projectprops.type_id'} = $folder_type_cvterm_id;
307 } elsif ($folder_for_crosses){
308 my $folder_type_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'folder_for_crosses', 'project_property')->cvterm_id();
309 $projectprop_params{'projectprops.type_id'} = $folder_type_cvterm_id;
310 } elsif ($folder_for_genotyping_trials){
311 my $folder_type_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'folder_for_genotyping_trials', 'project_property')->cvterm_id();
312 $projectprop_params{'projectprops.type_id'} = $folder_type_cvterm_id;
313 } elsif ($folder_for_genotyping_projects){
314 my $folder_type_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'folder_for_genotyping_projects', 'project_property')->cvterm_id();
315 $projectprop_params{'projectprops.type_id'} = $folder_type_cvterm_id;
316 } elsif ($folder_for_tracking_activities){
317 my $folder_type_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'folder_for_tracking_activities', 'project_property')->cvterm_id();
318 $projectprop_params{'projectprops.type_id'} = $folder_type_cvterm_id;
319 } elsif ($folder_for_transformations){
320 my $folder_type_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'folder_for_transformations', 'project_property')->cvterm_id();
321 $projectprop_params{'projectprops.type_id'} = $folder_type_cvterm_id;
324 my $breeding_program_rel = $schema->resultset('Project::ProjectRelationship')->search(\%object_project_params)->search_related("subject_project")->search_related("projectprops", \%projectprop_params, {'+select'=>'subject_project.name', '+as'=>'name' } );
326 my @folders;
327 while (my $row = $breeding_program_rel->next()) {
328 push @folders, [ $row->project_id(), $row->get_column('name') ];
331 return @folders;
336 ### OBJECT METHODS
338 # return a resultset with children of the folder
340 sub _get_children {
341 my $self = shift;
343 my @children;
345 my $folder_cvterm_id = $self->folder_cvterm_id();
346 my $rs = $self->bcs_schema()->resultset("Project::Project")->search_related( 'project_relationship_subject_projects', { object_project_id => $self->folder_id(), 'project_relationship_subject_projects.type_id' => $folder_cvterm_id }, { order_by => 'me.name' });
348 @children = map { $_->subject_project_id() } $rs->all();
350 my @child_folders;
351 foreach my $id (@children) {
352 my $folder = CXGN::Trial::Folder->new({
353 bcs_schema=> $self->bcs_schema(),
354 folder_id=>$id,
357 if ($self->folder_type() eq "breeding_program") {
358 if ($folder->project_parent()) {
359 if ($folder->project_parent()->name() eq $self->name()) {
360 #print STDERR "Pushing ".$folder->name().$folder->folder_type."\n";
361 push @child_folders, $folder;
364 } else {
365 #print STDERR "parent is not a breeding program... pushing ".$folder->name().$folder->folder_type."...\n";
366 push @child_folders, $folder;
370 return \@child_folders;
373 # return a resultset with children of the folder quickly
375 sub fast_children {
377 my $self = shift;
378 my $schema = shift;
379 my $parent_type = shift;
380 my (@folder_contents, %children);
382 #print STDERR "Running get children for project ".$self->{'name'}." at time ".localtime()."\n";
384 if ($parent_type eq 'breeding_program') {
385 my $rs = $schema->resultset("Project::Project")->search_related(
386 'project_relationship_subject_projects',
387 { 'type.name' => 'trial_folder'
389 { join => 'type'
391 @folder_contents = map { $_->subject_project_id() } $rs->all();
394 my $rs = $schema->resultset("Project::Project")->search_related(
395 'project_relationship_subject_projects',
396 { object_project_id => $self->{'id'},
397 subject_project_id => { 'not in' => \@folder_contents }
399 { join => { subject_project => { projectprops => 'type' } },
400 '+select' => ['subject_project.name', 'projectprops.value', 'type.name'],
401 '+as' => ['project_name', 'project_value', 'project_type']
405 while (my $row = $rs->next) {
406 my $name = $row->get_column('project_name');
407 $children{$name}{'name'} = $name;
408 $children{$name}{'id'} = $row->subject_project_id();
409 if ($row->get_column('project_value')){
410 $children{$name}{$row->get_column('project_value')} = 1;
412 if ($row->get_column('project_type')){
413 $children{$name}{$row->get_column('project_type')} = 1;
417 #print STDERR "Finished running get children for project ".$self->{'name'}." at time ".localtime()."\n"; #Children are: ".Dumper(%children);
418 return %children
421 sub set_folder_content_type {
422 my $self = shift;
423 my $type = shift; #folder_for_trials or folder_for_crosses
424 my $boolean_value = shift; #0 or 1
426 my $folder_type_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, $type, 'project_property')->cvterm_id();
427 my $prop = $self->bcs_schema->resultset('Project::Projectprop')->find({
428 project_id => $self->folder_id,
429 type_id => $folder_type_cvterm_id
431 if ($boolean_value){
432 if (!$prop){
433 my $new_prop = $self->bcs_schema->resultset('Project::Projectprop')->create({
434 project_id => $self->folder_id,
435 type_id => $folder_type_cvterm_id,
436 value => '1'
439 } else {
440 if ($prop){
441 $prop->delete();
448 sub associate_parent {
449 my $self = shift;
450 my $parent_id = shift;
452 my $folder_cvterm_id = $self->folder_cvterm_id();
453 my $breeding_program_trial_relationship_id = $self->breeding_program_trial_relationship_id();
455 #If the user selects 'None' to remove the trial from the folder, then the parent_id will be passed as 0. No new parent will be created.
456 if ($parent_id == 0) {
457 $self->remove_parents;
458 return;
461 my $parent_row = $self->bcs_schema()->resultset("Project::Project")->find( { project_id => $parent_id } );
463 if (!$parent_row) {
464 print STDERR "The folder specified as parent does not exist";
465 return;
468 my $parentprop_row = $self->bcs_schema()->resultset("Project::Projectprop")->find( { project_id => $parent_id, type_id => { -in => [ $folder_cvterm_id, $breeding_program_trial_relationship_id ] } } );
470 if (!$parentprop_row) {
471 print STDERR "The specified parent folder is not of type folder or breeding program. Ignoring.";
472 return;
475 $self->remove_parents;
477 my $project_rel_row = $self->bcs_schema()->resultset('Project::ProjectRelationship')->create({
478 object_project_id => $parent_id,
479 subject_project_id => $self->folder_id(),
480 type_id => $folder_cvterm_id,
482 $project_rel_row->insert();
484 $self->project_parent($parent_row);
486 my $parent_is_child = check_if_folder_is_child_in_tree($self->bcs_schema, $parent_id, $self->children());
487 if ($parent_is_child) {
488 print STDERR 'Parent '.$parent_id.' is child in tree of folder '.$self->folder_id()."\n";
489 my $parent_folder = CXGN::Trial::Folder->new({
490 bcs_schema => $self->bcs_schema,
491 folder_id => $parent_id
493 $parent_folder->remove_parents;
498 sub remove_parents {
499 my $self = shift;
500 #Remove any previous parents
501 my $project_rels = $self->bcs_schema()->resultset('Project::ProjectRelationship')->search({
502 subject_project_id => $self->folder_id(),
503 type_id => $self->folder_cvterm_id()
506 if ($project_rels->count() > 0) {
507 while (my $p = $project_rels->next()) {
508 print STDERR $p->subject_project_id." : ".$p->object_project_id." : Removing parent folder association...\n";
509 $p->delete();
512 return;
515 sub check_if_folder_is_child_in_tree {
516 my $schema = shift;
517 my $folder_id = shift;
518 my $children = shift;
519 foreach (@$children) {
520 my $child_folder_id = $_->folder_id();
521 if ($child_folder_id == $folder_id) {
522 return 1;
523 } else{
524 #print STDERR $child_folder_id."\n";
525 my $child_folder = CXGN::Trial::Folder->new({
526 bcs_schema => $schema,
527 folder_id => $child_folder_id
529 return check_if_folder_is_child_in_tree($schema, $folder_id, $child_folder->children() );
533 return;
536 sub associate_breeding_program {
537 my $self = shift;
538 my $breeding_program_id = shift;
540 if (!$breeding_program_id) {
541 print STDERR "No breeding_program_id provided. Ignoring association.\n";
542 return;
545 my $project_rel_row = $self->bcs_schema()->resultset('Project::ProjectRelationship')->find(
546 { object_project_id => $breeding_program_id,
547 subject_project_id => $self->folder_id(),
550 if (! $project_rel_row) {
551 #print STDERR "Creating folder association with breeding program id= $breeding_program_id, folder_id = ".$self->folder_id().", type_id = ".$self->breeding_program_trial_relationship_id()."\n";
552 $project_rel_row = $self->bcs_schema()->resultset('Project::ProjectRelationship')->create(
554 object_project_id => $breeding_program_id,
555 subject_project_id => $self->folder_id(),
556 type_id => $self->breeding_program_trial_relationship_id(),
559 $project_rel_row->insert();
561 else {
562 $project_rel_row->object_project_id($breeding_program_id);
563 $project_rel_row->update();
566 my $row = $self->bcs_schema()->resultset('Project::Project')->find( { project_id=> $project_rel_row->object_project_id() });
567 $self->breeding_program($row);
571 sub delete_folder {
572 my $self = shift;
574 my $children = $self->children();
575 if (scalar(@$children) > 0) {
576 return;
578 my $delete_folder = $self->bcs_schema->resultset("Project::Project")->find({ project_id => $self->folder_id })->delete();
579 return 1;
582 sub rename_folder {
583 my $self = shift;
584 my $new_name = shift;
585 my $folder_exists = $self->get_folder_by_name($new_name);
586 return 0 if $folder_exists;
587 my $update_folder = $self->bcs_schema->resultset("Project::Project")->find({ project_id => $self->folder_id });
588 $update_folder->name($new_name );
589 $update_folder->update();
590 return 1;
593 sub get_folder_by_name {
594 my $self= shift;
595 my $name = shift;
596 my $exists = $self->bcs_schema->resultset("Project::Project")->search( { name => $name } );
597 my $count = $exists->count();
598 if ( $exists->count() > 0 ) { return 1 } else { return 0 }
599 return;
602 sub remove_parent {
608 sub remove_child {
613 sub get_jstree_html {
614 shift;
615 my $self = shift;
616 my $schema = shift;
617 my $parent_type = shift;
618 my $project_type_of_interest = shift // 'trial';
619 #print STDERR "Running get js tree html on project ".$self->{'name'}." at time ".localtime()."\n";
620 my ($folder_type_of_interest, $local_type_of_interest, $html);
622 if ($project_type_of_interest eq 'trial') {
623 $local_type_of_interest = 'design'; # there is no 'trial' project prop, so using this as a proxy
624 $folder_type_of_interest = 'folder_for_trials';
626 elsif ($project_type_of_interest eq 'cross') {
627 $local_type_of_interest = 'crossing_trial';
628 $folder_type_of_interest = 'folder_for_crosses';
630 elsif ($project_type_of_interest eq 'genotyping_trial') {
631 $local_type_of_interest = 'genotyping_plate'; # in order to match projectprop value
632 $folder_type_of_interest = 'folder_for_genotyping_trials';
634 elsif ($project_type_of_interest eq 'genotyping_project') {
635 $local_type_of_interest = 'genotype_data_project';
636 $folder_type_of_interest = 'folder_for_genotyping_projects';
638 elsif ($project_type_of_interest eq 'activity') {
639 $local_type_of_interest = 'activity_record';
640 $folder_type_of_interest = 'folder_for_tracking_activities';
642 elsif ($project_type_of_interest eq 'transformation') {
643 $local_type_of_interest = 'transformation_project';
644 $folder_type_of_interest = 'folder_for_transformations';
647 $html .= _jstree_li_html($schema, $parent_type, $self->{'id'}, $self->{'name'});
648 $html .= "<ul>";
650 my %children = fast_children($self, $schema, $parent_type);
651 # print STDERR Dumper \%children;
652 if (%children) {
653 foreach my $child (sort keys %children) {
654 #print STDERR "Working on child ".$children{$child}->{'name'}."\n";
656 if ($project_type_of_interest eq 'trial' && $children{$child}->{'analysis_experiment'}) {
657 $html .= _jstree_li_html($schema, 'analyses', $children{$child}->{'id'}, $children{$child}->{'name'})."</li>";
659 # elsif ($project_type_of_interest eq 'trial' && $children{$child}->{'genotype_data_project'}) {
660 # $html .= _jstree_li_html($schema, 'genotyping_data_project', $children{$child}->{'id'}, $children{$child}->{'name'})."</li>";
662 # elsif ($project_type_of_interest eq 'trial' && $children{$child}->{'pcr_genotype_data_project'}) {
663 # $html .= _jstree_li_html($schema, 'pcr_genotyping_data_project', $children{$child}->{'id'}, $children{$child}->{'name'})."</li>";
665 elsif ($project_type_of_interest eq 'trial' && $children{$child}->{'sampling_trial'}) {
666 $html .= _jstree_li_html($schema, 'sampling_trial', $children{$child}->{'id'}, $children{$child}->{'name'})."</li>";
668 elsif ($project_type_of_interest eq 'genotyping_project' && $children{$child}->{'genotype_data_project'}) {
669 $html .= _jstree_li_html($schema, 'genotyping_project', $children{$child}->{'id'}, $children{$child}->{'name'})."</li>";
671 elsif ($project_type_of_interest eq 'genotyping_project' && $children{$child}->{'pcr_genotype_data_project'}) {
672 $html .= _jstree_li_html($schema, 'genotyping_project', $children{$child}->{'id'}, $children{$child}->{'name'})."</li>";
674 elsif ($children{$child}->{$folder_type_of_interest}) {
675 $html .= get_jstree_html('shift', $children{$child}, $schema, 'folder', $project_type_of_interest);
677 elsif (!$children{$child}->{'folder_for_crosses'} && !$children{$child}->{'folder_for_genotyping_trials'} && !$children{$child}->{'folder_for_trials'} && !$children{$child}->{'folder_for_genotyping_projects'} && !$children{$child}->{'folder_for_tracking_activities'} && !$children{$child}->{'folder_for_transformations'} && $children{$child}->{'trial_folder'}) {
678 $html .= get_jstree_html('shift', $children{$child}, $schema, 'folder', $project_type_of_interest);
680 elsif (($local_type_of_interest eq 'design' && $children{$child}->{'genotyping_plate'}) || ($local_type_of_interest eq 'design' && $children{$child}->{'genotype_data_project'}) || ($local_type_of_interest eq 'design' && $children{$child}->{'pcr_genotype_data_project'})){
681 next; #skip genotyping plates in field trial tree
683 elsif ($children{$child}->{$local_type_of_interest}) { #Only display $project of interest types.
684 $html .= _jstree_li_html($schema, $project_type_of_interest, $children{$child}->{'id'}, $children{$child}->{'name'})."</li>";
688 $html .= '</ul></li>';
689 #print STDERR "Finished, returning with html at time ".localtime()."\n";
690 return $html;
693 sub _jstree_li_html {
694 my $schema = shift;
695 my $type = shift;
696 my $id = shift;
697 my $name = shift;
698 # print STDERR "TYPE =".Dumper($type)."\n";
700 my $url = '#';
701 if ($type eq 'trial' || $type eq 'genotyping_trial' || $type eq 'sampling_trial') {
702 $url = "/breeders/trial/".$id;
703 } elsif ($type eq 'folder') {
704 $url = "/folder/".$id;
705 } elsif ($type eq 'cross') {
706 $url = "/cross/".$id;
707 } elsif ($type eq 'analyses') {
708 $url = "/analyses/".$id;
709 } elsif ($type eq 'breeding_program') {
710 $url = "/breeders/program/".$id;
711 } elsif ($type eq 'genotyping_project') {
712 $url = "/breeders/trial/".$id;
713 } elsif ($type eq 'activity' || $type eq 'transformation') {
714 $url = "/breeders/trial/".$id;
717 return "<li data-jstree='{\"type\":\"$type\"}' id=\"$id\"><a href=\"$url\">".$name.'</a>';
723 __PACKAGE__->meta->make_immutable();