/**
  * Akce pro vytvoření nového atributu
  * @SWG\Post(
  *   tags={"Attributes"},
  *   path="/attributes",
  *   summary="Create new attribute using defined preprocessing",
  *   consumes={"application/json","application/xml"},
  *   produces={"application/json","application/xml"},
  *   security={{"apiKey":{}},{"apiKeyHeader":{}}},
  *   @SWG\Parameter(
  *     description="New attribute",
  *     name="body",
  *     required=true,
  *     @SWG\Schema(ref="#/definitions/NewAttributeInput"),
  *     in="body"
  *   ),
  *   @SWG\Response(
  *     response=201,
  *     description="Attribute created",
  *     @SWG\Schema(
  *       ref="#/definitions/AttributeResponse"
  *     )
  *   ),
  *   @SWG\Response(
  *     response=400,
  *     description="Invalid API key supplied",
  *     @SWG\Schema(ref="#/definitions/StatusResponse")
  *   )
  * )
  * @throws \InvalidArgumentException
  */
 public function actionCreate()
 {
     /** @var array $inputData */
     $inputData = $this->input->getData();
     $miner = $this->findMinerWithCheckAccess(@$inputData['miner']);
     $this->minersFacade->checkMinerMetasource($miner);
     $currentUser = $this->getCurrentUser();
     //aktualizace informace o datových sloupcích
     $this->datasourcesFacade->updateDatasourceColumns($miner->datasource, $currentUser);
     try {
         if (!empty($inputData['column'])) {
             $datasourceColumn = $this->datasourcesFacade->findDatasourceColumn($miner->datasource, @$inputData['column']);
         } else {
             $datasourceColumn = $this->datasourcesFacade->findDatasourceColumnByName($miner->datasource, @$inputData['columnName']);
         }
     } catch (\Exception $e) {
         throw new InvalidArgumentException("Datasource columns was not found: " . @$inputData['columnName']);
     }
     //inicializace formátu
     $format = $datasourceColumn->format;
     if (!$format) {
         //TODO implementovat podporu automatického mapování
         $format = $this->metaAttributesFacade->simpleCreateMetaAttributeWithFormatFromDatasourceColumn($datasourceColumn, $currentUser);
         $datasourceColumn->format = $format;
         $this->datasourcesFacade->saveDatasourceColumn($datasourceColumn);
     }
     //vytvoření nového atributu
     $attribute = new Attribute();
     $attribute->metasource = $miner->metasource;
     $attribute->datasourceColumn = $datasourceColumn;
     $attribute->name = $this->minersFacade->prepareNewAttributeName($miner, $inputData['name']);
     $attribute->type = $attribute->datasourceColumn->type;
     if (@$inputData['specialPreprocessing'] == Preprocessing::SPECIALTYPE_EACHONE) {
         $preprocessing = $this->metaAttributesFacade->findPreprocessingEachOne($datasourceColumn->format);
         $attribute->preprocessing = $preprocessing;
     } else {
         throw new \BadMethodCallException('Selected preprocessing type is not supported.');
         //FIXME je nutné nalézt příslušný preprocessing...
     }
     $attribute->active = false;
     $this->metasourcesFacade->saveAttribute($attribute);
     //inicializace preprocessingu
     $metasourceTask = $this->metasourcesFacade->startAttributesPreprocessing($miner->metasource, [$attribute]);
     while ($metasourceTask && $metasourceTask->state != MetasourceTask::STATE_DONE) {
         $metasourceTask = $this->metasourcesFacade->preprocessAttributes($metasourceTask);
     }
     //smazání předzpracovávací úlohy
     $this->metasourcesFacade->deleteMetasourceTask($metasourceTask);
     $this->setXmlMapperElements('attribute');
     $this->resource = $attribute->getDataArr();
     $this->sendResource();
 }
 /**
  * Akce pro import CSV souboru (případně komprimovaného v ZIP archívu)
  * @SWG\Post(
  *   tags={"Datasources"},
  *   path="/datasources",
  *   summary="Create new datasource using uploaded file",
  *   consumes={"text/csv"},
  *   produces={"application/json","application/xml"},
  *   security={{"apiKey":{}},{"apiKeyHeader":{}}},
  *   @SWG\Parameter(
  *     name="name",
  *     description="Table name (if empty, will be auto-generated)",
  *     required=false,
  *     type="string",
  *     in="query"
  *   ),
  *   @SWG\Parameter(
  *     name="separator",
  *     description="Columns separator",
  *     required=true,
  *     type="string",
  *     in="query"
  *   ),
  *   @SWG\Parameter(
  *     name="encoding",
  *     description="File encoding",
  *     required=true,
  *     type="string",
  *     in="query",
  *     enum={"utf8","cp1250","iso-8859-1"}
  *   ),
  *   @SWG\Parameter(
  *     name="enclosure",
  *     description="Enclosure character",
  *     required=false,
  *     type="string",
  *     in="query"
  *   ),
  *   @SWG\Parameter(
  *     name="escape",
  *     description="Escape character",
  *     required=false,
  *     type="string",
  *     in="query"
  *   ),
  *   @SWG\Parameter(
  *     name="nullValue",
  *     description="Null value",
  *     required=false,
  *     type="string",
  *     in="query"
  *   ),
  *   @SWG\Parameter(
  *     name="type",
  *     description="Database type",
  *     required=true,
  *     type="string",
  *     enum={"limited","unlimited","mysql"},
  *     in="query"
  *   ),
  *   @SWG\Parameter(
  *     name="file",
  *     description="CSV file",
  *     required=true,
  *     type="file",
  *     in="formData"
  *   ),
  *   @SWG\Response(
  *     response=200,
  *     description="Datasource details",
  *     @SWG\Schema(
  *       ref="#/definitions/DatasourceWithColumnsResponse"
  *     )
  *   ),
  *   @SWG\Response(
  *     response=400,
  *     description="Invalid API key supplied",
  *     @SWG\Schema(ref="#/definitions/StatusResponse")
  *   )
  * )
  * @throws \InvalidArgumentException
  */
 public function actionCreate()
 {
     #region move uploaded file
     /** @var FileUpload $file */
     $file = $this->request->files['file'];
     //detekce typu souboru
     $fileType = $this->fileImportsFacade->detectFileType($file->getName());
     if ($fileType == FileImportsFacade::FILE_TYPE_UNKNOWN) {
         //jedná se o nepodporovaný typ souboru
         try {
             FileSystem::delete($this->fileImportsFacade->getTempFilename());
         } catch (\Exception $e) {
         }
         throw new \InvalidArgumentException('The uploaded file is not in supported format!');
     }
     //move file
     $filename = $this->fileImportsFacade->getTempFilename();
     $file->move($this->fileImportsFacade->getFilePath($filename));
     //pokus o automatickou extrakci souboru
     if ($fileType == FileImportsFacade::FILE_TYPE_ZIP) {
         $fileType = $this->fileImportsFacade->tryAutoUnzipFile($filename);
         if ($fileType != FileImportsFacade::FILE_TYPE_CSV) {
             try {
                 FileSystem::delete($this->fileImportsFacade->getFilePath($filename));
             } catch (\Exception $e) {
             }
             throw new \InvalidArgumentException('The uploaded ZIP file has to contain only one CSV file!');
         }
     }
     #endregion move uploaded file
     /** @var array $inputData */
     $inputData = $this->input->getData();
     //prepare default values
     if (empty($inputData['name'])) {
         $inputData['name'] = FileImportsFacade::sanitizeFileNameForImport($file->sanitizedName);
     } else {
         $inputData['name'] = FileImportsFacade::sanitizeFileNameForImport($inputData['name']);
     }
     if (empty($inputData['enclosure'])) {
         $inputData['enclosure'] = '"';
     }
     if (empty($inputData['escape'])) {
         $inputData['escape'] = '\\';
     }
     if (empty($inputData['nullValue'])) {
         $inputData['nullValue'] = '';
     }
     //upload data and prepare datasource
     $currentUser = $this->getCurrentUser();
     $dbDatasource = $this->fileImportsFacade->importCsvFile($filename, $inputData['type'], $currentUser, $inputData['name'], $inputData['encoding'], $inputData['separator'], $inputData['enclosure'], $inputData['escape'], $inputData['nullValue']);
     $datasource = $this->datasourcesFacade->prepareNewDatasourceFromDbDatasource($dbDatasource, $currentUser);
     $this->datasourcesFacade->saveDatasource($datasource);
     //aktualizace informace o datových sloupcích
     $this->datasourcesFacade->updateDatasourceColumns($datasource, $currentUser);
     //send response
     $this->actionRead($datasource->datasourceId);
 }