/*
 *+------------------------------------------------------------------------+
 *| IBM Confidential
 *| OCO Source Materials
 *| BI and PM: viewer
 *| (C) Copyright IBM Corp. 2001, 2009
 *|
 *| US Government Users Restricted Rights - Use, duplication or
 *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *|
 *+------------------------------------------------------------------------+
 */

function CSelectionMetadata()
{
	this.m_sContextId = "";
	this.m_sDataItem = "";
	this.m_sMetadataModelItem = "";
	this.m_sUseValue = "";
	this.m_sUseValueType;
	this.m_sType = null;
	this.m_sDisplayValue = "";
	this.m_sUsage = null;
	this.m_refQuery = null;
};

CSelectionMetadata.prototype.setContextId = function(sContextId)
{
	this.m_sContextId = sContextId;
};

CSelectionMetadata.prototype.getContextId = function()
{
	return this.m_sContextId;
};

CSelectionMetadata.prototype.setRefQuery = function(sRefQuery)
{
	this.m_refQuery = sRefQuery;
};

CSelectionMetadata.prototype.getRefQuery = function()
{
	return this.m_refQuery;
};

CSelectionMetadata.prototype.setDataItem = function(sDataItem)
{
	this.m_sDataItem = sDataItem;
};

CSelectionMetadata.prototype.getDataItem = function()
{
	return this.m_sDataItem;
};

CSelectionMetadata.prototype.setMetadataModelItem = function(sMetadataModelItem)
{
	this.m_sMetadataModelItem = sMetadataModelItem;
};

CSelectionMetadata.prototype.getMetadataModelItem = function()
{
	return this.m_sMetadataModelItem;
};

CSelectionMetadata.prototype.setUseValue = function(sUseValue)
{
	this.m_sUseValue = sUseValue;
};

CSelectionMetadata.prototype.getUseValue = function()
{
	return this.m_sUseValue;
};

CSelectionMetadata.prototype.setUseValueType = function(sUseValueType)
{
	this.m_sUseValueType = sUseValueType;
};

CSelectionMetadata.prototype.setType = function(sType)
{
	this.m_sType = sType;
};

CSelectionMetadata.prototype.getType = function()
{
	switch(this.m_sUseValueType)
	{
		case 25: // MemberUniqueName
		case 27: // DimensionUniqueName
		case 30: // HierarchyUniqueName
		case 32: // LevelUniqueName
		{
			return "memberUniqueName";
		}
		case 26: //MemberCaption
		{
			return "memberCaption";
		}
		case 1: // String
		case 55: //I18NExternalBuffer
		case 56: //I18NExternalBuffer
		{
			return "string";
		}
		case 2: //Int8
		case 3: //UInt8
		case 4: //Int16
		case 5: //UInt16
		case 6: //Int32
		case 7: //UInt32
		case 8: //Int64
		case 9: //UInt64
		case 10: //float
		case 11: //double
		case 12: //decimal
		case 16: //dt interval
		case 17: //ym interval
		case 18: //blob
		case 19: //RowIterator
		case 20: //DimInterator
		case 22: //Variant
		case 21: //MasterDataset
		case 23: //Binary
		case 24: //VarBinary
		case 54: //numeric
		{
			return parseInt(this.m_sUseValueType);
		}
		default:
		{
			return null;
		}
	}
};

CSelectionMetadata.prototype.getUseValueType = function()
{
	if(this.m_sType == null)
	{
		this.m_sType = this.getType();
	}

	return this.m_sType;
};;

CSelectionMetadata.prototype.setDisplayValue = function(sDisplayValue)
{
	this.m_sDisplayValue = sDisplayValue;
};

CSelectionMetadata.prototype.getDisplayValue = function()
{
	return this.m_sDisplayValue;
};

CSelectionMetadata.prototype.setUsage = function(sUsage)
{
	this.m_sUsage = sUsage;
};

CSelectionMetadata.prototype.getUsage = function()
{
	if(this.m_sUsage == "2")
	{
		return "measure";
	}
	else
	{
		return "nonMeasure";
	}
};

function CSelectionMetadataIterator(selectionObject, axisIndex)
{
	this.m_axisIndex = axisIndex;
	this.m_index = 0;
	this.m_selectionObject = selectionObject;
};

CSelectionMetadataIterator.prototype.getSelectionAxis = function()
{
	var selectionAxis = null;

	if(typeof this.m_selectionObject == "object" && this.m_axisIndex < this.m_selectionObject.getSelectedContextIds().length)
	{
		selectionAxis = this.m_selectionObject.getSelectedContextIds()[this.m_axisIndex];
	}

	return selectionAxis;
};

CSelectionMetadataIterator.prototype.hasNext = function()
{
	var selectionAxis = this.getSelectionAxis();
	if(selectionAxis != null)
	{
		return (this.m_index < selectionAxis.length);
	}
	else
	{
		return false;
	}
};

CSelectionMetadataIterator.prototype.next = function()
{
	var selectionMetadata = null;
	if(this.hasNext())
	{
		var selectionAxis = this.getSelectionAxis();

		selectionMetadata = new CSelectionMetadata();
		selectionMetadata.setContextId(this.m_selectionObject.m_contextIds[this.m_axisIndex][this.m_index]);
		selectionMetadata.setDataItem(this.m_selectionObject.getDataItems()[this.m_axisIndex][this.m_index]);
		selectionMetadata.setMetadataModelItem(this.m_selectionObject.getMetadataItems()[this.m_axisIndex][this.m_index]);

		if(this.m_selectionObject.getMuns()[this.m_axisIndex][this.m_index] != null && this.m_selectionObject.getMuns()[this.m_axisIndex][this.m_index] != "")
		{
			selectionMetadata.setUseValue(this.m_selectionObject.getMuns()[this.m_axisIndex][this.m_index]);
			selectionMetadata.setType("memberUniqueName");
		}
		else
		{
			selectionMetadata.setUseValue(this.m_selectionObject.getUseValues()[this.m_axisIndex][this.m_index]);
		}

		if(typeof this.m_selectionObject.m_selectionController == "object")
		{
			var ctxId = this.m_selectionObject.getSelectedContextIds()[this.m_axisIndex][this.m_index];

			selectionMetadata.setDisplayValue(this.m_selectionObject.m_selectionController.getDisplayValue(ctxId));
			selectionMetadata.setUseValueType(this.m_selectionObject.m_selectionController.getDataType(ctxId));
			selectionMetadata.setUsage(this.m_selectionObject.m_selectionController.getUsageInfo(ctxId));
			selectionMetadata.setRefQuery(this.m_selectionObject.m_selectionController.getRefQuery(ctxId));
		}

		++this.m_index;
	}

	return selectionMetadata;
};

function CAxisSelectionIterator(selectionObject)
{
	this.m_index = 0;
	this.m_selectionObject = selectionObject;
};

CAxisSelectionIterator.prototype.hasNext = function()
{
	return ((typeof this.m_selectionObject == "object") && (this.m_index < this.m_selectionObject.getSelectedContextIds().length));
};

CAxisSelectionIterator.prototype.next = function()
{
	var selectionMetadataIterator = null;

	if(this.hasNext())
	{
		selectionMetadataIterator = new CSelectionMetadataIterator(this.m_selectionObject, this.m_index);
		++this.m_index;
	}

	return selectionMetadataIterator;
};

function getViewerSelectionContext(selectionController, selectionContext)
{
	var selectedObjects = selectionController.getAllSelectedObjects();

	if(selectedObjects != null && selectedObjects.length > 0)
	{
		for(var index = 0; index < selectedObjects.length; ++index)
		{
			var usedIds = new Object();
			var axisSelectionIterator = new CAxisSelectionIterator(selectedObjects[index]);

			if(axisSelectionIterator.hasNext())
			{
				var selectionMetadataIterator = axisSelectionIterator.next();
				if(selectionMetadataIterator.hasNext())
				{
					var selectionMetadata = selectionMetadataIterator.next();
					var contextId = selectionMetadata.getContextId();

					usedIds[contextId] = true;
					var selectedCell = selectionContext.addSelectedCell(selectionMetadata.getDataItem(), selectionMetadata.getMetadataModelItem(), selectionMetadata.getUseValue(), selectionMetadata.getUseValueType(), selectionMetadata.getDisplayValue(), selectionMetadata.getUsage(), {"queryName":selectionMetadata.getRefQuery()});

					while(selectionMetadataIterator.hasNext())
					{
						selectionMetadata = selectionMetadataIterator.next();

						contextId = selectionMetadata.getContextId();
						if(typeof usedIds[contextId] == "undefined")
						{
							usedIds[contextId] = true;
							selectedCell.addDefiningCell(selectionMetadata.getDataItem(), selectionMetadata.getMetadataModelItem(), selectionMetadata.getUseValue(), selectionMetadata.getUseValueType(),  selectionMetadata.getDisplayValue(), selectionMetadata.getUsage());
						}
					}

					while(axisSelectionIterator.hasNext())
					{
						var selectionMetadataIterator = axisSelectionIterator.next();
						var starterCell = selectedCell;
						while(selectionMetadataIterator.hasNext())
						{
							selectionMetadata = selectionMetadataIterator.next();
							contextId = selectionMetadata.getContextId();
							if(typeof usedIds[contextId] == "undefined")
							{
								usedIds[contextId] = true;
								starterCell = starterCell.addDefiningCell(selectionMetadata.getDataItem(), selectionMetadata.getMetadataModelItem(), selectionMetadata.getUseValue(), selectionMetadata.getUseValueType(),  selectionMetadata.getDisplayValue(), selectionMetadata.getUsage());
							}
						}
					}
				}
			}
		}
	}

	var sSelectionContext = selectionContext.toString();

	if (typeof g_firefoxPluginHelper != "undefined" && g_firefoxPluginHelper != null)
	{
		g_firefoxPluginHelper.debug('Selection context', sSelectionContext, "xml");
	}

	return sSelectionContext;
};



