minor fixes
[sgn.git] / lib / CXGN / Stock / StockLookup.pm
blob14e01626d5f859a3440b75a2b06794a111791d64
1 package CXGN::Stock::StockLookup;
3 =head1 NAME
5 CXGN::Stock::StockLookup - a module to lookup stock names by unique name or synonym.
7 =head1 USAGE
9 my $stock_lookup = CXGN::Stock::StockLookup->new({ schema => $schema} );
12 =head1 DESCRIPTION
14 Looks up stocks ("Stock::Stock") that have a match with the unique name or synonym to the searched name. Provides a count of matching stocks when more than one stock is found. Provides the Stock::Stock object when only a single stock matches.
16 =head1 AUTHORS
18 Jeremy D. Edwards (jde22@cornell.edu)
20 =cut
22 use Moose;
23 use MooseX::FollowPBP;
24 use Moose::Util::TypeConstraints;
25 use Try::Tiny;
27 has 'schema' => (
28 is => 'rw',
29 isa => 'DBIx::Class::Schema',
30 lazy_build => 1,
33 =head2 predicate has_stock_name(), clearer clear_stock_name(), accessors stock_name()
35 functions to test, clear, set or get the stock name.
37 =cut
39 has 'stock_name' => (isa => 'Str', is => 'rw', predicate => 'has_stock_name', clearer => 'clear_stock_name');
41 =head2 function get_stock()
43 retrieves a stock row
45 =cut
47 sub get_stock {
48 my $self = shift;
49 my $stock_rs = $self->_get_stock_resultset();
50 my $stock;
51 if ($stock_rs->count == 1) {
52 $stock = $stock_rs->first;
53 } else {
54 return;
56 return $stock;
59 =head2 function get_stock_exact()
61 retrieves the stock row with an exact match to the stock name or synonym
63 =cut
65 sub get_stock_exact {
66 my $self = shift;
67 my $stock_rs = $self->_get_stock_resultset_exact();
68 my $stock;
69 if ($stock_rs->count == 1) {
70 $stock = $stock_rs->first;
71 } else {
72 return;
74 return $stock;
77 =head2 function get_matching_stock_count()
79 retrieves the number of stocks that match the name (or synonym)
81 =cut
83 sub get_matching_stock_count {
84 my $self = shift;
85 my $stock_name = $self->get_stock_name();
86 my $stock_rs = $self->_get_stock_resultset();
87 if (!$stock_rs) {
88 return;
90 my $stock_match_count = $stock_rs->count;
91 if (!$stock_match_count) {
92 return 0;
94 if ($stock_match_count == 0) {
95 return;
97 return $stock_match_count;
100 sub _get_stock_resultset {
101 my $self = shift;
102 my $schema = $self->get_schema();
103 my $stock_name = $self->get_stock_name();
104 my $stock_rs = $schema->resultset("Stock::Stock")
105 ->search({ 'me.is_obsolete' => { '!=' => 't' },
106 -or => [
107 'lower(me.uniquename)' => { like => lc($stock_name) },
109 -and => [
110 'lower(type.name)' => { like => '%synonym%' },
111 'lower(stockprops.value)' => { like => lc($stock_name) },
116 join => { 'stockprops' => 'type'} ,
117 distinct => 1
120 return $stock_rs;
123 sub _get_stock_resultset_exact {
124 my $self = shift;
125 my $schema = $self->get_schema();
126 my $stock_name = $self->get_stock_name();
127 my $stock_rs = $schema->resultset("Stock::Stock")
128 ->search({ 'me.is_obsolete' => { '!=' => 't' },
129 'lower(uniquename)' => lc($stock_name),
132 join => { 'stockprops' => 'type'} ,
133 distinct => 1,
136 return $stock_rs;
139 #######
141 #######