Merge pull request #5290 from solgenomics/topic/fix_upload_pehno
[sgn.git] / lib / CXGN / Stock / Catalog.pm
blob2ab6669881e6f3f660c00e0f833a5daaae681ba2
2 package CXGN::Stock::Catalog;
4 use Moose;
5 use Data::Dumper;
7 extends 'CXGN::JSONProp';
9 # a list of representative images, given as image_ids
10 has 'images' => ( isa => 'Maybe[ArrayRef]', is => 'rw' );
12 # list of hashrefs like { stock_center => { name => ..., count_available => ..., delivery_time => } }
13 has 'order_source' => ( isa => 'Maybe[ArrayRef]', is => 'rw');
15 # item type such as single accession or a set of 10 accessions
16 has 'item_type' => ( isa => 'Str', is => 'rw');
18 # material type such as seed or plant
19 has 'material_type' => ( isa => 'Str', is => 'rw');
21 # center that generates clones or seed
22 has 'material_source' => ( isa => 'Maybe[Str]', is => 'rw');
24 has 'category' => ( isa => 'Str', is => 'rw' );
26 has 'species' => ( isa => 'Str', is => 'rw' );
28 has 'variety' => ( isa => 'Maybe[Str]', is => 'rw' );
30 has 'breeding_program' => ( isa => 'Int', is => 'rw');
32 has 'additional_info' => ( isa => 'Maybe[Str]', is => 'rw' );
34 has 'contact_person_id' => ( isa => 'Int', is => 'rw') ;
36 has 'availability' => ( isa => 'Maybe[Str]', is => 'rw');
38 # a general human readable description of the stock
39 #has 'description' => ( isa => 'Str', is => 'rw' );
41 # availability status: in_stock, delayed, currently_unavailable ...
42 #has 'availability' => ( isa => 'Str', is => 'rw' );
45 sub BUILD {
46 my $self = shift;
47 my $args = shift;
49 $self->prop_table('stockprop');
50 $self->prop_namespace('Stock::Stockprop');
51 $self->prop_primary_key('stockprop_id');
52 $self->prop_type('stock_catalog_json');
53 $self->cv_name('stock_property');
54 $self->allowed_fields( [ qw | item_type species variety material_type category material_source additional_info breeding_program availability contact_person_id images | ] );
55 $self->parent_table('stock');
56 $self->parent_primary_key('stock_id');
58 $self->load();
61 sub get_catalog_items {
62 my $self = shift;
63 my $schema = $self->bcs_schema();
64 my $type = $self->prop_type();
65 my $type_id = $self->_prop_type_id();
66 my $key_ref = $self->allowed_fields();
67 my @fields = @$key_ref;
69 my $catalog_rs = $schema->resultset("Stock::Stockprop")->search({type_id => $type_id }, { order_by => {-asc => 'stock_id'} });
70 my @catalog_list;
71 while (my $r = $catalog_rs->next()){
72 my @each_row = ();
73 my $catalog_stock_id = $r->stock_id();
74 push @each_row, $catalog_stock_id;
75 my $item_detail_json = $r->value();
76 my $detail_hash = JSON::Any->jsonToObj($item_detail_json);
77 foreach my $field (@fields){
78 push @each_row, $detail_hash->{$field};
80 push @catalog_list, [@each_row];
82 # print STDERR "CATALOG LIST =".Dumper(\@catalog_list)."\n";
84 return \@catalog_list;
88 sub get_item_details {
89 my $self = shift;
90 my $args = shift;
91 my $schema = $self->bcs_schema();
92 my $item_id = $self->parent_id();
93 my $type = $self->prop_type();
94 my $type_id = $self->_prop_type_id();
95 my $key_ref = $self->allowed_fields();
96 my @fields = @$key_ref;
97 my @item_details;
98 my $item_details_rs = $schema->resultset("Stock::Stockprop")->find({stock_id => $item_id, type_id => $type_id});
99 my $details_json = $item_details_rs->value();
100 my $detail_hash = JSON::Any->jsonToObj($details_json);
101 foreach my $field (@fields){
102 push @item_details, $detail_hash->{$field};
105 return \@item_details;