Skip to content
Snippets Groups Projects
Commit c44b6337 authored by L. Ellenbeck's avatar L. Ellenbeck
Browse files

refactor

parent d6da8df4
No related branches found
No related tags found
1 merge request!1New: Script for update the existing pids
Pipeline #1272088 passed
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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment