modified key
[sgn.git] / lib / CXGN / BreedersToolbox / Delete.pm
blob9abf5e8a3524722b37e25a80da375777d8b2b853
2 package CXGN::BreedersToolbox::Delete;
4 use Moose;
6 use Data::Dumper;
8 has bcs_schema => (is => 'rw');
9 has metadata_schema => (is=> 'rw');
10 has phenome_schema => (is => 'rw');
12 =head2 delete_experiments_by_file
13 SEEMS TO BE DEPRECATED BECAUSE CRUCIAL FUNCTIONS WITHIN THIS CALL ARE COMMENTED OUT
15 Currently using SGN::Controller::AJAX::BreedersToolbox->delete_uploaded_phenotype_files to delete phenotype files from manage phenotype page
17 Usage: $cpd->delete_experiments_by_file($user_id, $md_file_id);
18 Desc: deletes the phenotype information associated with file $md_file_id
19 Ret: a hash with deletion statistics
20 Args: a user_id (for privilege check),
21 an md_file_id (primary key of metadata.md_files)
22 Side Effects: connects to the database and deletes information (be careful!)
23 Example:
25 =cut
27 # sub delete_experiments_by_file {
28 # my $self = shift;
29 # my $user_id = shift;
30 # my $md_file_id = shift;
32 # print STDERR "Get the md_file entry... ";
33 # my $srs = $self->metadata_schema->resultset("MdFiles")->search( { file_id => $md_file_id } );
35 # print STDERR "Retrieved ".$srs->count()." entries.\n";
36 # if ($srs->count() == 0) {
37 # return "The file specified does not exist."
38 # }
40 # my $file_row = $srs->first();
41 # my $metadata_id = $file_row->metadata_id()->metadata_id();
43 # print STDERR "Get the associated md_metadata info... ($metadata_id, $user_id)";
45 # my $frs = $self->metadata_schema->resultset("MdMetadata")->search( { metadata_id => $metadata_id, create_person_id=>$user_id });
47 # print STDERR "Retrieved ".$frs->count()." entries.\n";
48 # if ($frs->count()==0) {
49 # return "You don't have the necessary privileges to delete this file";
50 # }
52 # print STDERR "Get the entries from the linking table... ";
53 # my $prs = $self->phenome_schema -> resultset("NdExperimentMdFiles")->search( { file_id => $md_file_id });
55 # print STDERR "Retrieved ".$prs->count()." entries.\n";
56 # if ($prs->count() == 0) {
57 # print STDERR "No experiments have been loaded for file with md_file_id $md_file_id\n";
58 # }
59 # else {
60 # foreach my $prs_row ($prs->rows()) {
61 # print STDERR "Deleting the MdExperiment entries... ";
63 # # first delete the entry in the linking table...
64 # #
65 # my $nd_experiment_id = $prs_row->nd_experiment_id();
66 # $prs_row->delete();
68 # $self->_delete_phenotype_experiments($nd_experiment_id);
69 # }
70 # }
72 # # set md_files and/or metadata to obsolote
73 # print STDERR "Update the md_file table to obsolete... ";
74 # my $mdmd_row = $self->metadata_schema->resultset("MdMetadata")->find( { metadata_id => $metadata_id } );
75 # if ($mdmd_row) {
76 # $mdmd_row -> update( { obsolete => 1 });
77 # }
78 # print STDERR "Done.\n";
79 # print STDERR "Delete complete.\n";
81 # }
83 # sub delete_phenotype_data_by_trial {
84 # my $self = shift;
85 # my $trial_id = shift;
88 # $self->bcs_schema->txn_do(
89 # sub {
90 # # first, delete metadata entries
91 # #
92 # $self->delete_metadata_by_trial($trial_id);
94 # # delete phenotype data associated with trial
95 # #
96 # my $trial = $self->bcs_schema()->resultset("Project::Project")->search( { project_id => $trial_id });
98 # my $nd_experiment_rs = $self->bcs_schema()->resultset("NaturalDiversity::NdExperimentProject")->search( { project_id => $trial_id });
99 # my @nd_experiment_ids = map { $_->nd_experiment_id } $nd_experiment_rs->all();
101 # $self->_delete_phenotype_experiments(@nd_experiment_ids); # cascading deletes should take care of everything (IT DOESNT????)
103 # });
107 # sub delete_field_layout_by_trial {
108 # my $self = shift;
109 # my $trial_id = shift;
111 # # first, delete metadata entries
113 # $self->bcs_schema()->txn_do(
114 # sub {
115 # $self->delete_metadata_by_trial($trial_id);
117 # my $trial = $self->bcs_schema()->resultset("Project::Project")->search( { project_id => $trial_id });
119 # my $nd_experiment_rs = $self->bcs_schema()->resultset("NaturalDiversity::NdExperimentProject")->search( { project_id => $trial_id });
120 # my @nd_experiment_ids = map { $_->nd_experiment_id } $nd_experiment_rs->all();
122 # print STDERR "ND EXPERIMENTS: ".(join ",", @nd_experiment_ids)."\n";
124 # print STDERR "DELETING trial layout for trial id $trial_id...\n";
125 # return $self->_delete_field_layout_experiment($trial_id);
127 # );
130 # sub delete_metadata_by_trial {
131 # my $self = shift;
132 # my $trial_id = shift;
135 # # first, deal with entries in the md_metadata table, which may reference nd_experiment (through linking table)
136 # my $q = "SELECT distinct(metadata_id) FROM nd_experiment_project JOIN phenome.nd_experiment_md_files using(nd_experiment_id) JOIN metadata.md_files using(file_id) JOIN metadata.md_metadata using(metadata_id) WHERE project_id=?";
137 # my $h = $self->bcs_schema->storage()->dbh()->prepare($q);
138 # $h->execute($trial_id);
140 # while (my ($md_id) = $h->fetchrow_array()) {
141 # my $mdmd_row = $self->metadata_schema->resultset("MdMetadata")->find( { metadata_id => $md_id } );
142 # if ($mdmd_row) {
143 # print STDERR "Update the md_metadata table to obsolete... for $md_id";
144 # $mdmd_row -> update( { obsolete => 1 });
148 # # delete the entries from the linking table...
149 # $q = "SELECT distinct(file_id) FROM nd_experiment_project JOIN phenome.nd_experiment_md_files using(nd_experiment_id) JOIN metadata.md_files using(file_id) JOIN metadata.md_metadata using(metadata_id) WHERE project_id=?";
150 # $h = $self->bcs_schema->storage()->dbh()->prepare($q);
151 # $h->execute($trial_id);
153 # while (my ($file_id) = $h->fetchrow_array()) {
154 # my $ndemdf_rs = $self->phenome_schema->resultset("NdExperimentMdFiles")->search( { file_id=>$file_id });
155 # print STDERR "Delete phenome.nd_experiment_md_files row for file_id $file_id...\n";
156 # foreach my $row ($ndemdf_rs->all()) {
157 # $row->delete();
163 # sub _delete_phenotype_experiments {
164 # my $self = shift;
165 # my @nd_experiment_ids = @_;
168 # print STDERR "Deleting the MdExperiment entries... ";
170 # # retrieve the associated phenotype ids (they won't be deleted by the cascade)
172 # my $phenotypes_deleted = 0;
173 # my $nd_experiments_deleted = 0;
175 # my $phenotype_rs = $self->bcs_schema()->resultset("NaturalDiversity::NdExperimentPhenotype")->search( { nd_experiment_id=> { -in => [ @nd_experiment_ids ] }}, { join => 'phenotype' });
176 # if ($phenotype_rs->count() > 0) {
177 # foreach my $p ($phenotype_rs->all()) {
178 # print STDERR "Deleting phenotype_id ".$p->phenotype_id()."\n";
179 # $p->delete();
180 # $phenotypes_deleted++;
184 # # delete the experiments
186 # my $delete_rs = $self->bcs_schema()->resultset("NaturalDiversity::NdExperiment")->search({ nd_experiment_id => { -in => [ @nd_experiment_ids] }});
187 # $nd_experiments_deleted = $delete_rs->count();
188 # $delete_rs->delete_all();
189 # print STDERR "Done.\n";
191 # return { phenotypes_deleted => $phenotypes_deleted,
192 # nd_experiments_deleted => $nd_experiments_deleted
193 # };
196 # =head2 _delete_field_layout_experiment
198 # Usage:
199 # Desc:
200 # Ret:
201 # Args:
202 # Side Effects:
203 # Example:
205 # =cut
207 # sub _delete_field_layout_experiment {
208 # my $self = shift;
209 # my $trial_id = shift;
211 # # check if there are still associated phenotypes...
213 # if ($self->trial_has_phenotype_data()) {
214 # print STDERR "Attempt to delete field layout that still has associated phenotype data.\n";
215 # return { error => "Trial still has associated phenotyping experiment, cannot delete." };
218 # my $field_layout_type_id = $self->bcs_schema->resultset("Cv::Cvterm")->find( { name => "field_layout" })->cvterm_id();
219 # print STDERR "Field layout type id = $field_layout_type_id\n";
221 # my $plot_type_id = $self->bcs_schema->resultset("Cv::Cvterm")->find( { name => 'plot' })->cvterm_id();
222 # print STDERR "Plot type id = $plot_type_id\n";
224 # my $q = "SELECT stock_id FROM nd_experiment_project JOIN nd_experiment USING (nd_experiment_id) JOIN nd_experiment_stock ON (nd_experiment.nd_experiment_id = nd_experiment_stock.nd_experiment_id) JOIN stock USING(stock_id) WHERE nd_experiment.type_id=? AND project_id=? AND stock.type_id=?";
225 # my $h = $self->bcs_schema->storage()->dbh()->prepare($q);
226 # $h->execute($field_layout_type_id, $trial_id, $plot_type_id);
228 # my $plots_deleted = 0;
229 # while (my ($plot_id) = $h->fetchrow_array()) {
230 # my $plot = $self->bcs_schema()->resultset("Stock::Stock")->find( { stock_id => $plot_id });
231 # print STDERR "Deleting associated plot ".$plot->name()." (".$plot->stock_id().") \n";
232 # $plots_deleted++;
233 # $plot->delete();
236 # $q = "SELECT nd_experiment_id FROM nd_experiment JOIN nd_experiment_project USING(nd_experiment_id) WHERE nd_experiment.type_id=? AND project_id=?";
237 # $h = $self->bcs_schema->storage()->dbh()->prepare($q);
238 # $h->execute($field_layout_type_id, $trial_id);
240 # my ($nd_experiment_id) = $h->fetchrow_array();
241 # if ($nd_experiment_id) {
242 # print STDERR "Delete corresponding nd_experiment entry ($nd_experiment_id)...\n";
243 # my $nde = $self->bcs_schema()->resultset("NaturalDiversity::NdExperiment")->find( { nd_experiment_id => $nd_experiment_id });
244 # $nde->delete();
248 # #return { success => $plots_deleted };
249 # return { success => 1 };
252 # sub trial_has_phenotype_data {
253 # my $self = shift;
254 # my $trial_id = shift;
256 # my $phenotyping_experiment_type_id = $self->bcs_schema->resultset("Cv::Cvterm")->find( { name => 'phenotyping_experiment' })->cvterm_id();
258 # my $phenotype_experiment_rs = $self->bcs_schema()->resultset("NaturalDiversity::NdExperimentProject")->search(
260 # project_id => $trial_id, 'nd_experiment.type_id' => $phenotyping_experiment_type_id},
262 # join => 'nd_experiment'
264 # );
266 # return $phenotype_experiment_rs->count();
270 sub plot_has_phenotype_data {
271 my $self = shift;
272 my $plot_id = shift;
274 my $phenotype_rs = $self->bcs_schema->resultset("Stock::Stock")->search( { stock_id => $plot_id }, { join => { 'phenotypes' }});
276 if ($phenotype_rs->count() > 0) {
277 return 1;
279 return 0;