/**
 * Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
 *
 * WSO2 LLC. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

CREATE OR ALTER PROCEDURE WSO2_OB_EVENT_NOTIFICATION_CLEANUP_DATA_RESTORE_SP
AS

BEGIN

DECLARE @rowCount INT;
DECLARE @enableLog BIT;
DECLARE @logLevel VARCHAR(10);

-- ------------------------------------------
-- CONFIGURABLE ATTRIBUTES
-- ------------------------------------------
SET @enableLog = 'TRUE'; -- ENABLE LOGGING [DEFAULT : TRUE]
SET @logLevel = 'TRACE'; -- SET LOG LEVELS : TRACE



IF (@enableLog = 1) BEGIN
SELECT  '[' + convert(varchar, getdate(), 121) + '] WSO2_OB_EVENT_NOTIFICATION_CLEANUP_DATA_RESTORE_SP STARTED .... !';
END

-- ---------------------

SELECT @rowCount = COUNT(1)  FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME IN ('OB_NOTIFICATION');
IF (@rowCount = 1)
BEGIN
IF (@enableLog = 1 AND @logLevel IN ('TRACE')) BEGIN
SELECT  '[' + convert(varchar, getdate(), 121) + '] CLEANUP DATA RESTORATION STARTED ON OB_NOTIFICATION TABLE !';
END
INSERT INTO dbo.OB_NOTIFICATION SELECT A.* FROM dbo.BAK_OB_NOTIFICATION AS A LEFT JOIN dbo.OB_NOTIFICATION AS B ON A.NOTIFICATION_ID = B.NOTIFICATION_ID WHERE B.NOTIFICATION_ID IS NULL;
SELECT @rowCount =  @@rowcount;
IF (@enableLog = 1 ) BEGIN
SELECT  '[' + convert(varchar, getdate(), 121) + '] CLEANUP DATA RESTORATION COMPLETED ON OB_NOTIFICATION WITH '+CAST(@rowCount as varchar)
END
END

-- ---------------------
SET IDENTITY_INSERT OB_NOTIFICATION_EVENT ON;
SELECT @rowCount = COUNT(1)  FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME IN ('OB_NOTIFICATION_EVENT');
IF (@rowCount = 1)
BEGIN
IF (@enableLog = 1 AND @logLevel IN ('TRACE')) BEGIN
SELECT  '[' + convert(varchar, getdate(), 121) + '] CLEANUP DATA RESTORATION STARTED ON OB_NOTIFICATION_EVENT TABLE !';
END
INSERT INTO dbo.OB_NOTIFICATION_EVENT (EVENT_ID, NOTIFICATION_ID, EVENT_TYPE, EVENT_INFO) SELECT A.EVENT_ID , A.NOTIFICATION_ID, A.EVENT_TYPE , A.EVENT_INFO FROM dbo.BAK_OB_NOTIFICATION_EVENT AS A LEFT JOIN dbo.OB_NOTIFICATION_EVENT AS B ON A.NOTIFICATION_ID = B.NOTIFICATION_ID WHERE B.NOTIFICATION_ID IS NULL;
SELECT @rowCount =  @@rowcount;
IF (@enableLog = 1 ) BEGIN
SELECT  '[' + convert(varchar, getdate(), 121) + '] CLEANUP DATA RESTORATION COMPLETED ON OB_NOTIFICATION_EVENT WITH '+CAST(@rowCount as varchar)
END
END
SET IDENTITY_INSERT OB_NOTIFICATION_EVENT OFF;
-- ---------------------

SELECT @rowCount = COUNT(1)  FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME IN ('OB_NOTIFICATION_ERROR');
IF (@rowCount = 1)
BEGIN
IF (@enableLog = 1 AND @logLevel IN ('TRACE')) BEGIN
SELECT  '[' + convert(varchar, getdate(), 121) + '] CLEANUP DATA RESTORATION STARTED ON OB_NOTIFICATION_ERROR TABLE !';
END
INSERT INTO dbo.OB_NOTIFICATION_ERROR SELECT A.* FROM dbo.BAK_OB_NOTIFICATION_ERROR AS A LEFT JOIN dbo.OB_NOTIFICATION_ERROR AS B ON A.NOTIFICATION_ID = B.NOTIFICATION_ID WHERE B.NOTIFICATION_ID IS NULL;
SELECT @rowCount =  @@rowcount;
IF (@enableLog = 1 ) BEGIN
SELECT  '[' + convert(varchar, getdate(), 121) + '] CLEANUP DATA RESTORATION COMPLETED ON OB_NOTIFICATION_ERROR WITH '+CAST(@rowCount as varchar)
END
END

IF (@enableLog = 1) BEGIN
SELECT  '[' + convert(varchar, getdate(), 121) + '] WSO2_OB_EVENT_NOTIFICATION_CLEANUP_DATA_RESTORE_SP COMPLETED .... !';
END

END
