Handle metadata formats as entities

This commit is contained in:
Sebastian Meyer 2024-01-06 16:24:01 +01:00
parent 60f41db284
commit 152bc9030f
3 changed files with 35 additions and 62 deletions

View File

@ -24,6 +24,7 @@ namespace OCC\OaiPmh2\Console;
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;
@ -70,7 +71,8 @@ class UpdateFormatsCommand extends Command
}
}
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(
@ -91,23 +93,14 @@ class UpdateFormatsCommand extends Command
}
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 */

View File

@ -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();
}
@ -212,10 +199,8 @@ class Database
$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')
@ -451,27 +436,21 @@ class Database
/**
* 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();
}
/**

View File

@ -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;
@ -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')]
@ -184,7 +184,6 @@ class Record
public function setContent(?string $data = null, bool $validate = true): void
{
if (isset($data)) {
$data = trim($data);
if ($validate) {
try {
$data = $this->validate($data);
@ -234,11 +233,13 @@ class Record
*/
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()
]
);