Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove idenitfier/data wrapper, take 2 #3504

Draft
wants to merge 34 commits into
base: 2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
dfc5595
Removed [{"data":{},"identifier":""}] wrapper from the JSON metadata …
May 13, 2021
412df7d
Wrapped JSON metadata in the Metastore controller getAll()
May 13, 2021
ed123d4
WebServiceApiTest::getAll(). Added TODO
May 13, 2021
26f0b12
WebServiceApiTest. Fixed tests
May 13, 2021
535f2b0
Moved wrapMetadata() to the Service
May 13, 2021
49f7760
metastore_admin_preprocess_views_view_field(). Fixed $data array
May 13, 2021
eec2f2f
Service. Fixed codeclimate issue
May 13, 2021
2e35162
Service::getCatalog(). Fixed cypress tests
May 13, 2021
5a13366
LifeCycle/Data -> LifeCycle/LifeCycle
May 14, 2021
1a4cb99
Storage. Used the right variable name in getAll().
May 14, 2021
e742a5b
Metastore controller tests. Fixed GET request arguments order
May 14, 2021
8afd008
Metastore controller. Added test for distributions with ref ids
May 14, 2021
d48b788
Moved wrapping logic to the Service. Updated tests
May 17, 2021
bb66638
Replaced 'Service::wrapMetadata()' with 'self::wrapMetadata()' within…
May 17, 2021
a689719
Incorporated RootedJsonData into the wrapping changes
May 20, 2021
bb09cb3
Added todos to unwrap the data if it came wrapped from the db
May 20, 2021
83d91d8
Added the legacy schema
May 20, 2021
16772d6
Core schema unwrap
dafeder May 20, 2021
6020b57
dereference(). Decoded RootedJsonData objects
May 20, 2021
2a907cd
Used the legacy schema
May 20, 2021
a9d7e02
Service::getAll(). Fixed the variable name
May 20, 2021
9cbf275
Fixed the publisher schema. Removed "$schema"
May 21, 2021
d9e12c8
Dereferencer::dereference(). Allowed both a single object and an array
May 21, 2021
87b8055
Dereferencer. Fixed tests
May 21, 2021
86e5c40
Storage::createNewEntity(). Removed redundant 'data' property
May 21, 2021
f50c589
Fixed codeclimate issues
May 21, 2021
a8f8956
Added line break to add a new commit
May 25, 2021
321739a
Removed unused setDataModifierPlugins()
May 25, 2021
c0d3d0c
Added wrapping methods to Storage
May 25, 2021
1302cf3
Fixed codeclimate issues. Combined isWrappedMetadata() with unwrapMet…
May 25, 2021
b9e2db1
Added testGetRetrievePublishedLegacy()
May 26, 2021
e3b37ec
Added comments
May 26, 2021
796b91a
DataTest::testGetRetrievePublishedLegacy(). Used getEtmChain()
May 27, 2021
db24093
Merge remote-tracking branch 'origin/2.x' into Feature/Referencer
dafeder Jun 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion modules/metastore/metastore.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ services:
arguments:
- '@config.factory'
- '@dkan.metastore.storage'
- '@dkan.metastore.service'
calls:
- [setLoggerFactory, ['@logger.factory']]

Expand All @@ -63,7 +64,7 @@ services:
class: \Drupal\metastore\ValidMetadataFactory
arguments:
- '@dkan.metastore.schema_retriever'

dkan.metastore.metastore_item_factory:
class: \Drupal\metastore\NodeWrapper\NodeDataFactory
arguments:
Expand Down
16 changes: 6 additions & 10 deletions modules/metastore/modules/metastore_admin/metastore_admin.module
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,31 @@ function metastore_admin_preprocess_views_view_field(&$vars) {
$entity_id = $entity->id();

// Modify data titles to display the metadata title values rather than uuids.
$data = json_decode($entity->field_json_metadata->value);
$title = (array) $data;
$data = json_decode($entity->field_json_metadata->value, TRUE);

if ($vars['field']->field == 'title') {
switch ($entity->field_data_type->value) {
case 'keyword':
case 'theme':
$vars['output'] = [
'#markup' => Markup::create(
metastore_admin_create_text_link($title['data'], 'internal:/node/' . $entity_id)
metastore_admin_create_text_link($data, 'internal:/node/' . $entity_id)
),
];
break;

case 'publisher':
$name = (array) $title['data'];

$vars['output'] = [
'#markup' => Markup::create(
metastore_admin_create_text_link($name['name'], 'internal:/node/' . $entity_id)
metastore_admin_create_text_link($data['name'], 'internal:/node/' . $entity_id)
),
];
break;

case 'distribution':
$dist = (array) $title['data'];
$mediatype = isset($dist['mediaType']) ? $entity_id . ' ' . $dist['mediaType'] : '';
$format = isset($dist['format']) ? $entity_id . ' ' . $dist['format'] : $mediatype;
$title = $dist['title'] ?? $format;
$mediatype = isset($data['mediaType']) ? $entity_id . ' ' . $data['mediaType'] : '';
$format = isset($data['format']) ? $entity_id . ' ' . $data['format'] : $mediatype;
$title = $data['title'] ?? $format;

$vars['output'] = [
'#markup' => Markup::create(
Expand Down
8 changes: 4 additions & 4 deletions modules/metastore/src/LifeCycle/LifeCycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ protected function datasetUpdate(MetastoreItemInterface $data) {
protected function distributionLoad(MetastoreItemInterface $data) {
$metadata = $data->getMetaData();

if (!isset($metadata->data->downloadURL)) {
if (!isset($metadata->downloadURL)) {
return;
}

$downloadUrl = $metadata->data->downloadURL;
$downloadUrl = $metadata->downloadURL;

if (isset($downloadUrl) && !filter_var($downloadUrl, FILTER_VALIDATE_URL)) {
$resourceIdentifier = $downloadUrl;
Expand All @@ -144,14 +144,14 @@ protected function distributionLoad(MetastoreItemInterface $data) {
$downloadUrl = isset($original) ? $original : "";

$refProperty = "%Ref:downloadURL";
$metadata->data->{$refProperty} = count($ref) == 0 ? NULL : $ref;
$metadata->{$refProperty} = count($ref) == 0 ? NULL : $ref;
}

if (is_string($downloadUrl)) {
$downloadUrl = UrlHostTokenResolver::resolve($downloadUrl);
}

$metadata->data->downloadURL = $downloadUrl;
$metadata->downloadURL = $downloadUrl;

$data->setMetadata($metadata);
}
Expand Down
6 changes: 5 additions & 1 deletion modules/metastore/src/NodeWrapper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drupal\common\LoggerTrait;
use Drupal\Core\Entity\EntityInterface;
use Drupal\metastore\MetastoreItemInterface;
use Drupal\metastore\Storage\Data as Storage;
use Drupal\node\Entity\Node;

/**
Expand Down Expand Up @@ -74,7 +75,10 @@ public function getDataType() {
*/
public function getMetaData() {
$this->fix();
return json_decode($this->node->get('field_json_metadata')->getString());
$metadata = $this->node->get('field_json_metadata')->getString();
// Legacy metadata could come unwrapped from the db.
$metadata = Storage::unwrapMetadata($metadata);
return json_decode($metadata);
}

/**
Expand Down
24 changes: 19 additions & 5 deletions modules/metastore/src/Reference/Dereferencer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Contracts\FactoryInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\common\LoggerTrait;
use Drupal\metastore\Service;

/**
* Metastore dereferencer.
Expand All @@ -20,12 +21,24 @@ class Dereferencer {
*/
private $storageFactory;

/**
* Metastore service.
*
* @var \Drupal\metastore\Service
*/
private $service;

/**
* Constructor.
*/
public function __construct(ConfigFactoryInterface $configService, FactoryInterface $storageFactory) {
public function __construct(
ConfigFactoryInterface $configService,
FactoryInterface $storageFactory,
Service $service
) {
$this->setConfigService($configService);
$this->storageFactory = $storageFactory;
$this->service = $service;
}

/**
Expand All @@ -48,8 +61,8 @@ public function dereference($data) {
if (isset($data->{$propertyId})) {
$referenceProperty = "%Ref:{$propertyId}";
[$ref, $actual] = $this->dereferenceProperty($propertyId, $data->{$propertyId});
$data->{$referenceProperty} = $ref;
$data->{$propertyId} = $actual;
$data->{$referenceProperty} = is_array($ref) ? array_map('json_decode', $ref) : json_decode($ref);
$data->{$propertyId} = is_array($actual) ? array_map('json_decode', $actual) : json_decode($actual);
}
}
return $data;
Expand Down Expand Up @@ -128,8 +141,9 @@ private function dereferenceSingle(string $property_id, string $uuid) {
$value = $storage->retrieve($uuid);

if ($value) {
$metadata = json_decode($value);
return [$metadata, $metadata->data];
$metadata = $this->service->getValidMetadataFactory()->get($property_id, $value);
$wrapped_metadata = $this->service->wrapMetadata($uuid, $metadata);
return [$wrapped_metadata, $metadata];
}
// If a property node was not found, it most likely means it was deleted
// while still being referenced.
Expand Down
10 changes: 4 additions & 6 deletions modules/metastore/src/Reference/Referencer.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,14 @@ private function checkExistingReference(string $property_id, $data) {
* @throws \Drupal\Core\Entity\EntityStorageException
*/
private function createPropertyReference(string $property_id, $value) {
// Create json metadata for the reference.
$data = new \stdClass();
$data->identifier = $this->getUuidService()->generate($property_id, $value);
$data->data = $value;
$json = json_encode($data);
$json = json_encode($value);
$identifier = $this->getUuidService()->generate($property_id, $value);

// Create node to store this reference.
$storage = $this->storageFactory->getInstance($property_id);
$entity_uuid = $storage->store($json, $data->identifier);
$entity_uuid = $storage->store($json, $identifier);
return $entity_uuid;

}

}
1 change: 1 addition & 0 deletions modules/metastore/src/SchemaRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function getAllIds() {
'theme.ui',
'keyword',
'keyword.ui',
'legacy',
];
}

Expand Down
56 changes: 30 additions & 26 deletions modules/metastore/src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,6 @@ public function __construct(SchemaRetriever $schemaRetriever, DataFactory $facto
$this->validMetadataFactory = $validMetadataFactory;
}

/**
* Setter to discover data modifier plugins.
*
* @param \Drupal\common\Plugin\DataModifierManager $pluginManager
* Injected plugin manager.
*/
public function setDataModifierPlugins(DataModifierManager $pluginManager) {
$this->pluginManager = $pluginManager;
$this->plugins = $this->discover();
}

/**
* Get schemas.
*/
Expand Down Expand Up @@ -134,20 +123,21 @@ private function getStorage(string $schema_id): StorerInterface {
public function getAll($schema_id): array {
$jsonStringsArray = $this->getStorage($schema_id)->retrieveAll();

$objects = array_map(
function ($jsonString) use ($schema_id) {
$data = $this->validMetadataFactory->get($schema_id, $jsonString);
try {
return $this->dispatchEvent(self::EVENT_DATA_GET, $data, function ($data) {
return $data instanceof RootedJsonData;
});
$objects = [];
foreach ($jsonStringsArray as $id => $jsonString) {
$data = $this->validMetadataFactory->get($schema_id, $jsonString);
try {
$object = $this->dispatchEvent(self::EVENT_DATA_GET, $data);
if ($schema_id != 'dataset') {
$object = $this->wrapMetadata($id, $object);
}
catch (\Exception $e) {
return new RootedJsonData(json_encode(["message" => $e->getMessage()]));
}
},
$jsonStringsArray
);
}
catch (\Exception $e) {
$object = $this->validMetadataFactory->get(NULL, json_encode(["message" => $e->getMessage()]));
}

$objects[] = $object;
}

return $this->dispatchEvent(self::EVENT_DATA_GET_ALL, $objects, function ($data) {
if (!is_array($data)) {
Expand Down Expand Up @@ -175,8 +165,10 @@ function ($jsonString) use ($schema_id) {
public function get($schema_id, $identifier): RootedJsonData {
$json_string = $this->getStorage($schema_id)->retrievePublished($identifier);
$data = $this->validMetadataFactory->get($schema_id, $json_string);

$data = $this->dispatchEvent(self::EVENT_DATA_GET, $data);
if ($schema_id != 'dataset') {
$data = $this->wrapMetadata($identifier, $data);
}
return $data;
}

Expand Down Expand Up @@ -367,7 +359,7 @@ public function delete($schema_id, $identifier) {
*/
public function getCatalog() {
$catalog = $this->getSchema('catalog');
$catalog->dataset = $this->getAll('dataset');
$catalog->dataset = array_values($this->getAll('dataset'));

return $catalog;
}
Expand Down Expand Up @@ -429,4 +421,16 @@ public static function removeReferences(RootedJsonData $object, $prefix = "%"):
return $object;
}

/**
* Wraps metadata with [{"data":{},"identifier":""}].
*/
public function wrapMetadata($uuid, RootedJsonData $metadata): RootedJsonData {
$wrapped_metadata = [
'identifier' => $uuid,
'data' => $metadata->get('$'),
];

return $this->getValidMetadataFactory()->get('legacy', json_encode($wrapped_metadata));
}

}
38 changes: 31 additions & 7 deletions modules/metastore/src/Storage/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ public function retrieveAll(): array {
$all = [];
foreach ($entity_ids as $nid) {
$entity = $this->entityStorage->load($nid);
if ($entity->get('moderation_state')->getString() === 'published') {
$all[] = $entity->get('field_json_metadata')->getString();
if ($entity->get('moderation_state')->getString() !== 'published') {
continue;
}
$metadata = $entity->get('field_json_metadata')->getString();
// Legacy metadata could come unwrapped from the db.
$all[$entity->uuid()] = self::unwrapMetadata($metadata);
}
return $all;
}
Expand All @@ -116,7 +119,9 @@ public function retrievePublished(string $uuid) : ?string {
$entity = $this->getEntityPublishedRevision($uuid);

if ($entity && $entity->get('moderation_state')->getString() == 'published') {
return $entity->get('field_json_metadata')->getString();
$metadata = $entity->get('field_json_metadata')->getString();
// Legacy metadata could come unwrapped from the db.
return self::unwrapMetadata($metadata);
}

throw new \Exception("Error retrieving published dataset: {$uuid} not found.");
Expand All @@ -136,11 +141,13 @@ public function retrieve(string $uuid) : ?string {
$entity = $this->getEntityLatestRevision($uuid);
}

if ($entity) {
return $entity->get('field_json_metadata')->getString();
if (!$entity) {
throw new \Exception("Error retrieving dataset: {$uuid} not found.");
}

throw new \Exception("Error retrieving dataset: {$uuid} not found.");
$metadata = $entity->get('field_json_metadata')->getString();
// Legacy metadata could come unwrapped from the db.
return self::unwrapMetadata($metadata);
}

/**
Expand Down Expand Up @@ -291,7 +298,7 @@ private function createNewEntity($uuid, $data) {
$title = isset($data->title) ? $data->title : $data->name;
}
else {
$title = md5(json_encode($data->data));
$title = md5(json_encode($data));
}
$entity = $this->entityStorage
->create(
Expand Down Expand Up @@ -380,4 +387,21 @@ public function getDefaultModerationState() {
->getConfiguration()['default_moderation_state'];
}

/**
* Unwraps metadata.
*
* @param string $jsonString
* Metadata.
*
* @return string
* Unwrapped metadata.
*/
public static function unwrapMetadata(string $jsonString) {
$metadata = json_decode($jsonString, TRUE);
if (is_array($metadata) && count($metadata) == 2 && isset($metadata['identifier']) && isset($metadata['data'])) {
return json_encode($metadata['data']);
}
return $jsonString;
}

}
1 change: 1 addition & 0 deletions modules/metastore/tests/src/SchemaRetrieverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function testGetAllIds() {
'theme.ui',
'keyword',
'keyword.ui',
'legacy'
];
$this->assertEquals($expected, $ids);
}
Expand Down
Loading