alter proc [dbo].[proc_ofacOtherDataManagement]
	 @flag			VARCHAR(25)		= NULL	
	,@user			VARCHAR(50)		= NULL
	,@xml           XML             = NULL
AS

SET NOCOUNT ON;

BEGIN TRY

	CREATE TABLE #msg(errorCode INT, msg VARCHAR(100), id INT)	
	DECLARE @dataSource VARCHAR(30)

     if @flag='OTHER'
     begin
			SELECT 1 error_code,'No Record Found.' mes,NULL
			return;
			
     end
	 else if @flag='EXTERNAL-DATA-UPLOAD'
	 begin
			BEGIN TRANSACTION;
			SELECT  Position = p.value('@POSITION', 'varchar(50)') ,
					[FullName] = p.value('@NAME', 'varchar(25)') ,
					Country = p.value('@COUNTRY', 'varchar(25)') 
                      
			INTO    #TEMP1
			FROM    @xml.nodes('/root/row') AS tmp ( p );

			Declare @fileName varchar(100); 
			Select @fileName = 'ExternalDataUpload_' + FORMAT(GETDATE(), 'yyyyMMddhhmmssfff'), @dataSource = 'CIA';

			DELETE T FROM #TEMP1 T
			INNER JOIN blacklist(NOLOCK) L ON l.name = T.FullName and L.country = T.Country

			INSERT INTO blacklistLog (totalRecord, dataSource, createdBy, createdDate)
			SELECT COUNT(1), @dataSource, @user, GETDATE() FROM #TEMP1

			INSERT INTO blacklist (OFACKEY,ENTNUM,NAME,VESSELTYPE,COUNTRY,REMARKS,SORTORDER,FROMFILE,DATASOURCE,INDENT,SOURCEENTNUM,CREATEDDATE,CREATEDBY)
			SELECT '', '',FullName,'CIA',Country,Position,4,@fileName,@dataSource,'C','C'+ FORMAT(GETDATE(), 'yyyyMMddhhmmssfff'),GETDATE(),@user
			FROM #TEMP1
			
			UPDATE blacklist SET ENTNUM = 'CIA'+ CAST(rowId AS VARCHAR),OFACKEY = 'CIA'+ CAST(rowId AS VARCHAR)  WHERE DATASOURCE = 'CIA'

			MERGE blackListHistory AS blh
			USING (SELECT rowId, ofacKey, entNum, name, vesselType, address, city, state, zip, country, remarks, sortOrder, fromFile, dataSource, indEnt, sourceEntNum FROM blacklist WITH(NOLOCK) WHERE dataSource = @dataSource ) AS bl
				ON	ISNULL(blh.ofacKey, '')				= ISNULL(bl.ofacKey, '') AND			ISNULL(blh.entNum, '')		= ISNULL(bl.entNum, '') 
					AND ISNULL(blh.name, '')			= ISNULL(bl.name, '') AND				ISNULL(blh.vesselType, '')	= ISNULL(bl.vesselType, '') 
					AND ISNULL(blh.address, '')			= ISNULL(bl.address, '') AND			ISNULL(blh.city, '')		= ISNULL(bl.city, '') 
					AND ISNULL(blh.state, '')			= ISNULL(bl.state, '') AND				ISNULL(blh.zip, '')			= ISNULL(bl.zip, '') 
					AND ISNULL(blh.country, '')			= ISNULL(bl.country, '') AND			ISNULL(blh.remarks, '')		= ISNULL(bl.remarks, '') 
					AND ISNULL(blh.sortOrder, '')		= ISNULL(bl.sortOrder, '') AND			ISNULL(blh.fromFile, '')	= ISNULL(bl.fromFile, '') 
					AND ISNULL(blh.dataSource, '')		= ISNULL(bl.dataSource, '') AND			ISNULL(blh.indEnt, '')		= ISNULL(bl.indEnt, '')
					AND ISNULL(blh.sourceEntNum, '')	= ISNULL(bl.sourceEntNum, '') AND bl.dataSource = @dataSource 
			WHEN NOT MATCHED THEN
			INSERT(blackListId, ofacKey, entNum, name, vesselType, address, city, state, zip, country, remarks, sortOrder, fromFile, dataSource, indEnt, sourceEntNum)
			VALUES(bl.rowId, bl.ofacKey, bl.entNum, bl.name, bl.vesselType, bl.address, bl.city, bl.state, bl.zip, bl.country, bl.remarks, bl.sortOrder, bl.fromFile, bl.dataSource, bl.indEnt, bl.sourceEntNum);

			IF @@TRANCOUNT > 0
			COMMIT TRANSACTION

			SELECT 0 error_code,'PEPS CIA Data uploaded successfully' mes,NULL
			
			RETURN;
	 END
	 ELSE IF @flag = 'Export-Data'
	 BEGIN
		SELECT  ofacKey,entNum,name,vesselType,address,city,country,remarks,dataSource,createdDate,createdBy 
		FROM blacklist(NOLOCK)
		WHERE isActive='Y'
	 END
END TRY

BEGIN CATCH

	IF @@TRANCOUNT > 0
     ROLLBACK TRANSACTION
     SELECT 1 error_code, ERROR_MESSAGE() mes, null id

END CATCH




