Skip to content

Commit

Permalink
Merge pull request #2630 from oat-sa/backport/metadata-import-export-…
Browse files Browse the repository at this point in the history
…improvments

Backport Metadata import/export improvements
  • Loading branch information
bartlomiejmarszal authored Nov 22, 2024
2 parents 5561be1 + efae284 commit 8d0ebee
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 51 deletions.
5 changes: 0 additions & 5 deletions model/qti/metadata/importer/MetaMetadataImportMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,10 @@ public function mapMetaMetadataToProperties(
foreach ($metaMetadataProperties as $metaMetadataProperty) {
if ($match = $this->matchProperty($metaMetadataProperty, $itemClass->getProperties(true))) {
$matchedProperties['itemProperties'][$metaMetadataProperty['uri']] = $match;
continue;
}

if ($testClass && $match = $this->matchProperty($metaMetadataProperty, $testClass->getProperties(true))) {
$matchedProperties['testProperties'][$metaMetadataProperty['uri']] = $match;
continue;
}
if ($match === null) {
throw new PropertyDoesNotExistException($metaMetadataProperty);
}
}
return $matchedProperties;
Expand Down
65 changes: 48 additions & 17 deletions model/qti/metadata/ontology/MappedMetadataInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use oat\generis\model\OntologyAwareTrait;
use oat\taoBackOffice\model\lists\ListService;
use oat\taoQtiItem\model\qti\metadata\simple\SimpleMetadataValue;
use tao_helpers_form_elements_Readonly as ReadOnlyWidget;

class MappedMetadataInjector
{
Expand Down Expand Up @@ -54,27 +55,37 @@ public function inject(array $mappedProperties, array $metadataValues, Resource
if ($currentValue && $currentValue === $metadataValue->getValue()) {
continue;
}
if ($mappedProperties[$mappedPath]->getRange()->getUri() === RDFS_LITERAL) {
if (
$mappedProperties[$mappedPath]->getWidget()
&& $mappedProperties[$mappedPath]->getWidget()->getUri() === ReadOnlyWidget::WIDGET_ID
) {
continue;
}
if (
$mappedProperties[$mappedPath]->getRange()
&& $mappedProperties[$mappedPath]->getRange()->getUri() === RDFS_LITERAL
) {
// If resource already has property value, remove it.
if ($resource->getPropertyValuesCollection($mappedProperties[$mappedPath])->count() > 0) {
$propertyValue = $resource->getUniquePropertyValue($mappedProperties[$mappedPath]);
$resource->removePropertyValue($mappedProperties[$mappedPath], $propertyValue);
}

$resource->setPropertyValue($mappedProperties[$mappedPath], $metadataValue->getValue());
break;
}

$list = $this->listService->getListElements($mappedProperties[$mappedPath]->getRange());
foreach ($list as $listElement) {
if (
$listElement->getLabel() === $metadataValue->getValue()
|| $listElement->getOriginalUri() === $metadataValue->getValue()
) {
$resource->setPropertyValue(
$mappedProperties[$mappedPath],
$this->getResource($listElement->getUri())
);
/** @var Property $property */
$property = $mappedProperties[$mappedPath];
if ($property->isMultiple() === false) {
break;
}
}
if ($mappedProperties[$mappedPath]->getRange() !== null) {
$this->setListValue($mappedProperties[$mappedPath], $resource, $metadataValue);
break;
}

if ($mappedProperties[$mappedPath]->getRange() === null) {
$resource->setPropertyValue(
$mappedProperties[$mappedPath],
$this->getResource($metadataValue->getValue())
);
break;
}
}
}
Expand All @@ -84,4 +95,24 @@ private function isInjectableProperty(array $mappedProperties, string $mappedPat
{
return isset($mappedProperties[$mappedPath]) && $mappedProperties[$mappedPath] instanceof Property;
}

private function setListValue(Property $property, Resource $resource, SimpleMetadataValue $metadataValue): void
{
$list = $this->listService->getListElements($property->getRange());
foreach ($list as $listElement) {
if (
$listElement->getLabel() === $metadataValue->getValue()
|| $listElement->getOriginalUri() === $metadataValue->getValue()
) {
$resource->setPropertyValue(
$property,
$this->getResource($listElement->getUri())
);

if ($property->isMultiple() === false) {
break;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public function testMapMetaMetadataToProperties(): void
$propertyMock = $this->createMock(core_kernel_classes_Property::class);
$resourceMock = $this->createMock(core_kernel_classes_Resource::class);


$itemClass->method('getProperties')
->willReturn([$propertyMock, $propertyMock, $propertyMock]);
$testClass->method('getProperties')
Expand All @@ -86,7 +85,15 @@ public function testMapMetaMetadataToProperties(): void
'http://some-other-uri',
'http://widget.uri',
'http://some-other-uri',
'http://widget.uri'
'http://widget.uri',
'http://example.com/uri1',
'http://widget.uri',
'http://some-other-uri',
'http://widget.uri',
'http://some-other-uri',
'http://widget.uri',
'http://some-other-uri',
'http://widget.uri',
);
$propertyMock->method('getLabel')
->willReturn('label2', 'some-other-label', 'some-other-other-label');
Expand All @@ -105,31 +112,6 @@ public function testMapMetaMetadataToProperties(): void

$result = $this->subject->mapMetaMetadataToProperties($metaMetadataProperties, $itemClass, $testClass);
self::assertNotNull($result);
self::assertEquals(3, count($result['itemProperties']));
}

public function testMapMetaMetadataToPropertiesThrowErrorWhenCannotMapProperty(): void
{
$this->expectException(PropertyDoesNotExistException::class);
$metaMetadataProperties = [
[
'uri' => 'http://example.com/uri1',
'label' => 'label1',
'alias' => 'alias1',
'checksum' => 'qwerty1234',
'multiple' => 'http://resource.uri/false'
]
];

$itemClass = $this->createMock(core_kernel_classes_Class::class);
$testClass = $this->createMock(core_kernel_classes_Class::class);
$propertyMock = $this->createMock(core_kernel_classes_Property::class);

$itemClass->method('getProperties')
->willReturn([$propertyMock, $propertyMock, $propertyMock]);
$testClass->method('getProperties')
->willReturn([$propertyMock, $propertyMock, $propertyMock]);
$result = $this->subject->mapMetaMetadataToProperties($metaMetadataProperties, $itemClass, $testClass);
self::assertNotNull($result);
self::assertEquals(1, count($result['itemProperties']));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function testInject()
$classMock = $this->createMock(core_kernel_classes_Class::class);
$resourceMock = $this->createMock(core_kernel_classes_Resource::class);
$valueMock = $this->createMock(Value::class);
$widgetPropertyMock = $this->createMock(core_kernel_classes_Property::class);

$mappedProperties = [
'mappedPath1' => $propertyMock,
Expand All @@ -70,6 +71,12 @@ public function testInject()
$propertyMock->method('getRange')
->willReturn($classMock);

$propertyMock->method('getWidget')
->willReturn($widgetPropertyMock);

$widgetPropertyMock->method('getUri')
->willReturn('someUri');

$valueMock->method('getLabel')
->willReturn(
'label',
Expand All @@ -81,6 +88,7 @@ public function testInject()
);



$this->ontologyMock->method('getResource')
->willReturn($resourceMock);

Expand All @@ -94,7 +102,7 @@ public function testInject()
->willReturn('matchedUri', 'otherUri', 'otherUri');

$resourceMock
->expects(self::exactly(4))
->expects(self::exactly(2))
->method('setPropertyValue')
->with($propertyMock, $resourceMock);

Expand Down

0 comments on commit 8d0ebee

Please sign in to comment.