# $Id: BooleanListFilter.pm,v 1.3 2005/02/01 11:15:40 gilbertd Exp $ # # BioMart module for BioMart::Configuration::BooleanFilter # # You may distribute this module under the same terms as perl itself # POD documentation - main docs before the code =head1 NAME BioMart::Configuration::BooleanListFilter dgg - from BooleanFilter; allow list of several values (in) see also valuefilter. =head1 SYNOPSIS Object responsible for boolean included/excluded filters =head1 DESCRIPTION Object responsible for boolean included/excluded filters =head1 AUTHOR - Arek Kasprzyk, Damian Smedley =head1 CONTACT This module is part of the BioMart project http://www.ebi.ac.uk/biomart Questions can be posted to the mart-dev mailing list: mart-dev@ebi.ac.uk =head1 METHODS =cut package BioMart::Configuration::BooleanListFilter; # inherits from FilterBase, overrides toSQL use strict; use warnings; use Digest::MD5; use base qw(BioMart::Configuration::BaseFilter); ## BooleanFilter =head2 _new Usage : no arguments Description: creates a new BooleanFilter object which can contain a single Attribue object and a flag for excluded vs included Returntype : BioMart::Configuration::BooleanFilter Exceptions : none Caller : general =cut sub _new { my ($self, @param) = @_; $self->SUPER::_new(@param); $self->attr('attribute',undef); $self->attr('excluded',0); $self->attr('number_flag',0); $self->attr('attribute_table', undef); #dgg $self->attr('sql', undef); #dgg } sub _init { my ($self, @param) = @_; $self->SUPER::_init(@param); my $proto = shift @param; $self->attr('attribute', $proto->attribute); $self->attr('excluded', $proto->get('excluded')); $self->attr('number_flag',$proto->get('number_flag')); $self->attr('attribute_table', undef); #dgg $self->attr('sql', undef); #dgg } ## dgg; can we use instead _init ? sub initFromBoolFilter { my ($self, @param) = @_; my $proto = shift @param; ## $self->attr('params', $proto->get('params')); # but need to replace some my $paramCopy = {}; my $protoParams = $proto->get('params'); # $proto->_getParams; foreach my $key (keys %{$protoParams}) { $paramCopy->{$key} = $protoParams->{$key}; } ## not allowed ## $self->attr('params', $paramCopy ); $self->{'params'} = $paramCopy; $self->{'options'} = $proto->get('options'); # $self->attr('options', $proto->get('options')); ## bools $self->{'attribute'} = $proto->get('attribute');#$self->attr('attribute', $proto->get('attribute')); $self->{'excluded'} = $proto->get('excluded');#$self->attr('excluded', $proto->get('excluded')); $self->{'number_flag'} =$proto->get('number_flag'); #$self->attr('number_flag', $proto->get('number_flag')); } =head2 attribute Usage : Arg[1] - (optional) BioMart::Configuration::Attribute object Description: get/set method for the Attribute object for this BooleanFilter Returntype : BioMart::Configuration::Attribute object Exceptions : none Caller : caller =cut sub attribute { my ($self, $attribute) = @_; # stores attribute if ($attribute){ $self->set('attribute', $attribute); } return $self->get('attribute'); # my $attribute_table = $self->get('attribute_table'); # my $row = $attribute_table->nextRow(); # $attribute= $$row[0]; # return $attribute; } =head2 table Usage : Description: returns the table name associated with this filter; Returntype : String table name Exceptions : none Caller : caller =cut sub table { my $self = shift; my $attribute = $self->get('attribute'); # my $attribute = $self->attribute(); return $attribute->table; } =head2 setExcluded Usage : no arguments Description: sets this BooleanFilter as an exclude rather than include filter Returntype : none Exceptions : none Caller : caller =cut sub setExcluded { my $self = shift; $self->set('excluded',1); } =head2 setIncluded Usage : no arguments Description: sets this BooleanFilter as an include rather than exclude filter Returntype : none Exceptions : none Caller : caller =cut sub setIncluded { my $self = shift; $self->set('excluded',0); } =head2 setNumberFlag Usage : $filt->setNumberFlag; Description: set the number flag for 1,0, null type filters Returntype : none Exceptions : none Caller : caller =cut sub setNumberFlag { my $self = shift; $self->set('number_flag',1); } =head2 setTable dgg; from value filter =cut sub setTable { # stores BioMart::AttributeTable my ($self,$attribute_table) = @_; $self->set('attribute_table',$attribute_table); $self->set('sql',undef); } sub _toSQL { my $self = shift; # my $attribute = $self->attribute(); # my $condition = $self->get('number_flag') ? ' = 1' : ' IS NOT NULL'; # if ($self->get('excluded')){ # $condition = $self->get('number_flag') ? ' = 0' : ' IS NULL'; # } # return $attribute->toSQL().$condition; ## dgg my $sql = $self->get('sql'); if ($sql){ return $sql; } $sql = ''; my $or = ''; my $attribute_table = $self->get('attribute_table'); while (my $row = $attribute_table->nextRow()) { next unless ($$row[0]);#avoid NULL entries my $attribute= $$row[0]; $sql .= $or.$attribute->toSQL(); ## .$condition; $or = ($attribute->get('excluded')) ? ' AND ' : ' OR '; } $sql = '('.$sql.')' if ($sql); $self->set('sql',$sql); return $sql; } sub _hashCode { my $self = shift; my $digest = Digest::MD5->new; $digest->add($self->SUPER::_hashCode); $digest->add($self->table) if ($self->table); $digest->add($self->get('excluded')); $digest->add($self->attribute()->hashCode); return $digest->hexdigest; } 1;