4 package SGN
::Controller
::AJAX
::Audit
;
8 use File
::Temp qw
| tempfile
|;
10 use File
::Spec qw
| catfile
|;
11 use File
::Basename qw
| basename
|;
14 use CXGN
::Dataset
::File
;
19 BEGIN { extends
'Catalyst::Controller::REST' };
22 default => 'application/json',
24 map => { 'application/json' => 'JSON' },
28 sub retrieve_results
: Path
('/ajax/audit/retrieve_results'){
31 my $drop_menu_option = $c->req->param('db_table_list_id');
32 my $q = "select * from audit.".$drop_menu_option.";";
34 my $h = $c->dbc->dbh->prepare($q);
39 while (my ($audit_ts, $operation, $username, $logged_in_user, $before, $after, $transactioncode, $primary_key, $is_undo) = $h->fetchrow_array) {
40 $all_audits[$counter] = [$audit_ts, $operation, $username, $logged_in_user, $before, $after, $transactioncode, $primary_key, $is_undo];
46 $json_string = encode_json
(\
@all_audits);
48 result
=> $json_string,
52 sub retrieve_table_names
: Path
('/ajax/audit/retrieve_table_names'){
55 my $q = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'audit'";
56 my $h = $c->dbc->dbh->prepare($q);
59 while (my ($drop_options) = $h->fetchrow_array) {
60 push @ids, $drop_options;
64 $json_string = encode_json
(\
@ids);
66 result1
=> $json_string,
70 sub retrieve_stock_audits
: Path
('/ajax/audit/retrieve_stock_audits'){
73 my $stock_uniquename = $c->req->param('stock_uniquename');
74 my $q = "SELECT * FROM audit.stock_audit;";
75 my $h = $c->dbc->dbh->prepare($q);
82 while (my ($audit_ts, $operation, $username, $logged_in_user, $before, $after, $transactioncode, $primary_key, $is_undo) = $h->fetchrow_array) {
83 $after[$counter] = $after;
84 $before[$counter] = $before;
85 $all_audits[$counter] = [$audit_ts, $operation, $username, $logged_in_user, $before, $after, $transactioncode, $primary_key, $is_undo];
91 for (my $i = 0; $i<$counter; $i++){
92 my $operation = $all_audits[$i][1];
93 my $stock_json_string;
95 if ($operation eq "DELETE"){
96 $stock_json_string = decode_json
($before[$i]);
98 $stock_json_string = decode_json
($after[$i]);
102 warn "Failed to decode JSON at index $i: $@";
103 next; # Skip this iteration in case of error
105 my $desired_uniquename = $stock_json_string->{'uniquename'};
106 if($stock_uniquename eq $desired_uniquename){
107 push @matches, $all_audits[$i];
111 my $stock_match_json;
112 $stock_match_json = encode_json
(\
@matches);
114 $c->stash->{rest
} = {
115 stock_match_after
=> $stock_match_json,
120 sub retrieve_trial_audits
: Path
('/ajax/audit/retrieve_trial_audits'){
123 my $trial_id = $c->req->param('trial_id');
124 my $q = "SELECT * FROM audit.project_audit;";
125 my $h = $c->dbc->dbh->prepare($q);
132 while (my ($audit_ts, $operation, $username, $logged_in_user, $before, $after, $transactioncode, $primary_key, $is_undo) = $h->fetchrow_array) {
133 $after[$counter] = $after;
134 $before[$counter] = $before;
135 $all_audits[$counter] = [$audit_ts, $operation, $username, $logged_in_user, $before, $after, $transactioncode, $primary_key, $is_undo];
141 my $num_matches = 0; #this is to make sure only matched audits go into the matches array
143 for (my $i = 0; $i<$counter; $i++){
144 my $operation = $all_audits[$i][1];
146 if($operation eq "DELETE"){
147 $json_string = decode_json
($before[$i]);
149 $json_string = decode_json
($after[$i]);
151 my $desired_trial_id = $json_string->{'project_id'};
153 if($trial_id eq $desired_trial_id){
156 $matches[$num_matches] = $all_audits[$i];
162 my $match_trial_json;
163 $match_trial_json = encode_json
(\
@matches);
165 $c->stash->{rest
} = {
166 match_project
=> $match_trial_json,