4 CXGN::List - class that deals with website lists
8 my $list = CXGN::List->new( { dbh => $dbh, list_id => 34 } );
10 my $name = $list->name();
11 my $elements = $list->elements();
12 my $owner_id = $list->owner();
13 my $type = $list->type();
14 $list->remove_element('blabla');
15 $list->add_bulk(['blabla', 'bla']);
18 Class function (without instantiation):
20 my $new_list_id = CXGN::List::create_list($dbh, $name, $desc, $owner);
21 my $lists = CXGN::List::available_lists($dbh);
22 my $list_id = CXGN::List::exists_list($dbh, $name, $owner);
23 CXGN::List::delete_list($dbh, $list_id);
27 Lukas Mueller <lam87@cornell.edu>
36 use CXGN
::Stock
::Seedlot
;
38 has
'dbh' => ( isa
=> 'DBI::db',
43 has
'list_id' => (isa
=> 'Int',
48 has
'owner' => (isa
=> 'Int',
52 has
'name' => (isa
=> 'Str',
56 has
'description' => (isa
=> 'Str',
60 has
'type' => (isa
=> 'Str',
64 has
'elements' => (isa
=> 'ArrayRef',
68 has
'schema' => (isa
=> 'Bio::Chado::Schema', is
=> 'rw');
70 has
'phenome_schema' => (isa
=> 'CXGN::Phenome::Schema',is
=> 'rw');
72 has
'create_date' => (isa
=> 'Str',
76 has
'modified_date' => (isa
=> 'Str',
81 # class method: Use like so: CXGN::List::create_list
85 my ($name, $desc, $owner) = @_;
88 my $q = "INSERT INTO sgn_people.list (name, description, owner) VALUES (?, ?, ?) RETURNING list_id";
89 my $h = $dbh->prepare($q);
90 $h->execute($name, $desc, $owner);
91 ($new_list_id) = $h->fetchrow_array();
92 print STDERR
"NEW LIST using returning = $new_list_id\n";
94 #$q = "SELECT list.name, list.description, type_id, cvterm.name, list_id FROM sgn_people.list LEFT JOIN cvterm ON (type_id=cvterm_id)";
95 #$h = $dbh->prepare($q);
97 #while (my @data = $h->fetchrow_array()) {
98 #print STDERR join ", ", @data;
104 print "AN ERROR OCCURRED: $@\n";
110 # class method! see above
113 my $q = "SELECT cvterm_id, cvterm.name FROM cvterm JOIN cv USING(cv_id) WHERE cv.name = 'list_types' ";
114 my $h = $dbh->prepare($q);
117 while (my ($id, $name) = $h->fetchrow_array()) {
118 if ($name ne 'catalog_items') {
119 push @all_types, [ $id, $name ];
126 # class method! (see above)
128 sub available_lists
{
131 my $requested_type = shift;
133 my $q = "SELECT list_id, list.name, description, count(distinct(list_item_id)), type_id, cvterm.name, is_public, list.create_date, list.modified_date FROM sgn_people.list left join sgn_people.list_item using(list_id) LEFT JOIN cvterm ON (type_id=cvterm_id) WHERE owner=? GROUP BY list_id, list.name, description, type_id, cvterm.name, is_public, list.create_date, list.modified_date ORDER BY list.name";
134 my $h = $dbh->prepare($q);
138 while (my ($id, $name, $desc, $item_count, $type_id, $type, $public, $timestamp, $modify_timestamp) = $h->fetchrow_array()) {
139 if ($requested_type) {
140 if ($type && ($type eq $requested_type)) {
141 push @lists, [ $id, $name, $desc, $item_count, $type_id, $type, $public, $timestamp, $modify_timestamp ];
145 push @lists, [ $id, $name, $desc, $item_count, $type_id, $type, $public, $timestamp, $modify_timestamp ];
151 sub available_public_lists
{
153 my $requested_type = shift;
155 my $q = "SELECT list_id, list.name, description, count(distinct(list_item_id)), type_id, cvterm.name, sp_person.username, list.create_date, list.modified_date FROM sgn_people.list LEFT JOIN sgn_people.sp_person AS sp_person ON (sgn_people.list.owner=sp_person.sp_person_id) LEFT JOIN sgn_people.list_item using(list_id) LEFT JOIN cvterm ON (type_id=cvterm_id) WHERE is_public='t' GROUP BY list_id, list.name, description, type_id, cvterm.name, sp_person.username, list.create_date, list.modified_date ORDER BY list.name";
156 my $h = $dbh->prepare($q);
160 while (my ($id, $name, $desc, $item_count, $type_id, $type, $username, $timestamp, $modify_timestamp) = $h->fetchrow_array()) {
161 if ($requested_type) {
162 if ($type && ($type eq $requested_type)) {
163 push @lists, [ $id, $name, $desc, $item_count, $type_id, $type, $username, $timestamp, $modify_timestamp];
167 push @lists, [ $id, $name, $desc, $item_count, $type_id, $type, $username, $timestamp, $modify_timestamp];
176 my $requested_type = shift;
180 my $q = "SELECT list_id, list.name, description, count(distinct(list_item_id)), type_id, cvterm.name, is_public, list.create_date, list.modified_date FROM sgn_people.list left join sgn_people.list_item using(list_id) LEFT JOIN cvterm ON (type_id=cvterm_id) WHERE owner=? GROUP BY list_id, list.name, description, type_id, cvterm.name, is_public, list.create_date, list.modified_date ORDER BY list.name";
181 $h = $dbh->prepare($q);
184 my $q = "SELECT list_id, list.name, description, count(distinct(list_item_id)), type_id, cvterm.name, is_public, list.create_date, list.modified_date FROM sgn_people.list left join sgn_people.list_item using(list_id) LEFT JOIN cvterm ON (type_id=cvterm_id) GROUP BY list_id, list.name, description, type_id, cvterm.name, is_public, list.create_date, list.modified_date ORDER BY list.name";
185 $h = $dbh->prepare($q);
190 while (my ($id, $name, $desc, $item_count, $type_id, $type, $public, $timestamp, $modify_timestamp) = $h->fetchrow_array()) {
191 if ($requested_type) {
192 if ($type && ($type eq $requested_type)) {
193 push @lists, [ $id, $name, $desc, $item_count, $type_id, $type, $public, $timestamp, $modify_timestamp ];
197 push @lists, [ $id, $name, $desc, $item_count, $type_id, $type, $public, $timestamp, $modify_timestamp ];
207 my $q = "DELETE FROM sgn_people.list WHERE list_id=?";
210 my $h = $dbh->prepare($q);
211 $h->execute($list_id);
214 return "An error occurred while deleting list with id $list_id: $@";
225 my $q = "SELECT list_id, cvterm.name FROM sgn_people.list AS list LEFT JOIN cvterm ON (type_id=cvterm_id) WHERE list.name=? AND (list.owner=? OR list.is_public=TRUE)";
226 my $h = $dbh->prepare($q);
227 $h->execute($name, $owner);
228 my ($list_id, $list_type) = $h->fetchrow_array();
231 return { list_id
=> $list_id, list_type
=> $list_type };
233 return { list_id
=> undef };
236 around
'BUILDARGS' => sub {
241 my $q = "SELECT content from sgn_people.list join sgn_people.list_item using(list_id) WHERE list_id=? ORDER BY list_item_id ASC;";
243 my $h = $args->{dbh
}->prepare($q);
244 $h->execute($args->{list_id
});
246 while (my ($content) = $h->fetchrow_array()) {
247 push @list, $content;
249 $args->{elements
} = \
@list;
251 $q = "SELECT list.name, list.description, type_id, cvterm.name, owner FROM sgn_people.list LEFT JOIN cvterm ON (type_id=cvterm_id) WHERE list_id=?";
252 $h = $args->{dbh
}->prepare($q);
253 $h->execute($args->{list_id
});
254 my ($name, $desc, $type_id, $list_type, $owner) = $h->fetchrow_array();
256 $args->{name
} = $name || '';
257 $args->{description
} = $desc || '';
258 $args->{type
} = $list_type || '';
259 $args->{owner
} = $owner;
261 return $class->$orig($args);
264 after
'name' => sub {
268 if (!$name) { return; }
270 my $q = "SELECT list_id FROM sgn_people.list where name=? and owner=?";
271 my $h = $self->dbh->prepare($q);
272 $h->execute($name, $self->owner());
273 my ($old_list) = $h->fetchrow_array();
275 return "The list name $name already exists. Please choose another name.";
278 $q = "UPDATE sgn_people.list SET name=? WHERE list_id=?"; #removed "my"
279 $h = $self->dbh->prepare($q);
282 $h->execute($name, $self->list_id());
285 return "An error occurred updating the list name ($@)";
290 after
'type' => sub {
294 if (!$type) { return; }
296 my $q1 = "SELECT cvterm_id FROM cvterm WHERE name =?";
297 my $h1 = $self->dbh->prepare($q1);
299 my ($cvterm_id) =$h1->fetchrow_array();
301 return "The specified type does not exist";
304 my $q = "SELECT owner FROM sgn_people.list WHERE list_id=?";
305 my $h = $self->dbh()->prepare($q);
306 $h->execute($self->list_id);
309 $q = "UPDATE sgn_people.list SET type_id=? WHERE list_id=?";
310 $h = $self->dbh->prepare($q);
311 $h->execute($cvterm_id, $self->list_id);
314 return "An error occurred while updating the type of list ".self
->list_id." to $type. $@";
319 after
'description' => sub {
321 my $description = shift;
324 #print STDERR "NO desc provided... skipping!\n";
328 my $q = "UPDATE sgn_people.list SET description=? WHERE list_id=?";
329 my $h = $self->dbh->prepare($q);
332 $h->execute($description, $self->list_id());
335 return "An error occurred updating the list description ($@)";
344 #remove trailing spaces
345 $element =~ s/^\s+|\s+$//g;
347 return "Empty list elements are not allowed";
349 if ($self->exists_element($element)) {
350 return "The element $element already exists";
353 my $iq = "INSERT INTO sgn_people.list_item (list_id, content) VALUES (?, ?)";
354 my $ih = $self->dbh()->prepare($iq);
356 $ih->execute($self->list_id(), $element);
359 print STDERR Dumper
$@
;
360 return "An error occurred storing the element $element ($@)";
364 my $q = "UPDATE sgn_people.list SET modified_date = now() WHERE list_id=?";
365 my $h = $self->dbh()->prepare($q);
366 $h->execute($self->list_id());
370 my $elements = $self->elements();
371 push @
$elements, $element;
372 $self->elements($elements);
380 my $h = $self->dbh()->prepare("DELETE FROM sgn_people.list_item where list_id=? and content=?");
383 $h->execute($self->list_id(), $element);
387 return "An error occurred while attempting to delete item $element";
391 my $q = "UPDATE sgn_people.list SET modified_date = now() WHERE list_id=?";
392 my $h1 = $self->dbh()->prepare($q);
393 $h1->execute($self->list_id());
396 my $elements = $self->elements();
397 my @clean = grep(!/^$element$/, @
$elements);
398 $self->elements(\
@clean);
402 sub remove_element_by_id
{
404 my $element_id = shift;
405 my $h = $self->dbh()->prepare("SELECT content FROM sgn_people.list_item where list_id=? and list_item_id=?");
408 $h->execute($self->list_id(), $element_id);
411 return "An error occurred while attempting to delete item $element_id";
413 my ($element) = $h->fetchrow_array();
415 if (my $error = $self->remove_element($element)) {
422 sub update_element_by_id
{
424 my $element_id = shift;
426 my $h = $self->dbh()->prepare("UPDATE sgn_people.list_item SET content=? where list_id=? and list_item_id=?");
429 $h->execute($content, $self->list_id(), $element_id);
432 return "An error occurred while attempting to update item $element_id";
436 my $q = "UPDATE sgn_people.list SET modified_date = now() WHERE list_id=?";
437 my $h1 = $self->dbh()->prepare($q);
438 $h1->execute($self->list_id());
444 sub replace_by_name
{
446 my $item_name = shift;
447 my $new_name = shift;
448 my $h = $self->dbh()->prepare("UPDATE sgn_people.list_item SET content=? where list_id=? and content=?");
451 $h->execute($new_name, $self->list_id(), $item_name);
454 return "An error occurred while attempting to update item $item_name";
458 my $q = "UPDATE sgn_people.list SET modified_date = now() WHERE list_id=?";
459 my $h1 = $self->dbh()->prepare($q);
460 $h1->execute($self->list_id());
468 my $item_name = shift;
469 my $h = $self->dbh()->prepare("DELETE FROM sgn_people.list_item WHERE list_id=? and content=?");
472 $h->execute($self->list_id(), $item_name);
475 return "An error occurred while attempting to remove item $item_name";
479 my $q = "UPDATE sgn_people.list SET modified_date = now() WHERE list_id=?";
480 my $h1 = $self->dbh()->prepare($q);
481 $h1->execute($self->list_id());
490 my $h = $self->dbh->prepare("SELECT count(*) from sgn_people.list_item WHERE list_id=?");
491 $h->execute($self->list_id());
492 my ($count) = $h->fetchrow_array();
499 my $h = $self->dbh->prepare("SELECT is_public FROM sgn_people.list WHERE list_id=?");
500 $h->execute($self->list_id());
501 my $public = $h->fetchrow_array();
505 my $h = $self->dbh->prepare("UPDATE sgn_people.list SET is_public='t' WHERE list_id=?");
506 $h->execute($self->list_id());
507 $rows_affected = $h->rows;
508 } elsif ($public == 1) {
509 my $h = $self->dbh->prepare("UPDATE sgn_people.list SET is_public='f' WHERE list_id=?");
510 $h->execute($self->list_id());
511 $rows_affected = $h->rows;
513 return ($public, $rows_affected);
519 my $h = $self->dbh->prepare("UPDATE sgn_people.list SET is_public='t' WHERE list_id=?");
520 $h->execute($self->list_id());
521 my $rows_affected = $h->rows;
522 return $rows_affected;
528 my $h = $self->dbh->prepare("UPDATE sgn_people.list SET is_public='f' WHERE list_id=?");
529 $h->execute($self->list_id());
530 my $rows_affected = $h->rows;
531 return $rows_affected;
538 my $h = $self->dbh->prepare("INSERT INTO sgn_people.list (name, description, owner, type_id) SELECT name, description, ?, type_id FROM sgn_people.list as old WHERE old.list_id=? RETURNING list_id");
539 $h->execute($user_id, $self->list_id());
540 my $list_id = $h->fetchrow_array();
542 $h = $self->dbh->prepare("SELECT content FROM sgn_people.list_item WHERE list_id=?");
543 $h->execute($self->list_id());
545 while (my $el = $h->fetchrow_array) {
549 $self->add_bulk(\
@elements, $list_id);
554 sub check_if_public
{
556 my $h = $self->dbh->prepare("SELECT is_public FROM sgn_people.list WHERE list_id=?");
557 $h->execute($self->list_id());
558 my $public = $h->fetchrow_array();
566 my $q = "SELECT list_item_id FROM sgn_people.list join sgn_people.list_item using(list_id) where list.list_id =? and content = ?";
567 my $h = $self->dbh()->prepare($q);
568 $h->execute($self->list_id(), $item);
569 my ($list_item_id) = $h->fetchrow_array();
570 return $list_item_id;
576 my $q = "SELECT type_id FROM sgn_people.list WHERE list_id=?";
577 my $h = $self->dbh()->prepare($q);
578 $h->execute($self->list_id());
579 my ($type_id) = $h->fetchrow_array();
583 sub retrieve_elements_with_ids
{
587 my $q = "SELECT list_item_id, content from sgn_people.list_item WHERE list_id=? ORDER BY list_item_id ASC;";
589 my $h = $self->dbh()->prepare($q);
590 $h->execute($list_id);
592 while (my ($id, $content) = $h->fetchrow_array()) {
593 push @list, [ $id, $content ];
598 sub retrieve_elements
{
602 my $q = "SELECT list_item_id, content from sgn_people.list_item WHERE list_id=? ORDER BY list_item_id ASC;";
604 my $h = $self->dbh()->prepare($q);
605 $h->execute($list_id);
607 while (my ($item_id, $content) = $h->fetchrow_array()) {
608 push @list, $content;
615 my $elements = shift;
616 my $list_id = shift // $self->list_id();
617 my %elements_in_list;
620 s/^\s+|\s+$//g for @
$elements;
621 #print STDERR Dumper $elements;
623 my $q = "SELECT content FROM sgn_people.list join sgn_people.list_item using(list_id) where list.list_id =?";
624 my $h = $self->dbh()->prepare($q);
625 $h->execute($list_id);
626 while (my $list_content = $h->fetchrow_array()) {
627 $elements_in_list{$list_content} = 1;
630 $q = "SELECT list_item_id FROM sgn_people.list_item ORDER BY list_item_id DESC LIMIT 1";
631 $h = $self->dbh()->prepare($q);
633 my $list_item_id = $h->fetchrow_array() + 1;
635 my $iq = "INSERT INTO sgn_people.list_item (list_item_id, list_id, content) VALUES";
639 $self->dbh()->begin_work;
642 foreach (@
$elements) {
643 if ($_ && !exists $elements_in_list{$_}){
645 $content =~ s/\'/\'\'/g;
646 push @values, [$list_item_id, $list_id, $content];
647 $elements_in_list{$content} = 1;
648 push @elements_added, $content;
652 push @duplicates, $_;
657 my $num_values = scalar(@values);
659 if ($step < $num_values) {
660 $iq = $iq." (".$_->[0].",".$_->[1].",'".$_->[2]."'),";
662 $iq = $iq." (".$_->[0].",".$_->[1].",'".$_->[2]."');";
666 #print STDERR Dumper $iq;
668 $self->dbh()->do($iq);
670 $self->dbh()->commit;
673 $self->dbh()->rollback;
674 return {error
=> "An error occurred in bulk addition to list. ($@)"};
678 my $q = "UPDATE sgn_people.list SET modified_date = now() WHERE list_id=?";
679 my $h1 = $self->dbh()->prepare($q);
680 $h1->execute($list_id);
683 $elements = $self->elements();
684 push @
$elements, \
@elements_added;
685 $self->elements($elements);
687 my %response = (count
=> $count, duplicates
=> \
@duplicates);
693 my $item_ids = shift;
694 my $items_ids_sql = join ',', @
$item_ids;
696 my $q = "DELETE FROM sgn_people.list_item WHERE list_id=? AND list_item_id IN ($items_ids_sql)";
697 my $h = $self->dbh()->prepare($q);
698 $h->execute($self->list_id());
703 no warnings
'uninitialized';
706 my $items = $self->retrieve_elements_with_ids($self->list_id);
710 push @item_ids, $_->[0];
711 push @contents, $_->[1];
715 @sorted = map { $_->[0] }
716 sort { $a->[1] <=> $b->[1] }
717 map { [$_, $_=~/(\d+)/ ] }
719 } elsif ($sort eq 'DESC'){
720 @sorted = map { $_->[0] }
721 sort { $b->[1] <=> $a->[1] }
722 map { [$_, $_=~/(\d+)/ ] }
728 $self->delete_bulk(\
@item_ids);
729 $self->add_bulk(\
@sorted, $self->list_id);
734 sub seedlot_list_details
{
736 my $schema = $self->schema();
737 my $phenome_schema = $self->phenome_schema();
738 my $items = $self->elements();
739 my @seedlot_names = @
$items;
742 my $seedlot_type_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, "seedlot", "stock_type")->cvterm_id();
744 foreach my $seedlot(sort@seedlot_names) {
745 my $seedlot_rs = $schema->resultset("Stock::Stock")->find( { uniquename
=> $seedlot });
746 my $seedlot_id = $seedlot_rs->stock_id();
747 push @seedlot_ids, $seedlot_id;
750 foreach my $id (@seedlot_ids) {
754 my $seedlot_obj = CXGN
::Stock
::Seedlot
->new(
756 phenome_schema
=> $phenome_schema,
760 my $accessions = $seedlot_obj->accession();
761 my $crosses = $seedlot_obj->cross();
764 $content_name = $accessions->[1];
765 $content_id = $accessions->[0];
766 $content_type = 'accession'
770 $content_name = $crosses->[1];
771 $content_id = $crosses->[0];
772 $content_type = 'cross';
775 push @seedlot_details, [$id, $seedlot_obj->uniquename(), $content_id, $content_name, $content_type, $seedlot_obj->description(), $seedlot_obj->box_name(), $seedlot_obj->get_current_count_property(), $seedlot_obj->get_current_weight_property(), $seedlot_obj->quality()];
779 return \
@seedlot_details;