USE [Elyse_DB] GO /****** Object: StoredProcedure [controlling].[usp_UPD_release_state] 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: 20-09-2026 -- Description: -- Update the release state of a release ID for a given document. -- Adds release state, effective from and effective until. -- The following rules apply: -- A release which has release_state_type of 'Mutable' can only be changed to 'Authoritative' or another 'Mutable' type. -- A release which has release_state_type of 'Authoritative' can only be changed to 'Historical' or another 'Authoritative' type. -- A release which has release_state_type of 'Historical' can only be changed to 'Authoritative' or another of type 'Historical', except if enforced_retention is off. -- For a given document ID, only one release can have release_state_type of Authoritative. -- If another existing release for that document ID already has a -- release_state_type = 'Authoritative' then change it to the default 'Historical' -- and set the new one to 'Authoritative'. /* COPYRIGHT NOTICE This database schema and stored procedures are protected by copyright. Copyright. Silkwood Software Pty. Ltd. 2026 */ -- ============================================= CREATE PROCEDURE [controlling].[usp_UPD_release_state] @releaseid bigint = NULL, -- Mandatory @release_state_name_id bigint = NULL, -- Mandatory @effective_from datetime2(7) = NULL, -- Optional @effective_until datetime2(7) = 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 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, @userauthentication_status nchar(10) = 'Fail', -- The outcome of the authentication check of the user @tempmessage nvarchar(300) = '', @temp_userauth_status nchar(10) = 'Fail', @existing_release_state_name nvarchar(50) = NULL, -- The existing release state name string of the record which is to be changed. @existing_release_state_type nvarchar(20) = NULL, -- The release state type of the record which is to be changed. @new_release_state_name nvarchar(50) = NULL, -- The release state name string which the record is to be changed to @new_release_state_type nvarchar(20) = NULL, -- The release state type which the record is to be updated to @existing_auth_release_id_pk bigint = NULL, -- The release ID of the record which is the existing Authoritative release. @existing_auth_rel_state_name nvarchar(50) = NULL, -- The release state name of the record which is the existing Authoritative release. @existing_effective_from datetime2(7) = NULL, @existing_effective_until datetime2(7) = NULL, @release_identifier_name nvarchar(50) = NULL, -- The release identifier name string for the record to be updated. @existing_auth_rel_id_name nvarchar(50) = NULL, -- The release ID name string of the record which is presently Authoritative @def_rel_state_name_id bigint = NULL, -- The release state name ID which is the historical default. @def_rel_state_name nvarchar(50) = NULL, -- The release state name which is the historical default. @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'; 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 1 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 -- Validate @release_state_name_id IF @release_state_name_id = 0 SET @release_state_name_id = NULL; -- Check record id exists IF @release_state_name_id IS NULL BEGIN SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'NoRelStNamID', @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 NoRelStNamID.'); END ELSE -- A record id has been supplied BEGIN -- Check that the record id exists IF NOT EXISTS (SELECT 1 FROM base.release_state_names WHERE release_state_name_id = @release_state_name_id) BEGIN -- Record id does not exist SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'RelStNamIDNotExist', @message_text = @tempmessage OUTPUT; IF @tempmessage IS NOT NULL SET @message = CONCAT(@message, ' | ', 'Record ID: ', CONVERT(nvarchar(20), @release_state_name_id), '. ', @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on RelStNamIDNotExist.'); END END -- Check the change of release state type rules. SELECT @new_release_state_name = rsn.release_state_name, @new_release_state_type = rsn.release_state_type_id FROM base.release_state_names AS rsn WHERE rsn.release_state_name_id = @release_state_name_id; -- Check the release state type. SET @existing_release_state_type = (SELECT release_state_type_id FROM base.release_state_names WHERE release_state_name_id = (SELECT release_state_name_id FROM base.release_identifiers WHERE release_id_pk = @releaseid)) IF @existing_release_state_type IS NOT NULL AND @new_release_state_type <> @existing_release_state_type BEGIN -- Mutable, can only change to Authoritative IF @existing_release_state_type = 'Mutable' AND @new_release_state_type NOT IN ('Mutable', 'Authoritative') BEGIN SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'InvalidRelStTypChg', @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 InvalidRelStTypChg.'); END ELSE -- If Authoritative, can only change to Historical IF @existing_release_state_type = 'Authoritative' AND @new_release_state_type <> 'Historical' BEGIN SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'InvalidRelStTypChg', @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 InvalidRelStTypChg.'); END ELSE -- If Historical, can only change to Authoritative or Historical, except if enforced_retention is not On. IF @existing_release_state_type = 'Historical' AND @new_release_state_type = 'Mutable' AND EXISTS (SELECT 1 FROM base.global_settings_groups AS gsg WHERE gsg.setting_group_name = 'Master' AND gsg.enforced_retention = 'On') BEGIN SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'InvalidRelStTypChg', @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 InvalidRelStTypChg.'); END END -- END IF @new_release_state_type <> @existing_release_state_type -- Validate the effective dates IF @effective_from IS NOT NULL AND @effective_until IS NOT NULL AND @effective_from > @effective_until BEGIN SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'EffDatesInvalid', @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 EffDatesInvalid.'); 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 update query IF @transaction_ready = 'Ready' BEGIN BEGIN TRY BEGIN TRANSACTION -- ============================================================ -- Serialize all release-state changes for this document. -- ============================================================ SELECT ri.release_id_pk FROM base.release_identifiers AS ri WITH (UPDLOCK, HOLDLOCK) WHERE ri.doc_id = @documentid; -- Retrieve and lock the release being changed. -- The lock is held until the transaction completes. SELECT @documentid = ri.doc_id, @existing_release_state_name = rsn.release_state_name, @existing_release_state_type = rsn.release_state_type_id FROM base.release_identifiers AS ri INNER JOIN base.release_state_names AS rsn ON rsn.release_state_name_id = ri.release_state_name_id WHERE ri.release_id_pk = @releaseid; -- 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 the new release state type is Authoritative and the existing is not, then change the existing Authoritative record to the default Historical. IF @new_release_state_type = 'Authoritative' AND ISNULL(@existing_release_state_type, '') <> 'Authoritative' BEGIN -- Find existing Authoritative release ID for that document. SET @existing_auth_release_id_pk = (SELECT release_id_pk FROM base.release_identifiers AS ri INNER JOIN base.release_state_names AS rsn ON ri.release_state_name_id = rsn.release_state_name_id INNER JOIN base.release_state_types AS rst ON rsn.release_state_type_id = rst.release_state_type_id WHERE rst.release_state_type_id = 'Authoritative' AND ri.doc_id = @documentid) IF @existing_auth_release_id_pk IS NOT NULL -- In case there is no Authoritative release. BEGIN -- Before changing it, retrieve the release state name of the record which is presently Authoritative SET @existing_auth_rel_state_name = (SELECT release_state_name FROM base.release_state_names AS rsn INNER JOIN base.release_identifiers AS ri ON rsn.release_state_name_id = ri.release_state_name_id WHERE ri.release_id_pk = @existing_auth_release_id_pk) -- Concatenate with the release state type. SET @existing_auth_rel_state_name = CONCAT(@existing_auth_rel_state_name, ' | ', (SELECT rsn.release_state_type_id FROM base.release_state_names AS rsn INNER JOIN base.release_identifiers AS ri ON rsn.release_state_name_id = ri.release_state_name_id WHERE ri.release_id_pk = @existing_auth_release_id_pk)) -- Set the existing Authoritative release ID to Historical, using the default Release State Name for Release State Type = Historical. SET @def_rel_state_name_id = (SELECT release_state_name_id FROM base.release_state_names WHERE historical_default = 'Default') -- Retrieve the default release state name. SET @def_rel_state_name = (SELECT release_state_name FROM base.release_state_names WHERE historical_default = 'Default') -- Concatenate with the release state type. SET @def_rel_state_name = CONCAT(@def_rel_state_name, ' | ', (SELECT release_state_type_id FROM base.release_state_names WHERE historical_default = 'Default')) -- Change the release state of the release which is presently Authoritative to the default historical release state. UPDATE base.release_identifiers SET release_state_name_id = @def_rel_state_name_id, release_state_type_id = 'Historical', when_made_historical = SYSDATETIME() WHERE release_id_pk = @existing_auth_release_id_pk -- Retrieve the release identifier name string for the record which is the existing authoritative release. SET @existing_auth_rel_id_name = (SELECT release_identifier FROM base.release_identifiers AS ri WHERE release_id_pk = @existing_auth_release_id_pk) -- Add to the message to advise that this has been done EXEC internal.usp_SEL_message @message_id = 'StateAutoChg', @message_text = @tempmessage OUTPUT; IF @tempmessage IS NOT NULL SET @message = CONCAT(@tempmessage, ' ', @existing_auth_rel_id_name, ' | ', @message); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on StateAutoChg.'); -- Create audit log of updating of the superseded record. 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, record_name, original_value, new_value, notes, app_reference) VALUES (@username, @sidid, 'Update', 'Release State', @documentid, @existing_auth_release_id_pk, @existing_auth_rel_id_name, @existing_auth_rel_state_name, @def_rel_state_name, @inputnotes, @app_reference) END END -- END IF @existing_auth_release_id_pk IS NOT NULL END -- END IF @new_release_state_type = 'Authoritative' and... -- Retrieve the release state name string for the record which is to be updated SET @existing_release_state_name = (SELECT release_state_name FROM base.release_state_names WHERE release_state_name_id = (SELECT release_state_name_id FROM base.release_identifiers WHERE release_id_pk = @releaseid)) -- Concatenate with the release state type. SET @existing_release_state_name = CONCAT(@existing_release_state_name, ' | ', (SELECT release_state_type_id FROM base.release_state_names WHERE release_state_name_id = (SELECT release_state_name_id FROM base.release_identifiers WHERE release_id_pk = @releaseid)) ) -- Retrieve the existing effective_from date SET @existing_effective_from = (SELECT effective_from FROM base.release_identifiers WHERE release_id_pk = @releaseid) -- Retrieve the existing effective_until date SET @existing_effective_until = (SELECT effective_until FROM base.release_identifiers WHERE release_id_pk = @releaseid) -- Update the record to be changed. UPDATE base.release_identifiers SET release_state_name_id = @release_state_name_id, release_state_type_id = @new_release_state_type, effective_from = @effective_from, effective_until = @effective_until WHERE release_id_pk = @releaseid; IF @new_release_state_type = 'Authoritative' BEGIN UPDATE base.release_identifiers SET when_made_authoritative = SYSDATETIME() WHERE release_id_pk = @releaseid; END -- Create audit log entries. 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 -- Retrieve the release identifier name string for the record which was updated. SET @release_identifier_name = (SELECT release_identifier FROM base.release_identifiers WHERE release_id_pk = @releaseid) -- Retrieve the release state name which the record was changed to SET @new_release_state_name = (SELECT release_state_name FROM base.release_state_names WHERE release_state_name_id = @release_state_name_id) -- Concatenate with the release state type. SET @new_release_state_name = CONCAT(@new_release_state_name, ' | ', (SELECT release_state_type_id FROM base.release_state_names WHERE release_state_name_id = @release_state_name_id) ) INSERT INTO base.file_doc_data_log (created_by_username, created_by_sid_id, change_type, change_field, doc_id, record_id, record_name, original_value, new_value, notes, app_reference) VALUES (@username, @sidid, 'Update', 'Release State', @documentid, @releaseid, @release_identifier_name, @existing_release_state_name, @new_release_state_name, @inputnotes, @app_reference) -- Create an audit log if the effective_from date has changed IF (@existing_effective_from <> @effective_from) OR (@existing_effective_from IS NULL AND @effective_from IS NOT NULL) OR (@existing_effective_from IS NOT NULL AND @effective_from IS NULL) BEGIN INSERT INTO base.file_doc_data_log (created_by_username, created_by_sid_id, change_type, change_field, doc_id, record_id, record_name, original_value, new_value, notes, app_reference) VALUES (@username, @sidid, 'Update', 'Release Effective From', @documentid, @releaseid, @release_identifier_name, CONVERT(NVARCHAR(30), @existing_effective_from, 120), CONVERT(NVARCHAR(30), @effective_from, 120), @inputnotes, @app_reference) END -- Create an audit log if the effective_until date has changed IF (@existing_effective_until <> @effective_until) OR (@existing_effective_until IS NULL AND @effective_until IS NOT NULL) OR (@existing_effective_until IS NOT NULL AND @effective_until IS NULL) BEGIN INSERT INTO base.file_doc_data_log (created_by_username, created_by_sid_id, change_type, change_field, doc_id, record_id, record_name, original_value, new_value, notes, app_reference) VALUES (@username, @sidid, 'Update', 'Release Effective Until', @documentid, @releaseid, @release_identifier_name, CONVERT(NVARCHAR(30), @existing_effective_until, 120), CONVERT(NVARCHAR(30), @effective_until, 120), @inputnotes, @app_reference) END END -- END IF file_doc_change_log is On. 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 = 'UpdateError', @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 UpdateError.'); END CATCH END END GO