// ------------------------------------- Property Value Pair Class --------------------------------------

//
// Constructor
//
function PropertyValuePair()
{
	PropertyValuePair.prototype.propertyGuid = "";
	PropertyValuePair.prototype.propertyValue = "";

	if( arguments.length != 2 )
		throw "Incorrect number of parameters to PropertyValuePair constructor";

	this.propertyGuid = arguments[0];
	this.propertyValue = arguments[1];
}

// ------------------------------------------- Content Methods ------------------------------------------

//
// Create a new piece of content and return the response XML
//
function CreateNewContent( parentContentGuid, classGuid, relationship, name )
{
	var newContentGUID = "";

	var xmlRequest = BuildDoActionCreateXML( parentContentGuid, classGuid, relationship, name );

	var xmlResponse = window.Application.Api.Call( "ContentRegistry.DoAction", xmlRequest );

	if ( !xmlResponse.IsValidResponse() )
	{
		xmlResponse.ShowErrorDialog();
		return null;
	}

	return xmlResponse;
}

//
// Delete the specified piece of content
//
function DeleteContent( contentGuid )
{
	var xmlRequest = window.Application.Xml.NewMSDom( "<request><content_registry><guid>" + contentGuid + "</guid></content_registry></request>" );
	
	var xmlResponse = window.Application.Api.Call( "ContentRegistry.DeleteContentRegistry", xmlRequest );

	if( ! xmlResponse.IsValidResponse() )
	{
		xmlResponse.ShowErrorDialog();
		return false;
	}

	return true;
}

//
// Get the details of multiple pieces of content specified in the input XML
//
function GetContentDetailsWithXml(xmlContentGuids)
{
	var xmlContentNodes = xmlContentGuids.selectNodes("//guid");
	
	if( xmlContentNodes.length < 1 )
	{
		return null;
	}

	var xmlRequest = window.Application.Xml.NewDom();

	xmlRequest.SetRootName("<request>");

	xmlRequest.Add("<options>+");
	xmlRequest.Add("<detail_level>", "DETAILED");

	xmlRequest.Add("-<content_registries>+");

	var i = 0;

	for( i = 0; i < xmlContentNodes.length; i++ )
	{
		xmlRequest.Add("<content_registry>");
		xmlRequest.Add("@guid", xmlContentNodes[i].text);
	}

	var xmlResponse = window.Application.Api.Call( "ContentRegistry.Get", xmlRequest.Dom );

	if( ! xmlResponse.IsValidResponse() )
	{
		xmlResponse.ShowErrorDialog();
		return null;
	}
	
	return xmlResponse.Dom;
}

//
// Get the name of the specified piece of content
//
function GetContentName(contentGuid)
{
	var name = "";

	var xmlRequest = window.Application.Xml.NewMSDom( "<request><content_registry><guid>" + contentGuid + "</guid></content_registry></request>" );

	var xmlContentDom = GetContentDetailsWithXml( xmlRequest );

	var nameNode = xmlContentDom.selectSingleNode( "/response/data/content_registry/name" );

	if( nameNode != null )
	{
		name = nameNode.text;
	}
	
	return name;
}

//
// Re-name of the specified piece of content
//
// Expected parameters are as follows:
//
//		contentRegistryGuid - The GUID of the content to update
//		newName - The value to update the name to
//
function RenameContent( contentRegistryGuid, newName )
{
	var xmlRequest = window.Application.Xml.NewDom();

	xmlRequest.SetRootName("<request>");

	xmlRequest.Add("<content_registry>+");
	xmlRequest.Add("<guid>", contentRegistryGuid);
	xmlRequest.Add("<name>", newName);

	var xmlResponse = window.Application.Api.Call( "ContentRegistry.UpdateContentRegistry", xmlRequest.Dom );

	if( ! xmlResponse.IsValidResponse() )
	{
		xmlResponse.ShowErrorDialog();
		return false;
	}
	
	return true;
}

//
// Set property values on a piece of content
//
// Expected parameters are as follows:
//
//		contentGuid			- The GUID of the content to update
//		propertyValuePairs	- An array of PropertyValuePair objects
//
// Returns true if the update succeeded, false otherwise
//
function SetPropertyValues( contentGuid, propertyValuePairs )
{
	if( propertyValuePairs.length < 1 )
		return true;

	var xmlRequest = window.Application.Xml.NewDom();

	xmlRequest.SetRootName("<request>");

	AddPropertyValuesToRequest( xmlRequest, contentGuid, propertyValuePairs );

	var xmlResponse = window.Application.Api.Call( "ContentRegistry.SetContentRegistryAssignedPropertyValues", xmlRequest.Dom );

	if ( !xmlResponse.IsValidResponse() )
	{
		xmlResponse.ShowErrorDialog();
		return false;
	}

	return true;
}

//
// Add property values to the request XML
//
function AddPropertyValuesToRequest( xmlRequest, contentGuid, propertyValuePairs )
{
	for( var i = 0; i < propertyValuePairs.length; i++ )
	{
		xmlRequest.Add( "<content_registry_assigned_property_value>+" );

			if( contentGuid.length > 0 )
				xmlRequest.Add( "<content_registry_guid>", contentGuid );

			xmlRequest.Add( "<property_guid>", propertyValuePairs[i].propertyGuid );

			xmlRequest.Add( "<value>-", propertyValuePairs[i].propertyValue );
	}
}

//
// Rebake security on the piece of content
//
function RebakeSecurity(contentGuid)
{
	var xmlRequest = window.Application.Xml.NewDom();

	xmlRequest.SetRootName("<request>");

	xmlRequest.Add("<content_registries>+");
		xmlRequest.Add("<content_registry>");
		xmlRequest.Add("@guid", contentGuid);
	
	var xmlResponse = window.Application.Api.Call( "ContentRegistry.RebakeSecurity", xmlRequest.Dom );

	if ( ! xmlResponse.IsValidResponse() )
		xmlResponse.ShowErrorDialog();
}

//
// Updates the last update date of a content registry record
//
function TouchContent(contentGuid)
{
	var xmlRequest = window.Application.Xml.NewDom();

	xmlRequest.SetRootName("<request>");
	xmlRequest.Add("<content_registry>");
	xmlRequest.Add("@guid", contentGuid);
	
	var xmlResponse = window.Application.Api.Call( "ContentRegistry.TouchContentRegistry", xmlRequest.Dom );

	if ( ! xmlResponse.IsValidResponse() )
		xmlResponse.ShowErrorDialog();
}
