--[[
	This grouping script partition mammography (MG) images as follows:
	Each image in its own sequence
   
	Cristian Bonciu	August-September 2004        
	-	Created and implemented the functionality from pseudo-code prototypes provided by Allan 
	Allan Noordvyk     		07 SEP 2005    
     -	Added grouping of images with for processing intent only to all images series,
		so that they won't be shown as thumbnails.
     Mahesh Nagarajan		18 JAN 2006
     -	Added sort hint for ID SOP Instance UID just before filename
	Mahesh Nagarajan		01 FEB 2007
	-	Site customized code will be removed and re-located to GroupMG.site files ->
		starting with 11.5, unless extraction routines are fundamentally flawed, all
		change requests will be incorporated into a site file.
	- 	Modified syntaxes for grouping and sorting hints as part of v11.5 re-write
	-	"All Images" will no longer be created in the sequencing scripts; HRS will take 
		responsibility for generating this sequence
	Mahesh Nagarajan		14 MAY 2007
	-	Sequence presentation of MG changes now; sequences will now be grouped by 
		laterality, projection, spot compression and magnification factor
	-	Such sequences will be sorted on thumbnail group and acquisition time to 
		ensure that image acquired first come up at the top; premium view images 
		will also come at the top when they exist
	Mahesh Nagarajan		17 MAY 2007
	-	Removed the modality prefix and sort suffix from sort names as it looked stupid
	Mahesh Nagarajan		28 MAY 2007
	- 	Bringing back the "All Images" sequence until the corresponding HRS feature gets 
		implemented; this will get one sort order called "Default"
	Mahesh Nagarajan		13 JUN 2007
	-	The new sequence presentation changes are rolled back owing to potential safety 
		issues - rads are used to seeing MG images as one image per sequence; and may 
		miss images that are stacked behind the displayed images. These changes will be 
		re-evaluated once HRS has a better warning system in effect for un-viewed images.
	Mahesh Nagarajan		11 FEB 2008
	-	Adding back site-specific code snippets; after a long discussion with Cristian B., it is 
		understood that this code has to stay in the code until proper site files have been installed. Such 
		code is added back but within "dollar sign" demarcations to ensure easy removal at a later date.
	Mahesh Nagarajan		09 APR 2008
	-	Bringing back the sequecing presentaion where MG images are grouped by common properties 
		rather than image UID; this time deployed as site customization for Gwinnett
	Mahesh Nagarajan		May 22, 2008	
	-	Extending the sequence presentation of MG to Medical Imaging Northwest
	Shudong Liu		Oct. 27, 2008
	-	Add site Glens Falls Hospital into the list where MG images are grouped by
		common properties rather than image UID.(REI10055)
	Shudong Liu		Dec. 9, 2008
	-	Add site Caritas St. Elizabeth's Medical Center into the list where MG images 
		are grouped by common properties rather than image UID.	(REI9653)
	-	Institution name of site Medical Imaging Northwest was changed from 
		"MINW" to "DINW" (REI11115)
	-	Customized for site Saint Luke KC to create thumbnails for MG "For Processing" 
		images. (REI10234)		
	Shudong Liu    Feb. 18, 2009
	- 	Removed "*All Images*" sequence for HMI 11.6	
	Shudong Liu    Mar. 27, 2009
	- 	Ignore "For Processing" MG images. (CRDB73161)		
--]]

function RegisterSortingHintTypes( )
-- ////////////////////////////////////////////////////////////////////////////
-- This function will register the various sort orders that are created for MG
-- ////////////////////////////////////////////////////////////////////////////
    RegisterHintType( "DEFAULT SORT", "Default" )
end

function Execute()
-- ////////////////////////////////////////////////////////////////////////////
-- generates the hints for the sequencer as follows 
-- add IMAGE_UID_TAG as the grouping hint
-- no sorting required, since each image stays in its own sequence
-- ////////////////////////////////////////////////////////////////////////////

    -- If this is the wrong script for the supplied image...
    if (not SanityOK("MG")) then
      return nil
      -- =====================================================
    end
	
	local SiteName = GetAttr(INSTITUTION_NAME_TAG)
	if ( (SiteName~=nil and BestMatchInAttr(SiteName, "Winchester")) or 
		 (SiteName~=nil and BestMatchInAttr(SiteName, "St. Luke's Hospital-Lee's Summit Campus")
						and ValueForTag(DEVICE_MODEL_TAG) == "Hologic Selenia") ) then    
		-- This section accomodates requests by sites like Winchester to show both "FOR PROCESSING" and
		-- "FOR PRESENTATION" images on the thumbnail bar
	
		-- MaxN: It actually doesnt make sense to have a sort-hint for MG; as every MG
		-- sequence has only one image. The code below just ensures that the sort-hint is
		-- not null. This will also ensure that unnecessary errors are not logged.
		local sortHint01 = SortingHint:new( "Default Sort", "DEFAULT SORT" )
		sortHint01:AddEntry(MODALITY_PROP)
		sortHint01:AddEntry(INSTANCE_NUMBER_TAG)
		sortHint01:AddEntry(IMAGE_UID_TAG)
		sortHint01:AddEntry(ALI_IMAGE_FILENAME_TAG)

		-- "Sort Hint Set" created here...
		local sortHintSet01 = SortingHintSet:new( )
		sortHintSet01:AddHint( sortHint01, true ) 
	
		local groupHint01 = GroupingHint:new( ValueForTag( SERIES_DESC_PROP, "?" ) )		
		-- add image UID as the grouping hint ensuring the creation of a new sequence per MG image
		groupHint01:AddEntry(IMAGE_UID_TAG) -- don't check it since it should be always there 

		AddImage2Sequence( "MG Sequence", groupHint01, sortHintSet01 )
	
	elseif (SiteName~=nil and BestWordMatchInAttr(SiteName, "Gwinnett", "MINW", "DINW",
															"Glens Falls Hospital", 
															"Caritas St. Elizabeth's Medical Center",
															"ST ELIZABETH MEDICAL CENTER") ) then   
		-- This section accomodates requests by sites like Gwinnett to show both MG images in a stacked sequence
		-- presentation i.e. images with similar properties like laterality, projection etc are grouped together

		if ((ValueForTag(INTENT_PROP) ~= "FOR PROCESSING")) then

			local sortHint01 = SortingHint:new( "Default Sort", "DEFAULT SORT" )
			sortHint01:AddEntry(THUMBNAIL_GRP_PROP)
			sortHint01:AddEntry(ACQUISITON_DATETIME_TAG)
			sortHint01:AddEntry(ACQUISITON_DATE_TAG)
			sortHint01:AddEntry(ACQUISITON_TIME_TAG)
			sortHint01:AddEntry(IMAGE_ACQUISITION_DATE_TAG)
			sortHint01:AddEntry(IMAGE_ACQUISITON_TIME_TAG)
			sortHint01:AddEntry(INSTANCE_NUMBER_TAG)
			sortHint01:AddEntry(ALI_IMAGE_FILENAME_TAG)
			
			-- "Sort Hint Set" created here...
			local sortHintSet01 = SortingHintSet:new( )
			sortHintSet01:AddHint( sortHint01, true ) 
			
			if (HasAttr(ACQUISITON_TIME_TAG) or HasAttr(IMAGE_ACQUISITON_TIME_TAG)) then
				local sortHint02 = SortingHint:new( "Acquisition Time Sort", "ACQUISITION TIME SORT" )
			    sortHint02:AddEntry( ACQUISITON_DATETIME_TAG, ASCENDING_ORDER )
				sortHint02:AddEntry( ACQUISITON_DATE_TAG, ASCENDING_ORDER )
				sortHint02:AddEntry( ACQUISITON_TIME_TAG, ASCENDING_ORDER )
				sortHint02:AddEntry( IMAGE_ACQUISITION_DATE_TAG, ASCENDING_ORDER )
				sortHint02:AddEntry( IMAGE_ACQUISITON_TIME_TAG, ASCENDING_ORDER )
				
				-- If we are here, then the acquisition time sort was created successfully; adding it to the sort hint set for MR
				sortHintSet01:AddHint( sortHint02 )			
			end
			
			if (HasAttr(INSTANCE_NUMBER_TAG)) then
				local sortHint03 = SortingHint:new( "Image Number Sort", "IMAGE NUMBER SORT" )
				sortHint03:AddEntry( INSTANCE_NUMBER_TAG, ASCENDING_ORDER )
				
				-- If we are here, then the acquisition time sort was created successfully; adding it to the sort hint set for MR
				sortHintSet01:AddHint( sortHint03 )			
			end
			
			local sortHint04 = SortingHint:new( "File Name Sort", "FILE NAME SORT" )
			sortHint04:AddEntry( ALI_IMAGE_FILENAME_TAG, true )
			sortHintSet01:AddHint( sortHint04 ) 
			
			local groupHint01 = GroupingHint:new(ValueForTag( SERIES_DESC_PROP, "?" ))
			
			if( HasAttr(LATERALITY_PROP) ) then
				TraceC("Grouping hint: laterality\n")
				groupHint01:AddEntry(LATERALITY_PROP)
			end
			
			if( HasAttr(MG_PROJECTION_PROP) ) then
				TraceC("Grouping hint: laterality\n")
				groupHint01:AddEntry(MG_PROJECTION_PROP)
			end
			
			if( HasAttr(MG_IMP_DISP_PROP) ) then
				TraceC("Grouping hint: implant displacement\n")
				groupHint01:AddEntry(MG_IMP_DISP_PROP)
			end		
			
			if( HasAttr(MG_MAG_FACTOR_PROP) ) then
				TraceC("Grouping hint: mag. factor\n")
				groupHint01:AddEntry(MG_MAG_FACTOR_PROP)
			end

			if( HasAttr(MG_SPOT_COMP_PROP) ) then
				TraceC("Grouping hint: spot compression\n")
				groupHint01:AddEntry(MG_SPOT_COMP_PROP)
			end
			
			AddImage2Sequence( "MG Sequence", groupHint01, sortHintSet01 )
		end

	else
	-- Default MG sequence presentation: only "FOR PRESENTATION" images are shown and are presented in 
	-- a single image per sequence presentation
	
		if ((ValueForTag(INTENT_PROP) ~= "FOR PROCESSING")) then
			-- MaxN: It actually doesnt make sense to have a sort-hint for MG; as every MG
			-- sequence has only one image. The code below just ensures that the sort-hint is
			-- not null. This will also ensure that unnecessary errors are not logged.
			local sortHint01 = SortingHint:new( "Default Sort", "DEFAULT SORT" )
			sortHint01:AddEntry(MODALITY_PROP)
			sortHint01:AddEntry(INSTANCE_NUMBER_TAG)
			sortHint01:AddEntry(IMAGE_UID_TAG)
			sortHint01:AddEntry(ALI_IMAGE_FILENAME_TAG)
	
			-- "Sort Hint Set" created here...
			local sortHintSet01 = SortingHintSet:new( )
			sortHintSet01:AddHint( sortHint01, true ) 
		
			local groupHint01 = GroupingHint:new( ValueForTag( SERIES_DESC_PROP, "?" ) )		
			-- add image UID as the grouping hint ensuring the creation of a new sequence per MG image
			groupHint01:AddEntry(IMAGE_UID_TAG) -- don't check it since it should be always there 
	
			AddImage2Sequence( "MG Sequence", groupHint01, sortHintSet01 )
		end
	end
end

