protected function prepareCubeRequestColumns(CubeQueryRequest $request, CubeMetaData $cube, array $columnNames)
 {
     $metamodel = data_controller_get_metamodel();
     foreach ($columnNames as $requestColumnIndex => $columnName) {
         list($elementName, $subElementName, $propertyName) = ParameterHelper::splitName($columnName);
         list($referencedDatasetName, $referencedElementName) = ReferencePathHelper::splitReference($elementName);
         // checking that referenced cube exists
         $referencedCube = isset($referencedDatasetName) ? $metamodel->getCubeByDatasetName($referencedDatasetName) : NULL;
         if (isset($subElementName)) {
             if (isset($referencedCube)) {
                 throw new IllegalArgumentException(t('Referenced dimensions are not supported'));
             }
             // checking the level exists
             $dimension = $cube->getDimension($elementName);
             $level = $dimension->getLevel($subElementName);
             // adding the level
             if (isset($propertyName)) {
                 $request->addDimensionLevelProperty($requestColumnIndex, $elementName, $subElementName, $propertyName);
             } else {
                 $request->addDimensionLevel($requestColumnIndex, $elementName, $subElementName);
             }
         } else {
             $selectedRequest = $request;
             if (isset($referencedCube)) {
                 // checking the measure exists in the referenced cube
                 $measure = $referencedCube->getMeasure($referencedElementName);
                 // preparing referenced request
                 $selectedRequest = $request->registerReferencedRequest($referencedCube->name);
             } else {
                 // checking the measure exists
                 $measure = $cube->getMeasure($referencedElementName);
             }
             // adding the measure
             $selectedRequest->addMeasure($requestColumnIndex, $referencedElementName);
         }
     }
 }
 protected function prepareReferencedCubeQueryStatement(AbstractSQLDataSourceQueryHandler $datasourceHandler, DataControllerCallContext $callcontext, Statement $combinedStatement, array $datasetMappedCubeRequests, ReferenceLink $link)
 {
     if (!isset($link->nestedLinks)) {
         return;
     }
     $metamodel = data_controller_get_metamodel();
     foreach ($link->nestedLinks as $referencedLink) {
         $referencedRequest = NULL;
         if (isset($datasetMappedCubeRequests[$referencedLink->dataset->name])) {
             $referencedRequest = clone $datasetMappedCubeRequests[$referencedLink->dataset->name];
         } else {
             // checking if there is corresponding cube for the referenced dataset
             $possibleReferencedCube = $metamodel->findCubeByDatasetName($referencedLink->dataset->name);
             if (isset($possibleReferencedCube)) {
                 $referencedRequest = new CubeQueryRequest($possibleReferencedCube->name);
                 $referencedRequest->referenced = TRUE;
                 $datasetMappedCubeRequests[$possibleReferencedCube->sourceDatasetName] = $referencedRequest;
             }
         }
         if (isset($referencedRequest)) {
             // preparing parent cube
             $parentRequest = $datasetMappedCubeRequests[$link->dataset->name];
             $parentCubeName = $parentRequest->getCubeName();
             $parentCube = $metamodel->getCube($parentCubeName);
             // preparing referenced cube
             $referencedCubeName = $referencedRequest->getCubeName();
             $referencedCube = $metamodel->getCube($referencedCubeName);
             // adding required dimensions
             $joinConditions = NULL;
             foreach ($referencedLink->parentColumnNames as $columnIndex => $parentColumnName) {
                 // looking for a dimension in parent cube
                 list($parentDimension, ) = $parentCube->getDimensionAndLevelIndexBySourceColumnName($parentColumnName);
                 // looking for a dimension in referenced cube
                 $referencedColumnName = $referencedLink->columnNames[$columnIndex];
                 list($referencedDimension, ) = $referencedCube->getDimensionAndLevelIndexBySourceColumnName($referencedColumnName);
                 // checking if this dimension is part of query portion of parent request
                 $parentRequestDimensionQuery = $parentRequest->findDimensionQuery($parentDimension->name);
                 if (isset($parentRequestDimensionQuery)) {
                     // preparing level used in this query
                     $parentRequestDimensionLevelIndex4Query = $parentDimension->getLevelIndex($parentRequestDimensionQuery->levelName);
                     // preparing level to be used for the query in referenced request (using the same level index as for parent dimension)
                     $referencedDimensionLevel4Query = $referencedDimension->levels[$parentRequestDimensionLevelIndex4Query];
                     // copying the query request to referenced cube
                     $referencedRequestDimensionQuery = new __CubeQueryRequest_DimensionQuery($referencedDimension->name, $referencedDimensionLevel4Query->name);
                     $referencedRequestDimensionQuery->values = $parentRequestDimensionQuery->values;
                     $referencedRequest->importDimensionQueryFrom($referencedRequestDimensionQuery);
                 }
                 // checking if there is a related query for parent column name
                 $parentRequestSourceDatasetQuery = $parentRequest->findSourceDatasetPropertyQuery($parentColumnName);
                 if (isset($parentRequestSourceDatasetQuery)) {
                     // copying the query request to referenced cube
                     $referencedRequest->addSourceDatasetPropertyQueryValues($referencedColumnName, $parentRequestSourceDatasetQuery->values);
                 }
                 // checking if this dimension is part of parent request
                 $parentRequestDimension = $parentRequest->findDimension($parentDimension->name);
                 if (!isset($parentRequestDimension)) {
                     // because this dimension is not in list of returned columns we should not use it to link with referenced cube
                     continue;
                 }
                 $selectedParentDimensionLevelIndex = $parentDimension->getLevelIndex($parentRequestDimension->levelName);
                 $selectedParentDimensionLevel = $parentDimension->levels[$selectedParentDimensionLevelIndex];
                 // preparing level from referenced cube (using the same level index as for parent dimension)
                 $selectedReferencedDimensionLevel = $referencedDimension->levels[$selectedParentDimensionLevelIndex];
                 $referencedRequest->addDimensionLevel(NULL, $referencedDimension->name, $selectedReferencedDimensionLevel->name);
                 $parentDatabaseColumnName = ParameterHelper::assembleDatabaseColumnName($datasourceHandler->getMaximumEntityNameLength(), $parentRequest->referenced ? ReferencePathHelper::assembleReference($parentCube->sourceDatasetName, $parentDimension->name) : $parentDimension->name, $selectedParentDimensionLevel->name);
                 $referencedDatabaseColumnName = ParameterHelper::assembleDatabaseColumnName($datasourceHandler->getMaximumEntityNameLength(), ReferencePathHelper::assembleReference($referencedCube->sourceDatasetName, $referencedDimension->name), $selectedReferencedDimensionLevel->name);
                 $joinConditions[] = new JoinConditionSection($referencedDatabaseColumnName, new TableColumnConditionSectionValue(self::$TABLE_ALIAS__REFERENCED . $link->linkId, $parentDatabaseColumnName));
             }
             if (!isset($joinConditions)) {
                 throw new IllegalArgumentException(t("There is no common columns to join '@datasetNameA' and '@datasetNameB' datasets", array('@datasetNameA' => $parentCube->publicName, '@datasetNameB' => $referencedCube->publicName)));
             }
             // preparing aggregation statement for referenced cube
             $referencedAggregationStatement = $this->prepareSelectedCubeQueryStatement($datasourceHandler, $callcontext, $referencedRequest);
             list($isSubqueryRequired, $assembledReferencedCubeSections) = $referencedAggregationStatement->prepareSections(NULL);
             $referencedCubeSubquerySection = new SubquerySection(Statement::assemble($isSubqueryRequired, NULL, $assembledReferencedCubeSections, Statement::$INDENT_LEFT_OUTER_JOIN_SUBQUERY, FALSE), self::$TABLE_ALIAS__REFERENCED . $referencedLink->linkId);
             // preparing columns which are returned by referenced aggregation
             foreach ($referencedAggregationStatement->tables as $table) {
                 if (!isset($table->columns)) {
                     continue;
                 }
                 foreach ($table->columns as $column) {
                     if (!$column->visible) {
                         continue;
                     }
                     $referencedCubeSubquerySection->columns[] = new ColumnSection($column->alias);
                 }
             }
             // linking with parent cube
             foreach ($joinConditions as $joinCondition) {
                 // we do not need to return columns which are used to join with parent cube
                 $referencedCubeSubquerySection->getColumn($joinCondition->subjectColumnName)->visible = FALSE;
                 $referencedCubeSubquerySection->conditions[] = $joinCondition;
             }
             // adding to resulting statement
             $combinedStatement->tables[] = $referencedCubeSubquerySection;
             // applying referenced cubes measure conditions on resulting statement as well
             $measureQueries = $referencedRequest->findMeasureQueries();
             if (isset($measureQueries)) {
                 foreach ($measureQueries as $measureQuery) {
                     $measureName = ReferencePathHelper::assembleReference($referencedCube->sourceDatasetName, $measureQuery->measureName);
                     $measureDatabaseColumnName = ReferencePathHelper::assembleDatabaseColumnName($datasourceHandler->getMaximumEntityNameLength(), $measureName);
                     foreach ($measureQuery->values as $measureValue) {
                         $combinedStatement->conditions[] = new WhereConditionSection(self::$TABLE_ALIAS__REFERENCED . $referencedLink->linkId, $measureDatabaseColumnName, new ExactConditionSectionValue($datasourceHandler->formatOperatorValue($callcontext, $referencedRequest, $referencedCube->sourceDatasetName, NULL, $measureValue)));
                     }
                 }
             }
         } else {
             throw new UnsupportedOperationException(t('Cube joins using intermediate dataset is not supported yet'));
             // preparing statement for intermediate dataset
             $requiredColumnNames = $referencedLink->columnNames;
             $referencedIntermediateDatasetStatement = $datasourceHandler->prepareDatasetSourceStatement($callcontext, $referencedLink->dataset, $requiredColumnNames);
             // adding condition to join with parent statement
             $referencedIntermediateDatasetTableSection = $referencedIntermediateDatasetStatement->tables[0];
             foreach ($referencedLink->columnNames as $columnIndex => $referencedColumnName) {
                 $referencedDatabaseColumnName = $referencedColumnName;
                 $parentColumnName = $referencedLink->parentColumnNames[$columnIndex];
                 $parentDatabaseColumnName = $parentColumnName;
                 $referencedIntermediateDatasetTableSection->conditions[] = new JoinConditionSection($referencedDatabaseColumnName, new TableColumnConditionSectionValue(self::$TABLE_ALIAS__REFERENCED . $link->linkId, $parentDatabaseColumnName));
             }
             $combinedStatement->merge($referencedIntermediateDatasetStatement);
         }
         // recursively check nested levels
         $this->prepareReferencedCubeQueryStatement($datasourceHandler, $callcontext, $combinedStatement, $datasetMappedCubeRequests, $referencedLink);
     }
 }