--[[----------------------------------------------------------------------------

WFCatSortByOnlineRelevance.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2015 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remains the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and are protected by all applicable
intellectual property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

----------------------------------------------------------------------------]]--
local WFCatSearchAndSortConstants = require "WFCatSearchAndSortConstants"
--==============================================================================

local WFCatSortByOnlineRelevance = {}
local tableName = WFCatSearchAndSortConstants.ONLINESEARCH_TEMP_TABLE_NAME

--------------------------------------------------------------------------------

function WFCatSortByOnlineRelevance.executeSort( wfCatalog, assetIds, order )

	-- relevancy order and the sort order in the search terms are opposite
	-- i.e sort order with 1 is the highest relevancy.
	-- hence we need to reverse the order for the search string
	if order == "ASC" then
		order = "DESC"
	else
		order = "ASC"
	end
	
	local sqlString = string.format( 
			[[ SELECT a.docId, a.assetId FROM assets a   
				INNER JOIN TEMP_ids t ON t.docId == a.docId 
				INNER JOIN %s s ON a.assetId == s.docId 
				ORDER BY s.sortOrder %s ]], 
			tableName, order )
						
	-- we need docId's in the table
	return wfCatalog:indexStore():query( {
			sql = sqlString,
			tempIds = assetIds,
			calm = 1,
		} )
end

--==============================================================================

return WFCatSortByOnlineRelevance
