USE [Elyse_DB] GO /****** Object: StoredProcedure [controlling].[usp_UPD_form_file_release_link] Script Date: Sun 22-11-2026 9:04:49 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: Silkwood Software -- Initial creation: 06-07-2026 -- Description: Updates a link between a form and a file to document release identifier field. -- Input is a form id plus other attribute fields to be updated. -- Output is message and transaction status /* COPYRIGHT NOTICE This database schema and stored procedures are protected by copyright. Copyright. Silkwood Software Pty. Ltd. 2026 */ -- ============================================= CREATE PROCEDURE [controlling].[usp_UPD_form_file_release_link] @formid bigint = NULL, @ismandatory nchar(3) = NULL, @fieldlength smallint = NULL, @formposition nvarchar(50) = NULL, @attribute1 nvarchar(50) = NULL, @attribute2 nvarchar(50) = NULL, @attribute3 nvarchar(50) = NULL, @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 @tempuserauth_status nchar(10) = '', -- Temporary value for authentication sp. @userauthentication_status nchar(10) = 'Fail', -- The outcome of the authentication check of the user -- Defaults to Fail until it is set to Pass. @tempmessage nvarchar(300) = '', @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 = 'Configurator', @user_authentication_result = @tempuserauth_status OUTPUT; IF @tempuserauth_status = 'Pass' SET @userauthentication_status = 'Pass'; EXEC [internal].[usp_AUTHENTICATE_user_role] @role_to_check = 'Controller', @user_authentication_result = @tempuserauth_status OUTPUT; IF @tempuserauth_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 -- Authenticate the user for the given form EXEC [internal].[usp_AUTHENTICATE_user_form] @form_id_to_check = @formid, @user_authentication_result = @tempuserauth_status OUTPUT; IF @tempuserauth_status = 'Fail' BEGIN -- The user does not have permission for this action SET @userauthentication_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'NoFormPermission', @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 NoFormPermission'); END IF @userauthentication_status = 'Pass' -- Don't do anything if the user is not authorised. BEGIN -- Data validation IF @formid = 0 SET @formid = NULL; -- Check form id has been supplied IF @formid IS NULL BEGIN SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'NoFormID', @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 NoFormID.'); END ELSE -- A form id has been supplied BEGIN -- Check that the form id exists IF NOT EXISTS (SELECT form_id FROM forms.form_identifier_names WHERE form_id = @formid) BEGIN -- Record id does not exist SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'FormNotExist', @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 FormNotExist.'); END END -- Validate @ismandatory IF @ismandatory = '' SET @ismandatory = NULL; IF UPPER(@ismandatory) = 'YES' SET @ismandatory = 'Yes'; IF UPPER(@ismandatory) = 'NO' SET @ismandatory = 'No'; IF NOT (@ismandatory = 'Yes' OR @ismandatory = 'No' OR @ismandatory IS NULL) BEGIN SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'FrmMandInvalid', @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 FrmMandInvalid.'); END -- Check if a record exists IF @data_validation_status = 'Pass' BEGIN IF NOT EXISTS (SELECT form_id FROM forms.form_file_release_id_links WHERE form_id = @formid) BEGIN -- Record does not exist SET @data_validation_status = 'Fail'; SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'NotExist', @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 NotExist.'); END END -- Output the failed data validation 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 IF user authentication = Pass -- Execute the insert query IF @transaction_ready = 'Ready' BEGIN BEGIN TRY UPDATE forms.form_file_release_id_links SET mandatory = @ismandatory, length = @fieldlength, position = ISNULL(@formposition, ''), attribute1 = ISNULL(@attribute1, ''), attribute2 = ISNULL(@attribute2, ''), attribute3 = ISNULL(@attribute3, '') WHERE form_id = @formid; 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