UPDATE TBIDB_BASE SET CONFIG_VALUE = '1.0.6' where CONFIG_NAME = 'DB_VERSION'
GO
/*
REPLACE @WORKFILESTORE WITH STORAGE-PATH FOR WORK
*/
CREATE VIEW [dbo].[VWIDB_CHANGE_LOG] AS
select 
	T.GUID AS ChangeID,
	T.IDB_OBJ_ID, 
	T1.TITLE as Attribute,  
	T3.TERM_VALUE as [Old value],
	T5.TERM_VALUE as [New value], 
	T.CHANGED_WHO [Changed who],
	T.CHANGED_WHEN [Changed when]
from TBIDB_OBJECT_METADATA_CHANGE T 
	INNER JOIN TBIDB_ATTRIBUTE T1 ON T.ATTR_ID = T1.GUID 
	INNER JOIN TBIDB_TERM_LANGUAGE T2 ON T.OLD_TEXT_TERM_LANG_ID = T2.GUID 
	INNER JOIN TBIDB_TERM_VALUE_VARCHAR T3 ON T2.TEXT_TERM_ID = T3.GUID
	INNER JOIN TBIDB_TERM_LANGUAGE T4 ON T.NEW_TEXT_TERM_LANG_ID = T4.GUID 
	INNER JOIN TBIDB_TERM_VALUE_VARCHAR T5 ON T4.TEXT_TERM_ID = T5.GUID
GO

ALTER TABLE TBIDB_OBJECT ADD PARENT_OBJ_ID BIGINT NOT NULL DEFAULT 0
GO 
ALTER TABLE TBIDB_OBJECT ADD IS_VERSION BIT NOT NULL DEFAULT 0
GO 
ALTER TABLE TBIDB_BE_ATTRIBUTE ADD DEFAULT_SEARCH_ATTRIBUTE BIT NOT NULL DEFAULT 0
GO 
-- =======================================================
-- Author:		Digital Data, DD MS
-- Create Date: 22.10.2020
-- Gunction returns all values for parameterized Attribute
-- =======================================================
CREATE FUNCTION [dbo].[FNIDB_PM_GET_VARIABLE_VALUE_FOR_ALL_OBJECTS] (@ATTRIBUTE VARCHAR(100),@LANG_CODE VARCHAR(10)) 
RETURNS @Output TABLE (IDB_OBJ_ID BIGINT,TERM_VALUE VARCHAR(900))
AS
BEGIN
	DECLARE 
		@ATTR_ID INTEGER,
		@TYP_ID TINYINT,
		@TYP_DESCR VARCHAR(100),
		@MULTI BIT,
		@LANG_ID INT,
		@TERM_RESULT VARCHAR(900)
	SELECT @LANG_ID = GUID FROM TBIDB_LANGUAGE WHERE LANG_CODE = @LANG_CODE
	SELECT @ATTR_ID = T.GUID, @TYP_ID = T.TYP_ID, @MULTI = T.MULTI_CONTEXT,@TYP_DESCR = T1.NAME_TYPE FROM TBIDB_ATTRIBUTE T, TBIDB_ATTRIBUTE_TYPE T1
	WHERE T.TYP_ID = T1.GUID AND UPPER(TITLE) = UPPER(@ATTRIBUTE)

	IF @TYP_ID IN (1,8) --VARCHAR
		BEGIN
			DECLARE @TL_ID BIGINT
			INSERT INTO @Output (IDB_OBJ_ID,TERM_VALUE) 
			SELECT OBJ.IDB_OBJ_ID,TERM.TERM_VALUE FROM 
				TBIDB_TERM_VALUE_VARCHAR TERM INNER JOIN TBIDB_TERM_LANGUAGE TLANG ON TERM.GUID = TLANG.TEXT_TERM_ID
				INNER JOIN TBIDB_OBJECT_METADATA MD ON TLANG.GUID = MD.TEXT_TERM_LANG_ID
				INNER JOIN TBIDB_OBJECT OBJ ON MD.IDB_OBJ_ID = OBJ.IDB_OBJ_ID
			WHERE 
				MD.ATTR_ID = @ATTR_ID 
				AND TLANG.LANGUAGE_ID = @LANG_ID
				AND OBJ.DELETED = 0 
			ORDER BY OBJ.IDB_OBJ_ID DESC
		END
	ELSE IF @TYP_ID IN (2,7,9) --BIGINT
		BEGIN
			INSERT INTO @Output (IDB_OBJ_ID,TERM_VALUE) 
			SELECT OBJ.IDB_OBJ_ID,TERM.TERM_VALUE 
			FROM 
				TBIDB_TERM_VALUE_INTEGER TERM INNER JOIN TBIDB_OBJECT_METADATA MD ON MD.INT_TERM_ID = TERM.GUID
				INNER JOIN TBIDB_OBJECT OBJ ON MD.IDB_OBJ_ID = OBJ.IDB_OBJ_ID
			WHERE 
				MD.ATTR_ID = @ATTR_ID 
				AND OBJ.DELETED = 0 
			ORDER BY OBJ.IDB_OBJ_ID DESC
		END
	ELSE IF @TYP_ID = 3 --FLOAT
		BEGIN
			INSERT INTO @Output (IDB_OBJ_ID,TERM_VALUE) 
			SELECT OBJ.IDB_OBJ_ID,TERM.TERM_VALUE 
			FROM 
				TBIDB_TERM_VALUE_FLOAT TERM INNER JOIN TBIDB_OBJECT_METADATA MD ON MD.FLOAT_TERM_ID = TERM.GUID
				INNER JOIN TBIDB_OBJECT OBJ ON MD.IDB_OBJ_ID = OBJ.IDB_OBJ_ID
			WHERE 
				MD.ATTR_ID = @ATTR_ID 
				AND OBJ.DELETED = 0 ORDER BY OBJ.IDB_OBJ_ID DESC
		END
	ELSE IF @TYP_ID = 4 
		BEGIN
			INSERT INTO @Output (IDB_OBJ_ID,TERM_VALUE) 
			SELECT OBJ.IDB_OBJ_ID,TERM.TERM_VALUE 
			FROM 
				TBIDB_TERM_VALUE_DECIMAL TERM INNER JOIN TBIDB_OBJECT_METADATA MD ON MD.DEC_TERM_ID = TERM.GUID
				INNER JOIN TBIDB_OBJECT OBJ ON MD.IDB_OBJ_ID = OBJ.IDB_OBJ_ID
			WHERE 
				MD.ATTR_ID = @ATTR_ID 
				AND OBJ.DELETED = 0 
			ORDER BY OBJ.IDB_OBJ_ID DESC
		END
	ELSE IF @TYP_ID = 5 
		BEGIN
			INSERT INTO @Output (IDB_OBJ_ID,TERM_VALUE) 
			SELECT OBJ.IDB_OBJ_ID,TERM.TERM_VALUE 
			FROM 
				TBIDB_TERM_VALUE_DATE TERM INNER JOIN TBIDB_OBJECT_METADATA MD ON MD.DATE_TERM_ID = TERM.GUID
				INNER JOIN TBIDB_OBJECT OBJ ON MD.IDB_OBJ_ID = OBJ.IDB_OBJ_ID
			WHERE
				MD.ATTR_ID = @ATTR_ID AND OBJ.DELETED = 0 
			ORDER BY OBJ.IDB_OBJ_ID DESC
		END
RETURN
END
GO

CREATE NONCLUSTERED INDEX [IDX_ATTR-ID_OBJ-INTTERMID]
ON [dbo].[TBIDB_OBJECT_METADATA] ([ATTR_ID])
INCLUDE ([IDB_OBJ_ID],[INT_TERM_ID])
GO
CREATE NONCLUSTERED INDEX IDXATTR_IDINT_TERM_ID_IDB_OBJ_ID
ON [dbo].[TBIDB_OBJECT_METADATA] ([ATTR_ID],[INT_TERM_ID])
INCLUDE ([IDB_OBJ_ID])
GO
CREATE NONCLUSTERED INDEX IDX_DELETED_OBJID
ON [dbo].[TBIDB_OBJECT] ([DELETED])
INCLUDE ([IDB_OBJ_ID])
GO
CREATE NONCLUSTERED INDEX [IDXATTR_ID-IDB_OBJ_ID-FLOAT_TERM_ID]
ON [dbo].[TBIDB_OBJECT_METADATA] ([ATTR_ID])
INCLUDE ([IDB_OBJ_ID],[FLOAT_TERM_ID])
GO
CREATE NONCLUSTERED INDEX [ATTR_ID_IDB_OBJ_ID_DEC_TERM_ID]
ON [dbo].[TBIDB_OBJECT_METADATA] ([ATTR_ID])
INCLUDE ([IDB_OBJ_ID],[DEC_TERM_ID])
GO
CREATE NONCLUSTERED INDEX [ATTR_ID-IDB_OBJ_ID-DATE_TERM_ID]
ON [dbo].[TBIDB_OBJECT_METADATA] ([ATTR_ID])
INCLUDE ([IDB_OBJ_ID],[DATE_TERM_ID])
GO
CREATE NONCLUSTERED INDEX [IDXATTR_ID-DATE_TERM_ID-IDB_OBJ_ID]
ON [dbo].[TBIDB_OBJECT_METADATA] ([ATTR_ID],[DATE_TERM_ID])
INCLUDE ([IDB_OBJ_ID])
GO
--DROP TABLE TBIDB_ACCESSRIGHT
CREATE TABLE TBIDB_ACCESSRIGHT
(
	GUID				BIGINT		    NOT NULL 	IDENTITY (1, 1),
	IDB_OBJ_ID			BIGINT			NOT NULL,
	USR_ID				INTEGER			NOT NULL,
	ACCESSRIGHT			INTEGER			NOT NULL,
	COMMENT				VARCHAR(250),
	ADDED_WHO			BIGINT			NOT NULL,
	ADDED_WHEN			DATETIME		DEFAULT GETDATE(),
	CONSTRAINT PK_TBIDB_ACCESSRIGHT_GUID    PRIMARY KEY (GUID),
	CONSTRAINT FK_TBIDB_ACCESSRIGHT_IDB_OBJ_ID FOREIGN KEY (IDB_OBJ_ID)  REFERENCES  TBIDB_OBJECT (IDB_OBJ_ID),
	CONSTRAINT FK_TBIDB_ACCESSRIGHT_ADDED_WHO FOREIGN KEY (ADDED_WHO)  REFERENCES  TBIDB_TERM_VALUE_VARCHAR (GUID),
	CONSTRAINT UQ_TBIDB_ACCESSRIGHT UNIQUE(IDB_OBJ_ID,USR_ID,ACCESSRIGHT)
)
GO

INSERT INTO [TBIDB_CATALOG] ([CAT_TITLE],[CAT_STRING]) VALUES ('APPSERV1','172.24.12.39')
GO

DECLARE
	@IDB_OBJ_ID			BIGINT,
	@USERNAME		VARCHAR(500)		
								
DECLARE cursorDoc CURSOR FOR 
	select IDB_OBJ_ID FROm IDB_TEST.DBO.TBIDB_OBJECT WHERE DELETED = 0
  OPEN cursorDoc 
    FETCH NEXT FROM cursorDoc INTO @IDB_OBJ_ID
    WHILE @@FETCH_STATUS = 0
	BEGIN
		DELETE FROM TBIDB_ACCESSRIGHT where IDB_OBJ_ID = @IDB_OBJ_ID;
		INSERT INTO TBIDB_ACCESSRIGHT (IDB_OBJ_ID,USR_ID,ACCESSRIGHT,ADDED_WHO) 
		SELECT @IDB_OBJ_ID,GUID,1,20242 FROM DD_ECM_TEST.dbo.TBDD_USER WHERE GUID <> 1
		INSERT INTO TBIDB_ACCESSRIGHT (IDB_OBJ_ID,USR_ID,ACCESSRIGHT,ADDED_WHO) 
		SELECT @IDB_OBJ_ID,GUID,4,20242 FROM DD_ECM_TEST.dbo.TBDD_USER WHERE GUID = 1

		FETCH NEXT FROM cursorDoc INTO @IDB_OBJ_ID
	END
CLOSE cursorDoc
DEALLOCATE cursorDoc

CREATE TABLE TBIDB_REFRESH_ON_APPSERVER
(
	GUID				BIGINT		    NOT NULL 	IDENTITY (1, 1),
	IDB_OBJ_ID			BIGINT			NOT NULL,
	USR_ID				INTEGER			NOT NULL,
	ACCESSRIGHT			INTEGER			NOT NULL,
	ADDED_WHO			BIGINT			NOT NULL,
	ADDED_WHEN			DATETIME		DEFAULT GETDATE(),
	CONSTRAINT PKTBIDB_REFRESH_ON_APPSERVER_GUID    PRIMARY KEY (GUID),
	CONSTRAINT FKTBIDB_REFRESH_ON_APPSERVER_IDB_OBJ_ID FOREIGN KEY (IDB_OBJ_ID)  REFERENCES  TBIDB_OBJECT (IDB_OBJ_ID),
	CONSTRAINT FKTBIDB_REFRESH_ON_APPSERVER_ADDED_WHO FOREIGN KEY (ADDED_WHO)  REFERENCES  TBIDB_TERM_VALUE_VARCHAR (GUID),
	CONSTRAINT UQ_TBIDB_REFRESH_ON_APPSERVER UNIQUE(IDB_OBJ_ID,USR_ID)
)
GO

USE [IDB_TEST]
GO
CREATE NONCLUSTERED INDEX IDX_TBIDB_ACCESSRIGHT_USRID
ON [dbo].[TBIDB_ACCESSRIGHT] ([USR_ID])
INCLUDE ([IDB_OBJ_ID])
GO

ALTER VIEW [dbo].[VWIDB_BE_ATTRIBUTE] AS
SELECT 
	T.[BE_ID]
	,T2.TITLE AS BE
	,[ATTR_ID]
	,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 
GO
--CREATE TABLE TBIDB_FILESTORE_CONFIG
--(
--	GUID				BIGINT		    NOT NULL 	IDENTITY (1, 1),
--	TITLE				VARCHAR(100)	NOT NULL,
--	STORAGE_PATH		VARCHAR(1000)	NOT NULL,
--	ACTIVE				BIT				NOT NULL	DEFAULT 0,
--	ADDED_WHO			VARCHAR(100),
--	ADDED_WHEN			DATETIME		DEFAULT GETDATE(),
--	CHANGED_WHO			VARCHAR(100),
--	CHANGED_WHEN		DATETIME,
--	CONSTRAINT PKTBIDB_FILESTORE_CONFIG_GUID    PRIMARY KEY (GUID),
--	CONSTRAINT UQ_TBIDB_FILESTORE_CONFIG_TITLE UNIQUE(TITLE),
--	CONSTRAINT UQ_TBIDB_FILESTORE_CONFIG_STORAGE_PATH UNIQUE(STORAGE_PATH),
--)
--GO
--CREATE TRIGGER [dbo].[TBIDB_FILESTORE_CONFIG_AFT_UPD] ON [dbo].[TBIDB_FILESTORE_CONFIG]
--FOR UPDATE
--AS
--  UPDATE TBIDB_FILESTORE_CONFIG SET CHANGED_WHEN = GETDATE()
--  FROM INSERTED
--  WHERE TBIDB_FILESTORE_CONFIG.GUID = INSERTED.GUID
--GO
--INSERT INTO TBIDB_FILESTORE_CONFIG (TITLE,STORAGE_PATH,ACTIVE,ADDED_WHO) VALUES ('WORK','@WORKFILESTORE',1,'Digital Data');
DROP TABLE TBIDB_FLOW_SEARCH_CATEGORY_ATTRIBUTES
CREATE TABLE TBIDB_FLOW_SEARCH_CATEGORY_ATTRIBUTES
(
	GUID				BIGINT		    NOT NULL 	IDENTITY (1, 1),
	ATTRIBUTE_ID		INTEGER			NOT NULL,
	ACTIVE				BIT				NOT NULL	DEFAULT 1,
	ADDED_WHO			VARCHAR(100)	NOT NULL	DEFAULT 'Digital Data (DEF)',
	ADDED_WHEN			DATETIME		DEFAULT GETDATE(),
	CHANGED_WHO			VARCHAR(100),
	CHANGED_WHEN		DATETIME,
	CONSTRAINT PKTBIDB_FLOW_SEARCH_CATEGORY_ATTRIBUTES_GUID    PRIMARY KEY (GUID),
	CONSTRAINT UQ_TBIDB_FLOW_SEARCH_CATEGORY_ATTRIBUTES UNIQUE(ATTRIBUTE_ID),
	CONSTRAINT FK_TBIDB_FLOW_SEARCH_CATEGORY_ATTRIBUTES FOREIGN KEY (ATTRIBUTE_ID)  REFERENCES  TBIDB_ATTRIBUTE (GUID),
)
GO
INSERT INTO TBIDB_FLOW_SEARCH_CATEGORY_ATTRIBUTES (ATTRIBUTE_ID) VALUES(1);

ALTER VIEW VWIDB_CATEGORIES_PER_OBJECT_AND_LANGUAGE AS
select 
	MD.IDB_OBJ_ID
	,TLANG.LANGUAGE_ID
	,TERM.TERM_VALUE
	,ATTR.TITLE AS ATTRIBUTE_TITLE
	,ATTR.GUID AS ATTRIBUTE_ID
FROM 
	TBIDB_TERM_VALUE_VARCHAR TERM INNER JOIN TBIDB_TERM_LANGUAGE TLANG ON TERM.GUID = TLANG.TEXT_TERM_ID
	INNER JOIN TBIDB_LANGUAGE_TERM_METADATA T1 ON TLANG.GUID = T1.LANG_TERM_ID
	INNER JOIN TBIDB_OBJECT_METADATA MD ON T1.GUID = MD.TEXT_TERM_LANG_ID
	INNER JOIN TBIDB_FLOW_SEARCH_CATEGORY_ATTRIBUTES CAT_ATTR ON MD.ATTR_ID = CAT_ATTR.ATTRIBUTE_ID
	INNER JOIN TBIDB_ATTRIBUTE ATTR ON CAT_ATTR.ATTRIBUTE_ID = ATTR.GUID
WHERE
	CAT_ATTR.ACTIVE = 1








