UPDATE TBDD_MODULES SET PRODUCT_VERSION = '2.4.5.5' where NAME = 'Record-Organizer' 
GO
Create FUNCTION [dbo].[FNPMO_GET_WINDREAM_VKT_INT](@pdwDocID INTEGER,@pdwAttributID INTEGER)
RETURNS VARCHAR(4000)
AS 
BEGIN
	DECLARE	
		@RESULT		VARCHAR(4000),
		@VALUE	INTEGER
		

		
	DECLARE	c_RESULT CURSOR FOR SELECT [dwValue] FROM [172.24.12.41\BRAINPOOL].[windream60].[dbo].[Vector] 
	where dwDocID = @pdwDocID and dwAttrID = @pdwAttributID


	BEGIN
		OPEN c_RESULT 
		FETCH NEXT FROM c_RESULT INTO @VALUE
    
		WHILE @@FETCH_STATUS = 0
			BEGIN
				IF LEN(@RESULT) > 0
					SET @RESULT = @RESULT + CONVERT(VARCHAR(30),@VALUE) + '#'
				ELSE
					SET @RESULT = '#' + CONVERT(VARCHAR(30),@VALUE) + '#'
				FETCH NEXT FROM c_RESULT INTO @VALUE
			END

		CLOSE c_RESULT
		DEALLOCATE c_RESULT

		RETURN(@RESULT)
	END
END


CREATE TABLE TBPMO_INDEX_MAN
( 
	GUID				INT				IDENTITY(1,1),              		
	DOCTYPE_ID			INT										NOT NULL,
	[ENTITY_ID]			INT										NOT NULL,
	NAME				VARCHAR(50)								NOT NULL,
	COL_VISIBLE			BIT				DEFAULT 1				NOT NULL,
	COLUMN_CAPTION		VARCHAR(50)		DEFAULT ''				NOT NULL,						
	ECM_INDEX			VARCHAR(50)		DEFAULT ''				NOT NULL,
	COMMENT				VARCHAR(250),
	DATATYPE			VARCHAR(50),
	SUGGESTION			BIT				DEFAULT 0				NOT NULL,	-- 1 - Es wird Eingabe vorgeschlagen, bei 0 - manuelle Eingabe.
	DEFAULT_VALUE		VARCHAR(100)		NOT NULL DEFAULT '',
	CONNECTION_ID		SMALLINT				DEFAULT 0,
	SEQUENCE			INTEGER NOT NULL DEFAULT 1,
	SQL_RESULT			VARCHAR(2000) NOT NULL DEFAULT '',
	SQL_CHECK			VARCHAR(2000) NOT NULL DEFAULT '',
	OPTIONAL			BIT				DEFAULT 0				NOT NULL,
	SAVE_VALUE			BIT				DEFAULT 0				NOT NULL,
	ACTIVE				BIT				DEFAULT 1				NOT NULL,	-- 1 - Index aktiv, bei 0 - inaktiv.
	ADDED_WHO			VARCHAR(50)		DEFAULT 'DEFAULT'		NOT NULL,
	ADDED_WHEN			DATETIME		DEFAULT GETDATE(),
	CHANGED_WHO			VARCHAR(50),
	CHANGED_WHEN		DATETIME,
	CONSTRAINT			PK_TBPMO_INDEX_MAN		PRIMARY KEY(GUID),
	CONSTRAINT			FK_TBPMO_INDEX_MAN_DOCTYPE_ID	FOREIGN KEY(DOCTYPE_ID)	REFERENCES TBDD_DOKUMENTART(GUID),
	CONSTRAINT			FK_TBPMO_INDEX_MAN_CID	FOREIGN KEY(CONNECTION_ID)	REFERENCES TBDD_CONNECTION(GUID)
)
GO
------------------------------------------------------------------------------
CREATE TRIGGER TBPMO_INDEX_MAN_AFT_UPD ON TBPMO_INDEX_MAN
FOR UPDATE
AS
BEGIN
	UPDATE TBPMO_INDEX_MAN SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBPMO_INDEX_MAN.GUID = INSERTED.GUID
END
GO
CREATE TABLE TBPMO_DOC_INDICES
(
	GUID			INTEGER NOT NULL IDENTITY(1,1),
	[DocID]			INTEGER			NOT NULL,
	[INDEX_ID]		INTEGER	NOT NULL,
	[VALUE]			VARCHAR(MAX)	NOT NULL,
	ADDED_WHO		VARCHAR(50)		DEFAULT 'DEFAULT',
	ADDED_WHEN		DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO     VARCHAR(50),
	CHANGED_WHEN	DATETIME,
	CONSTRAINT		PK_TBPMO_DOC_INDICES		PRIMARY KEY (GUID),
	CONSTRAINT		FK_TBPMO_DOC_INDICES_INDEX_ID	FOREIGN KEY([INDEX_ID])	REFERENCES TBPMO_INDEX_MAN(GUID),
)
GO

CREATE TRIGGER TBPMO_DOC_INDICES_AFT_UPD ON TBPMO_DOC_INDICES
FOR UPDATE
AS
	UPDATE TBPMO_DOC_INDICES SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBPMO_DOC_INDICES.[DocID] = INSERTED.[DocID]
GO
ALTER TABLE TBPMO_DOCSEARCH_RESULTLIST_CONFIG
ADD VISIBLE BIT NOT NULL DEFAULT 1
CREATE TABLE TBPMO_DOCRESULT_LIST
(
	[DocID]			INTEGER			NOT NULL,
	[ENTITY_ID]		VARCHAR(500)	NOT NULL,
	[RECORD_ID]		VARCHAR(500)	NOT NULL,
	[PARENT_ID]		VARCHAR(500)	NOT NULL,
	[DISPLAY_NAME]	VARCHAR(250)	NOT NULL,
	[FULL_FILENAME]	VARCHAR(5000)	NOT NULL,
	[Filename]		VARCHAR(500)	NOT NULL,
	[Doctype]		VARCHAR(250)	NOT NULL,
	[Version]		SMALLINT		NOT NULL	DEFAULT 0,
	[Objecttype]	VARCHAR(250)	NOT NULL,
	Creation_DateTime	DATETIME	NOT NULL,
	Change_DateTime		DATETIME,
	STRING1			VARCHAR(500),
	STRING2			VARCHAR(500),
	STRING3			VARCHAR(500),
	STRING4			VARCHAR(500),
	STRING5			VARCHAR(500),
	STRING6			VARCHAR(500),
	STRING7			VARCHAR(500),
	STRING8			VARCHAR(500),
	STRING9			VARCHAR(500),
	STRING10		VARCHAR(500),
	IN_WORK			BIT				NOT NULL	DEFAULT 0,
	IN_WORK_USER	VARCHAR(250),
	IN_WORK_COMMENT	VARCHAR(500),
	ADDED_WHO		VARCHAR(50)		DEFAULT 'DEFAULT',
	ADDED_WHEN		DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO     VARCHAR(50),
	CHANGED_WHEN	DATETIME,
	CONSTRAINT		PK_TBPMO_DOCRESULT_LIST		PRIMARY KEY ([DocID])
)
GO

CREATE TRIGGER TBPMO_DOCRESULT_LIST_AFT_UPD ON TBPMO_DOCRESULT_LIST
FOR UPDATE
AS
	UPDATE TBPMO_DOCRESULT_LIST SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBPMO_DOCRESULT_LIST.[DocID] = INSERTED.[DocID]
GO
USE [DD_ECM_RENOLIT]
GO

CREATE VIEW [dbo].[VWPMO_WD_DOC_SYNC] AS 
SELECT 
	T.dwDocID as DocID
	,[dbo].[FNPMO_GET_WINDREAM_VKT_INT](T.dwDocID,339)							AS [ENTITY_ID]
	,[dbo].[FNPMO_GET_WINDREAM_VKT_INT](T.dwDocID,338)							AS [RECORD_ID]
	,[dbo].[FNPMO_GET_WINDREAM_VKT_INT](T.dwDocID,340)							AS [PARENT_ID]
	,dbo.FNPMO_GET_WINDREAM_FILE_PATH (T.dwDocID) + T.szLongName				AS FULL_FILENAME
	,szLongName																	as [Filename], 
	T.szText39																	AS Doctype
	,T.dwVersionNumber																AS [Version]
	,CAST(CAST(CONVERT(DATE,CONVERT(VARCHAR(10),T.dwCreationDate)) AS DATETIME) + CAST(STUFF(STUFF(REPLICATE('0',6-LEN(T.dwCreation_Time)) + convert(VARCHAR(6),T.dwCreation_Time),3,0,':'),6,0,':') AS DATETIME) AS DATETIME) AS Creation_DateTime
	,CAST(CAST(CONVERT(DATE,CONVERT(VARCHAR(10),T.dwChangeDate)) AS DATETIME) + CAST(STUFF(STUFF(REPLICATE('0',6-LEN(T.dwChange_Time)) + convert(VARCHAR(6),T.dwChange_Time),3,0,':'),6,0,':') AS DATETIME) AS DATETIME) AS Change_DateTime
	,T1.szDocTypeName																AS OBJECTTYPE
FROM 
	[172.24.12.41\SDD_VMX02_APS02].windream60.dbo.BaseAttributes T
	,[172.24.12.41\SDD_VMX02_APS02].windream60.dbo.ObjectType T1
WHERE 
	--T.dwObjectTypeID > 23 AND
	T.dwObjectTypeID = T1.dwObjectTypeID 
	AND [dbo].[FNPMO_GET_WINDREAM_VKT_INT](T.dwDocID,338) is not null 

GO


---

CREATE PROCEDURE [dbo].[PRPMO_DOCRESULT_CREATE]
AS
BEGIN TRY
DECLARE @DocID	INTEGER,
	@ENTITY_ID		VARCHAR(500),
	@RECORD_ID		VARCHAR(500),
	@PARENT_ID		VARCHAR(500),
	@DISPLAY_NAME	VARCHAR(250),
	@FULL_FILENAME	VARCHAR(5000),
	@Filename		VARCHAR(500),
	@Doctype		VARCHAR(250),
	@Version		SMALLINT,
	@Objecttype	VARCHAR(250),
	@Creation_DateTime DATETIME,
	@Change_DateTime DATETIME
DECLARE c_Values CURSOR LOCAL FOR 
	select DocID,ISNULL([ENTITY_ID],0),RECORD_ID,ISNULL(PARENT_ID,0),[Filename],REPLACE(REPLACE(FULL_FILENAME,'\\\','\'),'\\','\'),[Filename],[Doctype],[Version],Objecttype,
	Creation_DateTime,Change_DateTime from VWPMO_WD_DOC_SEARCH --WHERE CONVERT(DATE,Creation_DateTime) = CONVERT(DATE,GETDATE())
 	OPEN c_Values 
   	FETCH NEXT FROM c_Values INTO @DocID,@ENTITY_ID,@RECORD_ID,@PARENT_ID,@Filename,@FULL_FILENAME,
	@Filename,@Doctype,@Version,@Objecttype,@Creation_DateTime,@Change_DateTime
   	WHILE @@FETCH_STATUS = 0
	BEGIN
		IF NOT EXISTS(SELECT [DocID] FROM TBPMO_DOCRESULT_LIST WHERE DocID = @DocID)
		INSERT INTO TBPMO_DOCRESULT_LIST (
			[DocID],
			[ENTITY_ID],
			[RECORD_ID],
			[PARENT_ID],
			[DISPLAY_NAME],
			[FULL_FILENAME],
			[Filename],
			[Doctype],
			[Version],
			[Objecttype],
			Creation_DateTime,
			Change_DateTime) 
		VALUES (
			@DocID,
			@ENTITY_ID,
			@RECORD_ID,
			@PARENT_ID,
			@Filename,
			@FULL_FILENAME,
			@Filename,
			@Doctype,
			@Version,
			@Objecttype,
			@Creation_DateTime,
			@Change_DateTime
			)
		--#####
		FETCH NEXT FROM c_Values INTO @DocID,@ENTITY_ID,@RECORD_ID,@PARENT_ID,@Filename,@FULL_FILENAME,
	@Filename,@Doctype,@Version,@Objecttype,@Creation_DateTime,@Change_DateTime
	END
CLOSE c_Values
DEALLOCATE c_Values
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) + ' - ROW: ' + CONVERT(VARCHAR(10),ERROR_LINE())
	+ ' - ERROR-MESSAGE: ' 
    + CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO
CREATE PROCEDURE [dbo].[PRPMO_DOCRESULT_CHANGE]
AS
BEGIN TRY
DECLARE @DocID	INTEGER,
	@ENTITY_ID		VARCHAR(500),
	@RECORD_ID		VARCHAR(500),
	@PARENT_ID		VARCHAR(500),
	@DISPLAY_NAME	VARCHAR(250),
	@FULL_FILENAME	VARCHAR(5000),
	@Filename		VARCHAR(500),
	@Doctype		VARCHAR(250),
	@Version		SMALLINT,
	@Objecttype	VARCHAR(250),
	@Creation_DateTime DATETIME,
	@Change_DateTime DATETIME
DECLARE c_Values CURSOR LOCAL FOR 
	select DocID,ISNULL([ENTITY_ID],0),RECORD_ID,ISNULL(PARENT_ID,0),[Filename],REPLACE(REPLACE(FULL_FILENAME,'\\\','\'),'\\','\'),
	[Filename],ISNULL([Doctype],'') as Doctype,ISNULL([Version],0) as [Version],Objecttype,
	Creation_DateTime,Change_DateTime from VWPMO_WD_DOC_SYNC WHERE CONVERT(DATE,Creation_DateTime) = CONVERT(DATE,GETDATE())
 	OPEN c_Values 
   	FETCH NEXT FROM c_Values INTO @DocID,@ENTITY_ID,@RECORD_ID,@PARENT_ID,@Filename,@FULL_FILENAME,
	@Filename,@Doctype,@Version,@Objecttype,
	@Creation_DateTime,@Change_DateTime
   	WHILE @@FETCH_STATUS = 0
	BEGIN
		IF NOT EXISTS(SELECT [DocID] FROM TBPMO_DOCRESULT_LIST WHERE DocID = @DocID)
			BEGIN
			PRINT 'INSERT NECESSARY'
			INSERT INTO TBPMO_DOCRESULT_LIST (
				[DocID],
				[ENTITY_ID],
				[RECORD_ID],
				[PARENT_ID],
				[DISPLAY_NAME],
				[FULL_FILENAME],
				[Filename],
				[Doctype],
				[Version],
				[Objecttype],
				Creation_DateTime,
				Change_DateTime) 
			VALUES (
				@DocID,
				@ENTITY_ID,
				@RECORD_ID,
				@PARENT_ID,
				@Filename,
				@FULL_FILENAME,
				@Filename,
				@Doctype,
				@Version,
				@Objecttype,
				@Creation_DateTime,
				@Change_DateTime
				)
			END
		ELSE
			UPDATE TBPMO_DOCRESULT_LIST SET 
				[ENTITY_ID] = @ENTITY_ID,
				[RECORD_ID] = @RECORD_ID,
				[PARENT_ID] = @PARENT_ID,
				[FULL_FILENAME] = @FULL_FILENAME,
				[Filename] = @Filename,
				[Doctype] = @Doctype,
				[Version] = @Version,
				[Objecttype] = @Objecttype,
				Change_DateTime = @Change_DateTime
			WHERE DocID = @DocID
				
		--#####
		FETCH NEXT FROM c_Values INTO @DocID,@ENTITY_ID,@RECORD_ID,@PARENT_ID,@Filename,@FULL_FILENAME,
	@Filename,@Doctype,@Version,@Objecttype,@Creation_DateTime,@Change_DateTime
	END
CLOSE c_Values
DEALLOCATE c_Values
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) + ' - ROW: ' + CONVERT(VARCHAR(10),ERROR_LINE())
	+ ' - ERROR-MESSAGE: ' 
    + CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO



ALTER PROCEDURE [dbo].[PRPMO_DELETE_DOCTYPE](@pDOCID INT)
AS
BEGIN TRY
	DELETE FROM TBPMO_WD_NAMECONVENTION_FORMAT WHERE ID IN (SELECT GUID FROM TBPMO_WD_FORMVIEW_DOKTYPES WHERE DOCTYPE_ID = @pDOCID)
	DELETE FROM TBPMO_WD_FORMVIEW_DOKTYPES WHERE DOCTYPE_ID = @pDOCID
	DELETE FROM TBDD_INDEX_MAN WHERE DOK_ID = @pDOCID
	DELETE FROM TBDD_INDEX_AUTOM WHERE DOCTYPE_ID = @pDOCID
	DELETE FROM TBDD_USRGRP_DOKTYPE WHERE DOCTYPE_ID = @pDOCID
	DELETE FROM TBPMO_CLIENT_DOCTYPE WHERE DOCTYPE_ID = @pDOCID
	DELETE FROM TBPMO_TEMPLATE WHERE DOCTYPE_ID = @pDOCID
	DELETE FROM TBPMO_INDEX_MAN WHERE DOCTYPE_ID = @pDOCID
	DELETE FROM TBDD_DOKUMENTART WHERE GUID = @pDOCID
END TRY
BEGIN CATCH   
	PRINT 'FEHLER IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) + ' - ZEILE: ' + CONVERT(VARCHAR(10),ERROR_LINE())
	+ ' - FEHLER-MESSAGE: ' 
    + CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH

GO

CREATE VIEW VWPMO_DOC_INDICES AS
SELECT 
	T.DocID,
	T.VALUE,
	T1.COLUMN_CAPTION,
	T1.GUID				AS INDEX_ID,
	T1.NAME				AS INDEX_NAME
FROM
	TBPMO_DOC_INDICES T,
	TBPMO_INDEX_MAN T1
WHERE
	T.INDEX_ID = T1.GUID AND
	T1.COL_VISIBLE = 1