5 use Symfony\Component\DependencyInjection\ContainerInterface;
6 use Symfony\Component\HttpFoundation\RequestStack;
7 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
8 use Drupal\dkan_common\JsonResponseTrait;
18 use JsonResponseTrait;
32 private $requestStack;
39 $this->requestStack = $requestStack;
45 public static function create(ContainerInterface $container) {
47 $requestStack = $container->get(
'request_stack');
62 $storage = $this->datastoreService->getStorage($identifier);
64 $data = $storage->getSummary();
65 return $this->getResponse($data);
67 throw new \Exception(
"no storage");
69 catch (\Exception $e) {
70 $exception = new \Exception(
"A datastore for resource {$identifier} does not exist.");
71 return $this->getResponseFromException($exception, 404);
78 public function import() {
80 $payloadJson = $this->requestStack->getCurrentRequest()->getContent();
81 $payload = json_decode($payloadJson);
83 if (isset($payload->resource_ids)) {
84 return $this->importMultiple($payload->resource_ids);
87 if (!isset($payload->resource_id)) {
88 return $this->getResponseFromException(
new \Exception(
"Invalid payload."));
92 $results = $this->datastoreService->import($payload->resource_id, FALSE);
93 return $this->getResponse($results);
95 catch (\Exception $e) {
96 return $this->getResponseFromException($e);
103 private function importMultiple(array $resourceIds) {
106 foreach ($resourceIds as $identifier) {
108 $results = $this->datastoreService->import($identifier, TRUE);
109 $responses[$identifier] = $results;
111 catch (\Exception $e) {
112 $responses[$identifier] = $e->getMessage();
116 return $this->getResponse($responses);
125 public function delete($identifier) {
127 $this->datastoreService->drop($identifier);
128 return $this->getResponse(
130 "identifier" => $identifier,
131 "message" =>
"The datastore for resource {$identifier} was succesfully dropped.",
135 catch (\Exception $e) {
136 return $this->getResponseFromException($e);
147 $payloadJson = $this->requestStack->getCurrentRequest()->getContent();
148 $payload = json_decode($payloadJson);
150 if (!isset($payload->resource_ids)) {
151 return $this->getResponseFromException(
new \Exception(
"Invalid payload."));
154 $identifiers = $payload->resource_ids;
157 foreach ($identifiers as $identifier) {
158 $responses[$identifier] = json_decode($this->
delete($identifier)->getContent());
161 return $this->getResponse($responses);
172 $data = $this->datastoreService->list();
173 return $this->getResponse($data);
175 catch (\Exception $e) {
176 return $this->getResponseFromException(
177 new \Exception(
"No importer data was returned. {$e->getMessage()}"),