public function import(Import\ImportStream $stream, Import\ImportContext $context) {
     $operation = $context->get('operation');
     switch ($operation) {
         case 'create' :
             $this->create($stream,$context);
             break;
         case 'update' :
             $this->update($stream,$context);
             break;
         default:
             throw new UnsupportedImportOperationException('Unsupported import operation "'.$operation.'" requested.');
             break;
     }
 }
    protected function update(Import\ImportStream $stream, Import\ImportContext $context) {

        $this->datasourceName = $context->get('datasourceName');

        $reports = $stream->get('reports');
        if (empty($reports)) {
            return;
        }

        $metamodel = data_controller_get_metamodel();
        $this->datasets = $metamodel->datasets;

        foreach ( $reports as $reportKey => $report ) {
            $report = (object) $report;
            $existingReport = gd_report_get_by_uuid($report->uuid,$this->datasourceName);
            if ( !$existingReport ) {
                // create
                $node = $this->createReport($report);
                if ( !empty($node->nid ) ) {
                    $reports[$reportKey] = $node;
                } else {
                    throw new Exception('Report node creation failed');
                }
            } else {
                $existingReport->title = $report->title;
                $existingReport->field_report_desc[$existingReport->language][0]['value'] = $report->description;
                $existingReport->field_report_datasource[$existingReport->language][0]['value'] = $this->datasourceName;
                $existingReport->field_report_custom_view[$existingReport->language][0]['value'] = $report->custom_view;
                $existingReport->field_report_tags[$existingReport->language] = array();
                if (!empty($report->tags)) {
                    foreach ($report->tags as $tid) {
                        $existingReport->field_report_tags[$existingReport->language][] = array('tid' => $tid);
                    }
                }

                // update dataset references
                $existingReport->field_report_dataset_sysnames[$existingReport->language] = array();
                foreach ( $report->datasets as $datasetIdentifier ) {
                    $dataset = GD_DatasetMetaModelLoaderHelper::findDatasetByUUID($this->datasets,$datasetIdentifier);
                    if (!isset($dataset)) {
                        $datasetName = NameSpaceHelper::addNameSpace(gd_datasource_get_active(), $datasetIdentifier);
                        $dataset = $metamodel->getDataset($datasetName);
                    }
                    $existingReport->field_report_dataset_sysnames[$existingReport->language][] = array('value'=>$dataset->name);
                }

                $this->processConfig($report->config);
                $existingReport->field_report_conf[$existingReport->language][0]['value'] = json_encode($report->config);

                gd_report_save($existingReport);

                $event = new DefaultEvent();
                $event->type = 101; // see gd_health_monitoring_database_install() for more details
                $event->owner = $existingReport->nid;
                EventRecorderFactory::getInstance()->record($event);

                $reports[$reportKey] = $existingReport;
            }
        }

        $stream->set('reports',$reports);
    }
    protected function update(Import\ImportStream $stream, Import\ImportContext $context) {
        $datasets = $stream->get('datasets');
        if (empty($datasets)) {
            return;
        }

        gd_datasource_set_active($context->get('datasourceName'));
        $metamodel = data_controller_get_metamodel();

        $environment_metamodel = data_controller_get_environment_metamodel();
        $datasource = $environment_metamodel->getDataSource($context->get('datasourceName'));

        foreach ( $datasets as $dataset ) {
            $existingDataset = GD_DatasetMetaModelLoaderHelper::findDatasetByUUID($metamodel->datasets, $dataset->uuid);
            if ( !$existingDataset ) {

                // map for re-linking dependent objects
                $dataset->originalName = $dataset->name;

                // create new name
                $newDatasetName = GD_NamingConvention::generateDatasetName();
                $dataset->name = $newDatasetName;
                $dataset->source = $newDatasetName;
                $dataset->datasourceName = $datasource->name;
            } else {
                // map for re-linking dependent objects
                $dataset->originalName = $dataset->name;

                $dataset->name = $existingDataset->name;
                $dataset->source = $existingDataset->source;
                $dataset->datasourceName = $existingDataset->datasourceName;
            }
        }

        // prepare dataset columns for import
        $this->prepareColumns($datasets);

        // prepares the metadata object, has to be of type RecordMetaData
        foreach ( $datasets as $key => $dataset ) {
            $metadata = new DatasetMetaData();
            $metadata->initializeFrom($dataset);
            $datasets[$key] = $metadata;
        }

        // ensure datasets are created in order to satisfy dependencies
        usort($datasets, array(new ReferencedDatasetComparator(), 'compare'));

        foreach ( $datasets as $dataset ) {
            $existingDataset = GD_DatasetMetaModelLoaderHelper::findDatasetByUUID($metamodel->datasets, $dataset->uuid);
            if ($existingDataset) {
                gd_data_controller_ddl_modify_dataset($dataset);
            } else {
                MetaModelFactory::getInstance()->startGlobalModification();
                try {
                    $transaction = db_transaction();
                    try {
                        gd_data_controller_ddl_create_dataset($dataset);
                    } catch (Exception $e) {
                        $transaction->rollback();
                        throw $e;
                    }
                } catch (Exception $e) {
                    MetaModelFactory::getInstance()->finishGlobalModification(false);
                    throw $e;
                }
                MetaModelFactory::getInstance()->finishGlobalModification(true);
            }
        }

        $stream->set('datasets',$datasets);
    }
    protected function update(Import\ImportStream $stream, Import\ImportContext $context) {
        $dashboards = $stream->get('dashboards');
        if (empty($dashboards)) {
            return;
        }

        $this->datasourceName = $context->get('datasourceName');

        $this->reports = gd_report_findall_by_datasource(LOAD_ENTITY,$this->datasourceName);

        $metamodel = data_controller_get_metamodel();
        $this->datasets = $metamodel->datasets;

        foreach ( $dashboards as $dashboardKey => $dashboard ) {
            $dashboard = (object) $dashboard;
            $existingDashboard = gd_dashboard_get_by_uuid($dashboard->uuid,$this->datasourceName);
            if ( !$existingDashboard ) {
                // create
                $node = $this->createDashboard($dashboard);
                if ( !empty($node->nid ) ) {
                    $dashboards[$dashboardKey] = $node;
                } else {
                    throw new Exception('Dashboard node creation failed');
                }
            } else {
                $existingDashboard->title = $dashboard->title;
                $existingDashboard->field_dashboard_desc[$existingDashboard->language][0]['value'] = $dashboard->description;
                $existingDashboard->field_dashboard_datasource[$existingDashboard->language][0]['value'] = $this->datasourceName;
                $existingDashboard->field_dashboard_custom_view[$existingDashboard->language][0]['value'] = $dashboard->custom_view;
                $existingDashboard->field_dashboard_public[$existingDashboard->language][0]['value'] = (int) $dashboard->public;

                // update report references
                $existingDashboard->field_dashboard_reports[$existingDashboard->language] = array();
                foreach ( $dashboard->reports as $reportUuid ) {
                    $reportNode = gd_report_get_by_uuid($reportUuid,$this->datasourceName);
                    $existingDashboard->field_dashboard_reports[$existingDashboard->language][] = array('nid'=>$reportNode->nid);
                }

                $this->processConfigReports($dashboard->config);
                $existingDashboard->field_dashboard_config[$existingDashboard->language][0]['value'] = json_encode($dashboard->config);

                gd_dashboard_save($existingDashboard);

                $event = new DefaultEvent();
                $event->type = 101; // see gd_health_monitoring_database_install() for more details
                $event->owner = $existingDashboard->nid;
                EventRecorderFactory::getInstance()->record($event);

                $dashboards[$dashboardKey] = $existingDashboard;
            }
        }

        // update report/dashboard ids in config, for linking
        foreach ( $dashboards as $dashboardKey => $dashboard ) {
            $config = json_decode($dashboard->field_dashboard_config[$dashboard->language][0]['value']);

            $this->processConfigDashboards($config);

            $dashboards[$dashboardKey]->field_dashboard_config[$dashboards[$dashboardKey]->language][0]['value'] = json_encode($config);
            gd_dashboard_save($dashboards[$dashboardKey]);
        }
    }