UPDATE TBIDB_BASE SET CONFIG_VALUE = '1.1.0' where CONFIG_NAME = 'DB_VERSION'
GO
ALTER TABLE TBIDB_DOC_INFO
ADD RELATIVE_PATH_ONLY_TERM BIGINT;
ALTER TABLE TBIDB_DOC_INFO
ADD EXTENSION_TERM BIGINT;

DECLARe @RELPATH VARCHAR(5000),@IDBOBJID BIGINT,@Extens VARCHAR(10)
DECLARE cursor1 CURSOR FOR 
	Select IDB_OBJ_ID, RELATIVE_PATH_ONLY,EXTENSION from TBIDB_DOC_INFO
  OPEN cursor1 
    FETCH NEXT FROM cursor1 INTO @IDBOBJID,@RELPATH,@Extens
    WHILE @@FETCH_STATUS = 0
	BEGIN
		DECLARE @TERMID BIGINT 
		EXECUTE [dbo].[PRIDB_NEW_TERM] 'Varchar',@RELPATH,'Digital Data', @TERM_ID = @TERMID OUTPUT
		DECLARE @TERM_EXTENS BIGINT 
		EXECUTE [dbo].[PRIDB_NEW_TERM] 'Varchar',@Extens,'Digital Data', @TERM_ID = @TERM_EXTENS OUTPUT

		UPDATE TBIDB_DOC_INFO SET RELATIVE_PATH_ONLY_TERM = @TERMID, EXTENSION_TERM = @TERM_EXTENS, CHANGED_WHO = 'Digital Data'  WHERE IDB_OBJ_ID = @IDBOBJID
		FETCH NEXT FROM cursor1 INTO @IDBOBJID,@RELPATH,@Extens
	END
CLOSE cursor1
DEALLOCATE cursor1
GO
ALTER TABLE TBIDB_DOC_INFO ALTER COLUMN RELATIVE_PATH_ONLY_TERM BIGINT NOT NULL;
ALTER TABLE TBIDB_DOC_INFO ALTER COLUMN EXTENSION_TERM BIGINT NOT NULL;
ALTER TABLE TBIDB_DOC_INFO
ADD CONSTRAINT FKTBIDB_DOC_INFO_RELATIVEPATHONLYTERM FOREIGN KEY (RELATIVE_PATH_ONLY_TERM) REFERENCES TBIDB_TERM_VALUE_VARCHAR(GUID); 
ALTER TABLE TBIDB_DOC_INFO
ADD CONSTRAINT FKTBIDB_DOC_INFO_EXTENSIONTERM FOREIGN KEY (EXTENSION_TERM) REFERENCES TBIDB_TERM_VALUE_VARCHAR(GUID); 
ALTER TABLE TBIDB_DOC_INFO
DROP COLUMN RELATIVE_PATH_ONLY;
ALTER TABLE TBIDB_DOC_INFO
DROP COLUMN EXTENSION;
USE [IDB_TEST]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		DD MS
-- Changed: 01.11.2021	MS	TERM_COLUMNS
-- Changed: 10.08.2020	MS	Anpassung
-- =============================================
ALTER PROCEDURE [dbo].[PRIDB_NEW_DOCUMENT] @OBJ_ST_ID INT, @REL_PATH VARCHAR(512),@WHO VARCHAR(100), @REF_DOCID BIGINT = 0, @BE VARCHAR(500) = '', @IDB_OBJ_ID BIGINT OUTPUT
AS
BEGIN
	DECLARE 
	@SHARE_PATH		VARCHAR(100),
	@OBJ_STORE_PATH	VARCHAR(250),
	@NEW_FULL_PATH	VARCHAR(512),
	@FILENAME_EXT	VARCHAR(250),
	@FILENAME_ONLY	VARCHAR(250),
	@RELATIVE_PATH	VARCHAR(512),
	@RELATIVE_PATH_ONLY		VARCHAR(512),
	@EXT			VARCHAR(10)

	SELECT 
		@SHARE_PATH = T.CAT_STRING, @OBJ_STORE_PATH =  T1.OBJ_PATH 
	FROM TBIDB_CATALOG T, TBIDB_OBJECT_STORE T1 WHERE T1.CAT_ID = T.GUID AND T1.GUID = @OBJ_ST_ID
	SET @NEW_FULL_PATH =  UPPER(@SHARE_PATH + '\' + @OBJ_STORE_PATH + '\' + @REL_PATH)
	SET @NEW_FULL_PATH = REPLACE(@NEW_FULL_PATH,'\\','')
	SET @RELATIVE_PATH =  UPPER(@REL_PATH)
	PRINT 'PRIDB_NEW_DOCUMENT - @NEW_FULL_PATH: ' + @NEW_FULL_PATH
	PRINT 'PRIDB_NEW_DOCUMENT - @RELATIVE_PATH: ' + @RELATIVE_PATH
	
	--SELECT @RELATIVE_PATH_ONLY = [dbo].[FNGetDirectoryPath] (@NEW_FULL_PATH);
	--PRINT 'PRIDB_NEW_DOCUMENT - @RELATIVE_PATH_ONLY1: ' + @RELATIVE_PATH_ONLY
	
	SELECT @FILENAME_EXT = [dbo].[FNGetFileName] (@NEW_FULL_PATH);
	SELECT @FILENAME_ONLY = [dbo].[FNGetFileNameWithoutExtension] (@NEW_FULL_PATH);
	SELECT @EXT = UPPER([dbo].[FNGetExtension] (@NEW_FULL_PATH));
	SET @RELATIVE_PATH_ONLY = REPLACE(@REL_PATH,'\' + @FILENAME_EXT,'')
	PRINT 'PRIDB_NEW_DOCUMENT - @RELATIVE_PATH_ONLY: ' + @RELATIVE_PATH_ONLY
	DECLARE @RELATIVE_PATH_ONLY_TERM BIGINT,@EXTENSION_TERM BIGINT
	EXECUTE [dbo].[PRIDB_NEW_TERM] 'Varchar',@RELATIVE_PATH_ONLY,@WHO, @TERM_ID = @RELATIVE_PATH_ONLY_TERM OUTPUT
	EXECUTE [dbo].[PRIDB_NEW_TERM] 'Varchar',@EXT,@WHO, @TERM_ID = @EXTENSION_TERM OUTPUT
	--PRINT '@EXT: ' + @EXT
	--IF NOT EXISTS(SELECT [IDB_OBJ_ID] FROM [TBIDB_DOC_INFO] WHERE [OBJ_ST_ID] =  @OBJ_ST_ID AND UPPER([RELATIVE_PATH]) = @RELATIVE_PATH)
	EXEC PRIDB_NEW_OBJECT 'DOC',@WHO,@BE,@REF_DOCID, @IDB_OBJ_ID = @IDB_OBJ_ID OUTPUT;
	PRINT 'PRIDB_NEW_DOCUMENT - @IDB_OBJ_ID from PRIDB_NEW_OBJECT: ' + CONVERT(VARCHAR(20),@IDB_OBJ_ID)
	BEGIN
		IF NOT EXISTS(SELECT IDB_OBJ_ID FROM [TBIDB_DOC_INFO] WHERE IDB_OBJ_ID = @IDB_OBJ_ID)
			INSERT INTO [dbo].[TBIDB_DOC_INFO]
				   ([IDB_OBJ_ID]
				   ,[OBJ_ST_ID]
				   ,[FILENAME_ONLY]
				   ,RELATIVE_PATH
				   ,RELATIVE_PATH_ONLY_TERM
				   ,EXTENSION_TERM
				   ,[ADDED_WHO])
			VALUES
				   (@IDB_OBJ_ID
				   ,@OBJ_ST_ID
				   ,@FILENAME_ONLY
				   ,@RELATIVE_PATH
				   ,@RELATIVE_PATH_ONLY_TERM
				   ,@EXTENSION_TERM
				   ,@WHO)
		ELSE
			BEGIN
				PRINT 'OBJECT ' + CONVERT(VARCHAR(20),@IDB_OBJ_ID) + ' ALREADY EXISTING ---- So ONLY UPDATE WITH NEW FILE PARAMS'
				UPDATE [TBIDB_DOC_INFO] SET [FILENAME_EXT] = @FILENAME_EXT,[FILENAME_ONLY] = @FILENAME_ONLY
					,@EXTENSION_TERM = @EXTENSION_TERM
					,[RELATIVE_PATH] = @RELATIVE_PATH
					,RELATIVE_PATH_ONLY_TERM = @RELATIVE_PATH_ONLY_TERM
					,CHANGED_WHO = @WHO
				WHERE IDB_OBJ_ID = @IDB_OBJ_ID
				IF EXISTS(SELECT IDB_OBJ_ID FROM TBIDB_OBJECT WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND DELETED = 1)
					BEGIN
						PRINT 'PRIDB_NEW_DOCUMENT - IDB_OBJ_ID: ' + CONVERT(VARCHAR(20),@IDB_OBJ_ID) + ' DELETED FLAG ACTIVE!!'
						UPDATE TBIDB_OBJECT SET DELETED	= 0 WHERE IDB_OBJ_ID = @IDB_OBJ_ID
					END
			END
	END
END
GO
