package ibase.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.mortbay.util.ajax.JSON;

import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;

public class ChartCreator
{
	public static JSONObject buildChartData(ArrayList<ArrayList<String>> graphDtlList, JSONObject jsonObjGraph, String dataSource)
	{
		String key = "";
		String currentvalue = "";
		String monthKey = null;

		JSONObject graphJson = new JSONObject();
		JSONObject categryJson = new JSONObject();
		List<String> metaDataList = null;
		List<String> xAxisColunList = null;
		List<String> yAxisColunList = null;
		List<String> xAxisColumnName = null;
		List<String> yAxisColumnName = null;
		List<String> renderInfoList = null;
		List<String> colorInfoList = null;
		List<String> valueList = null;
		List<String> lineVisibleList = null;
		List<String> dotThicknessList = null;
		List<String> showValList = null;
		List<String> chartTypeList =  new ArrayList<String>();
		List<String> mapValuList = null;
		JSONValue barDataVal = null;
		JSONValue jsonChartVal = null;
		JSONValue categryDataVal = null;
		JSONString jsnStringKey = null;
		JSONString jsnStringValue = null;
		JSONString dataStringKey = null;
		JSONString dataStringValue = null;
		Set<String> valueMapSet = null;
		Object[] array = null;
		HashMap<String, List<String>> allDataMap = null;
		LinkedHashMap<JSONValue, JSONValue> categoryMap = null;
		LinkedHashMap<JSONValue, JSONValue> barChartMap = null;
		LinkedHashMap<JSONValue, JSONValue> barMap = null;
		ArrayList<LinkedHashMap<JSONValue, JSONValue>> labelList = null;
		ArrayList<LinkedHashMap<JSONValue, JSONValue>> barValueList = null;
		ArrayList<LinkedHashMap<JSONValue, JSONValue>> allDataList = null;
		
	//added by swati  on 22 dec 2014 for linkedchart
		JSONString linkStringKey = null;
		JSONString linkStringValue = null;
		List<String> linkLMetadataList = new ArrayList<String>();
		List<String> linkTypeList = new ArrayList<String>();
		String linkedProperties="";
	// ended by swati on 22 dec 2014 for linkedchart
		for (int i = 0; i < graphDtlList.size(); i++)
		{
			if (i == 3)
			{
				xAxisColunList = (ArrayList<String>) graphDtlList.get(i);
			}
			if (i == 4)
			{
				yAxisColunList = (ArrayList<String>) graphDtlList.get(i);
			}
			if (i == 5)
			{
				metaDataList = (ArrayList<String>) graphDtlList.get(i);
			}
			if (i == 6)
			{
				xAxisColumnName = (ArrayList<String>) graphDtlList.get(i);
			}
			if (i == 7)
			{
				yAxisColumnName = (ArrayList<String>) graphDtlList.get(i);
			}
			if (i == 8)
			{
				renderInfoList = (ArrayList<String>) graphDtlList.get(i);
			}
			if (i == 9)
			{
				colorInfoList = (ArrayList<String>) graphDtlList.get(i);
			}
			if (i == 10)
			{
				lineVisibleList = (ArrayList<String>) graphDtlList.get(i);
			}
			if (i == 11)
			{
				dotThicknessList = (ArrayList<String>) graphDtlList.get(i);
			}
			if (i == 12)
			{
				showValList = (ArrayList<String>) graphDtlList.get(i);
			}
			if (i == 13)
			{
				chartTypeList = (ArrayList<String>) graphDtlList.get(i);
			}
		//added by swati  on 22 dec 2014 for -- linkedchart metadataname/properties
			if (i == 14)
			{
				linkLMetadataList =  (ArrayList<String>)graphDtlList.get(i);
			}
			if (i == 15)
			{
				linkedProperties =  ""+graphDtlList.get(i);
			}
			if (i == 16)
			{
				linkTypeList =  (ArrayList<String>)graphDtlList.get(i);
			}
		//ended by swati  on 22 dec 2014 for linkedchart
		}
        int cnt=0;
		if (jsonObjGraph != null)
		{
			cnt++;
			allDataMap = new HashMap<String, List<String>>();
			barMap = new LinkedHashMap<JSONValue, JSONValue>();          
			labelList = new ArrayList<LinkedHashMap<JSONValue, JSONValue>>();
			barValueList = new ArrayList<LinkedHashMap<JSONValue, JSONValue>>();
			allDataList = new ArrayList<LinkedHashMap<JSONValue, JSONValue>>();
			try
			{
				JSONObject jsonObjFrGraph =(JSONObject) jsonObjGraph.get(dataSource);	
				for (int jsnCnt = 0; jsnCnt < jsonObjFrGraph.size(); jsnCnt++)
				{
					JSONObject fetchRowWiseVal = (JSONObject) jsonObjFrGraph.get(String.valueOf(jsnCnt));
					for (int xCnt = 0; xCnt < xAxisColunList.size(); xCnt++)
					{
						key = "" + fetchRowWiseVal.get(xAxisColunList.get(xCnt));
					}
					valueList = new ArrayList<String>();
					for (int yCnt = 0; yCnt < yAxisColunList.size(); yCnt++)
					{
						currentvalue = (fetchRowWiseVal.get(yAxisColunList.get(yCnt)).toString().replace('"', ' ').trim());
						valueList.add(currentvalue);
					}					
					allDataMap.put(key, valueList);					
					key = "";
				}
				valueMapSet = allDataMap.keySet();
				if(chartTypeList.get(0).trim().equals("bar"))
				{
					for (String keyString : valueMapSet)
					{
						mapValuList = allDataMap.get(keyString);
						categoryMap = new LinkedHashMap<JSONValue, JSONValue>();
						keyString = keyString.replace('"', ' ').trim();
						jsnStringValue = new JSONString(keyString);
						jsnStringKey = new JSONString("label");
						categoryMap.put(jsnStringKey, jsnStringValue);
						labelList.add(categoryMap);
						dataStringKey = new JSONString("value");
					}
					for (int seriesCnt = 0; seriesCnt < mapValuList.size(); seriesCnt++)
					{
						barMap = new LinkedHashMap<JSONValue, JSONValue>();
						barValueList = new ArrayList<LinkedHashMap<JSONValue, JSONValue>>();
						array = valueMapSet.toArray();
						for (int columnCnt = 0; columnCnt < array.length; columnCnt++)
						{
							monthKey = (String) array[columnCnt];
							mapValuList = allDataMap.get(monthKey);
							barChartMap = new LinkedHashMap<JSONValue, JSONValue>();
							dataStringValue = new JSONString("" + mapValuList.get(seriesCnt));
							barChartMap.put(dataStringKey, dataStringValue);
							barChartMap.put(new JSONString("showValue"), new JSONString(showValList.get(seriesCnt)));
							barValueList.add(barChartMap);
						}
						barDataVal = JSONParser.parseStrict(barValueList.toString().replace('=', ':'));
						if ("column".equals(renderInfoList.get(seriesCnt)))
						{
							barMap.put(new JSONString("renderas"), new JSONString("Column"));
							barMap.put(new JSONString("parentYAxis"), new JSONString("s"));
						}
						if ("line".equals(renderInfoList.get(seriesCnt)))
						{
							barMap.put(new JSONString("renderas"), new JSONString("Line"));
							barMap.put(new JSONString("parentYAxis"), new JSONString("s"));
							barMap.put(new JSONString("valuePosition"), new JSONString("ABOVE"));
						}
						else
						{							
							barMap.put(new JSONString("valuePosition"), new JSONString("BELOW"));
						}
						barMap.put(new JSONString("color"), new JSONString(colorInfoList.get(seriesCnt)));
						barMap.put(new JSONString("data"), barDataVal);
						barMap.put(new JSONString("seriesname"), new JSONString(yAxisColumnName.get(seriesCnt)));
						allDataList.add(barMap);						
					}
					categryDataVal = JSONParser.parseStrict(labelList.toString().replace('=', ':'));
					categryJson.put("category", categryDataVal);
					graphJson.put("dataset", JSONParser.parseStrict(allDataList.toString().replace('=', ':')));
					graphJson.put("categories", categryJson);	
					
				}
				else if(chartTypeList.get(0).trim().equals("pie"))
				{		
					for (String keyString : valueMapSet)
					{				
						mapValuList = allDataMap.get(keyString);
						categoryMap = new LinkedHashMap<JSONValue, JSONValue>();
						keyString = keyString.replace('"', ' ').trim();
						jsnStringValue = new JSONString(keyString);
						jsnStringKey   = new JSONString("label");
						categoryMap.put(jsnStringKey, jsnStringValue);						
						dataStringKey = new JSONString("value");
						
						for (int seriesCnt = 0; seriesCnt < mapValuList.size(); seriesCnt++)
						{
							dataStringValue = new JSONString("" + mapValuList.get(seriesCnt));
							categoryMap.put(dataStringKey, dataStringValue);
							barValueList.add(categoryMap);							
							barDataVal = JSONParser.parseStrict(barValueList.toString().replace('=', ':'));
						}
						graphJson.put("data", barDataVal);
						
					}
				}
				
				if(chartTypeList.get(0).trim().equals("stack"))
				{
					HashMap<String, String> dMap = new HashMap<String, String>();
					for (String keyString : valueMapSet)
					{
						mapValuList = allDataMap.get(keyString);
						jsnStringKey = new JSONString("label");
						dataStringKey = new JSONString("value");
						String value = mapValuList.get(1).replace("{", "");
						value = value.replace("}", "");
						String[] tempArray = value.split(",");
						barMap = new LinkedHashMap<JSONValue, JSONValue>();
						barValueList = new ArrayList<LinkedHashMap<JSONValue, JSONValue>>();
						for(int i=0;i<tempArray.length;i++)
						{
							if(! tempArray[i].equals(""))
							{
								String[] tempArr = tempArray[i].split(":");
								categoryMap = new LinkedHashMap<JSONValue, JSONValue>();
								barChartMap = new LinkedHashMap<JSONValue, JSONValue>();	
								for(int j=0;j<tempArr.length;j++)
								{
									jsnStringValue = new JSONString(tempArr[0]);									
									categoryMap.put(jsnStringKey, jsnStringValue);
									if(!barChartMap.containsKey(jsnStringKey))
									{
										barChartMap.put(dataStringKey, new JSONString(tempArr[1]));
										barChartMap.put(new JSONString("link"), new JSONString("n-/ibase/dwh/GwtDashboardPage.html?metadataname$pendingorder"));
									}
								}
								if(! labelList.contains(categoryMap))
								{
									labelList.add(categoryMap);
								}
								if(! barValueList.contains(categoryMap))
								{
									barValueList.add(barChartMap);
								}
							}
						}						
						barMap.put(new JSONString("seriesname"), new JSONString(mapValuList.get(0)));
						barDataVal = JSONParser.parseStrict(barValueList.toString().replace('=', ':'));
						barMap.put(new JSONString("data"), barDataVal);
						allDataList.add(barMap);
						graphJson.put("dataset", JSONParser.parseStrict(allDataList.toString().replace('=', ':')));
					 
					}
					categryDataVal = JSONParser.parseStrict(labelList.toString().replace('=', ':'));
					categryJson.put("category", categryDataVal);
					graphJson.put("categories", categryJson);	
				}
				/**Start of Linked chart logic by swati dates: 22 December 2014 **/
				 
				if(chartTypeList.get(0).trim().equals("linkedchart"))
				{
					for (String keyString : valueMapSet)
					{
						mapValuList = allDataMap.get(keyString);
						categoryMap = new LinkedHashMap<JSONValue, JSONValue>();
						keyString = keyString.replace('"', ' ').trim();
						jsnStringValue = new JSONString(keyString);
						jsnStringKey = new JSONString("label");
						categoryMap.put(jsnStringKey, jsnStringValue);
						labelList.add(categoryMap);
					}
					for (int seriesCnt = 0; seriesCnt < mapValuList.size(); seriesCnt++)
					{ 
						barMap = new LinkedHashMap<JSONValue, JSONValue>();
						barValueList = new ArrayList<LinkedHashMap<JSONValue, JSONValue>>();
						array = valueMapSet.toArray();
						for (int columnCnt = 0; columnCnt < array.length; columnCnt++)
						{
							/*start of new code by swati on 23 dec 2014*/
							String keyString = ""+array[columnCnt];
							barChartMap = new LinkedHashMap<JSONValue, JSONValue>();
							keyString = keyString.replace('"', ' ').trim();
							jsnStringValue = new JSONString(keyString);
							jsnStringKey = new JSONString("label");
							dataStringKey = new JSONString("value");
							/*end of new code by swati  on 23 dec 2014*/
							
							monthKey = (String) array[columnCnt];
							mapValuList = allDataMap.get(monthKey);
							dataStringValue = new JSONString("" + mapValuList.get(seriesCnt));
							barChartMap.put(dataStringKey, dataStringValue);
							
							barChartMap.put(new JSONString("showValue"), new JSONString(showValList.get(seriesCnt)));
							linkStringKey = new JSONString("link");
							String temp = Window.Location.getHref();
							String prevUrl = temp.substring(temp.indexOf('&'), temp.length());
							String period =FilterCreator.parameterList.get(1);
							String url=Window.Location.getParameter("titleName");
							
							//linkStringValue=new JSONString("P-detailsPopUp,"+linkedProperties+"-/ibase/dwh/GwtDashboardPage.html?metadataname$"+linkLMetadataList.get(seriesCnt)+"&"+xAxisColunList.get(0)+"$"+(""+jsnStringValue).replace('"', ' ').trim()+"&"+yAxisColunList.get(seriesCnt)+"$"+(""+dataStringValue).replace('"', ' ').trim());
							if(url=="undefined" || url.equals("undefined") || url.equalsIgnoreCase("undefined"))
							{								
								url="&titleName$"+(FilterCreator.title).get(0)+(""+jsnStringValue).replace('"', ' ').trim();
							}
							else
							{
								url="&titleName$"+url+":"+(FilterCreator.title).get(0)+(""+jsnStringValue).replace('"', ' ').trim();
							}
							if((prevUrl).trim().length()>1)
							{
							   linkStringValue=new JSONString("n-"+"/ibase/dwh/GwtDashboardPage.html?metadataname$"+linkLMetadataList.get(seriesCnt)+prevUrl.replace('=', '$')+"&PRD_SALESDASH$"+period+url+"&"+yAxisColunList.get(seriesCnt)+"$"+(""+dataStringValue).replace('"', ' ').trim()+"&"+xAxisColunList.get(0)+"$"+(""+jsnStringValue).replace('"', ' ').trim()); 
							}
							else
							{
							   linkStringValue=new JSONString("n-"+"/ibase/dwh/GwtDashboardPage.html?metadataname$"+linkLMetadataList.get(seriesCnt)+"&PRD_SALESDASH$"+period+url+"&"+yAxisColunList.get(seriesCnt)+"$"+(""+dataStringValue).replace('"', ' ').trim()+"&"+xAxisColunList.get(0)+"$"+(""+jsnStringValue).replace('"', ' ').trim());
							}
							barChartMap.put(linkStringKey, linkStringValue);
							barChartMap.put(new JSONString("color"), new JSONString(colorInfoList.get(seriesCnt)));
							barValueList.add(barChartMap);
						}
						barDataVal = JSONParser.parseStrict(barValueList.toString().replace('=', ':'));
						if ("column".equals(renderInfoList.get(seriesCnt)))
						{
							barMap.put(new JSONString("renderas"), new JSONString("Column"));
							barMap.put(new JSONString("parentYAxis"), new JSONString("s"));
						}
						if ("line".equals(renderInfoList.get(seriesCnt)))
						{
							barMap.put(new JSONString("renderas"), new JSONString("Line"));
							barMap.put(new JSONString("parentYAxis"), new JSONString("s"));
							barMap.put(new JSONString("valuePosition"), new JSONString("ABOVE"));
						}
						else
						{							
							barMap.put(new JSONString("valuePosition"), new JSONString("BELOW"));
						}
						barMap.put(new JSONString("data"), barDataVal);
						barMap.put(new JSONString("seriesname"), new JSONString(yAxisColumnName.get(seriesCnt)));
						allDataList.add(barMap);
					}
					
					categryDataVal = JSONParser.parseStrict(labelList.toString().replace('=', ':'));
					categryJson.put("category", categryDataVal);
					graphJson.put("dataset", JSONParser.parseStrict(allDataList.toString().replace('=', ':')));
					graphJson.put("categories", categryJson);
				
				}
			   /**End of Linkedd chart logic by swati dates: 22 December 2014 **/
				
				jsonChartVal = JSONParser.parseStrict(metaDataList.toString());
				graphJson.put("chart", jsonChartVal);
				
			} catch (Exception e)
			{
				Window.alert(" Exception :: Build Final Graph component [" + e.getMessage() + "]");

			}
		}
		return graphJson;
	}
	
	
	// added new method for heat map date: 16 Jan 2015
	
	public static JSONObject buildHeatMap(ArrayList<ArrayList<String>> graphDtlList, JSONObject jsonObjGraph, String dataSource)
	{
		LinkedHashMap<JSONString, JSONString> rowDataJson    =null;
		LinkedHashMap<JSONString, JSONString> columnDataJson = new LinkedHashMap<JSONString, JSONString>();
		LinkedHashMap<JSONString, JSONValue> dataJson        = new LinkedHashMap<JSONString, JSONValue>();
		LinkedHashMap<JSONString, JSONString> metaDataMap    = new LinkedHashMap<JSONString, JSONString>();
		ArrayList<String> tlLabelList= null;
		ArrayList<String> trLabelList=null;
		ArrayList<String> blLabelList= null;
		ArrayList<String> brLabelList= null;
		
		List<String> tlDataList= null;
		List<String> trDataList=null;
		List<String> blDataList= null;
		List<String> brDataList= null;		
		
		String key = ""; String currentvalue = "";
		JSONObject graphJson = new JSONObject();
		JSONObject colorDataJson = new JSONObject();
		JSONObject rowJson =new JSONObject();
		JSONObject columnJson = new JSONObject();
		JSONObject dataSetJson =new JSONObject();
		ArrayList<String> rowDataList=new ArrayList<String>();
		ArrayList<String> columnDataList=new ArrayList<String>();
		ArrayList<String> dataList=new ArrayList<String>();
		ArrayList<String> dataSetList=new ArrayList<String>();
		ArrayList<String> colorList = null;
		ArrayList<String> showValueList= null;
		
		List<String> metaDataList = null;
		List<String> rowIdList = null;		
		List<String> rowNameList = null;		
		List<String> columnIdList = null;		
		List<String> columnNameList = null;		
		List<String> colorRangeList = null;		
		List<String> valueList = null;
		List<String> mapValuList = null;
		JSONValue jsonChartVal = null;
		JSONValue colorDataVal = null;
		Set<String> valueMapSet = null;
		Set<String> mapSet = null;
		HashMap<String, List<String>> allDataMap = null;
		HashMap<String, List<String>> tlDataMap = null;
		HashMap<String, List<String>> trDataMap = null;
		HashMap<String, List<String>> blDataMap = null;
		HashMap<String, List<String>> brDataMap = null;
		
		JSONString jsnStringValue  = null;
		JSONString dataStringValue = null;
		JSONString linkStringValue = null;
		ArrayList<String> linkedMetadataList= new ArrayList<String>();
		for (int i = 0; i < graphDtlList.size(); i++)
		{
			if (i == 3)
			{
				metaDataList = (ArrayList<String>) graphDtlList.get(i);
			}			
			if (i == 4)
			{
				rowIdList = (ArrayList<String>) graphDtlList.get(i);
			}
			if (i == 5)
			{
				rowNameList = (ArrayList<String>) graphDtlList.get(i);
			}
			if (i == 6)
			{
				columnIdList = (ArrayList<String>) graphDtlList.get(i);
			}
			if (i == 7)
			{
				columnNameList = (ArrayList<String>) graphDtlList.get(i);
			}
			if (i == 8)
			{
				colorRangeList = (ArrayList<String>) graphDtlList.get(i);
			}
			if (i == 9)
			{
				colorList =  (ArrayList<String>)graphDtlList.get(i);
			}			
			if (i == 10)
			{
				showValueList =  (ArrayList<String>)graphDtlList.get(i);
			}			
			if (i == 11)
			{
				tlLabelList =  (ArrayList<String>)graphDtlList.get(i);
			}
			if (i == 12)
			{
				trLabelList =  (ArrayList<String>)graphDtlList.get(i);
			}
			if (i == 13)
			{
				blLabelList =  (ArrayList<String>)graphDtlList.get(i);
			}
			if (i == 14)
			{
				brLabelList =  (ArrayList<String>)graphDtlList.get(i);
			}
			if (i == 15)
			{
				linkedMetadataList =  (ArrayList<String>)graphDtlList.get(i);
			}
		}
		
		if (jsonObjGraph != null)
		{
			allDataMap = new HashMap<String, List<String>>();
			tlDataMap = new HashMap<String, List<String>>();
			trDataMap = new HashMap<String, List<String>>();
			blDataMap = new HashMap<String, List<String>>();
			brDataMap = new HashMap<String, List<String>>();
			
			
			try
			{
				
				//metaDataMap=metaDataList.get(0);
				
				jsonChartVal = JSONParser.parseStrict(metaDataList.get(0));
				graphJson.put("chart", jsonChartVal);
				
				JSONObject jsonObjFrGraph =(JSONObject) jsonObjGraph.get(dataSource);	
				for (int jsnCnt = 0; jsnCnt < jsonObjFrGraph.size(); jsnCnt++)
				{
					JSONObject fetchRowWiseVal = (JSONObject) jsonObjFrGraph.get(String.valueOf(jsnCnt));
					
					for (int xCnt = 0; xCnt < rowIdList.size(); xCnt++)
					{
						
						key = "" + fetchRowWiseVal.get(rowIdList.get(xCnt));
					}
					valueList = new ArrayList<String>();
					for (int yCnt = 0; yCnt < columnIdList.size(); yCnt++)
					{
						
						currentvalue = (fetchRowWiseVal.get(columnIdList.get(yCnt)).toString().replace('"', ' ').trim());
						valueList.add(currentvalue);
					}					
					allDataMap.put(key, valueList);		
					
					
					valueList = new ArrayList<String>();
					for (int yCnt = 0; yCnt < columnIdList.size(); yCnt++)
					{
						/*Window.alert("fetchRowWiseVal.get(columnIdList.get(yCnt) is "+fetchRowWiseVal.get(tlLabelList.get(yCnt)));
						Window.alert("fetchRowWiseVal.get(columnIdList.get(yCnt).toString().replace(', ' ').trim() "+fetchRowWiseVal.get(tlLabelList.get(yCnt).toString().replace('"', ' ').trim()));
						*/
						
						if(tlLabelList.get(yCnt).trim().length()!=0)
						{
							currentvalue = (fetchRowWiseVal.get(tlLabelList.get(yCnt)).toString().replace('"', ' ').trim());
						}else
						{
							currentvalue="";
						}
						
						valueList.add(currentvalue);
					}					
					tlDataMap.put(key, valueList);	
					
					valueList = new ArrayList<String>();
					for (int yCnt = 0; yCnt < columnIdList.size(); yCnt++)
					{
						//currentvalue = (fetchRowWiseVal.get(trLabelList.get(yCnt)).toString().replace('"', ' ').trim());
						
						if(trLabelList.get(yCnt).trim().length()!=0)
						{
							currentvalue = (fetchRowWiseVal.get(trLabelList.get(yCnt)).toString().replace('"', ' ').trim());
						}else
						{
							currentvalue="";
						}

						valueList.add(currentvalue);
					}					
					trDataMap.put(key, valueList);	
					
					//Window.alert("trDataMap is ="+trDataMap);
					
					valueList = new ArrayList<String>();
					for (int yCnt = 0; yCnt < columnIdList.size(); yCnt++)
					{
						//currentvalue = (fetchRowWiseVal.get(blLabelList.get(yCnt)).toString().replace('"', ' ').trim());
						if(blLabelList.get(yCnt).trim().length()!=0)
						{
							currentvalue = (fetchRowWiseVal.get(blLabelList.get(yCnt)).toString().replace('"', ' ').trim());
						}else
						{
							currentvalue="";
						}

						valueList.add(currentvalue);
					}					
					blDataMap.put(key, valueList);	
					
					
					valueList = new ArrayList<String>();
					for (int yCnt = 0; yCnt < columnIdList.size(); yCnt++)
					{
						//currentvalue = (fetchRowWiseVal.get(brLabelList.get(yCnt)).toString().replace('"', ' ').trim());
						if(brLabelList.get(yCnt).trim().length()!=0)
						{
							currentvalue = (fetchRowWiseVal.get(brLabelList.get(yCnt)).toString().replace('"', ' ').trim());
						}else
						{
							currentvalue="";
						}
						valueList.add(currentvalue);
					}					
					brDataMap.put(key, valueList);	
					key = "";
				}
				
				
				valueMapSet = allDataMap.keySet();
				ArrayList<String> rowList =new ArrayList<String>();
				for (String keyString : valueMapSet)
				{
					keyString = keyString.replace('"', ' ').trim();
					rowList.add(keyString);
					rowDataJson = new LinkedHashMap<JSONString, JSONString>();
					rowDataJson.put(new JSONString("id"),new JSONString(keyString));				
					
					rowDataList.add(rowDataJson.toString().replace('=', ':'));
				}
				
				rowJson.put("row", JSONParser.parseStrict(rowDataList.toString()));
				graphJson.put("rows", JSONParser.parseStrict(rowJson.toString()));
			
				for (int i = 0; i < columnNameList.size(); i++)
				{
					columnDataJson = new LinkedHashMap<JSONString, JSONString>();
					columnDataJson.put(new JSONString("id"),new JSONString(columnNameList.get(i)));
					columnDataList.add(columnDataJson.toString().replace('=', ':'));
				 }
				columnJson.put("column", JSONParser.parseStrict(columnDataList.toString()));
				graphJson.put("columns",  JSONParser.parseStrict(columnJson.toString()));
				
				
				mapSet =	allDataMap.keySet();
				for (String keyString : mapSet)
				{
					mapValuList = allDataMap.get(keyString);
					tlDataList = tlDataMap.get(keyString);
					trDataList = trDataMap.get(keyString);
					blDataList = blDataMap.get(keyString);
					brDataList = brDataMap.get(keyString);			
					jsnStringValue =  new JSONString(""+keyString);
					//Window.alert("jsnStringValue: "+jsnStringValue+"\n keyString:"+keyString);
					for (int i = 0; i < mapValuList.size(); i++)
					{
						dataStringValue = new JSONString("" + mapValuList.get(i));	
						dataJson = new LinkedHashMap<JSONString, JSONValue>();
						dataJson.put(new JSONString("rowid"),JSONParser.parseStrict(keyString));
				         dataJson.put(new JSONString("tlLabel"),new JSONString(tlDataList.get(i)));
				         dataJson.put(new JSONString("trLabel"),new JSONString(trDataList.get(i)));
				         dataJson.put(new JSONString("blLabel"),new JSONString(blDataList.get(i)));
				         dataJson.put(new JSONString("brLabel"),new JSONString(brDataList.get(i)));
						dataJson.put(new JSONString("showValue"),new JSONString(showValueList.get(i)));
						dataJson.put(new JSONString("columnid"),new JSONString(columnNameList.get(i)));
						dataJson.put(new JSONString("value"),new JSONString(mapValuList.get(i)));
						String temp = Window.Location.getHref();
						String prevUrl = temp.substring(temp.indexOf('&'), temp.length());
						String period =FilterCreator.parameterList.get(1);
						String url=Window.Location.getParameter("titleName");
						/*add link code here 29 jan 2015*/
						//linkStringValue=new JSONString("P-detailsPopUp,"+linkedProperties+"-/ibase/dwh/GwtDashboardPage.html?metadataname$"+linkLMetadataList.get(seriesCnt)+"&"+xAxisColunList.get(0)+"$"+(""+jsnStringValue).replace('"', ' ').trim()+"&"+yAxisColunList.get(seriesCnt)+"$"+(""+dataStringValue).replace('"', ' ').trim());
						
						if(url=="undefined" || url.equals("undefined") || url.equalsIgnoreCase("undefined"))
						{								
							url="&titleName$"+(FilterCreator.title).get(0)+(""+jsnStringValue).replace('"', ' ').trim();
						}
						else
						{
							url="&titleName$"+url+" : "+(FilterCreator.title).get(0)+(""+jsnStringValue).replace('"', ' ').trim();
						}
						url = url.replace("\\", " ");
						if((prevUrl).trim().length()>1)
						{
						   linkStringValue=new JSONString("n-"+"/ibase/dwh/GwtDashboardPage.html?metadataname$"+linkedMetadataList.get(i)+prevUrl.replace('=', '$')+"&PRD_SALESDASH$"+period+url+"&"+rowIdList.get(0)+"$"+((""+jsnStringValue).replace("\\", " ")).replace('"', ' ').trim()+"&"+keyString.replace('"', ' ').trim()+"$"+(""+dataStringValue).replace('"', ' ').trim()); 
						}
						else
						{
						   linkStringValue=new JSONString("n-"+"/ibase/dwh/GwtDashboardPage.html?metadataname$"+linkedMetadataList.get(i)+"&PRD_SALESDASH$"+period+url+"&"+rowIdList.get(0)+"$"+((""+jsnStringValue).replace("\\", " ")).replace('"', ' ').trim()+"&"+keyString.replace('"', ' ').trim()+"$"+(""+dataStringValue).replace('"', ' ').trim());
						}
						dataJson.put(new JSONString("link"),linkStringValue);
						/*end link code here 29 jan 2015*/
						dataList.add(dataJson.toString().replace('=', ':'));
					}
				}				
				dataSetJson.put("data", JSONParser.parseStrict(dataList.toString()));
				
				dataSetList.add(dataSetJson.toString().replace('=', ':'));
				graphJson.put("dataset",  JSONParser.parseStrict(dataSetList.toString()));
				
				for (int clrRngCnt = 0; clrRngCnt < colorRangeList.size(); clrRngCnt++) 
				{
					if(clrRngCnt==0)
					{
						colorDataJson.put("mapbypercent",new JSONString(colorRangeList.get(clrRngCnt)));
					}
					if(clrRngCnt==1 )
					{
						colorDataJson.put("gradient",new JSONString(colorRangeList.get(clrRngCnt)));
					}
					if(clrRngCnt==2)
					{
						colorDataJson.put("minvalue",new JSONString(colorRangeList.get(clrRngCnt)));
					}
					if(clrRngCnt==3)
					{
						colorDataJson.put("code",new JSONString(colorRangeList.get(clrRngCnt)));
					}
					if(clrRngCnt==4)
					{
						colorDataJson.put("startlabel",new JSONString((colorRangeList.get(clrRngCnt)).toString()));
					}
					if(clrRngCnt==5 )
					{
						colorDataJson.put("endlabel",new JSONString(colorRangeList.get(clrRngCnt)));
					}
				}
				colorDataVal = JSONParser.parseStrict(colorList.toString());
				colorDataJson.put("color", colorDataVal);
				graphJson.put("colorrange", colorDataJson);
				
			} catch (Exception e)
			{
				Window.alert(" Exception :: Build Final Graph component [" + e.getMessage() + "]");

			}
		}
		return graphJson;
	}
	// ended new method of heat map 
}
