UPDATE TBIDB_BASE SET CONFIG_VALUE = '1.0.3' where CONFIG_NAME = 'DB_VERSION'

CREATE NONCLUSTERED INDEX IDX_GUID_TEXTTEM_ID
ON [dbo].[TBIDB_TERM_LANGUAGE] ([LANGUAGE_ID])
INCLUDE ([GUID],[TEXT_TERM_ID])
GO
CREATE NONCLUSTERED INDEX IDX_CHANGED_WHEN
ON [dbo].[TBIDB_OBJECT_METADATA_CHANGE] ([CHANGED_WHEN])
INCLUDE ([GUID])
GO
CREATE NONCLUSTERED INDEX IDX_CHANGED_WHEN_IDBOBJID
ON [dbo].[TBIDB_OBJECT_METADATA_CHANGE] ([IDB_OBJ_ID])
INCLUDE ([CHANGED_WHEN])
GO
ALTER PROCEDURE [dbo].[PRIDB_NEW_OBJ_DATA] @IDB_OBJ_ID BIGINT,@ATTRIBUTE VARCHAR(100),@WHO VARCHAR(100), @TERM_VAL VARCHAR(MAX), 
@LANG_CODE VARCHAR(5) = 'de-DE', @FOREIGN_KEY BIGINT = 0, @OMD_ID BIGINT OUTPUT
AS
BEGIN
	DECLARE 
	@ATTR_ID INTEGER,
	@TYP_ID TINYINT,
	@TYP_DESCR VARCHAR(100),
	@MULTI BIT,
	@LANG_ID	INT
	--PRINT '@FOREIGN_KEY: ' + CONVERT(VARCHAR(50),@FOREIGN_KEY)
	IF @FOREIGN_KEY <> 0 AND @IDB_OBJ_ID <> @FOREIGN_KEY
		BEGIN
			IF NOT EXISTS(SELECT IDB_OBJ_ID FROM TBIDB_OBJECT WHERE REFERENCE_KEY = @FOREIGN_KEY)
				BEGIN
					PRINT 'FOUND NO IDB-OBJECT FOR REFERENCE KEY: ' + CONVERT(VARCHAR(50),@FOREIGN_KEY)
				END
			ELSE
				BEGIN
					SELECT @IDB_OBJ_ID = IDB_OBJ_ID FROM TBIDB_OBJECT WHERE REFERENCE_KEY = @FOREIGN_KEY
					--Print 'PRIDB_NEW_OBJ_DATA: Checked the DocID as Reference: ' + Convert(Varchar(10),@IDB_OBJ_ID)
				END
			
		END

	IF NOT EXISTS(SELECT GUID FROM TBIDB_ATTRIBUTE WHERE UPPER(TITLE) = UPPER(@ATTRIBUTE))
		RAISERROR ('NO VALID ATTRIBUTE',16,1)
	
	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_VAL,@WHO,@TERM_ID = @MY_TERM_ID OUTPUT;
	--PRINT 'PRIDB_NEW_OBJ_DATA: @TYP_ID is: ' + Convert(Varchar(10),@TYP_ID)
	IF @TYP_ID IN (1,8) --VARCHAR
		BEGIN
			IF NOT EXISTS(SELECT GUID FROM TBIDB_TERM_LANGUAGE WHERE LANGUAGE_ID = @LANG_ID AND TEXT_TERM_ID = @MY_TERM_ID)
				INSERT INTO TBIDB_TERM_LANGUAGE (LANGUAGE_ID,TEXT_TERM_ID,ADDED_WHO) VALUES (@LANG_ID,@MY_TERM_ID,@WHO)
			
			SELECT @MY_TERM_ID = MAX(GUID) FROM TBIDB_TERM_LANGUAGE WHERE LANGUAGE_ID = @LANG_ID AND TEXT_TERM_ID = @MY_TERM_ID

			IF @TYP_ID = 1 --Single Varchar
				BEGIN
					IF NOT EXISTS(SELECT GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID)
						INSERT INTO [dbo].[TBIDB_OBJECT_METADATA]
							   ([IDB_OBJ_ID]
							   ,[ATTR_ID]
							   ,[TEXT_TERM_LANG_ID]
							   ,[ADDED_WHO])
						 VALUES
							   (@IDB_OBJ_ID
							   ,@ATTR_ID
							   ,@MY_TERM_ID
							   ,@WHO)
				
					ELSE
						IF EXISTS(SELECT GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND [TEXT_TERM_LANG_ID] <> @MY_TERM_ID)
							UPDATE TBIDB_OBJECT_METADATA SET [TEXT_TERM_LANG_ID] = @MY_TERM_ID, CHANGED_WHO = @WHO WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID 
						SELECT @OMD_ID = GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID  AND [TEXT_TERM_LANG_ID] = @MY_TERM_ID
				END
			ELSE
				IF NOT EXISTS(SELECT GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND [TEXT_TERM_LANG_ID] = @MY_TERM_ID)
						INSERT INTO [dbo].[TBIDB_OBJECT_METADATA]
							   ([IDB_OBJ_ID]
							   ,[ATTR_ID]
							   ,[TEXT_TERM_LANG_ID]
							   ,[ADDED_WHO])
						 VALUES
							   (@IDB_OBJ_ID
							   ,@ATTR_ID
							   ,@MY_TERM_ID
							   ,@WHO)
				--ELSE
					--PRINT 'IDB-METADATAOBJECT-VALUE SEEMS TO EXIST: @IDB_OBJ_ID: ' + Convert(Varchar(10),@IDB_OBJ_ID)
				
		END
	ELSE IF @TYP_ID IN (2,7,9) --BIGINT
		BEGIN
			IF NOT EXISTS(SELECT GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID)
				BEGIN
					INSERT INTO [dbo].[TBIDB_OBJECT_METADATA]
							   ([IDB_OBJ_ID]
							   ,[ATTR_ID]
							   ,[INT_TERM_ID]
							   ,[ADDED_WHO])
						 VALUES
							   (@IDB_OBJ_ID
							   ,@ATTR_ID
							   ,@MY_TERM_ID
							   ,@WHO)
				END
			ELSE
				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)
					UPDATE TBIDB_OBJECT_METADATA SET [INT_TERM_ID] = @MY_TERM_ID, CHANGED_WHO = @WHO WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID
				SELECT @OMD_ID = GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND INT_TERM_ID = @MY_TERM_ID
		END
	ELSE IF @TYP_ID = 3 --FLOAT
		BEGIN
			IF NOT EXISTS(SELECT GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID)
				BEGIN
					INSERT INTO [dbo].[TBIDB_OBJECT_METADATA]
							   ([IDB_OBJ_ID]
							   ,[ATTR_ID]
							   ,[FLOAT_TERM_ID]
							   ,[ADDED_WHO])
						 VALUES
							   (@IDB_OBJ_ID
							   ,@ATTR_ID
							   ,@MY_TERM_ID
							   ,@WHO)
				END
			ELSE
				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)
					UPDATE TBIDB_OBJECT_METADATA SET [FLOAT_TERM_ID] = @MY_TERM_ID, CHANGED_WHO = @WHO WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID
				SELECT @OMD_ID = GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID  AND FLOAT_TERM_ID = @MY_TERM_ID
		END
	ELSE IF @TYP_ID = 4 --DECIMAL
		BEGIN
			IF NOT EXISTS(SELECT GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID)
				BEGIN
					INSERT INTO [dbo].[TBIDB_OBJECT_METADATA]
							   ([IDB_OBJ_ID]
							   ,[ATTR_ID]
							   ,[DEC_TERM_ID]
							   ,[ADDED_WHO])
						 VALUES
							   (@IDB_OBJ_ID
							   ,@ATTR_ID
							   ,@MY_TERM_ID
							   ,@WHO)
				END
			ELSE
				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)
					UPDATE TBIDB_OBJECT_METADATA SET [DEC_TERM_ID] = @MY_TERM_ID, CHANGED_WHO = @WHO WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID
			SELECT @OMD_ID = GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND DEC_TERM_ID = @MY_TERM_ID
		END
	ELSE IF @TYP_ID = 5 --DATE
		BEGIN
			IF NOT EXISTS(SELECT GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID)
				BEGIN
					INSERT INTO [dbo].[TBIDB_OBJECT_METADATA]
							   ([IDB_OBJ_ID]
							   ,[ATTR_ID]
							   ,[DATE_TERM_ID]
							   ,[ADDED_WHO])
						 VALUES
							   (@IDB_OBJ_ID
							   ,@ATTR_ID
							   ,@MY_TERM_ID
							   ,@WHO)
				END
			ELSE
				UPDATE TBIDB_OBJECT_METADATA SET [DATE_TERM_ID] = @MY_TERM_ID, CHANGED_WHO = @WHO WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID

			SELECT @OMD_ID = GUID FROM TBIDB_OBJECT_METADATA WHERE IDB_OBJ_ID = @IDB_OBJ_ID AND ATTR_ID = @ATTR_ID AND DATE_TERM_ID = @MY_TERM_ID
		END
	--PRINT 'PRIDB_NEW_OBJ_DATA - Got the new @OMD_ID: ' + CONVERT(VARCHAR(20),@OMD_ID)
END
GO

-- =============================================
-- Author:		DD MS
-- Change date: 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
	--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_EXT]
				   ,[FILENAME_ONLY]
				   ,EXTENSION
				   ,[RELATIVE_PATH]
				   ,[RELATIVE_PATH_ONLY]
				   ,[ADDED_WHO])
			VALUES
				   (@IDB_OBJ_ID
				   ,@OBJ_ST_ID
				   ,@FILENAME_EXT
				   ,@FILENAME_ONLY
				   ,@EXT
				   ,@RELATIVE_PATH
				   ,@RELATIVE_PATH_ONLY
				   ,@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 = @EXT
					,[RELATIVE_PATH] = @RELATIVE_PATH
					,[RELATIVE_PATH_ONLY] = @RELATIVE_PATH_ONLY
					,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
ALTER PROCEDURE PRIDB_GET_VALUE_TYPE @pVALUE VARCHAR(900)--,@oType VARCHAR(10) OUTPUT  
AS    
BEGIN
	declare @TBTYPE_RESULT table(pTYPE varchar(10))
	DECLARE @oTYPE as Varchar(10)
	SET @oType = 'NONE'
	DECLARE @BIGINT as BIGINT, @FLOAT FLOAT,@DECIMAL DECIMAL(19,2),@DATE DATE
	BEGIN TRY
		SET @BIGINT = CONVERT(bigint,@pVALUE)
		SET @oType = 'BIGINT'
		INSERT INTO @TBTYPE_RESULT (pTYPE) VALUES (@oType)
	END TRY
	BEGIN CATCH
		PRINT 'NO BIGINT'
	END CATCH
	--IF @oType = 'NONE'
		BEGIN TRY
			SET @FLOAT = CONVERT(FLOAT,@pVALUE)
			SET @oType = 'FLOAT'
			INSERT INTO @TBTYPE_RESULT (pTYPE) VALUES (@oType)
		END TRY
		BEGIN CATCH
			PRINT 'NO FLOAT'
		END CATCH
	--IF @oType = 'NONE'
		BEGIN TRY
			SET @DECIMAL = CONVERT(DECIMAL(19,2),@pVALUE)
			SET @oType = 'DECIMAL'
			INSERT INTO @TBTYPE_RESULT (pTYPE) VALUES (@oType)
		END TRY
		BEGIN CATCH
			PRINT 'NO DECIMAL'
		END CATCH
	--IF @oType = 'NONE'
		BEGIN TRY
			SET @DATE = CONVERT(DATE,@pVALUE)
			SET @oType = 'DATE'
			INSERT INTO @TBTYPE_RESULT (pTYPE) VALUES (@oType)
		END TRY
		BEGIN CATCH
			PRINT 'NO DATE'
		END CATCH
	--IF @oType = 'NONE'
		BEGIN TRY
			SET @oType = 'VARCHAR' 
			INSERT INTO @TBTYPE_RESULT (pTYPE) VALUES (@oType)
		END TRY
		BEGIN CATCH
			PRINT 'NO VARCHAR'
		END CATCH
	PRINT '@TYPE: ' + @oType
	SELECT * FROM @TBTYPE_RESULT
END


