Skip to content

Commit

Permalink
Merge branch 'fixTimeoutDataverse330-697' into 'stable-3_3_0'
Browse files Browse the repository at this point in the history
Resolve problemas de quando Dataverse excede tempo de resposta - 330

See merge request softwares-pkp/plugins_ojs/dataverse!167
  • Loading branch information
JhonathanLepidus committed Sep 24, 2024
2 parents 17869ea + fc7c654 commit 5c08357
Show file tree
Hide file tree
Showing 20 changed files with 550 additions and 229 deletions.
6 changes: 4 additions & 2 deletions api/v1/datasets/DatasetHandler.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ public function get($slimRequest, $response, $args)
$dataverseClient = new DataverseClient();
$dataset = $dataverseClient->getDatasetActions()->get($study->getPersistentId());
} catch (DataverseException $e) {
$request = $this->getRequest();
$submission = Services::get('submission')->get($study->getSubmissionId());
if ($e->getCode() === 404) {
DAORegistry::getDAO('DataverseStudyDAO')->deleteStudy($study);
}

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

Expand Down
88 changes: 88 additions & 0 deletions api/v1/dataverse/DataverseHandler.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

import('lib.pkp.classes.handler.APIHandler');
import('plugins.generic.dataverse.dataverseAPI.DataverseClient');

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

public function authorize($request, &$args, $roleAssignments)
{
import('lib.pkp.classes.security.authorization.PolicySet');
$rolePolicy = new PolicySet(COMBINING_PERMIT_OVERRIDES);

import('lib.pkp.classes.security.authorization.RoleBasedHandlerOperationPolicy');
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());
}
}
}
15 changes: 10 additions & 5 deletions classes/DataverseMetadata.inc.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

import('plugins.generic.dataverse.dataverseAPI.DataverseClient');

class DataverseMetadata
{
private $dataverseLicenses;
Expand Down Expand Up @@ -68,12 +70,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
25 changes: 18 additions & 7 deletions classes/components/forms/DataStatementForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@
class DataStatementForm extends FormComponent
{
public $id = FORM_DATA_STATEMENT;

public $method = 'PUT';

public function __construct($action, $locales, $publication)
{
$this->action = $action;
$this->locales = $locales;

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

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

import('plugins.generic.dataverse.classes.components.forms.FieldControlledVocabUrl');
Expand Down Expand Up @@ -53,7 +51,7 @@ public function __construct($action, $locales, $publication)
[
'value' => true,
'label' => __('plugins.generic.dataverse.dataStatement.researchDataSubmitted', [
'dataverseName' => $dataverseName,
'dataverseName' => '',
]),
'disabled' => true,
],
Expand All @@ -62,11 +60,24 @@ public function __construct($action, $locales, $publication)
]));
}

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

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

return $config;
}

private function getDataStatementOptions(): array
{
$dataStatementService = new DataStatementService();
$dataStatementTypes = $dataStatementService->getDataStatementTypes();
unset($dataStatementTypes[DATA_STATEMENT_TYPE_DATAVERSE_SUBMITTED]);
$dataStatementTypes = $dataStatementService->getDataStatementTypes(false);

$dataStatementOptions = array_map(function ($value, $label) {
return [
Expand Down
46 changes: 29 additions & 17 deletions classes/components/forms/DatasetMetadataForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,56 +21,68 @@ public function __construct($action, $method, $locales, $dataset)
$this->locales = $locales;

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

$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(),
'locales' => $this->locales,
'selected' => (array) $dataset->getKeywords() ?? [],
'value' => (array) $dataset->getKeywords() ?? []
'selected' => $datasetMetadata['keywords'],
'value' => $datasetMetadata['keywords']
]))
->addField(new FieldSelect('datasetSubject', [
'label' => __('plugins.generic.dataverse.metadataForm.subject.label'),
'isRequired' => true,
'options' => $dataverseMetadata->getDataverseSubjects(),
'value' => $dataset->getSubject(),
'value' => $datasetMetadata['subject'],
]))
->addField(new FieldSelect('datasetLicense', [
'label' => __('plugins.generic.dataverse.metadataForm.license.label'),
'isRequired' => true,
'options' => $this->mapLicensesForDisplay($dataverseLicenses),
'value' => $dataset->getLicense(),
'options' => [],
'value' => $datasetMetadata['license'],
]));
}

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

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

private function getVocabSuggestionUrlBase()
{
$request = Application::get()->getRequest();
$contextPath = $request->getContext()->getPath();
return $request->getDispatcher()->url($request, ROUTE_API, $contextPath, 'vocabs', null, null, ['vocab' => 'submissionKeyword']);
}

private function mapLicensesForDisplay(array $licenses): array
{
$mappedLicenses = [];
foreach ($licenses as $license) {
$mappedLicenses[] = ['label' => $license['name'], 'value' => $license['name']];
}
return $mappedLicenses;
}
}
19 changes: 4 additions & 15 deletions classes/components/forms/DraftDatasetFileForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,11 @@ private function getTemporaryFileApiUrl($context): string
private function getTermsOfUseData($contextId)
{
$locale = AppLocale::getLocale();

import('plugins.generic.dataverse.dataverseAPI.DataverseClient');
$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 [
'termsOfUseURL' => $termsOfUse
];
}
}
4 changes: 2 additions & 2 deletions classes/dispatchers/DataStatementTabDispatcher.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ class DataStatementTabDispatcher extends DataverseDispatcher
{
protected function registerHooks(): void
{
HookRegistry::register('TemplateManager::setupBackendPage', [$this, 'addDataStatementResourcesToFrontend']);
HookRegistry::register('TemplateManager::setupBackendPage', [$this, 'addDataStatementResourcesToBackend']);
HookRegistry::register('TemplateManager::display', [$this, 'addDataStatementTabResources']);
HookRegistry::register('Template::Workflow::Publication', [$this, 'addDataStatementTab']);
}

public function addDataStatementResourcesToFrontend(string $hookName): void
public function addDataStatementResourcesToBackend(string $hookName): void
{
import('plugins.generic.dataverse.classes.services.DataStatementService');
$request = \Application::get()->getRequest();
Expand Down
Loading

0 comments on commit 5c08357

Please sign in to comment.