-- 1.1.16.137 SP1 update scripts

--PR 111 - customer issue
	--- Clean up created roles for those operators which used to have CA4K Administration roles and changed while update to 1.1.16 -------
	UPDATE Operators
	SET RoleID = '82B19834-4EF0-405B-9AD1-23860AAF4139'
	FROM Operators AS O INNER JOIN Roles AS R ON O.RoleID = R.RoleID
	WHERE R.RoleName LIKE 'New Role (%'

	DELETE RolePartitions
	FROM RolePartitions AS RP INNER JOIN Roles AS R ON RP.RoleID = R.RoleID
	WHERE R.RoleName LIKE 'New Role (%'


	DELETE RolePlugins 
	FROM RolePlugins AS RP INNER JOIN Roles AS R ON RP.RoleID = R.RoleID
	WHERE RoleName LIKE 'New Role (%'

	DELETE Roles
	WHERE RoleName LIKE 'New Role (%'
	------------------------------------------------------------------------------------------------------------------------------------------

	-------- SET CA4K Administration role to admin operator if not exists -----
	IF NOT EXISTS (SELECT TOP 1 1 FROM Operators WHERE OperatorID = '1EEB696C-0E28-4702-8598-C379C95B45E1' AND RoleID = '82B19834-4EF0-405B-9AD1-23860AAF4139')
	BEGIN
	UPDATE Operators SET RoleID = '82B19834-4EF0-405B-9AD1-23860AAF4139' WHERE OperatorID = '1EEB696C-0E28-4702-8598-C379C95B45E1'
	END

	-------- Delete all partitions for CA4K Administration Role except for Admin Partition -------------
	DELETE FROM RolePartitions
	WHERE RoleID = '82B19834-4EF0-405B-9AD1-23860AAF4139'
	AND PartitionID <> '3DC78C7B-C9CE-4485-A835-63D577F623E6'

	-------- Insert missing CA4K Administration Role to RolePartitions table
	IF NOT EXISTS (SELECT RoleID FROM RolePartitions WHERE RoleID = '82B19834-4EF0-405B-9AD1-23860AAF4139' AND PartitionID = '3DC78C7B-C9CE-4485-A835-63D577F623E6')
	BEGIN
	INSERT INTO RolePartitions (PartitionID, RoleID, Restriction, LastUpdated) VALUES ('3DC78C7B-C9CE-4485-A835-63D577F623E6', '82B19834-4EF0-405B-9AD1-23860AAF4139', 0, GETUTCDATE())
	END

	-------- Clean OperatorPartitions Table ----------------
	DELETE FROM OperatorPartitions
	WHERE OperatorID IN
	(
	SELECT O.OperatorID --, OP.PartitionID
	FROM OperatorPartitions OP
	INNER JOIN Operators O ON O.OperatorID = OP.OperatorID
	WHERE
	O.RoleID = '82B19834-4EF0-405B-9AD1-23860AAF4139'
	AND O.OperatorID <> '1EEB696C-0E28-4702-8598-C379C95B45E1'
	)
	AND PartitionID <> '3DC78C7B-C9CE-4485-A835-63D577F623E6'

	------- Check If Admin Partition is available for the CA4K Administration Role's Operators If not insert data into OperatorPartitions -------------
	INSERT INTO OperatorPartitions(PartitionID, OperatorID, IsPrimaryPartition)
	SELECT DISTINCT '3DC78C7B-C9CE-4485-A835-63D577F623E6' AS PartitionID, O.OperatorID, 1 AS IsPrimaryPartition FROM Operators O
	WHERE O.RoleID = '82B19834-4EF0-405B-9AD1-23860AAF4139'
	AND NOT EXISTS (SELECT TOP 1 1 FROM OperatorPartitions WHERE OperatorPartitions.OperatorID = O.OperatorID AND OperatorPartitions.PartitionID = '3DC78C7B-C9CE-4485-A835-63D577F623E6')

	------- Set Admin Partition to Primary Partition ----------------
	UPDATE OperatorPartitions
	SET IsPrimaryPartition = 1
	WHERE OperatorID IN
	(
		SELECT O.OperatorID FROM OperatorPartitions OP
		INNER JOIN Operators O ON O.OperatorID = OP.OperatorID
		WHERE
		O.RoleID = '82B19834-4EF0-405B-9AD1-23860AAF4139'
		AND OP.PartitionID = '3DC78C7B-C9CE-4485-A835-63D577F623E6'
	)


--PR 112 (5) - customer issue
IF NOT EXISTS (SELECT 1 FROM [ScreenObjects] WHERE [ScreenObjectID] = 145 AND [ScreenID] = 15)
BEGIN
	INSERT INTO [ScreenObjects] ( [ScreenObjectID], [ScreenID], [ObjectName], [ObjectLabel], [ObjectType], [LangObjectName]) VALUES ( 145, 15, 'Partition Filter', 'cboPartitionFind', 'Infragistics.Win.UltraWinEditors.UltraComboEditor','String_Partition_Filter') 
END

