
UPDATE TBIDB_BASE SET CONFIG_VALUE = '1.0.8' where CONFIG_NAME = 'DB_VERSION'
GO
-- =============================================
-- Author:		DD MS
-- Creation date: 05.07.2021
-- =============================================
CREATE PROCEDURE [dbo].[PRIDB_GET_VALUE_DT] @IDB_OBJ_ID BIGINT, @LANG_CODE VARCHAR(5)
AS
DECLARE @ATTR_ID INTEGER,@ATTR_TITLE VARCHAR(100), @TYPE_NAME VARCHAR(100)
DECLARE		
	@TBRESULT TABLE (AttributID INT, AttributTitle VARCHAR(100), AttributType VARCHAR(100),ValueBigInt BIGINT, ValueText Varchar(912), ValueDec Decimal(19,2), ValueDate Date,
ADDED_WHEN DATETIME, CHANGED_WHEN DATETIME)

DECLARE cursorAttributes CURSOR LOCAL FOR 
	SELECT A.ATTR_ID, B.ATTR_TITLE, B.TYPE_NAME FROM TBIDB_OBJECT_METADATA A INNER JOIN VWIDB_BE_ATTRIBUTE B On A.ATTR_ID = B.ATTR_ID 
	WHERE A.IDB_OBJ_ID = @IDB_OBJ_ID AND B.LANG_CODE = @LANG_CODE
OPEN cursorAttributes 
FETCH NEXT FROM cursorAttributes INTO @ATTR_ID,@ATTR_TITLE,@TYPE_NAME
WHILE @@FETCH_STATUS = 0
BEGIN
	PRINT CONVERT(VARCHAR(2),@ATTR_ID) + '#' + @TYPE_NAME + '#' + @ATTR_TITLE
	IF @TYPE_NAME IN ('VARCHAR','VECTOR STRING')
		INSERT INTO @TBRESULT (AttributID,AttributTitle,AttributType,ValueText,ADDED_WHEN,CHANGED_WHEN)
		select 
			@ATTR_ID,@ATTR_TITLE,@TYPE_NAME,[dbo].[FNIDB_GET_TERM_TEXT] (@IDB_OBJ_ID,@ATTR_ID),T.ADDED_WHEN,T.CHANGED_WHEN
		from 
			TBIDB_OBJECT_METADATA T 
		WHERE T.IDB_OBJ_ID = @IDB_OBJ_ID AND T.ATTR_ID = @ATTR_ID
	ELSE IF @TYPE_NAME IN ('BIT','BIG INTEGER')
		INSERT INTO @TBRESULT (AttributID,AttributTitle,AttributType,ValueBigInt,ADDED_WHEN,CHANGED_WHEN)
		select 
			@ATTR_ID,@ATTR_TITLE,@TYPE_NAME,[dbo].[FNIDB_GET_TERM_INTEGER] (@IDB_OBJ_ID,@ATTR_ID),T.ADDED_WHEN,T.CHANGED_WHEN
		from 
			TBIDB_OBJECT_METADATA T 
		WHERE T.IDB_OBJ_ID = @IDB_OBJ_ID AND T.ATTR_ID = @ATTR_ID
	ELSE IF @TYPE_NAME = 'DATE'
	INSERT INTO @TBRESULT (AttributID,AttributTitle,AttributType,ValueDate,ADDED_WHEN,CHANGED_WHEN)
		select 
			@ATTR_ID,@ATTR_TITLE,@TYPE_NAME,[dbo].[FNIDB_GET_TERM_DATE] (@IDB_OBJ_ID,@ATTR_ID),T.ADDED_WHEN,T.CHANGED_WHEN
		from 
			TBIDB_OBJECT_METADATA T 
		WHERE T.IDB_OBJ_ID = @IDB_OBJ_ID AND T.ATTR_ID = @ATTR_ID
	ELSE IF @TYPE_NAME = 'DECIMAL'
	INSERT INTO @TBRESULT (AttributID,AttributTitle,AttributType,ValueDec,ADDED_WHEN,CHANGED_WHEN)
		select 
			@ATTR_ID,@ATTR_TITLE,@TYPE_NAME,[dbo].[FNIDB_GET_TERM_DECIMAL] (@IDB_OBJ_ID,@ATTR_ID),T.ADDED_WHEN,T.CHANGED_WHEN
		from 
			TBIDB_OBJECT_METADATA T 
		WHERE T.IDB_OBJ_ID = @IDB_OBJ_ID AND T.ATTR_ID = @ATTR_ID
	FETCH NEXT FROM cursorAttributes INTO @ATTR_ID,@ATTR_TITLE,@TYPE_NAME
END
CLOSE cursorAttributes
DEALLOCATE cursorAttributes

SELECT * FROM @TBRESULT order BY AttributID,ADDED_WHEN
GO

ALTER TABLE TBIDB_DOC_INFO ADD FULL_TEXT VARBINARY(MAX)  
GO 
ALTER TABLE TBIDB_DOC_INFO ADD FILE_SIZE INTEGER
GO 
ALTER TABLE TBIDB_FILE_OBJECT ADD FILE_SIZE BIGINT
GO 
-- =============================================
-- Author:	DD MS	15.03.2021
-- Change:	13.07.2021	Filesize
-- =============================================
ALTER PROCEDURE [dbo].[PRIDB_NEW_IDBFO] @REL_PATH VARCHAR(512),@FILESIZE BIGINT, @WHO VARCHAR(100), @IDB_OBJ_ID BIGINT,@OBJ_ST_ID INT
AS
BEGIN
	DECLARE 
	@FILENAME_EXT	VARCHAR(250),
	@FILENAME_ONLY	VARCHAR(250),
	@RELATIVE_PATH	VARCHAR(512),
	@RELATIVE_PATH_ONLY		VARCHAR(512),
	@EXT			VARCHAR(10)

	SET @FILENAME_ONLY = CONVERT(VARCHAR(20),@IDB_OBJ_ID) + '.ddfo'
	--PRINT @FILENAME_ONLY
	--PRINT @REL_PATH
	SELECT @RELATIVE_PATH_ONLY = REPLACE(@REL_PATH,@FILENAME_ONLY,'')
	--PRINT 'PRIDB_NEW_IDBFO - @RELATIVE_PATH_ONLY1: ' + @RELATIVE_PATH_ONLY
	DECLARE @RELPATH_TERM_ID BIGINT,@EXTENSION_TERM_ID BIGINT
	EXEC PRIDB_NEW_TERM 'VARCHAR',@RELATIVE_PATH_ONLY,@WHO,@TERM_ID = @RELPATH_TERM_ID OUTPUT;
	SELECT @EXT = UPPER([dbo].[FNGetExtension] (@REL_PATH));
	EXEC PRIDB_NEW_TERM 'VARCHAR',@EXT,@WHO,@TERM_ID = @EXTENSION_TERM_ID OUTPUT;

	--SELECT @FILENAME_EXT = [dbo].[FNGetFileName] (@REL_PATH);
	--SELECT @FILENAME_ONLY = [dbo].[FNGetFileNameWithoutExtension] (@REL_PATH);
	--SELECT @EXT = UPPER([dbo].[FNGetExtension] (@REL_PATH));
	PRINT 'PRIDB_NEW_IDBFO: ' + @REL_PATH

	BEGIN
		IF NOT EXISTS(SELECT IDB_OBJ_ID FROM TBIDB_FILE_OBJECT WHERE IDB_OBJ_ID = @IDB_OBJ_ID)
			INSERT INTO [dbo].[TBIDB_FILE_OBJECT]
				   ([IDB_OBJ_ID]
				   ,[OBJ_ST_ID]
				   ,EXTENSION_TERM_ID
				   ,RELPATH_TERM_ID	
				   ,FILE_SIZE
				   ,[ADDED_WHO])
			VALUES
				   (@IDB_OBJ_ID
				   ,@OBJ_ST_ID
				   ,@EXTENSION_TERM_ID
				   ,@RELPATH_TERM_ID
				   ,@FILESIZE
				   ,@WHO)
		ELSE
			BEGIN
				--PRINT 'OBJECT ' + CONVERT(VARCHAR(20),@IDB_OBJ_ID) + ' ALREADY EXISTING ---- So ONLY UPDATE WITH NEW FILE PARAMS'
				UPDATE TBIDB_FILE_OBJECT SET 
					EXTENSION_TERM_ID = @EXTENSION_TERM_ID
					,RELPATH_TERM_ID	 = @RELPATH_TERM_ID
					,FILE_SIZE = @FILESIZE
					,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_IDBFO - 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