/**
  * 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();
 }
 /**
  * Formulář pro vytvoření nového formátu
  * @return Form
  */
 protected function createComponentNewFormatForm()
 {
     $form = new Form();
     $form->addHidden('datasource');
     $form->addHidden('column');
     $metaAttribute = $form->addHidden('metaAttribute');
     $form->addText('metaAttributeName', 'Meta-attribute:')->setAttribute('readonly', 'readonly')->setAttribute('class', 'normalWidth');
     $formatName = $form->addText('formatName', 'Format name:')->setRequired()->setAttribute('class', 'normalWidth');
     $formatName->addRule(Form::MIN_LENGTH, 'Min length of format name is %s characters!', 3);
     $formatName->addRule(function (TextInput $control) use($metaAttribute) {
         try {
             $format = $this->metaAttributesFacade->findFormatByName($metaAttribute->value, $control->value);
             ///XXX
             if ($format instanceof Format) {
                 return false;
             }
         } catch (\Exception $e) {
             /*chybu ignorujeme (nenalezený metaatribut je OK)*/
         }
         return true;
     }, 'Format with this name already exists!');
     $form->addCheckbox('formatShared', 'Create shared (standard) format');
     $form->addSelect('formatType', 'Values range:', array('interval' => 'Continuous values (interval)', 'values' => 'Distinct values (enumeration)'))->setAttribute('class', 'normalWidth')->setDefaultValue('values');
     $submit = $form->addSubmit('create', 'Create format');
     $submit->setValidationScope(array($formatName));
     $submit->onClick[] = function (SubmitButton $button) {
         $values = $button->form->values;
         try {
             $datasourceColumn = $this->datasourcesFacade->findDatasourceColumn($values->datasource, $values->column);
             $metaAttribute = $this->metaAttributesFacade->findMetaAttribute($values->metaAttribute);
             $datasource = $this->datasourcesFacade->findDatasource($values->datasource);
             $this->databasesFacade->openDatabase($datasource->getDbConnection());
             $datasourceColumnValuesStatistic = $this->databasesFacade->getColumnValuesStatistic($datasource->dbTable, $datasourceColumn->name);
             $format = $this->metaAttributesFacade->createFormatFromDatasourceColumn($metaAttribute, $values->formatName, $datasourceColumn, $datasourceColumnValuesStatistic, @$values->formatType, @$values->formatShared);
             $datasourceColumn->format = $format;
             $this->datasourcesFacade->saveDatasourceColumn($datasourceColumn);
         } catch (\Exception $e) {
             $this->flashMessage($this->translator->translate('Format creation failed.'));
         }
         $this->redirect('Close');
     };
     $storno = $form->addSubmit('storno', 'Storno');
     $storno->setValidationScope(array());
     $storno->onClick[] = function (SubmitButton $button) {
         $values = $button->form->values;
         $this->redirect('SelectFormat', array('datasource' => $values->datasource, 'column' => $values->column, 'metaAttribute' => $values->metaAttribute));
     };
     $form->onError[] = function (Form $form) {
         $values = $form->values;
         $this->handleNewFormat($values->datasource, $values->column, $values->metaAttribute);
     };
     return $form;
 }
 /**
  * Akce pro hromadné předzpracování většího množství atributů
  * @param int $miner
  * @param int[]|null $columns=null
  * @param string[]|null $columnNames=null
  * @throws BadRequestException
  * @throws ForbiddenRequestException
  */
 public function renderAddAttributes($miner, $columns = null, $columnNames = null)
 {
     /** @var Miner $miner */
     $miner = $this->findMinerWithCheckAccess($miner);
     $this->minersFacade->checkMinerMetasource($miner);
     $this->minersFacade->checkMinerState($miner, $this->getCurrentUser());
     $this->template->miner = $miner;
     $this->template->metasource = $miner->metasource;
     /** @var DatasourceColumn[] $datasourceColumns */
     $datasourceColumns = [];
     if (trim(@$columns, ' ,;') != '') {
         //zpracováváme sloupce dle jejich ID
         $columns = explode(',', str_replace(';', ',', $columns));
         if (!empty($columns)) {
             foreach ($columns as $column) {
                 $datasourceColumns[] = $this->datasourcesFacade->findDatasourceColumn($miner->datasource, $column);
             }
         }
     } elseif (trim(@$columnNames, ' ,;') != '') {
         //zpracováváme sloupce dle jejich jména
         $columnNames = explode(',', str_replace(';', ',', $columnNames));
         if (!empty($columnNames)) {
             foreach ($columnNames as $columnName) {
                 $datasourceColumns[] = $this->datasourcesFacade->findDatasourceColumnByName($miner->datasource, $columnName);
             }
         }
     }
     if (empty($datasourceColumns)) {
         throw new BadRequestException('No data columns found.');
     }
     $datasourceColumnsIds = [];
     $currentUser = $this->getCurrentUser();
     foreach ($datasourceColumns as $datasourceColumn) {
         $datasourceColumnsIds[] = $datasourceColumn->datasourceColumnId;
         //kontrola, jestli je pro každý sloupec definován formát
         if (empty($datasourceColumn->format)) {
             //inicializace formátu (přiřazení metaatributu)
             //TODO podpora automatického mapování
             $format = $this->metaAttributesFacade->simpleCreateMetaAttributeWithFormatFromDatasourceColumn($datasourceColumn, $currentUser);
             $datasourceColumn->format = $format;
             $this->datasourcesFacade->saveDatasourceColumn($datasourceColumn);
         }
     }
     $this->template->datasourceColumns = $datasourceColumns;
     $this->template->datasourceColumnsIds = implode(',', $datasourceColumnsIds);
     $this->template->miner = $miner;
     //TODO kontrola, jestli je možné nabídnout další typy preprocessingu...
 }