Working with Cube Data Source

Operating system requirements: iOS 5.0 or later.

Mobile device: iPad.

Description

This example describes some methods of working with a cube data source. After starting the example the following operations are executed:

Source Code

Executing the example requires to place the following code instead of the executeExample method of the ViewController class (see the Data Analysis section):

-(void)executeExample {
    // Get cube dimension array
    SNArray allDimensions = [self allDimensions];
    // Get first cube dimension in array
    SPPLDimension dimension = allDimensions->objectAtIndex<PPLDimension>(0);
    // Get description of object, based on which the current dimension is created
    SPPLMetabaseRepositoryObjectDescriptor dimensionObjectDescriptor = dimension->relatedObjectDescriptor();
    // Create a data source based on repository object description
    SPPLDataSource createdDataSource = PPLDataSource::dataSource(dimensionObjectDescriptor);
    // Get array of data source dimensions
    SNArray baseDimensions = createdDataSource->dimensions();
    // Create a cube data source based on obtained repository object description
    SPPLCubeDataSource cubeDataSourse = PPLCubeDataSource::cubeDataSource(dimensionObjectDescriptor);
    
    // Get text description of cube object
    SNString cubeDescription = cubeDataSourse->description();
    printf("Cube description:\n\n%s", cubeDescription->UTF8String());
    // Get description of the repository object, based on which a cube data source is created
    SPPLMetabaseRepositoryObjectDescriptor cubeDescriptor = cubeDataSourse->descriptor();
    // Get name of the repository object, based on which a cube data source is created
    SNString cubeDescriptorName = cubeDescriptor->name();
    printf("Name of the repository object, based on which a cube data source is created: %s\n", cubeDescriptorName->UTF8String());
    
    if (cubeDataSourse->dataTableInfo()) {
        // Get dictionary with cube data table description
        SNMutableDictionary dataTableInfoDescription = cubeDataTableInfoDescription(cubeDataSourse->dataTableInfo());
        // Create a cube data table description
        SPPLCubeDataTableInfo cubeDataTableInfo = PPLCubeDataTableInfo::dataTableInfo(dataTableInfoDescription);
        
        // Get cube data table name
        SNString cubeDataTableName = cubeDataTableInfo->name();
        printf("Cube data table name: %s\n", cubeDataTableName->UTF8String());
        // Get primary key of cube data table
        SNString cubeDataTablePrimaryKey = cubeDataTableInfo->primaryKey();
        printf("Primary key of cube data table: %s\n", cubeDataTablePrimaryKey->UTF8String());
        // Get cube data table fact key
        SNString cubeDataTableFactsKey = cubeDataTableInfo->factsKey();
        printf("Cube data table fact key: %s\n", cubeDataTableFactsKey->UTF8String());
        // Get cube data table fact key
        SNString cubeDataTableDateKey = cubeDataTableInfo->dateKey();
        printf("Cube data table fact key: %s\n", cubeDataTableDateKey->UTF8String());
        // Get cube data table value key
        SNString cubeDataTableValueKey = cubeDataTableInfo->valueKey();
        printf("Cube data table value key: %s\n", cubeDataTableValueKey->UTF8String());
        // Get array of all cube data table fields
        SNArray cubeDataTableFields = cubeDataTableInfo->fields();
        // Get object of data table last field
        SPPLCubeTableField cubeDataTableField = cubeDataTableFields->objectAtIndex<PPLCubeTableField>(cubeDataTableFields->count() - 1);
        // Get field name
        SNString cubeDataTableFieldName = cubeDataTableField->name();
        // Get field type
        SNString cubeDataTableFieldType = cubeDataTableField->type();
        printf("Data table field:\"%s\", type %s\n", cubeDataTableFieldName->UTF8String(), cubeDataTableFieldType->UTF8String());
        // Get identifier of dimension linked with field
        SNString cubeDataTableFieldRelatedDimensionsID = cubeDataTableField->relatedDimensionsId();
        // Display information on identifier of dimension linked with field in the development environment console
        if(cubeDataTableFieldRelatedDimensionsID != NULL) {
            printf("Identifier of dimension linked with field \"%s\": %s\n", cubeDataTableFieldName->UTF8String(),
            cubeDataTableFieldRelatedDimensionsID->UTF8String());
            } else {
            printf("Identifier of dimension linked with field \"%s\", missing\n", cubeDataTableFieldName->UTF8String());
        }
        } else {
        printf("Dictionary with description of cube data table is not defined \n");
    }
    
    if (cubeDataSourse->dataTableInfo()) {
        // Get dictionary with cube fact table description
        SNMutableDictionary facsTableInfoDescription = cubeFactsTableInfoDescription(cubeDataSourse->factsTableInfo());
        // Create a cube fact table description
        SPPLCubeFactsTableInfo cubeFactsTableInfo = PPLCubeFactsTableInfo::factsTableInfo(facsTableInfoDescription);
        
        // Get cube fact table name
        SNString cubeFactsTableName = cubeFactsTableInfo->name();
        printf("Cube fact table name: %s\n", cubeFactsTableName->UTF8String());
        // Get name of cube fact table primary key
        SNString cubeFactsTablePrimaryKey = cubeFactsTableInfo->primaryKey();
        printf("Cube fact table primary key: %s\n", cubeFactsTablePrimaryKey->UTF8String());
        // Get cube fact table data level key
        SNString cubeFactsTableDataLevelKey = cubeFactsTableInfo->dataLevelKey();
        printf("Cube fact table data level key: %s\n", cubeFactsTableDataLevelKey->UTF8String());
        // Get array of all cube fact table fields
        SNArray cubeFactsTableFields = cubeFactsTableInfo->fields();
        // Get object of fact table last field
        SPPLCubeTableField cubeFactsTableField = cubeFactsTableFields->objectAtIndex<PPLCubeTableField>(cubeFactsTableFields->count() - 1);
        // Get field name
        SNString cubeFactsTableFieldName = cubeFactsTableField->name();
        // Get field type
        SNString cubeFactsTableFieldType = cubeFactsTableField->type();
        printf("Fact table field:\"%s\", type %s\n", cubeFactsTableFieldName->UTF8String(), cubeFactsTableFieldType->UTF8String());
        // Get identifier of dimension linked with field
        SNString cubeFactsTableFieldRelatedDimensionsID = cubeFactsTableField->relatedDimensionsId();
        // Display information on identifier of dimension linked with field in the development environment console
        if(cubeFactsTableFieldRelatedDimensionsID != NULL) {
            printf("Identifier of dimension linked with field \"%s\": %s\n", cubeFactsTableFieldName->UTF8String(),
            cubeFactsTableFieldRelatedDimensionsID->UTF8String());
            } else {
            printf("Identifier of dimension linked with field \"%s\", missing\n", cubeFactsTableFieldName->UTF8String());
        }
        } else {
        printf("Dictionary with description of cube fact table is not defined \n");
    }
    
    if (cubeDataSourse->dataTableInfo() != NULL && cubeDataSourse->dataTableInfo() != NULL) {
        // Get selection for object data source dimensions
        SPPLDimensionSelectionSet pivotSet = m_gridPivot->selectionSet();
        // Get cube data matrix
        SPPLDataSourceMatrix cubeMatrix = cubeDataSourse->obtainMatrix(pivotSet, baseDimensions);
        if(cubeMatrix != NULL) printf("Cube matrix is successfully created\n");
        else printf("Cube matrix is not created\n");
    } else {
        printf("Cube matrix is not created \n");
    }
    
    // Create a dictionary containing description of cube table new field
    SNMutableDictionary fieldDescription = NMutableDictionary::mutableDictionary();
    // Add field name to dictionary
    fieldDescription->setObjectForKey(NStr("Comments"), NStr("name"));
    // Add field type to dictionary
    fieldDescription->setObjectForKey(NStr("INTEGER"), NStr("type"));
    // Create a new field of cube table
    SPPLCubeTableField field = PPLCubeTableField::tableField(fieldDescription);
    // Get new field name
    SNString tableName = field->name();
    printf("Cube table new field name: %s\n", tableName->UTF8String());
}
// Returns dictionary with description of cube data table
SNMutableDictionary cubeDataTableInfoDescription (SPPLCubeDataTableInfo dataTableInfo) {
    SNMutableDictionary tableInfoDictionary = NMutableDictionary::mutableDictionary();
    tableInfoDictionary->setObjectForKey(dataTableInfo->name(), NStr("name"));
    tableInfoDictionary->setObjectForKey(dataTableInfo->primaryKey(), NStr("primarykey"));
    tableInfoDictionary->setObjectForKey(dataTableInfo->factsKey(), NStr("facts"));
    tableInfoDictionary->setObjectForKey(dataTableInfo->dateKey(), NStr("date"));
    tableInfoDictionary->setObjectForKey(dataTableInfo->valueKey(), NStr("value"));
    
    if (dataTableInfo->fields() != NULL) {
        SNMutableArray fieldDictionaries = NMutableArray::mutableArray();
        for (int i = 0; i < dataTableInfo->fields()->count(); i++) {
            SPPLCubeTableField field = dataTableInfo->fields()->objectAtIndex<PPLCubeTableField>(i);
            // Get dictionary with cube table field description
            SNMutableDictionary fieldDictionary = cubeTableFieldDescription(field);
            fieldDictionaries->addObject(fieldDictionary);
        }
        tableInfoDictionary->setObjectForKey(fieldDictionaries, NStr("Fields"));
    }
    return tableInfoDictionary;
}
// Returns dictionary with cube fact table description
SNMutableDictionary cubeFactsTableInfoDescription (SPPLCubeFactsTableInfo cubeFactsTableInfo) {
    SNMutableDictionary tableInfoDictionary = NMutableDictionary::mutableDictionary();
    tableInfoDictionary->setObjectForKey(cubeFactsTableInfo->name(), NStr("name"));
    tableInfoDictionary->setObjectForKey(cubeFactsTableInfo->primaryKey(), NStr("primarykey"));
    tableInfoDictionary->setObjectForKey(cubeFactsTableInfo->dataLevelKey(), NStr("datalevel"));
    
    if (cubeFactsTableInfo->fields() != NULL) {
        SNMutableArray fieldDictionaries = NMutableArray::mutableArray();
        for (int i = 0; i < cubeFactsTableInfo->fields()->count(); i++) {
            SPPLCubeTableField field = cubeFactsTableInfo->fields()->objectAtIndex<PPLCubeTableField>(i);
            // Get dictionary with cube table field description
            SNMutableDictionary fieldDictionary = cubeTableFieldDescription(field);
            fieldDictionaries->addObject(fieldDictionary);
        }
        tableInfoDictionary->setObjectForKey(fieldDictionaries, NStr("Fields"));
    }
    return tableInfoDictionary;
}
// Returns dictionary with cube table field description
SNMutableDictionary cubeTableFieldDescription (SPPLCubeTableField field) {
    SNMutableDictionary fieldDictionary = NMutableDictionary::mutableDictionary();
    fieldDictionary->setObjectForKey(field->name(), NStr("name"));
    fieldDictionary->setObjectForKey(field->type(), NStr("type"));
    fieldDictionary->setObjectForKey(field->relatedDimensionsId(), NStr("dim"));
    return fieldDictionary;
}

After executing the example the development environment console displays result of cube matrix creation, information about cube data source, and also name of the created cube field:

Cube description:

 

descriptor: Object:

Key: 528

Object Id: CUBE_WORLD

Name: World Development Indicators

Type: 3

Modified date: NDate: Thu 22 Dec 2011 18:21:01                                          

Data source ref id: NULL

Hidden NO

Size: 449612

Version: 1646670

Dependencies: NULL

 

dimensions: <NMutableArray:

id: CALENDAR_Y_ENG_533

name: Calendar

Elements tree:

Elements count: 11

id: CUT_REGIONS_68045

name: Regions

Elements tree:

Elements count: 173

id: FACTS_WDI_COPY_1557

name: Indicators

Elements tree:

Elements count: 3

>

Name of the repository object, based on which a cube data source is created: World Development Indicators

Cube data table name: CUBE_DATA

Cube data table primary key: D_KEY

Cube data table fact key: F_KEY

Cube data table date key: DT

Cube data table value key: VALUE

Data table field:"VALUE", type REAL

Identifier of dimension linked to the "VALUE" field is missing

Cube fact table name: CUBE_FACTS

Cube fact table primary key: F_KEY

Cube fact table data level key: DL

Fact table field:"DL", type INTEGER

Identifier of dimension linked to the "DL" field is missing

Cube matrix is successfully created

Name of cube table new field: Comments

See also:

Example of pplib++ Library Use