--[[
	This script generates the hints for sorting the DX sequences within the study
	"single image" sequences first, "all images" sequence last 
	
	Mahesh Nagarajan	13 FEB 2007
	- 	Modified syntaxes for sorting hints as part of v11.5 re-write
	-	Site customized code will be removed and re-located to SortDX.site files -> starting with 11.5, 
		unless sort orders are fundamentally flawed, all change requests to sorting schemes will be
		incorporated into a site file for the site.
	Mahesh Nagarajan	12 JUN 2008
	-	Changing the sorting of DX thumbnails so it resembles the DX All Images sequence sorting; 
		this will change the primary sort from image number to acquisition time
	Mahesh Nagarajan   08 JUL 2007	
	-	Adding custom sort hint - a combination of series & image number primarily for 
		use at Hopital du Sacre Coeur de Mtl	
	Shudong Liu 	14 JAN 2009
	-	Customized for site Hopital du Sacre Coeur de Mtl to sort image by laterality. (CRDB70236)		
    James Zhang     27 JULY 2009
    -   Customized for CHI to sort thumbnails first by Thumbnail group,
        then by ID Image Time, as per REI 13971(CRDB00077777).
    James Zhang     10 Nov 2009
    -   Added one more sort hint for Hopital du Sacre Coeur de Mtl,
        as per REI 10966(CRDB00080784).
    James Zhang     10 Nov 2009
	 -	Customized for CHI again according to new requirement.
        Look at CRDB00081076(REI 15697) for detail.
    James Zhang     24 Nov 2009
	 -	Adding the 4th site name for CHI.
--]]

-- ////////////////////////////////////////////////////////////////////////////
-- DX grouping function
-- ////////////////////////////////////////////////////////////////////////////

function Execute()
-- ////////////////////////////////////////////////////////////////////////////
-- generates the hints for sorting the DX sequences within a study as follows 
-- single image sequences first, all images sequence last 
--- ////////////////////////////////////////////////////////////////////////////
 
    local modalityAttr = GetAttr(MODALITY_TAG)
    if( not modalityAttr ) then
            return -- wrong context (no modality in the input collection)
            -- =====================================================
    end
    if( not BestMatchInAttr(modalityAttr, "DX") ) then
            return -- wrong sDXipt or wrong context (DX script applied on other modality)
            -- =====================================================
    end	

    RUIS_VAL = "Hopital du Sacre Coeur de Mtl" 
	CHI1_VAL = "St. John's Outpatient Diagnostic"
	CHI2_VAL = "Neosho Medical Center"
    CHI3_VAL = "Maude-Norton Memorial Hospital"
    CHI4_VAL = "St. Johns Regional Medical Center"
	
    local isRUIS = false
    local isCHI = false
	
	local srtHint = SortingHint:new("DX Sequence Sort", "DEFAULT")
	
	local SiteName = GetAttr(INSTITUTION_NAME_TAG)
    if (SiteName ~= nil) then
        if (BestWordMatchInAttr(SiteName, RUIS_VAL)) then
            isRUIS = true
            
            -- Site customization for RUIS, look at REI 10966 for detail.
            srtHint:AddEntry(SERIES_NUMBER_TAG, true)
            srtHint:AddEntry(LATERALITY_TAG, true)    -- try to get Left and Right images lumped together
            srtHint:AddEntry(THUMBNAIL_GRP_PROP, true) -- Thumbnail Group was assigned specially for RUIS
            srtHint:AddEntry(INSTANCE_NUMBER_TAG, true)
        else
            if (BestWordMatchInAttr(SiteName, CHI1_VAL, CHI2_VAL, CHI3_VAL, CHI4_VAL)) then
                isCHI = true
            end
        end
    end

     -- CHI's Acquisition Time value is not reliable; they don't like Thumbnail Group either
    if (not isCHI) then
        srtHint:AddEntry(THUMBNAIL_GRP_PROP, true)
        srtHint:AddEntry(ACQUISITON_DATETIME_TAG, true)
        srtHint:AddEntry(ACQUISITON_DATE_TAG, true)
        srtHint:AddEntry(ACQUISITON_TIME_TAG, true)
    end
	
    srtHint:AddEntry(IMAGE_ACQUISITON_TIME_TAG, true)
    srtHint:AddEntry(ALI_IMAGE_FILENAME_TAG, true) 	
	srtHint:AddEntry(INSTANCE_NUMBER_TAG, true)
    srtHint:AddEntry(IMAGE_UID_TAG, true)
	srtHint:AddEntry(SERIES_DATE_TAG, true)
    srtHint:AddEntry(SERIES_TIME_TAG, true)
    srtHint:AddEntry(SERIES_NUMBER_TAG, true)
    srtHint:AddEntry(SERIES_UID_TAG, true)
	
    SortSequencesInStudy(srtHint) 
end

