Check Whether a Measure Group is Mapped With Dimension Group

Introduction

For the past few months I have been working on a dashboard application where ADOMD is the data source. Our application contains many charts, grids, maps and some other UI controls. We are using MDX queries for generating the widgets used in our application. As you all know, the data must be correct and accurate in every dashboard application. Only then can we get the correct visuals in the charts, grids, maps and so on. So it is necessary to check for the proper data and we must avoid the unwanted calls to the server for fetching the data.

Background

A few days ago I was working on a Bubble Map in High Map. For the map we had to specify the latitude and longitude without any faults in the data. Here we will use a mapping function to check the dimension group and measure group is mapped or not. This will ensure the data is proper and without this check, our MDX query may continue to run. This will definitely make the end users close our application and search for another one. So my idea is to provide a message if the measure group and dimension groups are not mapped, to avoid running the queries for a long time.

What we will do

We will use the following procedure.

  1. An ajax call to pass the information to check the process.
  2. A controller function where we create a ADOMD connection object and pass the information to the model class.
  3. A model class where we build and run the query with the necessary parameters.

The last step will provide you an information that says whether the given measure group is mapped with the dimension group.

Using the code

So let us start the coding now.

As we discussed earlier, first of all we need an ajax call, right?

  • An Ajax Call

The following is our ajax call implementation.

  1. var cubeName = '';  
  2. var measureGroupName = '';  
  3. var dimensionGroupName = '';  
  4. var serverName = 'My server name';  
  5. var databaseName = 'My database name';  
  6. cubeName = 'My cube name';  
  7. measureGroupName = sessionStorage.getItem("measureGroupName");  
  8. dimensionGroupName = sessionStorage.getItem("dimensionGroupName");  
  9. var checkMeasuresMapped =   
  10. {  
  11.     cubeName: cubeName,  
  12.     measureGroupName: measureGroupName,  
  13.     dimensionGroupName: dimensionGroupName,  
  14.     serverName: serverName,  
  15.     databaseName: databaseName  
  16. };  
  17. $.ajax(  
  18.     {  
  19.     async: ajaxAsync,  
  20.     url: '../CheckMeasureGroupMapping/',  
  21.     type: 'POST',  
  22.     dataType: 'json',  
  23.     data: JSON.stringify(checkMeasuresMapped),  
  24.     contentType: "application/json; charset=utf-8",  
  25.     success: function(data)   
  26.     {  
  27.         if (data.indexOf("Error") > -1)   
  28.         {  
  29.             $('#btnCreate').css('enabled''false');  
  30.             $('#Preview').html('<p style="color:red;">Error : ' + data.replace('Error''') + '</p>');  
  31.         }   
  32.         else if (data == "Mapped")   
  33.         {  
  34.             //Condition satisfied, write your codes here  
  35.             ajaxAsync = true;  
  36.         }   
  37.         else   
  38.         {  
  39.             $('#Preview').html('<p style="color:red;">Warning : The given measure is not mapped with the dimension. Please check.</p>');  
  40.         }  
  41.     },  
  42.     error: function(xhrequest, ErrorText, thrownError)  
  43.     {  
  44.         console.log(ErrorText + "," + thrownError);  
  45.         $('#Preview').html('<p style="color:red;">Error : ' + ErrorText + '</p>');  
  46.     }  
  47. });  
Please understand that I have passed all the necessary information. We will list what they all are.
  1. Cube Name.
  2. Measure Group Name.
  3. Dimension Group Name.
  4. Server Name.
  5. Database Name.

Once you are done with the ajax implementation, you will get the information in our controller, right? That is how the ajax call works. If you are new to the ajax calls, please refer to the following links.

http://www.w3schools.com/ajax/
http://www.w3schools.com/jquery/ajax_ajax.asp

  • Controller Function

I hope you are all done with the ajax implementation, now we will move on to the next part. Please refer to the following code.

  1. ///<summary>  
  2. ///This method is used to find whether the given measure is mapped to the dimension  
  3. ///<param name="cubeName"></param>  
  4. ///<param name="measureGroupName"></param>  
  5. ///<param name="dimensionGroupName"></param>  
  6. ///</summary>  
  7. [HandleError]  
  8. [AcceptVerbs(HttpVerbs.Post)]  
  9. public ActionResult CheckMeasureGroupMapping(string cubeName, string measureGroupName, string dimensionGroupName, string serverName, string databaseName) {  
  10.     try   
  11.     {  
  12.         DataTable dt = new DataTable();  
  13.         dt = adomdConn.CheckMeasureGroupMapping(cubeName, measureGroupName, dimensionGroupName, serverName, databaseName);  
  14.         if (dt.Rows.Count < 1)   
  15.         {  
  16.             return Json("The given measure is not mapped with the dimension. Please check.", JsonRequestBehavior.AllowGet);  
  17.         }  
  18.         else   
  19.         {  
  20.             return Json("Mapped", JsonRequestBehavior.AllowGet);  
  21.         }  
  22.     }   
  23.     catch (AdomdErrorResponseException ex)   
  24.     {  
  25.         string errrorText = "Query Error" + System.Web.HttpUtility.HtmlEncode(ex.Message);  
  26.         return Json(errrorText, JsonRequestBehavior.AllowGet);  
  27.     }  
  28. }  
Here CheckMeasureGroupMapping is our ActionResult and we are passing the information to our model class. If our conditions are satisfied then the preceding function will return the rows in a datatable. With the row count of that data table we can arrive at our conclusion.
  • Model Function

Now we are in the final part, since we have the necessary parameters in our model class, it is time to build the query and run it in the ADOMD server. So the following is the function to do that.

  1. public DataTable CheckMeasureGroupMapping(string cubeName, string measureGroupName, string dimensionGroupName, string serverName, string databaseName)  
  2. {  
  3.     try   
  4.     {  
  5.         string query = string.Empty;  
  6.         string queryBefore = string.Empty;  
  7.         queryBefore = "SELECT [MEASUREGROUP_NAME] AS [MEASUREGROUP],[MEASUREGROUP_CARDINALITY],[DIMENSION_UNIQUE_NAME] AS [DIM],[DIMENSION_GRANULARITY] AS [DIM_KEY],[DIMENSION_CARDINALITY],[DIMENSION_IS_VISIBLE] AS [IS_VISIBLE],[DIMENSION_IS_FACT_DIMENSION] AS [IS_FACT_DIM] FROM $system.MDSCHEMA_MEASUREGROUP_DIMENSIONS WHERE [CUBE_NAME] = {cubeName} AND [MEASUREGROUP_NAME] ={measureGroupName} AND [DIMENSION_UNIQUE_NAME]={dimensionGroupName}";  
  8.         query = queryBefore.Replace("{cubeName}""'" + cubeName + "'").Replace("{measureGroupName}""'" + measureGroupName + "'").Replace("{dimensionGroupName}""'" + dimensionGroupName + "'");  
  9.         StringBuilder sbConnectionString = new StringBuilder();  
  10.         sbConnectionString.Append("Provider=MSOLAP;data source=");  
  11.         sbConnectionString.Append(serverName + ";initial catalog=" + databaseName + ";Integrated Security=SSPI;Persist Security Info=False;");  
  12.         using(AdomdConnection conn = new AdomdConnection(sbConnectionString.ToString()))  
  13.         {  
  14.             conn.Open();  
  15.             using(AdomdCommand cmd = new AdomdCommand(query, conn))  
  16.             {  
  17.                 DataTable dt = new DataTable();  
  18.                 AdomdDataAdapter da = new AdomdDataAdapter(cmd);  
  19.                 da.Fill(dt);  
  20.                 return dt;  
  21.             }  
  22.         }  
  23.     }  
  24.     catch (AdomdErrorResponseException ex)   
  25.     {  
  26.         throw;  
  27.     }  
  28. }  
As you all can see, the following is the query for determining whether the given measure group is mapped with the dimension group or not.
  1. SELECT  
  2. [MEASUREGROUP_NAME] AS [MEASUREGROUP],  
  3. [MEASUREGROUP_CARDINALITY],  
  4. [DIMENSION_UNIQUE_NAME] AS [DIM],  
  5. [DIMENSION_GRANULARITY] AS [DIM_KEY],  
  6. [DIMENSION_CARDINALITY],  
  7. [DIMENSION_IS_VISIBLE] AS [IS_VISIBLE],  
  8. [DIMENSION_IS_FACT_DIMENSION] AS [IS_FACT_DIM]  
  9. FROM  
  10. $system.MDSCHEMA_MEASUREGROUP_DIMENSIONS  
  11. WHERE  
  12. [CUBE_NAME] = {cubeName}  
  13. AND [MEASUREGROUP_NAME] = {measureGroupName}  
  14. AND [DIMENSION_UNIQUE_NAME] = {dimensionGroupName} ";  
We are running this query with the parameters and we will return the result to our controller.

At the end

At the end we will check the condition as we have shown in the ajax call.
  1. if (data.indexOf("Error") > -1)   
  2. {  
  3.     $('#btnCreate').css('enabled''false');  
  4.     $('#Preview').html('<p style="color:red;">Error : ' + data.replace('Error''') + '</p>');  
  5. }  
  6. else if (data == "Mapped")  
  7. {  
  8.     //Condition satisfied, write your codes here  
  9.     ajaxAsync = true;  
  10. }   
  11. else  
  12. {  
  13.     $('#Preview').html('<p style="color:red;">Warning : The given measure is not mapped with the dimension. Please check.</p>');  
  14. }  
So we are done with this requirement.

Happy coding!

Conclusion

I hope you liked my article. Please share your valuable feedbacks. It means a lot.

Additional references Output

If the given measure group is not mapped with the measure group, you will get the following as output in your preview area.



Points of interests

ADOMD, ADOMD Measure, ADOMD Dimension, ADOMD Measure Groups, ADOMD Dimension Group, Mapping measure group and dimension group.


Similar Articles