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
d6da8df4
Commit
d6da8df4
authored
Mar 6, 2024
by
Petar Hristov
Browse files
Options
Downloads
Patches
Plain Diff
Fix
parent
165cc25f
Branches
Branches containing commit
No related tags found
1 merge request
!1
New: Script for update the existing pids
Pipeline
#1271358
passed
Mar 6, 2024
Stage: build
Stage: test
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/PID Record Migrator/PID Record Migrator.csproj
+2
-2
2 additions, 2 deletions
src/PID Record Migrator/PID Record Migrator.csproj
src/PID Record Migrator/Program.cs
+119
-148
119 additions, 148 deletions
src/PID Record Migrator/Program.cs
with
121 additions
and
150 deletions
src/PID Record Migrator/PID Record Migrator.csproj
+
2
−
2
View file @
d6da8df4
...
...
@@ -9,8 +9,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Coscine.ApiClient" Version="1.4.0-issue-2627-addpi001
4
" />
<PackageReference Include="Coscine.ApiClient.Core" Version="1.4.0-issue-2627-addpi001
4
" />
<PackageReference Include="Coscine.ApiClient" Version="1.4.0-issue-2627-addpi001
6
" />
<PackageReference Include="Coscine.ApiClient.Core" Version="1.4.0-issue-2627-addpi001
6
" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Winton.Extensions.Configuration.Consul" Version="3.4.0" />
...
...
This diff is collapsed.
Click to expand it.
src/PID Record Migrator/Program.cs
+
119
−
148
View file @
d6da8df4
...
...
@@ -59,18 +59,12 @@ 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
digitalObjectLocationUrl
=
configuration
.
GetSection
(
"PidConfiguration"
).
GetSection
(
"digitalObjectLocationUrl"
).
Value
;
var
apiRouteResources
=
configuration
.
GetSection
(
"PidConfiguration"
).
GetSection
(
"apiRouteResources"
).
Value
;
var
apiRouteProjects
=
configuration
.
GetSection
(
"PidConfiguration"
).
GetSection
(
"apiRouteProjects"
).
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!"
);
Console
.
WriteLine
(
$"- Search yielded
{
pids
.
Count
()}
PID(s)"
);
...
...
@@ -101,21 +95,14 @@ foreach (var pid in pids)
try
{
// Check if project or resource
if
(
pid
.
Type
.
ToString
()
==
"Resource"
)
if
(
pid
.
Type
.
ToString
()
?.
Equals
(
"resource"
,
StringComparison
.
OrdinalIgnoreCase
)
==
true
)
{
// Generate the PID record
var
resourceHandleValues
=
GenerateResourceHandleValues
(
pid
.
Prefix
,
pid
.
Suffix
);
// Send the update with the request content
try
{
await
handleApi
.
UpdateHandleAsync
(
pid
.
Prefix
,
pid
.
Suffix
,
new
HandleForUpdateDto
{
Values
=
resourceHandleValues
.
ToList
(),
}
);
// Generate the PID record
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
()
});
updatedPidCount
++;
}
catch
(
Exception
e
)
...
...
@@ -123,22 +110,14 @@ foreach (var pid in pids)
Console
.
WriteLine
(
$"└ Updating the following PID:
{
pid
.
Suffix
}
FAILED.\n
{
e
.
Message
}
\n"
);
}
}
if
(
pid
.
Type
.
ToString
()
==
"Project"
)
if
(
pid
.
Type
.
ToString
()
?.
Equals
(
"project"
,
StringComparison
.
OrdinalIgnoreCase
)
==
true
)
{
// Generate the PID record
var
projectHandleValues
=
GenerateProjectHandleValues
(
pid
.
Prefix
,
pid
.
Suffix
);
// Send the update with the request content
try
{
await
handleApi
.
UpdateHandleAsync
(
pid
.
Prefix
,
pid
.
Suffix
,
new
HandleForUpdateDto
{
Values
=
projectHandleValues
.
ToList
(),
}
);
// Generate the PID record
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
()
});
updatedPidCount
++;
}
catch
(
Exception
e
)
...
...
@@ -158,177 +137,169 @@ foreach (var pid in pids)
Console
.
WriteLine
(
$"Updated
{
updatedPidCount
}
Coscine PIDs\n"
);
Console
.
WriteLine
(
$"Finished"
);
IEnumerable
<
HandleValueForUpdateDto
>
Generate
Resource
HandleValues
(
string
prefix
,
string
suffix
)
async
Task
<
IEnumerable
<
HandleValueForUpdateDto
>
>
Generate
Project
HandleValues
Async
(
string
prefix
,
Guid
suffix
)
{
var
handleUrl
=
$"http://hdl.handle.net/
{
prefix
}
/
{
suffix
}
"
;
var
subpath
=
$"/pid/?pid=
{
HttpUtility
.
UrlEncode
(
handleUrl
)}
"
;
var
baseUrl
=
configuration
.
GetSection
(
"ConnectionConfiguration"
).
GetSection
(
"ProxyUrl"
).
Value
;
const
string
pidUrl
=
""
;
var
fullUrl
=
$"
{
baseUrl
?.
TrimEnd
(
'/'
)}
/
{
subpath
.
TrimStart
(
'/'
)}
"
;
var
handles
=
new
List
<
HandleValueForUpdateDto
>();
int
idxCount
=
1
;
// Call resourceApi
var
resource
=
resourceApi
.
GetResourceAsync
(
new
Guid
(
suffix
)).
Result
;
var
isPublic
=
resource
.
Data
.
Visibility
.
DisplayName
==
"Public"
;
var
url
=
new
HandleValueForUpdateDto
// Use the configuration to build necessary URLs and prefixes
var
pid
=
$"
{
prefix
.
Trim
(
'/'
)}
/
{
suffix
}
"
;
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
idx
=
1
;
// Create the URL handle value
handles
.
Add
(
new
()
{
Idx
=
idx
Count
,
Idx
=
idx
++
,
Type
=
"URL"
,
ParsedData
=
string
.
IsNullOrEmpty
(
pidUrl
)
?
fullUrl
:
pidUrl
};
idxCount
++;
handles
.
Add
(
url
);
ParsedData
=
new
Uri
(
baseUri
,
$"/pid/?pid=
{
HttpUtility
.
UrlEncode
(
pid
)}
"
)
});
var
kernelInformationProfile
=
new
HandleValueForUpdateDto
// Create the kernel information profile handle value
handles
.
Add
(
new
()
{
Idx
=
idx
Count
,
Idx
=
idx
++
,
Type
=
PidHandles
.
KernelInformationProfileHandle
,
ParsedData
=
PidHandles
.
CoscineKernelInformationProfileHandle
};
idxCount
++;
handles
.
Add
(
kernelInformationProfile
);
});
if
(
resource
?.
Data
?.
DateCreated
?.
ToString
()
is
not
null
&&
isPublic
)
// Create the date created handle value
if
(
projectDto
.
CreationDate
is
not
null
&&
isProjectPublic
)
{
var
dateCreated
=
new
HandleValueForUpdateDto
handles
.
Add
(
new
()
{
Idx
=
idx
Count
,
Idx
=
idx
++
,
Type
=
PidHandles
.
DateCreatedHandle
,
ParsedData
=
resource
?.
Data
?.
DateCreated
?.
ToString
()
??
string
.
Empty
,
};
idxCount
++;
handles
.
Add
(
dateCreated
);
ParsedData
=
projectDto
.
CreationDate
});
}
var
digitalObjectLocation
=
new
HandleValueForUpdateDto
// Create the digital object location handle value
handles
.
Add
(
new
()
{
Idx
=
idx
Count
,
Idx
=
idx
++
,
Type
=
PidHandles
.
DigitalObjectLocationHandle
,
ParsedData
=
$"
{
digitalObjectLocationUrl
}
/coscine/api/v2/resources/
{
suffix
}
"
};
idxCount
++;
handles
.
Add
(
digitalObjectLocation
);
ParsedData
=
new
Uri
(
digitalObjectLocationUri
,
$"/coscine/api/v2/projects/
{
projectDto
.
Id
}
"
)
});
var
digitalObjectType
=
new
HandleValueForUpdateDto
// Create the digital object type handle value
handles
.
Add
(
new
()
{
Idx
=
idx
Count
,
Idx
=
idx
++
,
Type
=
PidHandles
.
DigitalObjectTypeHandle
,
ParsedData
=
PidHandles
.
DigitalObjectTypeResourceHandle
};
idxCount
++;
handles
.
Add
(
digitalObjectType
);
ParsedData
=
PidHandles
.
DigitalObjectTypeProjectHandle
});
if
(
resource
?.
Data
.
Disciplines
?.
FirstOrDefault
()?.
DisplayNameEn
is
not
null
&&
isPublic
)
// Create the topic (discipline) handle value
var
disciplines
=
projectDto
.
Disciplines
.
Where
(
pd
=>
pd
.
DisplayNameEn
is
not
null
).
ToList
();
if
(
disciplines
.
Count
!=
0
&&
isProjectPublic
)
{
var
topic
=
new
HandleValueForUpdateDto
handles
.
Add
(
new
()
{
Idx
=
idx
Count
,
Idx
=
idx
++
,
Type
=
PidHandles
.
TopicHandle
,
ParsedData
=
resource
?.
Data
.
Disciplines
?.
FirstOrDefault
()?.
DisplayNameEn
??
string
.
Empty
};
idxCount
++;
handles
.
Add
(
topic
);
// NOTE: Consider adding all disciplines as a comma-separated string
ParsedData
=
disciplines
.
First
().
DisplayNameEn
??
string
.
Empty
});
}
if
(
resource
?.
Data
.
License
.
Url
is
not
null
&&
isPublic
)
// Create the contact (organizations) handle value
if
(
projectDto
.
Organizations
.
Count
!=
0
&&
isProjectPublic
)
{
var
licenseUrl
=
new
HandleValueForUpdateDto
handles
.
Add
(
new
()
{
Idx
=
idxCount
,
Type
=
PidHandles
.
LicenseHandle
,
ParsedData
=
resource
?.
Data
.
License
.
Url
??
string
.
Empty
};
idxCount
++;
handles
.
Add
(
licenseUrl
);
Idx
=
idx
++,
Type
=
PidHandles
.
ContactHandle
,
// NOTE: Consider adding all institutes as a comma-separated string
ParsedData
=
projectDto
.
Organizations
.
First
().
Uri
});
}
return
handles
;
}
IEnumerable
<
HandleValueForUpdateDto
>
Generate
Project
HandleValues
(
string
prefix
,
string
suffix
)
async
Task
<
IEnumerable
<
HandleValueForUpdateDto
>
>
Generate
Resource
HandleValues
Async
(
string
prefix
,
Guid
suffix
)
{
var
handleUrl
=
$"http://hdl.handle.net/
{
prefix
}
/
{
suffix
}
"
;
var
subpath
=
$"/pid/?pid=
{
HttpUtility
.
UrlEncode
(
handleUrl
)}
"
;
var
baseUrl
=
configuration
.
GetSection
(
"ConnectionConfiguration"
).
GetSection
(
"ProxyUrl"
).
Value
;
var
handles
=
new
List
<
HandleValueForUpdateDto
>();
const
string
pidUrl
=
""
;
// Use the configuration to build necessary URLs and prefixes
var
pid
=
$"
{
prefix
.
Trim
(
'/'
)}
/
{
suffix
}
"
;
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
fullUrl
=
$"
{
baseUrl
?.
TrimEnd
(
'/'
)}
/
{
subpath
.
TrimStart
(
'/'
)}
"
;
var
handles
=
new
List
<
HandleValueForUpdateDto
>();
// Call projectApi
var
project
=
projectApi
.
GetProjectAsync
(
suffix
).
Result
;
var
isPublic
=
project
.
Data
.
Visibility
.
DisplayName
==
"Public"
;
int
idxCount
=
1
;
var
idx
=
1
;
var
url
=
new
HandleValueForUpdateDto
// Create the URL handle value
handles
.
Add
(
new
()
{
Idx
=
idx
Count
,
Idx
=
idx
++
,
Type
=
"URL"
,
ParsedData
=
string
.
IsNullOrEmpty
(
pidUrl
)
?
fullUrl
:
pidUrl
};
idxCount
++;
handles
.
Add
(
url
);
ParsedData
=
new
Uri
(
baseUri
,
$"/pid/?pid=
{
HttpUtility
.
UrlEncode
(
pid
)}
"
)
});
var
kernelInformationProfile
=
new
HandleValueForUpdateDto
// Create the kernel information profile handle value
handles
.
Add
(
new
()
{
Idx
=
idx
Count
,
Idx
=
idx
++
,
Type
=
PidHandles
.
KernelInformationProfileHandle
,
ParsedData
=
PidHandles
.
CoscineKernelInformationProfileHandle
};
idxCount
++;
handles
.
Add
(
kernelInformationProfile
);
});
if
(
project
.
Data
.
CreationDate
is
not
null
&&
isPublic
)
// Create the date created handle value
if
(
resourceDto
.
DateCreated
is
not
null
&&
isResourcePublic
)
{
var
dateCreated
=
new
HandleValueForUpdateDto
handles
.
Add
(
new
()
{
Idx
=
idx
Count
,
Idx
=
idx
++
,
Type
=
PidHandles
.
DateCreatedHandle
,
ParsedData
=
project
.
Data
.
CreationDate
??
project
.
Data
.
StartDate
};
idxCount
++;
handles
.
Add
(
dateCreated
);
ParsedData
=
resourceDto
.
DateCreated
});
}
var
digitalObjectLocation
=
new
HandleValueForUpdateDto
// Create the digital object location handle value
handles
.
Add
(
new
()
{
Idx
=
idx
Count
,
Idx
=
idx
++
,
Type
=
PidHandles
.
DigitalObjectLocationHandle
,
ParsedData
=
$"
{
digitalObjectLocationUrl
}
/coscine/api/v2/projects/
{
suffix
}
"
};
idxCount
++;
handles
.
Add
(
digitalObjectLocation
);
ParsedData
=
new
Uri
(
digitalObjectLocationUri
,
$"/coscine/api/v2/resources/
{
resourceDto
.
Id
}
"
)
});
var
digitalObjectType
=
new
HandleValueForUpdateDto
// Create the digital object type handle value
handles
.
Add
(
new
()
{
Idx
=
idx
Count
,
Idx
=
idx
++
,
Type
=
PidHandles
.
DigitalObjectTypeHandle
,
ParsedData
=
PidHandles
.
DigitalObjectTypeProjectHandle
};
idxCount
++;
handles
.
Add
(
digitalObjectType
);
ParsedData
=
PidHandles
.
DigitalObjectTypeResourceHandle
});
if
(
project
?.
Data
.
Disciplines
.
FirstOrDefault
()?.
DisplayNameEn
is
not
null
&&
isPublic
)
// Create the topic (discipline) handle value
var
disciplines
=
resourceDto
.
Disciplines
.
Where
(
rd
=>
rd
.
DisplayNameEn
is
not
null
).
ToList
();
if
(
disciplines
.
Count
!=
0
&&
isResourcePublic
)
{
var
topic
=
new
HandleValueForUpdateDto
handles
.
Add
(
new
()
{
Idx
=
idx
Count
,
Idx
=
idx
++
,
Type
=
PidHandles
.
TopicHandle
,
ParsedData
=
project
?.
Data
.
Disciplines
.
FirstOrDefault
()?.
DisplayNameEn
??
string
.
Empty
};
idxCount
++;
handles
.
Add
(
topic
);
// NOTE: Consider adding all disciplines as a comma-separated string
ParsedData
=
disciplines
.
First
().
DisplayNameEn
??
string
.
Empty
});
}
if
(
project
?.
Data
.
Organizations
.
FirstOrDefault
()?.
Uri
.
ToString
()
is
not
null
&&
isPublic
)
// Create the license handle value
if
(
resourceDto
.
License
?.
Url
is
not
null
&&
isResourcePublic
)
{
var
contact
=
new
HandleValueForUpdateDto
handles
.
Add
(
new
()
{
Idx
=
idxCount
,
Type
=
PidHandles
.
ContactHandle
,
ParsedData
=
project
?.
Data
.
Organizations
.
FirstOrDefault
()?.
Uri
.
ToString
()
??
string
.
Empty
};
idxCount
++;
handles
.
Add
(
contact
);
Idx
=
idx
++,
Type
=
PidHandles
.
LicenseHandle
,
ParsedData
=
resourceDto
.
License
.
Url
});
}
return
handles
;
}
\ No newline at end of file
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