USE [Elyse_DB] GO /****** Object: StoredProcedure [editing].[usp_INS_file_doc_release_id] Script Date: Sun 22-11-2026 9:04:49 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: Silkwood Software -- Create date: 05-07-2026 -- Description: Initial creation -- Inserts a document release ID for a given file. -- A check is made of the file to doc link table. If the doc ID linked to the file -- by the release ID is not already linked to the file then a record shall be created which creates -- the link in the xref.file_to_document_links table. /* COPYRIGHT NOTICE This database schema and stored procedures are protected by copyright. Copyright. Silkwood Software Pty. Ltd. 2026 */ -- ============================================= CREATE PROCEDURE [editing].[usp_INS_file_doc_release_id] @fileid bigint = NULL, @releaseid bigint = NULL, @notes nvarchar(1000) = '', -- Optional @app_reference nvarchar(1000) = '', -- Optional @message nvarchar(1000) = NULL OUTPUT, @transaction_status nvarchar(50) = NULL OUTPUT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; DECLARE @connectedusersid varbinary(100) = SUSER_SID(ORIGINAL_LOGIN()), -- The SID of the connected user @username nvarchar(150) = ORIGINAL_LOGIN(), -- The username @sidid bigint = NULL, @release_identifier nvarchar(50) = '', @existing_rel_id nvarchar(50) = '', @release_docid nvarchar(50) = '', @old_release_docid nvarchar(50) = '', @userauthentication_status nchar(10) = 'Fail', -- The outcome of the authentication check of the user @tempmessage nvarchar(300) = '', @temp_userauth_status nchar(10) = 'Fail', @fileauthenticationstatus nchar(10) = '', -- Communicates if the user is not authorised to access the file -- or alternatively if the file does not exist. The result does not distinguish either. @docauthenticationstatus nchar(10) = '', -- Communicates if the user is not authorised to access the document -- or alternatively if the document does not exist. The result does not distinguish either. @documentid nvarchar(50) = '', @transaction_ready nchar(10) = 'Ready', @data_validation_status nchar(10) = 'Pass'; -- Parameters which have been initialised at declaration but not explicitly set might be output as null to calling functions. SET @transaction_status = 'Transaction not attempted'; -- Connected user authentication -- Authenticate the connected user for the role EXEC [internal].[usp_AUTHENTICATE_user_role] @role_to_check = 'Controller', @user_authentication_result = @temp_userauth_status OUTPUT; IF @temp_userauth_status = 'Pass' SET @userauthentication_status = 'Pass'; EXEC [internal].[usp_AUTHENTICATE_user_role] @role_to_check = 'Editor', @user_authentication_result = @temp_userauth_status OUTPUT; IF @temp_userauth_status = 'Pass' SET @userauthentication_status = 'Pass'; IF @userauthentication_status = 'Fail' BEGIN -- The user does not have permission for this action SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'NoPermission', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT(@message, ' | ', ISNULL(@username, ''), ' ', @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on NoPermission.'); END IF @userauthentication_status = 'Pass' -- Don't do anything if the user is not authorised. BEGIN -- Data validation -- Check the release ID IF @releaseid = 0 SET @releaseid = NULL; IF @releaseid IS NULL BEGIN SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'ReleaseIDNotSupplied', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT(@message, ' | ', @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on ReleaseIDNotSupplied.'); END ELSE -- An ID has been supplied. Check that it is valid. IF NOT EXISTS (SELECT release_id_pk FROM base.release_identifiers WHERE release_id_pk = @releaseid) BEGIN SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'ReleaseIDNotExist', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT(@message, ' | ', @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on ReleaseIDNotExist.'); END -- Check the document ID which is linked to the release ID. -- Check if the document ID exists and the user has permission to edit the document data IF @data_validation_status = 'Pass' BEGIN SET @documentid = (SELECT doc_id FROM base.release_identifiers WHERE release_id_pk = @releaseid) EXEC [internal].[usp_AUTHENTICATE_doc_ed_perm] @doc_id_to_check_ep = @documentid, @user_authentication_result_dep = @docauthenticationstatus OUTPUT; IF @docauthenticationstatus = 'Fail' BEGIN -- Doc id does not exist or the user does not have permission to edit it SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'NoDocIDAccess', @message_text = @tempmessage OUTPUT; IF @tempmessage IS NOT NULL SET @message = CONCAT_WS(' | ', @message, @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on NoDocIDAccess.'); END END -- Check that a file id was supplied IF @fileid = 0 SET @fileid = NULL; IF @fileid IS NULL BEGIN SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'NoFileID', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT(@message, ' | ', @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on NoFileID.'); END ELSE -- Check that the file id is valid BEGIN -- Check if it exists and the user has permission to edit the File data EXEC [internal].[usp_AUTHENTICATE_file_ed_perm] @file_id_to_check_ep = @fileid, @user_authentication_result_fep = @fileauthenticationstatus OUTPUT; IF @fileauthenticationstatus = 'Fail' BEGIN -- file id does not exist or the user does not have permission to edit it SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'NoFileIDAccess', @message_text = @tempmessage OUTPUT; IF @tempmessage IS NOT NULL SET @message = CONCAT(@message, ' | ', 'File ID: ', CONVERT(nvarchar(20), @fileid), '. ', @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on NoFileIDAccess.'); END END -- Check the release lock. Only permit change if no release ID has already been set. IF EXISTS (SELECT gsg.release_lock FROM base.global_settings_groups AS gsg WHERE gsg.setting_group_name = 'Master' AND gsg.release_lock = 'On') AND EXISTS (SELECT release_id_pk FROM base.file_metadata WHERE file_id = @fileid AND release_id_pk IS NOT NULL) BEGIN SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'ReleaseLockOn', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT(@message, ' | ', @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on ReleaseLockOn.'); END -- Change is not permitted if Release State Type is Authoritative or Historical. IF EXISTS (SELECT release_state_type_id FROM base.release_identifiers WHERE release_id_pk = @releaseid AND (release_state_type_id = 'Authoritative' OR release_state_type_id = 'Historical')) BEGIN EXEC internal.usp_SEL_message @message_id = 'RelIdChgNotPermitted', @message_text = @tempmessage OUTPUT; IF @tempmessage IS NOT NULL SET @message = CONCAT_WS(' | ', @message, @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on RelIdChgNotPermitted.'); SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; END IF @data_validation_status = 'Fail' BEGIN EXEC internal.usp_SEL_message @message_id = 'FailedDataValidation', @message_text = @tempmessage OUTPUT; IF @tempmessage IS NOT NULL SET @message = CONCAT_WS(' | ', @message, @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on FailedDataValidation.'); END -- End data validation END -- END User authentication = Pass ----============================================================================================= -- Execute the insert query IF @transaction_ready = 'Ready' BEGIN BEGIN TRY BEGIN TRANSACTION -- Noting that docid is not case sensitive, the code should to store the docid -- with the same letter case as is stored rather than what was supplied. SET @documentid = (SELECT doc_id FROM base.document_id_list AS dil WHERE dil.doc_id = @documentid); -- Check if the doc_id - file_id link pair record already exists IF NOT EXISTS (SELECT file_id FROM xref.file_to_document_links WHERE file_id = @fileid AND doc_id = @documentid) BEGIN -- Create a new file-doc link INSERT INTO xref.file_to_document_links (doc_id, file_id) VALUES (@documentid, @fileid); END -- Select the previous release identifier SELECT @existing_rel_id = rid.release_identifier FROM base.file_metadata AS fm LEFT JOIN base.release_identifiers AS rid ON fm.release_id_pk = rid.release_id_pk WHERE fm.file_id = @fileid; -- Select the document id linked to the previous release identifier SELECT @old_release_docid = rid.doc_id FROM base.file_metadata AS fm LEFT JOIN base.release_identifiers AS rid ON fm.release_id_pk = rid.release_id_pk WHERE fm.file_id = @fileid; -- Select the document ID linked to the new release identifier SELECT @release_docid = rid.doc_id FROM base.release_identifiers AS rid WHERE rid.release_id_pk = @releaseid; -- ====================== -- Add the release id to the file id. UPDATE base.file_metadata SET release_id_pk = @releaseid WHERE file_id = @fileid; -- ====================== -- Create an audit log entry -- Select the SID ID for the connected user SELECT @sidid = sl.sid_id FROM user_restr.sid_list AS sl WHERE sl.sid = @connectedusersid; -- Select the release identifer SELECT @release_identifier = rid.release_identifier FROM base.release_identifiers AS rid WHERE rid.release_id_pk = @releaseid; IF EXISTS (SELECT gsg.file_doc_change_log FROM base.global_settings_groups AS gsg WHERE gsg.setting_group_name = 'Master' AND gsg.file_doc_change_log = 'On') BEGIN INSERT INTO base.file_doc_data_log (created_by_username, created_by_sid_id, change_type, change_field, file_id, doc_id, record_id, record_name, original_value, new_value, notes, app_reference) VALUES (@username, @sidid, 'Update', 'File-Doc Release', @fileid, @old_release_docid, @releaseid, @release_docid, @existing_rel_id, @release_identifier, @notes, @app_reference) END COMMIT TRANSACTION EXEC internal.usp_SEL_message @message_id = 'Success', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT_WS(' | ', @message, @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on Success.'); SET @transaction_status = 'Good'; END TRY BEGIN CATCH IF XACT_STATE() <> 0 ROLLBACK TRANSACTION; SET @transaction_status = 'Bad'; EXEC internal.usp_SEL_message @message_id = 'InsertError', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT(@message, ' | ', @tempmessage, ' | ', CONVERT(nvarchar(10),ERROR_NUMBER()), ' | ', ERROR_MESSAGE()); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on InsertError.'); END CATCH END END GO