Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 93 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| RdfImporter | |
0.00% |
0 / 93 |
|
0.00% |
0 / 7 |
420 | |
0.00% |
0 / 1 |
| readStrategyFromConfig | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| import | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| flatImport | |
0.00% |
0 / 65 |
|
0.00% |
0 / 1 |
182 | |||
| isDuplicated | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getProperties | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTestTakerImportEventDispatcher | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLogger | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoTestTaker\models; |
| 24 | |
| 25 | use common_report_Report as Report; |
| 26 | use common_Utils; |
| 27 | use core_kernel_classes_Class; |
| 28 | use core_kernel_classes_Resource; |
| 29 | use EasyRdf\Format; |
| 30 | use EasyRdf\Graph; |
| 31 | use oat\generis\model\OntologyRdf; |
| 32 | use oat\generis\model\user\UserRdf; |
| 33 | use oat\oatbox\log\LoggerService; |
| 34 | use oat\tao\model\metadata\exception\InconsistencyConfigException; |
| 35 | use oat\tao\model\TaoOntology; |
| 36 | use oat\taoTestTaker\models\events\dispatcher\TestTakerImportEventDispatcher; |
| 37 | use tao_models_classes_import_RdfImporter; |
| 38 | |
| 39 | class RdfImporter extends tao_models_classes_import_RdfImporter |
| 40 | { |
| 41 | public const CONFIG_ID = 'taoTestTaker/rdfImporterConfig'; |
| 42 | |
| 43 | public const OPTION_STRATEGY = 'strategy'; |
| 44 | |
| 45 | public const OPTION_STRATEGY_FAIL_ON_DUPLICATE = 'fail'; |
| 46 | public const OPTION_STRATEGY_SKIP_ON_DUPLICATE = 'skip'; |
| 47 | public const OPTION_STRATEGY_IMPORT_ON_DUPLICATE = 'import'; |
| 48 | |
| 49 | public const AVAILABLE_STRATEGIES = [ |
| 50 | self::OPTION_STRATEGY_FAIL_ON_DUPLICATE, |
| 51 | self::OPTION_STRATEGY_SKIP_ON_DUPLICATE, |
| 52 | self::OPTION_STRATEGY_IMPORT_ON_DUPLICATE |
| 53 | ]; |
| 54 | |
| 55 | private const NOT_IMPORTABLE_STRATEGIES = [ |
| 56 | self::OPTION_STRATEGY_FAIL_ON_DUPLICATE, |
| 57 | self::OPTION_STRATEGY_SKIP_ON_DUPLICATE |
| 58 | ]; |
| 59 | |
| 60 | /** @var $strategy string */ |
| 61 | private $strategy; |
| 62 | |
| 63 | private function readStrategyFromConfig(): string |
| 64 | { |
| 65 | $config = $this->getServiceLocator()->get(self::CONFIG_ID); |
| 66 | |
| 67 | $strategy = $config->getOption(self::OPTION_STRATEGY); |
| 68 | |
| 69 | if (!in_array($strategy, self::AVAILABLE_STRATEGIES, true)) { |
| 70 | $message = sprintf( |
| 71 | "Bad strategy `%s` configured. Strategy should be one of `[%s]", |
| 72 | $strategy, |
| 73 | implode(',', self::AVAILABLE_STRATEGIES) |
| 74 | ); |
| 75 | |
| 76 | $this->getLogger()->logError($message); |
| 77 | |
| 78 | throw new InconsistencyConfigException($message); |
| 79 | } |
| 80 | |
| 81 | $this->strategy = $strategy; |
| 82 | |
| 83 | return $this->strategy; |
| 84 | } |
| 85 | |
| 86 | public function import($class, $form, $userId = null) |
| 87 | { |
| 88 | $report = parent::import($class, $form); |
| 89 | |
| 90 | $this->getTestTakerImportEventDispatcher() |
| 91 | ->dispatch( |
| 92 | $report, |
| 93 | function ($resource) { |
| 94 | return $this->getProperties($resource); |
| 95 | } |
| 96 | ); |
| 97 | |
| 98 | return $report; |
| 99 | } |
| 100 | |
| 101 | protected function flatImport($content, core_kernel_classes_Class $class) |
| 102 | { |
| 103 | $report = new Report(Report::TYPE_INFO, __('Data import started')); |
| 104 | |
| 105 | $graph = new Graph(); |
| 106 | $graph->parse($content); |
| 107 | |
| 108 | // keep type property |
| 109 | $map = [ |
| 110 | OntologyRdf::RDF_PROPERTY => OntologyRdf::RDF_PROPERTY |
| 111 | ]; |
| 112 | |
| 113 | foreach ($graph->resources() as $resource) { |
| 114 | $map[$resource->getUri()] = common_Utils::getNewUri(); |
| 115 | } |
| 116 | |
| 117 | $format = Format::getFormat('php'); |
| 118 | $data = $graph->serialise($format); |
| 119 | |
| 120 | $importedCount = 0; |
| 121 | |
| 122 | try { |
| 123 | $strategy = $this->readStrategyFromConfig(); |
| 124 | } catch (InconsistencyConfigException $exception) { |
| 125 | $report->add( |
| 126 | new Report( |
| 127 | Report::TYPE_ERROR, |
| 128 | self::class . " configured incorrectly" |
| 129 | ) |
| 130 | ); |
| 131 | |
| 132 | $report->setType(Report::TYPE_ERROR); |
| 133 | $report->setMessage('Data import failed'); |
| 134 | |
| 135 | $this->getLogger()->logError($exception->getMessage()); |
| 136 | |
| 137 | return $report; |
| 138 | } |
| 139 | |
| 140 | foreach ($data as $subjectUri => $propertiesValues) { |
| 141 | $loginProperty = current($propertiesValues[UserRdf::PROPERTY_LOGIN]); |
| 142 | $login = $loginProperty['value']; |
| 143 | |
| 144 | $isDuplicated = $this->isDuplicated($login); |
| 145 | |
| 146 | $duplicationReport = null; |
| 147 | |
| 148 | if ($isDuplicated && in_array($strategy, self::NOT_IMPORTABLE_STRATEGIES, true)) { |
| 149 | $message = "User `%s` was duplicated."; |
| 150 | $report->add(new Report(Report::TYPE_WARNING, sprintf($message, $login))); |
| 151 | |
| 152 | if ($strategy === self::OPTION_STRATEGY_FAIL_ON_DUPLICATE) { |
| 153 | $report->add( |
| 154 | new Report( |
| 155 | Report::TYPE_ERROR, |
| 156 | 'Since the `Fail on duplicate` strategy was chosen, import will now fail' |
| 157 | ) |
| 158 | ); |
| 159 | |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | if ($strategy === self::OPTION_STRATEGY_SKIP_ON_DUPLICATE) { |
| 164 | $report->add( |
| 165 | new Report( |
| 166 | Report::TYPE_WARNING, |
| 167 | 'Since the `Skip on duplicate` strategy was chosen, import will now skip this ' |
| 168 | . 'user, without importing it' |
| 169 | ) |
| 170 | ); |
| 171 | |
| 172 | continue; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | $resource = new core_kernel_classes_Resource($map[$subjectUri]); |
| 177 | $subreport = $this->importProperties($resource, $propertiesValues, $map, $class); |
| 178 | $report->add($subreport); |
| 179 | |
| 180 | if (!$subreport->containsError()) { |
| 181 | $importedCount++; |
| 182 | } |
| 183 | |
| 184 | if ($isDuplicated && $strategy === self::OPTION_STRATEGY_IMPORT_ON_DUPLICATE) { |
| 185 | $report->add( |
| 186 | new Report( |
| 187 | Report::TYPE_WARNING, |
| 188 | 'Since the `Import on duplicate` strategy was chosen, import will import the user, ' |
| 189 | . 'but behaviour is unpredicted' |
| 190 | ) |
| 191 | ); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if ($importedCount > 0 && !$report->containsError()) { |
| 196 | $report->setType(Report::TYPE_SUCCESS); |
| 197 | $report->setMessage(__('Data imported successfully')); |
| 198 | } |
| 199 | |
| 200 | return $report; |
| 201 | } |
| 202 | |
| 203 | private function isDuplicated(string $login): bool |
| 204 | { |
| 205 | $baseResourceClassInstance = new core_kernel_classes_Class(TaoOntology::CLASS_URI_SUBJECT); |
| 206 | |
| 207 | $resources = $baseResourceClassInstance |
| 208 | ->searchInstances([UserRdf::PROPERTY_LOGIN => $login], ['recursive' => true, 'like' => false]); |
| 209 | |
| 210 | return count($resources) > 0; |
| 211 | } |
| 212 | |
| 213 | protected function getProperties(core_kernel_classes_Resource $resource): array |
| 214 | { |
| 215 | return []; |
| 216 | } |
| 217 | |
| 218 | private function getTestTakerImportEventDispatcher(): TestTakerImportEventDispatcher |
| 219 | { |
| 220 | return $this->getServiceLocator()->get(TestTakerImportEventDispatcher::class); |
| 221 | } |
| 222 | |
| 223 | private function getLogger(): LoggerService |
| 224 | { |
| 225 | return $this->getServiceLocator()->get(LoggerService::SERVICE_ID); |
| 226 | } |
| 227 | } |