MCPcopy Create free account
hub / github.com/Rello/analytics / importFile

Method importFile

lib/Service/DataloadService.php:683–737  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

681 * @throws \OCP\DB\Exception
682 */
683 public function importFile(int $objectId, $path, bool $isDataset, $storageMapping = null)
684 {
685 $datasetId = $this->getDatasetId($objectId, $isDataset);
686 if ($datasetId != '') {
687 $insert = $update = 0;
688 $reportMetadata = array();
689 $reportMetadata['link'] = $path;
690 $reportMetadata['user_id'] = $this->userId;
691 $result = $this->DatasourceController->read(DatasourceController::DATASET_TYPE_LOCAL_CSV, $reportMetadata, false);
692 $dataset = $this->DatasetService->read((int)$datasetId);
693 if (is_array($dataset) && ($dataset['storageMode'] ?? 'legacy') === 'flexible_shared') {
694 try {
695 $header = is_array($result['header'] ?? null) ? $result['header'] : [];
696 $rows = is_array($result['data'] ?? null) ? $result['data'] : [];
697 if ($storageMapping === null) {
698 $columns = $this->FlexibleStorageService->getDescriptor((int)$datasetId)['columns'];
699 $storageMapping = $this->positionalImportMapping($columns, $header, $rows);
700 }
701 $flexibleResult = $this->FlexibleStorageService->executeMappedLoad(
702 (int)$datasetId,
703 $storageMapping,
704 $header,
705 $rows,
706 false
707 );
708 $this->DatasetService->provider((int)$datasetId);
709 return $flexibleResult;
710 } catch (FlexibleStorageException $e) {
711 return ['insert' => 0, 'update' => 0, 'delete' => 0, 'error' => 1, 'validationErrors' => [$e->toResponse()['error']]];
712 }
713 }
714
715 if ($result['error'] === 0) {
716 foreach ($result['data'] as &$row) {
717 $action = $this->StorageService->update($datasetId, $row[0], $row[1], $row[2]);
718 $insert = $insert + $action['insert'];
719 $update = $update + $action['update'];
720 }
721 }
722
723 $result = [
724 'insert' => $insert,
725 'update' => $update,
726 'error' => $result['error'],
727 ];
728
729 // Update the Context Chat backend
730 $this->DatasetService->provider($datasetId);
731
732 //if ($result['error'] === 0) $this->ActivityManager->triggerEvent($dataset, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD_IMPORT);
733 return $result;
734 } else {
735 return false;
736 }
737 }
738
739 private function getDatasetId($objectId, bool $isDataset)
740 {

Callers 1

onEventMethod · 0.45

Calls 8

getDatasetIdMethod · 0.95
getDescriptorMethod · 0.80
executeMappedLoadMethod · 0.80
toResponseMethod · 0.80
readMethod · 0.45
providerMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected