/**
  * @param Orchestration $entity
  * @param \Keboola\StorageApi\Client $storageApi
  * @return Orchestration
  */
 public function fillOrchestrationEntity(Orchestration $entity, Client $storageApi)
 {
     $oldTokenId = $entity->getTokenId();
     $oldName = $entity->getName();
     /** @var $entity Orchestration */
     $entity = parent::fillEntity($entity);
     $name = $entity->getName();
     if (empty($name)) {
         $entity->setName($oldName);
     }
     // token change
     if ($oldTokenId !== $entity->getTokenId()) {
         $token = $storageApi->getToken($entity->getTokenId());
         $token = new Token(new Client(array('token' => $token['token'], 'url' => $storageApi->getApiUrl())));
         $entity->setToken($token)->setTokenId($token->getId())->setTokenDesc($token->getDescription())->setTokenOwnerName($token->getOwnerName())->setProjectId($token->getProjectId());
     }
     // notifications
     $data = $this->getPostJsonData();
     if (!empty($data['notifications'])) {
         $data['notifications'] = array_map(function ($row) {
             $notification = new Notification();
             $notification->fromArray($row);
             return $notification;
         }, $data['notifications']);
         $entity->setNotifications($data['notifications']);
     }
     // tasks
     $data = $this->getPostJsonData();
     if (array_key_exists('tasks', $data)) {
         $entity->removeTasks();
         $data['tasks'] = array_map(function ($row) {
             $notification = new OrchestrationTask();
             $notification->fromArray($row);
             return $notification;
         }, $data['tasks']);
         foreach ($data['tasks'] as $task) {
             $entity->addTask($task);
         }
     }
     //@FIXME catch and throw exception
     return $entity;
 }
 /**
  * @param Orchestration $entity
  * @param \Keboola\StorageApi\Client $storageApi
  * @return Orchestration
  */
 public function fillOrchestrationEntity(Orchestration $entity, Client $storageApi)
 {
     /** @var $entity Orchestration */
     $entity = parent::fillEntity($entity);
     if (!$entity->getTokenId()) {
         $entity->setTokenId($storageApi->createToken('manage', sprintf('Orchestrator %s', $entity->getName()), null, true));
     }
     // fill token data
     $token = $storageApi->getToken($entity->getTokenId());
     $token = new Token(new Client(array('token' => $token['token'], 'url' => $storageApi->getApiUrl())));
     $entity->setToken($token)->setTokenId($token->getId())->setTokenDesc($token->getDescription())->setTokenOwnerName($token->getOwnerName())->setProjectId($token->getProjectId());
     // notifications
     $data = $this->getPostJsonData();
     if (!empty($data['notifications'])) {
         $data['notifications'] = array_map(function ($row) {
             $notification = new Notification();
             $notification->fromArray($row);
             return $notification;
         }, $data['notifications']);
         $entity->setNotifications($data['notifications']);
     }
     // tasks
     $data = $this->getPostJsonData();
     if (!empty($data['tasks'])) {
         $data['tasks'] = array_map(function ($row) {
             $notification = new OrchestrationTask();
             $notification->fromArray($row);
             return $notification;
         }, $data['tasks']);
         foreach ($data['tasks'] as $task) {
             $entity->addTask($task);
         }
     }
     //@FIXME catch and throw exception
     return $entity;
 }
 /**
  * Manually execute job
  *
  * @param Request $request
  * @return Response
  */
 public function postJobsAsyncAction(Request $request)
 {
     $orchestrationId = null;
     try {
         $form = $this->createForm(new OrchestrationRunAsyncType($this->dbOrchestrationManager, $this->token));
         $handler = new RestFormHandler($form, $request);
         if ($handler->process()) {
             $orchestrationId = OrchestrationOrConfigEntity::parseOrchestrationId($handler->getPost('config'));
         }
     } catch (HttpException $e) {
         $exception = new OrchestratorException($e->getStatusCode(), $e->getMessage());
         $exception->setExceptionCode('ORCHESTRATION_VALIDATION');
         throw $exception;
     }
     return $this->handlePostJobs($orchestrationId, $request, true);
 }