-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathupdateMRI_Upload.pl
executable file
·189 lines (158 loc) · 5.95 KB
/
updateMRI_Upload.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/perl
# Zia Mohades 2014
# zia.mohades@mcgill.ca
# Perl tool to update the mri_upload table
use strict;
use Cwd qw/ abs_path /;
use File::Basename qw/ dirname /;
use File::Find;
use File::Temp qw/ tempdir /;
use FindBin;
use Getopt::Tabular;
use File::Basename;
use lib "$FindBin::Bin";
use DICOM::DICOM;
use DB::DBI;
my $verbose = 0;
my $profile = undef;
my $source_location = '';
my $tarchive = '';
my $query = '';
my $sth = undef;
my $tarchiveID = 0;
my $User = `whoami`;
my $versionInfo = sprintf "%d revision %2d", q$Revision: 1.24 $
=~ /: (\d+)\.(\d+)/;
my $globArchiveLocation = 0; # whether to use strict ArchiveLocation strings
# or to glob them (like '%Loc')
my $Help = <<HELP;
*******************************************************************************
Author :
Date :
Version : $versionInfo
This program does the following:
- Updates the mri_upload table to populate the fields/columns:
UploadedBy,UploadDate,TarchiveID and SourceLocation
HELP
my $Usage = "------------------------------------------
$0 updates the mri_upload table to populate the fields
Usage:\n\t $0 -profile <profile>
\n\n See $0 -help for more info\n\n";
my @arg_table =
(
["Main options", "section"],
["-profile","string",1, \$profile, "Specify the name of the config file
which resides in .loris_mri in the current directory."],
["-verbose", "boolean", 1, \$verbose, "Be verbose."],
["-globLocation", "boolean", 1, \$globArchiveLocation, "Loosen the".
" validity check of the tarchive allowing for the possibility that".
" the tarchive was moved to a different directory."],
["-sourceLocation", "string", 1, \$source_location, "The location".
" where the uploaded file exists."],
["-tarchivePath","string",1, \$tarchive, "The absolute path to".
" tarchive-file"]
);
# Parse arguments
&Getopt::Tabular::SetHelp($Help, $Usage);
&Getopt::Tabular::GetOptions(\@arg_table, \@ARGV) || exit 1;
################################################################
################# checking for profile settings#################
################################################################
if (-f "$ENV{LORIS_CONFIG}/.loris_mri/$profile") {
{
package Settings; do "$ENV{LORIS_CONFIG}/.loris_mri/$profile"
}
}
if ($profile && !@Settings::db) {
print "\n\tERROR: You don't have a configuration file named '$profile' in:
$ENV{LORIS_CONFIG}/.loris_mri/ \n\n";
exit 2;
}
################################################################
################# if profile not specified######################
################################################################
if(!$profile) {
print $Usage; print "\n\tERROR: You must specify an existing profile.\n\n";
exit 3;
}
################################################################
################# if tarchive not specified#####################
################################################################
unless (-e $tarchive) {
print "\nERROR: Could not find archive $tarchive. \nPlease, make sure the
path to the archive is correct. Upload will exit now.\n\n\n";
exit 4;
}
################################################################
#################if the sourcelocation is not set###############
################################################################
unless (-e $source_location) {
print "\nERROR: Could not find sourcelocation $source_location \nPlease,
make sure the sourcelocation is correct. Upload will
exit now.\n\n\n";
exit 5;
}
################################################################
#####establish database connection if database option is set####
################################################################
my $dbh = &DB::DBI::connect_to_db(@Settings::db);
print "Connecting to database.\n" if $verbose;
################################################################
#####check to see if the tarchiveid is already set or not#######
######if it's already in the mri_upload table then it will######
######generate an error#########################################
################################################################
# fetch tarchiveLibraryDir from ConfigSettings in the database
my $tarchiveLibraryDir = &DB::DBI::getConfigSetting(
\$dbh,'tarchiveLibraryDir'
);
# determine tarchive path stored in the database (without tarchiveLibraryDir)
my $tarchive_path = $tarchive;
$tarchive_path =~ s/$tarchiveLibraryDir\/?//g;
my $where = " WHERE t.ArchiveLocation =?";
if ($globArchiveLocation) {
$where = " WHERE t.ArchiveLocation LIKE ?";
$tarchive_path = basename($tarchive);
}
($query = <<QUERY) =~ s/\n/ /gm;
SELECT
COUNT(*)
FROM
mri_upload m
JOIN
tarchive t ON (t.TarchiveID=m.TarchiveID)
QUERY
$query .= $where;
$sth = $dbh->prepare($query);
$sth->execute($tarchive_path);
my $count = $sth->fetchrow_array;
if($count>0) {
print "\n\tERROR: the tarchive is already uploaded \n\n";
exit 6;
}
################################################################
#####get the tarchiveid from tarchive table#####################
################################################################
($query = <<QUERY) =~ s/\n/ /gm;
SELECT
t.TarchiveID
FROM
tarchive t
QUERY
$query .= $where;
$sth = $dbh->prepare($query);
$sth->execute("%".$tarchive_path."%");
my $tarchiveID = $sth->fetchrow_array;
################################################################
#####populate the mri_upload columns with the correct values####
################################################################
($query = <<QUERY) =~ s/\n/ /gm;
INSERT INTO mri_upload
(UploadedBy, UploadDate, TarchiveID, DecompressedLocation)
VALUES
(?, now(), ?, ?)
QUERY
my $mri_upload_insert = $dbh->prepare($query);
$mri_upload_insert->execute($User,$tarchiveID,$source_location);
print "Done updateMRI_upload.pl execution!\n" if $verbose;
exit 0;