Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PID Record Migrator
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Coscine
backend
scripts
PID Record Migrator
Commits
c44b6337
Commit
c44b6337
authored
1 year ago
by
L. Ellenbeck
Browse files
Options
Downloads
Patches
Plain Diff
refactor
parent
d6da8df4
No related branches found
No related tags found
1 merge request
!1
New: Script for update the existing pids
Pipeline
#1272088
passed
1 year ago
Stage: build
Stage: test
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/PID Record Migrator/Program.cs
+184
-107
184 additions, 107 deletions
src/PID Record Migrator/Program.cs
with
184 additions
and
107 deletions
src/PID Record Migrator/Program.cs
+
184
−
107
View file @
c44b6337
using
Coscine.Api.Core.Shared
;
using
System.Web
;
using
Coscine.Api.Core.Shared
;
using
Coscine.ApiClient
;
using
Coscine.ApiClient.Core.Api
;
using
Coscine.ApiClient.Core.Client
;
using
Coscine.ApiClient.Core.Model
;
using
Microsoft.Extensions.Configuration
;
using
System.Web
;
using
Winton.Extensions.Configuration.Consul
;
var
_dummyMode
=
true
;
...
...
@@ -59,12 +59,23 @@ var pidApi = new PidApi(apiConfiguration);
Console
.
WriteLine
(
$"- Collecting Coscine data ..."
);
var
pids
=
await
RequestUtil
.
WrapPagedRequest
<
PidDtoPagedResponse
,
PidDto
>(
(
currentPage
)
=>
pidApi
.
GetPidsAsync
(
includeProjects
:
true
,
includeResources
:
true
,
includeDeleted
:
false
,
pageNumber
:
currentPage
,
pageSize
:
250
)
(
currentPage
)
=>
pidApi
.
GetPidsAsync
(
includeProjects
:
true
,
includeResources
:
true
,
includeDeleted
:
false
,
pageNumber
:
currentPage
,
pageSize
:
250
)
);
var
proxyUrlString
=
configuration
.
GetSection
(
"ConnectionConfiguration"
).
GetSection
(
"ProxyUrl"
).
Value
var
proxyUrlString
=
configuration
.
GetSection
(
"ConnectionConfiguration"
).
GetSection
(
"ProxyUrl"
).
Value
??
throw
new
Exception
(
"ConnectionConfiguration:ProxyUrl is not defined in the configuration!"
);
var
digitalObjectLocationUrlString
=
configuration
.
GetSection
(
"PidConfiguration"
).
GetSection
(
"digitalObjectLocationUrl"
).
Value
??
throw
new
Exception
(
"PidConfiguration:DigitalObjectLocationUrl is not defined in the configuration!"
);
var
digitalObjectLocationUrlString
=
configuration
.
GetSection
(
"PidConfiguration"
).
GetSection
(
"digitalObjectLocationUrl"
).
Value
??
throw
new
Exception
(
"PidConfiguration:DigitalObjectLocationUrl is not defined in the configuration!"
);
Console
.
WriteLine
(
$"- Search yielded
{
pids
.
Count
()}
PID(s)"
);
...
...
@@ -79,12 +90,14 @@ foreach (var pid in pids)
// Check if there's an entry in the handle service
try
{
Console
.
WriteLine
(
$
"- Find the PID in the handle service ..."
);
Console
.
WriteLine
(
"- Find the PID in the handle service ..."
);
var
handle
=
(
await
handleApi
.
GetHandleAsync
(
pid
.
Prefix
,
pid
.
Suffix
)).
Data
;
}
catch
(
Exception
e
)
{
Console
.
WriteLine
(
$"└ The following PID:
{
pid
.
Suffix
}
could not be found in the handle service.\n
{
e
.
Message
}
\n"
);
Console
.
WriteLine
(
$"└ The following PID:
{
pid
.
Suffix
}
could not be found in the handle service.\n
{
e
.
Message
}
\n"
);
continue
;
}
// Only update when PIDs are available
...
...
@@ -100,14 +113,23 @@ foreach (var pid in pids)
try
{
// Generate the PID record
var
resourceHandleValues
=
await
GenerateResourceHandleValuesAsync
(
pid
.
Prefix
,
Guid
.
Parse
(
pid
.
Suffix
));
var
resourceHandleValues
=
await
GenerateResourceHandleValuesAsync
(
pid
.
Prefix
,
Guid
.
Parse
(
pid
.
Suffix
)
);
// Send the update with the request content
await
handleApi
.
UpdateHandleAsync
(
pid
.
Prefix
,
pid
.
Suffix
,
new
HandleForUpdateDto
{
Values
=
resourceHandleValues
.
ToList
()
});
await
handleApi
.
UpdateHandleAsync
(
pid
.
Prefix
,
pid
.
Suffix
,
new
HandleForUpdateDto
{
Values
=
resourceHandleValues
.
ToList
()
}
);
updatedPidCount
++;
}
catch
(
Exception
e
)
{
Console
.
WriteLine
(
$"└ Updating the following PID:
{
pid
.
Suffix
}
FAILED.\n
{
e
.
Message
}
\n"
);
Console
.
WriteLine
(
$"└ Updating the following PID:
{
pid
.
Suffix
}
FAILED.\n
{
e
.
Message
}
\n"
);
}
}
if
(
pid
.
Type
.
ToString
()?.
Equals
(
"project"
,
StringComparison
.
OrdinalIgnoreCase
)
==
true
)
...
...
@@ -115,20 +137,31 @@ foreach (var pid in pids)
try
{
// Generate the PID record
var
projectHandleValues
=
await
GenerateProjectHandleValuesAsync
(
pid
.
Prefix
,
Guid
.
Parse
(
pid
.
Suffix
));
var
projectHandleValues
=
await
GenerateProjectHandleValuesAsync
(
pid
.
Prefix
,
Guid
.
Parse
(
pid
.
Suffix
)
);
// Send the update with the request content
await
handleApi
.
UpdateHandleAsync
(
pid
.
Prefix
,
pid
.
Suffix
,
new
HandleForUpdateDto
{
Values
=
projectHandleValues
.
ToList
()
});
await
handleApi
.
UpdateHandleAsync
(
pid
.
Prefix
,
pid
.
Suffix
,
new
HandleForUpdateDto
{
Values
=
projectHandleValues
.
ToList
()
}
);
updatedPidCount
++;
}
catch
(
Exception
e
)
{
Console
.
WriteLine
(
$"└ Updating the following PID:
{
pid
.
Suffix
}
FAILED.\n
{
e
.
Message
}
\n"
);
Console
.
WriteLine
(
$"└ Updating the following PID:
{
pid
.
Suffix
}
FAILED.\n
{
e
.
Message
}
\n"
);
}
}
}
catch
(
Exception
e
)
{
Console
.
WriteLine
(
$"└ Updating the following PID:
{
pid
.
Suffix
}
FAILED.\n
{
e
.
Message
}
\n"
);
Console
.
WriteLine
(
$"└ Updating the following PID:
{
pid
.
Suffix
}
FAILED.\n
{
e
.
Message
}
\n"
);
Console
.
WriteLine
(
$"└ Migration FAILED.
{
e
.
Message
}
"
);
return
;
}
...
...
@@ -137,7 +170,10 @@ foreach (var pid in pids)
Console
.
WriteLine
(
$"Updated
{
updatedPidCount
}
Coscine PIDs\n"
);
Console
.
WriteLine
(
$"Finished"
);
async
Task
<
IEnumerable
<
HandleValueForUpdateDto
>>
GenerateProjectHandleValuesAsync
(
string
prefix
,
Guid
suffix
)
async
Task
<
IEnumerable
<
HandleValueForUpdateDto
>>
GenerateProjectHandleValuesAsync
(
string
prefix
,
Guid
suffix
)
{
var
handles
=
new
List
<
HandleValueForUpdateDto
>();
...
...
@@ -146,82 +182,104 @@ async Task<IEnumerable<HandleValueForUpdateDto>> GenerateProjectHandleValuesAsyn
var
baseUri
=
new
Uri
(
proxyUrlString
,
UriKind
.
Absolute
);
var
digitalObjectLocationUri
=
new
Uri
(
digitalObjectLocationUrlString
,
UriKind
.
Absolute
);
var
projectDto
=
(
await
projectApi
.
GetProjectAsync
(
suffix
.
ToString
())).
Data
;
var
isProjectPublic
=
projectDto
?.
Visibility
?.
DisplayName
.
Equals
(
"public"
,
StringComparison
.
OrdinalIgnoreCase
)
??
false
;
var
isProjectPublic
=
projectDto
?.
Visibility
?.
DisplayName
.
Equals
(
"public"
,
StringComparison
.
OrdinalIgnoreCase
)
??
false
;
var
idx
=
1
;
// Create the URL handle value
handles
.
Add
(
new
()
handles
.
Add
(
new
()
{
Idx
=
idx
++,
Type
=
"URL"
,
ParsedData
=
new
Uri
(
baseUri
,
$"/pid/?pid=
{
HttpUtility
.
UrlEncode
(
pid
)}
"
)
});
}
);
// Create the kernel information profile handle value
handles
.
Add
(
new
()
handles
.
Add
(
new
()
{
Idx
=
idx
++,
Type
=
PidHandles
.
KernelInformationProfileHandle
,
ParsedData
=
PidHandles
.
CoscineKernelInformationProfileHandle
});
}
);
// Create the date created handle value
if
(
projectDto
.
CreationDate
is
not
null
&&
isProjectPublic
)
{
handles
.
Add
(
new
()
handles
.
Add
(
new
()
{
Idx
=
idx
++,
Type
=
PidHandles
.
DateCreatedHandle
,
ParsedData
=
projectDto
.
CreationDate
});
}
);
}
// Create the digital object location handle value
handles
.
Add
(
new
()
handles
.
Add
(
new
()
{
Idx
=
idx
++,
Type
=
PidHandles
.
DigitalObjectLocationHandle
,
ParsedData
=
new
Uri
(
digitalObjectLocationUri
,
$"/coscine/api/v2/projects/
{
projectDto
.
Id
}
"
)
});
ParsedData
=
new
Uri
(
digitalObjectLocationUri
,
$"/coscine/api/v2/projects/
{
projectDto
.
Id
}
"
)
}
);
// Create the digital object type handle value
handles
.
Add
(
new
()
handles
.
Add
(
new
()
{
Idx
=
idx
++,
Type
=
PidHandles
.
DigitalObjectTypeHandle
,
ParsedData
=
PidHandles
.
DigitalObjectTypeProjectHandle
});
}
);
// Create the topic (discipline) handle value
var
disciplines
=
projectDto
.
Disciplines
.
Where
(
pd
=>
pd
.
DisplayNameEn
is
not
null
).
ToList
();
if
(
disciplines
.
Count
!=
0
&&
isProjectPublic
)
{
handles
.
Add
(
new
()
handles
.
Add
(
new
()
{
Idx
=
idx
++,
Type
=
PidHandles
.
TopicHandle
,
// NOTE: Consider adding all disciplines as a comma-separated string
ParsedData
=
disciplines
.
First
().
DisplayNameEn
??
string
.
Empty
});
ParsedData
=
disciplines
[
0
].
DisplayNameEn
??
string
.
Empty
}
);
}
// Create the contact (organizations) handle value
if
(
projectDto
.
Organizations
.
Count
!=
0
&&
isProjectPublic
)
{
handles
.
Add
(
new
()
handles
.
Add
(
new
()
{
Idx
=
idx
++,
Type
=
PidHandles
.
ContactHandle
,
// NOTE: Consider adding all institutes as a comma-separated string
ParsedData
=
projectDto
.
Organizations
.
First
().
Uri
});
ParsedData
=
projectDto
.
Organizations
[
0
].
Uri
}
);
}
return
handles
;
}
async
Task
<
IEnumerable
<
HandleValueForUpdateDto
>>
GenerateResourceHandleValuesAsync
(
string
prefix
,
Guid
suffix
)
async
Task
<
IEnumerable
<
HandleValueForUpdateDto
>>
GenerateResourceHandleValuesAsync
(
string
prefix
,
Guid
suffix
)
{
var
handles
=
new
List
<
HandleValueForUpdateDto
>();
...
...
@@ -230,75 +288,94 @@ async Task<IEnumerable<HandleValueForUpdateDto>> GenerateResourceHandleValuesAsy
var
baseUri
=
new
Uri
(
proxyUrlString
,
UriKind
.
Absolute
);
var
digitalObjectLocationUri
=
new
Uri
(
digitalObjectLocationUrlString
,
UriKind
.
Absolute
);
var
resourceDto
=
(
await
resourceApi
.
GetResourceAsync
(
suffix
)).
Data
;
var
isResourcePublic
=
resourceDto
?.
Visibility
?.
DisplayName
.
Equals
(
"public"
,
StringComparison
.
OrdinalIgnoreCase
)
??
false
;
var
isResourcePublic
=
resourceDto
?.
Visibility
?.
DisplayName
.
Equals
(
"public"
,
StringComparison
.
OrdinalIgnoreCase
)
??
false
;
var
idx
=
1
;
// Create the URL handle value
handles
.
Add
(
new
()
handles
.
Add
(
new
()
{
Idx
=
idx
++,
Type
=
"URL"
,
ParsedData
=
new
Uri
(
baseUri
,
$"/pid/?pid=
{
HttpUtility
.
UrlEncode
(
pid
)}
"
)
});
}
);
// Create the kernel information profile handle value
handles
.
Add
(
new
()
handles
.
Add
(
new
()
{
Idx
=
idx
++,
Type
=
PidHandles
.
KernelInformationProfileHandle
,
ParsedData
=
PidHandles
.
CoscineKernelInformationProfileHandle
});
}
);
// Create the date created handle value
if
(
resourceDto
.
DateCreated
is
not
null
&&
isResourcePublic
)
{
handles
.
Add
(
new
()
handles
.
Add
(
new
()
{
Idx
=
idx
++,
Type
=
PidHandles
.
DateCreatedHandle
,
ParsedData
=
resourceDto
.
DateCreated
});
}
);
}
// Create the digital object location handle value
handles
.
Add
(
new
()
handles
.
Add
(
new
()
{
Idx
=
idx
++,
Type
=
PidHandles
.
DigitalObjectLocationHandle
,
ParsedData
=
new
Uri
(
digitalObjectLocationUri
,
$"/coscine/api/v2/resources/
{
resourceDto
.
Id
}
"
)
});
ParsedData
=
new
Uri
(
digitalObjectLocationUri
,
$"/coscine/api/v2/resources/
{
resourceDto
.
Id
}
"
)
}
);
// Create the digital object type handle value
handles
.
Add
(
new
()
handles
.
Add
(
new
()
{
Idx
=
idx
++,
Type
=
PidHandles
.
DigitalObjectTypeHandle
,
ParsedData
=
PidHandles
.
DigitalObjectTypeResourceHandle
});
}
);
// Create the topic (discipline) handle value
var
disciplines
=
resourceDto
.
Disciplines
.
Where
(
rd
=>
rd
.
DisplayNameEn
is
not
null
).
ToList
();
if
(
disciplines
.
Count
!=
0
&&
isResourcePublic
)
{
handles
.
Add
(
new
()
handles
.
Add
(
new
()
{
Idx
=
idx
++,
Type
=
PidHandles
.
TopicHandle
,
// NOTE: Consider adding all disciplines as a comma-separated string
ParsedData
=
disciplines
.
First
().
DisplayNameEn
??
string
.
Empty
});
ParsedData
=
disciplines
[
0
].
DisplayNameEn
??
string
.
Empty
}
);
}
// Create the license handle value
if
(
resourceDto
.
License
?.
Url
is
not
null
&&
isResourcePublic
)
{
handles
.
Add
(
new
()
handles
.
Add
(
new
()
{
Idx
=
idx
++,
Type
=
PidHandles
.
LicenseHandle
,
ParsedData
=
resourceDto
.
License
.
Url
});
}
);
}
return
handles
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment