Skip to content
Snippets Groups Projects
Commit d6da8df4 authored by Petar Hristov's avatar Petar Hristov :speech_balloon:
Browse files

Fix

parent 165cc25f
Branches
No related tags found
1 merge request!1New: Script for update the existing pids
Pipeline #1271358 passed
......@@ -9,8 +9,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Coscine.ApiClient" Version="1.4.0-issue-2627-addpi0014" />
<PackageReference Include="Coscine.ApiClient.Core" Version="1.4.0-issue-2627-addpi0014" />
<PackageReference Include="Coscine.ApiClient" Version="1.4.0-issue-2627-addpi0016" />
<PackageReference Include="Coscine.ApiClient.Core" Version="1.4.0-issue-2627-addpi0016" />
<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" />
......
......@@ -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> GenerateResourceHandleValues(string prefix, string suffix)
async Task<IEnumerable<HandleValueForUpdateDto>> GenerateProjectHandleValuesAsync(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 = idxCount,
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 = idxCount,
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 = idxCount,
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 = idxCount,
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 = idxCount,
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 = idxCount,
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> GenerateProjectHandleValues(string prefix, string suffix)
async Task<IEnumerable<HandleValueForUpdateDto>> GenerateResourceHandleValuesAsync(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 = idxCount,
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 = idxCount,
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 = idxCount,
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 = idxCount,
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 = idxCount,
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 = idxCount,
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment