* Import data into dataset from an internal or external file * * @NoAdminRequired * @param int $objectId * @param $path * @param bool $isDataset * @return array|false * @throws \OCP\DB\Exception */
(int $objectId, $path, bool $isDataset, $storageMapping = null)
| 663 | * @throws \OCP\DB\Exception |
| 664 | */ |
| 665 | public function importFile(int $objectId, $path, bool $isDataset, $storageMapping = null) |
| 666 | { |
| 667 | $datasetId = $this->getDatasetId($objectId, $isDataset); |
| 668 | if ($datasetId != '') { |
| 669 | $insert = $update = 0; |
| 670 | $reportMetadata = array(); |
| 671 | $reportMetadata['link'] = $path; |
| 672 | $reportMetadata['user_id'] = $this->userId; |
| 673 | $result = $this->DatasourceController->read(DatasourceController::DATASET_TYPE_LOCAL_CSV, $reportMetadata, false); |
| 674 | $dataset = $this->DatasetService->read((int)$datasetId); |
| 675 | if (is_array($dataset) && ($dataset['storageMode'] ?? 'legacy') === 'flexible_shared') { |
| 676 | try { |
| 677 | $header = is_array($result['header'] ?? null) ? $result['header'] : []; |
| 678 | $rows = is_array($result['data'] ?? null) ? $result['data'] : []; |
| 679 | if ($storageMapping === null) { |
| 680 | $columns = $this->FlexibleStorageService->getDescriptor((int)$datasetId)['columns']; |
| 681 | $storageMapping = $this->positionalImportMapping($columns, $header, $rows); |
| 682 | } |
| 683 | $flexibleResult = $this->FlexibleStorageService->executeMappedLoad( |
| 684 | (int)$datasetId, |
| 685 | $storageMapping, |
| 686 | $header, |
| 687 | $rows, |
| 688 | false |
| 689 | ); |
| 690 | $this->DatasetService->provider((int)$datasetId); |
| 691 | return $flexibleResult; |
| 692 | } catch (FlexibleStorageException $e) { |
| 693 | return ['insert' => 0, 'update' => 0, 'delete' => 0, 'error' => 1, 'validationErrors' => [$e->toResponse()['error']]]; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | if ($result['error'] === 0) { |
| 698 | foreach ($result['data'] as &$row) { |
| 699 | $action = $this->StorageService->update($datasetId, $row[0], $row[1], $row[2]); |
| 700 | $insert = $insert + $action['insert']; |
| 701 | $update = $update + $action['update']; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | $result = [ |
| 706 | 'insert' => $insert, |
| 707 | 'update' => $update, |
| 708 | 'error' => $result['error'], |
| 709 | ]; |
| 710 | |
| 711 | // Update the Context Chat backend |
| 712 | $this->DatasetService->provider($datasetId); |
| 713 | |
| 714 | //if ($result['error'] === 0) $this->ActivityManager->triggerEvent($dataset, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD_IMPORT); |
| 715 | return $result; |
| 716 | } else { |
| 717 | return false; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | private function getDatasetId($objectId, bool $isDataset) |
| 722 | { |
no test coverage detected