UPDATE TBDD_MODULES SET DB_VERSION = '2.2.0.0' where NAME = 'Process-Manager'
GO

UPDATE TBPM_TYPE SET BEZEICHNUNG = 'Metadata based' where BEZEICHNUNG = 'Man. Validation'
GO
UPDATE TBPM_TYPE SET BEZEICHNUNG = 'Serially' where BEZEICHNUNG = 'Automatism'
GO
DROP TABLE TBPM_ADHOC_WF_SERIEL_PARTIES
DROP TABLE TBPM_WF_ADHOC_OBJECT
--Ein Object wird einem Profil zugeordnet
CREATE TABLE TBPM_ADHOC_WF_OBJECT
(
	GUID           			BIGINT		IDENTITY(1,1),
	PROFIL_ID				INTEGER		NOT NULL,
	OBJ_ID					BIGINT		NOT NULL,
	WORK_INSTRUCTION		VARCHAR(250)	NOT NULL,
	WF_FINISHED				DATETIME,
	ADDED_WHO				VARCHAR(100)		DEFAULT 'SYSTEM'	NOT NULL,
	ADDED_WHEN				DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO				VARCHAR(100),
	CHANGED_WHEN			DATETIME,
	CONSTRAINT	PK_TBPM_ADHOC_WF_OBJECT PRIMARY KEY(GUID),
	CONSTRAINT	FK_TBPM_ADHOC_WF_OBJECT_PROFIL_ID FOREIGN KEY (PROFIL_ID)  REFERENCES  TBPM_PROFILE (GUID)
)
GO
CREATE TRIGGER TBPM_ADHOC_WF_OBJECT_AFT_UPD ON TBPM_ADHOC_WF_OBJECT
FOR UPDATE
AS
  UPDATE TBPM_ADHOC_WF_OBJECT SET CHANGED_WHEN = GETDATE()
  FROM INSERTED
  WHERE TBPM_ADHOC_WF_OBJECT.GUID = INSERTED.GUID
GO
--DROP TABLE TBPM_ADHOC_WF_SERIEL_PARTIES
CREATE TABLE TBPM_ADHOC_WF_SERIEL_PARTIES
(
	GUID           			BIGINT		IDENTITY(1,1),
	AH_WF_ID				BIGINT		NOT NULL,
	USR_ID					INTEGER,
	FINISHED_WHEN			DATETIME,
	SEQUENCE				TINYINT		NOT NULL DEFAULT 0,
	ADDED_WHO				VARCHAR(50)		DEFAULT 'SYSTEM'	NOT NULL,
	ADDED_WHEN				DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO				VARCHAR(50),
	CHANGED_WHEN			DATETIME,
	CONSTRAINT	PK_TBPM_ADHOC_WF_SERIEL_PARTIES PRIMARY KEY(GUID),
	CONSTRAINT	UQ_TBPM_ADHOC_WF_SERIEL_PARTIES UNIQUE (AH_WF_ID,USR_ID),
	CONSTRAINT	FK_TBPM_ADHOC_WF_SERIEL_PARTIES_PROFIL_ID FOREIGN KEY (AH_WF_ID)  REFERENCES  TBPM_ADHOC_WF_OBJECT (GUID),
	CONSTRAINT	FK_TBPM_ADHOC_WF_SERIEL_PARTIES_USR_ID FOREIGN KEY (USR_ID)  REFERENCES  TBDD_USER (GUID)
)
GO
CREATE TRIGGER TBPM_PROFILE_SERIELL_PARTIES_AFT_UPD ON TBPM_ADHOC_WF_SERIEL_PARTIES
FOR UPDATE
AS
  UPDATE TBPM_ADHOC_WF_SERIEL_PARTIES SET CHANGED_WHEN = GETDATE()
  FROM INSERTED
  WHERE TBPM_ADHOC_WF_SERIEL_PARTIES.GUID = INSERTED.GUID
GO
CREATE OR ALTER PROCEDURE [dbo].[PRPM_AH_WF_ADD_OBJ_REL] @pOBJ_ID BIGINT, @pPROFILID INTEGER, @pTEXT VARCHAR(200), @pWHO VARCHAR(100), @OUT_ID BIGINT OUTPUT
AS
BEGIN
	INSERT INTO [dbo].[TBPM_ADHOC_WF_OBJECT]
           ([PROFIL_ID]
           ,[OBJ_ID]
           ,[WORK_INSTRUCTION]
           ,[ADDED_WHO])
     VALUES
           (@pPROFILID
           ,@pOBJ_ID
           ,@pTEXT
           ,@pWHO);
	SELECT @OUT_ID = MAX(GUID) FROM TBPM_ADHOC_WF_OBJECT WHERE OBJ_ID = @pOBJ_ID AND PROFIL_ID = @pPROFILID
	PRINT 'NEW @OUT_ID: ' + CONVERT(VARCHAR(10),@OUT_ID)
END
GO
CREATE OR ALTER PROCEDURE [dbo].[PRPM_AH_WF_PARTY] @pAH_WF_ID BIGINT, @pUSR_ID INTEGER, @pWHO VARCHAR(100), @OUT_ID BIGINT OUTPUT
AS
BEGIN
	DECLARE @MAX_SEQU TINYINT
	SELECT @MAX_SEQU= COALESCE(MAX(SEQUENCE),0) FROM TBPM_ADHOC_WF_SERIEL_PARTIES WHERE AH_WF_ID = @pAH_WF_ID
	SET @MAX_SEQU += 1
	SELECT @MAX_SEQU
	INSERT INTO [dbo].[TBPM_ADHOC_WF_SERIEL_PARTIES]
           ([AH_WF_ID]
           ,[USR_ID]
           ,[SEQUENCE]
           ,[ADDED_WHO])
     VALUES
           (@pAH_WF_ID
           ,@pUSR_ID
           ,@MAX_SEQU
           ,@pWHO)

	SELECT @OUT_ID = MAX(GUID) FROM TBPM_ADHOC_WF_SERIEL_PARTIES WHERE AH_WF_ID = @pAH_WF_ID
	PRINT 'NEW @OUTAHWFP_ID: ' + CONVERT(VARCHAR(10),@OUT_ID)
END
GO
-- 
