<?xml version="1.0" ?> 
<!-- 	JNCC  Creates a series of new entries in the taxon_designation table on the basis of
	a designation_key and a rucksack provided by the user. All list items in the rucksack will
	be assigned a designation (associated with the designation key provided.
	XML Report by Steve WIlkinson JNCC but uses original source generated by Mike Weidelli (Littlefield)
	 
	 Outstanding issues 10/06/09:
	 - SW couldn't make the RAISERROR work?
	 - Should we move the designation key to the first column - means the format of the rest of the file is not relevant?
	
	Version 1 - 27 April 2009  
       
 --> 

<batchupdate menupath="Designations" title="Des2 Assigns Taxons to a designation (using rucksack)"
	description="Uses a rucksack containing a list of taxa and assign them a designation (in the form of a key provided by the user)." >

<SQL>
<Where keytype="Default">
	DECLARE @Taxon_Designation_Key			CHAR(16),		
			@Date							DATETIME,		
			@Taxon_Designation_Type_Key		CHAR(16),		
			@Taxon_List_Item_Key			CHAR(16),
			@UserID							CHAR(16),		
			@Custodian						CHAR(8)			
	
	SET		@Date	=	GETDATE()
	
	/*	Obtain user id. Assumes most recent active session with System Manager rights is the one making the request 
			(doesn't seem to be a better way). */
	SELECT	@UserID 	=	S.User_Name_Key
	FROM	Session	S
	INNER JOIN	"User"	U	ON	S.User_Name_Key	=	U.Name_Key
	WHERE	GetDate()	>=	Date_Time_Start
	AND		S.Date_Time_End IS NULL
	AND		U.Security_Level	=	5	-- System manager
	ORDER BY S.Date_Time_Start DESC
	
	SET		@Taxon_Designation_Type_Key = NULL
	
	SELECT	@Taxon_Designation_Type_Key = Taxon_Designation_Type_Key
	FROM	Taxon_Designation_Type
	WHERE	<Condition name="Designation Type Key" field="Taxon_Designation_Type_Key" operator="equal" type="text" /> 
	
	IF	@Taxon_Designation_Type_Key IS NULL
		RAISERROR	('Taxon Designation Type not found.', 10, 0)
	ELSE
	BEGIN
	
		DECLARE	TLI_Cursor CURSOR FOR 
		(	
			SELECT	Taxon_List_Item_Key
			FROM	TAXON_LIST_ITEM
			WHERE <Condition field= "Taxon_List_Item_key" entrycount ="-1" operator="equal" type="Taxon"  name="Select Rucksack" userucksack = "yes" />

		)

		OPEN	TLI_Cursor
		
		FETCH NEXT FROM TLI_Cursor
		INTO	@Taxon_List_Item_Key

		-- Loops through the list of taxon list items and adds designations for them.
		WHILE @@FETCH_STATUS = 0
		BEGIN
			IF NOT EXISTS(	SELECT	1 
							FROM	Taxon_Designation 
							WHERE	Taxon_Designation_Type_Key	=	@Taxon_Designation_Type_Key
								AND	Taxon_List_Item_Key			=	@Taxon_List_Item_Key		)
			BEGIN
				EXECUTE spNextKey 'Taxon_Designation', @Taxon_Designation_Key OUTPUT
						
				SET		@Custodian = LEFT(@Taxon_Designation_Key, 8 )
				
				INSERT INTO Taxon_Designation	(
						Taxon_Designation_Key,
						Date_From,
						Taxon_Designation_Type_Key,
						Taxon_List_Item_Key,
						Status_Exclusion,
						Entered_By,
						Entry_Date,
						System_Supplied_Data,
						Custodian	)
				VALUES (@Taxon_Designation_Key,
						@Date,
						@Taxon_Designation_Type_Key,
						@Taxon_List_Item_Key,
						0,
						@UserID,
						@Date,
						0,
						@Custodian	)
			END
			
			FETCH NEXT FROM TLI_Cursor
			INTO	@Taxon_List_Item_Key
		END
		
		CLOSE		TLI_Cursor
		DEALLOCATE	TLI_Cursor
	END
	
</Where>
</SQL>
</batchupdate>
