Skip to content

Commit

Permalink
Merge branch 'release-8.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed May 14, 2021
2 parents 6387c47 + b7044a8 commit e17409a
Show file tree
Hide file tree
Showing 5 changed files with 679 additions and 0 deletions.
47 changes: 47 additions & 0 deletions migrations/Version202105041023253148_taoTestTaker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace oat\taoTestTaker\migrations;

use Doctrine\DBAL\Schema\Schema;
use oat\oatbox\config\ConfigurationService;
use oat\tao\scripts\tools\migrations\AbstractMigration;
use oat\taoTestTaker\models\RdfImporter;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version202105041023253148_taoTestTaker extends AbstractMigration
{

public function getDescription(): string
{
return 'Register validation config for `' . RdfImporter::class . '`';
}

public function up(Schema $schema): void
{
$this->getServiceManager()->register(
RdfImporter::CONFIG_ID,
new ConfigurationService(
[
RdfImporter::OPTION_STRATEGY => RdfImporter::OPTION_STRATEGY_FAIL_ON_DUPLICATE
]
)
);
}

public function down(Schema $schema): void
{
//Reverting to default behaviour for RdfImporter - import even duplicated TTs
$this->getServiceManager()->register(
RdfImporter::CONFIG_ID,
new ConfigurationService(
[
RdfImporter::OPTION_STRATEGY => RdfImporter::OPTION_STRATEGY_IMPORT_ON_DUPLICATE
]
)
);
}
}
170 changes: 170 additions & 0 deletions models/RdfImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,67 @@

namespace oat\taoTestTaker\models;

use common_report_Report as Report;
use common_Utils;
use core_kernel_classes_Class;
use core_kernel_classes_Resource;
use EasyRdf_Format;
use EasyRdf_Graph;
use oat\generis\model\OntologyRdf;
use oat\generis\model\user\UserRdf;
use oat\oatbox\log\LoggerService;
use oat\tao\model\metadata\exception\InconsistencyConfigException;
use oat\tao\model\TaoOntology;
use oat\taoTestTaker\models\events\dispatcher\TestTakerImportEventDispatcher;
use tao_models_classes_import_RdfImporter;

class RdfImporter extends tao_models_classes_import_RdfImporter
{
public const CONFIG_ID = 'taoTestTaker/rdfImporterConfig';

public const OPTION_STRATEGY = 'strategy';

public const OPTION_STRATEGY_FAIL_ON_DUPLICATE = 'fail';
public const OPTION_STRATEGY_SKIP_ON_DUPLICATE = 'skip';
public const OPTION_STRATEGY_IMPORT_ON_DUPLICATE = 'import';

public const AVAILABLE_STRATEGIES = [
self::OPTION_STRATEGY_FAIL_ON_DUPLICATE,
self::OPTION_STRATEGY_SKIP_ON_DUPLICATE,
self::OPTION_STRATEGY_IMPORT_ON_DUPLICATE
];

private const NOT_IMPORTABLE_STRATEGIES = [
self::OPTION_STRATEGY_FAIL_ON_DUPLICATE,
self::OPTION_STRATEGY_SKIP_ON_DUPLICATE
];

/** @var $strategy string */
private $strategy;

private function readStrategyFromConfig(): string
{
$config = $this->getServiceLocator()->get(self::CONFIG_ID);

$strategy = $config->getOption(self::OPTION_STRATEGY);

if (!in_array($strategy, self::AVAILABLE_STRATEGIES, true)) {
$message = sprintf(
"Bad strategy `%s` configured. Strategy should be one of `[%s]",
$strategy,
implode(',', self::AVAILABLE_STRATEGIES)
);

$this->getLogger()->logError($message);

throw new InconsistencyConfigException($message);
}

$this->strategy = $strategy;

return $this->strategy;
}

public function import($class, $form, $userId = null)
{
$report = parent::import($class, $form);
Expand All @@ -44,6 +99,116 @@ function ($resource)
return $report;
}

protected function flatImport($content, core_kernel_classes_Class $class)
{
$report = new Report(Report::TYPE_INFO, __('Data import started'));

$graph = new EasyRdf_Graph();
$graph->parse($content);

// keep type property
$map = [
OntologyRdf::RDF_PROPERTY => OntologyRdf::RDF_PROPERTY
];

foreach ($graph->resources() as $resource) {
$map[$resource->getUri()] = common_Utils::getNewUri();
}

$format = EasyRdf_Format::getFormat('php');
$data = $graph->serialise($format);

$importedCount = 0;

try {
$strategy = $this->readStrategyFromConfig();
} catch (InconsistencyConfigException $exception) {
$report->add(
new Report(
Report::TYPE_ERROR,
self::class . " configured incorrectly"
)
);

$report->setType(Report::TYPE_ERROR);
$report->setMessage('Data import failed');

$this->getLogger()->logError($exception->getMessage());

return $report;
}

foreach ($data as $subjectUri => $propertiesValues) {
$loginProperty = current($propertiesValues[UserRdf::PROPERTY_LOGIN]);
$login = $loginProperty['value'];

$isDuplicated = $this->isDuplicated($login);

$duplicationReport = null;

if ($isDuplicated && in_array($strategy, self::NOT_IMPORTABLE_STRATEGIES, true)) {
$message = "User `%s` was duplicated.";
$report->add(new Report(Report::TYPE_WARNING, sprintf($message, $login)));

if ($strategy === self::OPTION_STRATEGY_FAIL_ON_DUPLICATE) {
$report->add(
new Report(
Report::TYPE_ERROR,
'Since the `Fail on duplicate` strategy was chosen, import will now fail'
)
);

break;
}

if ($strategy === self::OPTION_STRATEGY_SKIP_ON_DUPLICATE) {
$report->add(
new Report(
Report::TYPE_WARNING,
'Since the `Skip on duplicate` strategy was chosen, import will now skip this user, without importing it'
)
);

continue;
}
}

$resource = new core_kernel_classes_Resource($map[$subjectUri]);
$subreport = $this->importProperties($resource, $propertiesValues, $map, $class);
$report->add($subreport);

if (!$subreport->containsError()) {
$importedCount++;
}

if ($isDuplicated && $strategy === self::OPTION_STRATEGY_IMPORT_ON_DUPLICATE){
$report->add(
new Report(
Report::TYPE_WARNING,
'Since the `Import on duplicate` strategy was chosen, import will import the user, but behaviour is unpredicted'
)
);
}
}

if ($importedCount > 0 && !$report->containsError()) {
$report->setType(Report::TYPE_SUCCESS);
$report->setMessage(__('Data imported successfully'));
}

return $report;
}

private function isDuplicated(string $login): bool
{
$baseResourceClassInstance = new core_kernel_classes_Class(TaoOntology::CLASS_URI_SUBJECT);

$resources = $baseResourceClassInstance
->searchInstances([UserRdf::PROPERTY_LOGIN => $login], ['recursive' => true, 'like' => false]);

return count($resources) > 0;
}

protected function getProperties(core_kernel_classes_Resource $resource): array
{
return [];
Expand All @@ -53,4 +218,9 @@ private function getTestTakerImportEventDispatcher(): TestTakerImportEventDispat
{
return $this->getServiceLocator()->get(TestTakerImportEventDispatcher::class);
}

private function getLogger(): LoggerService
{
return $this->getServiceLocator()->get(LoggerService::SERVICE_ID);
}
}
Loading

0 comments on commit e17409a

Please sign in to comment.