UPDATE TBDD_MODULES SET DB_VERSION = '2.4.8.8' where NAME = 'Record-Organizer' 
GO 
CREATE PROCEDURE [dbo].[PRPMO_DELETE_NODE](@pNODE_GUID INT,@WHO VARCHAR(50))
AS
DECLARE
	@SUBNODE_ID			INTEGER,
	@RECORD_ID	INTEGER

	SELECT @RECORD_ID = RECORD_ID from TBPMO_STRUCTURE_NODES where GUID = @pNODE_GUID
	IF  @RECORD_ID <> NULL
		BEGIN
			DELETE FROM TBPMO_DOC_RECORD_LINK WHERE RECORD_ID = @RECORD_ID
			EXEC PRPMO_DELETE_RECORD @RECORD_ID, @WHO, 'DELETE NODE'
		END
	DELETE FROM TBPMO_STRUCTURE_NODES WHERE GUID = @SUBNODE_ID
GO

--ALTER TABLE TBPMO_WD_FORMVIEW_DOKTYPES ADD TARGET_PATH VARCHAR(500)	NOT NULL DEFAULT ''
--GO 
ALTER TABLE TBPMO_WD_FORMVIEW_DOKTYPES ADD DYNAMIC_FOLDER VARCHAR(500) NOT NULL DEFAULT ''
GO 
DECLARE
	@GUID	INTEGER,
	@DOCTYPE_ID			INTEGER,
	@CAPTION		VARCHAR(500)		
								
DECLARE c_REDO CURSOR FOR 
	select GUID, DOCTYPE_ID from TBPMO_WD_FORMVIEW_DOKTYPES
  OPEN c_REDO 
    FETCH NEXT FROM c_REDO INTO @GUID,@DOCTYPE_ID
    WHILE @@FETCH_STATUS = 0
	BEGIN
		UPDATE TBPMO_WD_FORMVIEW_DOKTYPES SET DYNAMIC_FOLDER = (SELECT FOLDER_FOR_INDEX FROM TBDD_DOKUMENTART WHERE GUID = @DOCTYPE_ID) WHERE GUID = @GUID
		FETCH NEXT FROM c_REDO INTO @GUID,@DOCTYPE_ID
	END
CLOSE c_REDO
DEALLOCATE c_REDO
GO
CREATE PROCEDURE [dbo].[PRPMO_CREATE_NODE](@pRESULT INT OUTPUT,@pRECORD_ID INT,@pNODE_CONFIG_ID INT, @pNODE_CAPTION VARCHAR(250), @pNODE_ID1 VARCHAR(1000) , @pNODE_PARENT_ID INT, @pADDED_WHO VARCHAR(50))
AS
DECLARE @FEHLER				VARCHAR(4000),
		@LINE				VARCHAR(10)

DECLARE	@OUTPUTDATA			TABLE(NODE_ID INT)

BEGIN TRY
--RAISERROR ('ENTITY IS LOCKED - NO INSERT ALLOWED!',16,1)
	INSERT INTO TBPMO_STRUCTURE_NODES (RECORD_ID,NODE_CONFIG_ID, NODE_CAPTION, ID1, PARENT_GUID, ADDED_WHO) 
	OUTPUT INSERTED.GUID INTO @OUTPUTDATA
	VALUES (@pRECORD_ID,@pNODE_CONFIG_ID,@pNODE_CAPTION,@pNODE_ID1,@pNODE_PARENT_ID,@pADDED_WHO)
	SELECT @pRESULT = NODE_ID FROM @OUTPUTDATA
	--##### CUSTOMER ROUTINE ######################
	--IF @pRESULT > 0
	--	EXEC PRPMO_CUST_COMOS_CREATE_CONTROL_VALUES @pRESULT, @pNODE_CONFIG_ID, @pRECORD_ID,@pNODE_ID1,@pNODE_CAPTION,@pADDED_WHO
	--##### END - CUSTOMER #########################
		
	RETURN
END TRY
BEGIN CATCH   --Fehlerbehandlung
	SET @pRESULT = 0
	SET @LINE = CONVERT(VARCHAR(10),ERROR_LINE())
	SET @FEHLER = CONVERT(VARCHAR(4000),ERROR_MESSAGE()) + CHAR(13) + CHAR(10) + 'ERROR-LINE: ' + @LINE
	--INSERT INTO TBPMO_LOG_ESSENTIALS(REFERENCE_KEY,REFERENCE_STRING,COMMENT,ADDED_WHO) VALUES(@pFORM_ID,'FORM-ID',CONVERT(VARCHAR(4000),ERROR_MESSAGE()),LTRIM(RTRIM(@pADDED_WHO)))
	RAISERROR(@FEHLER,16,1)
	RETURN
END CATCH
GO
ALTER TABLE TBPMO_STRUCTURE_NODES ADD DEPTH INTEGER NOT NULL DEFAULT 0
GO 
CREATE FUNCTION [dbo].[FNPMO_CUST_NODE_EX_UP] 
-- =============================================
-- Author:		DIGITAL DATA, MS
-- Create date: 24.07.2018
-- Description:	<Description,,>
-- =============================================
(@NODE_CONFIG_ID INTEGER, 
	@TPLNR VARCHAR(100),
	@NODE_CAPTION VARCHAR(250),
	@PARENT_GUID INTEGER,@NODE_COLOR VARCHAR(50),
	@SEQUENCE TINYINT,@DEPTH INT
)
RETURNS 
@Table TABLE 
    (NODE_GUID INTEGER,CAPTION VARCHAR(250), RESULT VARCHAR(20),COLOR VARCHAR(50),SEQUENCE TINYINT, DEPTH INT, ID INT IDENTITY(1, 1))
AS
	BEGIN
		INSERT INTO @TABLE (NODE_GUID,CAPTION,COLOR,SEQUENCE,DEPTH)
		SELECT GUID,NODE_CAPTION,COLOR,SEQUENCE,DEPTH FROM TBPMO_STRUCTURE_NODES WHERE ID1 = @TPLNR AND PARENT_GUID = @PARENT_GUID

		IF (SELECT COUNT(*) FROM @TABLE) = 1
			IF (SELECT CAPTION FROM @TABLE WHERE ID = 1) = @NODE_CAPTION
				IF (SELECT COLOR FROM @TABLE WHERE ID = 1) <> @NODE_COLOR
					UPDATE @TABLE SET RESULT = 'UPDATE' WHERE ID = 1
				ELSE IF (SELECT SEQUENCE FROM @TABLE WHERE ID = 1) <> @SEQUENCE
					UPDATE @TABLE SET RESULT = 'UPDATE' WHERE ID = 1
				ELSE IF (SELECT DEPTH FROM @TABLE WHERE ID = 1) <> @DEPTH
					UPDATE @TABLE SET RESULT = 'UPDATE' WHERE ID = 1
				ELSE
					UPDATE @TABLE SET RESULT = 'OK' WHERE ID = 1
			ELSE
				UPDATE @TABLE SET RESULT = 'UPDATE' WHERE ID = 1
		ELSE
			INSERT INTO @TABLE (NODE_GUID,CAPTION,COLOR,SEQUENCE,DEPTH,RESULT) VALUES (0,'','',1,@DEPTH,'INSERT')
	RETURN 
END
GO
/****** Object:  StoredProcedure [dbo].[PRPMO_STRUCTURE_NODES_CHECK_NODE]    Script Date: 09.11.2018 08:38:32 ******/
CREATE PROCEDURE [dbo].[PRPMO_STRUCTURE_NODES_CHECK_NODE](@pNODEGUID INTEGER OUTPUT,
	@NODE_CONFIG_ID INTEGER,@NODE_CAPTION VARCHAR(250),@TPLNR VARCHAR(1000),@PARENT_GUID INTEGER,@NODE_COLOR VARCHAR(50),@DEPTH INTEGER,@SEQUENCE TINYINT)
AS
DECLARE @FEHLER		VARCHAR(4000),
		@LINE		VARCHAR(10),
		@PRAEFIX	VARCHAR(100),
		@RESULT_CHECK	VARCHAR(250),
		@SEQUENCE_IST	TINYINT,
		@NC_IST	VARCHAR(250)

BEGIN TRY
	SET @PRAEFIX = REPLICATE(' ', @DEPTH)
	SELECT @pNODEGUID = NODE_GUID, @NC_IST = CAPTION, @RESULT_CHECK = RESULT,@SEQUENCE_IST = SEQUENCE FROM 
	[dbo].[FNPMO_CUST_NODE_EX_UP] (@NODE_CONFIG_ID, @TPLNR,@NODE_CAPTION,@PARENT_GUID,@NODE_COLOR,@SEQUENCE,@DEPTH)
	IF @NODE_COLOR = '0'
		SET @NODE_COLOR = ''
	IF @RESULT_CHECK = 'UPDATE'
		BEGIN
			--PRINT @PRAEFIX + '## UPDATE ' + CONVERT(VARCHAR(10),@pNODEGUID) + ' #' + @NC_IST + '#'
			UPDATE TBPMO_STRUCTURE_NODES SET NODE_CAPTION = @NODE_CAPTION, ACTIVE = 1,
			COLOR = @NODE_COLOR,SEQUENCE = @SEQUENCE,DEPTH = @DEPTH,CHANGED_WHO = 'DD PMO_CHECKNODE UPD' WHERE GUID = @pNODEGUID
		END
	ELSE IF @RESULT_CHECK = 'INSERT'
		BEGIN
			--PRINT @PRAEFIX + '## INSERT - NODE (' + @NODE_CAPTION + ') MUSS ANGELEGT WERDEN'
			INSERT INTO TBPMO_STRUCTURE_NODES (NODE_CONFIG_ID,NODE_CAPTION,ID1,PARENT_GUID,COLOR,SEQUENCE,DEPTH, ADDED_WHO) VALUES (
				@NODE_CONFIG_ID,@NODE_CAPTION,@TPLNR,@PARENT_GUID,@NODE_COLOR,@SEQUENCE, @DEPTH, 'DD PMO_CHECKNODE INS')
			SELECT @pNODEGUID = NODE_GUID, @RESULT_CHECK = RESULT FROM [dbo].[FNPMO_CUST_NODE_EX_UP] (@NODE_CONFIG_ID, @TPLNR,@NODE_CAPTION,@PARENT_GUID,@NODE_COLOR,@SEQUENCE,@DEPTH)
		END
	IF NOT EXISTS(SELECT NODE_GUID FROM TBPMO_CUST_CREATED_NODES_IN_RUN WHERE NODE_GUID = @pNODEGUID)
		INSERT INTO TBPMO_CUST_CREATED_NODES_IN_RUN (NODE_GUID, ENTITY) VALUES (@pNODEGUID,'SAP')
	RETURN
END TRY
BEGIN CATCH   --Fehlerbehandlung
	SET @LINE = CONVERT(VARCHAR(10),ERROR_LINE())
	SET @FEHLER = CONVERT(VARCHAR(4000),ERROR_MESSAGE()) + CHAR(13) + CHAR(10) + 'ERROR-LINE: ' + @LINE
	RAISERROR(@FEHLER,16,1)
END CATCH
GO
ALTER TABLE TBPMO_WD_FORMVIEW_DOKTYPES ADD ROOT_FOLDER VARCHAR(1000) NOT NULL DEFAULT ''
GO

