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>
37 has
'dbh' => ( isa
=> 'DBI::db',
42 has
'list_id' => (isa
=> 'Int',
47 has
'owner' => (isa
=> 'Int',
51 has
'name' => (isa
=> 'Str',
55 has
'description' => (isa
=> 'Str',
59 has
'type' => (isa
=> 'Str',
63 has
'elements' => (isa
=> 'ArrayRef',
67 # class method: Use like so: CXGN::List::create_list
70 my ($name, $desc, $owner) = @_;
73 my $q = "INSERT INTO sgn_people.list (name, description, owner) VALUES (?, ?, ?) RETURNING list_id";
74 my $h = $dbh->prepare($q);
75 $h->execute($name, $desc, $owner);
76 ($new_list_id) = $h->fetchrow_array();
77 print STDERR
"NEW LIST using returning = $new_list_id\n";
79 #$q = "SELECT list.name, list.description, type_id, cvterm.name, list_id FROM sgn_people.list LEFT JOIN cvterm ON (type_id=cvterm_id)";
80 #$h = $dbh->prepare($q);
82 #while (my @data = $h->fetchrow_array()) {
83 #print STDERR join ", ", @data;
89 print "AN ERROR OCCURRED: $@\n";
95 # class method! see above
98 my $q = "SELECT cvterm_id, cvterm.name FROM cvterm JOIN cv USING(cv_id) WHERE cv.name = 'list_types' ";
99 my $h = $dbh->prepare($q);
102 while (my ($id, $name) = $h->fetchrow_array()) {
103 push @all_types, [ $id, $name ];
109 # class method! (see above)
111 sub available_lists
{
114 my $requested_type = shift;
116 my $q = "SELECT list_id, list.name, description, count(distinct(list_item_id)), type_id, cvterm.name, is_public 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 ORDER BY list.name";
117 my $h = $dbh->prepare($q);
121 while (my ($id, $name, $desc, $item_count, $type_id, $type, $public) = $h->fetchrow_array()) {
122 if ($requested_type) {
123 if ($type && ($type eq $requested_type)) {
124 push @lists, [ $id, $name, $desc, $item_count, $type_id, $type, $public ];
128 push @lists, [ $id, $name, $desc, $item_count, $type_id, $type, $public ];
134 sub available_public_lists
{
136 my $requested_type = shift;
138 my $q = "SELECT list_id, list.name, description, count(distinct(list_item_id)), type_id, cvterm.name, sp_person.username 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 ORDER BY list.name";
139 my $h = $dbh->prepare($q);
143 while (my ($id, $name, $desc, $item_count, $type_id, $type, $username) = $h->fetchrow_array()) {
144 if ($requested_type) {
145 if ($type && ($type eq $requested_type)) {
146 push @lists, [ $id, $name, $desc, $item_count, $type_id, $type, $username ];
150 push @lists, [ $id, $name, $desc, $item_count, $type_id, $type, $username ];
160 my $q = "DELETE FROM sgn_people.list WHERE list_id=?";
163 my $h = $dbh->prepare($q);
164 $h->execute($list_id);
167 return "An error occurred while deleting list with id $list_id: $@";
178 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=?";
179 my $h = $dbh->prepare($q);
180 $h->execute($name, $owner);
181 my ($list_id, $list_type) = $h->fetchrow_array();
184 return { list_id
=> $list_id, list_type
=> $list_type };
186 return { list_id
=> undef };
190 around
'BUILDARGS' => sub {
195 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;";
197 my $h = $args->{dbh
}->prepare($q);
198 $h->execute($args->{list_id
});
200 while (my ($content) = $h->fetchrow_array()) {
201 push @list, $content;
203 $args->{elements
} = \
@list;
205 $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=?";
206 $h = $args->{dbh
}->prepare($q);
207 $h->execute($args->{list_id
});
208 my ($name, $desc, $type_id, $list_type, $owner) = $h->fetchrow_array();
210 $args->{name
} = $name || '';
211 $args->{description
} = $desc || '';
212 $args->{type
} = $list_type || '';
213 $args->{owner
} = $owner;
215 return $class->$orig($args);
218 after
'name' => sub {
222 if (!$name) { return; }
224 my $q = "SELECT list_id FROM sgn_people.list where name=? and owner=?";
225 my $h = $self->dbh->prepare($q);
226 $h->execute($name, $self->owner());
227 my ($old_list) = $h->fetchrow_array();
229 return "The list name $name already exists. Please choose another name.";
232 $q = "UPDATE sgn_people.list SET name=? WHERE list_id=?"; #removed "my"
233 $h = $self->dbh->prepare($q);
236 $h->execute($name, $self->list_id());
239 return "An error occurred updating the list name ($@)";
244 after
'type' => sub {
248 if (!$type) { return; }
250 my $q1 = "SELECT cvterm_id FROM cvterm WHERE name =?";
251 my $h1 = $self->dbh->prepare($q1);
253 my ($cvterm_id) =$h1->fetchrow_array();
255 return "The specified type does not exist";
258 my $q = "SELECT owner FROM sgn_people.list WHERE list_id=?";
259 my $h = $self->dbh()->prepare($q);
260 $h->execute($self->list_id);
263 $q = "UPDATE sgn_people.list SET type_id=? WHERE list_id=?";
264 $h = $self->dbh->prepare($q);
265 $h->execute($cvterm_id, $self->list_id);
268 return "An error occurred while updating the type of list ".self
->list_id." to $type. $@";
273 after
'description' => sub {
275 my $description = shift;
278 #print STDERR "NO desc provided... skipping!\n";
282 my $q = "UPDATE sgn_people.list SET description=? WHERE list_id=?";
283 my $h = $self->dbh->prepare($q);
286 $h->execute($description, $self->list_id());
289 return "An error occurred updating the list description ($@)";
298 #remove trailing spaces
299 $element =~ s/^\s+|\s+$//g;
301 return "Empty list elements are not allowed";
303 if ($self->exists_element($element)) {
304 return "The element $element already exists";
307 my $iq = "INSERT INTO sgn_people.list_item (list_id, content) VALUES (?, ?)";
308 my $ih = $self->dbh()->prepare($iq);
310 $ih->execute($self->list_id(), $element);
313 print STDERR Dumper
$@
;
314 return "An error occurred storing the element $element ($@)";
317 my $elements = $self->elements();
318 push @
$elements, $element;
319 $self->elements($elements);
327 my $h = $self->dbh()->prepare("DELETE FROM sgn_people.list_item where list_id=? and content=?");
330 $h->execute($self->list_id(), $element);
334 return "An error occurred while attempting to delete item $element";
336 my $elements = $self->elements();
337 my @clean = grep(!/^$element$/, @
$elements);
338 $self->elements(\
@clean);
342 sub remove_element_by_id
{
344 my $element_id = shift;
345 my $h = $self->dbh()->prepare("SELECT content FROM sgn_people.list_item where list_id=? and list_item_id=?");
348 $h->execute($self->list_id(), $element_id);
351 return "An error occurred while attempting to delete item $element_id";
353 my ($element) = $h->fetchrow_array();
355 if (my $error = $self->remove_element($element)) {
362 sub update_element_by_id
{
364 my $element_id = shift;
366 my $h = $self->dbh()->prepare("UPDATE sgn_people.list_item SET content=? where list_id=? and list_item_id=?");
369 $h->execute($content, $self->list_id(), $element_id);
372 return "An error occurred while attempting to update item $element_id";
378 sub replace_by_name
{
380 my $item_name = shift;
381 my $new_name = shift;
382 my $h = $self->dbh()->prepare("UPDATE sgn_people.list_item SET content=? where list_id=? and content=?");
385 $h->execute($new_name, $self->list_id(), $item_name);
388 return "An error occurred while attempting to update item $item_name";
396 my $item_name = shift;
397 my $h = $self->dbh()->prepare("DELETE FROM sgn_people.list_item WHERE list_id=? and content=?");
400 $h->execute($self->list_id(), $item_name);
403 return "An error occurred while attempting to remove item $item_name";
412 my $h = $self->dbh->prepare("SELECT count(*) from sgn_people.list_item WHERE list_id=?");
413 $h->execute($self->list_id());
414 my ($count) = $h->fetchrow_array();
421 my $h = $self->dbh->prepare("SELECT is_public FROM sgn_people.list WHERE list_id=?");
422 $h->execute($self->list_id());
423 my $public = $h->fetchrow_array();
427 my $h = $self->dbh->prepare("UPDATE sgn_people.list SET is_public='t' WHERE list_id=?");
428 $h->execute($self->list_id());
429 $rows_affected = $h->rows;
430 } elsif ($public == 1) {
431 my $h = $self->dbh->prepare("UPDATE sgn_people.list SET is_public='f' WHERE list_id=?");
432 $h->execute($self->list_id());
433 $rows_affected = $h->rows;
435 return ($public, $rows_affected);
441 my $h = $self->dbh->prepare("UPDATE sgn_people.list SET is_public='t' WHERE list_id=?");
442 $h->execute($self->list_id());
443 my $rows_affected = $h->rows;
444 return $rows_affected;
450 my $h = $self->dbh->prepare("UPDATE sgn_people.list SET is_public='f' WHERE list_id=?");
451 $h->execute($self->list_id());
452 my $rows_affected = $h->rows;
453 return $rows_affected;
460 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");
461 $h->execute($user_id, $self->list_id());
462 my $list_id = $h->fetchrow_array();
464 $h = $self->dbh->prepare("SELECT content FROM sgn_people.list_item WHERE list_id=?");
465 $h->execute($self->list_id());
467 while (my $el = $h->fetchrow_array) {
471 $self->add_bulk(\
@elements, $list_id);
476 sub check_if_public
{
478 my $h = $self->dbh->prepare("SELECT is_public FROM sgn_people.list WHERE list_id=?");
479 $h->execute($self->list_id());
480 my $public = $h->fetchrow_array();
488 my $q = "SELECT list_item_id FROM sgn_people.list join sgn_people.list_item using(list_id) where list.list_id =? and content = ?";
489 my $h = $self->dbh()->prepare($q);
490 $h->execute($self->list_id(), $item);
491 my ($list_item_id) = $h->fetchrow_array();
492 return $list_item_id;
498 my $q = "SELECT type_id FROM sgn_people.list WHERE list_id=?";
499 my $h = $self->dbh()->prepare($q);
500 $h->execute($self->list_id());
501 my ($type_id) = $h->fetchrow_array();
505 sub retrieve_elements_with_ids
{
509 my $q = "SELECT list_item_id, content from sgn_people.list_item WHERE list_id=? ORDER BY list_item_id ASC;";
511 my $h = $self->dbh()->prepare($q);
512 $h->execute($list_id);
514 while (my ($id, $content) = $h->fetchrow_array()) {
515 push @list, [ $id, $content ];
522 my $elements = shift;
523 my $list_id = shift // $self->list_id();
524 my %elements_in_list;
527 s/^\s+|\s+$//g for @
$elements;
528 #print STDERR Dumper $elements;
530 my $q = "SELECT content FROM sgn_people.list join sgn_people.list_item using(list_id) where list.list_id =?";
531 my $h = $self->dbh()->prepare($q);
532 $h->execute($list_id);
533 while (my $list_content = $h->fetchrow_array()) {
534 $elements_in_list{$list_content} = 1;
537 $q = "SELECT list_item_id FROM sgn_people.list_item ORDER BY list_item_id DESC LIMIT 1";
538 $h = $self->dbh()->prepare($q);
540 my $list_item_id = $h->fetchrow_array() + 1;
542 my $iq = "INSERT INTO sgn_people.list_item (list_item_id, list_id, content) VALUES";
546 $self->dbh()->begin_work;
549 foreach (@
$elements) {
550 if ($_ && !exists $elements_in_list{$_}){
552 $content =~ s/\'/\'\'/g;
553 push @values, [$list_item_id, $list_id, $content];
554 $elements_in_list{$content} = 1;
555 push @elements_added, $content;
559 push @duplicates, $_;
564 my $num_values = scalar(@values);
566 if ($step < $num_values) {
567 $iq = $iq." (".$_->[0].",".$_->[1].",'".$_->[2]."'),";
569 $iq = $iq." (".$_->[0].",".$_->[1].",'".$_->[2]."');";
573 #print STDERR Dumper $iq;
575 $self->dbh()->do($iq);
577 $self->dbh()->commit;
580 $self->dbh()->rollback;
581 return {error
=> "An error occurred in bulk addition to list. ($@)"};
584 $elements = $self->elements();
585 push @
$elements, \
@elements_added;
586 $self->elements($elements);
588 my %response = (count
=> $count, duplicates
=> \
@duplicates);
594 my $item_ids = shift;
595 my $items_ids_sql = join ',', @
$item_ids;
597 my $q = "DELETE FROM sgn_people.list_item WHERE list_id=? AND list_item_id IN ($items_ids_sql)";
598 my $h = $self->dbh()->prepare($q);
599 $h->execute($self->list_id());
604 no warnings
'uninitialized';
607 my $items = $self->retrieve_elements_with_ids($self->list_id);
611 push @item_ids, $_->[0];
612 push @contents, $_->[1];
616 @sorted = map { $_->[0] }
617 sort { $a->[1] <=> $b->[1] }
618 map { [$_, $_=~/(\d+)/ ] }
620 } elsif ($sort eq 'DESC'){
621 @sorted = map { $_->[0] }
622 sort { $b->[1] <=> $a->[1] }
623 map { [$_, $_=~/(\d+)/ ] }
629 $self->delete_bulk(\
@item_ids);
630 $self->add_bulk(\
@sorted, $self->list_id);