IF NOT EXISTS(SELECT GUID FROM TBDD_MODULES WHERE SHORT_NAME = 'EMLP')
INSERT INTO TBDD_MODULES(GUID,NAME,SHORT_NAME,LICENSE,STRING1) VALUES 
(6,'Email-Profiler','EMLP','kc6j9HSv/UJVAzFjMZv2A3wHZFy+2nNLKP9Qs8g9EVY=','W')
GO
UPDATE TBDD_MODULES SET DB_VERSION = '1.3.0.0' where SHORT_NAME = 'EMLP' 
GO 
CREATE TABLE TBEMLP_CONFIG
(
	GUID           				TINYINT,
	FONT_BODY					VARCHAR(50)		NOT NULL DEFAULT 'Calibri',
	LOG_ERRORS_ONLY				BIT				NOT NULL DEFAULT 1,
	CHECK_INTERVALL_MINUTES		INTEGER			NOT NULL DEFAULT 5,
	LAST_TICK					DATETIME,
	PATH_EMAIL_TEMP				VARCHAR(1000)	NOT NULL,
	PATH_EMAIL_ERRORS			VARCHAR(1000)	NOT NULL,
	WM_CON_STRING				VARCHAR(1000)	NOT NULL,
	ADDED_WHEN					DATETIME				 DEFAULT GETDATE(),
	CHANGED_WHO	    			VARCHAR(50),
	CHANGED_WHEN  				DATETIME,
	CONSTRAINT					PK_TBEMLP_CONFIG_GUID PRIMARY KEY(GUID),
	CONSTRAINT 	  				CH_TBEMLP_CONFIG_GUID	CHECK(GUID = 1)
)
GO
CREATE TRIGGER TBEMLP_CONFIG_AFT_UPD ON TBEMLP_CONFIG
FOR UPDATE
AS
	UPDATE TBEMLP_CONFIG SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBEMLP_CONFIG.GUID = INSERTED.GUID
GO
INSERT INTO TBEMLP_CONFIG (GUID, PATH_EMAIL_TEMP,PATH_EMAIL_ERRORS,WM_CON_STRING) VALUES (1,'E:\TEMP\EMAIL_PROFILER\TEMP','E:\TEMP\EMAIL_PROFILER\ERROR','Data Source=172.24.12.41\tests;Initial Catalog=DD_ECM_TEST;Persist Security Info=True;User ID=sa;Password=dd')
GO
------------------------------------------------------------------------------

CREATE TABLE TBEMLP_POLL_PROFILES
(
	GUID            INTEGER		IDENTITY(1,1),              		
	PROFILE_NAME   	VARCHAR(100)	NOT NULL,	
	POLL_TYPE   	VARCHAR(100)	NOT NULL,						--IMAP OR POP
	EMAIL_CONF_ID	INTEGER			NOT NULL,
	COMMENT			VARCHAR(500),
	LAST_TICK		DATETIME,
	ACTIVE   	    BIT				DEFAULT 0				NOT NULL,	-- 
	SEQUENCE		INT				DEFAULT 1,
	ADDED_WHO	    VARCHAR(50)		DEFAULT 'Default',
	ADDED_WHEN	    DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO     VARCHAR(50),
	CHANGED_WHEN	DATETIME,
	CONSTRAINT		PK_TBEMLP_POLL_PROFILES	PRIMARY KEY (GUID)
)
GO
INSERT INTO TBEMLP_POLL_PROFILES (PROFILE_NAME,POLL_TYPE,EMAIL_CONF_ID) VALUES ('TEST1','POP',1)
GO
------------------------------------------------------------------------------
CREATE TRIGGER TBEMLP_POLL_PROFILES_AFT_UPD ON TBEMLP_POLL_PROFILES
FOR UPDATE
AS
	UPDATE TBEMLP_POLL_PROFILES SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBEMLP_POLL_PROFILES.GUID = INSERTED.GUID
GO
CREATE TABLE TBEMLP_POLL_PROCESS
(
	GUID				INTEGER		IDENTITY(1,1),              		
	PROCESS_NAME   		VARCHAR(100)	NOT NULL,	
	STEP_NAME			VARCHAR(50)		NOT NULL,			--'Easy Approval' bei ProcessManager,
	PROFILE_ID			INTEGER			NOT NULL,
	COPY_2_HDD			BIT				NOT NULL DEFAULT 0,
	WM_IDX_BODY_TEXT	VARCHAR(50) NOT NULL DEFAULT '',
	WM_IDX_BODY_SUBSTR_LENGTH	INTEGER NOT NULL DEFAULT 0,
	WM_IMPORT			BIT				NOT NULL DEFAULT 0,
	WM_OBJEKTTYPE		VARCHAR(100),
	WM_VECTOR_LOG		VARCHAR(100),
	WM_PATH				VARCHAR(1000),
	WM_FILE_NAME		VARCHAR(100),
	WM_REFERENCE_INDEX	VARCHAR(100),
	PATH_EMAIL_TEMP		VARCHAR(1000)	NOT NULL DEFAULT '',
	PATH_EMAIL_ERRORS	VARCHAR(1000)	NOT NULL DEFAULT '',
	PATH_ORIGINAL VARCHAR(1000),
	DELETE_MAIL			BIT				NOT NULL DEFAULT 0,
	USE_FOR_DIRECT_ANSWER BIT NOT NULL DEFAULT 0,
	COMMENT				VARCHAR(500),
	ACTIVE   			BIT				DEFAULT 1 NOT NULL,	--
	SEQUENCE			INT				DEFAULT 1,
	ADDED_WHO			VARCHAR(50)		DEFAULT 'Default',
	ADDED_WHEN			DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO			VARCHAR(50),
	CHANGED_WHEN		DATETIME,
	CONSTRAINT			PK_TBEMLP_POLL_PROCESS	PRIMARY KEY (GUID),
	CONSTRAINT			FK_TBEMLP_POLL_PROCESS_PROFILE_ID FOREIGN KEY (PROFILE_ID)  REFERENCES  TBEMLP_POLL_PROFILES (GUID)
)
GO
INSERT INTO TBEMLP_POLL_PROCESS (PROCESS_NAME,STEP_NAME,PROFILE_ID,PATH_EMAIL_TEMP,PATH_EMAIL_ERRORS,COPY_2_HDD,
WM_IMPORT,
WM_OBJEKTTYPE,
WM_VECTOR_LOG,
WM_PATH,
WM_FILE_NAME,
WM_REFERENCE_INDEX,
SEQUENCE,DELETE_MAIL) VALUES ('ProcessManager','Easy Approval',1,'E:\TEMP\EMAIL_PROFILER\TEMP','E:\TEMP\EMAIL_PROFILER\ERROR',1,
0,
'DIGITAL DATA - Entwicklung',
'Vektor_Text1',
'W:\Import_Temp',
'EA_[%DATE]_',
'Dokument-ID',
1,1)
GO
CREATE TRIGGER TBEMLP_POLL_PROCESS_AFT_UPD ON TBEMLP_POLL_PROCESS
FOR UPDATE
AS
	UPDATE TBEMLP_POLL_PROCESS SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBEMLP_POLL_PROCESS.GUID = INSERTED.GUID
GO
------------------------------------------------------------------------------
--select * from TBEMLP_POLL_STEPS
--GO
CREATE TABLE TBEMLP_POLL_STEPS
(
	GUID				INTEGER			IDENTITY(1,1),      
	PROCESS_ID			INTEGER			NOT NULL,        		
	STEP_NAME			VARCHAR(50)		NOT NULL,			--'z.B. Invoice Approved or Invoice Disapproved' bei ProcessManager,
	KEYWORDS_BODY		VARCHAR(1000),
	COMMENT				VARCHAR(500),
	ACTIVE   			BIT				DEFAULT 1 NOT NULL,	--
	SEQUENCE			INT				DEFAULT 1,
	ADDED_WHO			VARCHAR(50)		DEFAULT 'Default',
	ADDED_WHEN			DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO			VARCHAR(50),
	CHANGED_WHEN		DATETIME,
	CONSTRAINT			PK_TBEMLP_POLL_STEPS	PRIMARY KEY (GUID),
	CONSTRAINT			FK_TBEMLP_POLL_STEPS_PROCESS_ID FOREIGN KEY (PROCESS_ID)  REFERENCES  TBEMLP_POLL_PROCESS (GUID)
)
GO
INSERT INTO TBEMLP_POLL_STEPS (PROCESS_ID,STEP_NAME,KEYWORDS_BODY,ACTIVE) VALUES (1,'Invoice Disapproved','Disapproved;Abgelehnt',1)
GO
INSERT INTO TBEMLP_POLL_STEPS (PROCESS_ID,STEP_NAME,KEYWORDS_BODY,ACTIVE) VALUES (1,'Invoice Approved','OK;Approved;OK',1)
GO
CREATE TRIGGER TBEMLP_POLL_STEPS_AFT_UPD ON TBEMLP_POLL_STEPS
FOR UPDATE
AS
	UPDATE TBEMLP_POLL_STEPS SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBEMLP_POLL_STEPS.GUID = INSERTED.GUID
GO
CREATE TABLE TBEMLP_POLL_INDEXING_STEPS
(
	GUID					SMALLINT		IDENTITY(1,1),      
	STEP_ID					INTEGER			NOT NULL,        		
	INDEXNAME				VARCHAR(100)	NOT NULL,			--'Easy Approval' bei ProcessManager,
	INDEXVALUE				VARCHAR(100)	NOT NULL,
	USE_FOR_DIRECT_ANSWER	BIT			NOT NULL DEFAULT 0,
	ACTIVE   				BIT				DEFAULT 1 NOT NULL,	--
	SEQUENCE				INT				DEFAULT 1,
	ADDED_WHO				VARCHAR(50)		DEFAULT 'Default',
	ADDED_WHEN				DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO				VARCHAR(50),
	CHANGED_WHEN			DATETIME,
	CONSTRAINT				PK_TBEMLP_POLL_INDEXING_STEPS	PRIMARY KEY (GUID),
	CONSTRAINT				FK_TBEMLP_POLL_INDEXING_STEPS_STEP_ID FOREIGN KEY (STEP_ID)  REFERENCES  TBEMLP_POLL_STEPS (GUID)
)
GO
CREATE TRIGGER TBEMLP_POLL_INDEXING_STEPS_AFT_UPD ON TBEMLP_POLL_INDEXING_STEPS
FOR UPDATE
AS
	UPDATE TBEMLP_POLL_INDEXING_STEPS SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBEMLP_POLL_INDEXING_STEPS.GUID = INSERTED.GUID
GO
INSERT INTO TBEMLP_POLL_INDEXING_STEPS (STEP_ID,INDEXNAME,INDEXVALUE) VALUES (1,'Dokumentart','Email-Approved')
GO
INSERT INTO TBEMLP_POLL_INDEXING_STEPS (STEP_ID,INDEXNAME,INDEXVALUE) VALUES (2,'Dokumentart','Email-Disapproved')
GO
--select * from TBEMLP_POLL_INDEXING_STEPS
--GO
CREATE TABLE TBEMLP_EMAIL_OUT
(
	GUID				INTEGER		NOT NULL	IDENTITY(1,1),
	REMINDER_TYPE_ID	INTEGER		NOT NULL DEFAULT '1', 
	SENDING_PROFILE		INTEGER		NOT NULL,
	REFERENCE_ID		INTEGER		NOT NULL,
	REFERENCE_STRING	VARCHAR(200),
	ENTITY_ID			INTEGER,	
	WF_ID				INTEGER		NOT NULL,
	WF_REFERENCE		VARCHAR(200),
	EMAIL_ADRESS		VARCHAR(1000)	NOT NULL,
	EMAIL_SUBJ			VARCHAR(500)	NOT NULL,
	EMAIL_BODY			VARCHAR(500)	NOT NULL,
	EMAIL_ATTMT1		VARCHAR(512),
	EMAIL_SENT			DATETIME,
	COMMENT				VARCHAR(500),
	ADDED_WHO			VARCHAR(50)	NOT NULL	DEFAULT 'DEFAULT',
	ADDED_WHEN			DATETIME 				DEFAULT GETDATE(),
	CHANGED_WHO			VARCHAR(50),
	CHANGED_WHEN		DATETIME,
	ERROR_TIMESTAMP DATETIME,
	ERROR_MSG VARCHAR(900),
	CONSTRAINT			PK_TBEMLP_EMAIL_OUT		PRIMARY KEY (GUID),
	CONSTRAINT			FK_TBEMLP_EMAIL_OUT_SENDING_PROFILE FOREIGN KEY (SENDING_PROFILE)  REFERENCES  TBDD_EMAIL_ACCOUNT (GUID)
)
GO
CREATE TABLE TBEMLP_HISTORY
(
	GUID				INTEGER		IDENTITY(1,1),              		
	WORK_PROCESS		VARCHAR(100),
	EMAIL_MSGID			VARCHAR(500)			NOT NULL,
	EMAIL_FROM VARCHAR(500) NOT NULL DEFAULT 'NONE',
	EMAIL_SUBJECT		VARCHAR(MAX),
	EMAIL_DATE			DATETIME,
	EMAIL_BODY			VARCHAR(MAX),
	EMAIL_SUBSTRING1	VARCHAR(2000),
	EMAIL_SUBSTRING2	VARCHAR(2000),
	DATE_DELETED_INBOX DATETIME,
	COMMENT				VARCHAR(500),
	ADDED_WHEN	    DATETIME 		DEFAULT GETDATE(),
	CONSTRAINT		PK_TBEMLP_HISTORY	PRIMARY KEY (GUID)
)
GO
CREATE TABLE TBEMLP_HISTORY_STATE
(
GUID					INTEGER		    NOT NULL 	IDENTITY (1, 1),
MESSAGE_ID				VARCHAR(900)	NOT NULL,
STATE_TITLE				VARCHAR(900) 	NOT NULL,
STATE_TITLE1			VARCHAR(900) ,
COMMENT					VARCHAR(3000),
ADDED_WHO				VARCHAR(30)     DEFAULT 'Digital Data',
ADDED_WHEN   			DATETIME 	    DEFAULT (GETDATE()),
CHANGED_WHO				VARCHAR(30),
CHANGED_WHEN			DATETIME,
CONSTRAINT PK_TBEMLP_HISTORY_STATE     	   PRIMARY KEY (GUID)
)
GO
CREATE PROCEDURE PRCUST_ADD_HISTORY_STATE @MessageID VARCHAR(250), @TITLE1 VARCHAR(250), @TITLE2 VARCHAR(250), @COMMENT VARCHAR(3000) = 0
AS 
BEGIN
	INSERT INTO TBEMLP_HISTORY_STATE
		(MESSAGE_ID,STATE_TITLE,STATE_TITLE1,COMMENT) VALUES (@MessageID,@TITLE1,@TITLE2,@COMMENT)
END
GO
-- =============================================
-- Author:			DD MS
-- Creation date:	07.05.2020
-- =============================================
CREATE PROCEDURE PREMAIL_NEW_EMAIL @SENDING_PROFILE TINYINT, @EMAIL_TO VARCHAR(100),@SUBJECT VARCHAR(200),@BODY VARCHAR(MAX),@REF_STRING As VARCHAR(900),@REF_ID BIGINT,@WF_ID INT,@WHO VARCHAR(100) AS 
BEGIN
	INSERT INTO [dbo].[TBEMLP_EMAIL_OUT]
		([SENDING_PROFILE]
		,[WF_ID]
		,[EMAIL_ADRESS]
		,[EMAIL_SUBJ]
		,[EMAIL_BODY]
		,REFERENCE_STRING
		,REFERENCE_ID
		,[ADDED_WHO])
	VALUES (@SENDING_PROFILE,
		@WF_ID,
		@EMAIL_TO,
		@SUBJECT,
		@BODY,
		@REF_STRING,
		@REF_ID,
		@WHO)
END
GO


CREATE TABLE dbo.TBEDMI_ITEM_VALUE(
	GUID				BIGINT NOT NULL 	IDENTITY (1, 1),
	REFERENCE_GUID		nvarchar(max) NOT NULL,
	ITEM_DESCRIPTION	nvarchar(max) NOT NULL,
	ITEM_VALUE			nvarchar(max) NULL,
	CREATEDWHEN			datetime	NOT NULL DEFAULT GETDATE(),
	CREATEDWHO			nvarchar(max),
	CHANGEDWHEN			datetime,
	GROUP_COUNTER		integer NULL,
	SPEC_NAME			VARCHAR(250),
	IS_REQUIRED			BIT NOT NULL DEFAULT 0
) 
GO

