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

--Ein Object wird einem Profil zugeordnet
CREATE TABLE TBPM_WF_ADHOC_OBJECT
(
	GUID           			BIGINT		IDENTITY(1,1),
	PROFIL_ID				INTEGER		NOT NULL,
	OBJ_ID					BIGINT		NOT NULL,
	WORK_INSTRUCTION		VARCHAR(250)	NOT NULL,
	CMD_WF_SELECT			NVARCHAR(MAX) 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_WF_ADHOC_OBJECT PRIMARY KEY(GUID),
	CONSTRAINT	FK_TBPM_WF_ADHOC_OBJECT_PROFIL_ID FOREIGN KEY (PROFIL_ID)  REFERENCES  TBPM_PROFILE (GUID)
)
GO
CREATE TRIGGER TBPM_WF_ADHOC_OBJECT_AFT_UPD ON TBPM_WF_ADHOC_OBJECT
FOR UPDATE
AS
  UPDATE TBPM_WF_ADHOC_OBJECT SET CHANGED_WHEN = GETDATE()
  FROM INSERTED
  WHERE TBPM_WF_ADHOC_OBJECT.GUID = INSERTED.GUID
GO

CREATE TABLE TBPM_WF_ADHOC_OBJECT_PARTIES
(
	GUID           			BIGINT		IDENTITY(1,1),
	AH_WF_ID				BIGINT		NOT NULL,
	ENTITY_TYPE				VARCHAR(50)	NOT NULL,
	USER_OR_GROUP_ID		INTEGER		NOT NULL,
	CRITERIA_NEXT_STEP		NVARCHAR(MAX),
	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_WF_ADHOC_OBJECT_PARTIES PRIMARY KEY(GUID),
	CONSTRAINT	FK_TBPM_WF_ADHOC_OBJECT_PARTIES_PROFIL_ID FOREIGN KEY (AH_WF_ID)  REFERENCES  TBPM_WF_ADHOC_OBJECT (GUID)
)
GO
CREATE TRIGGER TBPM_PROFILE_SERIELL_PARTIES_AFT_UPD ON TBPM_WF_ADHOC_OBJECT_PARTIES
FOR UPDATE
AS
  UPDATE TBPM_WF_ADHOC_OBJECT_PARTIES SET CHANGED_WHEN = GETDATE()
  FROM INSERTED
  WHERE TBPM_WF_ADHOC_OBJECT_PARTIES.GUID = INSERTED.GUID
GO

CREATE TABLE TBPM_SERIELL_PARTIES_STATE
(
	GUID           			BIGINT		IDENTITY(1,1),
	SPID_ID					BIGINT		NOT NULL,
	--ENTITY_TYPE				VARCHAR(50)	NOT NULL,
	--USER_OR_GROUP_ID		INTEGER		NOT NULL,
	STATE_DESC				VARCHAR(500)	NOT NULL,	
	COMMENT					VARCHAR(500),
	ADDED_WHO				VARCHAR(50)		DEFAULT 'SYSTEM'	NOT NULL,
	ADDED_WHEN				DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO				VARCHAR(50),
	CHANGED_WHEN			DATETIME ,
	CONSTRAINT	PK_TBPM_SP_STATE PRIMARY KEY(GUID),
	CONSTRAINT	FK_TBPM_SP_STATE_SPID_ID FOREIGN KEY (SPID_ID)  REFERENCES  TBPM_PROFILE_SERIELL_PARTIES (GUID)
)
GO
CREATE TRIGGER TBPM_SERIELL_PARTIES_STATE_AFT_UPD ON TBPM_SERIELL_PARTIES_STATE
FOR UPDATE
AS
  UPDATE TBPM_SERIELL_PARTIES_STATE SET CHANGED_WHEN = GETDATE()
  FROM INSERTED
  WHERE TBPM_SERIELL_PARTIES_STATE.GUID = INSERTED.GUID
GO