/**
     * Find existing reference via datasets
     *
     * @param $reference
     * @return null
     * @throws Exception
     */
    protected function findExistingReference ( $reference ) {

        $referenceCandidateNodes = (array) gd_reference_get_references(LOAD_ENTITY);

        // find references with exact same reference points
        $matchedReferences = array();
        foreach ( $referenceCandidateNodes as $referenceCandidateNode ) {
            $match = false;
            // don't go any further if reference point counts don't match
            if ( count((array) get_node_field_node_ref($referenceCandidateNode,'field_reference_point',null)) == count($reference->referencePoints) ) {
                continue;
            }
            // must match all reference points
            foreach ( (array) get_node_field_node_ref($referenceCandidateNode,'field_reference_point',null) as $referencePointNid ) {
                $referencePointNode = node_load($referencePointNid);
                foreach ( $reference->referencePoints as $referencePoint ) {
                    if ( $referencePoint->dataset == get_node_field_value($referencePointNode,'field_ref_point_dataset_sysname') ) {
                        $match = true;
                    } else {
                        $match = false;
                    }
                }
            }

            if ( $match ) {
                $matchedReferences[] = $referenceCandidateNode;
            }
        }

        if ( count($matchedReferences) == 1 ) {
            return $matchedReferences[0];
        } else if ( count($matchedReferences) > 1 ) {
            throw new Exception('More than one identical reference found.');
        } else {
            return null;
        }

    }
    public function finalize(AbstractMetaModel $metamodel) {
        parent::finalize($metamodel);

        $loaderName = $this->getName();

        // initializing all cubes. We separated registration from initialization to support inter-cube references
        $loadedDatasets = NULL;
        foreach ($metamodel->datasets as $dataset) {
            // working only with datasets which are created by this loader
            if ($dataset->loaderName != $loaderName) {
                continue;
            }

            StarSchemaCubeMetaData::registerFromDataset($metamodel, $dataset);

            $loadedDatasets[$dataset->nid] = $dataset;
        }

        if (isset($loadedDatasets)) {
            $dataset_nids = array_keys($loadedDatasets);

            // loading measures
            $measureNodes = gd_measure_get_measures_4_dataset($dataset_nids, LOAD_ENTITY);
            // grouping measures in context of dataset
            $datasetsMeasureNodes = $this->groupNodesByDataset($measureNodes, 'field_measure_dataset');

            // creating cubes for all datasets
            foreach ($loadedDatasets as $dataset) {
                // preparing corresponding cube
                $cube = StarSchemaCubeMetaData::initializeFromDataset($metamodel, $dataset);
                // assigning a loader which created the cube
                $cube->factsDataset->loaderName = $loaderName;
                $cube->loaderName = $loaderName;
                // preparing additional measures
                $datasetMeasureNodes = isset($datasetsMeasureNodes[$dataset->nid]) ? $datasetsMeasureNodes[$dataset->nid] : NULL;
                if (isset($datasetMeasureNodes)) {
                    foreach ($datasetMeasureNodes as $measureNode) {
                        $measure = $cube->registerMeasure(get_node_field_value($measureNode, 'field_measure_sysname', 0, 'value', TRUE));
                        $measure->publicName = $measureNode->title;
                        $measure->description = get_node_field_value($measureNode, 'field_measure_desc');
                        $measure->function = get_node_field_value($measureNode, 'field_measure_function', 0, 'value', TRUE);
                    }
                }
            }
        }

        // processing references
        $referenceNodes = gd_reference_get_references(LOAD_ENTITY);
        $referencePointNodes = isset($referenceNodes) ? gd_reference_get_reference_points(LOAD_ENTITY) : NULL;
        if (isset($referencePointNodes)) {
            foreach ($referenceNodes as $referenceNode) {
                GD_DatasetMetaModelLoaderHelper::prepareReference($metamodel, $referenceNode, $referencePointNodes);
            }
        }
    }