CREATE PROCEDURE [MIG_GET_PROJ_CONFIG_PREF_VALUE_Sp]
(
	 @InputProjectID NVARCHAR(MAX),
	 @FieldName NVARCHAR(MAX),
	 @OutputPrefValue INT OUTPUT
)
AS BEGIN
	DECLARE @DomainName NVARCHAR(MAX)
	DECLARE @DefaultValue NVARCHAR(MAX)
	DECLARE @PrefName NVARCHAR(MAX)
	
	SET @OutputPrefValue = NULL

	--           PROJECT_ID    CLASS_ID      PREF_NAME                  PREF_VALUE (Sample)
	--           *             *             CHILD_LINKS_IN             1
	--           *             DOC           CHILD_LINKS_IN             2
	--           P1            DOC           CHILD_LINKS_IN             3

	SELECT 
		@DomainName = MIG_PROJ_CONFIG_PREF_NAMES_MAPPING.Domain,
		@DefaultValue = MIG_PROJ_CONFIG_PREF_NAMES_MAPPING.DefaultValue,
		@PrefName = MIG_PROJ_CONFIG_PREF_NAMES_MAPPING.LegacyPrefName,
		@OutputPrefValue = CASE 
							WHEN PROJECT_SPECIFIC.ZZ_ID IS NOT NULL THEN
								PROJECT_SPECIFIC.PREF_VALUE
							WHEN CLASS_SPECFIC.ZZ_ID IS NOT NULL THEN
								CLASS_SPECFIC.PREF_VALUE
							WHEN ALL_CLASS.ZZ_ID IS NOT NULL THEN
								ALL_CLASS.PREF_VALUE
							ELSE
								NULL
						END
	FROM PROJECT
	JOIN MIG_PROJ_CONFIG_PREF_NAMES_MAPPING 
	  ON MIG_PROJ_CONFIG_PREF_NAMES_MAPPING.Field = @FieldName
	LEFT JOIN PROJ_CONFIG PROJECT_SPECIFIC
	  ON PROJECT_SPECIFIC.PROJECT_ID = PROJECT.PROJECT_ID
	 AND PROJECT_SPECIFIC.CLASS_ID = MIG_PROJ_CONFIG_PREF_NAMES_MAPPING.Class
	 AND PROJECT_SPECIFIC.PREF_NAME = MIG_PROJ_CONFIG_PREF_NAMES_MAPPING.LegacyPrefName
	LEFT JOIN PROJ_CONFIG CLASS_SPECFIC
	  ON CLASS_SPECFIC.PROJECT_ID = '*'
	 AND CLASS_SPECFIC.CLASS_ID = MIG_PROJ_CONFIG_PREF_NAMES_MAPPING.Class
	 AND CLASS_SPECFIC.PREF_NAME = MIG_PROJ_CONFIG_PREF_NAMES_MAPPING.LegacyPrefName
	LEFT JOIN PROJ_CONFIG ALL_CLASS
	  ON ALL_CLASS.PROJECT_ID = '*'
	 AND ALL_CLASS.CLASS_ID = '*'
	 AND ALL_CLASS.PREF_NAME = MIG_PROJ_CONFIG_PREF_NAMES_MAPPING.LegacyPrefName
	WHERE PROJECT.PROJECT_ID = @InputProjectID

	
	IF @DefaultValue = 'pdvlt.cout.copy.all.links' AND @PrefName = ''
		BEGIN
				SET @OutputPrefValue = '20'
		END

END




