UPDATE TBIDB_BASE SET CONFIG_VALUE = '1.1.1' where CONFIG_NAME = 'DB_VERSION'
GO
ALTER VIEW [dbo].[VWIDB_BE_ATTRIBUTE] AS
SELECT 
	T.[BE_ID]
	,T2.TITLE AS BE
	,[ATTR_ID]
	,T.DEFAULT_SEARCH_ATTRIBUTE
	,T6.TERM_VALUE AS ATTR_TITLE
	,T1.TYP_ID
	,T1.TYP_ID AS [TYPE_ID]
	,T3.NAME_TYPE AS [TYPE_NAME]
	,T1.VIEW_VISIBLE
	,T1.VIEW_SEQUENCE
	,T5.GUID AS LANG_ID
	,T5.LANG_CODE
	,T.ADDED_WHEN
	,T.ADDED_WHO
	,T.CHANGED_WHEN
	,T.CHANGED_WHO
  FROM 
	[TBIDB_BE_ATTRIBUTE] T INNER JOIN TBIDB_ATTRIBUTE T1 ON T.ATTR_ID = T1.GUID
	INNER JOIN TBIDB_BUSINESS_ENTITY T2 ON T.BE_ID = T2.GUID
	INNER JOIN TBIDB_ATTRIBUTE_TYPE T3 ON T1.TYP_ID = T3.GUID
	INNER JOIN TBIDB_ATTRIBUTE_LANGUAGE T4 ON T1.GUID = T4.ATTRIBUTE_ID
	INNER JOIN TBIDB_LANGUAGE T5 ON T4.LANGUAGE_ID = T5.GUID
	INNER JOIN TBIDB_TERM_VALUE_VARCHAR T6 ON T4.TERM_ID = T6.GUID
  WHERE  
	T1.DELETED = 0 
UNION
SELECT 
	T.[BE_ID]
	,T2.TITLE AS BE
	,[ATTR_ID]
	,T.DEFAULT_SEARCH_ATTRIBUTE
	,T6.TERM_VALUE AS ATTR_TITLE
	,T1.TYP_ID
	,T1.TYP_ID AS [TYPE_ID]
	,T3.NAME_TYPE AS [TYPE_NAME]
	,T1.VIEW_VISIBLE
	,T1.VIEW_SEQUENCE
	,T5.GUID AS LANG_ID
	,T5.LANG_CODE
	,T.ADDED_WHEN
	,T.ADDED_WHO
	,T.CHANGED_WHEN
	,T.CHANGED_WHO
  FROM 
GO

ALTER   PROCEDURE [dbo].[PRIDB_CREATE_VIEW_DOC_DATA] @pLANGCODE VARCHAR(5) ,@pVar VARCHAR(15) = 'DEFAULT'
AS
DECLARE
	@ATTR_ID	INT,
	@TITLE	VARCHAR(100),
	@TYPID	INT,
	@SQL1		NVARCHAR(MAX),
	@Date		Varchar(20),
	@Bool4		Varchar(1),
	@Integer24	Varchar(20),
	@Float		Varchar(20),
	@RELPATH	Varchar(200),
	@SEQUENCE	Integer,
	@NAME_VIEW	VARCHAR(20)

	IF @pVar = 'DEFAULT'
		SET @NAME_VIEW = 'VWIDB_DOC_DATA_'
	ELSE
		SET @NAME_VIEW = 'VWIDB_DOCRESULT_'
	SET @SQL1 = 'CREATE VIEW ' + @NAME_VIEW + UPPER(LEFT(@pLANGCODE,2)) + ' AS '+ char(10) + 'SELECT ' + char(10) + char(9) 

	IF @pVar = 'DEFAULT'
		SET @SQL1 += +'T.IDB_OBJ_ID' + char(10)
	ELSE
		SET @SQL1 += +'T.IDB_OBJ_ID as DocID' + char(10)

	SET @SQL1 += char(9) + ',DT.Doctype as Doctype' + char(10)
BEGIN								
	DECLARE c_PRIDB_CREATE_VIEW_DOC_DATA CURSOR FOR 
		SELECT DISTINCT  ATTR_ID,ATTR_TITLE,TYPE_ID,VIEW_SEQUENCE
		FROM VWIDB_BE_ATTRIBUTE WHERE VIEW_VISIBLE = 1 AND LANG_CODE = @pLANGCODE AND ATTR_ID IS NOT NULL
		ORDER BY VIEW_SEQUENCE
  
	OPEN c_PRIDB_CREATE_VIEW_DOC_DATA 
		FETCH NEXT FROM c_PRIDB_CREATE_VIEW_DOC_DATA INTO @ATTR_ID,@TITLE,@TYPID,@SEQUENCE
		WHILE @@FETCH_STATUS = 0
		BEGIN
			DECLARE @FN_COMMAND as VARCHAR(100), @FN_TYP as VARCHAR(50)
			IF @TYPID IN (1,8)
				BEGIN
					SET @FN_COMMAND = '(SELECT [dbo].[FNIDB_GET_TERM_@TYPE] (@OBJ_ID,@ATTR_ID,''@LANG_CODE''))'
					SET @FN_COMMAND = REPLACE(@FN_COMMAND,'@LANG_CODE',@pLANGCODE)
				END
			ELSE
				SET @FN_COMMAND = '(SELECT [dbo].[FNIDB_GET_TERM_@TYPE] (@OBJ_ID,@ATTR_ID))'
			SET @FN_COMMAND = REPLACE(@FN_COMMAND,'@OBJ_ID','T.IDB_OBJ_ID')
			SET @FN_COMMAND = REPLACE(@FN_COMMAND,'@ATTR_ID',@ATTR_ID)
			
			
			IF @TYPID = 1
				SET @FN_TYP = 'TEXT' 
			ELSE IF @TYPID = 2
				SET @FN_TYP = 'INTEGER'
			ELSE IF @TYPID = 3
				SET @FN_TYP = 'FLOAT'
			ELSE IF @TYPID = 4
				SET @FN_TYP = 'DECIMAL'
			ELSE IF @TYPID = 5
				SET @FN_TYP = 'DATE'
			ELSE IF @TYPID = 6
				SET @FN_TYP = 'DATE_TIME'
			ELSE IF @TYPID = 7
				SET @FN_TYP = 'BIT'
			ELSE IF @TYPID = 8
				SET @FN_TYP = 'TEXT_VECTOR'
			ELSE IF @TYPID = 8
				SET @FN_TYP = 'INTEGER_VECTOR'
			
			SET @FN_COMMAND = REPLACE(@FN_COMMAND,'@TYPE',@FN_TYP)

			--PRINT '@FN_COMMAND: ' + @FN_COMMAND
			SET @SQL1 += char(9) + ',' + @FN_COMMAND + char(9) + char(9) + ' AS ''' + @TITLE + '''' + char(10)
			--PRINT @SQL1
			FETCH NEXT FROM c_PRIDB_CREATE_VIEW_DOC_DATA INTO @ATTR_ID,@TITLE,@TYPID,@SEQUENCE
		END
	CLOSE c_PRIDB_CREATE_VIEW_DOC_DATA
	DEALLOCATE c_PRIDB_CREATE_VIEW_DOC_DATA
	IF @pVar = 'DEFAULT'
		BEGIN
			SET @SQL1 += char(9) + ',T5.TERM_VALUE AS DocExtension' + char(10)
			+ char(9) + ',T5.TERM_VALUE AS DocName' + char(10)
			+ char(9) + ',CONCAT(T4.TERM_VALUE,''\'',T5.TERM_VALUE) AS DocRelativePath' + char(10)
			+ char(9) + ',T4.TERM_VALUE AS DocRelativePathOnly' + char(10)
		END
	SET @SQL1 += char(9) + ',T.ADDED_WHEN AS [Added when]' + char(10)
		+ char(9) + ',T.ADDED_WHO AS [Added who]' + char(10)
		+ char(9) + ',T.CHANGED_WHEN_SUBSTRUCTURE AS [Changed when]' + char(10)
		+ char(9) + ',T.CHANGED_WHO_SUBSTRUCTURE AS [Changed who]' + char(10)

	SET @SQL1 += 'FROM' + char(10)
	IF @pVar = 'DEFAULT'
		SET @SQL1 += char(9)+'TBIDB_OBJECT T INNER JOIN TBIDB_FILE_OBJECT T2 ON T.IDB_OBJ_ID = T2.IDB_OBJ_ID' + char(10)
		+ char(9) + 'INNER JOIN VWIDB_DOC_DOCTYPE DT ON T.IDB_OBJ_ID = DT.IDB_OBJ_ID' + char(10)
		+ char(9) + 'INNER JOIN TBIDB_OBJECT_STORE T3 ON T2.OBJ_ST_ID = T3.GUID' + char(10)
		+ char(9) + 'INNER JOIN TBIDB_TERM_VALUE_VARCHAR T4 ON T2.RELPATH_TERM_ID = T4.GUID' + char(10)
		+ char(9) + 'INNER JOIN TBIDB_TERM_VALUE_VARCHAR T5 ON T2.FILENAME_TERM_ID = T5.GUID' + char(10)
	ELSE
		SET @SQL1 += char(9) + 'TBIDB_OBJECT T ' + char(10)
		+ char(9) + 'INNER JOIN VWIDB_DOC_DOCTYPE DT ON T.IDB_OBJ_ID = DT.IDB_OBJ_ID' + char(10)

	SET @SQL1 += 'WHERE'  + char(10)
	+ char(9) + 'DT.LANG_CODE = ''' + @pLANGCODE + ''' AND' + char(10) 
	+ char(9) + 'T.KIND_TYPE_ID = 1 AND T.DELETED = 0 AND T.ACTIVE = 1'
	PRINT @SQL1
	IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = @NAME_VIEW + UPPER(LEFT(@pLANGCODE,2)))
			BEGIN
				DECLARE @DELETE NVARCHAR(100)
				SET @DELETE = 'DROP VIEW [dbo].[' + @NAME_VIEW + UPPER(LEFT(@pLANGCODE,2)) + ']'
				EXEC sp_executesql @DELETE
			END
	EXEC sp_executesql @SQL1
END
GO
DELETE FROM TBIDB_CATALOG WHERE CAT_TITLE IN ('AHWF_CMD_LAYOUT_SELECT','AHWF_CMD_USR_SELECT');
DELETE FROM TBIDB_COMMON_SQL WHERE TITLE IN ('AHWF_CMD_LAYOUT_SELECT','AHWF_CMD_USR_SELECT','RECENT_FILES_USER');
INSERT INTO TBIDB_COMMON_SQL (TITLE,SQL_COMMAND) VALUES ('AHWF_CMD_LAYOUT_SELECT','SELECT NAME + '', ''  + PRENAME AS FULLNAME,EMAIL,USERNAME FROM DD_ECM.dbo.TBDD_USER WHERE ACTIVE = 1')
GO
INSERT INTO TBIDB_COMMON_SQL (TITLE,SQL_COMMAND) VALUES ('AHWF_CMD_USR_SELECT','SELECT NAME + '', ''  + PRENAME AS FULLNAME,EMAIL,USERNAME FROM TBDD_USER')
GO
INSERT INTO TBIDB_COMMON_SQL (TITLE,SQL_COMMAND) VALUES ('RECENT_FILES_USER','--3 Columns are needed: 1 DocID, 2 Description1, 3 Description2
SELECT TOP 10 DocID,Doctype,DisplayFileName FROM VWIDB_DOCRESULT_DE WHERE [Changed who] = ''@UserName'' ORDER BY [Changed when] DESC')
GO
INSERT INTO TBIDB_COMMON_SQL (TITLE,SQL_COMMAND) VALUES ('ZF_DISPLAY_PM_JOBS_INDIVIDUAL','SELECT COUNT(A.DOC_ID) FROM  (SELECT DISTINCT DOC_ID FROM TBPM_PROFILE_FILES WHERE PROFIL_ID = 1) A')
GO
INSERT INTO TBIDB_COMMON_SQL (TITLE,SQL_COMMAND) VALUES ('ZF_DISPLAY_PM_JOBS_GROUP','SELECT COUNT(A.DOC_ID) FROM  (SELECT DISTINCT DOC_ID FROM TBPM_PROFILE_FILES WHERE PROFIL_ID IN (SELECT PROFILE_ID FROM [dbo].[FNPM_GET_ACTIVE_PROFILES_USER] (@USR_ID))) A')
GO
