add is_variable accessor.
[sgn.git] / lib / CXGN / BreedersToolbox / SoilData.pm
blobfc52b15c47374cab0c7822b02f5398fcadcf984f
1 package CXGN::BreedersToolbox::SoilData;
4 =head1 NAME
6 CXGN::BreedersToolbox::SoilData - a class to manage soil data
8 =head1 DESCRIPTION
10 The projectprop of type "soil_data_json" is stored as JSON.
12 =head1 EXAMPLE
14 my $soil_data = CXGN::BreedersToolbox::SoilData->new( { schema => $schema});
16 =head1 AUTHOR
18 Titima Tantikanjana <tt15@cornell.edu>
20 =cut
23 use Moose;
25 extends 'CXGN::JSONProp';
27 use JSON::Any;
28 use Data::Dumper;
29 use SGN::Model::Cvterm;
32 has 'description' => (isa => 'Str', is => 'rw');
34 has 'date' => (isa => 'Maybe[Str]', is => 'rw');
36 has 'gps' => (isa => 'Maybe[Str]', is => 'rw');
38 has 'type_of_sampling' => (isa => 'Str', is => 'rw');
40 has 'data_type_order' => (isa => 'ArrayRef', is => 'rw');
42 has 'soil_data_details' => (isa => 'HashRef', is => 'rw');
45 sub BUILD {
46 my $self = shift;
47 my $args = shift;
49 $self->prop_table('projectprop');
50 $self->prop_namespace('Project::Projectprop');
51 $self->prop_primary_key('projectprop_id');
52 $self->prop_type('soil_data_json');
53 $self->cv_name('project_property');
54 $self->allowed_fields([ qw | description date gps type_of_sampling data_type_order soil_data_details | ]);
55 $self->parent_table('project');
56 $self->parent_primary_key('project_id');
58 $self->load();
62 sub get_all_soil_data {
63 my $self = shift;
64 my $schema = $self->bcs_schema();
65 my $project_id = $self->parent_id();
66 my $type_id = $self->_prop_type_id();
68 my $soil_data_rs = $schema->resultset("Project::Projectprop")->search({ project_id => $project_id, type_id => $type_id }, { order_by => {-asc => 'projectprop_id'} });
69 my @soil_data_list;
70 while (my $r = $soil_data_rs->next()){
71 my $prop_id = $r->projectprop_id();
72 my $soil_data_json = $r->value();
73 my $soil_data_hash = JSON::Any->jsonToObj($soil_data_json);
74 my $description = $soil_data_hash->{'description'};
75 my $date = $soil_data_hash->{'date'};
76 my $gps = $soil_data_hash->{'gps'};
77 my $type_of_sampling = $soil_data_hash->{'type_of_sampling'};
78 my $data_type_order = $soil_data_hash->{'data_type_order'};
79 my $soil_data_details = $soil_data_hash->{'soil_data_details'};
81 push @soil_data_list, [$prop_id, $description, $date, $gps, $type_of_sampling, $data_type_order, $soil_data_details, $project_id];
84 return \@soil_data_list;
88 sub get_soil_data {
89 my $self = shift;
90 my $schema = $self->bcs_schema();
91 my $project_id = $self->parent_id();
92 my $projectprop_id = $self->prop_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;
98 my $soil_data_rs = $schema->resultset("Project::Projectprop")->find({ project_id => $project_id, projectprop_id => $projectprop_id, type_id => $type_id }, { order_by => {-asc => 'projectprop_id'} });
99 my $soil_data_json = $soil_data_rs->value();
100 my $soil_data_hash = JSON::Any->jsonToObj($soil_data_json);
101 my @soil_data_info;
103 foreach my $field (@fields) {
104 push @soil_data_info, $soil_data_hash->{$field};
107 return \@soil_data_info;
111 sub delete_soil_data {
112 my $self = shift;
113 my $schema = $self->bcs_schema();
114 my $dbh = $schema->storage()->dbh();
115 my $project_id = $self->parent_id();
116 my $projectprop_id = $self->prop_id();
117 my $type = $self->prop_type();
118 my $type_id = $self->_prop_type_id();
120 eval {
121 $dbh->begin_work();
123 my $q = "DELETE FROM projectprop WHERE projectprop_id = ? AND project_id = ? AND type_id = ?";
124 my $h = $dbh->prepare($q);
126 $h->execute($projectprop_id, $project_id, $type_id);
129 if ($@) {
130 print STDERR "An error occurred while deleting soil data "."$@\n";
131 $dbh->rollback();
132 return $@;
133 } else {
134 $dbh->commit();
135 return 0;