CREATE OR ALTER PROCEDURE [MIG_GENERATE_USERS_PREFERENCES_Sp]
AS BEGIN
	-- The purpose of this stored procedure is to generate/populate the data in [MIG_SW_PROPS] table.

	DECLARE @Itr_UserName NVARCHAR(50)


	EXEC MIG_INSERT_PLM_PREFS_MAPPING_INFO_Sp
	EXEC MIG_INSERT_AUTOCAD_MAPPING_INFO_Sp
	EXEC MIG_INSERT_CATIAV_MAPPING_INFO_Sp
	EXEC MIG_INSERT_CREO_MAPPING_INFO_Sp
	EXEC MIG_INSERT_DRAFTSIGHT_MAPPING_INFO_Sp
	EXEC MIG_INSERT_INVENTOR_MAPPING_INFO_Sp
	EXEC MIG_INSERT_ME_MAPPING_INFO_Sp
	EXEC MIG_INSERT_MSOFFICE_MAPPING_INFO_Sp
	EXEC MIG_INSERT_SOLIDEDGE_MAPPING_INFO_Sp
	EXEC MIG_INSERT_SOLIDWORKS_MAPPING_INFO_Sp
	EXEC MIG_INSERT_TOOLKIT_MAPPING_INFO_Sp
	EXEC MIG_INSERT_UG_MAPPING_INFO_Sp
	EXEC MIG_PROPS_LIST_VALUES_MAPPING_Sp


	-- RESET SOLID WORKS DATA

	DELETE FROM MIG_SOLIDWORKS_PREFERENCES

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_SOLIDWORKS_PREFERENCES([user_id], dsca)
	SELECT MIG_USERS.LN_USER, 'Migration'
	FROM MIG_USERS
	WHERE (MIG_USERS.USER_NAME  =  'system' OR MIG_USERS.LN_USER  <>  'PLMADMIN')

	-- RESET SOLID WORKS DATA - END

	
	-- RESET TOOLKIT DATA

	DELETE FROM MIG_TOOLKIT_PREFERENCES

	
	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_TOOLKIT_PREFERENCES([user_id], dsca)
	SELECT MIG_USERS.LN_USER, 'Migration'
	FROM MIG_USERS
	WHERE (MIG_USERS.USER_NAME  =  'system' OR MIG_USERS.LN_USER  <>  'PLMADMIN')

	-- RESET TOOLKIT DATA - END

	-- RESET PLM PREF DATA

	DELETE FROM MIG_PLM_PREFERENCES

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_PLM_PREFERENCES([user_id], dsca)
	SELECT MIG_USERS.LN_USER, 'Migration'
	FROM MIG_USERS
	WHERE (MIG_USERS.USER_NAME  =  'system' OR MIG_USERS.LN_USER  <>  'PLMADMIN')

	-- RESET PLM PREF DATA - END

	
	-- RESET INVENTOR PREF DATA

	DELETE FROM MIG_INVENTOR_PREFERENCES

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_INVENTOR_PREFERENCES([user_id], dsca)
	SELECT MIG_USERS.LN_USER, 'Migration'
	FROM MIG_USERS
	WHERE (MIG_USERS.USER_NAME  =  'system' OR MIG_USERS.LN_USER  <>  'PLMADMIN')

	-- RESET INVENTOR PREF DATA - END

	-- *******************************************************************************

	-- RESET PROE/CREO PREF DATA

	DELETE FROM MIG_CREO_PREFERENCES

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_CREO_PREFERENCES([user_id], dsca)
	SELECT MIG_USERS.LN_USER, 'Migration'
	FROM MIG_USERS
	WHERE (MIG_USERS.USER_NAME  =  'system' OR MIG_USERS.LN_USER  <>  'PLMADMIN')

	-- RESET PROE/CREO PREF DATA - END


	-- RESET MSOFFICE PREF DATA

	DELETE FROM MIG_MSOFFICE_PREFERENCES

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_MSOFFICE_PREFERENCES([user_id], dsca)
	SELECT MIG_USERS.LN_USER, 'Migration'
	FROM MIG_USERS
	WHERE (MIG_USERS.USER_NAME  =  'system' OR MIG_USERS.LN_USER  <>  'PLMADMIN')

	-- RESET MSOFFICE PREF DATA - END


	-- RESET AUTOCAD PREF DATA

	DELETE FROM MIG_AUTOCAD_PREFERENCES

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_AUTOCAD_PREFERENCES([user_id], dsca)
	SELECT MIG_USERS.LN_USER, 'Migration'
	FROM MIG_USERS
	WHERE (MIG_USERS.USER_NAME  =  'system' OR MIG_USERS.LN_USER  <>  'PLMADMIN')

	-- RESET AUTOCAD PREF DATA - END


	-- RESET UG PREF DATA

	DELETE FROM MIG_UG_PREFERENCES

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_UG_PREFERENCES([user_id], dsca)
	SELECT MIG_USERS.LN_USER, 'Migration'
	FROM MIG_USERS
	WHERE (MIG_USERS.USER_NAME  =  'system' OR MIG_USERS.LN_USER  <>  'PLMADMIN')

	-- RESET UG PREF DATA - END


	-- RESET SOLIDEDGE PREF DATA

	DELETE FROM MIG_SOLIDEDGE_PREFERENCES

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_SOLIDEDGE_PREFERENCES([user_id], dsca)
	SELECT MIG_USERS.LN_USER, 'Migration'
	FROM MIG_USERS
	WHERE (MIG_USERS.USER_NAME  =  'system' OR MIG_USERS.LN_USER  <>  'PLMADMIN')

	-- RESET SOLIDEDGE PREF DATA - END


	-- RESET CATIAV5 PREF DATA

	DELETE FROM MIG_CATIAV_PREFERENCES

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_CATIAV_PREFERENCES([user_id], dsca)
	SELECT MIG_USERS.LN_USER, 'Migration'
	FROM MIG_USERS
	WHERE (MIG_USERS.USER_NAME  =  'system' OR MIG_USERS.LN_USER  <>  'PLMADMIN')

	-- RESET CATIAV5 PREF DATA - END


	-- RESET DRAFTSIGHT PREF DATA

	DELETE FROM MIG_DRAFTSIGHT_PREFERENCES

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_DRAFTSIGHT_PREFERENCES([user_id], dsca)
	SELECT MIG_USERS.LN_USER, 'Migration'
	FROM MIG_USERS
	WHERE (MIG_USERS.USER_NAME  =  'system' OR MIG_USERS.LN_USER  <>  'PLMADMIN')

	-- RESET DRAFTSIGHT PREF DATA - END


	-- RESET ME10 PREF DATA

	DELETE FROM MIG_ME_PREFERENCES

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_ME_PREFERENCES([user_id], dsca)
	SELECT MIG_USERS.LN_USER, 'Migration'
	FROM MIG_USERS
	WHERE (MIG_USERS.USER_NAME  =  'system' OR MIG_USERS.LN_USER  <>  'PLMADMIN')

	-- RESET ME10 PREF DATA - END


	--Update preferences for all users (admin level)
	--exec MIG_SET_USERS_PREFERENCES_Sp 'SOLIDWORKS', ''
	--exec MIG_SET_USERS_PREFERENCES_Sp 'TOOLKIT', ''
	--exec MIG_SET_USERS_PREFERENCES_Sp 'WEBPLM', ''
	--exec MIG_SET_USERS_PREFERENCES_Sp 'INVENTOR', ''
	--exec MIG_SET_USERS_PREFERENCES_Sp 'PROE', '' 
	--exec MIG_SET_USERS_PREFERENCES_Sp 'MSOFFICE', ''
	--exec MIG_SET_USERS_PREFERENCES_Sp 'AUTOCAD', ''
	--exec MIG_SET_USERS_PREFERENCES_Sp 'UG', ''
	--exec MIG_SET_USERS_PREFERENCES_Sp 'SOLIDEDGE', ''
	--exec MIG_SET_USERS_PREFERENCES_Sp 'CATIAV5', ''
	--exec MIG_SET_USERS_PREFERENCES_Sp 'DRAFTSIGHT', ''
	--exec MIG_SET_USERS_PREFERENCES_Sp 'ME10', ''

	exec MIG_UPDATE_USERS_PREFERENCE_Sp 'SOLIDWORKS', ''
	exec MIG_UPDATE_USERS_PREFERENCE_Sp 'TOOLKIT', ''
	exec MIG_UPDATE_USERS_PREFERENCE_Sp 'WEBPLM', ''
	exec MIG_UPDATE_USERS_PREFERENCE_Sp 'INVENTOR', ''
	exec MIG_UPDATE_USERS_PREFERENCE_Sp 'PROE', '' 
	exec MIG_UPDATE_USERS_PREFERENCE_Sp 'MSOFFICE', ''
	exec MIG_UPDATE_USERS_PREFERENCE_Sp 'AUTOCAD', ''
	exec MIG_UPDATE_USERS_PREFERENCE_Sp 'UG', ''
	exec MIG_UPDATE_USERS_PREFERENCE_Sp 'SOLIDEDGE', ''
	exec MIG_UPDATE_USERS_PREFERENCE_Sp 'CATIAV5', ''
	exec MIG_UPDATE_USERS_PREFERENCE_Sp 'DRAFTSIGHT', ''
	exec MIG_UPDATE_USERS_PREFERENCE_Sp 'ME10', ''


	DECLARE RecordsCursor CURSOR  
	LOCAL FORWARD_ONLY FOR  
		SELECT 
			USERS.USER_NAME
		FROM USERS 
		JOIN MIG_USERS 
		ON MIG_USERS.USER_NAME = USERS.USER_NAME 
		WHERE (MIG_USERS.USER_NAME  =  'system' OR MIG_USERS.LN_USER  <>  'PLMADMIN')

	OPEN RecordsCursor  
	WHILE 1 = 1   
	BEGIN
		FETCH NEXT FROM RecordsCursor INTO @Itr_UserName
		IF @@FETCH_STATUS <> 0
			BREAK

		--exec MIG_SET_USERS_PREFERENCES_Sp 'SOLIDWORKS', @Itr_UserName
		--exec MIG_SET_USERS_PREFERENCES_Sp 'TOOLKIT', @Itr_UserName
		--exec MIG_SET_USERS_PREFERENCES_Sp 'WEBPLM', @Itr_UserName	
		--exec MIG_SET_USERS_PREFERENCES_Sp 'INVENTOR', @Itr_UserName
		--exec MIG_SET_USERS_PREFERENCES_Sp 'PROE', @Itr_UserName
		--exec MIG_SET_USERS_PREFERENCES_Sp 'MSOFFICE', @Itr_UserName
		--exec MIG_SET_USERS_PREFERENCES_Sp 'AUTOCAD', @Itr_UserName
		--exec MIG_SET_USERS_PREFERENCES_Sp 'UG', @Itr_UserName
		--exec MIG_SET_USERS_PREFERENCES_Sp 'SOLIDEDGE', @Itr_UserName
		--exec MIG_SET_USERS_PREFERENCES_Sp 'CATIAV5', @Itr_UserName
		--exec MIG_SET_USERS_PREFERENCES_Sp 'DRAFTSIGHT', @Itr_UserName
		--exec MIG_SET_USERS_PREFERENCES_Sp 'ME10', @Itr_UserName

		
		exec MIG_UPDATE_USERS_PREFERENCE_Sp 'SOLIDWORKS', @Itr_UserName
		exec MIG_UPDATE_USERS_PREFERENCE_Sp 'TOOLKIT', @Itr_UserName
		exec MIG_UPDATE_USERS_PREFERENCE_Sp 'WEBPLM', @Itr_UserName	
		exec MIG_UPDATE_USERS_PREFERENCE_Sp 'INVENTOR', @Itr_UserName
		exec MIG_UPDATE_USERS_PREFERENCE_Sp 'PROE', @Itr_UserName
		exec MIG_UPDATE_USERS_PREFERENCE_Sp 'MSOFFICE', @Itr_UserName
		exec MIG_UPDATE_USERS_PREFERENCE_Sp 'AUTOCAD', @Itr_UserName
		exec MIG_UPDATE_USERS_PREFERENCE_Sp 'UG', @Itr_UserName
		exec MIG_UPDATE_USERS_PREFERENCE_Sp 'SOLIDEDGE', @Itr_UserName
		exec MIG_UPDATE_USERS_PREFERENCE_Sp 'CATIAV5', @Itr_UserName
		exec MIG_UPDATE_USERS_PREFERENCE_Sp 'DRAFTSIGHT', @Itr_UserName
		exec MIG_UPDATE_USERS_PREFERENCE_Sp 'ME10', @Itr_UserName


	END

	CLOSE RecordsCursor
END

-- exec MIG_GENERATE_USERS_PREFERENCES_Sp
-- SELECT * FROM MIG_SOLIDWORKS_PREFERENCES
-- SELECT * FROM MIG_TOOLKIT_PREFERENCES
-- SELECT * FROM MIG_PLM_PREFERENCES
  




