UPDATE TBIDB_BASE SET CONFIG_VALUE = '1.1.2' where CONFIG_NAME = 'DB_VERSION'
GO
CREATE PROCEDURE [dbo].[PRIDB_DELETE_IDB_OBJECT_FINALLY] @pIDB_OBJ_ID BIGINT 
AS
							
BEGIN
		DELETE FROm TBIDB_CONVERSATION_MESSAGE WHERE CONV_ID IN (SELECT GUID FROM TBIDB_CONVERSATION_OBJECT WHERE IDB_OBJ_ID = @pIDB_OBJ_ID);
		DELETE FROm TBIDB_CONVERSATION_USER WHERE CONV_ID IN (SELECT GUID FROM TBIDB_CONVERSATION_OBJECT WHERE IDB_OBJ_ID = @pIDB_OBJ_ID);
		DELETE FROM TBIDB_CONVERSATION_OBJECT WHERE IDB_OBJ_ID = @pIDB_OBJ_ID;
		DELETE FROM TBIDB_ACCESSRIGHT WHERE IDB_OBJ_ID = @pIDB_OBJ_ID;
		DELETE FROM TBIDB_LANGUAGE_TERM WHERE OBJECT_ID in (SELECT GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @pIDB_OBJ_ID )
		DELETE FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @pIDB_OBJ_ID;
		DELETE FROM TBIDB_OBJECT_METADATA_DELETE WHERE IDB_OBJ_ID = @pIDB_OBJ_ID;
		DELETE FROM TBIDB_OBJECT_METADATA_CHANGE WHERE IDB_OBJ_ID = @pIDB_OBJ_ID;
		DELETE FROM TBIDB_OBJECT_METADATA_ADD WHERE IDB_OBJ_ID = @pIDB_OBJ_ID;
		DELETE FROM TBIDB_OBJECT_BE WHERE IDB_OBJ_ID = @pIDB_OBJ_ID;
		DELETE FROM TBIDB_OBJECT_STRUCTURE_TYPE WHERE IDB_OBJ_ID = @pIDB_OBJ_ID;
		--DELETE FROM TBIDB_FILE_OBJECT WHERE IDB_OBJ_ID = @pIDB_OBJ_ID
		DELETE FROM TBIDB_DOC_INFO WHERE IDB_OBJ_ID = @pIDB_OBJ_ID;
		
		DELETE FROM TBIDB_OBJECT WHERE IDB_OBJ_ID = @pIDB_OBJ_ID;

END
GO
EXEC PRIDB_NEW_IDB_STATE 'IDBObject deleted1', 'SYSTEM';
GO
CREATE OR ALTER VIEW VWIDB_OBJ_STATES AS 
SELECT 
	A.GUID ID
	,A.MY_OBJECT_ID AS IDB_OBJ_ID
	,C.LANG_ID
	,LANG.LANG_CODE
	,TERM.TERM_VALUE AS STATE_LANG 
	,A.ADDED_WHEN
FROM
	TBIDB_MYOBJECT_STATES A INNER JOIN TBIDB_STATES B ON A.STATE_ID = B.GUID 
	INNER JOIN TBIDB_LANGUAGE_TERM C ON B.GUID = C.OBJECT_ID
	INNER JOIN TBIDB_LANGUAGE LANG ON C.LANG_ID = LANG.GUID
	INNER JOIN TBIDB_TERM_VALUE_VARCHAR TERM ON C.TERM_ID = TERM.GUID
WHERE 
	C.KIND_TYPE = 3
	GO
ALTER PROCEDURE [dbo].[PRIDB_OBJECT_SET_DELETED] @IDB_OBJECT BIGINT, @WHO VARCHAR(100)
AS
BEGIN
	UPDATE TBIDB_OBJECT SET DELETED = 1,CHANGED_WHO = @WHO WHERE IDB_OBJ_ID = @IDB_OBJECT;
	EXEC PRIDB_OBJECT_SET_STATE @IDB_OBJECT,'IDBObject deleted1',@WHO;
END
GO
USE [IDB]
GO

--CHANGED	28.06.2023	MS
ALTER   PROCEDURE [dbo].[PRIDB_DELETE_TERM_OBJECT_METADATA] @IDB_OBJ_ID BIGINT, @ATTRIBUTE VARCHAR(100), @TERM VARCHAR(900), @WHO VARCHAR(100),@LANG_CODE VARCHAR(5) = 'de-DE',@ID_ISFOREIGN as BIT = False
AS
BEGIN
	DECLARE 
		@ATTR_ID INTEGER,
		@TYP_ID TINYINT,
		@TYP_DESCR VARCHAR(100),
		@MULTI BIT,
		@LANG_ID	INT
	--IF @ID_ISFOREIGN = 1
	--	BEGIN
	--		SELECT @IDB_OBJ_ID = IDB_OBJ_ID FROM TBIDB_OBJECT WHERE REFERENCE_KEY = @IDB_OBJ_ID
	--	END

	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(T.TITLE) = UPPER(@ATTRIBUTE)

	SELECT @LANG_ID = GUID FROM TBIDB_LANGUAGE WHERE LANG_CODE = @LANG_CODE

	DECLARE @MY_TERM_ID BIGINT
	EXEC PRIDB_NEW_TERM @TYP_DESCR,@TERM,'',@TERM_ID = @MY_TERM_ID OUTPUT;
	
	PRINT '@MY_TERM_ID: ' + CONVERT(VARCHAR(10),@MY_TERM_ID)

	IF @TYP_ID IN (1,8) --VARCHAR
		BEGIN
			PRINT 'Attribute is varchar...'
			IF EXISTS(SELECT MD.GUID FROM TBIDB_OBJECT_METADATA MD INNER JOIN TBIDB_LANGUAGE_TERM LT ON MD.GUID = LT.[OBJECT_ID] WHERE MD.IDB_OBJ_ID = @IDB_OBJ_ID AND MD.ATTR_ID = @ATTR_ID
			AND LT.TERM_ID = @MY_TERM_ID)
				BEGIN
					DELETE FROM TBIDB_LANGUAGE_TERM WHERE [OBJECT_ID] = (SELECT GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID)
					AND TERM_ID = @MY_TERM_ID;
					PRINT 'PRIDB_DEL_TERM_OBJECT_METADATA: DELETED TEXT ROW!'
					INSERT INTO TBIDB_OBJECT_METADATA_DELETE (IDB_OBJ_ID,ATTR_ID,TEXT_TERM_ID,DELETED_WHO, ADDED_WHO,ADDED_WHEN) 
					SELECT @IDB_OBJ_ID,@ATTR_ID, @MY_TERM_ID,@WHO,ADDED_WHO,ADDED_WHEN  FROM TBIDB_OBJECT_METADATA
						 WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID;
				END
			ELSE
				PRINT 'TERM SEEMS NOT TO EXIST'
		END
	ELSE IF @TYP_ID IN (2,7,9) --BIGINT
		BEGIN
			IF EXISTS(SELECT GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND INT_TERM_ID = @MY_TERM_ID)
				BEGIN
					DELETE FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND INT_TERM_ID = @MY_TERM_ID
					PRINT 'PRIDB_DEL_TERM_OBJECT_METADATA: DELETED INTEGER ROW!'
					INSERT INTO TBIDB_OBJECT_METADATA_DELETE (IDB_OBJ_ID,ATTR_ID,INT_TERM_ID,DELETED_WHO, ADDED_WHO,ADDED_WHEN) 
					SELECT @IDB_OBJ_ID,@ATTR_ID, @MY_TERM_ID,@WHO,ADDED_WHO,ADDED_WHEN  FROM TBIDB_OBJECT_METADATA
						 WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND INT_TERM_ID = @MY_TERM_ID
				END
		END
	ELSE IF @TYP_ID = 3 --FLOAT
		BEGIN
			IF EXISTS(SELECT GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND FLOAT_TERM_ID = @MY_TERM_ID)
				BEGIN
					DELETE FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND FLOAT_TERM_ID = @MY_TERM_ID
					PRINT 'PRIDB_DEL_TERM_OBJECT_METADATA: DELETED FLOAT ROW!'
					INSERT INTO TBIDB_OBJECT_METADATA_DELETE (IDB_OBJ_ID,ATTR_ID,FLOAT_TERM_ID,DELETED_WHO, ADDED_WHO,ADDED_WHEN) 
					SELECT @IDB_OBJ_ID,@ATTR_ID, @MY_TERM_ID,@WHO,ADDED_WHO,ADDED_WHEN  FROM TBIDB_OBJECT_METADATA
						 WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND FLOAT_TERM_ID = @MY_TERM_ID
				END
		END
	ELSE IF @TYP_ID = 4 --DECIMAL
		BEGIN
			IF EXISTS(SELECT GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND DEC_TERM_ID = @MY_TERM_ID)
				BEGIN
					DELETE FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND DEC_TERM_ID = @MY_TERM_ID
					PRINT 'PRIDB_DEL_TERM_OBJECT_METADATA: DELETED DECIMAL ROW!'
					INSERT INTO TBIDB_OBJECT_METADATA_DELETE (IDB_OBJ_ID,ATTR_ID,DEC_TERM_ID,DELETED_WHO, ADDED_WHO,ADDED_WHEN) 
					SELECT @IDB_OBJ_ID,@ATTR_ID, @MY_TERM_ID,@WHO,ADDED_WHO,ADDED_WHEN  FROM TBIDB_OBJECT_METADATA
						 WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND DEC_TERM_ID = @MY_TERM_ID
				END
		END
	ELSE IF @TYP_ID = 5 --DATE
		BEGIN
			IF EXISTS(SELECT GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND DATE_TERM_ID = @MY_TERM_ID)
				BEGIN
					DELETE FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND DATE_TERM_ID = @MY_TERM_ID
					PRINT 'PRIDB_DEL_TERM_OBJECT_METADATA: DELETED DATE ROW!'
					INSERT INTO TBIDB_OBJECT_METADATA_DELETE (IDB_OBJ_ID,ATTR_ID,DATE_TERM_ID,DELETED_WHO, ADDED_WHO,ADDED_WHEN) 
					SELECT @IDB_OBJ_ID,@ATTR_ID, @MY_TERM_ID,@WHO,ADDED_WHO,ADDED_WHEN  FROM TBIDB_OBJECT_METADATA
						 WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND DATE_TERM_ID = @MY_TERM_ID
				END
		END

	
END
