-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem.groovy
217 lines (165 loc) · 6.87 KB
/
item.groovy
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import groovy.sql.Sql
class item_migrator {
static void main(String[] args) {
def config = new ConfigSlurper().parse(new File('config.groovy').toURL())
def sql = Sql.newInstance(config.old_db_url,config.old_db_user, config.old_db_pwd)
def newsql = Sql.newInstance(config.new_db_url,config.new_db_user, config.new_db_pwd)
// check that we have paths defined.
if (!config.old_asset_path)
throw new RuntimeException("old_asset_path is not defined, unable to migrate assets.");
if (!config.new_asset_path)
throw new RuntimeException("new_asset_path is not defined, unable to migrate assets.");
// Clear out any old attachments, both on disk and in the database.
newsql.execute("delete from attachment")
"rm -rf ${config.new_asset_path}/*".execute().waitFor();
sql.eachRow('''
SELECT DISTINCT
sub.submission_id,
sub.applicant_id,
item.last_modified,
sub.item_id,
bun.name as bundle_name,
bun.primary_bitstream_id,
bit.bitstream_id,
bit.internal_id,
fr.mimetype,
bit.name
FROM
item,
vireosubmission sub,
item2bundle i2b,
bundle bun,
bundle2bitstream b2b,
bitstream bit,
bitstreamformatregistry fr
WHERE
sub.item_id = item.item_id AND
sub.item_id = i2b. item_id AND
i2b.bundle_id = bun.bundle_id AND
i2b.bundle_id = b2b.bundle_id AND
bit.bitstream_id = b2b.bitstream_id AND
fr.bitstream_format_id = bit.bitstream_format_id
ORDER BY bitstream_id ASC
''') {
row ->
if (!submissionExists(newsql, row.submission_id)) {
return;
}
//public enum AttachmentType {
// UNKNOWN,
// PRIMARY,
// SUPPLEMENTAL,
// LICENSE,
// ARCHIVED,
// FEEDBACK
int type = 0; // unknown
if ("ORIGINAL".equals(row.bundle_name) || "CONTENT".equals(row.bundle_name)) {
if (row.primary_bitstream_id == row.bitstream_id) {
type = 1; // Primary
} else {
type = 2; // Supplemental
}
} else if ("LICENSE".equals(row.bundle_name)) {
type = 3; // license
} else if ("ARCHIVE".equals(row.bundle_name)) {
type = 4; // Archived
} else if ("NOTES".equals(row.bundle_name)) {
type = 5; // feedback
}
def old_file_path = name_to_path(row.internal_id)
def uuid = UUID.randomUUID().toString();
String name1 = uuid.substring(0, 2);
String name2 = uuid.substring(2, 4);
String name3 = uuid.substring(4, 6);
String name4 = uuid.substring(6, 8);
String hashDir = uuid.substring(0, 2) + File.separator + uuid.substring(2, 4) + File.separator + uuid.substring(4, 6) + File.separator + uuid.substring(6, 8);
// Make a link from the archive into the attachments directory
// We should probably create a UUID and 'mv' the asset into the attachments directory - with that nam
try {
// Make the hash directory;
runCommand("mkdir -p ${config.new_asset_path}/${hashDir}");
if ("cp".equals(config.asset_command)) {
// Actualy copy the assets for real
runCommand("cp ${config.old_asset_path}/${old_file_path}/${row.internal_id} ${config.new_asset_path}/${hashDir}/${uuid}");
} else if ("mv".equals(config.asset_command)) {
// Move the files destroying the original
runCommand("mv ${config.old_asset_path}/${old_file_path}/${row.internal_id} ${config.new_asset_path}/${hashDir}/${uuid}");
} else if ("ln".equals(config.asset_command)) {
// Link the files preserving the original and saving space.
runCommand("ln -s ${config.old_asset_path}/${old_file_path}/${row.internal_id} ${config.new_asset_path}/${hashDir}/${uuid}");
}
} catch (Exception ex) {
println("Exception " + ex);
}
// If name is not unique add on a number
def subname = row.name
def count = 1
// This has a problem where if there is more than two submissions with the same name
// they get the suffix of '12' and then '123' - unique - yes - but we should probably
// shave off the trailing number before adding the new one on
while(!is_name_unique(newsql, subname, row.submission_id)) {
//println("Fixing Name: " + subname)
def name_parts = subname.tokenize(".")
subname = name_parts[0] + count + "." + name_parts[1]
//println "\n\n** New Name: " + subname
count++
}
def params = [
row.bitstream_id,
uuid + "|" + row.mimetype,
row.last_modified,
subname,
type,
row.applicant_id,
row.submission_id
]
if (row.name != null) {
newsql.execute('''insert into attachment (
id,
data,
date,
name,
type,
person_id,
submission_id
)
values (
?,?,?,?,?,?,?
)''', params);
}
} // for each row
// Update sequence counter
def row = newsql.firstRow("select (max(id) + 1) max from attachment")
newsql.execute("alter sequence seq_attachment restart with " + row.max)
}
// Compute the path in the dspace asset directory given the long numeric name
static String name_to_path(String name) {
def p1 = name.substring(0,2)
def p2 = name.substring(2,4)
def p3 = name.substring(4,6)
def path = p1 + "/" + p2 + "/" + p3
return path
}
// Determine if a given filename is already used within this submission
static Boolean is_name_unique(Sql sql, String name, Integer submission_id) {
def params = [submission_id, name]
def row = sql.rows("select name from attachment where submission_id = ? and name = ?", params)
if (row.size ==0 ) {
return true
} else {
return false
}
}
// Determine if a submission exists
static Boolean submissionExists(Sql sql, Integer subId) {
def rows = sql.rows("select id from submission where id = " + subId)
if (rows[0] != null) {
return true
} else
return false
}
static void runCommand(String command) {
def proc = command.execute();
proc.waitFor();
}
}