CREATE FUNCTION MIG_GET_PROJ_PREF_VALUE_Fn
(
    @InputProjectID NVARCHAR(MAX)
   ,@TargetPropName NVARCHAR(MAX) 
   ,@TargetDomain NVARCHAR(MAX) 
   ,@TargetDataType NVARCHAR(MAX)
   ,@LegacyClassName NVARCHAR(MAX)
   ,@LegacyPropName NVARCHAR(MAX)
   ,@DefaultValue NVARCHAR(MAX) 
)
RETURNS INT
AS
BEGIN
	DECLARE @result NVARCHAR(MAX)
	SET @result = NULL

	SELECT 
		@result = 
			CASE 
				WHEN PS.ZZ_ID IS NOT NULL THEN
					CAST(PS.PREF_VALUE AS INT)
				WHEN CS.ZZ_ID IS NOT NULL THEN
					CAST(CS.PREF_VALUE AS INT)
				WHEN AC.ZZ_ID IS NOT NULL THEN
					CAST(AC.PREF_VALUE AS INT)
				ELSE
					NULL
			END
	FROM PROJECT
	LEFT JOIN PROJ_CONFIG		PS --(By Project)
	  ON PS.PROJECT_ID = @InputProjectID
	 AND PS.CLASS_ID = @LegacyClassName
	 AND PS.PREF_NAME = @LegacyPropName
	LEFT JOIN PROJ_CONFIG		CS --(By Class)
	  ON CS.PROJECT_ID = '*'
	 AND CS.CLASS_ID = @LegacyClassName
	 AND CS.PREF_NAME = @LegacyPropName
	LEFT JOIN PROJ_CONFIG		AC --(For All Classes)
	  ON AC.PROJECT_ID = '*'
	 AND AC.CLASS_ID = '*'
	 AND AC.PREF_NAME = @LegacyPropName
	WHERE PROJECT.PROJECT_ID = @InputProjectID

	--IF @result IS NOT NULL
	--	SET @result = CAST(@DefaultValue AS INT)
	--ELSE
	--	SET @result = CAST(@DefaultValue AS INT)

	--This table curretly has only 4 domains.
	--DOMAIN
	--pdproj
	--pdvlt.ckin
	--pdvlt.cout
	--pdyesno
	IF @result IS NOT NULL
		BEGIN
			IF @TargetDomain = 'pdvlt.ckin' --UNCH_IN_COPY_LINKS
				IF @result = '0' --Keep All Links
					SET @result = 10
				ELSE IF @result = '3' --Check-In All Objects
					SET @result = 20
				ELSE IF @result = '4' --Check-In Selected Objects
					SET @result = 20
				ELSE 
					SET @result = @DefaultValue
			ELSE IF @TargetDomain = 'pdvlt.cout'	--UNCH_COPY_LINKS		+		UNCH_COPY_FILE_LINKS
				IF @result = '0'					--No Copy						--No Copy
					SET @result = 10
				ELSE IF @result = '1'				--Copy All Links				--Copy All Links
					SET @result = 20
				ELSE IF @result = '2'				--Copy Selected Links			--	
					SET @result = 20
				ELSE IF @result = '3'				--Check-Out All Objects			--Check-Out All Objects
					SET @result = 30
				ELSE IF @result = '4'				--Check-Out Selected Objects	--Check-Out Selected Objects
					SET @result = 30
				ELSE IF @result = '5'				--								--Check-Out Files and Copy All Child Document Files
					SET @result = 30
				ELSE IF @result = '6'				--								--Check-Out Files and Copy Selected Child Document Files
					SET @result = 30
				ELSE
					SET @result = @DefaultValue
			ELSE IF @TargetDomain = 'pdyesno'
				IF @result = '1'
					SET @result = 1
				ELSE 
					SET @result = 2
		END

	If @result IS NULL
		BEGIN
			SET @result = @DefaultValue
		END

	RETURN @result
END



