Merge pull request #64 from solgenomics/topic/update_view_permission
[SMMID.git] / lib / SMMID / Controller / Search.pm
blob0c7aa737782723c494aeedfe27ae843be9a2d395
2 package SMMID::Controller::Search;
4 use Moose;
5 use Data::Dumper;
6 use JSON::XS;
7 use SMMID::Authentication::ViewPermission;
9 BEGIN { extends 'Catalyst::Controller'; }
11 sub search : Path('/search') Args(0) {
12 my $self = shift;
13 my $c = shift;
15 $c->stash->{template} = '/search.mas';
18 sub search_results :Path('/search/results') Args(0) {
19 my $self = shift;
20 my $c = shift;
22 my $search_term = $c->req->param("term");
23 my $smid_id = $c->req->param("smid_id");
24 my $formula = $c->req->param("formula");
25 my $retention_time = $c->req->param("retention_time");
26 my $retention_time_range = $c->req->param("retention_time_range");
27 my $molecular_weight = $c->req->param("molecular_weight");
28 my $molecular_weight_range = $c->req->param("molecular_weight_range") || 0;
29 my $smiles = $c->req->param('smiles');
31 my $rs;
32 if ($search_term) { # simple search
33 print STDERR "Searching simple search with search term '$search_term'\n";
34 $rs = $c->model('SMIDDB')->resultset("SMIDDB::Result::Compound")->search( { -or => [ smid_id => { ilike => '%'.$search_term.'%' }, formula => { ilike => '%'.$search_term.'%'}, smiles => { ilike => '%'.$search_term.'%' } ] } );
37 else {
38 print STDERR "Complex search...\n";
39 $rs = $c->model('SMIDDB')->resultset("SMIDDB::Result::Compound")->search( {}, { join => 'experiments'} );
41 print STDERR "Currently matched ".$rs->count()." smids...\n";
43 if ($smid_id) {
44 $rs = $rs->search( { smid_id => { ilike => '%'.$smid_id.'%' }} );
45 print STDERR "With parameter smid_id = $smid_id matched ".$rs->count()." smids...\n";
48 if ($formula) {
49 $rs = $rs->search( { formula => { ilike => '%'.$formula.'%'} });
50 print STDERR "Currently matched ".$rs->count()." smids...\n";
53 if ($smiles) {
54 $rs = $rs->search( { smiles => { ilike => '%'.$smiles.'%' } });
55 print STDERR "Currently matched ".$rs->count()." smids...\n";
59 if ($molecular_weight) {
60 $rs = $rs->search(
61 { -and => [ molecular_weight => { '>' => ($molecular_weight - $molecular_weight_range) },
62 molecular_weight => { '<' => ($molecular_weight + $molecular_weight_range) } ] } );
63 print STDERR "after MW currently matched ".$rs->count()." smids...\n";
66 if ($retention_time) {
72 my @results;
73 while (my $row = $rs->next()) {
74 next if (!SMMID::Authentication::ViewPermission::can_view_smid($c->user(), $row, $c->model("SMIDDB")));
75 my $compound_id = $row->compound_id();
76 my $smid_id = $row->smid_id();
77 my $formula = $row->formula();
78 my $smiles = $row->smiles();
80 my $smid_link = '<a href=\"/smid/'.$compound_id.'\">'.$smid_id."</a>";
81 push @results, [ $smid_link, $formula, $smiles ];
86 #print STDERR Dumper(\@results);
88 $c->stash->{data} = encode_json(\@results);
90 #print STDERR "JSON: ".$c->stash->{data}."\n";
92 $c->stash->{template} = '/search/results.mas';