Skip to content

Commit

Permalink
#1253 Enrichment config import WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
j3nsch committed Nov 14, 2024
1 parent 0d92fda commit ce1ab9f
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 6 deletions.
90 changes: 89 additions & 1 deletion library/Application/Configuration/EnrichmentConfigImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,101 @@
* @license http://www.gnu.org/licenses/gpl.html General Public License
*/

use Opus\Common\EnrichmentKey;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Imports Yaml configuration for Enrichments.
*
* TODO support output over OutputInterface
*/
class Application_Configuration_EnrichmentConfigImporter
{
/** @var OutputInterface */
private $output;

/**
* @param string $filePath
*/
public function import($filePath)
{
// TODO implement
$config = yaml_parse_file($filePath);

if (! $config || ! is_array($config)) {
// TODO throw exception
}

if (isset($config['enrichments'])) {
$enrichmentConfigs = $config['enrichments'];
} else {
$enrichmentConfigs = [$config];
}

foreach ($enrichmentConfigs as $enrichment) {
$this->createEnrichment($enrichment);
}
}

public function createEnrichment($config)
{
$name = $config['name'];

$enrichmentKey = EnrichmentKey::fetchByName($name);

if ($enrichmentKey !== null) {
$this->getOutput()->writeln("Enrichment '{$enrichmentKey}' already exists");
return;
}

$enrichmentKey = EnrichmentKey::new();

$enrichmentKey->setName($name);

if (isset($config['type'])) {
$type = $config['type'];
$enrichmentKey->setType($type);
}

if (isset($config['options'])) {
$options = $config['config'];
$enrichmentKey->setOptions($options);
}

// TODO strict validation

$enrichmentKey->store();

if (isset($config['label'])) {
$this->createTranslations($config['label']);
}

// TODO set translations
}

public function createTranslations($translations)
{

}

/**
* @param OutputInterface $output
* @return $this
*/
public function setOutput($output)
{
$this->output = $output;
return $this;
}

/**
* @return OutputInterface
*/
public function getOutput()
{
if ($this->output === null) {
$this->output = new NullOutput();
}
return $this->output;
}
}
4 changes: 2 additions & 2 deletions library/Application/Console/Tool/EnrichmentImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
*
* Command for importing Enrichment configurations.
*/
class Application_Console_Tool_EnrichmentImportCommand extends Command
{
Expand Down Expand Up @@ -77,7 +77,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return self::FAILURE;
}

$importer = EnrichmentConfigImporter();
$importer = new Application_Configuration_EnrichmentConfigImporter();

$importer->import($file);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* This file is part of OPUS. The software OPUS has been originally developed
* at the University of Stuttgart with funding from the German Research Net,
* the Federal Department of Higher Education and Research and the Ministry
* of Science, Research and the Arts of the State of Baden-Wuerttemberg.
*
* OPUS 4 is a complete rewrite of the original OPUS software and was developed
* by the Stuttgart University Library, the Library Service Center
* Baden-Wuerttemberg, the Cooperative Library Network Berlin-Brandenburg,
* the Saarland University and State Library, the Saxon State Library -
* Dresden State and University Library, the Bielefeld University Library and
* the University Library of Hamburg University of Technology with funding from
* the German Research Foundation and the European Regional Development Fund.
*
* LICENCE
* OPUS is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the Licence, or any later version.
* OPUS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with OPUS; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright Copyright (c) 2024, OPUS 4 development team
* @license http://www.gnu.org/licenses/gpl.html General Public License
*/

class Application_Configuration_EnrichmentConfigImporterTest extends ControllerTestCase
{
/** @var string */
protected $additionalResources = 'database';

/** @var Application_Configuration_EnrichmentConfigImporter */
private $importer;

public function setUp() : void
{
parent::setUp();

$this->importer = new Application_Configuration_EnrichmentConfigImporter();
}

public function testImportSingleEnrichmentConfig()
{
$yamlFile = APPLICATION_PATH . '/tests/resources/enrichments/conferenceType.yml';

$this->importer->import($yamlFile);
}

public function testImportMultipleEnrichmentConfigs()
{
$yamlFile = APPLICATION_PATH . '/tests/resources/enrichments/import.yml';

$this->importer->import($yamlFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public function testImport()

$this->markTestIncomplete('not implemented yet');
}

public function testImportFileNotFound()
{

}
}
12 changes: 9 additions & 3 deletions tests/resources/enrichments/conferenceType.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# Enrichment configuration for conference types
name: conferenceType

type: SelectType

label:
de: Art der Konferenzveröffentlichung
en: Coverence Type
en: Conference Type

config: |
# TODO support additional translation keys (like hint)
# TODO support strict validation settign

options: |
Konferenzband
Konferenzartikel
Konferenz-Poster
Konferenz-Abstract
Sonstiges
Sonstiges
# TODO support multiple option keys
1 change: 1 addition & 0 deletions tests/resources/enrichments/import.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Example of configuration for multiple enrichments
enrichments:
- name: opus.import.checksum
- name: opus.import.date
Expand Down

0 comments on commit ce1ab9f

Please sign in to comment.