public function __destruct() {
        // because it is executed in destructor we should not allow exceptions to reach PHP script execution engine
        // otherwise execution of the script will halt
        try {
            $this->flush();
        }
        catch (Exception $e) {
            LogHelper::log_error($e);
        }

        parent::__destruct();
    }
/**
 * @param $dashboardNode
 * @return array
 */
function gd_dashboard_build_page ( $dashboardNode ) {
    $event = new DefaultEvent();
    gd_datasource_set_active(get_node_field_value($dashboardNode,'field_dashboard_datasource'));

    ob_start();

    /**
     * Build current dashboard config
     */
    $DashboardConfig = new GD_DashboardConfig($dashboardNode,$_GET);
    drupal_add_library('gd_dashboard', 'GD_Dashboard_View');

    print '<div id="dashboard-view" class="gd-container">';


    // dashboard view
    echo '<div class="row">';

    echo '  <div class="col-md-6">';
    echo '    <h2>'.$dashboardNode->title.'</h2>';
    if ( get_node_field_value($dashboardNode, 'field_dashboard_desc') ) {
        echo '<p>'.get_node_field_value($dashboardNode, 'field_dashboard_desc').'</p>';
    }
    echo '  </div>';
    echo '  <div class="col-md-6">';

    echo '<div class="pull-right">';
    // is not public
    if ( arg(0) != 'public' ) {
        $edit = false;
        if ( gd_account_user_is_admin() || gd_account_user_is_datasource_admin(null,$DashboardConfig->getDatasource()) ) {
            $edit = true;
        }

        if ($edit) {
            echo '<a role="button" type="button" id="editButton" tabindex="100" class="btn btn-default gd-dashboard-editbtn" href="/cp/dashboard/'.$dashboardNode->nid.'">Edit</a>';
        }
    }

    if ( gd_dashboard_get_setting('export') && $DashboardConfig->isExportable() ) {
        echo ' <button role="button" type="button" id="exportButton" tabindex="100" class="btn btn-default gd-dashboard-exportbtn" data-dashboard="'.$dashboardNode->nid.'">Export</button>';
    }
    echo '</div>';

    echo '  </div>';
    echo '</div>';

    $options = array();
    if ( $DashboardConfig->isPublic() && arg(0) == 'public' ) {
        $options['public'] = TRUE;
        drupal_add_http_header('Cache-Control','no-transform,public,max-age=3600,s-maxage=3600');
        drupal_add_http_header('Expires',gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
    }
    $configView = new GD_DashboardView($DashboardConfig);
    print $configView->getView($options);

    print '</div>';

    $DashboardConfig->attachRequiredLibs(); // must be called after building view, or libs won't be set yet

    $page = array(
        '#show_messages' => false,
        '#theme' => 'page',
        '#type' => 'page',
        'content' => array(
            'system_main' => array(
                '#markup' => ob_get_clean()
            )
        ),
        'post_header' => array(
            '#markup' => ''
        ),
        'pre_content' => array(
            '#markup' => ''
        )
    );

    if (isset($dashboardNode->nid)) {
        $event->type = 1; // see gd_health_monitoring_database_install() for more details
        $event->owner = $dashboardNode->nid;

        EventRecorderFactory::getInstance()->record($event);
    }

    return $page;
}
    protected function createReport ( $report ) {
        $node = new stdClass();
        $node->type = NODE_TYPE_REPORT;
        $node->language = LANGUAGE_NONE;
        $node->status = NODE_PUBLISHED;
        node_object_prepare($node);

        $node->originalNid = $report->id;
        $node->title = $report->title;

        $node->field_report_uuid[$node->language][0]['value'] = $report->uuid;
        $node->field_report_datasource[$node->language][0]['value'] = $this->datasourceName;

        if ( !empty($report->description) ) {
            $node->field_report_desc[$node->language][0]['value'] = $report->description;
        }

        if ( !empty($report->custom_view) ) {
            $node->field_report_custom_view[$node->language][0]['value'] = $report->custom_view;
        }

        $node->field_report_tags[$node->language] = array();
        if (!empty($report->tags)) {
            foreach ($report->tags as $tid) {
                $node->field_report_tags[$node->language][] = array('tid' => $tid);
            }
        }

        // update dataset references
        $node->field_report_dataset_sysnames[$node->language] = array();
        $metamodel = data_controller_get_metamodel();
        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);
            }
            $node->field_report_dataset_sysnames[$node->language][] = array('value'=>$dataset->name);
        }

        // update dataset references
        $this->processConfig($report->config);
        $node->field_report_conf[$node->language][0]['value'] = json_encode($report->config);

        gd_report_save($node);

        if (isset($node->nid)) {
            $event = new DefaultEvent();
            $event->type = 100; // see gd_health_monitoring_database_install() for more details
            $event->owner = $node->nid;
            EventRecorderFactory::getInstance()->record($event);
        }

        return $node;
    }
    public function getData ( $force = false, $all = false ) {

        if ( (!isset($this->data) || $force) && !empty($this->datasets) ) {

            $columns = $this->columns;
            if ( !empty($this->options['visual']['useColumnDataForColor']) && $this->hasColumn($this->options['visual']['useColumnDataForColor']) ) {
                $columns[] = $this->options['visual']['useColumnDataForColor'];
            }

            $limit = $all ? NULL : $this->getQueryLimit();

            $event = new DefaultEvent();
            $event->owner = $this->getId();
            $offset = $this->offset;
            $sort = $this->sort;
            if ($this->options['config']['chartType'] == 'pivot_table') {
                $offset = 0;
                $limit = null;
                $sort = NULL;
            }

            $this->data = gd_data_controller_query_data_by_ui_metadata($this->datasets[0], $columns, $this->getQueryFilters(), $sort, $offset, $limit, $this->getQueryFormulas());
            $this->count = NULL;
            if (isset($event->owner)) {
                $event->type = 10; // see gd_health_monitoring_database_install() for more details
                EventRecorderFactory::getInstance()->record($event);
            }

            if ( $this->isDataTruncated && $this->getDisplayType() !== 'table' ) {
                drupal_add_http_header('gd_warnings', 'Result truncated to maximum '.$this->getChartMaxLimit().' records.', false);
            }
        }

        return $this->data;
    }
    protected function createDashboard ( $dashboard ) {
        $node = new stdClass();
        $node->type = NODE_TYPE_DASHBOARD;
        $node->language = LANGUAGE_NONE;
        $node->status = NODE_PUBLISHED;
        node_object_prepare($node);

        $node->originalNid = $dashboard->id;
        $node->title = $dashboard->title;
        $node->field_dashboard_datasource[$node->language][0]['value'] = $this->datasourceName;

        if ( !empty($dashboard->uuid) ) {
            $node->field_dashboard_uuid[$node->language][0]['value'] = $dashboard->uuid;
        }

        if ( !empty($dashboard->alias) ) {
            for($i = 0; $i < count($dashboard->alias); $i++) {
                $node->field_dashboard_alias[$node->language][] = array('value'=>$dashboard->alias[$i]);
            }
        }

        if ( !empty($dashboard->tags) ) {
            for($i = 0; $i < count($dashboard->tags); $i++) {
                $node->field_dashboard_tags[$node->language][] = array('tid'=>$dashboard->tags[$i]);
            }
        }

        if ( !empty($dashboard->description) ) {
            $node->field_dashboard_desc[$node->language][0]['value'] = $dashboard->description;
        }

        if ( !empty($dashboard->custom_view) ) {
            $node->field_dashboard_custom_view[$node->language][0]['value'] = $dashboard->custom_view;
        }

        // public dashboard flag
        if ( isset($dashboard->public) ) {
            $node->field_dashboard_public[$node->language][0]['value'] = $dashboard->public;
        }

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

        // update dataset references
        $this->processConfigReports($dashboard->config);
        $node->field_dashboard_config[$node->language][0]['value'] = json_encode($dashboard->config);

        gd_dashboard_save($node);

        if (isset($node->nid)) {
            $event = new DefaultEvent();
            $event->type = 100; // see gd_health_monitoring_database_install() for more details
            $event->owner = $node->nid;
            EventRecorderFactory::getInstance()->record($event);
        }

        return $node;
    }