USE [Elyse_DB] GO /****** Object: StoredProcedure [editing].[usp_INS_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: -- Creates a new release number for a given document. -- Output is a status message and a transaction status. /* COPYRIGHT NOTICE This database schema and stored procedures are protected by copyright. Copyright. Silkwood Software Pty. Ltd. 2026 */ -- ============================================= CREATE PROCEDURE [editing].[usp_INS_doc_release_id] @documentid nvarchar(50) = NULL, -- Mandatory @release_identifier nvarchar(50) = NULL, -- Mandatory @release_type_id bigint = NULL, -- Optional @listposition int = NULL, -- Optional @inputnotes nvarchar(max) = '', -- Optional @app_reference nvarchar(1000) = '', -- Application reference text. Optional @message nvarchar(1000) = NULL OUTPUT, @transaction_status nvarchar(50) = NULL OUTPUT, @newrecordid bigint = NULL OUTPUT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; DECLARE @nopermissionmessage nvarchar(200) = '', -- Communicates that the user does not have the required permission @connectedusersid varbinary(100) = SUSER_SID(ORIGINAL_LOGIN()), -- The SID of the connected user @sidid bigint = NULL, @username nvarchar(150) = ORIGINAL_LOGIN(), -- The username @userauthentication_status nchar(10) = 'Fail', -- The outcome of the authentication check of the user @temp_userauth_status nchar(10) = 'Fail', @tempmessage nvarchar(300) = '', @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. @tempvalidationstatus nchar(10) = '', @transaction_ready nchar(10) = 'Ready', @data_validation_status nchar(10) = 'Pass'; -- Status is pass unless it is set to 'Fail'. -- 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 document id IF @documentid = '' SET @documentid = NULL; IF @documentid IS NULL BEGIN -- A document id has not been supplied SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'NoDocID', @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 NoDocID.'); END ELSE -- doc id has been supplied BEGIN -- Check if the document ID exists and the user has edit permission for the document 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 edit permission for SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'DocIDNotExist', @message_text = @tempmessage OUTPUT; IF @tempmessage IS NOT NULL SET @message = CONCAT(@message, ' | ', 'Document ID: ', LEFT(@documentid, 20), '... ', @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on DocIDNotExist.'); END END -- Check the release identifier IF NULLIF(LTRIM(RTRIM(@release_identifier)), '') IS NULL SET @release_identifier = NULL; -- Check release identifier has been supplied IF @release_identifier IS NULL BEGIN SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'NoReleaseID', @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 NoReleaseID.'); END ELSE -- A release identifier has been supplied BEGIN IF EXISTS (SELECT doc_id FROM base.release_identifiers WHERE doc_id = @documentid AND release_identifier = @release_identifier) BEGIN EXEC internal.usp_SEL_message @message_id = 'RecordExists', @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 RecordExists.'); SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; END END -- Check the release type ID. Release Type is optional IF @release_type_id IS NOT NULL BEGIN IF NOT EXISTS (SELECT release_type_id FROM base.release_id_types WHERE release_type_id = @release_type_id) BEGIN EXEC internal.usp_SEL_message @message_id = 'ReleaseTypeNotExist', @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 ReleaseTypeNotExist.'); SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; END END -- Output the data validation status failed message 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 -- 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. BEGIN TRANSACTION; SET @documentid = (SELECT doc_id FROM base.document_id_list AS dil WHERE dil.doc_id = @documentid); INSERT INTO base.release_identifiers (doc_id, release_identifier, release_type_id, list_position, notes) VALUES (@documentid, @release_identifier, @release_type_id, @listposition, @inputnotes); SET @newrecordid = SCOPE_IDENTITY(); -- 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; 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, doc_id, record_id, original_value, new_value, notes, app_reference) VALUES (@username, @sidid, 'Create', 'Release', @documentid, @newrecordid, NULL, @release_identifier, @inputnotes, @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