void SyncManagerImpl::DeleteDataFromUserRecurs(const ElementPtr& element, const UserDiscriminator& discriminator)
{
	ObjectElementPtr objectElement = ObjectElement::Cast(element);
	if (objectElement)
	{
		if (discriminator(objectElement->GetOwnerID()))
		{
#if defined(SYNC_DEBUG)
			LogInfo("%s Deleting object %s with owner %i", m_syncContext->GetLocalUser()->GetName().GetString().c_str(), objectElement->GetName().GetString().c_str(), objectElement->GetOwnerID());
#endif
			// Create a delete operation to represent this change
			DeleteOperationPtr deleteOp = new DeleteOperation(element->GetGUID(), element->GetParent()->GetGUID(), m_syncContext);

			// Execute the operation
			deleteOp->Apply(m_syncContext);

			// Add the op to the list of ops applied sync the last sync, so that remote machines 
			// are sent the delete operation
			m_syncContext->AddAppliedOperation(deleteOp);

			// Add the op to the list of pending notifications, to notify the user on this machine that the element has been deleted
			m_pendingNotifications.push(deleteOp);
		}
		else
		{
			// Recurs to the children of this element
			for (int32 i = objectElement->GetElementCount() - 1; i >= 0; --i)
			{
				DeleteDataFromUserRecurs(objectElement->GetElementAt(i), discriminator);
			}
		}
	}
}
void SyncManagerImpl::AddCreateOpForElementRecurs(RemoteSyncPeer& peer, const ElementPtr& element)
{
	// Construct a CreateOperation for this element
	XGuid parentGuid = kInvalidXGuid;
	ElementPtr parent = element->GetParent();
	if (parent)
	{
		parentGuid = parent->GetGUID();
	}

	{
		CreateOperationPtr createOp = new CreateOperation(
			element->GetElementType(),
			element->GetName(),
			element->GetGUID(),
			parentGuid,
			element->GetXValue(),
			m_syncContext->GetAuthorityLevel(),
			m_syncContext);

		// Add to the list of outgoing ops
		peer.SendOp(createOp);
	}

	// If this is an object element...
	ObjectElementPtr objElement = ObjectElement::Cast(element);
	if (objElement)
	{
		// Recurs to the children of this element
		for (int32 i = 0; i < objElement->GetElementCount(); ++i)
		{
			AddCreateOpForElementRecurs(peer, objElement->GetElementAt(i));
		}
	}
	else
	{
		// If the element is an array, add Insert operations for all its entries
		ArrayElement* arrayElement = reflection_cast<ArrayElement>(element);
		if (arrayElement != nullptr)
		{
			int32 numEntries = arrayElement->GetCount();
			for (int32 i = 0; i < numEntries; ++i)
			{
				InsertOperationPtr insertOp = new InsertOperation(
					element->GetGUID(),
					i,
					arrayElement->GetXValue(i),
					m_syncContext->GetAuthorityLevel(),
					m_syncContext);

				// Add to the list of outgoing ops
				peer.SendOp(insertOp);
			}
		}
	}
}