Skip to content

Commit

Permalink
Merge branch 'fixTimeoutDataverse340-700' into 'main'
Browse files Browse the repository at this point in the history
Corrige problemas de quando Dataverse excede tempo de resposta - 3.4.0

See merge request softwares-pkp/plugins_ojs/dataverse!168
  • Loading branch information
JhonathanLepidus committed Sep 30, 2024
2 parents ed2fe07 + 99c544b commit c5c5311
Show file tree
Hide file tree
Showing 27 changed files with 608 additions and 179 deletions.
11 changes: 8 additions & 3 deletions api/v1/datasets/DatasetHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ public function get($slimRequest, $response, $args)
$dataverseClient = new DataverseClient();
$dataset = $dataverseClient->getDatasetActions()->get($study->getPersistentId());
} catch (DataverseException $e) {
$request = $this->getRequest();
$submission = Repo::submission()->get($study->getSubmissionId());
if ($e->getCode() === 404) {
DAORegistry::getDAO('DataverseStudyDAO')->deleteStudy($study);
}

$error = $e->getMessage();
$message = 'plugins.generic.dataverse.error.getFailed';

Expand Down Expand Up @@ -281,9 +283,12 @@ public function getCitation($slimRequest, $response, $args)
return $response->withStatus(404)->withJsonError('api.404.resourceNotFound');
}

$queryParams = $slimRequest->getQueryParams();
$datasetIsPublished = (bool) $queryParams['datasetIsPublished'];

try {
$dataverseClient = new DataverseClient();
$citation = $dataverseClient->getDatasetActions()->getCitation($study->getPersistentId());
$citation = $dataverseClient->getDatasetActions()->getCitation($study->getPersistentId(), $datasetIsPublished);
} catch (DataverseException $e) {
error_log('Error getting citation: ' . $e->getMessage());
return $response->withStatus($e->getCode())->withJsonError('api.error.researchDataCitationNotFound');
Expand Down
92 changes: 92 additions & 0 deletions api/v1/dataverse/DataverseHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace APP\plugins\generic\dataverse\api\v1\dataverse;

use PKP\handler\APIHandler;
use PKP\security\Role;
use PKP\security\authorization\PolicySet;
use PKP\security\authorization\RoleBasedHandlerOperationPolicy;
use APP\plugins\generic\dataverse\dataverseAPI\DataverseClient;
use APP\plugins\generic\dataverse\classes\exception\DataverseException;

class DataverseHandler extends APIHandler
{
public function __construct()
{
$this->_handlerPath = 'dataverse';
$roles = [Role::ROLE_ID_MANAGER, Role::ROLE_ID_SUB_EDITOR, Role::ROLE_ID_AUTHOR];
$this->_endpoints = [
'GET' => [
[
'pattern' => $this->getEndpointPattern() . '/dataverseName',
'handler' => [$this, 'getDataverseName'],
'roles' => $roles
],
[
'pattern' => $this->getEndpointPattern() . '/rootDataverseName',
'handler' => [$this, 'getRootDataverseName'],
'roles' => $roles
],
[
'pattern' => $this->getEndpointPattern() . '/licenses',
'handler' => [$this, 'getDataverseLicenses'],
'roles' => $roles
],
],
];
parent::__construct();
}

public function authorize($request, &$args, $roleAssignments)
{
$rolePolicy = new PolicySet(PolicySet::COMBINING_PERMIT_OVERRIDES);

foreach ($roleAssignments as $role => $operations) {
$rolePolicy->addPolicy(new RoleBasedHandlerOperationPolicy($request, $role, $operations));
}
$this->addPolicy($rolePolicy);

return parent::authorize($request, $args, $roleAssignments);
}

public function getDataverseName($slimRequest, $response, $args)
{
try {
$dataverseClient = new DataverseClient();
$dataverseCollection = $dataverseClient->getDataverseCollectionActions()->get();
$dataverseName = $dataverseCollection->getName();

return $response->withJson(['dataverseName' => $dataverseName], 200);
} catch (DataverseException $e) {
error_log('Dataverse API error while getting dataverse name: ' . $e->getMessage());
return $response->withStatus($e->getCode());
}
}

public function getRootDataverseName($slimRequest, $response, $args)
{
try {
$dataverseClient = new DataverseClient();
$rootDataverseCollection = $dataverseClient->getDataverseCollectionActions()->getRoot();
$rootDataverseName = $rootDataverseCollection->getName();

return $response->withJson(['rootDataverseName' => $rootDataverseName], 200);
} catch (DataverseException $e) {
error_log('Dataverse API error while getting root name: ' . $e->getMessage());
return $response->withStatus($e->getCode());
}
}

public function getDataverseLicenses($slimRequest, $response, $args)
{
try {
$dataverseClient = new DataverseClient();
$dataverseLicenses = $dataverseClient->getDataverseCollectionActions()->getLicenses();

return $response->withJson(['licenses' => $dataverseLicenses], 200);
} catch (DataverseException $e) {
error_log('Dataverse API error while getting licenses: ' . $e->getMessage());
return $response->withStatus($e->getCode());
}
}
}
14 changes: 9 additions & 5 deletions classes/DataverseMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use APP\core\Application;
use PKP\db\DAORegistry;
use APP\plugins\generic\dataverse\classes\dataverseConfiguration\DataverseConfiguration;
use APP\plugins\generic\dataverse\dataverseAPI\DataverseClient;

class DataverseMetadata
{
Expand Down Expand Up @@ -74,12 +75,15 @@ public function getDataverseSubjects(): array

public function getDataverseLicenses(): ?array
{
$configuration = $this->getDataverseConfiguration();
$licensesUrl = $configuration->getDataverseServerUrl() . '/api/licenses';
$response = json_decode(file_get_contents($licensesUrl), true);
$this->dataverseLicenses = $response['data'] ?? [];
try {
$dataverseClient = new DataverseClient();
$this->dataverseLicenses = $dataverseClient->getDataverseCollectionActions()->getLicenses();

return $this->dataverseLicenses;
return $this->dataverseLicenses;
} catch (DataverseException $e) {
error_log('Dataverse API error (licenses): ' . $e->getMessage());
return [];
}
}

public function getDefaultLicense(): ?string
Expand Down
31 changes: 21 additions & 10 deletions classes/components/forms/DataStatementForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ public function __construct($action, $publication, $page)
$this->action = $action;
$this->locales = $this->getFormLocales($context);

$dataStatementService = new DataStatementService();
$dataStatementOptions = $this->getDataStatementOptions($dataStatementService, $page);
$dataverseName = $dataStatementService->getDataverseName();
$dataStatementOptions = $this->getDataStatementOptions($page);

$this->dataversePluginApiUrl = $request->getDispatcher()->url($request, Application::ROUTE_API, $context->getPath(), 'dataverse');
$vocabApiUrl = $request->getDispatcher()->url($request, Application::ROUTE_API, $context->getPath(), 'vocabs');

$this->addField(new FieldOptions('dataStatementTypes', [
Expand Down Expand Up @@ -61,7 +60,7 @@ public function __construct($action, $publication, $page)
[
'value' => true,
'label' => __('plugins.generic.dataverse.dataStatement.researchDataSubmitted', [
'dataverseName' => $dataverseName,
'dataverseName' => '',
]),
'disabled' => true,
],
Expand All @@ -71,6 +70,20 @@ public function __construct($action, $publication, $page)
}
}

public function getConfig()
{
$config = parent::getConfig();

$config = array_merge(
$config,
[
'dataversePluginApiUrl' => $this->dataversePluginApiUrl
]
);

return $config;
}

private function getFormLocales($context): array
{
$supportedFormLocales = $context->getSupportedFormLocales();
Expand All @@ -83,13 +96,11 @@ private function getFormLocales($context): array
return $formLocales;
}

private function getDataStatementOptions($dataStatementService, $page): array
private function getDataStatementOptions($page): array
{
$dataStatementTypes = $dataStatementService->getDataStatementTypes();

if ($page == 'workflow') {
unset($dataStatementTypes[DataStatementService::DATA_STATEMENT_TYPE_DATAVERSE_SUBMITTED]);
}
$dataStatementService = new DataStatementService();
$includeSubmittedType = ($page == 'submission');
$dataStatementTypes = $dataStatementService->getDataStatementTypes($includeSubmittedType);

return array_map(function ($value, $label) {
return [
Expand Down
50 changes: 39 additions & 11 deletions classes/components/forms/DatasetMetadataForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,53 +21,81 @@ public function __construct($action, $method, $dataset, $page)
$this->locales = $this->mapCurrentLocale();

$dataverseMetadata = new DataverseMetadata();
$dataverseLicenses = $dataverseMetadata->getDataverseLicenses();
$dataverseLicenses = [];
$datasetMetadata = $this->getDatasetMetadata($dataset);

if ($page == 'workflow') {
$mappedKeywords = (array) $dataset->getKeywords() ?? [];
$mappedKeywords = [Locale::getLocale() => $mappedKeywords];
if ($page == 'submission') {
$dataverseLicenses = $dataverseMetadata->getDataverseLicenses();

if (empty($datasetMetadata['license'])) {
$datasetMetadata['license'] = $dataverseMetadata->getDefaultLicense();
}
}

if ($page == 'workflow') {
$this->addField(new FieldText('datasetTitle', [
'label' => __('plugins.generic.dataverse.metadataForm.title'),
'isRequired' => true,
'value' => $dataset->getTitle(),
'value' => $datasetMetadata['title'],
'size' => 'large',
]))
->addField(new FieldRichTextarea('datasetDescription', [
'label' => __('plugins.generic.dataverse.metadataForm.description'),
'isRequired' => true,
'toolbar' => 'bold italic superscript subscript | link | blockquote bullist numlist | image | code',
'plugins' => 'paste,link,lists,image,code',
'value' => $dataset->getDescription()
'value' => $datasetMetadata['description']
]))
->addField(new FieldControlledVocab('datasetKeywords', [
'label' => __('plugins.generic.dataverse.metadataForm.keyword'),
'tooltip' => __('manager.setup.metadata.keywords.description'),
'apiUrl' => $this->getVocabSuggestionUrlBase(),
'isMultilingual' => true,
'locales' => $this->locales,
'value' => $mappedKeywords
'value' => $datasetMetadata['keywords']
]));
}

$selectedLicense = $dataset->getLicense() ?? $dataverseMetadata->getDefaultLicense();

$this->addField(new FieldSelect('datasetSubject', [
'label' => __('plugins.generic.dataverse.metadataForm.subject.label'),
'description' => ($page == 'submission' ? __('plugins.generic.dataverse.metadataForm.subject.description') : ''),
'isRequired' => true,
'options' => $dataverseMetadata->getDataverseSubjects(),
'value' => $dataset->getSubject(),
'value' => $datasetMetadata['subject'],
]))
->addField(new FieldSelect('datasetLicense', [
'label' => __('plugins.generic.dataverse.metadataForm.license.label'),
'description' => ($page == 'submission' ? __('plugins.generic.dataverse.metadataForm.license.description') : ''),
'isRequired' => true,
'options' => $this->mapLicensesForDisplay($dataverseLicenses),
'value' => $selectedLicense,
'value' => $datasetMetadata['license'],
]));
}

private function getDatasetMetadata($dataset)
{
if (is_null($dataset)) {
return [
'title' => '',
'description' => '',
'keywords' => [Locale::getLocale() => []],
'subject' => '',
'license' => ''
];
}

$mappedKeywords = (array) $dataset->getKeywords() ?? [];
$mappedKeywords = [Locale::getLocale() => $mappedKeywords];

return [
'title' => $dataset->getTitle(),
'description' => $dataset->getDescription(),
'keywords' => $mappedKeywords,
'subject' => $dataset->getSubject(),
'license' => $dataset->getLicense()
];
}

private function getVocabSuggestionUrlBase()
{
$request = Application::get()->getRequest();
Expand Down
13 changes: 2 additions & 11 deletions classes/components/forms/DraftDatasetFileForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,8 @@ private function getTermsOfUseData($contextId)
$locale = Locale::getLocale();
$dataverseClient = new DataverseClient();
$configuration = DAORegistry::getDAO('DataverseConfigurationDAO')->get($contextId);
$termsOfUse = $configuration->getLocalizedData('termsOfUse', $locale);

try {
$dataverseCollection = $dataverseClient->getDataverseCollectionActions()->get();
$termsOfUse = $configuration->getLocalizedData('termsOfUse', $locale);
return [
'dataverseName' => $dataverseCollection->getName(),
'termsOfUseURL' => $termsOfUse
];
} catch (DataverseException $e) {
error_log('Dataverse API error: ' . $e->getMessage());
return [];
}
return ['termsOfUserUrl' => $termsOfUse];
}
}
2 changes: 2 additions & 0 deletions classes/components/listPanel/DatasetFilesListPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class DatasetFilesListPanel extends ListPanel
{
public $addFileLabel = '';
public $dataversePluginApiUrl = '';
public $fileListUrl = '';
public $fileActionUrl = '';
public $isLoading = false;
Expand All @@ -32,6 +33,7 @@ public function getConfig()
$config,
[
'addFileLabel' => $this->addFileLabel,
'dataversePluginApiUrl' => $this->dataversePluginApiUrl,
'fileListUrl' => $this->fileListUrl,
'fileActionUrl' => $this->fileActionUrl,
'addFileModalTitle' => $this->addFileModalTitle,
Expand Down
2 changes: 1 addition & 1 deletion classes/dispatchers/DatasetInformationDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function addDatasetInformation(string $hookName, array $params): bool
$dataverseClient = new DataverseClient();

try {
$citation = $dataverseClient->getDatasetActions()->getCitation($study->getPersistentId());
$citation = $dataverseClient->getDatasetActions()->getCitation($study->getPersistentId(), null);
$templateMgr->assign('datasetInfo', $citation);
$output .= $templateMgr->fetch($this->plugin->getTemplateResource('dataCitation.tpl'));
} catch (DataverseException $e) {
Expand Down
Loading

0 comments on commit c5c5311

Please sign in to comment.