<commit_before>
package SGN::Controller::AJAX::BreederSearch;

use Moose;

use List::MoreUtils qw | any all |;
use JSON::Any;
use Data::Dumper;
use CXGN::BreederSearch;

BEGIN { extends 'Catalyst::Controller::REST'; };

__PACKAGE__->config(
    default   => 'application/json',
    stash_key => 'rest',
    map       => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
    );

sub get_data : Path('/ajax/breeder/search') Args(0) { 
    my $self = shift;
    my $c = shift;
    
    my $criteria_list;
    my @selects = qw | select1 select2 select3 |;
    foreach my $s (@selects) { 
	my $value = $c->req->param($s);
	if ($value) { 
	    push @$criteria_list, $c->req->param($s);
	}
    }
    my $output = $c->req->param('select4') || 'plots';

    my $dataref = {};
    
    my @params = qw | c1_data c2_data c3_data |;
    my $data_tainted = 0;

    if (!$criteria_list) {
	$c->stash->{rest} = { };
	return;
    }
    for (my $i=0; $i<scalar(@$criteria_list); $i++) { 
	my $data;
	print STDERR "PARAM: $params[$i]\n";
	if (defined($params[$i]) && ($params[$i] ne '')) { $data =  $c->req->param($params[$i]); }
	if (defined($data) && ($data ne '') && ($data !~ /^[\d,\/ ]+$/g)) { 
	    print STDERR "Illegal chars in '$data'\n"; 
	    $data_tainted =1;
	}
	# items need to be quoted in sql
	#
	print STDERR "DATA: $data\n";
	if ($data) { 
	    my $qdata = join ",", (map { "\'$_\'"; } (split ",", $data));
	    $dataref->{$criteria_list->[-1]}->{$criteria_list->[$i]} = $qdata;
	}
    }
    
    if ($data_tainted) { 
	$c->stash->{rest} =  { error => "Illegal data.", };
	return;
    }
    
    my $stocks = undef;
    my $error = "";

     foreach my $select (@$criteria_list) { 
     	print STDERR "Checking $select\n";
     	chomp($select);
     	if (! any { $select eq $_ } ('accessions', 'projects', 'locations', 'years', 'traits', 'genotypes', undef)) { 
     	    $error = "Valid keys are projects, years, traits and locations";
     	    $c->stash->{rest} = { error => $error };
     	    return;
     	}
     }
     my $dbh = $c->dbc->dbh();

     my $item = $criteria_list->[-1];

     my $bs = CXGN::BreederSearch->new( { dbh=>$dbh } );
    
     my $results_ref = $bs->get_intersect($criteria_list, $dataref); 

    my $stock_ref = [];
    my $stockdataref->{$output} = $dataref->{$criteria_list->[-1]};

    push @$criteria_list, $output;
    print STDERR "OUTPUT: $output CRITERIA: ", Data::Dumper::Dumper($criteria_list);
    my $stock_ref = $bs->get_intersect($criteria_list, $stockdataref);
    
    print STDERR "RESULTS: ".Data::Dumper::Dumper($results_ref);

    if ($stock_ref->{message}) { 
	$c->stash->{rest} = { 
	    list => $results_ref->{results},
	    message => $stock_ref->{message},<commit_msg>fix variable declaration error message in breeders search<commit_after>
package SGN::Controller::AJAX::BreederSearch;

use Moose;

use List::MoreUtils qw | any all |;
use JSON::Any;
use Data::Dumper;
use CXGN::BreederSearch;

BEGIN { extends 'Catalyst::Controller::REST'; };

__PACKAGE__->config(
    default   => 'application/json',
    stash_key => 'rest',
    map       => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
    );

sub get_data : Path('/ajax/breeder/search') Args(0) { 
    my $self = shift;
    my $c = shift;
    
    my $criteria_list;
    my @selects = qw | select1 select2 select3 |;
    foreach my $s (@selects) { 
	my $value = $c->req->param($s);
	if ($value) { 
	    push @$criteria_list, $c->req->param($s);
	}
    }
    my $output = $c->req->param('select4') || 'plots';

    my $dataref = {};
    
    my @params = qw | c1_data c2_data c3_data |;
    my $data_tainted = 0;

    if (!$criteria_list) {
	$c->stash->{rest} = { };
	return;
    }
    for (my $i=0; $i<scalar(@$criteria_list); $i++) { 
	my $data;
	print STDERR "PARAM: $params[$i]\n";
	if (defined($params[$i]) && ($params[$i] ne '')) { $data =  $c->req->param($params[$i]); }
	if (defined($data) && ($data ne '') && ($data !~ /^[\d,\/ ]+$/g)) { 
	    print STDERR "Illegal chars in '$data'\n"; 
	    $data_tainted =1;
	}
	# items need to be quoted in sql
	#
	print STDERR "DATA: $data\n";
	if ($data) { 
	    my $qdata = join ",", (map { "\'$_\'"; } (split ",", $data));
	    $dataref->{$criteria_list->[-1]}->{$criteria_list->[$i]} = $qdata;
	}
    }
    
    if ($data_tainted) { 
	$c->stash->{rest} =  { error => "Illegal data.", };
	return;
    }
    
    my $stocks = undef;
    my $error = "";

     foreach my $select (@$criteria_list) { 
     	print STDERR "Checking $select\n";
     	chomp($select);
     	if (! any { $select eq $_ } ('accessions', 'projects', 'locations', 'years', 'traits', 'genotypes', undef)) { 
     	    $error = "Valid keys are projects, years, traits and locations";
     	    $c->stash->{rest} = { error => $error };
     	    return;
     	}
     }
     my $dbh = $c->dbc->dbh();

     my $item = $criteria_list->[-1];

     my $bs = CXGN::BreederSearch->new( { dbh=>$dbh } );
    
     my $results_ref = $bs->get_intersect($criteria_list, $dataref); 

    my $stock_ref = [];
    my $stockdataref->{$output} = $dataref->{$criteria_list->[-1]};

    push @$criteria_list, $output;
    print STDERR "OUTPUT: $output CRITERIA: ", Data::Dumper::Dumper($criteria_list);
    $stock_ref = $bs->get_intersect($criteria_list, $stockdataref);
    
    print STDERR "RESULTS: ".Data::Dumper::Dumper($results_ref);

    if ($stock_ref->{message}) { 
	$c->stash->{rest} = { 
	    list => $results_ref->{results},
	    message => $stock_ref->{message},
{'fraction_non_alphanumeric': 0.21209369892863628, 'fraction_numerical': 0.005084437988015254, 'mean_word_length': 2.959741193386053, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '659817', 'n_tokens_mistral': 2180, 'n_tokens_neox': 2089, 'n_words': 443}