public function prepareDimension(MetaModel $metamodel, DatasetMetaData $dataset, $columnName, CubeMetaData $cube)
 {
     $column = $dataset->getColumn($columnName);
     $sourceDatasetColumn = $cube->sourceDataset->getColumn($columnName);
     $dimension = $cube->getDimension($columnName);
     $this->prepareYearLevel($dimension, $column);
     // cube source dataset column contains a reference to year identifier
     $sourceDatasetColumn->initializeTypeFrom(Sequence::getSequenceColumnType());
     // adding a reference to date dataset
     $referenceName = DateDimensionDatasetNames::LEVEL_YEARS;
     $metamodel->registerSimpleReferencePoint($referenceName, DateDimensionDatasetNames::LEVEL_YEARS, 'year_id');
     $metamodel->registerSimpleReferencePoint($referenceName, $cube->sourceDatasetName, $columnName);
 }
    public function prepareDimension(MetaModel $metamodel, DatasetMetaData $dataset, $columnName, CubeMetaData $cube) {
        $logicalColumn = $dataset->getColumn($columnName);
        $column = $cube->factsDataset->getColumn($columnName);
        $dimension = $cube->getDimension($columnName);

        // preparing the dimension properties
        $dimension->attributeColumnName = $columnName;
        $dimension->setDatasetName(StarSchemaNamingConvention::getAttributeRelatedName($dataset->name, $columnName));

        // preparing dimension dataset
        $dimension->dataset = new DatasetMetaData();
        $dimension->dataset->name = $dimension->datasetName;
        $dimension->dataset->publicName = $dataset->publicName . " [$logicalColumn->publicName]";
        $dimension->dataset->description = t("Lookup table to store unique values from '@columnName' column", array('@columnName' => $logicalColumn->publicName));
        $dimension->dataset->datasourceName = $dataset->datasourceName;
        $dimension->dataset->source = StarSchemaNamingConvention::getAttributeRelatedName($dataset->source, $columnName);
        $dimension->dataset->markAsPrivate();
        // adding dimension dataset aliases
        if (isset($dataset->aliases)) {
            foreach ($dataset->aliases as $alias) {
                $dimension->dataset->aliases[] = StarSchemaNamingConvention::getAttributeRelatedName($alias, $columnName);
            }
        }

        // adding key column
        $keyColumn = $dimension->dataset->registerColumn($columnName);
        $keyColumn->publicName = $logicalColumn->publicName;
        $keyColumn->description = t("System generated ID to identify each unique value from '@columnName' column", array('@columnName' => $logicalColumn->publicName));
        $keyColumn->initializeTypeFrom(Sequence::getSequenceColumnType());
        $keyColumn->key = TRUE;
        $keyColumn->visible = FALSE;
        
        // adding 'value' column
        $valueColumn = $dimension->dataset->registerColumn('value');
        $valueColumn->publicName = $logicalColumn->publicName;
        $valueColumn->description = t("Actual value from '@columnName' column", array('@columnName' => $logicalColumn->publicName));
        $valueColumn->initializeTypeFrom($logicalColumn->type);

        // facts dataset column contains a reference to lookup
        $column->initializeTypeFrom(Sequence::getSequenceColumnType());
        $column->type->logicalApplicationType = StringDataTypeHandler::DATA_TYPE;

        // marking that the dimension dataset object contains complete meta data & registering it in meta model
        $dimension->dataset->markAsComplete();
        $metamodel->registerDataset($dimension->dataset);

        // adding a reference to the dimension dataset
        $referenceName = $dimension->datasetName;
        $metamodel->registerSimpleReferencePoint($referenceName, $dimension->datasetName, $columnName);
        $metamodel->registerSimpleReferencePoint($referenceName, $cube->factsDatasetName, $columnName);
    }
 protected function adjustReferencedDataType4Casting($datasetName, $columnName) {
     return Sequence::getSequenceColumnType()->applicationType;
 }
 protected function prepareColumnDatabaseType(DataSourceHandler $handler, ColumnMetaData $column)
 {
     if (isset($column->type->applicationType)) {
         $storageDataType = NULL;
         list($datasetName) = ReferencePathHelper::splitReference($column->type->applicationType);
         if (isset($datasetName)) {
             $storageDataType = Sequence::getSequenceColumnType()->applicationType;
         } else {
             $datatypeHandler = DataTypeFactory::getInstance()->getHandler($column->type->applicationType);
             $storageDataType = $datatypeHandler->getStorageDataType();
         }
         switch ($storageDataType) {
             case StringDataTypeHandler::$DATA_TYPE:
                 break;
             case IntegerDataTypeHandler::$DATA_TYPE:
                 $this->prepareInteger($column);
                 break;
             case NumberDataTypeHandler::$DATA_TYPE:
             case CurrencyDataTypeHandler::$DATA_TYPE:
             case PercentDataTypeHandler::$DATA_TYPE:
                 $this->prepareNumber($column);
                 break;
             case BooleanDataTypeHandler::$DATA_TYPE:
                 // calculating length of mapping of TRUE and FALSE values
                 $valueTrue = $handler->castValue(BooleanDataTypeHandler::$DATA_TYPE, TRUE);
                 $valueFalse = $handler->castValue(BooleanDataTypeHandler::$DATA_TYPE, FALSE);
                 // length of the field depends on length of the mappings
                 $lengthValueTrue = strlen($valueTrue);
                 $lengthValueFalse = strlen($valueFalse);
                 $length = MathHelper::max($lengthValueTrue, $lengthValueFalse);
                 // detecting type for each value and selecting primary type
                 $datatype = DataTypeFactory::getInstance()->selectDataType(array(DataTypeFactory::getInstance()->autoDetectDataType($valueTrue), DataTypeFactory::getInstance()->autoDetectDataType($valueFalse)));
                 // for numeric values we use integer storage type, for rest - string
                 if ($datatype === IntegerDataTypeHandler::$DATA_TYPE) {
                     $this->prepareInteger($column, $length);
                 } elseif ($lengthValueTrue === $lengthValueFalse) {
                     $this->prepareFixedLengthString($column, $length);
                 } else {
                     $this->prepareVariableLengthString($column, $length);
                 }
                 break;
             case DateTimeDataTypeHandler::$DATA_TYPE:
                 $this->prepareDate($column, TRUE, TRUE);
                 break;
             case DateDataTypeHandler::$DATA_TYPE:
                 $this->prepareDate($column);
                 break;
             case TimeDataTypeHandler::$DATA_TYPE:
                 $this->prepareDate($column, FALSE, TRUE);
                 break;
             default:
                 throw new UnsupportedOperationException(t("Unsupported data type for '@columnName' column: @columnType", array('@columnName' => $column->publicName, '@columnType' => $column->type->applicationType)));
         }
     }
     if (!isset($column->type->databaseType)) {
         $this->prepareVariableLengthString($column);
     }
     return $column->type->databaseType;
 }
    protected function generateAndStoreIdentifiers($lookupDatasetName, array $uniqueSetColumns, array $nonKeyColumns = NULL, $sequenceName, array &$lookupValues) {
        $dataManipulationController = data_controller_dml_get_instance();
        $metamodel = data_controller_get_metamodel();

        $lookupDataset = $metamodel->getDataset($lookupDatasetName);
        $identifierColumnName = $lookupDataset->getKeyColumn()->name;

        $lookupCacheKey = $this->prepareLookupCacheKey($lookupDataset->name);

        // preparing insert operation meta data
        $recordsHolder = new IndexedRecordsHolder();
        $recordsHolder->recordMetaData = new RecordMetaData();
        // registering 'identifier' column
        $column = $recordsHolder->recordMetaData->registerColumn($identifierColumnName);
        $column->initializeTypeFrom(Sequence::getSequenceColumnType());
        // registering columns which represent lookup key
        foreach ($uniqueSetColumns as $uniqueSetColumn) {
            $column = $recordsHolder->recordMetaData->registerColumn($uniqueSetColumn->name);
            $column->initializeTypeFrom($uniqueSetColumn->type);
            $column->key = TRUE;
        }
        // registering non key columns
        if (isset($nonKeyColumns)) {
            foreach ($nonKeyColumns as $nonKeyColumn) {
                $column = $recordsHolder->recordMetaData->registerColumn($nonKeyColumn->name);
                $column->initializeTypeFrom($nonKeyColumn->type);
            }
        }

        // generating identifiers for source table
        $identifiers = Sequence::getNextSequenceValues($sequenceName, count($lookupValues));

        // preparing records for insert operation
        foreach ($lookupValues as $lookupKey => $lookupValue) {
            $identifier = array_pop($identifiers);

            $record = array($identifier);

            foreach ($uniqueSetColumns as $uniqueSetColumn) {
                $columnName = $uniqueSetColumn->name;
                $record[] = $lookupValue->$columnName;
            }

            if (isset($nonKeyColumns)) {
                $lookupValue = $lookupValues[$lookupKey];
                foreach ($nonKeyColumns as $nonKeyColumn) {
                    $record[] = $lookupValue->{$nonKeyColumn->name};
                }
            }

            $recordInstance = $recordsHolder->initiateRecordInstance();
            $recordInstance->initializeFrom($record);
            $recordsHolder->registerRecordInstance($recordInstance);

            $lookupValue->identifier = $identifier;
            $this->cachedIdentifiers[$lookupCacheKey][$lookupKey] = $identifier;
        }

        $this->freeSpaceInIdentifierCache($lookupDataset->name);

        // storing 'missing' records
        $dataManipulationController->insertDatasetRecordBatch($lookupDataset->name, $recordsHolder);
    }
    public function prepareDimension(MetaModel $metamodel, DatasetMetaData $dataset, $columnName, CubeMetaData $cube) {
        $column = $cube->factsDataset->getColumn($columnName);
        $dimension = $cube->getDimension($columnName);

        $referencedCube = $metamodel->findCube($this->datasetName);
        // TODO DHS Management Cube (we do not have cubes for lookups)
        $referencedDatasetName = isset($referencedCube) ? $referencedCube->factsDatasetName : $this->datasetName;

        // preparing dimension properties
        $dimension->attributeColumnName = $columnName;

        // preparing dimension dataset
        $dimension->setDatasetName($this->datasetName);
        $dimension->dataset = $metamodel->getDataset($dimension->datasetName);

        // facts dataset column contains a reference to lookup
        // TODO DHS Management Cube (column type has been already prepared)
        if (!isset($column->type->applicationType)) {
            $column->initializeTypeFrom(Sequence::getSequenceColumnType());
        }

        // adding a reference to dimension dataset
        $referenceName = $referencedDatasetName;
        $metamodel->registerSimpleReferencePoint($referenceName, $referencedDatasetName, NULL);
        $metamodel->registerSimpleReferencePoint($referenceName, $cube->factsDatasetName, $columnName);
        // ... to support retrieving properties of the dimension dataset
        $metamodel->registerSimpleReferencePoint($referenceName, $dimension->datasetName, $dimension->dataset->getKeyColumn()->name);
    }