-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateMarcToInstanceMapping.sh
233 lines (193 loc) · 6.09 KB
/
updateMarcToInstanceMapping.sh
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/bash
function main() {
total_count=0
total_succeeded_count=0
total_error_count=0
local okapi_url=$1
local tenant=$2
local username=$3
local password=$4
local limit=$5
local release=$6
if [[ -z "$okapi_url" ]]
then
read -p "Enter Okapi URL: " okapi_url
if [[ -z "$okapi_url" ]]
then
echo "Okapi URL cannot be empty."
return 1
fi
fi
if [[ -z "$tenant" ]]
then
read -p "Enter Okapi tenant: " tenant
if [[ -z "$tenant" ]]
then
echo "Okapi tenant cannot be empty."
return 1
fi
fi
if [[ -z "$username" ]]
then
read -p "Enter admin username: " username
if [[ -z "$username" ]]
then
echo "Username cannot be empty."
return 1
fi
fi
if [[ -z "$password" ]]
then
read -s -p "Enter admin password: " password
echo
if [[ -z "$password" ]]
then
echo "Password cannot be empty."
return 1
fi
fi
if [[ -z "$limit" ]]
then
read -p "Enter limit number: " limit
fi
if [[ -z "$release" ]]
then
read -p "Enter release version (0 - Kiwi, 1 - Lotus and higher): " release
if [[ -z "$release" ]]
then
release=1
fi
fi
# Login by admin
echo "Logging in as $username..."
login_body="{\"username\":\"${username}\",\"password\":\"${password}\"}"
login_url="${okapi_url}/authn/login"
token=$(curl -X POST "${login_url}" --silent \
-H "X-Okapi-Tenant: $tenant" \
-H "Content-Type: application/json" \
-d "${login_body}" | awk 'BEGIN { FS="\""; RS="," }; { if ($2 == "okapiToken") {print $4} }')
if [[ -z $token ]]
then
echo "Cannot login. Shutting down the script."
return 1
else
echo "Login successful. Token retrieved."
fi
# GET Instances count
echo "Retrieving total instance count..."
get_instances_count_url="${okapi_url}/instance-storage/instances?limit=0"
total_records=$(curl -X GET "${get_instances_count_url}" --silent \
-H "X-Okapi-Tenant: $tenant" \
-H "X-Okapi-Token: $token" \
-H "Content-Type: application/json" | awk 'BEGIN { FS="\"totalRecords\": "; RS="," }; { print $2 }')
# Trim total records to remove spaces from integer value
total_records=$(echo $total_records | tr -d ' ')
if [[ $total_records -le 0 ]]
then
echo "The number of total records is equal to 0."
exit 1
else
echo "Total records found: $total_records"
fi
if [[ -z $limit ]]
then
limit=$total_records
fi
# Offset set up
if [ -f "offset.txt" ]; then
offset=$(<offset.txt)
fi
if [[ -z $offset ]]
then
offset=0
echo "$offset" > offset.txt
fi
# GET All Instance ids
echo "Retrieving instance IDs with limit $limit and offset $offset..."
get_all_instance_ids_url="${okapi_url}/instance-storage/instances?limit=${limit}&offset=${offset}"
get_all_instance_ids_url=$(echo $get_all_instance_ids_url | tr -d ' ')
instances=$(curl -X GET "${get_all_instance_ids_url}" --silent \
-H "X-Okapi-Tenant: $tenant" \
-H "X-Okapi-Token: $token" \
-H "Content-Type: application/json" |
jq -r '.instances[] | select(.source=="MARC") | .id + "," + (._version|tostring)')
read -a instances <<< $instances
total_count=${#instances[@]}
echo "Total instances to process: $total_count"
for i in "${instances[@]}"
do
instance_id="$(cut -d',' -f1 <<< $i)"
version="$(cut -d',' -f2 <<< $i)"
echo "Processing instance ID: $instance_id, version: $version"
processMarcRecord $okapi_url $tenant $token $instance_id $version $release
done
# Reset offset if there are no records left
if [[ $offset -ge $total_records ]]
then
offset=0
else
offset=$((offset+limit))
fi
echo "$offset" > offset.txt
echo "Total count: $total_count, Total succeeded count: $total_succeeded_count, Total error count: $total_error_count"
}
function processMarcRecord() {
local okapi_url=$1
local tenant=$2
local token=$3
local instance_id=$4
local version=$5
local release=$6
echo "Fetching MARC-Bib record for instance ID: $instance_id..."
get_srs_marc_record_url="${okapi_url}/change-manager/parsedRecords?externalId=${instance_id}"
local change_manager_body=$(curl -X GET "${get_srs_marc_record_url}" --silent \
-H "X-Okapi-Tenant: $tenant" \
-H "X-Okapi-Token: $token" \
-H "Content-Type: application/json")
if jq -e . >/dev/null 2>&1 <<< "$change_manager_body"; then
if [[ "$release" -eq 1 ]]
then
change_manager_body="$(jq --arg version "$version" '. += {"relatedRecordVersion":$version}' <<< $change_manager_body)"
fi
echo "MARC-Bib record fetched successfully."
echo $change_manager_body >> output.json
else
total_error_count=$((total_error_count+1))
echo "Error fetching MARC-Bib record for instance ID: $instance_id. Logging error."
echo "$instance_id - $change_manager_body" >> results.txt
return 1
fi
local record_id="$(jq -r '. .id' <<< "$change_manager_body")"
if [[ -z "$record_id" ]]
then
total_error_count=$((total_error_count+1))
echo "Record ID is empty for instance ID: $instance_id. Logging error."
echo "$instance_id - RecordId is empty" >> results.txt
return 1
fi
echo "Updating MARC-Bib record with ID: $record_id..."
put_srs_marc_record_url="${okapi_url}/change-manager/parsedRecords/${record_id}"
local response=$(curl -X PUT "${put_srs_marc_record_url}" --silent \
-H "X-Okapi-Tenant: $tenant" \
-H "X-Okapi-Token: $token" \
-H "Content-Type: application/json" \
-H "Accept-Charset: utf-8" \
-w "%{http_code}\n" \
-d "${change_manager_body}")
if [[ $response == "202" ]]
then
total_succeeded_count=$((total_succeeded_count+1))
echo "Record ID: $record_id updated successfully."
else
total_error_count=$((total_error_count+1))
echo "Error updating record ID: $record_id. Response code: $response. Logging error."
fi
echo "$instance_id - ok" >> results.txt
}
# Main entry point
SECONDS=0
echo "Starting script execution..."
main $1 $2 $3 $4 $5 $6
duration=$SECONDS
echo "Script execution completed."
echo "$(( $duration / 3600 )) hours, $((( $duration / 60 ) % 60 )) minutes and $(( $duration % 60 )) seconds elapsed."