Defining a Query Programmatically

To define a query programmatically, use the following process:

Example: Defining a query programmatically

This example assumes that the variable m_metadataMgr exists and is initialized as a MetadataManager bean. The following code provides an example of defining a query programmatically.


//strMeasureSales is the unique identifier of the Sales measure //strMeasureUnits is the unique identifier of the Units measure //strProductDim is the unique identifier of the Product dimension // //Create a QueryManager and specify the metadata to use QueryManager m_QueryManager = new QueryManager (); m_QueryManager.setMetadataManager (m_metadataMgr); // //Create an empty Query using the QueryManager factory method Query m_query = m_QueryManager.createQuery (); // //Specify the Sales and Units measures. String [] measList = new String [2]; measList [0] = strMeasureSales; measList [1] = strMeasureUnits; // //Create a selection for the Product dimension. Selection sels [] = new Selection [1]; sels [0] = new Selection (strProductDim); // //Create a member step to specify the Product members ms = new MemberStep (strProductDim); ms.addMember ("AUDIODIV"); ms.addMember ("VIDEODIV"); // //Add the member step to the selection. sels[0].addStep (ms); // //Apply the selections to the query. m_query.applySelections (sels); // //Initialize the Query object (that is, run the query //for first time). The initCubeQuery method creates a //multidimensional data source in which the query is //represented as a cube. Since the dimensions parameter //is specified as "null", this method uses a default //dimension layout based on the dimensionality //of the measures in "measlist". m_query.initCubeQuery (measList, null);

Note: For an example that specifies the layout of dimensions in a query, see the example in Defining a Query Using QueryBuilder.