aggregate and update data
[sgn.git] / lib / CXGN / Transformation / AddAutogeneratedNameInfo.pm
blob7863e9046e4dc52a0ed2564f81ba2b94b67c8aac
1 package CXGN::Transformation::AddAutogeneratedNameInfo;
3 =head1 NAME
5 CXGN::Transformation::AddAutogeneratedNameInfo - a module to add autogenerated name metadata.
7 =head1 USAGE
10 =head1 DESCRIPTION
13 =head1 AUTHORS
15 Titima Tantikanjana (tt15@cornell.edu)
17 =cut
19 use Moose;
20 use MooseX::FollowPBP;
21 use Moose::Util::TypeConstraints;
22 use Try::Tiny;
23 use SGN::Model::Cvterm;
24 use Data::Dumper;
25 use JSON;
28 has 'schema' => (
29 is => 'rw',
30 isa => 'DBIx::Class::Schema',
31 predicate => 'has_schema',
32 required => 1,
35 has 'dbh' => (
36 is => 'rw',
37 predicate => 'has_dbh',
38 required => 1,
41 has 'breeding_program_id' => (
42 isa =>'Int',
43 is => 'rw',
44 predicate => 'has_breeding_program_id',
45 required => 1,
48 has 'prefix' => (
49 isa => 'Str',
50 is => 'rw',
51 predicate => 'has_prefix',
52 required => 1,
55 has 'current_serial_number' => (
56 isa => 'Int',
57 is => 'rw',
58 predicate => 'has_prefix',
59 required => 1,
62 has 'number_of_digits' => (
63 isa => 'Int',
64 is => 'rw',
65 predicate => 'has_number_of_digits',
68 has 'description' => (
69 isa => 'Str',
70 is => 'rw',
71 predicate => 'has_description',
74 has 'added_by' => (
75 isa => 'Int',
76 is => 'rw',
77 predicate => 'has_added_by',
78 required => 1,
81 has 'date' => (
82 isa => 'Str',
83 is => 'rw',
84 predicate => 'has_date',
88 sub add_info {
90 my $self = shift;
91 my $schema = $self->get_schema();
92 my $breeding_program_id = $self->get_breeding_program_id();
93 my $prefix = $self->get_prefix();
94 my $current_serial_number = $self->get_current_serial_number();
95 my $number_of_digits = $self->get_number_of_digits();
96 my $description = $self->get_description();
97 my $added_by = $self->get_added_by();;
98 my $date = $self->get_date();;
99 my $transaction_error;
101 my $coderef = sub {
103 my $name_metadata_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'autogenerated_name_metadata', 'project_property');
104 my %name_metadata;
105 $name_metadata{'current_serial_number'} = $current_serial_number;
106 $name_metadata{'number_of_digits'} = $number_of_digits;
107 $name_metadata{'description'} = $description;
108 $name_metadata{'added_by'} = $added_by;
109 $name_metadata{'date'} = $date;
111 my $stored_name_metadata_string;
112 my $new_name_metadata_string;
113 my $name_metadata_hash = {};
114 my $program = $schema->resultset('Project::Project')->find({ project_id => $breeding_program_id});
115 if (!$program) {
116 print STDERR "Breeding Program not found!\n";
117 return;
119 my $metadata_projectprop_rs = $program->projectprops({type_id => $name_metadata_cvterm->cvterm_id});
120 if ($metadata_projectprop_rs->count == 1){
121 $stored_name_metadata_string = $metadata_projectprop_rs->first->value();
122 $name_metadata_hash = decode_json $stored_name_metadata_string;
123 print STDERR "PREVIOUS METADATA =".Dumper($name_metadata_hash)."\n";
124 $name_metadata_hash->{$prefix} = \%name_metadata;
125 $new_name_metadata_string = encode_json $name_metadata_hash;
126 print STDERR "UPDATED METADATA =".Dumper($name_metadata_hash)."\n";
128 $metadata_projectprop_rs->first->update({value=>$new_name_metadata_string});
129 } elsif ($metadata_projectprop_rs->count > 1) {
130 print STDERR "More than one found!\n";
131 return;
132 } else {
133 $name_metadata_hash->{$prefix} = \%name_metadata;
134 $new_name_metadata_string = encode_json $name_metadata_hash;
135 print STDERR "NEW METADATA =".Dumper($name_metadata_hash)."\n";
137 $program->create_projectprops({$name_metadata_cvterm->name() => $new_name_metadata_string});
141 try {
142 $schema->txn_do($coderef);
143 } catch {
144 $transaction_error = $_;
147 if ($transaction_error) {
148 print STDERR "Transaction error adding autogenerated name metadata: $transaction_error\n";
149 return;
152 return 1;
158 #######
160 #######