Skip to content

Commit

Permalink
MetadataJournal, MetadataPublication, MetadataAuthor skips round trip…
Browse files Browse the repository at this point in the history
… database if already in memory
  • Loading branch information
GaziYucel committed Mar 1, 2024
1 parent c6480c7 commit 6fe8986
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 52 deletions.
88 changes: 40 additions & 48 deletions classes/Db/PluginDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class PluginDAO
*/
public function getCitations(int $publicationId): array
{
if (empty($publicationId))
return [];
if (empty($publicationId)) return [];

$publication = $this->getPublication($publicationId);

Expand All @@ -54,8 +53,7 @@ public function getCitations(int $publicationId): array
true
);

if (empty($fromDb) || json_last_error() !== JSON_ERROR_NONE)
return [];
if (empty($fromDb) || json_last_error() !== JSON_ERROR_NONE) return [];

$result = [];

Expand All @@ -77,8 +75,7 @@ public function getCitations(int $publicationId): array
*/
public function saveCitations(int $publicationId, array $citations): bool
{
if (empty($publicationId))
return false;
if (empty($publicationId)) return false;

$publication = $this->getPublication($publicationId);

Expand All @@ -93,28 +90,27 @@ public function saveCitations(int $publicationId, array $citations): bool
}

/**
* This method retrieves publicationWork for a publication and returns normalized to MetadataPublication.
* This method retrieves metadata for a publication and returns normalized to MetadataPublication.
* If nothing found, the method returns a new MetadataPublication.
* If publication is provided, the metadata is retrieved from provided publication object.
*
* @param int $publicationId
* @param Publication|null $publication
* @return MetadataPublication
*/
public function getMetadataPublication(int $publicationId): MetadataPublication
public function getMetadataPublication(int $publicationId, ?Publication $publication = null): MetadataPublication
{
if (empty($publicationId))
return new MetadataPublication();
if (empty($publicationId)) return new MetadataPublication();

$publication = $this->getPublication($publicationId);
if (empty($publication)) $publication = $this->getPublication($publicationId);

$fromDb = json_decode(
$publication->getData(CitationManagerPlugin::CITATION_MANAGER_METADATA_PUBLICATION),
true
);
$metadata = $publication->getData(CitationManagerPlugin::CITATION_MANAGER_METADATA_PUBLICATION);

if (is_string($metadata)) $metadata = json_decode($metadata, true);

if (empty($fromDb) || json_last_error() !== JSON_ERROR_NONE)
return new MetadataPublication();
if (empty($metadata) || json_last_error() !== JSON_ERROR_NONE) return new MetadataPublication();

return ClassHelper::getClassWithValuesAssigned(new MetadataPublication(), $fromDb);
return ClassHelper::getClassWithValuesAssigned(new MetadataPublication(), (array)$metadata);
}

/**
Expand All @@ -126,8 +122,7 @@ public function getMetadataPublication(int $publicationId): MetadataPublication
*/
public function saveMetadataPublication(int $publicationId, MetadataPublication $metadataPublication): bool
{
if (empty($publicationId))
return false;
if (empty($publicationId)) return false;

$publication = $this->getPublication($publicationId);

Expand All @@ -144,26 +139,25 @@ public function saveMetadataPublication(int $publicationId, MetadataPublication
/**
* This method retrieves author metadata for an author and returns normalized.
* If nothing found, the method returns a new MetadataAuthor.
* If author is provided, the metadata is retrieved from provided author object.
*
* @param int $authorId
* @param Author|null $author
* @return MetadataAuthor
*/
public function getMetadataAuthor(int $authorId): MetadataAuthor
public function getMetadataAuthor(int $authorId, ?Author $author = null): MetadataAuthor
{
if (empty($authorId))
return new MetadataAuthor();
if (empty($authorId)) return new MetadataAuthor();

$author = $this->getAuthor($authorId);
if (empty($author)) $author = $this->getAuthor($authorId);

$fromDb = json_decode(
$author->getData(CitationManagerPlugin::CITATION_MANAGER_METADATA_AUTHOR),
true
);
$metadata = $author->getData(CitationManagerPlugin::CITATION_MANAGER_METADATA_AUTHOR);

if (is_string($metadata)) $metadata = json_decode($metadata, true);

if (empty($fromDb) || json_last_error() !== JSON_ERROR_NONE)
return new MetadataAuthor();
if (empty($metadata) || json_last_error() !== JSON_ERROR_NONE) return new MetadataAuthor();

return ClassHelper::getClassWithValuesAssigned(new MetadataAuthor(), $fromDb);
return ClassHelper::getClassWithValuesAssigned(new MetadataAuthor(), (array)$metadata);
}

/**
Expand All @@ -175,8 +169,7 @@ public function getMetadataAuthor(int $authorId): MetadataAuthor
*/
public function saveMetadataAuthor(int $authorId, MetadataAuthor $metadataAuthor): bool
{
if (empty($authorId))
return false;
if (empty($authorId)) return false;

$author = $this->getAuthor($authorId);

Expand All @@ -191,31 +184,31 @@ public function saveMetadataAuthor(int $authorId, MetadataAuthor $metadataAuthor
}

/**
* This method retrieves JournalModel from journal_settings and returns normalized to JournalModel.
* If nothing found, the method returns a new JournalModel.
* This method retrieves metadata for a journal and returns normalized to MetadataJournal.
* If nothing found, the method returns a new MetadataJournal.
* If journal is provided, the metadata is retrieved from provided journal object.
*
* @param int $contextId
* @param int $journalId
* @param Journal|null $journal
* @return MetadataJournal
*/
public function getMetadataJournal(int $contextId): MetadataJournal
public function getMetadataJournal(int $journalId, ?Journal $journal = null): MetadataJournal
{
if (empty($contextId))
return new MetadataJournal();
if (empty($journalId)) return new MetadataJournal();

// Reload the context schema
Services::get('schema')->get(SCHEMA_CONTEXT, true);

$journal = $this->getJournal($contextId);
if (empty($journal)) $journal = $this->getJournal($journalId);

$fromDb = json_decode(
$journal->getData(CitationManagerPlugin::CITATION_MANAGER_METADATA_JOURNAL),
true
);
$metadata = $journal->getData(CitationManagerPlugin::CITATION_MANAGER_METADATA_JOURNAL);

if (is_string($metadata)) $metadata = json_decode($metadata, true);

if (empty($fromDb) || json_last_error() !== JSON_ERROR_NONE)
if (empty($metadata) || json_last_error() !== JSON_ERROR_NONE)
return new MetadataJournal();

return ClassHelper::getClassWithValuesAssigned(new MetadataJournal(), $fromDb);
return ClassHelper::getClassWithValuesAssigned(new MetadataJournal(), (array)$metadata);
}

/**
Expand All @@ -227,8 +220,7 @@ public function getMetadataJournal(int $contextId): MetadataJournal
*/
public function saveMetadataJournal(int $contextId, MetadataJournal $metadataJournal): bool
{
if (empty($contextId))
return false;
if (empty($contextId)) return false;

// Reload the context schema
Services::get('schema')->get(SCHEMA_CONTEXT, true);
Expand Down
7 changes: 3 additions & 4 deletions classes/Workflow/WorkflowTab.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ public function execute(string $hookName, array $args): void

// author(s)
$authors = [];
/* @var Author $author */
foreach ($publication->getData('authors') as $id => $author) {
/* @var Author $author */
$metadataAuthor = json_decode($author->getData(CitationManagerPlugin::CITATION_MANAGER_METADATA_AUTHOR));
if (empty($metadataAuthor)) $metadataAuthor = new MetadataAuthor();
$author->setData(CitationManagerPlugin::CITATION_MANAGER_METADATA_AUTHOR, $metadataAuthor);
$author->setData(CitationManagerPlugin::CITATION_MANAGER_METADATA_AUTHOR,
$pluginDao->getMetadataAuthor($author->getId(), $author));
$authors[] = $author;
}

Expand Down

0 comments on commit 6fe8986

Please sign in to comment.