Skip to content

Commit

Permalink
Merge pull request #24 from lepidus/main
Browse files Browse the repository at this point in the history
Feature/added work exists validation (OMP 3.4.0)
  • Loading branch information
thiagolepidus authored Jan 28, 2025
2 parents c00b7be + 3cfdde1 commit dddfc1d
Show file tree
Hide file tree
Showing 96 changed files with 1,739 additions and 4,721 deletions.
3 changes: 3 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ include:
.unit_test_template:
before_script:
- composer install

code_formatting_check:
image: ubuntu:22.04
23 changes: 0 additions & 23 deletions ThothPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
* @brief Plugin for integration with Thoth for communication and synchronization of book data between the two platforms
*/

use APP\plugins\generic\thoth\classes\APIKeyEncryption;
use PKP\core\JSONMessage;

import('lib.pkp.classes.plugins.GenericPlugin');
import('plugins.generic.thoth.classes.ThothBadgeRender');
import('plugins.generic.thoth.classes.ThothNotification');
import('plugins.generic.thoth.classes.ThothRegister');
import('plugins.generic.thoth.classes.ThothUpdater');
import('plugins.generic.thoth.lib.thothAPI.exceptions.ThothException');

class ThothPlugin extends GenericPlugin
{
Expand Down Expand Up @@ -125,25 +123,4 @@ public function manage($args, $request)
}
return parent::manage($args, $request);
}

public function getThothClient($contextId = null)
{
$contextId = $contextId ?? Application::get()->getRequest()->getContext()->getId();

$email = $this->getSetting($contextId, 'email');
$password = $this->getSetting($contextId, 'password');

if (!$email || !$password) {
throw new ThothException('Credentials not configured', 0);
}

$password = APIKeyEncryption::decryptString($password);
$testEnvironment = $this->getSetting($contextId, 'testEnvironment');

import('plugins.generic.thoth.lib.thothAPI.ThothClient');
$thothClient = new ThothClient($testEnvironment);
$thothClient->login($email, $password);

return $thothClient;
}
}
19 changes: 11 additions & 8 deletions ThothSettingsForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
use PKP\form\validation\FormValidatorCSRF;
use PKP\form\validation\FormValidatorCustom;
use PKP\form\validation\FormValidatorPost;

import('plugins.generic.thoth.lib.thothAPI.ThothClient');
use ThothApi\Exception\QueryException;
use ThothApi\GraphQL\Client;

class ThothSettingsForm extends Form
{
Expand Down Expand Up @@ -53,13 +53,16 @@ public function __construct($plugin, $contextId)
function ($password) use ($form) {
$email = trim($this->getData('email'));
$testEnvironment = $this->getData('testEnvironment');
$thothClient = new ThothClient($testEnvironment);
$httpConfig = [];
if ($testEnvironment) {
$httpConfig['base_uri'] = 'http://localhost:8000/';
}

$client = new Client($httpConfig);

try {
$thothClient->login(
$email,
$password
);
} catch (ThothException $e) {
$client->login($email, $password);
} catch (QueryException $e) {
return false;
}
return true;
Expand Down
71 changes: 14 additions & 57 deletions classes/ThothRegister.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use APP\i18n\AppLocale;
use APP\notification\Notification;
use PKP\security\Role;
use ThothApi\Exception\QueryException;

import('plugins.generic.thoth.classes.facades.ThothService');
import('plugins.generic.thoth.classes.ThothValidator');
Expand Down Expand Up @@ -69,12 +70,13 @@ public function addThothField($hookName, $form)
$errors = [];

try {
$thothClient = $this->plugin->getThothClient($submission->getData('contextId'));
$publishers = $thothClient->linkedPublishers();
$thothClient = ThothContainer::getInstance()->get('client');
$thothAccountDetails = $thothClient->accountDetails();
$publishers = $thothAccountDetails['resourceAccess']['linkedPublishers'];
$imprints = $thothClient->imprints(['publishers' => array_column($publishers, 'publisherId')]);
} catch (ThothException $e) {
} catch (QueryException $e) {
$errors[] = __('plugins.generic.thoth.connectionError');
error_log($e->getMessage());
error_log('Failed to send the request to Thoth: ' . $e->getMessage());
}

if (empty($errors)) {
Expand All @@ -101,8 +103,8 @@ public function addThothField($hookName, $form)
$imprintOptions = [];
foreach ($imprints as $imprint) {
$imprintOptions[] = [
'value' => $imprint['imprintId'],
'label' => $imprint['imprintName']
'value' => $imprint->getImprintId(),
'label' => $imprint->getImprintName()
];
}

Expand All @@ -120,53 +122,9 @@ public function addThothField($hookName, $form)
'required' => true,
'showWhen' => 'registerConfirmation',
'groupId' => 'default',
'value' => $imprints[0]['imprintId'] ?? null
'value' => $imprintOptions[0]['value'] ?? null
]));

// return false;

// try {
// $thothClient = $this->plugin->getThothClient($submission->getData('contextId'));
// $publishers = $thothClient->linkedPublishers();
// $imprints = $thothClient->imprints(['publishers' => array_column($publishers, 'publisherId')]);

// $imprintOptions = [];
// foreach ($imprints as $imprint) {
// $imprintOptions[] = [
// 'value' => $imprint['imprintId'],
// 'label' => $imprint['imprintName']
// ];
// }

// $form->addField(new \PKP\components\forms\FieldOptions('registerConfirmation', [
// 'label' => __('plugins.generic.thoth.register.label'),
// 'options' => [
// ['value' => true, 'label' => __('plugins.generic.thoth.register.confirmation')]
// ],
// 'value' => false,
// 'groupId' => 'default',
// ]))
// ->addField(new \PKP\components\forms\FieldSelect('imprint', [
// 'label' => __('plugins.generic.thoth.imprint'),
// 'options' => $imprintOptions,
// 'required' => true,
// 'showWhen' => 'registerConfirmation',
// 'groupId' => 'default',
// 'value' => $imprints[0]['imprintId'] ?? null
// ]));
// } catch (ThothException $e) {
// $warningIconHtml = '<span class="fa fa-exclamation-triangle pkpIcon--inline"></span>';
// $noticeMsg = __('plugins.generic.thoth.connectionError');
// $msg = '<div class="pkpNotification pkpNotification--warning">' . $warningIconHtml . $noticeMsg . '</div>';

// $form->addField(new \PKP\components\forms\FieldHTML('registerNotice', [
// 'description' => $msg,
// 'groupId' => 'default',
// ]));

// error_log($e->getMessage());
// }

return false;
}

Expand Down Expand Up @@ -249,23 +207,22 @@ public function registerWork($submission, $imprint)
}

try {
$thothClient = $this->plugin->getThothClient($submissionContext->getId());
$thothBook = ThothService::work()->registerBook($thothClient, $submission, $imprint);
Repo::submission()->edit($submission, ['thothWorkId' => $thothBook->getId()]);
$thothBook = ThothService::work()->registerBook($submission, $imprint);
Repo::submission()->edit($submission, ['thothWorkId' => $thothBook->getWorkId()]);
ThothNotification::notify(
$request,
$submission,
Notification::NOTIFICATION_TYPE_SUCCESS,
'plugins.generic.thoth.register.success'
);
} catch (ThothException $e) {
error_log($e->getMessage());
} catch (QueryException $e) {
error_log('Failed to send the request to Thoth: ' . $e->getMessage());
ThothNotification::notify(
$request,
$submission,
Notification::NOTIFICATION_TYPE_ERROR,
'plugins.generic.thoth.register.error',
$e->getError()
$e->getMessage()
);
}
}
Expand Down
10 changes: 5 additions & 5 deletions classes/ThothUpdater.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use APP\facades\Repo;
use APP\notification\Notification;
use ThothApi\Exception\QueryException;

import('plugins.generic.thoth.classes.facades.ThothService');

Expand All @@ -41,22 +42,21 @@ public function updateWork($hookName, $args)
}

try {
$thothClient = $this->plugin->getThothClient($submission->getData('contextId'));
ThothService::work()->updateBook($thothClient, $thothWorkId, $submission, $publication);
ThothService::work()->updateBook($thothWorkId, $submission, $publication);
ThothNotification::notify(
$request,
$submission,
Notification::NOTIFICATION_TYPE_SUCCESS,
'plugins.generic.thoth.update.success'
);
} catch (ThothException $e) {
error_log($e->getMessage());
} catch (QueryException $e) {
error_log('Failed to send the request to Thoth: ' . $e->getMessage());
ThothNotification::notify(
$request,
$submission,
Notification::NOTIFICATION_TYPE_ERROR,
'plugins.generic.thoth.update.error',
$e->getError()
$e->getMessage()
);
}

Expand Down
70 changes: 69 additions & 1 deletion classes/ThothValidator.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require_once(__DIR__ . '/../vendor/autoload.php');

use Biblys\Isbn\Isbn;
use ThothApi\GraphQL\Models\Work as ThothWork;

import('plugins.generic.thoth.classes.facades.ThothService');

Expand All @@ -26,6 +27,28 @@ public static function validate($submission)
{
$errors = [];

$publication = $submission->getCurrentPublication();
$doi = $publication->getData('doiObject')?->getResolvingUrl();

if ($doi !== null) {
$errors = array_merge($errors, self::validateDoiExists($doi));
}

$request = Application::get()->getRequest();
$context = $request->getContext();
$dispatcher = $request->getDispatcher();

$landingPage = $dispatcher->url(
$request,
ROUTE_PAGE,
$context->getPath(),
'catalog',
'book',
$submission->getBestId()
);

$errors = array_merge($errors, self::validateLandingPageExists($landingPage));

$publicationFormats = Application::getRepresentationDao()
->getApprovedByPublicationId($submission->getData('currentPublicationId'))
->toArray();
Expand All @@ -45,7 +68,10 @@ public static function validateIsbn($publicationFormats)
foreach ($publicationFormats as $publicationFormat) {
try {
$isbn = ThothService::publication()->getIsbnByPublicationFormat($publicationFormat);
Isbn::validateAsIsbn13($isbn);
if ($isbn !== null) {
Isbn::validateAsIsbn13($isbn);
}
$errors = array_merge($errors, self::validateIsbnExists($isbn));
} catch (Exception $e) {
$errors[] = __('plugins.generic.thoth.validation.isbn', [
'isbn' => $isbn,
Expand All @@ -56,4 +82,46 @@ public static function validateIsbn($publicationFormats)

return $errors;
}

public static function validateDoiExists($doi)
{
$errors = [];

try {
$work = ThothService::work()->getByDoi($doi);
if ($work instanceof ThothWork) {
$errors[] = __('plugins.generic.thoth.validation.doiExists', ['doi' => $doi]);
}
} catch (Exception $e) {
return $errors;
}

return $errors;
}

public static function validateLandingPageExists($landingPage)
{
$errors = [];

$works = ThothService::work()->search($landingPage);

if (!empty($works)) {
$errors[] = __('plugins.generic.thoth.validation.landingPageExists', ['landingPage' => $landingPage]);
}

return $errors;
}

public static function validateIsbnExists($isbn)
{
$errors = [];

$publications = ThothService::publication()->search($isbn);

if (!empty($publications)) {
$errors[] = __('plugins.generic.thoth.validation.isbnExists', ['isbn' => $isbn]);
}

return $errors;
}
}
6 changes: 3 additions & 3 deletions classes/components/forms/RegisterForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public function __construct($action, $imprints, $errors)
$imprintOptions = [];
foreach ($imprints as $imprint) {
$imprintOptions[] = [
'value' => $imprint['imprintId'],
'label' => $imprint['imprintName']
'value' => $imprint->getImprintId(),
'label' => $imprint->getImprintName()
];
}

Expand All @@ -86,7 +86,7 @@ public function __construct($action, $imprints, $errors)
'options' => $imprintOptions,
'required' => true,
'groupId' => 'default',
'value' => $imprints[0]['imprintId'] ?? null
'value' => $imprintOptions[0]['value'] ?? null
]));
}

Expand Down
Loading

0 comments on commit dddfc1d

Please sign in to comment.