Skip to content

Commit

Permalink
Handle metadata formats as entities
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-meyer committed Jan 6, 2024
1 parent 60f41db commit 152bc90
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 62 deletions.
29 changes: 11 additions & 18 deletions src/Console/UpdateFormatsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

use OCC\OaiPmh2\Configuration;
use OCC\OaiPmh2\Database;
use OCC\OaiPmh2\Database\Format;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -70,7 +71,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}
try {
Database::getInstance()->addOrUpdateMetadataFormat($prefix, $format['namespace'], $format['schema']);
$format = new Format($prefix, $format['namespace'], $format['schema']);
Database::getInstance()->addOrUpdateMetadataFormat($format);
++$added;
$output->writeln([
sprintf(
Expand All @@ -91,23 +93,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
foreach (array_keys($inDatabase) as $prefix) {
if (!in_array($prefix, array_keys($formats), true)) {
if (Database::getInstance()->removeMetadataFormat($prefix)) {
++$deleted;
$output->writeln([
sprintf(
' [OK] Metadata format "%s" and all associated records deleted successfully! ',
$prefix
)
]);
} else {
$failure = true;
$output->writeln([
sprintf(
' [ERROR] Could not delete metadata format "%s". ',
$prefix
)
]);
}
Database::getInstance()->removeMetadataFormat($inDatabase[$prefix]);
++$deleted;
$output->writeln([
sprintf(
' [OK] Metadata format "%s" and all associated records deleted successfully! ',
$prefix
)
]);
}
}
/** @var Application */
Expand Down
61 changes: 20 additions & 41 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,32 +73,19 @@ class Database
/**
* Add or update metadata format.
*
* @param string $prefix The metadata prefix
* @param string $namespace The namespace URI
* @param string $schema The schema URL
* @param Format $newFormat The metadata format
*
* @return void
*
* @throws ValidationFailedException
*/
public function addOrUpdateMetadataFormat(string $prefix, string $namespace, string $schema): void
public function addOrUpdateMetadataFormat(Format $newFormat): void
{
$format = $this->entityManager->find(Format::class, $prefix);
if (isset($format)) {
try {
$format->setNamespace($namespace);
$format->setSchema($schema);
} catch (ValidationFailedException $exception) {
throw $exception;
}
$oldFormat = $this->entityManager->find(Format::class, $newFormat->getPrefix());
if (isset($oldFormat)) {
$oldFormat->setNamespace($newFormat->getNamespace());
$oldFormat->setSchema($newFormat->getSchema());
} else {
try {
$format = new Format($prefix, $namespace, $schema);
} catch (ValidationFailedException $exception) {
throw $exception;
}
$this->entityManager->persist($newFormat);
}
$this->entityManager->persist($format);
$this->entityManager->flush();
}

Expand Down Expand Up @@ -212,10 +199,8 @@ public function getMetadataFormats(?string $identifier = null): Result
$dql->select('format')
->from(Format::class, 'format', 'format.prefix');
if (isset($identifier)) {
$dql->innerJoin(
'format.records',
'record',
'WITH',
$dql->innerJoin(Record::class, 'record')
->where(
$dql->expr()->andX(
$dql->expr()->eq('record.identifier', ':identifier'),
$dql->expr()->isNotNull('record.content')
Expand Down Expand Up @@ -451,27 +436,21 @@ public function pruneResumptionTokens(): int
/**
* Remove metadata format and all associated records.
*
* @param string $prefix The metadata prefix
* @param Format $format The metadata format
*
* @return bool TRUE on success or FALSE on failure
* @return void
*/
public function removeMetadataFormat(string $prefix): bool
public function removeMetadataFormat(Format $format): void
{
$format = $this->entityManager->find(Format::class, $prefix);
if (isset($format)) {
$repository = $this->entityManager->getRepository(Record::class);
$criteria = Criteria::create()->where(Criteria::expr()->eq('format', $format));
$records = $repository->matching($criteria);
foreach ($records as $record) {
$this->entityManager->remove($record);
}
$this->entityManager->remove($format);
$this->entityManager->flush();
$this->pruneOrphanSets();
return true;
} else {
return false;
$repository = $this->entityManager->getRepository(Record::class);
$criteria = Criteria::create()->where(Criteria::expr()->eq('format', $format));
$records = $repository->matching($criteria);
foreach ($records as $record) {
$this->entityManager->remove($record);
}
$this->entityManager->remove($format);
$this->entityManager->flush();
$this->pruneOrphanSets();
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Database/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Record
* The associated format.
*/
#[ORM\Id]
#[ORM\ManyToOne(targetEntity: Format::class, inversedBy: 'records')]
#[ORM\ManyToOne(targetEntity: Format::class)]
#[ORM\JoinColumn(name: 'format', referencedColumnName: 'prefix')]
private Format $format;

Expand All @@ -72,7 +72,7 @@ class Record
*
* @var Collection<string, Set>
*/
#[ORM\ManyToMany(targetEntity: Set::class, inversedBy: 'records', indexBy: 'spec')]
#[ORM\ManyToMany(targetEntity: Set::class, inversedBy: 'records', indexBy: 'spec', cascade: ['persist'])]
#[ORM\JoinTable(name: 'records_sets')]
#[ORM\JoinColumn(name: 'record_identifier', referencedColumnName: 'identifier')]
#[ORM\JoinColumn(name: 'record_format', referencedColumnName: 'format')]
Expand Down Expand Up @@ -184,7 +184,6 @@ public function removeSet(Set $set): void
public function setContent(?string $data = null, bool $validate = true): void
{
if (isset($data)) {
$data = trim($data);
if ($validate) {
try {
$data = $this->validate($data);
Expand Down Expand Up @@ -234,11 +233,13 @@ public function setLastChanged(?DateTime $dateTime = null): void
*/
protected function validate(string $xml): string
{
$xml = trim($xml);
$validator = Validation::createValidator();
$violations = $validator->validate(
$xml,
[
new Assert\Type('string'),
// TODO: Validate well-formed XML.
new Assert\NotBlank()
]
);
Expand Down

0 comments on commit 152bc90

Please sign in to comment.