DROP TABLE TBECM_JR_FIW_STEP;
DROP TABLE TBECM_JR_FIW_PROFILE;
DROP TABLE TBECM_JR_JOB;
DROP TABLE TBECM_JR_TYPE;


CREATE TABLE TBECM_JR_TYPE
(
	GUID			INT				IDENTITY(1,1),              		    
	JOB_TYPE		VARCHAR(100) 	NOT NULL,
	ACTIVE   		BIT				NOT NULL	DEFAULT 1,
	COMMENT			VARCHAR(250),
	ADDED_WHO		VARCHAR(100)	NOT NULL	DEFAULT 'DEFAULT',
	ADDED_WHEN		DATETIME 					DEFAULT GETDATE(),
	CHANGED_WHO		VARCHAR(100),
	CHANGED_WHEN	DATETIME,
	CONSTRAINT		PKTBECM_JR_TYPE	PRIMARY KEY(GUID),
	CONSTRAINT 		UNIQUE_TBECM_JR_TYPE	UNIQUE(JOB_TYPE)
)
GO
-----
INSERT INTO TBECM_JR_TYPE (JOB_TYPE) VALUES ('FIM-HotFolder');
INSERT INTO TBECM_JR_TYPE (JOB_TYPE) VALUES ('FIH-Attributing-SQL');

CREATE TABLE TBECM_JR_JOB
(
	GUID			INT		IDENTITY(1,1),              		    
	JOB_TYPE_ID		INT				NOT NULL,
	TITLE			VARCHAR(250) 	NOT NULL,
	QUARTZ_DEF		VARCHAR(250)	NOT NULL,
	COMMENT			VARCHAR(500)	NOT NULL	DEFAULT '',
	ACTIVE   		BIT		NOT NULL	DEFAULT 1,
	ADDED_WHO		VARCHAR(100)	NOT NULL	DEFAULT 'DEFAULT',
	ADDED_WHEN		DATETIME 					DEFAULT GETDATE(),
	CHANGED_WHO		VARCHAR(100),
	CHANGED_WHEN	DATETIME,
	CONSTRAINT		PKTBECM_JR_JOB	PRIMARY KEY(GUID),
	CONSTRAINT		FKTBECM_JR_JOB_JOB_TYPEID	FOREIGN KEY(JOB_TYPE_ID)	REFERENCES TBECM_JR_TYPE(GUID),
)
GO
------------------------------------------------------------------------------
CREATE TRIGGER TBTBECM_JR_JOB_AFT_UPD ON TBECM_JR_JOB
FOR UPDATE
AS
BEGIN TRY
	 UPDATE TBECM_JR_JOB SET CHANGED_WHEN = GETDATE() FROM INSERTED 
	 WHERE TBECM_JR_JOB.GUID = INSERTED.GUID
	
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) 
		+ ' - ERROR-MESSAGE: ' 
		+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO
CREATE TABLE TBECM_JR_FIW_PROFILE
(
	GUID			INT				IDENTITY(1,1),        
	JR_JOB_ID		INTEGER			NOT NULL,
	WM_OBJECTTYPE	VARCHAR(100) 	NOT NULL,
	SOURCE_FOLDER	VARCHAR(500)	NOT NULL,
	TARGET_FOLDER	VARCHAR(500)	NOT NULL,
	BACKUP_FOLDER	VARCHAR(500)	NOT NULL	DEFAULT '',
	SF_DATE_FORMAT	VARCHAR(50)		NOT NULL	DEFAULT '',
	DEL_FILE_SUCCESS	BIT			NOT NULL	DEFAULT 1,
	INCL_SUBFOLDER	BIT				NOT NULL	DEFAULT 0,
	EXCLUDE_REGEX	NVARCHAR(MAX)	NOT NULL	DEFAULT '',
	ACTIVE   		BIT				NOT NULL	DEFAULT 1,
	COMMENT			VARCHAR(250),
	ADDED_WHO		VARCHAR(100)	NOT NULL	DEFAULT 'DEFAULT',
	ADDED_WHEN		DATETIME 					DEFAULT GETDATE(),
	CHANGED_WHO		VARCHAR(100),
	CHANGED_WHEN	DATETIME,
	CONSTRAINT		PKTBECM_JR_FIW_PROFILE	PRIMARY KEY(GUID),
	CONSTRAINT		FKTBECM_TBECM_JR_FIW_PROFILE_JOBID	FOREIGN KEY(JR_JOB_ID)	REFERENCES TBECM_JR_JOB(GUID),
	CONSTRAINT 		UNIQUE_TBECM_JR_FIW_PROFILE	UNIQUE(SOURCE_FOLDER,TARGET_FOLDER)
)
GO
CREATE TRIGGER TBECM_JR_FIW_PROFILE_AFT_UPD ON TBECM_JR_FIW_PROFILE
FOR UPDATE
AS
BEGIN TRY
	 UPDATE TBECM_JR_FIW_PROFILE SET CHANGED_WHEN = GETDATE() FROM INSERTED 
	 WHERE TBECM_JR_FIW_PROFILE.GUID = INSERTED.GUID
	
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) 
		+ ' - ERROR-MESSAGE: ' 
		+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO

CREATE TABLE TBECM_JR_FIW_STEP
(
	GUID			INT				IDENTITY(1,1),        
	PROFILE_ID		INTEGER			NOT NULL,
	IDX_NAME		VARCHAR(100) 	NOT NULL,
	METHOD			VARCHAR(100)	NOT NULL,
	ARGUMENT1		VARCHAR(500)	NOT NULL	DEFAULT '',
	ARGUMENT2		VARCHAR(500)	NOT NULL	DEFAULT '',
	ARGUMENT3		VARCHAR(500)	NOT NULL	DEFAULT '',
	SCOPE			VARCHAR(50)		NOT NULL	DEFAULT '',
	ACTIVE   		BIT				NOT NULL	DEFAULT 1,
	COMMENT			VARCHAR(250),
	ADDED_WHO		VARCHAR(100)	NOT NULL	DEFAULT 'DEFAULT',
	ADDED_WHEN		DATETIME 					DEFAULT GETDATE(),
	CHANGED_WHO		VARCHAR(100),
	CHANGED_WHEN	DATETIME,
	CONSTRAINT		PKTBECM_JR_FIW_STEP	PRIMARY KEY(GUID),
	CONSTRAINT		FKTTBECM_JR_FIW_STEP_PROF_ID	FOREIGN KEY(PROFILE_ID)	REFERENCES TBECM_JR_FIW_PROFILE(GUID),
)
GO