5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
20 class Service implements ContainerInjectionInterface {
35 private $schemaRetriever;
42 public static function create(ContainerInterface $container) {
44 $container->get(
'dkan_schema.schema_retriever'),
45 $container->get(
'dkan_metastore.sae_factory')
53 $this->schemaRetriever = $schemaRetriever;
54 $this->saeFactory = $saeFactory;
64 $this->pluginManager = $pluginManager;
65 $this->plugins = $this->discover();
73 foreach ($this->schemaRetriever->getAllIds() as $id) {
74 $schema = $this->schemaRetriever->retrieve($id);
75 $schemas[$id] = json_decode($schema);
84 $schema = $this->schemaRetriever->retrieve($identifier);
85 $schema = json_decode($schema);
99 public function getAll($schema_id): array {
101 $datasets = $this->getEngine($schema_id)->get();
104 $unflattened = array_map(
105 function ($json_string) use ($schema_id) {
106 if (!empty($this->plugins)) {
107 $json_string = $this->modifyData($schema_id, $json_string);
109 return json_decode($json_string);
128 public function get($schema_id, $identifier):
string {
129 $data = $this->getEngine($schema_id)
131 if (!empty($this->plugins)) {
132 $data = $this->modifyData($schema_id, $data);
148 private function modifyData(
string $schema_id,
string $data) {
149 $dataObj = json_decode($data);
151 foreach ($this->plugins as $plugin) {
152 if ($plugin->requiresModification($schema_id, $dataObj)) {
153 $dataObj = $plugin->modify($schema_id, $dataObj);
157 return json_encode($dataObj);
174 if (function_exists(
'drupal_static')) {
178 $json = $this->getEngine($schema_id)
180 $data = json_decode($json);
182 $resources = $data->distribution;
198 public function post($schema_id,
string $data): string {
200 $decoded = json_decode($data, TRUE);
201 if (isset($decoded[
'identifier'])) {
202 $identifier = $decoded[
'identifier'];
203 if ($this->objectExists($schema_id, $identifier)) {
208 return $this->getEngine($schema_id)->post($data);
224 public function put($schema_id, $identifier,
string $data): array {
225 $obj = json_decode($data);
226 if (isset($obj->identifier) && $obj->identifier != $identifier) {
229 elseif ($this->objectExists($schema_id, $identifier) && $this->objectIsEquivalent($schema_id, $identifier, $data)) {
233 return $this->proceedWithPut($schema_id, $identifier, $data);
250 private function proceedWithPut($schema_id, $identifier,
string $data): array {
251 if ($this->objectExists($schema_id, $identifier)) {
252 $this->getEngine($schema_id)->put($identifier, $data);
253 return [
'identifier' => $identifier,
'new' => FALSE];
256 $this->getEngine($schema_id)->post($data);
257 return [
'identifier' => $identifier,
'new' => TRUE];
274 public function patch($schema_id, $identifier, $data) {
275 $engine = $this->getEngine($schema_id);
276 if ($this->objectExists($schema_id, $identifier)) {
277 $engine->patch($identifier, $data);
295 public function delete($schema_id, $identifier) {
296 $engine = $this->getEngine($schema_id);
298 $engine->delete($identifier);
311 $catalog->dataset = $this->
getAll(
'dataset');
319 private function objectExists($schemaId, $identifier) {
321 $this->getEngine($schemaId)->get($identifier);
324 catch (\Exception $e) {
346 private function objectIsEquivalent(
string $schema_id,
string $identifier,
string $new_metadata) {
347 $existing_metadata = $this->getEngine($schema_id)->get($identifier);
348 return json_decode($new_metadata) == json_decode($existing_metadata);
354 private function getEngine($schemaId) {
355 return $this->saeFactory->getInstance($schemaId);