/* Update-Schritte 2026.05.0: 1. in Tabelle system.tPipelines Spalte ExcludedExecutionTimesJSON hinzufügen 2. Tabelle system.tAPI_Log in system.tAPILog umbenennen 3. Saxess Features aktivieren */ GO ALTER TABLE system.tPipelines ADD ExcludedExecutionTimesJSON NVARCHAR(MAX) NULL; GO UPDATE system.tPipelines SET ExcludedExecutionTimesJSON = '[]'; ALTER TABLE system.tPipelines ALTER COLUMN ExcludedExecutionTimesJSON NVARCHAR(MAX) NOT NULL; GO EXEC sp_rename 'system.tAPI_Log', 'tAPILog'; GO DELETE FROM system.tSettings WHERE SettingID = 'AddOnFeaturesPreferencesSaxessComponents'; INSERT INTO system.tSettings (SettingID, SettingDescription, DataTypeCODE, ValueText, ValueDateTime, ValueInt, ValueIntScale, ValueJSON, Category, ReadOnlySystemPropertyBool) VALUES ('AddOnFeaturesPreferencesSaxessComponents', '', 'BOOLEAN', '', '1900-01-01', 1, 1, '', '', 1); GO DROP FUNCTION IF EXISTS system.fUnifyLinebreaks; GO /* Function for adjusting Linebreaks of type "LF" to "CRLF" Keeps existing "CRLF" Gerd Tautenhahn for Saxess Software GmbH Last modified: 11/2022 for OCT 5.9 Testcall Function SELECT system.fUnifyLinebreaks ('Hallo',) Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fUnifyLinebreaks',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fUnifyLinebreaks','PARAMETER',NULL) */ CREATE FUNCTION system.fUnifyLinebreaks ( @String NVARCHAR(MAX) ) RETURNS NVARCHAR(MAX) AS BEGIN SET @String = REPLACE( REPLACE( REPLACE( @String,CHAR(13)+CHAR(10),CHAR(27)+CHAR(27) -- existing linebreaks are cached as ESC charactr ),CHAR(10),CHAR(13)+CHAR(10) -- effective replace ),CHAR(27)+CHAR(27),CHAR(13)+CHAR(10) -- back caching ); RETURN @String; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'fUnifyLinebreaks' -- enter function name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the function from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'DEMO' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 0; -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'FUNCTION' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag',@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET function documentation SET @value = N'Function for adjusting Linebreaks of type "LF" to "CRLF"'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@String'; SET @value = N'Any string which may contain linebreaks'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP FUNCTION IF EXISTS system.fDetermineTransactionUsername; GO /* Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Funktion returns the Name of an existing active User in OCT Usermanamgent or the string 403. Determine the name of user, for whom a transaction is done. The resulting name may be - an existing and active user of the OCT Usermanagement (including the special one with name "public") - an login who exists not in Usermanagment, but in the database as user with the role FactorySerivce or db_owner (priviledged user) - the string 'public' if no business user were found an a user public is declared - the string '403' if no authorized user was found Rules: - User with the Role db_octservice or db_owner are allowed to act in own name or in foreign name - to signalize that they act in own name, they pass in the username 'SQL' - User with other roles can act only as themself (the user which is connected) - even a user with role sysadmin can't pass in a foreign username Keep in Mind: - if a user with role db_owner is also also owner of the database he will be not appear with his login, instead with username 'system' - a user may be db_owner and has the role db_octservice - a user may be sysadmin, than he is not an db_owner - a user may be internal OCTUser and also db_owner - in OCT Usermanagement - never a user with Name '403' must be created - this user would always been blocked - a User with the Name of the db_octservice must not be created (but he may be there, due to later db role assignment) - a User with the role db_owner may exist in the OCT Usermangement TODO for later editions where XLS Client must no longer be supported -- Return User with an role to determine if db_octservice is acting for itself or not Don't use the function IS_ROLEMEMBER on SQL Server, its buggy Testcall SELECT system.fDetermineTransactionUsername ('SQL') SELECT system.fDetermineTransactionUsername ('W10\admin') Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fDetermineTransactionUsername',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fDetermineTransactionUsername','PARAMETER',NULL) */ CREATE FUNCTION system.fDetermineTransactionUsername ( @Username AS NVARCHAR(255)='' ) RETURNS NVARCHAR (255) AS BEGIN DECLARE @TransactionUsername NVARCHAR (255) = '403' ,@IsPrivilegedUser INT = 0 ,@UserBlocked INT = 0 ,@UserIsActiveOCTUser INT = 0; -- trim Username SET @Username = LTRIM(RTRIM(@Username)); -- 1. Determine if a privileged User is acting - the dbo is never member of any role, thats why its tested separat IF IS_MEMBER('db_octservice') = 1 OR IS_MEMBER('db_owner')= 1 OR USER_NAME()='system' BEGIN SET @IsPrivilegedUser = 1; END; -- 2. If privileged user is acting, every Username is accepted, she string 'SQL' signalise they are acting as themself IF @IsPrivilegedUser = 1 IF @Username = 'SQL' BEGIN SET @TransactionUsername = ORIGINAL_LOGIN() END ELSE BEGIN SET @TransactionUsername = @Username; END ELSE -- else only the Original_Login() is accepted - this happens to enable Working with the Excel Client or direct in SSMS (if user is GRANTEd to Execute SPs) BEGIN SET @TransactionUsername = ORIGINAL_LOGIN() END -- 3. Check if the user is an active OCT User IF (SELECT Count(UserKey) FROM system.trUser WHERE UserName = @TransactionUsername AND Status = 'Active') = 1 BEGIN SET @UserIsActiveOCTUser = 1; END -- privileged users are always exepted when they act for thereself (even if they exists in usermanagement as inactive or don't exists there) IF @IsPrivilegedUser = 1 AND @Username = 'SQL' BEGIN SET @TransactionUsername = @TransactionUsername -- do nothing, accept the user END -- When privileged user is acting for a User, only active users are accepted IF @IsPrivilegedUser = 1 AND @Username <> 'SQL' AND @UserIsActiveOCTUser = 0 BEGIN SET @TransactionUsername = '403' SET @UserBlocked = 1 END -- When a non privileged User is connected directly, he is blocked if he is not an active user IF @IsPrivilegedUser = 0 AND @UserIsActiveOCTUser = 0 BEGIN SET @TransactionUsername = '403' SET @UserBlocked = 1 END -- 4. If the user was blocked, check if a public user exists, which can be used for the transaction IF @UserBlocked = 1 BEGIN IF EXISTS ( SELECT UserKey FROM system.trUser WHERE UserName = 'public' AND Status = 'Active' ) SET @TransactionUsername = 'public' END RETURN @TransactionUsername END GO ALTER AUTHORIZATION ON OBJECT :: system.fDetermineTransactionUsername TO db_octservice; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'fDetermineTransactionUsername' -- enter function name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the function from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'FUNCTION' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag',@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET function documentation SET @value = N'Function and determine the business username for the transaction - only for active users and the priviledged database roles db_octservice and db_owner an username is returnd.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'The name of the user, in which name the transaction shall happen. Pass in "SQL" if the Service shall act for himself.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP FUNCTION IF EXISTS system.fPivotStringIntoTable; GO /* Function to split a String into a TableVariable The Delimiter String will be replace by a CHAR(28) and the string splitted on this Gerd Tautenhahn for Saxess Software GmbH Last modified: 09/2022 for OCT 5.8 Testcall Function SELECT * FROM system.fPivotStringIntoTable ('xxxNxxx', 'N') Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fPivotStringIntoTable',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fPivotStringIntoTable','PARAMETER',NULL) */ CREATE FUNCTION system.fPivotStringIntoTable ( @String NVARCHAR(MAX) ,@Delimiter NVARCHAR(5) ) RETURNS @tblSplitValues TABLE (txtValues NVARCHAR(MAX)) AS BEGIN DECLARE @spr NCHAR(1) = CHAR(28) DECLARE @auxString NVARCHAR (MAX) = '' SET @auxString = REPLACE(@String, @Delimiter,@spr); WITH Split(stpos,endpos) AS( SELECT 0 AS stpos, CHARINDEX(@spr,@auxString) AS endpos UNION ALL SELECT CAST(endpos AS INT)+1, CHARINDEX(@spr,@auxString,endpos+1) FROM Split WHERE endpos > 0 ) INSERT @tblSplitValues SELECT SUBSTRING(@auxString,stpos,COALESCE(NULLIF(endpos,0),LEN(@auxString)+1)-stpos) FROM Split OPTION (MAXRECURSION 32766) -- it works ONLY up to 32.766 rows RETURN END GO ALTER AUTHORIZATION ON OBJECT :: system.fPivotStringIntoTable TO db_octservice; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'fPivotStringIntoTable' -- enter function name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the function from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'FUNCTION' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag',@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET function documentation SET @value = N'Function to split a String into a TableVariable.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@String'; SET @value = N'The pivot string.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Delimiter'; SET @value = N'The delimiter to split the string.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP FUNCTION IF EXISTS system.fMaskSQL; GO /* Function to mask strings, which will be part of an SQL Statement @StringValue - it just doubles all inverted commas. returns a masked string Gerd Tautenhahn for Saxess Software GmbH Last modified: 03/2023 for OCT 5.8 Testcall Function DECLARE @String NVARCHAR(255); SET @String = N'ToDo''s'; PRINT @String; SELECT system.fMaskSQL(@String); Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fMaskSQL',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fMaskSQL','PARAMETER',NULL) */ CREATE FUNCTION system.fMaskSQL (@StringValue AS NVARCHAR(MAX)) RETURNS NVARCHAR (MAX) AS BEGIN RETURN REPLACE(@StringValue,N'''',N'''''') END GO ALTER AUTHORIZATION ON OBJECT :: system.fMaskSQL TO db_octservice; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'fMaskSQL' -- enter function name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the function from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'FUNCTION' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag',@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET function documentation SET @value = N'Function to mask strings, which will be part of an SQL Statement - it just doubles all inverted commas.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@StringValue'; SET @value = N'String, which will be returned with doubled inverted commas.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP FUNCTION IF EXISTS system.fProtectBoolean; GO /* Function returns a '0' when the passed parameter is not either '0' or '1' @BooleanValue ='<#NV>'is ignored and not changed Gerd Tautenhahn for Saxess Software GmbH Last modified: 09/2022 for OCT 5.8 Testcall Function SELECT system.fProtectBoolean (2) Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fProtectBoolean',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fProtectBoolean','PARAMETER',NULL) */ CREATE FUNCTION system.fProtectBoolean (@BooleanValue AS NVARCHAR(255)) RETURNS NVARCHAR (255) AS BEGIN IF @BooleanValue <>'<#NV>' BEGIN IF @BooleanValue <>'0' AND @BooleanValue <>'1' SET @BooleanValue= '0' END RETURN @BooleanValue END GO -- Scalar value functions require EXECUTE permissions ALTER AUTHORIZATION ON OBJECT :: system.fProtectBoolean TO db_octservice; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'fProtectBoolean' -- enter function name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the function from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'FUNCTION' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag',@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET function documentation SET @value = N'Function returns a 0 when the passed parameter is not either 0 or 1. @BooleanValue =<#NV>is ignored and not changed.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@BooleanValue'; SET @value = N'Boolean parameter.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP FUNCTION IF EXISTS system.fProtectID; GO /* Function to replace all special signs from ID An ID may contain Letters, Numbers,'_' and '-', but nothing else Function returns a clean string Gerd Tautenhahn for Saxess Software GmbH Last modified: 09/2022 for OCT 5.8 Testcall Function SELECT system.fProtectID ('T-1/3') Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fProtectID',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fProtectID','PARAMETER',NULL) */ CREATE FUNCTION system.fProtectID (@ElementID AS NVARCHAR(255)) RETURNS NVARCHAR (255) AS BEGIN SET @ElementID = RTRIM(LTRIM(@ElementID)) RETURN REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( REPLACE(@ElementID,'|',''), ';',''),'''',''),'/',''),'\',''),'"',''),',',''), '>',''),'<',''),'[',''),']',''),'(',''),')',''),'{',''),'}',''),'!',''),'$',''),'&',''),'^',''),'@',''), '%',''),':',''),'`',''),'´',''),'?',''),'§',''),'#',''),'~',''),'+',''),'=',''),'.',''),'€',''),' ','') END GO -- Scalar value functions require EXECUTE permissions ALTER AUTHORIZATION ON OBJECT :: system.fProtectID TO db_octservice; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'fProtectID' -- enter function name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the function from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'FUNCTION' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag',@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET function documentation SET @value = N'Function to replace all special signs from ID.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@ElementID'; SET @value = N'Element ID Parameter.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP FUNCTION IF EXISTS system.fProtectInt; GO /* Function cast all expression to int or return 0 for non-int values, except the <#NV> String this is passed anytime Gerd Tautenhahn for Saxess Software GmbH Last modified: 09/2022 for OCT 5.8 Testcalls Function SELECT system.fProtectInt('-1') SELECT system.fProtectInt('1') SELECT system.fProtectInt('Hase') SELECT system.fProtectInt('<#NV>') SELECT system.fProtectInt('') SELECT system.fProtectInt('0') SELECT system.fProtectInt('9999999999999999999999999999999999999999999999999999999999') Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fProtectInt',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fProtectInt','PARAMETER',NULL) */ CREATE FUNCTION system.fProtectInt (@IntValue AS NVARCHAR(255)) RETURNS NVARCHAR (255) AS BEGIN IF @IntValue <>'<#NV>' BEGIN SET @IntValue = COALESCE(CAST(TRY_CAST(@IntValue AS INT) AS NVARCHAR(255)),0) END RETURN @IntValue END GO ALTER AUTHORIZATION ON OBJECT :: system.fProtectInt TO db_octservice; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'fProtectInt' -- enter function name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the function from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'FUNCTION' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag',@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET function documentation SET @value = N'Function cast all expression to int or return 0 for non-int values, except the <#NV> String this is passed anytime.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@IntValue'; SET @value = N'Parameter int value.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP FUNCTION IF EXISTS system.fProtectString; GO /* Function to eliminate all not permitted special characters (Hochkommata, '|','),(', ';' ) and line breaks at the end from @StringValue Function returns a string without the not permitted special characters Gerd Tautenhahn for Saxess Software GmbH Last modified: 06/2023 for OCT 5.9 Testcall Function SELECT system.fProtectString ('X),(Y;Z') Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fProtectString',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fProtectString','PARAMETER',NULL) */ CREATE FUNCTION system.fProtectString (@StringValue AS NVARCHAR(MAX)) RETURNS NVARCHAR (MAX) AS BEGIN -- remove line break only at the end of the string IF LEN(@StringValue) > 1 BEGIN SET @StringValue = SUBSTRING(@StringValue, 0, len(@StringValue) - 2) + REPLACE(REPLACE(SUBSTRING(@StringValue, LEN(@StringValue) - 2, 3), CHAR(10), ''), CHAR(13), '') END -- remove special characters RETURN REPLACE(REPLACE(REPLACE(@StringValue,'|',''), '),(',''), '--','') END GO ALTER AUTHORIZATION ON OBJECT :: system.fProtectString TO db_octservice; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'fProtectString' -- enter function name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the function from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'FUNCTION' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag',@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET function documentation SET @value = N'Function to eliminate all not permitted special characters from @StringValue.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@StringValue'; SET @value = N'Parameter string value.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP FUNCTION IF EXISTS system.fGET_Setting; GO /* Table value function to GET values of a setting Saxess Software GmbH Last modified: 10/2023 for OCT 5.10 Testcall Function SELECT * FROM system.fGET_Setting('DBVersion') Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fGET_Setting',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'FUNCTION', 'fGET_Setting','PARAMETER',NULL) */ CREATE FUNCTION system.fGET_Setting ( @SettingID NVARCHAR(50) ) RETURNS TABLE AS RETURN ( SELECT SettingDescription , DataTypeCODE , ValueText , ValueDateTime , ValueInt , ValueIntScale , ValueJSON , Category , ReadOnlySystemPropertyBool FROM system.tSettings WHERE SettingID = @SettingID UNION SELECT NULL AS SettingDescription , NULL AS DataTypeCODE , NULL AS ValueText , NULL AS ValueDateTime , NULL AS ValueInt , NULL AS ValueIntScale , NULL AS ValueJSON , NULL AS Category , NULL AS ReadOnlySystemPropertyBool WHERE CASE WHEN EXISTS( SELECT 1 FROM system.tSettings WHERE SettingID = @SettingID ) THEN 1 ELSE 0 END = 0 ); GO GRANT SELECT ON system.fGET_Setting TO db_octservice; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'fGET_Setting' -- enter function name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the function from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'FUNCTION' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag',@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET function documentation SET @value = N'Table valued functino to get values of a setting.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@SettingID'; SET @value = N'SettingID information that should be returned.'; GO DROP TRIGGER IF EXISTS system.mqt_trUserRights_from_trRights; GO /* Trigger to start materialize effectiv Userrights on every INSERT, UPDATE, DELETE on system.trRights Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall None - is triggerd Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'TABLE', 'trRights','TRIGGER','mqt_trUserRights_from_trRights') */ CREATE TRIGGER system.mqt_trUserRights_from_trRights ON system.trRights AFTER INSERT, UPDATE, DELETE AS BEGIN EXEC system.spMATERIALIZE_trUserRights; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' ,@level1name NVARCHAR(255) = N'trRights' ,@level2name NVARCHAR(255) = N'mqt_trUserRights_from_trRights' ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the trigger from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'TABLE' ,@level2type NVARCHAR(255) = N'TRIGGER' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty 'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty 'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty 'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; -- SET documententation ************************************************************************* -- SET Trigger documentation SET @value = N'Trigger to start materialize effectiv Userrights on every INSERT, UPDATE, DELETE on system.trRights.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP TRIGGER IF EXISTS system.mqt_trUserRights_from_trUser; GO /* Trigger to start materialize effectiv Userrights on every INSERT, UPDATE, DELETE on system.trUser Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall None - is triggerd Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'TABLE', 'trUser','TRIGGER','mqt_trUserRights_from_trUser') */ CREATE TRIGGER system.mqt_trUserRights_from_trUser ON system.trUser AFTER INSERT, UPDATE, DELETE AS BEGIN EXEC system.spMATERIALIZE_trUserRights; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' ,@level1name NVARCHAR(255) = N'trUser' ,@level2name NVARCHAR(255) = N'mqt_trUserRights_from_trUser' ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the trigger from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'TABLE' ,@level2type NVARCHAR(255) = N'TRIGGER' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty 'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty 'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty 'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; -- SET documententation ************************************************************************* -- SET Trigger documentation SET @value = N'Trigger to start materialize effectiv Userrights on every INSERT, UPDATE, DELETE on system.trUser.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO GO DROP PROCEDURE IF EXISTS system.spPOST_APILogEntry; GO /* Internal POST Operation for one API LogEntry, always used, when a public procedure is called Writes an LogEntry, as long as Gerd Tautenhahn for Saxess Software GmbH Last modified: 11/2022 for OCT 5.9 Testcall DECLARE @RESULT AS NVARCHAR(255) EXEC @RESULT = system.spPOST_APILogEntry 'SQL' ,'gerd.tautenhahn@saxess-software.com' ,'calc.spTest' ,'ThisAreTheParameter' ,1 ,403 ,'20150731' ,'Comment'; PRINT @RESULT; Testcall table SELECT TOP 5 * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_APILogEntry',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_APILogEntry','PARAMETER',NULL) */ CREATE PROCEDURE system.spPOST_APILogEntry @Username AS NVARCHAR(255) ,@TransactUsername AS NVARCHAR(255) ,@ProcedureName AS NVARCHAR(255) ,@ParameterString AS NVARCHAR (MAX) ,@EffectedRows AS INT ,@ReturnCode AS INT ,@TimestampCall AS Datetime ,@Comment AS NVARCHAR (2000) AS DECLARE @TimestampReturn AS Datetime = GETUTCDATE() ,@ProcessCode AS NVARCHAR(255) = N'' ,@DatabaseIsReadOnly AS INTEGER = 0; SELECT @DatabaseIsReadOnly = is_read_only FROM sys.databases WHERE Name = DB_NAME(); IF NOT @DatabaseIsReadOnly = 1 BEGIN -- the functions CURRENT_User may not be avaiable for Azure in impersonation context (WITH EXECUTE AS OWNER...), depening on the object owner. BEGIN TRY INSERT INTO system.tAPILog ( SQLSystemUser ,UserName ,TransactUsername ,ProcedureName ,ParameterString ,EffectedRows ,ReturnCode ,TimestampCall ,TimestampReturn ,Comment ,ProcessCode ) VALUES (CURRENT_USER,@Username,@TransactUsername,@ProcedureName,@ParameterString,@EffectedRows,@ReturnCode,@TimestampCall,@TimestampReturn,@Comment,@ProcessCode) END TRY BEGIN CATCH INSERT INTO system.tAPILog ( SQLSystemUser ,UserName ,TransactUsername ,ProcedureName ,ParameterString ,EffectedRows ,ReturnCode ,TimestampCall ,TimestampReturn ,Comment ,ProcessCode ) VALUES ('Impersonated User as dbo',@Username,@TransactUsername,@ProcedureName,@ParameterString,@EffectedRows,@ReturnCode,@TimestampCall,@TimestampReturn,@Comment,@ProcessCode) END CATCH END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_APILogEntry' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N' Internal POST Operation for one API LogEntry, always caused, when a public procedure is called'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TransactUsername'; SET @value = N'The name of the user for whom this procedure was executed.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProcedureName'; SET @value = N'Name of the stored procedure which was executed'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ParameterString'; SET @value = N'List of parameters, which were given to the called procedure.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@EffectedRows'; SET @value = N'Count of effected rows which (returnd from the called procedure). This number my include counts from temporary tables.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ReturnCode'; SET @value = N'signals the result of the called procedure (sucessful, not found objects...)'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TimestampCall'; SET @value = N'Timestamp the procedure was called'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Comment'; SET @value = N'Return messages from the SQL Server or from the procedure for logging.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_ClusterWriteRightTransactionUser; GO /* GET Operation for Write Rights on Cluster Needs already a validated Transactionsuser ! Returns for OCT Clusteradmins and privileged roles 200 Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall DECLARE @Returncode INT EXEC @Returncode = system.spGET_ClusterWriteRightTransactionUser 'W10\admin' PRINT @Returncode Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_ClusterWriteRightTransactionUser',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_ClusterWriteRightTransactionUser','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_ClusterWriteRightTransactionUser @TransactionUsername AS NVARCHAR(255) AS BEGIN DECLARE @Right NVARCHAR (255) ='none' DECLARE @IsPrivilegedUser INT = 0 -- 1. Determine if a privileged User is acting (doublehoppel ? only priviledged user are allowed to execute the transaction (and sysadmin) IF IS_MEMBER('db_octservice') = 1 OR IS_MEMBER('db_owner')= 1 OR USER_NAME()='system' BEGIN SET @IsPrivilegedUser = 1; END; -- always return 200 for privileged user which is acting for himself (dbo does not matter here, as for dbo the Transactusername is the the Original_Login()) IF @IsPrivilegedUser = 1 AND @TransactionUsername = ORIGINAL_LOGIN() BEGIN RETURN 200 END -- Check for Write Line on cluster SELECT @Right=COALESCE([Right],'no') FROM system.trUserRights WHERE FactoryID = '' AND UserName = @TransactionUsername; IF @Right = 'Write' BEGIN RETURN 200 END ELSE BEGIN RETURN 401 END END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ClusterWriteRightTransactionUser' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for Write Rights on Cluster Needs already a validated Transactionsuser ! Returns for OCT Clusteradmins and privileged role member 200'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@TransactionUsername'; SET @value = N'Name of validated Transactionuser'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_ClusterWriteRight; GO /* GET Operation for Write Rights on a Cluster Returns 200 if User has ClusterWriteRights Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = system.spGET_ClusterWriteRight 'SQL' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_ClusterWriteRight',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_ClusterWriteRight','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_ClusterWriteRight @Username NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_ClusterWriteRight; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 3 - Test that user has write access to the cluster SELECT @ResultCode = IIF([Right] = N'Write', 200, 401) FROM system.trUserRights WHERE FactoryID = N'' AND UserName = @TransactUsername; IF @ResultCode <> 200 RAISERROR('User have no write access to the cluster', 16, 10); SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_ClusterWriteRight; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_ClusterWriteRight; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ClusterWriteRight'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for Write Rights on a Cluster Returns 200 if User has ClusterWriteRights'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_TransactUsername; GO /* Function returns the name of an existing active OCT user or raises an error. Determine the name of user, for whom a transaction is done. The resulting name may be - an existing and active user of the OCT Usermanagement (including the special one with name "public") - a login which doesn't exist in OCT, but in the database as user with the role FactorySerivce or db_owner (priviledged user) - the string 'public' if no business user was found and a user public is declared Rules: - user with the Role db_octservice or db_owner are allowed to act in own name or in foreign name - to signalize that they act in own name, they pass in the username 'SQL' - user with other roles can only act as themself (the user which is connected) - even a user with role sysadmin can't pass in a foreign username Keep in Mind: - if a user with role db_owner is also owner of the database he will be not appear with his login, instead with username 'dbo' - a user may be db_owner and has the role db_octservice - a user may be sysadmin, then he is not db_owner - a user may be an internal OCT user and also db_owner - in OCT user management - a user with name '403' is not allowed to exist - this user would always be blocked - a user with the name of the db_octservice can not the created (but he may be there, due to later db role assignment) - a user with the role db_owner may exist in the OCT user mangement TODO for later editions where XLS Client is no longer supported: - return user with a role to determine if db_octservice is acting for himself or not Don't use the function IS_ROLEMEMBER on SQL Server, its buggy Saxess Software GmbH Last modified: 09/2023 for OCT 5.10 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = system.spGET_TransactUsername 'sxs' PRINT @RC SELECT TOP 3 * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_TransactUsername',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_TransactUsername','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_TransactUsername ( @TransactUsername NVARCHAR(255) OUTPUT , @Username NVARCHAR(255) = N'' ) AS BEGIN DECLARE @IsPrivilegedUser INT = 0; DECLARE @UserBlocked INT = 0; DECLARE @UserIsActiveOCTUser INT = 0; -- Trim username SET @Username = LTRIM(RTRIM(@Username)); -- 1. Determine if a privileged User is acting - the dbo is never member of any role, thats why its tested separat IF IS_MEMBER('db_octservice') = 1 OR IS_MEMBER('db_owner') = 1 OR USER_NAME() = 'dbo' BEGIN SET @IsPrivilegedUser = 1; END; -- 2. If privileged user is acting, every Username is accepted, she string 'SQL' signalise they are acting as themself IF @IsPrivilegedUser = 1 IF @Username = 'SQL' BEGIN SET @TransactUsername = ORIGINAL_LOGIN(); END; ELSE BEGIN SET @TransactUsername = @Username; END; ELSE -- Else only the Original_Login() is accepted - this happens to enable working with the Excel Client or direct in SSMS (if user is GRANTED to execute SPs) BEGIN SET @TransactUsername = ORIGINAL_LOGIN(); END; -- 3. Check if the user is an active OCT User IF ( SELECT COUNT(UserKey)FROM system.trUser WHERE UserName = @TransactUsername AND Status = 'Active' ) = 1 BEGIN SET @UserIsActiveOCTUser = 1; END; -- Privileged users are always excepted when they act for themself (even if they exist in OCT user management as inactive or don't exist there) IF @IsPrivilegedUser = 1 AND @Username = 'SQL' BEGIN SET @TransactUsername = @TransactUsername; -- do nothing, accept the user END; -- When privileged user is acting for a User, only active users are accepted IF @IsPrivilegedUser = 1 AND @Username <> 'SQL' AND @UserIsActiveOCTUser = 0 BEGIN SET @TransactUsername = N'403'; SET @UserBlocked = 1; END; -- When a non privileged User is connected directly, he is blocked if he is not an active user IF @IsPrivilegedUser = 0 AND @UserIsActiveOCTUser = 0 BEGIN SET @TransactUsername = N'403'; SET @UserBlocked = 1; END; -- 4. If the user was blocked, check if a public user exists, which can be used for the transaction IF @UserBlocked = 1 BEGIN IF EXISTS (SELECT UserKey FROM system.trUser WHERE UserName = 'public' AND Status = 'Active') SET @TransactUsername = N'public'; END; IF @TransactUsername = N'403' EXEC system.spSEND_Message 'ERROR', 'Transaction user doesn''t exist' END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_TransactUsername' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N''; -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Internal stored procedure to return the current transaction user.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; GO DROP PROCEDURE IF EXISTS planning.spGET_FactoryWriteRight; GO /* GET Operation for Write Rights on a Factory Needs already resolved Transactuser ! Returns on success 200 Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall Procedure DECLARE @RESULT AS NVARCHAR(255) EXEC @RESULT = planning.spGET_FactoryWriteRight 'W10\admin','ZT' PRINT @RESULT Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_FactoryWriteRight',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_FactoryWriteRight','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_FactoryWriteRight @TransactionUsername NVARCHAR(255) ,@FactoryID NVARCHAR(255) AS BEGIN DECLARE @Right NVARCHAR(255) = '' ,@IsPrivilegedUser INT = 0; -- 1. Determine if a privileged User is acting (doublehoppel ? only priviledged user are allowed to execute the transaction (and sysadmin) IF IS_MEMBER('db_octservice') = 1 OR IS_MEMBER('db_owner')= 1 OR USER_NAME()='planning' BEGIN SET @IsPrivilegedUser = 1; END; --check for existance of Factory IF NOT EXISTS ( SELECT FactoryID FROM planning.tdFactories WHERE FactoryID = @FactoryID ) BEGIN RETURN 404; END -- always return 200 for privileged user which is acting for himself (dbo does not matter here, as for dbo the Transactusername is the the Original_Login()) IF @IsPrivilegedUser = 1 AND @TransactionUsername = ORIGINAL_LOGIN() BEGIN RETURN 200; END -- check for write or read Rights for Factory SELECT @Right=IIF ([Right] = 'Write','Write', '401') FROM system.trUserRights WHERE FactoryID = @FactoryID AND ProductLineID = '' AND UserName = @TransactionUsername; IF @Right = 'Write' BEGIN RETURN 200; END ELSE BEGIN RETURN 401; END END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_FactoryWriteRight' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for Write Rights on a Factory Needs already resolved Transactuser ! Returns on success 200'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@TransactionUsername'; SET @value = N'Username which is already resolved to Transactusername.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'ID of the requested Factory.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_ProductLineWriteRight ; GO /* GET Operation for Write Rights on ProductLine Needs already a Transactionsuser ! Returns for User with Write Rights 200 Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall Procedure DECLARE @RC INT; EXEC @RC = planning.spGET_ProductLineWriteRight @TransactionUsername = 'W10\admin' ,@FactoryID = 'ZT' ,@ProductLineID = 'U' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ProductLineWriteRight',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ProductLineWriteRight','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_ProductLineWriteRight @TransactionUsername NVARCHAR(255) ,@FactoryID NVARCHAR(255) ,@ProductLineID NVARCHAR(255) AS DECLARE @Right NVARCHAR(255) = '' ,@IsPrivilegedUser INT = 0; -- 1. Determine if a privileged User is acting (doublehoppel ? only priviledged user are allowed to execute the transaction (and sysadmin) IF IS_MEMBER('db_octservice') = 1 OR IS_MEMBER('db_owner')= 1 OR USER_NAME()='planning' BEGIN SET @IsPrivilegedUser = 1; END; --check for existance of ProductLine IF NOT EXISTS ( SELECT FactoryID, ProductLineID FROM planning.tdProductLines WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID ) BEGIN RETURN 404; END -- always return 200 for privileged user which is acting for himself (dbo does not matter here, as for dbo the Transactusername is the the Original_Login()) IF @IsPrivilegedUser = 1 AND @TransactionUsername = ORIGINAL_LOGIN() BEGIN RETURN 200; END -- check for write or read Rights for ProductLine SELECT @Right=IIF ([Right] = 'Write','Write', '401') FROM system.trUserRights WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND UserName = @TransactionUsername; IF @Right = 'Write' BEGIN RETURN 200; END ELSE BEGIN RETURN 401; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ProductLineWriteRight' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for Write Rights on ProductLine Needs already a Transactionsuser ! Returns for User with Write Rights 200'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@TransactionUsername'; SET @value = N'Username resolved to TransactionUsername'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'ID of the requested Factory.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ID of the requested ProductLine.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_ClusterPropertiesReadRight; GO /* GET Operation for read right on Clusterlevel Needs already resolved Transaktionsuser, therefore PRIVATE - call only in case of passing in a validated Transactusername ! Returns in case of read rights for this user 200, else 401 Read rights have all user which have got read rights in OCT Usermanagement or have the privileged OCT database roles (db_octservice, db_owner) This procedure is always called after determination of the @Transactusername over function dbo.sx_pf_Determine_Transactusername, therefore it is already ensured that - the delivered username is an active user from OCT Usermangement or a user with privileged role The Procedure therefore always return 200, except the @Transactusername is '403', which is the return code in case of no access, which was not handle als exception in the calling procedure Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall DECLARE @RESULT AS NVARCHAR(255) EXEC @RESULT = planning.spGET_ClusterPropertiesReadRight 'W10\admin' PRINT @RESULT Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ClusterPropertiesReadRight',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ClusterPropertiesReadRight','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_ClusterPropertiesReadRight @TransactionUsername AS NVARCHAR(255) AS BEGIN IF @TransactionUsername = '403' BEGIN RETURN 401 END ELSE BEGIN RETURN 200 END END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ClusterPropertiesReadRight' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N' GET Operation for read right on Clusterlevel Needs already resolved Transaktionsuser, therefore PRIVATE ! Returns in case of read rights for this user 200, else 401 Read rights have all user which have got read rights in OCT Usermanagement or have the privileged OCT database roles (db_octservice, db_owner) This procedure is always called after determination of the @Transactusername over function dbo.sx_pf_Determine_Transactusername, therefore it is already ensured that - the delivered username is an active user from OCT Usermangement or a user with privileged role The Procedure therefore always return 200, except the @Transactusername is ''403'', which is the return code in case of no access, which was not handle als exception in the calling procedure'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@TransactionUsername'; SET @value = N'TransactionUsername - the already als valid and granted name of an existing OCT User or a member of an priviledged role.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_FactoryReadRight; GO /* GET Operation for read right on a Factory Needs an alreaday resolved Transactuser ! Returns 200 in case of Read Rights. User with write rights are also returned with 200 Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall Procedure DECLARE @RC INT; EXEC @RC = planning.spGET_FactoryReadRight @TransactionUsername = 'W10\admin' ,@FactoryID = 'ZT' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_FactoryReadRight',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_FactoryReadRight','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_FactoryReadRight @TransactionUsername NVARCHAR(255) ,@FactoryID NVARCHAR(255) AS BEGIN DECLARE @Right NVARCHAR(255) = '' ,@IsPrivilegedUser INT = 0; -- 1. Determine if a privileged User is acting (doublehoppel ? only priviledged user are allowed to execute the transaction (and sysadmin) IF IS_MEMBER('db_octservice') = 1 OR IS_MEMBER('db_owner')= 1 OR USER_NAME()='planning' BEGIN SET @IsPrivilegedUser = 1; END; --check for existance of Factory IF NOT EXISTS ( SELECT FactoryID FROM planning.tdFactories WHERE FactoryID = @FactoryID ) BEGIN RETURN 404; END -- always return 200 for privileged user which is acting for himself (dbo does not matter here, as for dbo the Transactusername is the the Original_Login()) IF @IsPrivilegedUser = 1 AND @TransactionUsername = ORIGINAL_LOGIN() BEGIN RETURN 200; END -- check for write or read Rights for Factory SELECT @Right=IIF ([Right] = 'Write' OR [Right] = 'Read','Read', '401') FROM system.trUserRights WHERE FactoryID = @FactoryID AND ProductLineID = '' AND UserName = @TransactionUsername; IF @Right = 'Read' BEGIN RETURN 200; END ELSE BEGIN RETURN 401; END END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_FactoryReadRight' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for read right on a Factory Needs an alreaday resolved Transactuser ! Returns 200 in case of Read Rights. User with write rights are also returned with 200'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@TransactionUsername'; SET @value = N'Name of the alread resolved TransactionUser.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID for which read rights are requested.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_ProductLineReadRight ; GO /* GET Operation for Read Rights on a ProductLine Needs already resolved Transactionuser ! Returns on success 200 for Users with Read or Write Rights Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall Procedure DECLARE @RC INT; EXEC @RC = planning.spGET_ProductLineReadRight @TransactionUsername = 'W10\admin' ,@FactoryID = 'ZT' ,@ProductLineID = 'U' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ProductLineReadRight',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ProductLineReadRight','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_ProductLineReadRight @TransactionUsername NVARCHAR(255) ,@FactoryID NVARCHAR(255) ,@ProductLineID NVARCHAR(255) AS BEGIN DECLARE @Right NVARCHAR(255) = '' ,@IsPrivilegedUser INT = 0; -- 1. Determine if a privileged User is acting (doublehoppel ? only priviledged user are allowed to execute the transaction (and sysadmin) IF IS_MEMBER('db_octservice') = 1 OR IS_MEMBER('db_owner')= 1 OR USER_NAME()='planning' BEGIN SET @IsPrivilegedUser = 1; END; --check for existance of ProductLine IF NOT EXISTS ( SELECT FactoryID, ProductLineID FROM planning.tdProductLines WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID ) BEGIN RETURN 404; END -- always return 200 for privileged user which is acting for himself (dbo does not matter here, as for dbo the Transactusername is the the Original_Login()) IF @IsPrivilegedUser = 1 AND @TransactionUsername = ORIGINAL_LOGIN() BEGIN RETURN 200; END -- check for write or read Rights for ProductLine SELECT @Right=IIF ([Right] = 'Write' OR [Right] = 'Read','Read', '401') FROM system.trUserRights WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND UserName = @TransactionUsername; IF @Right = 'Read' BEGIN RETURN 200; END ELSE BEGIN RETURN 401; END END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ProductLineReadRight' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for Read Rights on a ProductLine Needs already resolved Transactionuser ! Returns on success 200 for Users with Read or Write Rights'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@TransactionUsername'; SET @value = N'Username resolved to TransactionUsername.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'ID of the Factory where the ProductLine is located.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ID of the ProductLine where read rights are requested.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spMATERIALIZE_trUserRights ; GO /* Procedure for materialization of UserRights Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 CUSTOM: Performanceverbesserung durch temporäre Tabelle in Schritt 2a Testcall EXEC system.spMATERIALIZE_trUserRights SELECT * FROM system.trUserRights order by Username, FactoryID, ProductLineID Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spMATERIALIZE_trUserRights',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spMATERIALIZE_trUserRights','PARAMETER',NULL) */ CREATE PROCEDURE system.spMATERIALIZE_trUserRights AS BEGIN BEGIN TRY -- Table to store the calculated Rights IF OBJECT_ID('tempdb..#RightsTemp') IS NOT NULL DROP TABLE #RightsTemp; CREATE TABLE #RightsTemp ( Source NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,UserKey BIGINT NOT NULL ,Username NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,FactoryKey BIGINT NOT NULL ,ProductLineKey BIGINT NOT NULL ,FactoryID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,[RightExplicit] NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,[RightImplicit] NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ReadCommentMandatory INT NOT NULL ,WriteCommentMandatory INT NOT NULL ); -- Concept for saving only positive Rights, no Denys -- ClusterLevel ******************************************************************************************************************** -- this are Rights for FactoryID = '' AND ProductLineID = '' -- Clusteradmins ***************************** -- Has Write Rights on Clusterlevel, no limitation possible -- Insert Write Rights on ClusterLevel -- Insert Write Rights on FactoryLevel -- Insert Write Rights on ProductLine Level -- ClusterReader ****************************** -- Has Read Rights on Clusterlevel, may have additional Write Rights on Factory / ProductLine Level -- Insert Read Rights on ClusterLevel -- Insert Read Rights on FactoryLevel -- Insert Read Rights on ProductLine Level -- FactoryLevel ******************************************************************************************************************** -- Rights defined on FactoryLevel with FactoryID <> 0 AND ProductLineID = '' -- Write Rights on Factories -- Has Write Rights on a Factory - no limitation under this Factory possible -- Read Rights on Factories -- Has Read Rights on Factory Level, may have additional Write Rights on ProductLineLevel -- ProductLineLevel ************************************************************************* -- Rights defined on ProductLineLevel with FactoryID <> 0 AND ProductLineID <> 0 -- Write Rights on ProductLines -- Read Rights on ProductLines -- Preparation ************************************************************************** -- Create Table with all rows, this are: -- for User with Rights on ClusterLevel Linecount = 1 + FactoryCount + PLCount -- for User with Rights on FactoryLevel Linecount = 1 + FactoryWithRightCount + PLCount of FactoriesWith Rights -- for User with Rights on ProductLineLevel Linecount = 1 + FactoryCountPLWithRights + PLCount with Rights -- Helper Table with Stucture ******************************************************************* IF OBJECT_ID('tempdb..#FullStructure') IS NOT NULL DROP TABLE #FullStructure; CREATE TABLE #FullStructure ( RowKey BIGINT IDENTITY (1,1) NOT NULL ,[Level] NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,FactoryKey BIGINT NOT NULL ,ProductLineKey BIGINT NOT NULL ,FactoryID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ); -- C Level INSERT INTO #FullStructure SELECT 'Cluster' AS Level ,0 AS FactoryKey ,0 AS ProductLineKey ,'' AS FactoryID ,'' ASProductLineID; -- F Level INSERT INTO #FullStructure SELECT 'Factory' AS Level ,dF.FactoryKey ,0 AS ProductLineKey ,dF.FactoryID ,'' AS ProductLineID FROM planning.tdFactories dF; -- PL Level INSERT INTO #FullStructure SELECT 'ProductLine' AS Level ,dF.FactoryKey ,dPL.ProductLineKey ,dF.FactoryID ,dPL.ProductLineID FROM planning.tdProductLines dPL LEFT JOIN planning.tdFactories dF ON dPL.FactoryKey = dF.FactoryKey; -- SELECT * FROM #FullStructure -- Fill UserRight Table with fitting RowCount and set all to read ************************************************************************ -- 1a. The Full Structure Rows for User with Clusterrights -> no additional Rows for Rights on Factory / ProductLine Level need INSERT INTO #RightsTemp SELECT 'Row-ClusterRight' AS Source ,rU.UserKey ,rU.UserName ,fs.FactoryKey ,fs.ProductLineKey ,fs.FactoryID ,fs.ProductLineID ,'-' AS [RightExplicit] ,'Read' AS [RightImplizit] ,0 AS ReadCommentMandatory ,0 AS WriteCommentMandatory FROM #FullStructure fs LEFT JOIN system.trUser rU ON 1=1 WHERE rU.Status = 'Active' -- Clusterrights AND rU.UserKey IN (SELECT UserKey FROM system.trRights WHERE FactoryID = '' AND ProductLineID = ''); -- 1b. Cluster Read right Row for Transfer for alle user with any right except the already done users with clusterrights INSERT INTO #RightsTemp SELECT 'Row-TransferCluser' AS Source ,rU.UserKey ,rU.UserName ,fs.FactoryKey ,fs.ProductLineKey ,fs.FactoryID ,fs.ProductLineID ,'-' AS [RightExplicit] ,'Read' AS [RightImplizit] ,0 AS ReadCommentMandatory ,0 AS WriteCommentMandatory FROM #FullStructure fs LEFT JOIN system.trUser rU ON 1=1 WHERE rU.Status = 'Active' AND fs.Level = 'Cluster' -- anybody who has any set right AND rU.UserKey IN (SELECT DISTINCT UserKey FROM system.trRights) -- and is not already ClusterRight Owner AND rU.UserKey NOT IN (SELECT DISTINCT UserKey FROM system.trRights WHERE FactoryID = ''); -- 1c. Rows for User with direct Factory Rights INSERT INTO #RightsTemp SELECT 'Row-FactoryRights' AS Source ,rU.UserKey ,rU.UserName ,fs.FactoryKey ,fs.ProductLineKey ,fs.FactoryID ,fs.ProductLineID ,'-' AS [RightExplicit] ,'Read' AS [RightImplizit] ,0 AS ReadCommentMandatory ,0 AS WriteCommentMandatory FROM #FullStructure fs INNER JOIN ( SELECT UserKey,FactoryID FROM system.trRights WHERE FactoryID <> '' AND ProductLineID = '' ) rFactoryDirect ON fs.FactoryID = rFactoryDirect.FactoryID COLLATE DATABASE_DEFAULT INNER JOIN system.trUser rU ON rFactoryDirect.UserKey = rU.UserKey WHERE rU.Status = 'Active' AND rFactoryDirect.FactoryID IS NOT NULL -- and is not already ClusterRight Owner AND rU.UserKey NOT IN (SELECT DISTINCT UserKey FROM system.trRights WHERE FactoryID = ''); --1d. Rows for Factory Transfer Rights for User with direct ProduclineRights Rights (but not already direct Factory / Clusterrights) INSERT INTO #RightsTemp SELECT 'Row-Transfer Factory' AS [Source] ,rU.UserKey ,rU.UserName ,fs.FactoryKey ,fs.ProductLineKey ,fs.FactoryID ,fs.ProductLineID ,'-' AS [RightExplicit] ,'Read' AS [RightImplizit] ,0 AS ReadCommentMandatory ,0 AS WriteCommentMandatory FROM #FullStructure fs INNER JOIN ( SELECT UserKey,FactoryID,ProductLineID FROM system.trRights WHERE FactoryID <> '' AND ProductLineID <> '' ) rPLDirect ON fs.FactoryID = rPLDirect.FactoryID COLLATE DATABASE_DEFAULT-- Join only over Factory INNER JOIN system.trUser rU ON rPLDirect.UserKey = rU.UserKey WHERE rU.Status = 'Active' AND fs.Level = 'Factory' -- exclude direct Factory Rights AND CAST(rU.UserKey AS NVARCHAR(255)) +'_'+fs.FactoryID COLLATE DATABASE_DEFAULT NOT IN ( SELECT CAST(UserKey AS NVARCHAR(255)) +'_'+FactoryID COLLATE DATABASE_DEFAULT FROM system.trRights WHERE FactoryID <> '' AND ProductLineID = '' ) -- exclude ClusterRight owner AND rU.UserKey NOT IN (SELECT UserKey FROM system.trRights WHERE FactoryID = '' ) GROUP BY rU.UserKey ,rU.UserName ,fs.FactoryKey ,fs.ProductLineKey ,fs.FactoryID ,fs.ProductLineID; --1e. Rows Rights for User with direct ProduclineRights Rights (which not already have this due to FactoryRights) INSERT INTO #RightsTemp SELECT 'Row-ProductLineRights' AS Source ,rU.UserKey ,rU.UserName ,fs.FactoryKey ,fs.ProductLineKey ,fs.FactoryID ,fs.ProductLineID ,'-' AS [RightExplicit] ,'Read' AS [RightImplizit] ,0 AS ReadCommentMandatory ,0 AS WriteCommentMandatory FROM #FullStructure fs INNER JOIN ( SELECT UserKey,FactoryID,ProductLineID FROM system.trRights WHERE FactoryID <> '' AND ProductLineID <> '' ) rPLDirect ON fs.FactoryID = rPLDirect.FactoryID COLLATE DATABASE_DEFAULT AND fs.ProductLineID = rPLDirect.ProductLineID COLLATE DATABASE_DEFAULT INNER JOIN system.trUser rU ON rPLDirect.UserKey = rU.UserKey WHERE rU.Status = 'Active' -- exclude direct Factory Rights AND CAST(rU.UserKey AS NVARCHAR(255)) +'_'+fs.FactoryID COLLATE DATABASE_DEFAULT NOT IN ( SELECT CAST(UserKey AS NVARCHAR(255)) +'_'+FactoryID COLLATE DATABASE_DEFAULT FROM system.trRights WHERE FactoryID <> '' AND ProductLineID = '' ) -- exclude ClusterRight owner AND rU.UserKey NOT IN (SELECT UserKey FROM system.trRights WHERE FactoryID = '' ); --SELECT * FROM #RightsTemp rt ORDER BY Username,FactoryID,rt.ProductLineID; --2. Evaluate Rights to give write Rights and set Commentproperties######################################################################### -- 2a. SET Clusterrights DROP TABLE IF EXISTS #Rights; CREATE TABLE #Rights ( RightKey INT NOT NULL ,UserKey INT NOT NULL ,UserName NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,FactoryID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,[Right] NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ReadCommentMandatory INT NOT NULL ,WriteCommentMandatory INT NOT NULL ); INSERT INTO #Rights SELECT * FROM system.trRights UPDATE #RightsTemp SET [Source] = 'ClusterRight' ,RightExplicit = rR.[Right] ,ReadCommentMandatory = rR.ReadCommentMandatory ,WriteCommentMandatory = rR.WriteCommentMandatory FROM #RightsTemp rt INNER JOIN #Rights rR ON rt.UserKey = rR.UserKey WHERE rR.FactoryID = '' AND rR.ProductLineID = ''; -- 2a. Set Factory Rights with downwards inheritance UPDATE #RightsTemp SET [Source] = 'FactoryRight' ,RightExplicit = rR.[Right] ,ReadCommentMandatory = rR.ReadCommentMandatory ,WriteCommentMandatory = rR.WriteCommentMandatory FROM #RightsTemp rt INNER JOIN #Rights rR ON rt.UserKey = rR.UserKey AND rt.FactoryID = rR.FactoryID COLLATE DATABASE_DEFAULT WHERE rR.FactoryID <> '' AND rR.ProductLineID = ''; -- 2a. Set ProductLine Rights UPDATE #RightsTemp SET [Source] = 'ProductLineRight' ,RightExplicit = rR.[Right] ,ReadCommentMandatory = rR.ReadCommentMandatory ,WriteCommentMandatory = rR.WriteCommentMandatory FROM #RightsTemp rt INNER JOIN #Rights rR ON rt.UserKey = rR.UserKey AND rt.FactoryID = rR.FactoryID COLLATE DATABASE_DEFAULT AND rt.ProductLineID = rR.ProductLineID COLLATE DATABASE_DEFAULT WHERE rR.FactoryID <> '' AND rR.ProductLineID <> ''; --SELECT * FROM #RightsTemp ORDER BY Username,FactoryID,ProductLineID -- OUTPUT -- TRUNCATE TABLE system.trUserRights -- deactivated, because a /* The minimum permission required is ALTER on table_name. TRUNCATE TABLE permissions default to the table owner, members of the sysadmin fixed server role, and the db_owner and db_ddladmin fixed database roles, and are not transferable. However, you can incorporate the TRUNCATE TABLE statement within a module, such as a stored procedure, and grant appropriate permissions to the module using the EXECUTE AS clause. The EXECUTE AS Clause I did not master till now in foreign environments. */ DELETE FROM system.trUserRights; INSERT INTO system.trUserRights ( UserKey ,UserName ,FactoryID ,ProductLineID ,[Right] ,ReadCommentMandatory ,WriteCommentMandatory ) SELECT UserKey ,Username ,FactoryID ,ProductLineID ,IIF(RightExplicit='-',RightImplicit,RightExplicit) AS [Right] ,ReadCommentMandatory ,WriteCommentMandatory FROM #RightsTemp END TRY BEGIN CATCH Print 'Rollback due to not executable command.'; END CATCH END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spMATERIALIZE_trUserRights' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to materialize effectiv Userrights on all levels from trUser and trRights, which save only hightest level. Procedure will be triggered from any action on tables trUser and trRights.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; GO DROP PROCEDURE IF EXISTS system.spPOST_Right; GO /* POST Operation for UserRight UserRight will be created always, not fitting other rights will be deleted to ensure enheritance Gerd Tautenhahn for Saxess Software GmbH Last modified: 04/2023 for OCT 5.9 Test call DECLARE @RC INT ,@UserName NVARCHAR(255) = 'SQL' ,@PostUserName NVARCHAR(255) = 'sxs' ,@FactoryID NVARCHAR(255) = 'ZT' ,@ProductLineID NVARCHAR(255) = 'U' ,@Right NVARCHAR(255) = 'Write' ,@ReadCommentMandatory NVARCHAR(255) = '' ,@WriteCommentMandatory NVARCHAR(255) = '' EXECUTE @RC = system.spPOST_Right @UserName, @PostUserName, @FactoryID, @ProductLineID, @Right, @ReadCommentMandatory, @WriteCommentMandatory PRINT @RC Testcall tables SELECT * FROM system.trRights; SELECT TOP 10 * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'dbo', 'PROCEDURE', 'sx_pf_POST_Right',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'dbo', 'PROCEDURE', 'sx_pf_POST_Right','PARAMETER',NULL) */ CREATE PROCEDURE system.spPOST_Right @UserName NVARCHAR(255), @PostUserName NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @Right NVARCHAR(255), @ReadCommentMandatory NVARCHAR(255), @WriteCommentMandatory NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @PostUserKey INT = 0 ,@FactoryKey INT = 0 ,@ProductLineKey INT = 0 ,@TransactUsername NVARCHAR(255) = N'' ,@ClusterRight NVARCHAR(255) = N'' ,@FactoryRight NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'ISNULL') + N''',''' + ISNULL(@PostUserName, N'ISNULL') + N''',''' + ISNULL(@FactoryID, N'ISNULL') + N''',''' + ISNULL(@ProductLineID, N'ISNULL') + N''',''' + ISNULL(@Right, N'ISNULL') + N''',''' + ISNULL(@ReadCommentMandatory, N'ISNULL') + N''',''' + ISNULL(@WriteCommentMandatory, N'ISNULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@Message NVARCHAR(2000) = N'' ,@PostUserIsAdmin INT = 0; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @PostUserName IS NULL SET @PostUserName = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @Right IS NULL SET @Right = N''; IF @ReadCommentMandatory IS NULL SET @ReadCommentMandatory = N''; IF @WriteCommentMandatory IS NULL SET @WriteCommentMandatory = N''; -- Capitlize the word Read / Write, lower case for other letters SET @Right = UPPER(LEFT(@Right,1))+LOWER(SUBSTRING(@Right,2,LEN(@Right))); BEGIN TRY BEGIN TRANSACTION sx_pf_POST_Right; -- STEP 0.2 - Protect input parameters SET @PostUserName = system.fProtectString (@PostUserName); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @FactoryID = system.fProtectID (@FactoryID); SET @Right = system.fProtectString (@Right); SET @ReadCommentMandatory = system.fProtectBoolean (@ReadCommentMandatory); SET @WriteCommentMandatory = system.fProtectBoolean (@WriteCommentMandatory); -- STEP 0.3 - Clean IDs SET @Right = LTRIM(RTRIM(@Right)); SET @PostUserName = LTRIM(RTRIM(@PostUserName)); SET @FactoryID = LTRIM(RTRIM(@FactoryID)); SET @ProductLineID = LTRIM(RTRIM(@ProductLineID)); IF @Right = N'' OR @PostUserName = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- It`s forbidden to post rights for system users with name 'SQL' IF @PostUserName = N'SQL' BEGIN SET @ResultCode = 403; RAISERROR('Forbidden to post rights for system users reserved name.', 16, 10, @PostUserName); END; -- Determine transaction user, the transactuser 'public' is not allowed to post rights SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END ELSE IF @TransactUsername = N'public' BEGIN SET @ResultCode = 403; RAISERROR('''Public'' user is not allowed to post rights.', 16, 10); END; -- Only OCT Administrators can manage users IF NOT EXISTS (SELECT UserKey FROM system.trUser WHERE UserName = @TransactUsername AND IsAdministratorFlag = 1 AND Status = 'Active' ) BEGIN SET @Message = CONCAT('Invalid rights, User "',@Transactusername,'" is not an OCT Administrator.'); RAISERROR(@Message, 16, 10); END; -- It`s forbidden for a ClusterAdmin to change his own rights IF @PostUserName = @TransactUsername BEGIN SET @ResultCode = 403; RAISERROR('Forbidden for a ClusterAdmin to change his own rights', 16, 10); END; -- Its forbidden to change other administrators rights SELECT @PostUserIsAdmin = COUNT(UserName) FROM system.trUser WHERE UserName = @PostUserName AND IsAdministratorFlag = 1; IF @PostUserIsAdmin = 1 BEGIN SET @ResultCode = 403; RAISERROR(N'Forbidden to change the rights of an Administrator.', 16, 10); END; -- Empty factory ID must always be connected to empty ProductLineID IF @FactoryID = N'' AND @ProductLineID <> N'' BEGIN SET @ResultCode = 401; RAISERROR('Empty factory ID must always be connected to empty ProductLineID.', 16, 10); END; -- STEP 2.3 - Check keys IF @FactoryID <> N'' BEGIN SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; IF @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys not exists', 16, 10); END; END; IF @ProductLineID <> N'' BEGIN SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE ProductLineID = @ProductLineID AND FactoryKey = @FactoryKey; IF @ProductLineKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys not exists', 16, 10); END; END; SELECT @PostUserKey = UserKey FROM system.trUser WHERE UserName = @PostUserName; IF @PostUserKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys not exists', 16, 10); END; -- STEP 3 - Set Rule IF @Right = N'Deny' BEGIN -- 'Deny' will delete a right, but never create an entry IF @FactoryID = N'' DELETE FROM system.trRights WHERE UserKey = @PostUserKey; ELSE IF @ProductLineID = N'' DELETE FROM system.trRights WHERE UserKey = @PostUserKey AND FactoryID = @FactoryID; ELSE DELETE FROM system.trRights WHERE UserKey = @PostUserKey AND FactoryID = @FactoryID AND ProductLineID = @ProductLineID; SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = 204; END ELSE BEGIN -- Saves only explicit rights on objects and deletes rights which get invalid through right definition on upper levels -- Check always Clusteright SELECT TOP (1) @ClusterRight = COALESCE([Right], N'None') FROM system.trRights WHERE UserKey = @PostUserKey AND FactoryID = N'' ORDER BY [Right]; SET @ResultCode = 204; -- CLUSTER RIGHTS are posted +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -- Write / Read -> delete all other Rights and insert IF @FactoryID = N'' AND @ProductLineID = N'' BEGIN DELETE FROM system.trRights WHERE UserKey = @PostUserKey; SET @EffectedRows += @@ROWCOUNT; INSERT INTO system.trRights (UserKey ,UserName ,FactoryID ,ProductLineID ,ProductID ,[Right] ,ReadCommentMandatory ,WriteCommentMandatory) VALUES (@PostUserKey -- UserKey int ,@PostUserName -- UserName nvarchar(255) ,N'' -- FactoryID nvarchar(255) ,N'' -- ProductLineID nvarchar(255) ,N'' -- ProductID nvarchar(255) ,@Right -- Right nvarchar(255) ,@ReadCommentMandatory -- ReadCommentMandatory int ,@WriteCommentMandatory -- WriteCommentMandatory int ); SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = 200; END; -- FACTORY RIGHTS are posted +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ IF @FactoryID <> N'' AND @ProductLineID = N'' BEGIN -- Write -> delete all other Right in this Factory, dont accept if user has write on Cluster IF @Right = N'Write' AND @ClusterRight <> N'Write' BEGIN DELETE FROM system.trRights WHERE UserKey = @PostUserKey AND FactoryID = @FactoryID; SET @EffectedRows += @@ROWCOUNT; INSERT INTO system.trRights ( UserKey ,UserName ,FactoryID ,ProductLineID ,ProductID ,[Right] ,ReadCommentMandatory ,WriteCommentMandatory) VALUES (@PostUserKey -- UserKey int ,@PostUserName -- UserName nvarchar(255) ,@FactoryID -- FactoryID nvarchar(255) ,N'' -- ProductLineID nvarchar(255) ,N'' -- ProductID nvarchar(255) ,@Right -- Right nvarchar(255) ,@ReadCommentMandatory -- ReadCommentMandatory int ,@WriteCommentMandatory -- WriteCommentMandatory int ); SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = 200; END; -- Read if no cluster rights set -> delete all other Rights in this Factory, dont accept if user has write or read on Cluster IF @Right = N'Read' AND @ClusterRight <> N'Write' AND @ClusterRight <> N'Read' BEGIN DELETE FROM system.trRights WHERE UserKey = @PostUserKey AND FactoryID = @FactoryID; SET @EffectedRows += @@ROWCOUNT; INSERT INTO system.trRights (UserKey ,UserName ,FactoryID ,ProductLineID ,ProductID ,[Right] ,ReadCommentMandatory ,WriteCommentMandatory) VALUES ( @PostUserKey -- UserKey int ,@PostUserName -- UserName nvarchar(255) ,@FactoryID -- FactoryID nvarchar(255) ,N'' -- ProductLineID nvarchar(255) ,N'' -- ProductID nvarchar(255) ,@Right -- Right nvarchar(255) ,@ReadCommentMandatory -- ReadCommentMandatory int ,@WriteCommentMandatory -- WriteCommentMandatory int ); SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = 200; END; -- Read if Clusterwrite rights -> delete all other Rights in this Factory, dont accept if user has write or read on Cluster IF @Right = N'Read' AND @ClusterRight = N'Read' BEGIN DELETE FROM system.trRights WHERE UserKey = @PostUserKey AND FactoryID = @FactoryID; SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = 200; END END; -- PRODUCTLINE RIGHT is posted +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ IF @FactoryID <> N'' AND @ProductLineID <> N'' BEGIN -- Look for Rights on Factory Level SELECT TOP (1) @FactoryRight = COALESCE([Right], N'None') FROM system.trRights WHERE UserKey = @PostUserKey AND FactoryID = @FactoryID AND ProductLineID = N'' ORDER BY [Right]; -- Write -> dont accept if user has already write on Cluster or Factory IF @Right = N'Write' AND @ClusterRight <> N'Write' AND @FactoryRight <> N'Write' BEGIN DELETE FROM system.trRights WHERE UserKey = @PostUserKey AND FactoryID = @FactoryID AND ProductLineID = @ProductLineID; SET @EffectedRows += @@ROWCOUNT; INSERT INTO system.trRights ( UserKey ,UserName ,FactoryID ,ProductLineID ,ProductID ,[Right] ,ReadCommentMandatory ,WriteCommentMandatory) VALUES ( @PostUserKey -- UserKey int ,@PostUserName -- UserName nvarchar(255) ,@FactoryID -- FactoryID nvarchar(255) ,@ProductLineID -- ProductLineID nvarchar(255) ,N'' -- ProductID nvarchar(255) ,@Right -- Right nvarchar(255) ,@ReadCommentMandatory -- ReadCommentMandatory int ,@WriteCommentMandatory -- WriteCommentMandatory int ); SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = 200; END; -- Read -> don`t accept if user has read or write on Cluster or Factory IF @Right = N'Read' AND @ClusterRight <> N'Write' AND @ClusterRight <> N'Read' AND @FactoryRight <> N'Write' AND @FactoryRight <> N'Read' BEGIN DELETE FROM system.trRights WHERE UserKey = @PostUserKey AND FactoryID = @FactoryID AND ProductLineID = @ProductLineID; SET @EffectedRows += @@ROWCOUNT; INSERT INTO system.trRights ( UserKey ,UserName ,FactoryID ,ProductLineID ,ProductID ,[Right] ,ReadCommentMandatory ,WriteCommentMandatory) VALUES ( @PostUserKey -- UserKey int ,@PostUserName -- UserName nvarchar(255) ,@FactoryID -- FactoryID nvarchar(255) ,@ProductLineID -- ProductLineID nvarchar(255) ,N'' -- ProductID nvarchar(255) ,@Right -- Right nvarchar(255) ,@ReadCommentMandatory -- ReadCommentMandatory int ,@WriteCommentMandatory -- WriteCommentMandatory int ); SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = 200; END; -- Read -> if read should only kill an existing write in Factories with global Read Rights, just delete Write Right IF @Right = N'Read' AND @FactoryRight = N'Read' BEGIN DELETE FROM system.trRights WHERE UserKey = @PostUserKey AND FactoryID = @FactoryID AND ProductLineID = @ProductLineID; SET @EffectedRows += @@ROWCOUNT; END; END; END; COMMIT TRANSACTION sx_pf_POST_Right; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_Right; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Right' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for UserRight UserRight will be created always, not fitting other rights will be deleted to ensure enheritance'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@UserName'; SET @value = N'UserName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PostUserName'; SET @value = N'PostUserName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Right'; SET @value = N'Right'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ReadCommentMandatory'; SET @value = N'ReadCommentMandatory'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@WriteCommentMandatory'; SET @value = N'WriteCommentMandatory'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spPOST_User; GO /* POST Operation for a User Not existing User will be created, existing one updated Gerd Tautenhahn for Saxess Software GmbH Last modified: 04/2023 for OCT 5.9 Testcall Procedure DECLARE @RC INT ,@Username NVARCHAR(255) = N'SQL' ,@PostUserName NVARCHAR(255) = N'sxs' ,@PersonSurname NVARCHAR(255) = N'' ,@PersonFirstName NVARCHAR(255) = N'' ,@Email NVARCHAR(255) = N'' ,@LDAPIP NVARCHAR(255) = N'' ,@Status NVARCHAR(255) = N'Active' ,@DataFlowRightsCODE NVARCHAR(255) = N'DESIGNER' ,@IsAdministratorFlag BIT = 0; EXECUTE @RC = system.spPOST_User @Username ,@PostUserName ,@PersonSurname ,@PersonFirstName ,@Email ,@LDAPIP ,@Status ,@DataFlowRightsCODE ,@IsAdministratorFlag; PRINT @RC; Testcall Target Table SELECT * FROM system.trUser; SELECT TOP 5 * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_User',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_User','PARAMETER',NULL) */ CREATE PROCEDURE system.spPOST_User @Username NVARCHAR(255) ,@PostUserName NVARCHAR(255) ,@PersonSurname NVARCHAR(255) ,@PersonFirstName NVARCHAR(255) ,@Email NVARCHAR(255) ,@LDAPIP NVARCHAR(255) ,@Status NVARCHAR(255) ,@DataFlowRightsCODE NVARCHAR(255) ,@IsAdministratorFlag BIT AS BEGIN SET NOCOUNT ON; DECLARE @PostUserKey INT = 0 ,@TransactUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@PostUserName, N'NULL') + N''',''' + ISNULL(@PersonSurname, N'NULL') + N''',''' + ISNULL(@PersonFirstName, N'NULL') + N''',''' + ISNULL(@Email, N'NULL') + N''',''' + ISNULL(@LDAPIP, N'NULL') + N''',''' + ISNULL(@Status, N'NULL') + N''',''' + ISNULL(@DataFlowRightsCODE, N'NULL') + N'''' + ISNULL(CAST(@IsAdministratorFlag AS NCHAR(1)), N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@Message NVARCHAR(2000) = N''; DECLARE @tmpUserTable TABLE (UserKey INT NOT NULL); IF @Username IS NULL SET @Username = N''; IF @PostUserName IS NULL SET @PostUserName = N''; IF @PersonSurname IS NULL SET @PersonSurname = N''; IF @PersonFirstName IS NULL SET @PersonFirstName = N''; IF @Email IS NULL SET @Email = N''; IF @LDAPIP IS NULL SET @LDAPIP = N''; IF @Status IS NULL SET @Status = N''; IF @DataFlowRightsCODE IS NULL SET @DataFlowRightsCODE = N''; IF @IsAdministratorFlag IS NULL SET @IsAdministratorFlag = 0; BEGIN TRY BEGIN TRANSACTION spPOST_User; -- STEP 0.2 - Protect input parameters SET @PostUserName = system.fProtectString (@PostUserName); SET @PersonSurname = system.fProtectString (@PersonSurname); SET @PersonFirstName = system.fProtectString (@PersonFirstName); SET @Email = system.fProtectString (@Email); SET @LDAPIP = system.fProtectString (@LDAPIP); SET @Status = system.fProtectString (@Status); -- DataFlowRightsCODE Check IF @DataFlowRightsCODE NOT IN (N'DESIGNER', N'EXECUTOR') SET @DataFlowRightsCODE = N'NONE'; IF @PostUserName = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- Don`t allow creation of an User "SQL" - this is a reserverd word IF @PostUserName = N'SQL' BEGIN SET @ResultCode = 403; RAISERROR('A username "SQL" is not an valid username.', 16, 10); END; -- Determine transaction user, the transactuser 'public' is not allowed to change rights SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END ELSE IF @TransactUsername = N'public' BEGIN SET @ResultCode = 403; RAISERROR('User "public" is not allowed to change rights', 16, 10); END; -- Only OCT Administrators can manage users IF NOT EXISTS (SELECT UserKey FROM system.trUser WHERE UserName = @TransactUsername AND IsAdministratorFlag = 1 AND Status = N'Active' ) BEGIN SET @Message = CONCAT('Invalid rights, User "',@Transactusername,'" is not an OCT Administrator.'); RAISERROR(@Message, 16, 10); END; -- The Clusteradmin can`t set himself to Inactive or remove his designer rights IF @TransactUsername = @PostUsername BEGIN SET @Status = N'Active'; SET @DataFlowRightsCODE = N'DESIGNER'; SET @IsAdministratorFlag = 1; END; -- Administrators have always full rights IF @IsAdministratorFlag = 1 BEGIN SET @DataFlowRightsCODE = N'DESIGNER'; END; -- Get keys SELECT @PostUserKey = UserKey FROM system.trUser WHERE UserName = @PostUserName; -- If user does not yet exist, it will be created IF @PostUserKey = 0 BEGIN INSERT INTO system.trUser (UserName, PersonSurname, PersonFirstName, Email, LDAPIP, Status, DataFlowRightsCODE, IsAdministratorFlag) OUTPUT INSERTED.UserKey INTO @tmpUserTable VALUES (@PostUserName, '', '', '', '', '', '', 0); SET @EffectedRows = 1; -- the determination on the table variable is necessary because of TRIGGERS on system.trUser SELECT TOP (1) @PostUserKey = UserKey FROM @tmpUserTable ORDER BY UserKey; SET @Resultcode = 201; END ELSE -- existing User case BEGIN SET @ResultCode = 200; END -- Update the User with the commited attributes UPDATE system.trUser SET PersonSurname = IIF(@PersonSurname = N'<#NV>', PersonSurname, @PersonSurname) ,PersonFirstName = IIF(@PersonFirstName = N'<#NV>', PersonFirstName, @PersonFirstName) ,Email = IIF(@Email = N'<#NV>', Email, @Email) ,LDAPIP = IIF(@LDAPIP = N'<#NV>', LDAPIP, @LDAPIP) ,Status = IIF(@Status = N'<#NV>', Status, @Status) ,DataFlowRightsCODE = @DataFlowRightsCODE ,IsAdministratorFlag = @IsAdministratorFlag WHERE UserKey = @PostUserKey; SET @EffectedRows += @@ROWCOUNT; -- Administrators get full DataEntry Rights IF @IsAdministratorFlag = 1 BEGIN DELETE FROM system.trRights WHERE UserName = @PostUserName; INSERT INTO system.trRights ( UserKey ,UserName ,FactoryID ,ProductLineID ,ProductID ,[Right] ,ReadCommentMandatory ,WriteCommentMandatory ) VALUES (@PostUserKey,@PostUserName,'','','','Write',0,0) END; SET @ResultCode = 200; COMMIT TRANSACTION spPOST_User; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spPOST_User; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_User' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for a User Not existing User will be created, existing one updated If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PostUserName'; SET @value = N'PostUserName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PersonSurname'; SET @value = N'PersonSurname'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PersonFirstName'; SET @value = N'PersonFirstName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Email'; SET @value = N'Email'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@LDAPIP'; SET @value = N'LDAPIP'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Status'; SET @value = N'Status'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@DataFlowRightsCODE'; SET @value = N'CODE Word for the DataFlow Rights of this user - DESIGNER, EXECUTOR, NONE'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@IsAdministratorFlag'; SET @value = N'Flag 0/1 so set this user as an Administrator of this database.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_ParamTable; GO /* Procedure to auto create param tables from all products in ProductLine PARAM in Factory ZT Uses dynamic SQL ! Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall EXEC planning.spPOST_ParamTable '1-1' Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ParamTable',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ParamTable','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_ParamTable @ProductID NVARCHAR(255) AS BEGIN SET NOCOUNT ON -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@ProductID ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@Username NVARCHAR(255) = N'SQL' DECLARE @TableDelete NVARCHAR(MAX) = '' ,@SQLDrop NVARCHAR(MAX) = '' ,@ProductKey BIGINT = 0; -- START TRANSACTION *********************************************************************************** BEGIN TRY BEGIN TRANSACTION spParamTables; -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- DELETE existing param.tPivot table SET @TableDelete = 'param.[tPivot' + @ProductID+ ']'; SET @SQLDrop = 'DROP TABLE IF EXISTS ' + @TableDelete; EXECUTE sp_executesql @SQLDrop; -- CREATE new table SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE FactoryID = 'ZT' AND ProductLineID = 'PARAM' AND ProductID = @ProductID; IF @ProductKey > 0 BEGIN -- CREATE TABLE ------------------------------------------------------------------------------------------------------------------- DECLARE @TableSchema NVARCHAR(255) = 'param.' ,@TablePreName NVARCHAR(255) = 'tPivot' ,@StringBasic NVARCHAR(MAX) = 'RowKey BIGINT IDENTITY(1,1),FactoryID NVARCHAR(255),ProductLineID NVARCHAR(255),ProductID NVARCHAR(255),TimeID INT' ,@StringColumns NVARCHAR(MAX) = '' ,@SQLCreate NVARCHAR(MAX) = '' ,@TableName NVARCHAR(MAX) = ''; SELECT @TableName = @TableSchema + '[' + @TablePreName + @ProductID + ']'; -- Gets all ValueSeriesIDs from a Product in ProductlLine 'PARAM' in Factory 'ZT', ValueSeriesIDs will become column headers -- All Text Values will become NVARCHAR(4000), all Numeric Columns Type Money -- Collect definition of all columns SELECT @StringColumns = COALESCE(@StringColumns + ',', '') + IIF( dVS.[IsNumeric]=0 ,'[' + CONVERT(NVARCHAR(255),dVS.ValueSeriesID) + '] NVARCHAR(4000)' ,'[' + CONVERT(NVARCHAR(255),dVS.ValueSeriesID) + '] MONEY' ) FROM planning.tdValueSeries dVS WHERE dVS.ProductKey = @ProductKey ORDER BY dVS.ValueSeriesNo; -- create the table SET @SQLCreate = 'CREATE TABLE ' + @TableName + '(' + @StringBasic + @StringColumns + ' ,CONSTRAINT [PK_' + @TablePreName + @ProductID +'] PRIMARY KEY NONCLUSTERED (RowKey))'; EXECUTE sp_executesql @SQLCreate; -- Fill Table ------------------------------------------------------------------------------------------------------- DECLARE @STRING_PivotList NVARCHAR(MAX) ,@STRING_SelectColums NVARCHAR(MAX) ,@STRING_Insert NVARCHAR(MAX); -- Get string for dynamic SQL used in PIVOT command (...ValueSeriesID IN ([...])) SELECT @STRING_PivotList = COALESCE(@STRING_PivotList + ',', '') + '[' + CONVERT(NVARCHAR(255),ValueSeriesID) + ']' FROM planning.tdValueSeries dVS WHERE dVS.ProductKey = @ProductKey ORDER BY dVS.ValueSeriesNo; -- Get string for dynamic SQL - ColumnNames SELECT @STRING_SelectColums = COALESCE(@STRING_SelectColums + ',', '') + 'COALESCE([' + CONVERT(NVARCHAR(255),ValueSeriesID) + '],'''') AS [' + convert(NVARCHAR(255),ValueSeriesID) + ']' FROM planning.tdValueSeries dVS WHERE dVS.ProductKey = @ProductKey ORDER BY dVS.ValueSeriesNo; -- Get string for dynamic SQL used in INSERT command SELECT @STRING_Insert = COALESCE(@STRING_Insert + ',', '') +IIF( dVS.[IsNumeric]=0 ,'COALESCE([' + CONVERT(NVARCHAR(255),ValueSeriesID) + '],'''')' ,'COALESCE(CAST([' + CONVERT(NVARCHAR(255),dVS.ValueSeriesID) + '] AS MONEY) ' + ',0) ' ) FROM planning.tdValueSeries dVS WHERE dVS.ProductKey = @ProductKey ORDER BY dVS.ValueSeriesNo; -- Select Product Values to #table to not use dynamic SQL on real table DROP TABLE IF EXISTS #ParamValues; CREATE TABLE #ParamValues ( FactoryID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,TimeID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,Value NVARCHAR(4000) COLLATE DATABASE_DEFAULT NOT NULL ); INSERT INTO #ParamValues SELECT fV.FactoryID ,fV.ProductLineID ,fV.ProductID ,fV.ValueSeriesID ,fV.TimeID ,CASE WHEN dVS.[IsNumeric] = 0 THEN fV.ValueText WHEN dVS.[IsNumeric] = 1 THEN CONVERT(NVARCHAR(255),CAST(fV.ValueInt AS MONEY)/dVS.Scale,2) --warum Parameter 2 im Convert ? END AS Value FROM planning.tfValues fV LEFT JOIN planning.tdValueSeries dVS ON fV.ValueSeriesKey = dVS.ValueSeriesKey LEFT JOIN planning.tdProducts dP ON fV.ProductKey = dP.ProductKey WHERE fV.ProductKey = @ProductKey ORDER BY dVS.ValueSeriesNo; DECLARE @SQLText NVARCHAR(MAX) = 'INSERT INTO ' + @TableName + ' SELECT PivotT.FactoryID,PivotT.ProductLineID,PivotT.ProductID,PivotT.TimeID ,' + @STRING_Insert + ' FROM #ParamValues PIVOT (MAX(Value) FOR ValueSeriesID IN (' + @STRING_PivotList + ')) AS PivotT'; EXECUTE sp_executesql @SQLText; SELECT @EffectedRows = COUNT(1) FROM #ParamValues; -- job well done SET @ResultCode = 200; END ELSE BEGIN SET @ResultCode = 404; END COMMIT TRANSACTION spParamTables; END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spParamTables; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_ParamTable' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N''; -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to auto create param tables from all products in ProductLine PARAM in Factory ZT'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@ProductID'; SET @value = N'ProductID of an Product in Factory ZT / ProductLine PARAM'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spCOPY_Factory; GO /* COPY Operation for a Factory Not yet existing factory is copied when Source Factory exists Saxess Software GmbH Last modified 01/2024 for OCT 5.10 Test call 1. Empty source or target FactoryID => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User has no Rights => 401 Unauthorized 4. Not exists Source Factory => 404 Not Found 5. Target Factory key already exists => 403 Forbidden 6. Factory created > 201 Created DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL'; ,@SourceFactoryID NVARCHAR(255) = 'ZT' ,@TargetFactoryID NVARCHAR(255) = 'ZT2' ,@TargetFactoryNameShort NVARCHAR(255) = 'Neue Factory'; EXECUTE @RC = planning.spCOPY_Factory @Username ,@SourceFactoryID ,@TargetFactoryID ,@TargetFactoryNameShort; PRINT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spCOPY_Factory',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spCOPY_Factory','PARAMETER',NULL) */ CREATE PROCEDURE planning.spCOPY_Factory @Username NVARCHAR(255), @SourceFactoryID NVARCHAR(255), @TargetFactoryID NVARCHAR(255), @TargetFactoryNameShort NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @TransactUserKey INT = 0; DECLARE @SourceFactoryKey INT = 0; DECLARE @SourceFactoryNameShort NVARCHAR(255) = N''; DECLARE @TargetFactoryKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@SourceFactoryID, N'NULL') + N''',''' + ISNULL(@TargetFactoryID, N'NULL') + N''',''' + ISNULL(@TargetFactoryNameShort, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @SourceFactoryID IS NULL SET @SourceFactoryID = N''; IF @TargetFactoryID IS NULL SET @TargetFactoryID = N''; IF @TargetFactoryNameShort IS NULL SET @TargetFactoryNameShort = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_COPY_Factory; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @SourceFactoryID = system.fProtectID (@SourceFactoryID); SET @TargetFactoryID = system.fProtectID (@TargetFactoryID); SET @TargetFactoryNameShort = system.fProtectString (@TargetFactoryNameShort); IF @SourceFactoryID = N'' OR @TargetFactoryID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine SourceKeys SELECT @SourceFactoryKey = FactoryKey, @SourceFactoryNameShort = NameShort FROM planning.tdFactories WHERE FactoryID = @SourceFactoryID; IF @SourceFactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Source keys don`t exists', 16, 10); END; -- STEP 1.3 - Determine TargetKeys SELECT @TargetFactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @TargetFactoryID; IF @TargetFactoryKey <> 0 BEGIN SET @ResultCode = 403; RAISERROR('Target Format key already exists', 16, 10); END; -- STEP 3 - Copy data DECLARE @Keys AS TABLE ([Key] INT NOT NULL); -- dFactory INSERT INTO planning.tdFactories ( FactoryID ,NameShort ,NameLong ,CommentUser ,CommentDev ,ResponsiblePerson ,ImageName ) OUTPUT INSERTED.FactoryKey INTO @Keys([Key]) SELECT @TargetFactoryID AS FactoryID , @TargetFactoryNameShort AS NameShort , NameLong , CommentUser , CommentDev , ResponsiblePerson , ImageName FROM planning.tdFactories WHERE FactoryID = @SourceFactoryID; SET @EffectedRows = @@ROWCOUNT; SELECT TOP 1 @TargetFactoryKey = [Key] FROM @Keys; --dProductLine INSERT INTO planning.tdProductLines ( FactoryKey ,ProductLineID ,FactoryID ,NameShort ,NameLong ,CommentUser ,CommentDev ,ResponsiblePerson ,ImageName ,DefaultTemplate ,GlobalAttributeSource1 ,GlobalAttributeAlias1 ,GlobalAttributeSource2 ,GlobalAttributeAlias2 ,GlobalAttributeSource3 ,GlobalAttributeAlias3 ,GlobalAttributeSource4 ,GlobalAttributeAlias4 ,GlobalAttributeSource5 ,GlobalAttributeAlias5 ,GlobalAttributeSource6 ,GlobalAttributeAlias6 ,GlobalAttributeSource7 ,GlobalAttributeAlias7 ,GlobalAttributeSource8 ,GlobalAttributeAlias8 ,GlobalAttributeSource9 ,GlobalAttributeAlias9 ,GlobalAttributeSource10 ,GlobalAttributeAlias10 ,GlobalAttributeSource11 ,GlobalAttributeAlias11 ,GlobalAttributeSource12 ,GlobalAttributeAlias12 ,GlobalAttributeSource13 ,GlobalAttributeAlias13 ,GlobalAttributeSource14 ,GlobalAttributeAlias14 ,GlobalAttributeSource15 ,GlobalAttributeAlias15 ,GlobalAttributeSource16 ,GlobalAttributeAlias16 ,GlobalAttributeSource17 ,GlobalAttributeAlias17 ,GlobalAttributeSource18 ,GlobalAttributeAlias18 ,GlobalAttributeSource19 ,GlobalAttributeAlias19 ,GlobalAttributeSource20 ,GlobalAttributeAlias20 ,GlobalAttributeSource21 ,GlobalAttributeAlias21 ,GlobalAttributeSource22 ,GlobalAttributeAlias22 ,GlobalAttributeSource23 ,GlobalAttributeAlias23 ,GlobalAttributeSource24 ,GlobalAttributeAlias24 ,GlobalAttributeSource25 ,GlobalAttributeAlias25 ,LayoutJSON ) SELECT @TargetFactoryKey AS FactoryKey , ProductLineID , @TargetFactoryID AS FactoryID , NameShort , NameLong , CommentUser , CommentDev , ResponsiblePerson , ImageName , DefaultTemplate , GlobalAttributeSource1, GlobalAttributeAlias1, GlobalAttributeSource2, GlobalAttributeAlias2, GlobalAttributeSource3, GlobalAttributeAlias3 , GlobalAttributeSource4, GlobalAttributeAlias4, GlobalAttributeSource5, GlobalAttributeAlias5, GlobalAttributeSource6, GlobalAttributeAlias6 , GlobalAttributeSource7, GlobalAttributeAlias7, GlobalAttributeSource8, GlobalAttributeAlias8, GlobalAttributeSource9, GlobalAttributeAlias9 , GlobalAttributeSource10, GlobalAttributeAlias10, GlobalAttributeSource11, GlobalAttributeAlias11, GlobalAttributeSource12, GlobalAttributeAlias12 , GlobalAttributeSource13, GlobalAttributeAlias13, GlobalAttributeSource14, GlobalAttributeAlias14, GlobalAttributeSource15, GlobalAttributeAlias15 , GlobalAttributeSource16, GlobalAttributeAlias16, GlobalAttributeSource17, GlobalAttributeAlias17, GlobalAttributeSource18, GlobalAttributeAlias18 , GlobalAttributeSource19, GlobalAttributeAlias19, GlobalAttributeSource20, GlobalAttributeAlias20, GlobalAttributeSource21, GlobalAttributeAlias21 , GlobalAttributeSource22, GlobalAttributeAlias22, GlobalAttributeSource23, GlobalAttributeAlias23, GlobalAttributeSource24, GlobalAttributeAlias24 , GlobalAttributeSource25, GlobalAttributeAlias25 , LayoutJSON FROM planning.tdProductLines WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; --dProduct INSERT INTO planning.tdProducts ( ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,TimeType ,NameShort ,NameLong ,CommentUser ,CommentDev ,ResponsiblePerson ,ImageName ,Status ,Template ,TemplateVersion ,GlobalAttribute1 ,GlobalAttribute2 ,GlobalAttribute3 ,GlobalAttribute4 ,GlobalAttribute5 ,GlobalAttribute6 ,GlobalAttribute7 ,GlobalAttribute8 ,GlobalAttribute9 ,GlobalAttribute10 ,GlobalAttribute11 ,GlobalAttribute12 ,GlobalAttribute13 ,GlobalAttribute14 ,GlobalAttribute15 ,GlobalAttribute16 ,GlobalAttribute17 ,GlobalAttribute18 ,GlobalAttribute19 ,GlobalAttribute20 ,GlobalAttribute21 ,GlobalAttribute22 ,GlobalAttribute23 ,GlobalAttribute24 ,GlobalAttribute25 ) SELECT PL.ProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , ProductID , dP.ProductLineID , @TargetFactoryID AS FactoryID , TimeType , NameShort , NameLong , CommentUser , CommentDev , ResponsiblePerson , ImageName , Status , Template , TemplateVersion , GlobalAttribute1, GlobalAttribute2, GlobalAttribute3, GlobalAttribute4, GlobalAttribute5, GlobalAttribute6, GlobalAttribute7 , GlobalAttribute8, GlobalAttribute9, GlobalAttribute10, GlobalAttribute11, GlobalAttribute12, GlobalAttribute13, GlobalAttribute14 , GlobalAttribute15, GlobalAttribute16, GlobalAttribute17, GlobalAttribute18, GlobalAttribute19, GlobalAttribute20, GlobalAttribute21 , GlobalAttribute22, GlobalAttribute23, GlobalAttribute24, GlobalAttribute25 FROM planning.tdProducts dP LEFT JOIN ( SELECT ProductLineKey, ProductLineID FROM planning.tdProductLines WHERE FactoryKey = @TargetFactoryKey) PL ON PL.ProductLineID = dP.ProductLineID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; --dValueSeries INSERT INTO planning.tdValueSeries ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,ValueSeriesNo ,NameShort ,NameLong ,CommentUser ,CommentDev ,ImageName ,IsNumeric ,VisibilityLevel ,ValueSource ,ValueListID ,ValueFormatID ,Unit ,Scale ,Effect ,EffectParameter ,AllowZero ) SELECT P.ProductKey AS ProductKey , P.ProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , dVS.ProductID , dVS.ProductLineID , @TargetFactoryID AS FactoryID , ValueSeriesID , ValueSeriesNo , NameShort , NameLong , CommentUser , CommentDev , ImageName , [IsNumeric] , VisibilityLevel , ValueSource , ValueListID , ValueFormatID , Unit , Scale , Effect , EffectParameter , AllowZero FROM planning.tdValueSeries dVS LEFT JOIN ( SELECT ProductKey, ProductLineKey, ProductID, ProductLineID FROM planning.tdProducts WHERE FactoryKey = @TargetFactoryKey) P ON P.ProductID = dVS.ProductID AND P.ProductLineID = dVS.ProductLineID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; --dTime INSERT INTO planning.tdTime ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,TimeID ,FormatID ) SELECT P.ProductKey AS ProductKey , P.ProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , dT.ProductID , dT.ProductLineID , @TargetFactoryID AS FactoryID , TimeID , FormatID FROM planning.tdTime dT LEFT JOIN ( SELECT ProductKey, ProductLineKey, ProductID, ProductLineID FROM planning.tdProducts WHERE FactoryKey = @TargetFactoryKey) P ON P.ProductID = dT.ProductID AND P.ProductLineID = dT.ProductLineID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; --fValues INSERT INTO planning.tfValues ( ValueSeriesKey ,ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueText ,ValueComment ) SELECT VS.ValueSeriesKey AS ValueSeriesKey , VS.ProductKey AS ProductKey , VS.ProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , fV.ProductID , fV.ProductLineID , @TargetFactoryID AS FactoryID , fV.ValueSeriesID , TimeID , ValueFormula , ValueInt , ValueText , ValueComment FROM planning.tfValues fV LEFT JOIN ( SELECT ProductKey, ValueSeriesKey, ProductLineKey, ProductID, ValueSeriesID, ProductLineID FROM planning.tdValueSeries WHERE FactoryKey = @TargetFactoryKey) VS ON fV.ValueSeriesID = VS.ValueSeriesID AND fV.ProductID = VS.ProductID AND fV.ProductLineID = VS.ProductLineID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; --gFactories INSERT INTO planning.tgFactories ( FactoryKey ,FactoryID ,PropertyID ,PropertyName ,CommentUser ,CommentDev ,Unit ,ValueText ,ValueInt ,Scale ,IsROSystemProperty ,FormatID ) SELECT @TargetFactoryKey AS FactoryKey ,@TargetFactoryID AS FactoryID ,PropertyID ,PropertyName ,CommentUser ,CommentDev ,Unit ,ValueText ,ValueInt ,Scale ,IsROSystemProperty ,FormatID FROM planning.tgFactories WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; --gProductLines INSERT INTO planning.tgProductLines ( ProductLineKey ,FactoryKey ,ProductLineID ,FactoryID ,PropertyID ,PropertyName ,CommentUser ,CommentDev ,Unit ,ValueText ,ValueInt ,Scale ,IsROSystemProperty ,FormatID ) SELECT PL.ProductLineKey AS ProductLineKey ,@TargetFactoryKey AS FactoryKey ,gPL.ProductLineID ,@TargetFactoryID AS FactoryID ,PropertyID ,PropertyName ,CommentUser ,CommentDev ,Unit ,ValueText ,ValueInt ,Scale ,IsROSystemProperty ,FormatID FROM planning.tgProductLines gPL LEFT JOIN ( SELECT ProductLineKey, ProductLineID FROM planning.tdProductLines WHERE FactoryKey = @TargetFactoryKey) PL ON PL.ProductLineID = gPL.ProductLineID WHERE FactoryKey = @SourceFactoryKey SET @EffectedRows += @@ROWCOUNT; --gProducts INSERT INTO planning.tgProducts ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,PropertyID ,PropertyName ,CommentUser ,CommentDev ,Unit ,ValueText ,ValueInt ,Scale ,IsROSystemProperty ,FormatID ) SELECT P.ProductKey AS ProductKey , P.ProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , gP.ProductID , gP.ProductLineID , @TargetFactoryID AS FactoryID , PropertyID , PropertyName , CommentUser , CommentDev , Unit , ValueText , ValueInt , Scale , IsROSystemProperty , FormatID FROM planning.tgProducts gP LEFT JOIN ( SELECT ProductKey, ProductLineKey, ProductID, ProductLineID FROM planning.tdProducts WHERE FactoryKey = @TargetFactoryKey) P ON P.ProductID = gP.ProductID AND P.ProductLineID = gP.ProductLineID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; --tFiles INSERT INTO planning.tFiles ( FactoryKey ,ProductLineKey ,ProductKey ,FactoryID ,ProductLineID ,ProductID ,FileID ,FileName ,FileExtension ,FileBody ,UploadedBy ,UploadedAt ,Filepath ) SELECT @TargetFactoryKey ,P.ProductLineKey ,P.ProductKey ,@TargetFactoryID AS FactoryID ,tF.ProductLineID AS ProductLineID ,tF.ProductID AS ProductID ,tF.FileID ,tF.FileName ,tF.FileExtension ,tF.FileBody ,tF.UploadedBy ,tF.UploadedAt ,tF.Filepath FROM planning.tFiles tF LEFT JOIN ( SELECT ProductKey, ProductLineKey, ProductID, ProductLineID FROM planning.tdProducts WHERE FactoryKey = @TargetFactoryKey) P ON P.ProductID = tF.ProductID AND P.ProductLineID = tF.ProductLineID WHERE FactoryID = @SourceFactoryID; SET @EffectedRows += @@ROWCOUNT; --tTabs - they can exist on multiple levels ! Tabs on Factories without Productlines and on Productlines without Products may exist INSERT INTO planning.tTabs ( FactoryKey ,ProductLineKey ,ProductKey ,FactoryID ,ProductLineID ,ProductID ,TabID ,Orderindex ,TabName ,TabHint ,TabText ,PresentationCODE ,Datasource ,IsVisibleBOOL ,ParameterJSON ,SQLCommand ) SELECT @TargetFactoryKey ,COALESCE(PL.ProductLineKey,-1) AS ProductLineKey ,COALESCE(P.ProductKey,-1) AS ProductKey ,@TargetFactoryID AS FactoryID ,COALESCE(tT.ProductLineID,'') AS ProductLineID ,COALESCE(tT.ProductID,'') AS ProductID ,tT.TabID ,tT.Orderindex ,tT.TabName ,tT.TabHint ,tT.TabText ,tT.PresentationCODE ,tT.Datasource ,tT.IsVisibleBOOL ,tT.ParameterJSON ,tT.SQLCommand FROM planning.tTabs tT -- Determine new ProductKeys LEFT JOIN ( SELECT ProductKey ,ProductLineKey ,ProductID ,ProductLineID FROM planning.tdProducts WHERE FactoryKey = @TargetFactoryKey ) P ON COALESCE(P.ProductID,'') = tT.ProductID AND COALESCE(P.ProductLineID,'') = tT.ProductLineID -- Determine new ProductLineKeys LEFT JOIN ( SELECT ProductLineKey ,ProductLineID FROM planning.tdProductlines WHERE FactoryKey = @TargetFactoryKey ) PL ON COALESCE(PL.ProductLineID,'') = tT.ProductLineID WHERE FactoryID = @SourceFactoryID; SET @EffectedRows += @@ROWCOUNT; -- tTabLayouts INSERT INTO planning.tTabLayouts ( FactoryID ,ProductLineID ,TabID ,LayoutID ,LayoutName ,LayoutJSON ,OrderIndex ,LayoutOwner ) SELECT @TargetFactoryID , ProductLineID , TabID , LayoutID , LayoutName , LayoutJSON , OrderIndex , LayoutOwner FROM planning.tTabLayouts WHERE FactoryID = @SourceFactoryID; SET @EffectedRows += @@ROWCOUNT; --fStatements CopyInformation INSERT INTO planning.tfStatements ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ActionType ,Statement ,UserName ,PCName ,ProcessorCode ,IPAddresses ,Timestamp ,IsDeleted ,IsResolved ) SELECT ProductKey , dp.ProductLineKey , @TargetFactoryKey AS FactoryKey , ProductID , dp.ProductLineID , @TargetFactoryID AS FactoryID , N'COPY' AS ActionType , N'Product produced as a copy of Factory <' + @SourceFactoryID + N'>"' + @SourceFactoryNameShort + N'" / ProductLine <' + dp.ProductLineID + N'>"' + dpl.NameShort + N'" / Product <' + ProductID + N'>"' + dp.NameShort + '"' AS Statement , @TransactUsername AS UserName , N'' AS PCName , N'' AS ProcessorCode , N'' AS IPAddresses , GETUTCDATE() AS Timestamp , 0 AS IsDeleted , 0 AS IsResolved FROM planning.tdProducts dp LEFT JOIN planning.tdProductLines dpl ON dp.ProductLineKey= dpl.ProductLineKey WHERE dp.FactoryKey = @TargetFactoryKey; SET @EffectedRows += @@ROWCOUNT; -- Right materialization EXEC system.spMATERIALIZE_trUserRights; SET @ResultCode = 201; COMMIT TRANSACTION sx_pf_COPY_Factory; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_COPY_Factory; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spCOPY_Factory' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to COPY Operation for a Factory'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceFactoryID'; SET @value = N'SourceFactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetFactoryID'; SET @value = N'TargetFactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetFactoryNameShort'; SET @value = N'TargetFactoryNameShort'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spCOPY_Format; GO /* Procedure for COPY Operation for a Format Not yet existing Format is copied if source Format is available Gerd Tautenhahn for Saxess Software GmbH Last modified 09/2022 for OCT 5.8 DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@SourceFormatID NVARCHAR(255) = 'EingabeGanzzahl' ,@TargetFormatID NVARCHAR(255) = '88C26'; EXECUTE @RC = planning.spCOPY_Format @Username ,@SourceFormatID ,@TargetFormatID; PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spCOPY_Format',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spCOPY_Format','PARAMETER',NULL) */ CREATE PROCEDURE planning.spCOPY_Format @Username NVARCHAR(255), @SourceFormatID NVARCHAR(255), @TargetFormatID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @SourceFormatKey INT = 0; DECLARE @TargetFormatKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@SourceFormatID, N'NULL') + N''',''' + ISNULL(@TargetFormatID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @SourceFormatID IS NULL SET @SourceFormatID = N''; IF @TargetFormatID IS NULL SET @TargetFormatID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_COPY_Format; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @SourceFormatID = system.fProtectID (@SourceFormatID); SET @TargetFormatID = system.fProtectID (@TargetFormatID); IF @SourceFormatID = N'' OR @TargetFormatID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine SourceKeys SELECT @SourceFormatKey = FormatKey FROM planning.thFormats WHERE FormatID = @SourceFormatID; IF @SourceFormatKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Source keys don`t exists', 16, 10); END; -- STEP 1.3 - Determine TargetKeys SELECT @TargetFormatKey = FormatKey FROM planning.thFormats WHERE FormatID = @TargetFormatID; IF @TargetFormatKey <> 0 BEGIN SET @ResultCode = 403; RAISERROR('Target Format key already exists', 16, 10); END; -- STEP 3 - Copy data --hFormats INSERT INTO planning.thFormats ( FormatID ,BackgroundColor ,FontColor ,ValueFormat ,Bold ,Italic ,AlignmentHorizontal ,AlignmentVertical ,TextUnderlined ,BorderToNext ,BorderToPrevious ) SELECT @TargetFormatID AS FormatID , BackgroundColor , FontColor , ValueFormat , Bold , Italic , AlignmentHorizontal , AlignmentVertical , TextUnderlined , BorderToNext , BorderToPrevious FROM planning.thFormats WHERE FormatKey = @SourceFormatKey; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 201; COMMIT TRANSACTION sx_pf_COPY_Format; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_COPY_Format; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spCOPY_Format' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure for COPY Operation for a Format'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceFormatID'; SET @value = N'SourceFormatID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetFormatID'; SET @value = N'TargetFormatID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spCOPY_List; GO /* COPY Operation for a List Not yet existing list is copied if Sources List is available Gerd Tautenhahn for Saxess Software GmbH Last modified 09/2022 for OCT 5.8 DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@SourceListID NVARCHAR(255) = 'suEvents' ,@TargetListID NVARCHAR(255) = '88C26'; EXECUTE @RC = planning.spCOPY_List @Username ,@SourceListID ,@TargetListID; PRINT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spCOPY_List',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spCOPY_List','PARAMETER',NULL) */ CREATE PROCEDURE planning.spCOPY_List @Username NVARCHAR(255), @SourceListID NVARCHAR(255), @TargetListID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @SourceListKey INT = 0; DECLARE @TargetListKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@SourceListID, N'NULL') + N''',''' + ISNULL(@TargetListID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @SourceListID IS NULL SET @SourceListID = N''; IF @TargetListID IS NULL SET @TargetListID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_COPY_List; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @SourceListID = system.fProtectID (@SourceListID); SET @TargetListID = system.fProtectID (@TargetListID); IF @SourceListID = N'' OR @TargetListID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine SourceKeys SELECT @SourceListKey = ListKey FROM planning.thLists WHERE ListID = @SourceListID; IF @SourceListKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Source keys don`t exists', 16, 10); END; -- STEP 1.3 - Determine TargetKeys SELECT @TargetListKey = ListKey FROM planning.thLists WHERE ListID = @TargetListID; IF @TargetListKey <> 0 BEGIN SET @ResultCode = 403; RAISERROR('Target List key already exists', 16, 10); END; -- STEP 3 - Copy data --hLists INSERT INTO planning.thLists ( ListID ,NameShort ,NameLong ,CommentDev ,CommentUser ,Datentyp ,Source ,SourceFormula ,FormatID ) SELECT @TargetListID AS ListsID , NameShort , NameLong , CommentDev , CommentUser , Datentyp , Source , SourceFormula , FormatID FROM planning.thLists WHERE ListKey = @SourceListKey; SET @EffectedRows += @@ROWCOUNT; --hListValues INSERT INTO planning.thListValues ( ListID ,ValueInt ,Scale ,ValueText ,ValueComment ,FormatID ) SELECT @TargetListID AS ListsID , ValueInt , Scale , ValueText , ValueComment , FormatID FROM planning.thListValues WHERE ListID = @SourceListID; SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = 201; COMMIT TRANSACTION sx_pf_COPY_List; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_COPY_List; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spCOPY_List' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure for COPY Operation for a List'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceListID'; SET @value = N'SourceListID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetListID'; SET @value = N'TargetListID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spCOPY_Product; GO /* COPY Operation for a Product - creates a new Product from an Template - * as Target ProductID is supported, the next free numeric ID is used then Saxess Software GmbH Last modified 01/2024 for OCT 5.10 Test call 1. Empty source or target FactoryID / ProductLineID / ProductID => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User has no Rights => 401 Unauthorized 4. Not exists source Factory / ProductLine / Product => 404 Not Found 5. Not exists target Factory / ProductLine => 404 Not Found 6. Target Product key already exists => 403 Forbidden 7. Product copied => 201 Created DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@SourceFactoryID NVARCHAR(255) = 'WSB' ,@SourceProductLineID NVARCHAR(255) = 'KRED1' ,@SourceProductID NVARCHAR(255) = '0' ,@TargetFactoryID NVARCHAR(255) = 'WSB' ,@TargetProductLineID NVARCHAR(255) = 'KRED1' ,@TargetProductID NVARCHAR(255) = '*' ,@TargetProductNameShort NVARCHAR(255) = 'Test'; EXECUTE @RC = planning.spCOPY_Product @Username ,@SourceFactoryID ,@SourceProductLineID ,@SourceProductID ,@TargetFactoryID ,@TargetProductLineID ,@TargetProductID ,@TargetProductNameShort; PRINT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spCOPY_Product',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spCOPY_Product','PARAMETER',NULL) */ CREATE PROCEDURE planning.spCOPY_Product @Username NVARCHAR(255), @SourceFactoryID NVARCHAR(255), @SourceProductLineID NVARCHAR(255), @SourceProductID NVARCHAR(255), @TargetFactoryID NVARCHAR(255), @TargetProductLineID NVARCHAR(255), @TargetProductID NVARCHAR(255), @TargetProductNameShort NVARCHAR(255), @OutputID NVARCHAR(50) = NULL OUTPUT AS BEGIN SET NOCOUNT ON; DECLARE @SourceFactoryKey INT = 0; DECLARE @SourceProductKey INT = 0; DECLARE @SourceProductLineKey INT = 0; DECLARE @TargetProductKey INT = 0; DECLARE @SourceFactoryNameShort NVARCHAR(255) = N''; DECLARE @SourceProductLineNameShort NVARCHAR(255) = N''; DECLARE @SourceProductNameShort NVARCHAR(255) = N''; DECLARE @TargetFactoryKey INT = 0; DECLARE @TargetProductLineKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@SourceFactoryID, N'NULL') + N''',''' + ISNULL(@SourceProductLineID, N'NULL') + N''',''' + ISNULL(@SourceProductID, N'NULL') + N''',''' + ISNULL(@TargetFactoryID, N'NULL') + N''',''' + ISNULL(@TargetProductLineID, N'NULL') + N''',''' + ISNULL(@TargetProductID, N'NULL') + N''',''' + ISNULL(@TargetProductNameShort, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @SourceFactoryID IS NULL SET @SourceFactoryID = N''; IF @SourceProductLineID IS NULL SET @SourceProductLineID = N''; IF @SourceProductID IS NULL SET @SourceProductID = N''; IF @TargetFactoryID IS NULL SET @TargetFactoryID = N''; IF @TargetProductLineID IS NULL SET @TargetProductLineID = N''; IF @TargetProductID IS NULL SET @TargetProductID = N''; IF @TargetProductNameShort IS NULL SET @TargetProductNameShort = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_COPY_Product; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @SourceFactoryID = system.fProtectID (@SourceFactoryID); SET @SourceProductLineID = system.fProtectID (@SourceProductLineID); SET @SourceProductID = system.fProtectID (@SourceProductID); SET @TargetFactoryID = system.fProtectID (@TargetFactoryID); SET @TargetProductLineID = system.fProtectID (@TargetProductLineID); SET @TargetProductID = system.fProtectID (@TargetProductID); SET @TargetProductNameShort = system.fProtectString (@TargetProductNameShort); IF @SourceFactoryID = N'' OR @SourceProductLineID = N'' OR @SourceProductID = N'' OR @TargetFactoryID = N'' OR @TargetProductLineID = N'' OR @TargetProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @TargetFactoryID, @TargetProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights on target', 16, 10); EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @SourceFactoryID, @SourceProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights on source', 16, 10); -- STEP 1.2 - Determine SourceKeys SELECT @SourceFactoryKey = FactoryKey, @SourceFactoryNameShort = NameShort FROM planning.tdFactories WHERE FactoryID = @SourceFactoryID; SELECT @SourceProductLineKey = ProductLineKey, @SourceProductLineNameShort = NameShort FROM planning.tdProductLines WHERE FactoryKey = @SourceFactoryKey AND ProductLineID = @SourceProductLineID; SELECT @SourceProductKey = ProductKey, @SourceProductNameShort = NameShort FROM planning.tdProducts WHERE FactoryKey = @SourceFactoryKey AND ProductLineKey = @SourceProductLineKey AND ProductID = @SourceProductID; IF @SourceFactoryKey = 0 OR @SourceProductLineKey = 0 OR @SourceProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Source keys don`t exists', 16, 10); END; -- STEP 1.3 - Determine TargetKeys SELECT @TargetFactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @TargetFactoryID; SELECT @TargetProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @TargetFactoryKey AND ProductLineID = @TargetProductLineID; SELECT @TargetProductKey = ProductKey FROM planning.tdProducts WHERE FactoryKey = @TargetFactoryKey AND ProductLineKey = @TargetProductLineKey AND ProductID = @TargetProductID; IF @TargetFactoryKey = 0 OR @TargetProductLineKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Target keys don`t exists', 16, 10); END; IF @TargetProductKey <> 0 BEGIN SET @ResultCode = 403; RAISERROR('Target Product key already exists', 16, 10); END; -- STEP 1.4 - If ProductID * is submitted, search the next free numeric ProductID IF @TargetProductID = '*' BEGIN SELECT @TargetProductID = COALESCE(MAX(CAST(ProductID AS BIGINT)), 0) + 1 FROM planning.tdProducts WHERE ProductLineKey = @TargetProductLineKey AND TRY_CAST(ProductID AS BIGINT) IS NOT NULL; -- if it there is no others Products in ProductLine`s IF @TargetProductID = '*' SELECT @TargetProductID = 1; END -- STEP 3 - Copy data --dProduct DECLARE @TargetProductKeyTable AS TABLE (KeyNumber BIGINT NOT NULL); INSERT INTO planning.tdProducts ( ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,TimeType ,NameShort ,NameLong ,CommentUser ,CommentDev ,ResponsiblePerson ,ImageName ,Status ,Template ,TemplateVersion ,GlobalAttribute1 ,GlobalAttribute2 ,GlobalAttribute3 ,GlobalAttribute4 ,GlobalAttribute5 ,GlobalAttribute6 ,GlobalAttribute7 ,GlobalAttribute8 ,GlobalAttribute9 ,GlobalAttribute10 ,GlobalAttribute11 ,GlobalAttribute12 ,GlobalAttribute13 ,GlobalAttribute14 ,GlobalAttribute15 ,GlobalAttribute16 ,GlobalAttribute17 ,GlobalAttribute18 ,GlobalAttribute19 ,GlobalAttribute20 ,GlobalAttribute21 ,GlobalAttribute22 ,GlobalAttribute23 ,GlobalAttribute24 ,GlobalAttribute25 ) OUTPUT INSERTED.ProductKey INTO @TargetProductKeyTable SELECT @TargetProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , @TargetProductID AS ProductID , @TargetProductLineID AS ProductLineID , @TargetFactoryID AS FactoryID , TimeType , @TargetProductNameShort AS NameShort , NameLong , CommentUser , CommentDev , ResponsiblePerson , ImageName , Status , Template , TemplateVersion , GlobalAttribute1 , GlobalAttribute2 , GlobalAttribute3 , GlobalAttribute4 , GlobalAttribute5 , GlobalAttribute6 , GlobalAttribute7 , GlobalAttribute8 , GlobalAttribute9 , GlobalAttribute10, GlobalAttribute11, GlobalAttribute12, GlobalAttribute13, GlobalAttribute14 , GlobalAttribute15, GlobalAttribute16, GlobalAttribute17, GlobalAttribute18, GlobalAttribute19, GlobalAttribute20, GlobalAttribute21 , GlobalAttribute22, GlobalAttribute23, GlobalAttribute24, GlobalAttribute25 FROM planning.tdProducts WHERE ProductKey = @SourceProductKey; SET @EffectedRows += @@ROWCOUNT; SELECT TOP 1 @TargetProductKey = KeyNumber FROM @TargetProductKeyTable; --gProducts Inhalt INSERT INTO planning.tgProducts ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,PropertyID ,PropertyName ,CommentUser ,CommentDev ,Unit ,ValueText ,ValueInt ,Scale ,IsROSystemProperty ,FormatID ) SELECT @TargetProductKey AS ProductKey , @TargetProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , @TargetProductID AS ProductID , @TargetProductLineID AS ProductLineID , @TargetFactoryID AS FactoryID , PropertyID , PropertyName , CommentUser , CommentDev , Unit , ValueText , ValueInt , Scale , IsROSystemProperty , FormatID FROM planning.tgProducts WHERE ProductKey = @SourceProductKey; SET @EffectedRows += @@ROWCOUNT; --dValueSeries INSERT INTO planning.tdValueSeries ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,ValueSeriesNo ,NameShort ,NameLong ,CommentUser ,CommentDev ,ImageName ,IsNumeric ,VisibilityLevel ,ValueSource ,ValueListID ,ValueFormatID ,Unit ,Scale ,Effect ,EffectParameter ,AllowZero ) SELECT @TargetProductKey AS ProductKey , @TargetProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , @TargetProductID AS ProductID , @TargetProductLineID AS ProductLineID , @TargetFactoryID AS FactoryID , ValueSeriesID , ValueSeriesNo , NameShort , NameLong , CommentUser , CommentDev , ImageName , [IsNumeric] , VisibilityLevel , ValueSource , ValueListID , ValueFormatID , Unit , Scale , Effect , EffectParameter , AllowZero FROM planning.tdValueSeries WHERE ProductKey = @SourceProductKey; SET @EffectedRows += @@ROWCOUNT; --dTime INSERT INTO planning.tdTime ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,TimeID ,FormatID ) SELECT @TargetProductKey AS ProductKey , @TargetProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , @TargetProductID AS ProductID , @TargetProductLineID AS ProductLineID , @TargetFactoryID AS FactoryID , TimeID , FormatID FROM planning.tdTime WHERE ProductKey = @SourceProductKey; SET @EffectedRows += @@ROWCOUNT; --fValues INSERT INTO planning.tfValues ( ValueSeriesKey ,ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueText ,ValueComment ) SELECT VS.ValueSeriesKey AS ValueSeriesKey , @TargetProductKey AS ProductKey , @TargetProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , @TargetProductID AS ProductID , @TargetProductLineID AS ProductLineID , @TargetFactoryID AS FactoryID , fV.ValueSeriesID , TimeID , ValueFormula , ValueInt , ValueText , ValueComment FROM planning.tfValues fV LEFT JOIN ( SELECT ValueSeriesKey, ProductKey, ValueSeriesID FROM planning.tdValueSeries WHERE ProductKey = @TargetProductKey) VS ON VS.ValueSeriesID = fV.ValueSeriesID WHERE fV.ProductKey = @SourceProductKey; SET @EffectedRows += @@ROWCOUNT; --tFiles INSERT INTO planning.tFiles ( FactoryKey ,ProductLineKey ,ProductKey ,FactoryID ,ProductLineID ,ProductID ,FileID ,FileName ,FileExtension ,FileBody ,UploadedBy ,UploadedAt ,Filepath ) SELECT @TargetFactoryKey ,@TargetProductLineKey ,@TargetProductKey ,@TargetFactoryID AS FactoryID ,@TargetProductLineID AS ProductLineID ,@TargetProductID AS ProductID ,tF.FileID ,tF.FileName ,tF.FileExtension ,tF.FileBody ,tF.UploadedBy ,tF.UploadedAt ,tF.Filepath FROM planning.tFiles tF WHERE FactoryID = @SourceFactoryID AND ProductLineID = @SourceProductLineID AND ProductID = @SourceProductID; --fStatements CopyInformation speichern INSERT INTO planning.tfStatements ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ActionType ,Statement ,UserName ,PCName ,ProcessorCode ,IPAddresses ,Timestamp ,IsDeleted ,IsResolved ) SELECT @TargetProductKey AS ProductKey , @TargetProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , @TargetProductID AS ProductID , @TargetProductLineID AS ProductLineID , @TargetFactoryID AS FactoryID , N'COPY' AS ActionType , N'Product produced as a copy of Factory <' + @SourceFactoryID + N'>"' + @SourceFactoryNameShort + N'"/ ProductLine <' + @SourceProductLineID + N'>"' + @SourceProductLineNameShort + N'"/ Product <' + @SourceProductID + N'>"' + @SourceProductNameShort + N'"' AS Statement , @TransactUsername AS UserName , N'' AS PCName , N'' AS ProcessorCode , N'' AS IPAddresses , GETUTCDATE() AS Timestamp , 0 AS IsDeleted , 0 AS IsResolved; SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = 201; -- return created ProductID in output parameter SET @OutputID = @TargetProductID; COMMIT TRANSACTION sx_pf_COPY_Product; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_COPY_Product; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spCOPY_Product' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to COPY Operation for a Product'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceFactoryID'; SET @value = N'SourceFactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceProductLineID'; SET @value = N'SourceProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceProductID'; SET @value = N'SourceProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetFactoryID'; SET @value = N'TargetFactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetProductLineID'; SET @value = N'TargetProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetProductID'; SET @value = N'TargetProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetProductNameShort'; SET @value = N'TargetProductNameShort'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spCOPY_ProductLine; GO /* COPY Operation for a ProductLine Not yet existing ProductLine is copied when Source ProductLine is available Saxess Software GmbH Last modified 01/2024 for OCT 5.10 Test call 1. Empty source or target FactoryID / ProductLineID => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User has no Rights => 401 Unauthorized 4. Not exists source Factory / ProductLine => 404 Not Found 5. Not exists target Factory => 404 Not Found 6. Target ProductLine key already exists => 403 Forbidden 7. ProductLine copied => 201 Created DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@SourceFactoryID NVARCHAR(255) = 'ZT' ,@SourceProductLineID NVARCHAR(255) = 'ZT' ,@TargetFactoryID NVARCHAR(255) = 'ZT' ,@TargetProductLineID NVARCHAR(255) = 'ZT32' ,@TargetProductLineNameShort NVARCHAR(255) = 'Neue ProductLine32'; EXECUTE @RC = planning.spCOPY_ProductLine @Username ,@SourceFactoryID ,@SourceProductLineID ,@TargetFactoryID ,@TargetProductLineID ,@TargetProductLineNameShort; PRINT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spCOPY_ProductLine',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spCOPY_ProductLine','PARAMETER',NULL) */ CREATE PROCEDURE planning.spCOPY_ProductLine @Username NVARCHAR(255), @SourceFactoryID NVARCHAR(255), @SourceProductLineID NVARCHAR(255), @TargetFactoryID NVARCHAR(255), @TargetProductLineID NVARCHAR(255), @TargetProductLineNameShort NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @TransactUserKey INT = 0; DECLARE @SourceFactoryKey INT = 0; DECLARE @SourceProductLineKey INT = 0; DECLARE @SourceFactoryNameShort NVARCHAR(255) = N''; DECLARE @SourceProductLineNameShort NVARCHAR(255) = N''; DECLARE @SourceProductNameShort NVARCHAR(255) = N''; DECLARE @TargetFactoryKey INT = 0; DECLARE @TargetProductLineKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@SourceFactoryID, N'NULL') + N''',''' + ISNULL(@SourceProductLineID, N'NULL') + N''',''' + ISNULL(@TargetFactoryID, N'NULL') + N''',''' + ISNULL(@TargetProductLineID, N'NULL') + N''',''' + ISNULL(@TargetProductLineNameShort, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @SourceFactoryID IS NULL SET @SourceFactoryID = N''; IF @SourceProductLineID IS NULL SET @SourceProductLineID = N''; IF @TargetFactoryID IS NULL SET @TargetFactoryID = N''; IF @TargetProductLineID IS NULL SET @TargetProductLineID = N''; IF @TargetProductLineNameShort IS NULL SET @TargetProductLineNameShort = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_COPY_ProductLine; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @SourceFactoryID = system.fProtectID (@SourceFactoryID); SET @SourceProductLineID = system.fProtectID (@SourceProductLineID); SET @TargetFactoryID = system.fProtectID (@TargetFactoryID); SET @TargetProductLineID = system.fProtectID (@TargetProductLineID); SET @TargetProductLineNameShort = system.fProtectString (@TargetProductLineNameShort); IF @SourceFactoryID = N'' OR @SourceProductLineID = N'' OR @TargetFactoryID = N'' OR @TargetProductLineID = N'' OR @TargetProductLineNameShort = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername, @TargetFactoryID; IF @ResultCode <> 200 RAISERROR('Invalid rights on target', 16, 10); EXEC @ResultCode = planning.spGET_FactoryReadRight @TransactUsername, @SourceFactoryID; IF @ResultCode <> 200 RAISERROR('Invalid rights on source', 16, 10); -- STEP 1.2 - Determine SourceKeys SELECT @SourceFactoryKey = FactoryKey,@SourceFactoryNameShort = NameShort FROM planning.tdFactories WHERE FactoryID = @SourceFactoryID; SELECT @SourceProductLineKey = ProductLineKey,@SourceProductLineNameShort = NameShort FROM planning.tdProductLines WHERE FactoryKey = @SourceFactoryKey AND ProductLineID = @SourceProductLineID; IF @SourceFactoryKey = 0 OR @SourceProductLineKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Source keys don`t exists', 16, 10); END; -- STEP 1.3 - Determine TargetKeys SELECT @TargetFactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @TargetFactoryID; SELECT @TargetProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @TargetFactoryKey AND ProductLineID = @TargetProductLineID; IF @TargetFactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Target keys don`t exists', 16, 10); END; IF @TargetProductLineKey <> 0 BEGIN SET @ResultCode = 403; RAISERROR('Target ProductLine key already exists', 16, 10); END; -- STEP 3 - Copy data DECLARE @Keys AS TABLE ([Key] INT NOT NULL); --dProductLine INSERT INTO planning.tdProductLines ( FactoryKey ,ProductLineID ,FactoryID ,NameShort ,NameLong ,CommentUser ,CommentDev ,ResponsiblePerson ,ImageName ,DefaultTemplate ,GlobalAttributeSource1 ,GlobalAttributeAlias1 ,GlobalAttributeSource2 ,GlobalAttributeAlias2 ,GlobalAttributeSource3 ,GlobalAttributeAlias3 ,GlobalAttributeSource4 ,GlobalAttributeAlias4 ,GlobalAttributeSource5 ,GlobalAttributeAlias5 ,GlobalAttributeSource6 ,GlobalAttributeAlias6 ,GlobalAttributeSource7 ,GlobalAttributeAlias7 ,GlobalAttributeSource8 ,GlobalAttributeAlias8 ,GlobalAttributeSource9 ,GlobalAttributeAlias9 ,GlobalAttributeSource10 ,GlobalAttributeAlias10 ,GlobalAttributeSource11 ,GlobalAttributeAlias11 ,GlobalAttributeSource12 ,GlobalAttributeAlias12 ,GlobalAttributeSource13 ,GlobalAttributeAlias13 ,GlobalAttributeSource14 ,GlobalAttributeAlias14 ,GlobalAttributeSource15 ,GlobalAttributeAlias15 ,GlobalAttributeSource16 ,GlobalAttributeAlias16 ,GlobalAttributeSource17 ,GlobalAttributeAlias17 ,GlobalAttributeSource18 ,GlobalAttributeAlias18 ,GlobalAttributeSource19 ,GlobalAttributeAlias19 ,GlobalAttributeSource20 ,GlobalAttributeAlias20 ,GlobalAttributeSource21 ,GlobalAttributeAlias21 ,GlobalAttributeSource22 ,GlobalAttributeAlias22 ,GlobalAttributeSource23 ,GlobalAttributeAlias23 ,GlobalAttributeSource24 ,GlobalAttributeAlias24 ,GlobalAttributeSource25 ,GlobalAttributeAlias25 ,LayoutJSON ) OUTPUT INSERTED.ProductLineKey INTO @Keys([Key]) SELECT @TargetFactoryKey AS FactoryKey , @TargetProductLineID AS ProductLineID , @TargetFactoryID AS FactoryID , @TargetProductLineNameShort AS NameShort , NameLong , CommentUser , CommentDev , ResponsiblePerson , ImageName , DefaultTemplate , GlobalAttributeSource1, GlobalAttributeAlias1, GlobalAttributeSource2, GlobalAttributeAlias2, GlobalAttributeSource3, GlobalAttributeAlias3 , GlobalAttributeSource4, GlobalAttributeAlias4, GlobalAttributeSource5, GlobalAttributeAlias5, GlobalAttributeSource6, GlobalAttributeAlias6 , GlobalAttributeSource7, GlobalAttributeAlias7, GlobalAttributeSource8, GlobalAttributeAlias8, GlobalAttributeSource9, GlobalAttributeAlias9 , GlobalAttributeSource10, GlobalAttributeAlias10, GlobalAttributeSource11, GlobalAttributeAlias11, GlobalAttributeSource12, GlobalAttributeAlias12 , GlobalAttributeSource13, GlobalAttributeAlias13, GlobalAttributeSource14, GlobalAttributeAlias14, GlobalAttributeSource15, GlobalAttributeAlias15 , GlobalAttributeSource16, GlobalAttributeAlias16, GlobalAttributeSource17, GlobalAttributeAlias17, GlobalAttributeSource18, GlobalAttributeAlias18 , GlobalAttributeSource19, GlobalAttributeAlias19, GlobalAttributeSource20, GlobalAttributeAlias20, GlobalAttributeSource21, GlobalAttributeAlias21 , GlobalAttributeSource22, GlobalAttributeAlias22, GlobalAttributeSource23, GlobalAttributeAlias23, GlobalAttributeSource24, GlobalAttributeAlias24 , GlobalAttributeSource25, GlobalAttributeAlias25 , LayoutJSON FROM planning.tdProductLines WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; SELECT TOP 1 @TargetProductLineKey = [Key] FROM @Keys; --dProduct INSERT INTO planning.tdProducts ( ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,TimeType ,NameShort ,NameLong ,CommentUser ,CommentDev ,ResponsiblePerson ,ImageName ,Status ,Template ,TemplateVersion ,GlobalAttribute1 ,GlobalAttribute2 ,GlobalAttribute3 ,GlobalAttribute4 ,GlobalAttribute5 ,GlobalAttribute6 ,GlobalAttribute7 ,GlobalAttribute8 ,GlobalAttribute9 ,GlobalAttribute10 ,GlobalAttribute11 ,GlobalAttribute12 ,GlobalAttribute13 ,GlobalAttribute14 ,GlobalAttribute15 ,GlobalAttribute16 ,GlobalAttribute17 ,GlobalAttribute18 ,GlobalAttribute19 ,GlobalAttribute20 ,GlobalAttribute21 ,GlobalAttribute22 ,GlobalAttribute23 ,GlobalAttribute24 ,GlobalAttribute25 ) SELECT @TargetProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , ProductID , @TargetProductLineID AS ProductLineID , @TargetFactoryID AS FactoryID , TimeType , NameShort , NameLong , CommentUser , CommentDev , ResponsiblePerson , ImageName , Status , Template , TemplateVersion , GlobalAttribute1, GlobalAttribute2, GlobalAttribute3, GlobalAttribute4, GlobalAttribute5, GlobalAttribute6, GlobalAttribute7 , GlobalAttribute8, GlobalAttribute9, GlobalAttribute10, GlobalAttribute11, GlobalAttribute12, GlobalAttribute13, GlobalAttribute14 , GlobalAttribute15, GlobalAttribute16, GlobalAttribute17, GlobalAttribute18, GlobalAttribute19, GlobalAttribute20, GlobalAttribute21 , GlobalAttribute22, GlobalAttribute23, GlobalAttribute24, GlobalAttribute25 FROM planning.tdProducts WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; --dValueSeries INSERT INTO planning.tdValueSeries ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,ValueSeriesNo ,NameShort ,NameLong ,CommentUser ,CommentDev ,ImageName ,IsNumeric ,VisibilityLevel ,ValueSource ,ValueListID ,ValueFormatID ,Unit ,Scale ,Effect ,EffectParameter ,AllowZero ) SELECT P.ProductKey AS ProductKey , @TargetProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , dVS.ProductID , @TargetProductLineID AS ProductLineID , @TargetFactoryID AS FactoryID , ValueSeriesID , ValueSeriesNo , NameShort , NameLong , CommentUser , CommentDev , ImageName , [IsNumeric] , VisibilityLevel , ValueSource , ValueListID , ValueFormatID , Unit , Scale , Effect , EffectParameter , AllowZero FROM planning.tdValueSeries dVS LEFT JOIN ( SELECT ProductKey, ProductID FROM planning.tdProducts WHERE ProductLineKey = @TargetProductLineKey) P ON P.ProductID = dVS.ProductID WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; --dTime INSERT INTO planning.tdTime ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,TimeID ,FormatID ) SELECT P.ProductKey AS ProductKey , @TargetProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , dT.ProductID , @TargetProductLineID AS ProductLineID , @TargetFactoryID AS FactoryID , TimeID , FormatID FROM planning.tdTime dT LEFT JOIN ( SELECT ProductKey, ProductID FROM planning.tdProducts WHERE ProductLineKey = @TargetProductLineKey) P ON P.ProductID = dT.ProductID WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; --fValues INSERT INTO planning.tfValues ( ValueSeriesKey ,ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueText ,ValueComment ) SELECT VS.ValueSeriesKey AS ValueSeriesKey , VS.ProductKey AS ProductKey , @TargetProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , fV.ProductID , @TargetProductLineID AS ProductLineID , @TargetFactoryID AS FactoryID , fV.ValueSeriesID , TimeID , ValueFormula , ValueInt , ValueText , ValueComment FROM planning.tfValues fV LEFT JOIN ( SELECT ValueSeriesKey, ProductKey, ValueSeriesID, ProductID FROM planning.tdValueSeries WHERE ProductLineKey = @TargetProductLineKey) VS ON fV.ValueSeriesID=VS.ValueSeriesID AND fV.ProductID = VS.ProductID WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; --gProductLines INSERT INTO planning.tgProductLines ( ProductLineKey ,FactoryKey ,ProductLineID ,FactoryID ,PropertyID ,PropertyName ,CommentUser ,CommentDev ,Unit ,ValueText ,ValueInt ,Scale ,IsROSystemProperty ,FormatID ) SELECT @TargetProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , @TargetProductLineID AS ProductLineID , @TargetFactoryID AS FactoryID , PropertyID , PropertyName , CommentUser , CommentDev , Unit , ValueText , ValueInt , Scale , IsROSystemProperty , FormatID FROM planning.tgProductLines WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; --gProducts INSERT INTO planning.tgProducts ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,PropertyID ,PropertyName ,CommentUser ,CommentDev ,Unit ,ValueText ,ValueInt ,Scale ,IsROSystemProperty ,FormatID ) SELECT P.ProductKey AS ProductKey , @TargetProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , gP.ProductID , @TargetProductLineID AS ProductLineID , @TargetFactoryID AS FactoryID , PropertyID , PropertyName , CommentUser , CommentDev , Unit , ValueText , ValueInt , Scale , IsROSystemProperty , FormatID FROM planning.tgProducts gP LEFT JOIN ( SELECT ProductKey, ProductID FROM planning.tdProducts WHERE ProductLineKey = @TargetProductLineKey) P ON P.ProductID = gP.ProductID WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; --rRights - no definition, write Rights on Factory will lead to implicit Write Rights for creator --tFiles INSERT INTO planning.tFiles ( FactoryKey ,ProductLineKey ,ProductKey ,FactoryID ,ProductLineID ,ProductID ,FileID ,FileName ,FileExtension ,FileBody ,UploadedBy ,UploadedAt ,Filepath ) SELECT @TargetFactoryKey ,@TargetProductLineKey ,P.ProductKey ,@TargetFactoryID AS FactoryID ,@TargetProductLineID AS ProductLineID ,tF.ProductID ,tF.FileID ,tF.FileName ,tF.FileExtension ,tF.FileBody ,tF.UploadedBy ,tF.UploadedAt ,tF.Filepath FROM planning.tFiles tF LEFT JOIN ( SELECT ProductKey, ProductID FROM planning.tdProducts WHERE ProductLineKey = @TargetProductLineKey) P ON P.ProductID = tF.ProductID WHERE FactoryID = @SourceFactoryID AND ProductLineID = @SourceProductLineID; SET @EffectedRows += @@ROWCOUNT; --tTabs INSERT INTO planning.tTabs ( FactoryKey ,ProductLineKey ,ProductKey ,FactoryID ,ProductLineID ,ProductID ,TabID ,Orderindex ,TabName ,TabHint ,TabText ,PresentationCODE ,Datasource ,IsVisibleBOOL ,ParameterJSON ,SQLCommand ) SELECT @TargetFactoryKey ,@TargetProductLineKey ,COALESCE(P.ProductKey,-1) AS ProductKey ,@TargetFactoryID AS FactoryID ,@TargetProductLineID AS ProductLineID ,COALESCE(tT.ProductID,'') AS ProductID ,tT.TabID ,tT.Orderindex ,tT.TabName ,tT.TabHint ,tT.TabText ,tT.PresentationCODE ,tT.Datasource ,tT.IsVisibleBOOL ,tT.ParameterJSON ,tT.SQLCommand FROM planning.tTabs tT LEFT JOIN ( SELECT ProductKey, ProductID FROM planning.tdProducts WHERE ProductLineKey = @TargetProductLineKey ) P ON COALESCE(P.ProductID,'') = tT.ProductID WHERE FactoryID = @SourceFactoryID AND ProductLineID = @SourceProductLineID; SET @EffectedRows += @@ROWCOUNT; -- tTabLayouts INSERT INTO planning.tTabLayouts ( FactoryID ,ProductLineID ,TabID ,LayoutID ,LayoutName ,LayoutJSON ,OrderIndex ,LayoutOwner ) SELECT @TargetFactoryID , @TargetProductLineID , TabID , LayoutID , LayoutName , LayoutJSON , OrderIndex , LayoutOwner FROM planning.tTabLayouts WHERE FactoryID = @SourceFactoryID AND ProductLineID = @SourceProductLineID; SET @EffectedRows += @@ROWCOUNT; --fStatements CopyInformation INSERT INTO planning.tfStatements ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ActionType ,Statement ,UserName ,PCName ,ProcessorCode ,IPAddresses ,Timestamp ,IsDeleted ,IsResolved ) SELECT ProductKey , @TargetProductLineKey AS ProductLineKey , @TargetFactoryKey AS FactoryKey , ProductID , @TargetProductLineID AS ProductLineID , @TargetFactoryID AS FactoryID , N'COPY' AS ActionType , N'Product produced as a copy of Factory <' + @SourceFactoryID + N'>"' + @SourceFactoryNameShort + N'"/ ProductLine <' + @SourceProductLineID + N'>"' + @SourceProductLineNameShort + N'"/ Product <' + ProductID + N'>"' + NameShort + N'"' AS Statement , @TransactUsername AS UserName , N'' AS PCName , N'' AS ProcessorCode , N'' AS IPAddresses , GETUTCDATE() AS Timestamp , 0 AS IsDeleted , 0 AS IsResolved FROM planning.tdProducts WHERE ProductLineKey = @TargetProductLineKey; SET @EffectedRows += @@ROWCOUNT; -- Rights materialization EXEC system.spMATERIALIZE_trUserRights; SET @ResultCode = 201; COMMIT TRANSACTION sx_pf_COPY_ProductLine; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_COPY_ProductLine; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spCOPY_ProductLine'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to COPY Operation for a ProductLine'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceFactoryID'; SET @value = N'SourceFactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceProductLineID'; SET @value = N'SourceProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetFactoryID'; SET @value = N'TargetFactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetProductLineID'; SET @value = N'TargetProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetProductLineNameShort'; SET @value = N'TargetProductLineNameShort'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_ClusterProperty; GO /* DELETE Operation for a ClusterProperty Deletes a Cluster Property. Gerd Tautenhahn for Saxess Software GmbH Last modified: 09/2022 for OCT 5.8 Test call 1. PropertyID is empty => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User does not have write access to the cluster => 401 Unauthorized 4. Property deleted => 200 OK 5. PropertyID not found => 204 No Content DECLARE @RC INT DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @PropertyID NVARCHAR(255) = 'rre' EXECUTE @RC = planning.spDELETE_ClusterProperty @Username, @PropertyID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_ClusterProperty',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_ClusterProperty','PARAMETER',NULL) */ CREATE PROCEDURE planning.spDELETE_ClusterProperty @Username NVARCHAR(255), @PropertyID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@PropertyID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N'' -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @PropertyID IS NULL SET @PropertyID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_DELETE_ClusterProperty; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @PropertyID = system.fProtectID (@PropertyID); IF @PropertyID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 3 - Delete DELETE FROM planning.tgCluster WHERE PropertyID = @PropertyID; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = IIF(@EffectedRows = 0, 204, 200); COMMIT TRANSACTION sx_pf_DELETE_ClusterProperty; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_DELETE_ClusterProperty; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_ClusterProperty'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Deletes a Cluster Property.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PropertyID'; SET @value = N'PropertyID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_Factory; GO /* DELETE Operation for a Factory Deletes an entire Factory (include ProductLines, Products, Values) Saxess Software GmbH Last modified: 09/2023 for OCT 5.10 Test call 1. FactoryID is empty => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User does not have write access to the cluster => 401 Unauthorized 4. Factory deleted => 200 OK 5. FactoryID not found => 204 No Content DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@FactoryID NVARCHAR(255) = 'Test'; EXECUTE @RC = planning.spDELETE_Factory @Username, @FactoryID; PRINT @RC; SELECT TOP 10 * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_Factory',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_Factory','PARAMETER',NULL) */ CREATE PROCEDURE planning.spDELETE_Factory @Username NVARCHAR(255) ,@FactoryID NVARCHAR(255) WITH EXECUTE AS 'dbo' AS BEGIN DECLARE @FactoryKey INT = 0 ,@TransactUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N''; -- NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryKey IS NULL SET @FactoryKey = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_DELETE_Factory; -- Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); IF @FactoryID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; IF @FactoryKey = 0 BEGIN SET @ResultCode = 204; END ELSE BEGIN -- Delete Factory items from all tables DELETE FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tdProducts WHERE FactoryKey = @FactoryKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tdValueSeries WHERE FactoryKey = @FactoryKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tdTime WHERE FactoryKey = @FactoryKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tfStatements WHERE FactoryKey = @FactoryKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tfValues WHERE FactoryKey = @FactoryKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tgFactories WHERE FactoryKey = @FactoryKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tgProductLines WHERE FactoryKey = @FactoryKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tgProducts WHERE FactoryKey = @FactoryKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tdFactories WHERE FactoryKey = @FactoryKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tFiles WHERE FactoryKey = @FactoryKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tTabs WHERE FactoryKey = @FactoryKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tTabLayouts WHERE FactoryID = @FactoryID; SET @EffectedRows += @@ROWCOUNT; -- Last, because of Right materialization DELETE FROM system.trRights --therefore EXECUTE AS dbo needed WHERE FactoryID = @FactoryID; SET @EffectedRows += @@ROWCOUNT; END; COMMIT TRANSACTION sx_pf_DELETE_Factory; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_DELETE_Factory; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_Factory'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N''; -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Deletes an entire Factory (including ProductLines, Products, Values)'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_FactoryProperty; GO /* DELETE Operation for a FactoryProperty Deletes an entire Factory Property Procedure for documentation demo Gerd Tautenhahn for Saxess Software GmbH Last modified: 09/2022 for OCT 5.8 Test call 1. PropertyID or FactoryID is empty => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User does not have write access to the Factory => 401 Unauthorized 4. Property deleted => 200 OK 5. PropertyID not found => 204 No Content DECLARE @RC INT DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @FactoryID NVARCHAR(255) = 'ZT' DECLARE @PropertyID NVARCHAR(255) = 'rre' EXECUTE @RC = planning.spDELETE_FactoryProperty @Username, @FactoryID, @PropertyID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_FactoryProperty',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_FactoryProperty','PARAMETER',NULL) */ CREATE PROCEDURE planning.spDELETE_FactoryProperty @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @PropertyID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @PropertyKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@PropertyID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N'' -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryKey IS NULL SET @FactoryKey = N''; IF @PropertyID IS NULL SET @PropertyID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_DELETE_FactoryProperty; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @PropertyID = system.fProtectID (@PropertyID); IF @FactoryID = N'' OR @PropertyID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername, @FactoryID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @PropertyKey = PropertyKey FROM planning.tgFactories WHERE FactoryKey = @FactoryKey AND PropertyID = @PropertyID; IF @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 3 - Delete DELETE FROM planning.tgFactories WHERE PropertyKey = @PropertyKey; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = IIF(@EffectedRows = 0, 204, 200); COMMIT TRANSACTION sx_pf_DELETE_FactoryProperty; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_DELETE_FactoryProperty; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_FactoryProperty'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Deletes an entire Factory Property'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PropertyID'; SET @value = N'PropertyID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_Format; GO /* DELETE Operation for a Format Gerd Tautenhahn for Saxess Software GmbH Last modified: 09/2022 for OCT 5.8 Test call 1. FormatID is empty => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User does not have write access to the cluster => 401 Unauthorized 4. Format deleted => 200 OK 5. Format not found => 204 No Content DECLARE @RC INT DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @FormatID NVARCHAR(255) = '510' EXECUTE @RC = planning.spDELETE_Format @Username, @FormatID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_Format',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_Format','PARAMETER',NULL) */ CREATE PROCEDURE planning.spDELETE_Format @Username NVARCHAR(255), @FormatID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FormatID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N'' -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FormatID IS NULL SET @FormatID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_DELETE_Format; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FormatID = system.fProtectID (@FormatID); IF @FormatID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 3 - Delete values DELETE FROM planning.thFormats WHERE FormatID = @FormatID; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = IIF(@EffectedRows = 0, 204, 200); COMMIT TRANSACTION sx_pf_DELETE_Format; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_DELETE_Format; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_Format' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'DELETE Operation for a Format'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FormatID'; SET @value = N'FormatID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_List; GO /* Procedure to DELETE a list Saxess Software GmbH Testcall DECLARE @RC INT; EXEC @RC = planning.spDELETE_List @Username = 'SQL', @ListID = 'suJaNein' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_List', NULL, NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_List', 'PARAMETER', NULL) */ CREATE PROCEDURE planning.spDELETE_List @Username NVARCHAR(255), @ListID NVARCHAR(255) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 2, @Username, @ListID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; BEGIN TRANSACTION sx_pf_DELETE_List -- Input parameter handling SET @ListID = COALESCE(system.fProtectID(@ListID), N''); EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 BEGIN EXEC system.spSEND_Message 'ERROR', 'Invalid rights'; END IF @ListID IN ('sxUnits', 'sxStatus', 'sxValueEffectParameters') BEGIN EXEC system.spSEND_Message 'ERROR', 'System lists can''t be deleted.' END DELETE FROM planning.thListValues WHERE ListID = @ListID; SET @AffectedRows = @@ROWCOUNT; DELETE FROM planning.thLists WHERE ListID = @ListID; SET @AffectedRows += @@ROWCOUNT; IF @AffectedRows = 0 EXEC system.spSEND_Message 'ERROR', 'List doesn''t exist'; COMMIT TRANSACTION sx_pf_DELETE_List SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_DELETE_List END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_List' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'DELETE Operation for a List'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ListID'; SET @value = N'ListID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_ListValue; GO /* DELETE Operation for one ListValue Operates over Key, as the ListValue has no ID Gerd Tautenhahn for Saxess Software GmbH Last modified: 09/2022 for OCT 5.8 Test call 1. ListID is empty => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User does not have write access to the cluster => 401 Unauthorized 4. ListValue deleted => 200 OK 5. ListValue not found => 204 No Content DECLARE @RC INT DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @ListValueKey INT = 18 EXECUTE @RC = planning.spDELETE_ListValue @Username, @ListValueKey PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_ListValue',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_ListValue','PARAMETER',NULL) */ CREATE PROCEDURE planning.spDELETE_ListValue @Username NVARCHAR(255), @ListValueKey INT AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(CAST(@ListValueKey AS NVARCHAR(255)), N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N'' -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ListValueKey IS NULL SET @ListValueKey = 0; BEGIN TRY BEGIN TRANSACTION sx_pf_DELETE_ListValue; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 3 - Delete values DELETE FROM planning.thListValues WHERE ListValueKey = @ListValueKey; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = IIF(@EffectedRows = 0, 204, 200); COMMIT TRANSACTION sx_pf_DELETE_ListValue; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_DELETE_ListValue; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_ListValue'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Operates over Key, as the ListValue has no ID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ListValueKey'; SET @value = N'ListValueKey'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_Product; GO /* DELETE Operation for a Product Deletes a product and all Values and Attributes Gerd Tautenhahn for Saxess Software GmbH Last modified: 09/2022 for OCT 5.8 Test call 1. ProductID is empty => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User does not have write access to ProductLine => 401 Unauthorized 4. Product not found => 204 No Content 5. Product deleted => 200 OK DECLARE @RC INT DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @FactoryID NVARCHAR(255) = 'ZT' DECLARE @ProductLineID NVARCHAR(255) = 'U' DECLARE @ProductID NVARCHAR(255) = '10' EXECUTE @RC = planning.spDELETE_Product @Username, @FactoryID, @ProductLineID, @ProductID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_Product',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_Product','PARAMETER',NULL) */ CREATE PROCEDURE planning.spDELETE_Product @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @ProductID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N'' -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_DELETE_Product; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); IF @ProductID = N'' OR @ProductLineID = N'' OR @FactoryID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE ProductLineID = @ProductLineID AND FactoryKey = @FactoryKey; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductID = @ProductID AND ProductLineKey = @ProductLineKey; IF @ProductLineKey = 0 OR @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 3 - Delete values DELETE FROM planning.tdValueSeries WHERE ProductKey = @ProductKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tdTime WHERE ProductKey = @ProductKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tfStatements WHERE ProductKey = @ProductKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tfValues WHERE ProductKey = @ProductKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tgProducts WHERE ProductKey = @ProductKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tdProducts WHERE ProductKey = @ProductKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tFiles WHERE ProductKey = @ProductKey; SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = IIF(@EffectedRows = 0, 204, 200); COMMIT TRANSACTION sx_pf_DELETE_Product; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_DELETE_Product; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_Product' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Deletes a product and all Values and Attributes'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_ProductLine; GO /* DELETE Operation for a ProductLine Deletes an entire Product Line (Products, Values, etc.) Saxess Software GmbH Last modified: 09/2023 for OCT 5.10 Test call 1. ProductLineID is empty => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User does not have write access to ProductLine => 401 Unauthorized 4. ProductLine not found => 204 No Content 5. ProductLine deleted => 200 OK DECLARE @RC INT DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @FactoryID NVARCHAR(255) = 'ZT' DECLARE @ProductLineID NVARCHAR(255) = 'V' EXECUTE @RC = planning.spDELETE_ProductLine @Username, @FactoryID, @ProductLineID PRINT @RC SELECT TOP 10 * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_ProductLine',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_ProductLine','PARAMETER',NULL) */ CREATE PROCEDURE planning.spDELETE_ProductLine @Username NVARCHAR(255) ,@FactoryID NVARCHAR(255) ,@ProductLineID NVARCHAR(255) WITH EXECUTE AS 'dbo' AS BEGIN DECLARE @FactoryKey INT = 0 ,@ProductLineKey INT = 0 ,@TransactUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N''; -- NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_DELETE_ProductLine; -- Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); IF @ProductLineID = N'' OR @FactoryID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Check rights EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername, @FactoryID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- Factory und ProductLineKey ermitteln SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE ProductLineID = @ProductLineID AND FactoryKey = @FactoryKey; IF @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- Delete values DELETE FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tdValueSeries WHERE ProductLineKey = @ProductLineKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tdTime WHERE ProductLineKey = @ProductLineKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tfStatements WHERE ProductLineKey = @ProductLineKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tfValues WHERE ProductLineKey = @ProductLineKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tgProductLines WHERE ProductLineKey = @ProductLineKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tgProducts WHERE ProductLineKey = @ProductLineKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tFiles WHERE ProductLineKey = @ProductLineKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tTabs WHERE ProductLineKey = @ProductLineKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tTabLayouts WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID; SET @EffectedRows += @@ROWCOUNT; -- last because of matrialization DELETE FROM system.trRights -- this needs EXECUTE AS dbo WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID; SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = IIF(@EffectedRows = 0, 204, 200); COMMIT TRANSACTION sx_pf_DELETE_ProductLine; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_DELETE_ProductLine; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_ProductLine'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N''; -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Deletes an entire Product Line (Products, Values, etc.)'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_ProductLineProperty; GO /* DELETE Operation for a ProductLineProperty Delete a ProductLineProperty Gerd Tautenhahn for Saxess Software GmbH Last modified: 09/2022 for OCT 5.8 Test call 1. PropertyID is empty => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User does not have write access to ProductLine => 401 Unauthorized 4. Property not found => 204 No Content 5. Property deleted => 200 OK DECLARE @RC INT DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @FactoryID NVARCHAR(255) = 'ZT' DECLARE @ProductLineID NVARCHAR(255) = 'U' DECLARE @PropertyID NVARCHAR(255) = 'test' EXECUTE @RC = planning.spDELETE_ProductLineProperty @Username, @FactoryID, @ProductLineID, @PropertyID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_ProductLineProperty',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_ProductLineProperty','PARAMETER',NULL) */ CREATE PROCEDURE planning.spDELETE_ProductLineProperty @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @PropertyID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @PropertyKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@PropertyID, N'NULL') + ''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N'' -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @PropertyID IS NULL SET @PropertyID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_DELETE_ProductLineProperty; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @PropertyID = system.fProtectID (@PropertyID); IF @PropertyID = N'' OR @ProductLineID = N'' OR @FactoryID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE ProductLineID = @ProductLineID AND FactoryKey = @FactoryKey; SELECT @PropertyKey = PropertyKey FROM planning.tgProductLines WHERE ProductLineKey = @ProductLineKey AND FactoryKey = @FactoryKey AND PropertyID = @PropertyID; IF @ProductLineKey = 0 OR @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 3 - Delete values DELETE FROM planning.tgProductLines WHERE PropertyKey = @PropertyKey; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = IIF(@EffectedRows = 0, 204, 200); COMMIT TRANSACTION sx_pf_DELETE_ProductLineProperty; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_DELETE_ProductLineProperty; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_ProductLineProperty'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'DELETE Operation for a ProductLineProperty'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PropertyID'; SET @value = N'PropertyID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_ProductProperty; GO /* DELETE Operation for a ProductProperty Delete a ProductProperty Gerd Tautenhahn for Saxess Software GmbH Last modified: 09/2022 for OCT 5.8 Test call 1. PropertyID is empty => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User does not have write access to ProductLine => 401 Unauthorized 4. Property not found => 204 No Content 5. Property deleted => 200 OK DECLARE @RC INT DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @FactoryID NVARCHAR(255) = 'ZT' DECLARE @ProductLineID NVARCHAR(255) = 'U' DECLARE @ProductID NVARCHAR(255) = '10' DECLARE @PropertyID NVARCHAR(255) = 'test' EXECUTE @RC = planning.spDELETE_ProductProperty @Username, @FactoryID, @ProductLineID, @ProductID, @PropertyID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_ProductProperty',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_ProductProperty','PARAMETER',NULL) */ CREATE PROCEDURE planning.spDELETE_ProductProperty @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @ProductID NVARCHAR(255), @PropertyID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @PropertyKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''',''' + ISNULL(@PropertyID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N'' -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @PropertyID IS NULL SET @PropertyID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_DELETE_ProductProperty; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); SET @PropertyID = system.fProtectID (@PropertyID); IF @PropertyID = N'' OR @ProductID = N'' OR @ProductLineID = N'' OR @FactoryID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE ProductLineID = @ProductLineID AND FactoryKey = @FactoryKey; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductID = @ProductID AND ProductLineKey = @ProductLineKey AND FactoryKey = @FactoryKey; SELECT @PropertyKey = PropertyKey FROM planning.tgProducts WHERE ProductKey = @ProductKey AND ProductLineKey = @ProductLineKey AND FactoryKey = @FactoryKey AND PropertyID = @PropertyID; IF @ProductKey = 0 OR @ProductLineKey = 0 OR @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 3 - Delete values DELETE FROM planning.tgProducts WHERE PropertyKey = @PropertyKey; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = IIF(@EffectedRows = 0, 204, 200); COMMIT TRANSACTION sx_pf_DELETE_ProductProperty; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_DELETE_ProductProperty; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_ProductProperty'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'DELETE Operation for a ProductProperty'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PropertyID'; SET @value = N'PropertyID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_TimeID; GO /* DELETE Operation for a TimeID Deletes a TimeID Gerd Tautenhahn for Saxess Software GmbH Last modified: 09/2022 for OCT 5.8 Test call 1. Empty FactoryID / ProductLineID / ProductID / TimeID = 0 => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User does not have write access to ProductLine => 401 Unauthorized 4. Value of TimeID deleted => 200 OK 5. There is none ProductID => 204 No Content DECLARE @RC INT DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @FactoryID NVARCHAR(255) = 'ZT' DECLARE @ProductLineID NVARCHAR(255) = 'U' DECLARE @ProductID NVARCHAR(255) = '10' DECLARE @TimeID INT = '20180515' EXECUTE @RC = planning.spDELETE_TimeID @Username, @FactoryID, @ProductLineID, @ProductID, @TimeID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_TimeID',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_TimeID','PARAMETER',NULL) */ CREATE PROCEDURE planning.spDELETE_TimeID @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @ProductID NVARCHAR(255), @TimeID INT AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''',' + ISNULL(CAST(@TimeID AS NVARCHAR(255)), N'NULL'); DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N'' -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @TimeID IS NULL SET @TimeID = 0; BEGIN TRY BEGIN TRANSACTION sx_pf_DELETE_TimeID; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); IF @ProductID = N'' OR @ProductLineID = N'' OR @FactoryID = N'' OR @TimeID = 0 BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE ProductLineID = @ProductLineID AND FactoryKey = @FactoryKey; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductID = @ProductID AND ProductLineKey = @ProductLineKey; IF @ProductKey = 0 OR @ProductLineKey = 0 OR @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 3.1 - Delete values DELETE FROM planning.tfValues WHERE ProductKey = @ProductKey AND TimeID = @TimeID; SET @EffectedRows = @@ROWCOUNT; -- STEP 3.2 - Delete Timeline DELETE FROM planning.tdTime WHERE ProductKey = @ProductKey AND TimeID = @TimeID; SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = IIF(@EffectedRows = 0, 204, 200); COMMIT TRANSACTION sx_pf_DELETE_TimeID; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_DELETE_TimeID; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_TimeID' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'DELETE Operation for a TimeID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TimeID'; SET @value = N'TimeID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_Value; GO /* DELETE Operation for a Value Delete a Value of ValueSeries with specified TimeID Gerd Tautenhahn for Saxess Software GmbH Last modified: 09/2022 for OCT 5.8 Test call 1. All IDs are empty or TimeID is 0 => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User does not have write access to ProductLine => 401 Unauthorized 4. Value not found => 204 No Content 5. Value deleted => 200 OK DECLARE @RC INT DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @FactoryID NVARCHAR(255) = 'ZT' DECLARE @ProductLineID NVARCHAR(255) = 'U' DECLARE @ProductID NVARCHAR(255) = '10' DECLARE @ValueSerieID NVARCHAR(255) = 'K1' DECLARE @TimeID INT = 20180615 EXECUTE @RC = planning.spDELETE_Value @Username, @FactoryID, @ProductLineID, @ProductID, @ValueSerieID, @TimeID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_Value',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_Value','PARAMETER',NULL) */ CREATE PROCEDURE planning.spDELETE_Value @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @ProductID NVARCHAR(255), @ValueSerieID NVARCHAR(255), @TimeID INT AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @ValueSerieKey INT = 0; DECLARE @ValueKey BIGINT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''',''' + ISNULL(@ValueSerieID, N'NULL') + N''',' + ISNULL(CAST(@TimeID AS NVARCHAR(255)), N'NULL'); DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N'' -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @ValueSerieID IS NULL SET @ValueSerieID = N''; IF @TimeID IS NULL SET @TimeID = 0; BEGIN TRY BEGIN TRANSACTION sx_pf_DELETE_Value; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @ValueSerieID = system.fProtectID (@ValueSerieID); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); IF @ValueSerieID = N'' OR @ProductID = N'' OR @ProductLineID = N'' OR @FactoryID = N'' OR @TimeID = 0 BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE ProductLineID = @ProductLineID AND FactoryKey = @FactoryKey; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductID = @ProductID AND ProductLineKey = @ProductLineKey; SELECT @ValueSerieKey = ValueSeriesKey FROM planning.tdValueSeries WHERE ValueSeriesID = @ValueSerieID AND ProductKey = @ProductKey; SELECT @ValueKey = ValueKey FROM planning.tfValues WHERE ValueSeriesKey = @ValueSerieKey AND TimeID = @TimeID; IF @ProductKey = 0 OR @ProductLineKey = 0 OR @FactoryKey = 0 OR @ValueSerieKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 3 - Delete values DELETE FROM planning.tfValues WHERE ValueKey = @ValueKey; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = IIF(@EffectedRows = 0, 204, 200); COMMIT TRANSACTION sx_pf_DELETE_Value; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_DELETE_Value; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_Value' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Delete a Value of ValueSeries with specified TimeID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueSerieID'; SET @value = N'ValueSerieID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TimeID'; SET @value = N'TimeID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_Values; GO /* DELETE Operation for Values Delete all Values of Value Series of Products Gerd Tautenhahn for Saxess Software GmbH Last modified: 09/2022 for OCT 5.8 Test call 1. All IDs are empty => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User does not have write access to ProductLine => 401 Unauthorized 4. Values not found => 204 No Content 5. Values deleted => 200 OK DECLARE @RC INT DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @FactoryID NVARCHAR(255) = 'ZT' DECLARE @ProductLineID NVARCHAR(255) = 'U' DECLARE @ProductID NVARCHAR(255) = '10' DECLARE @ValueSerieID NVARCHAR(255) = 'K1' EXECUTE @RC = planning.spDELETE_Values @Username, @FactoryID, @ProductLineID, @ProductID, @ValueSerieID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_Values',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_Values','PARAMETER',NULL) */ CREATE PROCEDURE planning.spDELETE_Values @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @ProductID NVARCHAR(255), @ValueSerieID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @ValueSerieKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''',''' + ISNULL(@ValueSerieID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N'' -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @ValueSerieID IS NULL SET @ValueSerieID = N''; BEGIN TRY BEGIN TRANSACTION spDELETE_Values; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @ValueSerieID = system.fProtectID (@ValueSerieID); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); IF @ValueSerieID = N'' OR @ProductID = N'' OR @ProductLineID = N'' OR @FactoryID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername(@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE ProductLineID = @ProductLineID AND FactoryKey = @FactoryKey; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductID = @ProductID AND ProductLineKey = @ProductLineKey; SELECT @ValueSerieKey = ValueSeriesKey FROM planning.tdValueSeries WHERE ValueSeriesID = @ValueSerieID AND ProductKey = @ProductKey; IF @ProductKey = 0 OR @ProductLineKey = 0 OR @FactoryKey = 0 OR @ValueSerieKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 3 - Delete values DELETE FROM planning.tfValues WHERE ValueSeriesKey = @ValueSerieKey; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = IIF (@EffectedRows = 0, 204, 200); COMMIT TRANSACTION spDELETE_Values; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spDELETE_Values; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO ALTER AUTHORIZATION ON OBJECT ::planning.spDELETE_Values TO db_octservice; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_Values' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Delete all Values of Value Series of Products'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueSerieID'; SET @value = N'ValueSerieID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_ValueSerie; GO /* DELETE Operation for one ValueSerie Deletes a ValueSerie and changes the Numbers of the other ValueSeries. If Series No 1 is delete, Procedure creates Placeholder in a new ValueSeries 1. Gerd Tautenhahn for Saxess Software GmbH Last modified: 09/2022 for OCT 5.8 Test call 1. ValueSerieID is empty => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User does not have write access to ProductLine => 401 Unauthorized 4. ValueSerie not found => 204 No Content 5. ValueSerie deleted => 200 OK DECLARE @RC INT DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @FactoryID NVARCHAR(255) = 'ZT' DECLARE @ProductLineID NVARCHAR(255) = 'U' DECLARE @ProductID NVARCHAR(255) = '10' DECLARE @ValueSerieID NVARCHAR(255) = 'K1' EXECUTE @RC = planning.spDELETE_ValueSerie @Username, @FactoryID, @ProductLineID, @ProductID, @ValueSerieID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_ValueSerie',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_ValueSerie','PARAMETER',NULL) */ CREATE PROCEDURE planning.spDELETE_ValueSerie @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @ProductID NVARCHAR(255), @ValueSerieID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @DelValueSerieKey INT = 0; DECLARE @DelValueSerieNo INT = 0; DECLARE @NewFirstValueSerieKey INT = 0; DECLARE @NewFirstValueSerieNo INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''',''' + ISNULL(@ValueSerieID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N'' -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @ValueSerieID IS NULL SET @ValueSerieID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_DELETE_ValueSerie; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @ValueSerieID = system.fProtectID (@ValueSerieID); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); IF @ValueSerieID = N'' OR @ProductID = N'' OR @ProductLineID = N'' OR @FactoryID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername(@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE ProductLineID = @ProductLineID AND FactoryKey = @FactoryKey; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductID = @ProductID AND ProductLineKey = @ProductLineKey; SELECT @DelValueSerieKey = ValueSeriesKey, @DelValueSerieNo = ValueSeriesNo FROM planning.tdValueSeries WHERE ProductKey = @ProductKey AND ValueSeriesID = @ValueSerieID; IF @ProductKey = 0 OR @ProductLineKey = 0 OR @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 3 - Delete values IF @DelValueSerieKey = 0 BEGIN SET @ResultCode = 204; END ELSE BEGIN -- STEP 3.2 - Delete ValueSerie DELETE FROM planning.tfValues WHERE ValueSeriesKey = @DelValueSerieKey; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tdValueSeries WHERE ValueSeriesKey = @DelValueSerieKey; SET @EffectedRows += @@ROWCOUNT; -- STEP 3.3 - Decrement all ValueSeriesNo which are bigger than the deleted on UPDATE planning.tdValueSeries SET ValueSeriesNo = ValueSeriesNo - 1 FROM planning.tdValueSeries WHERE ProductKey = @ProductKey AND ValueSeriesNo > @DelValueSerieNo; SET @EffectedRows += @@ROWCOUNT; -- STEP 3.4 - if the new first ValueSeries is not NO 1 (1,3..... Numbers), set it to 1 IF @NewFirstValueSerieNo - 1 > 1 BEGIN UPDATE planning.tdValueSeries SET ValueSeriesNo = 1 FROM planning.tdValueSeries WHERE ProductKey = @ProductKey AND ValueSeriesKey = @NewFirstValueSerieKey; SET @EffectedRows += @@ROWCOUNT; END; END; COMMIT TRANSACTION sx_pf_DELETE_ValueSerie; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_DELETE_ValueSerie; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_ValueSerie'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Deletes a ValueSerie and changes the Numbers of the other ValueSeries. If Series No 1 is delete, Procedure creates Placeholder in a new ValueSeries 1.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueSerieID'; SET @value = N'ValueSerieID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spEXPORT_Factory; GO /* Saxess Software GmbH Last modified: 04/2025 for OCT 5.11 EXPORT operation of a single Factory, including - its properties in gtables - its global attributes - its ProductLine - its products - it values - its dependend lists, which have to be merged in the existing lists during import - in two styles, one for import as sql script, one for Import over the GUI Import The Parameter @Output for SQL Server exists only for backwards compatibilty - its no longer used Test Call direct (for small Factories) DECLARE @RC INT; EXEC @RC = planning.spEXPORT_Factory @Username = N'SQL' ,@FactoryID = N'ZT'; PRINT @RC; Test Call over BCP Results must be stored in table, if column width is to big for SSMS or SQLCMD 0a. Activate Stepnumber for output in Line 624 around and execute prodecedure 0b. Its difficult to send commands with german Umlaute over cmd, thats why name the database without Umlaute 1. Execute this to store the results in the table DROP TABLE IF EXISTS dbo.tmpout_Export; CREATE TABLE dbo.tmpout_Export ( StepNumber INT, --enable Stepnumber inside for testing Command NVARCHAR(MAX) ); INSERT INTO dbo.tmpout_Export EXEC planning.spEXPORT_Factory N'SQL',N'ZT',1; SELECT * FROM dbo.tmpout_Export ORDER BY StepNumber; 2. Copy this table Content in a file over a .bat file with this command bcp.exe "SELECT Command FROM PlanningFactoryCars.dbo.tmpout_Export ORDER BY StepNumber" queryout import.sql -T -S localhost -n -e error.log -c -C65001 SELECT TOP 10 * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spEXPORT_Factory',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spEXPORT_Factory','PARAMETER',NULL) */ CREATE PROCEDURE planning.spEXPORT_Factory @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @OutputForSQLServer INT = 1 AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) ,@ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(CAST(@OutputForSQLServer AS NVARCHAR(255)), N'NULL') + N'''' ,@EffectedRows INTEGER = 0 ,@ResultCode INTEGER = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR (2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @OutputForSQLServer IS NULL SET @OutputForSQLServer = 1; BEGIN TRY DECLARE @FactoryKey BIGINT = 0; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString(@Username); SET @FactoryID = system.fProtectID(@FactoryID); IF @FactoryID = N'' BEGIN SET @ResultCode = 404; RAISERROR(N'Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR(N'Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; IF @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR(N'Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_FactoryReadRight @TransactUsername, @FactoryID; IF @ResultCode <> 200 BEGIN SET @ResultCode = 401; RAISERROR(N'Invalid rights', 16, 10); END; -- STEP 3.1 - Determine dependend objects -- Determine dependend Lists and store in temporary table DECLARE @DependendListIDs AS TABLE (ListID NVARCHAR(255)); ;WITH lists AS ( -- MERGE dependend Lists, used in ProductDataTable SELECT ValueListID FROM planning.tdValueSeries WHERE FactoryKey = @FactoryKey -- MERGE dependend Lists, used in Globalattributes UNION ALL SELECT GlobalAttributeSource1 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource2 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource3 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource4 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource5 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource6 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource7 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource8 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource9 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource10 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource11 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource12 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource13 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource14 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource15 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource16 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource17 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource18 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource19 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource20 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource21 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource22 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource23 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource24 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey UNION ALL SELECT GlobalAttributeSource25 FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey ) INSERT INTO @DependendListIDs (ListID) SELECT DISTINCT ValueListID FROM lists; -- Declare temporary Table for export results DECLARE @ExportTable AS TABLE ( StepNumber INT NOT NULL ,RowNumber INT NOT NULL ,Command NVARCHAR(MAX)); INSERT INTO @ExportTable VALUES (0,1, N'--CONFIG: Adjust variable names manually to fit your needs !') ,(0,1, N'DECLARE @Username NVARCHAR(255) = ''SQL''') ,(0,1, N'DECLARE @FactoryID NVARCHAR(255) = ''' + @FactoryID + N'''') ,(0,1, N'--This FactoryID will be deleted during import, if it exists. You should be sure !'); -- DELETE FactoryID if exists INSERT INTO @ExportTable VALUES (1000,1, N'EXEC planning.spDELETE_Factory @Username, @FactoryID;'); -- POST Factory PRINT N'Starting Export Factory'; INSERT INTO @ExportTable SELECT 1001,1, N'EXEC planning.spPOST_Factory @Username, @FactoryID,N''' + system.fMaskSQL(NameShort) + N''',N''' + system.fMaskSQL(NameLong) + N''',N''' + system.fMaskSQL(CommentUser) + N''',N''' + system.fMaskSQL(CommentDev) + N''',N''' + system.fMaskSQL(ResponsiblePerson) + N''',N''' + system.fMaskSQL(ImageName) + N''';' FROM planning.tdFactories dF WHERE dF.FactoryKey = @FactoryKey -- POST ProductLines PRINT N'Starting Export Produclines'; INSERT INTO @ExportTable SELECT 1002,1, N'EXEC planning.spPOST_ProductLine @Username,''' + ProductLineID + N''', @FactoryID,N''' + system.fMaskSQL(NameShort) + N''',N''' + system.fMaskSQL(NameLong) + N''',N''' + system.fMaskSQL(CommentUser) + N''',N''' + system.fMaskSQL(CommentDev) + N''',N''' + system.fMaskSQL(ResponsiblePerson) + N''',N''' + system.fMaskSQL(ImageName) + N''',N''' + system.fMaskSQL(DefaultTemplate) + N''',N''' + system.fMaskSQL(GlobalAttributeSource1) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias1) + N''',N''' + system.fMaskSQL(GlobalAttributeSource2) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias2) + N''',N''' + system.fMaskSQL(GlobalAttributeSource3) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias3) + N''',N''' + system.fMaskSQL(GlobalAttributeSource4) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias4) + N''',N''' + system.fMaskSQL(GlobalAttributeSource5) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias5) + N''',N''' + system.fMaskSQL(GlobalAttributeSource6) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias6) + N''',N''' + system.fMaskSQL(GlobalAttributeSource7) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias7) + N''',N''' + system.fMaskSQL(GlobalAttributeSource8) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias8) + N''',N''' + system.fMaskSQL(GlobalAttributeSource9) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias9) + N''',N''' + system.fMaskSQL(GlobalAttributeSource10) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias10) + N''',N''' + system.fMaskSQL(GlobalAttributeSource11) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias11) + N''',N''' + system.fMaskSQL(GlobalAttributeSource12) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias12) + N''',N''' + system.fMaskSQL(GlobalAttributeSource13) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias13) + N''',N''' + system.fMaskSQL(GlobalAttributeSource14) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias14) + N''',N''' + system.fMaskSQL(GlobalAttributeSource15) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias15) + N''',N''' + system.fMaskSQL(GlobalAttributeSource16) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias16) + N''',N''' + system.fMaskSQL(GlobalAttributeSource17) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias17) + N''',N''' + system.fMaskSQL(GlobalAttributeSource18) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias18) + N''',N''' + system.fMaskSQL(GlobalAttributeSource19) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias19) + N''',N''' + system.fMaskSQL(GlobalAttributeSource20) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias20) + N''',N''' + system.fMaskSQL(GlobalAttributeSource21) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias21) + N''',N''' + system.fMaskSQL(GlobalAttributeSource22) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias22) + N''',N''' + system.fMaskSQL(GlobalAttributeSource23) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias23) + N''',N''' + system.fMaskSQL(GlobalAttributeSource24) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias24) + N''',N''' + system.fMaskSQL(GlobalAttributeSource25) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias25) + N''',N''' + system.fMaskSQL(LayoutJSON) + N''';' FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey; -- POST Products PRINT N'Starting Export Products' INSERT INTO @ExportTable SELECT 2000,1, N'EXEC planning.spPOST_ProductEmpty @Username,N''' + ProductID + N''',N''' + ProductLineID + N''',@FactoryID,N''' + TimeType + N''',N''' + system.fMaskSQL(NameShort) + N''',N''' + system.fMaskSQL(NameLong) + N''',N''' + system.fMaskSQL( system.fUnifyLinebreaks( CommentUser ) ) + N''',N''' + system.fMaskSQL(CommentDev) + N''',N''' + system.fMaskSQL(ResponsiblePerson) + N''',N''' + system.fMaskSQL(ImageName) + N''',N''' + system.fMaskSQL([Status]) + N''',N''' + system.fMaskSQL(Template) + N''',N''' + TemplateVersion + N''',N''' + system.fMaskSQL(GlobalAttribute1) + N''',N''' + system.fMaskSQL(GlobalAttribute2) + N''',N''' + system.fMaskSQL(GlobalAttribute3) + N''',N''' + system.fMaskSQL(GlobalAttribute4) + N''',N''' + system.fMaskSQL(GlobalAttribute5) + N''',N''' + system.fMaskSQL(GlobalAttribute6) + N''',N''' + system.fMaskSQL(GlobalAttribute7) + N''',N''' + system.fMaskSQL(GlobalAttribute8) + N''',N''' + system.fMaskSQL(GlobalAttribute9) + N''',N''' + system.fMaskSQL(GlobalAttribute10) + N''',N''' + system.fMaskSQL(GlobalAttribute11) + N''',N''' + system.fMaskSQL(GlobalAttribute12) + N''',N''' + system.fMaskSQL(GlobalAttribute13) + N''',N''' + system.fMaskSQL(GlobalAttribute14) + N''',N''' + system.fMaskSQL(GlobalAttribute15) + N''',N''' + system.fMaskSQL(GlobalAttribute16) + N''',N''' + system.fMaskSQL(GlobalAttribute17) + N''',N''' + system.fMaskSQL(GlobalAttribute18) + N''',N''' + system.fMaskSQL(GlobalAttribute19) + N''',N''' + system.fMaskSQL(GlobalAttribute20) + N''',N''' + system.fMaskSQL(GlobalAttribute21) + N''',N''' + system.fMaskSQL(GlobalAttribute22) + N''',N''' + system.fMaskSQL(GlobalAttribute23) + N''',N''' + system.fMaskSQL(GlobalAttribute24) + N''',N''' + system.fMaskSQL(GlobalAttribute25) + N''';' FROM planning.tdProducts WHERE FactoryKey = @FactoryKey; -- POST Statements --> statements are not exported -- POST all ValueSeries as NewValueSeries PRINT N'Starting Export ValueSeries'; INSERT INTO @ExportTable SELECT 3000 ,ROW_NUMBER()OVER (ORDER BY FactoryID,ProductLineID,ProductID,ValueSeriesNo) AS RowNumber ,N'EXEC planning.spPOST_ValueSerie @Username,N''' + ProductID + N''',N''' + ProductLineID + N''',@FactoryID,N''' + ValueSeriesID + N''',N''' + CAST(ValueSeriesNo AS NVARCHAR (255)) + N''',N''' + system.fMaskSQL(NameShort) + N''',N''' + system.fMaskSQL(NameLong) + N''',N''' + system.fMaskSQL(CommentUser) + N''',N''' + system.fMaskSQL(CommentDev) + N''',N''' + system.fMaskSQL(ImageName) + N''',N''' + CAST([IsNumeric] AS NVARCHAR (255)) + N''',N''' + CAST(VisibilityLevel AS NVARCHAR (255)) + N''',N''' + ValueSource + N''',N''' + ValueListID + N''',N''' + ValueFormatID + N''',N''' + Unit + N''',N''' + CAST(Scale AS NVARCHAR (255)) + N''',N''' + Effect + N''',N''' + EffectParameter + N''',N''' + CAST(AllowZero AS NVARCHAR(255)) + N''';' FROM planning.tdValueSeries WHERE FactoryKey = @FactoryKey ORDER BY FactoryID ,ProductLineID ,ProductID ,ValueSeriesNo; -- POST dTime PRINT N'Starting Export Time'; ;WITH tms AS ( SELECT dP.ProductID,dP.ProductLineID ,(SELECT N'[(' + CAST(t.TimeID AS NVARCHAR(10)) + N',0,0,''''' + FormatID + ''''')],' -- with DeleteFlag, AddValue,FormatID FROM planning.tdTime t WHERE t.ProductKey = dP.ProductKey ORDER BY t.TimeID FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)') AS vals FROM planning.tdProducts dP WHERE dP.FactoryKey = @FactoryKey ) INSERT INTO @ExportTable SELECT 4000,1, N'EXEC planning.spPOST_Timeline @Username, N''' + ProductID + N''',''' + ProductLineID + N''', @FactoryID,1,N''' + LEFT(vals, LEN(vals) - 1) + N''';' FROM tms WHERE NOT vals IS NULL; -- POST ProductDataTableValues PRINT N'Starting Export PDT' ;WITH tms AS ( SELECT dP.ProductID,dP.ProductLineID ,(SELECT N'[(''''' + ValueSeriesID + N''''',' + CAST(TimeID AS NVARCHAR(255)) + N',''''' + ValueFormula + N''''',''''' + CAST(ValueInt AS NVARCHAR(255)) + N''''',''''' + system.fMaskSQL(ValueText) + N''''',''''' + system.fMaskSQL(system.fUnifyLinebreaks(ValueComment)) + N''''')],' FROM planning.tfValues t WHERE t.ProductKey = dP.ProductKey FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)') AS vals FROM planning.tdProducts dP WHERE dP.FactoryKey = @FactoryKey ) INSERT INTO @ExportTable SELECT 5000,1, N'EXEC planning.spPOST_ProductDataTableValues @Username,N''' + ProductID + N''',''' + ProductLineID + N''', @FactoryID,0,N''' + LEFT(vals, LEN(vals) - 1) + N''';' FROM tms WHERE NOT vals IS NULL; -- POST Factory Properties PRINT N'Starting Export FactoryPoperties'; INSERT INTO @ExportTable SELECT 6000,1, N'EXEC planning.spPOST_FactoryProperty @Username, @FactoryID,N''' + PropertyID + N''',N''' + system.fMaskSQL(PropertyName) + N''',N''' + system.fMaskSQL(CommentUser) + N''',N''' + system.fMaskSQL(CommentDev) + N''',N''' + Unit + N''',N''' + system.fMaskSQL(ValueText) + N''',N''' + CAST(ValueInt AS NVARCHAR(255)) + N''',N''' + CAST(Scale AS NVARCHAR(255)) + N''',N''' + CAST(IsROSystemProperty AS NVARCHAR(255)) + N''',N''' + FormatID + N''';' FROM planning.tgFactories WHERE FactoryKey = @FactoryKey; -- POST ProductLine Properties PRINT N'Starting Export Productline Properties'; INSERT INTO @ExportTable SELECT 6001,1, N'EXEC planning.spPOST_ProductLineProperty @Username,N''' + ProductLineID + N''',@FactoryID,N''' + PropertyID + N''',N''' + system.fMaskSQL(PropertyName) + N''',N''' + system.fMaskSQL(CommentUser) + N''',N''' + system.fMaskSQL(CommentDev) + N''',N''' + Unit + N''',N''' + system.fMaskSQL(ValueText) + N''',N''' + CAST(ValueInt AS NVARCHAR(255)) + N''',N''' + CAST(Scale AS NVARCHAR(255)) + N''',N''' + CAST(IsROSystemProperty AS NVARCHAR(255)) + N''',N''' + FormatID + N''';' FROM planning.tgProductLines WHERE FactoryKey = @FactoryKey; -- POST Product Properties PRINT N'Starting Export Product Properties'; INSERT INTO @ExportTable SELECT 6002,1, N'EXEC planning.spPOST_ProductProperty @Username,N''' + ProductID + N''',N''' + ProductLineID + N''',@FactoryID,N''' + PropertyID + N''',N''' + system.fMaskSQL(PropertyName) + N''',N''' + system.fMaskSQL(CommentUser) + N''',N''' + system.fMaskSQL(CommentDev) + N''',N''' + Unit + N''',N''' + system.fMaskSQL(ValueText) + N''',N''' + CAST(ValueInt AS NVARCHAR(255)) + N''',N''' + CAST(Scale AS NVARCHAR(255)) + N''',N''' + CAST(IsROSystemProperty AS NVARCHAR(255)) + N''',N''' + FormatID + N''';' FROM planning.tgProducts WHERE FactoryKey = @FactoryKey; -- POST Tabs PRINT N'Starting Export Tabs' INSERT INTO @ExportTable SELECT 6003 ,Row_Number() OVER (ORDER BY FactoryID, ProductLineID, ProductID, Orderindex) AS Row ,N'EXEC planning.spPOST_Tab @Username, @FactoryID, ''' + ProductLineID + N''',''' + ProductID + N''',''' + TabID + N''', ' + CAST(Orderindex AS NVARCHAR(255)) + N',N''' + system.fMaskSQL(TabName) + N''',N''' + system.fMaskSQL(TabHint) + N''',N''' + system.fMaskSQL(TabText) + N''',N''' + system.fMaskSQL(PresentationCODE) + N''',N''' + system.fMaskSQL(Datasource) + N''',N''' + CAST(IsVisibleBOOL AS NVARCHAR(255)) + N''',N''' + '' + N''',N''' -- LayoutJSON compatibility + system.fMaskSQL(ParameterJSON) + N''',N''' + system.fMaskSQL(SQLCommand) + N''';' FROM planning.tTabs WHERE FactoryKey = @FactoryKey -- POST TabLayouts PRINT N'Starting Export TabLayouts' INSERT INTO @ExportTable SELECT 6004 ,Row_Number() OVER (ORDER BY FactoryID, ProductLineID, TabID, LayoutOwner, OrderIndex) AS Row ,N'EXEC planning.spPOST_TabLayout @Username, @FactoryID, ''' + ProductLineID + N''',''' + TabID + N''',''' + LayoutID + N''',''' + system.fMaskSQL(LayoutName) + N''',''' + system.fMaskSQL(LayoutJSON) + N''',' + CAST(Orderindex AS NVARCHAR(255)) + N',''' + LayoutOwner + N'''; ' FROM planning.tTabLayouts WHERE FactoryID = @FactoryID; -- Create the lists PRINT N'Starting Export Lists' INSERT INTO @ExportTable SELECT 7001,1, N'EXEC planning.spPOST_List @Username,N''' + ListID + N''',N''' + system.fMaskSQL(NameShort) + N''',N''' + system.fMaskSQL(NameLong) + N''',N''' + system.fMaskSQL(CommentUser) + N''',N''' + system.fMaskSQL(CommentDev) + N''',N''' + Datentyp + N''',N''' + Source + N''',N''' + SourceFormula + N''',N''' + FormatID + N''';' FROM planning.thLists WHERE ListID IN (SELECT ListID COLLATE DATABASE_DEFAULT FROM @DependendListIDs); -- Create List Values PRINT N'Starting Export List Values'; INSERT INTO @ExportTable SELECT 7002,1, N'EXEC planning.spPOST_ListValue @Username,0,N''' + ListID + N''',' + CAST(ValueInt AS NVARCHAR(255)) + N',' + CAST(Scale AS NVARCHAR(255)) + N',N''' + system.fMaskSQL(ValueText) + N''',N''' + system.fMaskSQL(ValueComment) + N''',N''' + FormatID + N''';' FROM planning.thListValues WHERE ListID IN (SELECT ListID COLLATE DATABASE_DEFAULT FROM @DependendListIDs); -- Create Formats PRINT N'Starting Export Formats'; ;WITH DependendFormats AS ( -- used in depended lists from global attributes SELECT DISTINCT FormatID FROM planning.thListValues WHERE ListID IN (SELECT ListID COLLATE DATABASE_DEFAULT FROM @DependendListIDs) UNION -- used to format ValueSeries SELECT DISTINCT ValueFormatID FROM planning.tdValueSeries WHERE FactoryKey = @FactoryKey UNION -- used to format Timeline SELECT DISTINCT FormatID FROM planning.tdTime WHERE FactoryKey = @FactoryKey UNION -- used to format Ranges SELECT DISTINCT FormatID FROM planning.tgProducts WHERE FactoryKey = @FactoryKey AND PropertyID LIKE N'PDTRange_%' ) INSERT INTO @ExportTable SELECT 7003,1, N'EXEC planning.spPOST_Format @Username,N''' + FormatID + N''',N''' + BackgroundColor + N''',N''' + FontColor + N''',N''' + ValueFormat + N''',N''' + CAST(Bold AS NVARCHAR(255)) + N''',N''' + CAST(Italic AS NVARCHAR(255)) + N''',N''' + AlignmentHorizontal + N''',N''' + AlignmentVertical + N''',N''' + CAST(TextUnderlined AS NVARCHAR(255)) + N''',N''' + CAST(BorderToNext AS NVARCHAR(255)) + N''',N''' + CAST(BorderToPrevious AS NVARCHAR(255)) + N''';' FROM planning.thFormats WHERE FormatID IN (SELECT DISTINCT FormatID FROM DependendFormats WHERE LEN(FormatID)>0); PRINT N'Finishing Script'; INSERT INTO @ExportTable VALUES (8000,1, N'GO'); -- Final Value Output ################################################################################################################### PRINT N'Starting Output'; SELECT --StepNumber, --activate only for bcp test output Command FROM @ExportTable ORDER BY StepNumber ,RowNumber; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT N'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spEXPORT_Factory'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'EXPORT operation of a single Factory, including, properties in gtables, global attributes, ProductLine, products, values, dependend lists which have to be merged in the existing lists during import in two styles. One for import as sql script, one for Import over the GUI Import'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@OutputForSQLServer'; SET @value = N'OutputForSQLServer'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spEXPORT_Product; GO /* Procedure to export one single product, including - it values - its dependend lists, which have to be merged in the existing lists during import - in two styles, one for import as sql script, one for Import over the GUI Import Saxess Software GmbH Last modified: 04/2025 for OCT 5.11 TestCall DECLARE @RC int ,@Username NVARCHAR(255) = 'SQL' ,@FactoryID NVARCHAR(255) = 'ZT' ,@ProductLineID NVARCHAR(255) = 'U' ,@ProductID NVARCHAR(255) = '1'; EXECUTE @RC = planning.spEXPORT_Product @Username ,@FactoryID ,@ProductLineID ,@ProductID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spEXPORT_Product',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spEXPORT_Product','PARAMETER',NULL) */ CREATE PROCEDURE planning.spEXPORT_Product @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @ProductID NVARCHAR(255), @OutputForSQLServer INT = 1 AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@FactoryID ,N'NULL') + N''',''' + ISNULL(@ProductLineID ,N'NULL') + N''',''' + ISNULL(@ProductID ,N'NULL') + N''',''' + ISNULL(CAST(@OutputForSQLServer AS NVARCHAR(255)), N'NULL') + N'''' ,@EffectedRows INTEGER = 0 ,@ResultCode INTEGER = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR (2000) = N''; -- NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; BEGIN TRY DECLARE @ProductKey BIGINT = 0; DECLARE @ProductLineKey BIGINT = 0; DECLARE @Template NVARCHAR (255) = N''; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR(N'Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR(N'Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID; IF @ProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR(N'Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 BEGIN SET @ResultCode = 401; RAISERROR(N'Invalid rights', 16, 10); END; -- STEP 3.1 - Determine dependend objects -- Determine Template SELECT @Template = Template FROM planning.tdProducts WHERE ProductKey = @ProductKey; -- Deterime all ProductLines using this Template DECLARE @DependendProductLinesIDs AS TABLE (ProductLineKey BIGINT); INSERT INTO @DependendProductLinesIDs (ProductLineKey) SELECT DISTINCT ProductLineKey FROM planning.tdProducts WHERE Template = @Template; -- Determine dependend Lists and store in temporary table DECLARE @DependendListIDs AS TABLE (ListID NVARCHAR(255)); ;WITH lists AS ( -- MERGE dependend Lists, used in ProductDataTable SELECT ValueListID FROM planning.tdValueSeries WHERE ProductKey = @ProductKey -- MERGE dependend Lists, used in Globalattributes UNION ALL SELECT DISTINCT GlobalAttributeSource1 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource2 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource3 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource4 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource5 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource6 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource7 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource8 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource9 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource10 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource11 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource12 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource13 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource14 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource15 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource16 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource17 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource18 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource19 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource20 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource21 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource22 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource23 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource24 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) UNION ALL SELECT DISTINCT GlobalAttributeSource25 FROM planning.tdProductLines WHERE ProductLineKey IN (SELECT ProductLineKey FROM @DependendProductLinesIDs) ) INSERT INTO @DependendListIDs (ListID) SELECT DISTINCT ValueListID FROM lists WHERE ValueListID != ''; -- Declare temporary Table for export results DECLARE @ExportTable AS TABLE ( StepNumber INT NOT NULL ,RowNumber INT NOT NULL ,Command NVARCHAR(MAX)); INSERT INTO @ExportTable VALUES (0, 1, N'--CONFIG: Adjust variable names manually to fit your needs !') ,(0, 1, N'DECLARE @Username NVARCHAR(255) = N''SQL''') ,(0, 1, N'DECLARE @FactoryID NVARCHAR(255) = N''' + @FactoryID + N'''') ,(0, 1, N'DECLARE @ProductLineID NVARCHAR(255) = N''' + @ProductLineID + N'''') ,(0, 1, N'DECLARE @ProductID NVARCHAR(255) = N''' + @ProductID + N'''') ,(0, 1, N'--This ProductID will be deleted during import, if it exists. You should be sure !'); -- DELETE Product if exists INSERT INTO @ExportTable VALUES (1000, 1, N'EXEC planning.spDELETE_Product @Username, @FactoryID, @ProductLineID, @ProductID;'); -- POST Product INSERT INTO @ExportTable SELECT 2000, 1, N'EXEC planning.spPOST_ProductEmpty @Username, @ProductID, @ProductLineID, @FactoryID,N''' + TimeType + N''',N''' + system.fMaskSQL (NameShort) + N''',N''' + system.fMaskSQL (NameLong) + N''',N''' + system.fMaskSQL ( system.fUnifyLinebreaks ( CommentUser ) ) + N''',N''' + system.fMaskSQL (CommentDev) + N''',N''' + system.fMaskSQL (ResponsiblePerson) + N''',N''' + system.fMaskSQL (ImageName) + N''',N''' + system.fMaskSQL ([Status]) + N''',N''' + system.fMaskSQL (Template) + N''',N''' + system.fMaskSQL (TemplateVersion) + N''',N''' + system.fMaskSQL (GlobalAttribute1) + N''',N''' + system.fMaskSQL (GlobalAttribute2) + N''',N''' + system.fMaskSQL (GlobalAttribute3) + N''',N''' + system.fMaskSQL (GlobalAttribute4) + N''',N''' + system.fMaskSQL (GlobalAttribute5) + N''',N''' + system.fMaskSQL (GlobalAttribute6) + N''',N''' + system.fMaskSQL (GlobalAttribute7) + N''',N''' + system.fMaskSQL (GlobalAttribute8) + N''',N''' + system.fMaskSQL (GlobalAttribute9) + N''',N''' + system.fMaskSQL (GlobalAttribute10) + N''',N''' + system.fMaskSQL (GlobalAttribute11) + N''',N''' + system.fMaskSQL (GlobalAttribute12) + N''',N''' + system.fMaskSQL (GlobalAttribute13) + N''',N''' + system.fMaskSQL (GlobalAttribute14) + N''',N''' + system.fMaskSQL (GlobalAttribute15) + N''',N''' + system.fMaskSQL (GlobalAttribute16) + N''',N''' + system.fMaskSQL (GlobalAttribute17) + N''',N''' + system.fMaskSQL (GlobalAttribute18) + N''',N''' + system.fMaskSQL (GlobalAttribute19) + N''',N''' + system.fMaskSQL (GlobalAttribute20) + N''',N''' + system.fMaskSQL (GlobalAttribute21) + N''',N''' + system.fMaskSQL (GlobalAttribute22) + N''',N''' + system.fMaskSQL (GlobalAttribute23) + N''',N''' + system.fMaskSQL (GlobalAttribute24) + N''',N''' + system.fMaskSQL (GlobalAttribute25) + N''';' FROM planning.tdProducts WHERE ProductKey = @ProductKey; -- POST Product Properties INSERT INTO @ExportTable SELECT 3000, 1, N'EXEC planning.spPOST_ProductProperty @Username, @ProductID, @ProductLineID, @FactoryID,N''' + + PropertyID + N''',N''' + system.fMaskSQL (PropertyName) + N''',N''' + system.fMaskSQL (CommentUser) + N''',N''' + system.fMaskSQL (CommentDev) + N''',N''' + system.fMaskSQL (Unit) + N''',N''' + system.fMaskSQL (ValueText) + N''',N''' + CAST(ValueInt AS NVARCHAR(255)) + N''',N''' + CAST(Scale AS NVARCHAR(255)) + N''',N''' + CAST(IsROSystemProperty AS NVARCHAR(255)) + N''',N''' + FormatID + N''';' FROM planning.tgProducts WHERE ProductKey = @ProductKey; -- POST Statements -- statements are not exported -- POST all ValueSeries as NewValueSeries - N in Parameterlist needed INSERT INTO @ExportTable SELECT 4000 , ROW_NUMBER()OVER (ORDER BY ProductID,ValueSeriesNo) AS RowNumber , N'EXEC planning.spPOST_ValueSerie @Username, @ProductID, @ProductLineID, @FactoryID,N''' + + ValueSeriesID + N''',N''' + CAST(ValueSeriesNo AS NVARCHAR (255)) + N''',N''' + system.fMaskSQL (NameShort) + N''',N''' + system.fMaskSQL (NameLong) + N''',N''' + system.fMaskSQL (CommentUser) + N''',N''' + system.fMaskSQL (CommentDev) + N''',N''' + system.fMaskSQL (ImageName) + N''',N''' + CAST([IsNumeric] AS NVARCHAR (255)) + N''',N''' + CAST(VisibilityLevel AS NVARCHAR (255)) + N''',N''' + ValueSource + N''',N''' + ValueListID + N''',N''' + ValueFormatID + N''',N''' + Unit + N''',N''' + CAST(Scale AS NVARCHAR (255)) + N''',N''' + Effect + N''',N''' + EffectParameter + N''',N''' + CAST(AllowZero AS NVARCHAR (255)) + N''';' FROM planning.tdValueSeries WHERE ProductKey = @ProductKey ORDER BY ProductID ,ValueSeriesNo; ;WITH tms AS ( SELECT (SELECT N'[(' + CAST(TimeID AS NVARCHAR(10)) + N',0,0,''''' + FormatID + N''''')],' FROM planning.tdTime WHERE ProductKey = @ProductKey ORDER BY TimeID FOR XML PATH(''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)') AS val ) INSERT INTO @ExportTable SELECT 5000, 1, N'EXEC planning.spPOST_Timeline @Username, @ProductID, @ProductLineID, @FactoryID,1,N''' + LEFT(val, LEN(val) - 1) + N''';' FROM tms WHERE NOT val IS NULL; -- POST ProductDataTableValues - no N in Array needed ! ;WITH vals AS ( SELECT (SELECT N'[(''''' + fV.ValueSeriesID + N''''',' + CAST(fV.TimeID AS NVARCHAR(255)) + N',''''' + fV.ValueFormula + N''''',''''' + CAST(fV.ValueInt AS NVARCHAR(255)) + N''''',''''' + system.fMaskSQL (fV.ValueText) + N''''',''''' + system.fMaskSQL (system.fUnifyLinebreaks(fV.ValueComment)) + N''''')],' FROM planning.tfValues fV LEFT JOIN planning.tdValueSeries dVS ON fV.ValueSeriesKey = dVS.ValueSeriesKey WHERE fV.ProductKey = @ProductKey ORDER BY dVS.ValueSeriesNo FOR XML PATH(''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)') AS val ) INSERT INTO @ExportTable SELECT 6000, 1, N'EXEC planning.spPOST_ProductDataTableValues @Username, @ProductID, @ProductLineID, @FactoryID,0,N''' + LEFT(val, LEN(val) - 1) + N''';' FROM vals WHERE NOT val IS NULL; -- Create the lists INSERT INTO @ExportTable SELECT 7001, 1, N'EXECUTE planning.spPOST_List @Username,N''' + ListID + N''',N''' + system.fMaskSQL (NameShort) + N''',N''' + system.fMaskSQL (NameLong) + N''',N''' + system.fMaskSQL (CommentUser) + N''',N''' + system.fMaskSQL (CommentDev) + N''',N''' + Datentyp + N''',N''' + Source + N''',N''' + SourceFormula + N''',N''' + FormatID + N''';' FROM planning.thLists WHERE ListID IN (SELECT ListID COLLATE DATABASE_DEFAULT FROM @DependendListIDs); -- Create List Values INSERT INTO @ExportTable SELECT 7002, 1, N'EXECUTE planning.spPOST_ListValue @Username,0,N''' + ListID + N''',' + CAST(ValueInt AS NVARCHAR(255)) + N',' + CAST(Scale AS NVARCHAR(255)) + N',N''' + system.fMaskSQL (ValueText) + N''',N''' + system.fMaskSQL (ValueComment) + N''',N''' + FormatID + N''';' FROM planning.thListValues WHERE ListID IN (SELECT ListID COLLATE DATABASE_DEFAULT FROM @DependendListIDs); ;WITH DependendFormats AS ( -- used in depended lists from global attributes SELECT DISTINCT FormatID FROM planning.thListValues WHERE ListID IN (SELECT ListID COLLATE DATABASE_DEFAULT FROM @DependendListIDs) UNION -- used to format ValueSeries SELECT DISTINCT ValueFormatID FROM planning.tdValueSeries WHERE ProductKey = @ProductKey UNION -- used to format Timeline SELECT DISTINCT FormatID FROM planning.tdTime WHERE ProductKey = @ProductKey UNION -- used to format Ranges SELECT DISTINCT FormatID FROM planning.tgProducts WHERE ProductKey = @ProductKey AND PropertyID LIKE N'PDTRange_%' ) INSERT INTO @ExportTable SELECT 8001, 1, N'EXEC planning.spPOST_Format @Username,N''' + FormatID + N''',N''' + BackgroundColor + N''',N''' + FontColor + N''',N''' + ValueFormat + N''',N''' + CAST(Bold AS NVARCHAR(255)) + N''',N''' + CAST(Italic AS NVARCHAR(255)) + N''',N''' + AlignmentHorizontal + N''',N''' + AlignmentVertical + N''',N''' + CAST(TextUnderlined AS NVARCHAR(255)) + N''',N''' + CAST(BorderToNext AS NVARCHAR(255)) + N''',N''' + CAST(BorderToPrevious AS NVARCHAR(255)) + N''';' FROM planning.thFormats WHERE FormatID IN (SELECT DISTINCT FormatID FROM DependendFormats WHERE LEN(FormatID)>0); -- leere Strings aus FormatID in dTime m?ssen eliminiert werden INSERT INTO @ExportTable VALUES (9000, 1, N'GO'); -- Final Value Output ################################################################################################################### SELECT Command FROM @ExportTable ORDER BY StepNumber, RowNumber; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT N'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spEXPORT_Product'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to export one single product, including values, dependend lists, which have to be merged in the existing lists during import in two styles: one for import as sql script, one for Import over the GUI Import'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@OutputForSQLServer'; SET @value = N'OutputForSQLServer'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spEXPORT_ProductLine; GO /* EXPORT operation of a single productline, including - its properties in gtables - its global attributes - its products - it values - its dependend lists, which have to be merged in the existing lists during import - in two styles, one for import as sql script, one for Import over the GUI Import Saxess Software GmbH Last modified: 04/2025 for OCT 5.11 Testcall Procedure direct DECLARE @RC INT; EXEC @RC = planning.spEXPORT_ProductLine @Username = N'SQL' ,@FactoryID = N'ZT' ,@ProductLineID = N'U'; PRINT @RC; Testcall over BCP Results must be stored in table, if column width is to big for SSMS or SQLCMD 0a. Activate Stepnumber for output in Line 753 around and execute prodecedure 0b. Its difficult to send commands with german Umlaute over cmd, thats why name the database without Umlaute 1. Execute this to store the results in the table IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'tmpout_Export') AND type in (N'U')) DROP TABLE tmpout_Export GO CREATE TABLE tmpout_Export ( StepNumber INT, Command NVARCHAR(MAX) ) INSERT INTO tmpout_Export EXEC planning.spEXPORT_ProductLine 'SQL','ZT','U',1 2. Copy this table Content in a file over a .bat file with this command bcp.exe "SELECT Command FROM PlanningFactoryCars.tmpout_Export ORDER BY StepNumber" queryout import.sql -T -S localhost -n -e error.log -c -C65001 Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spEXPORT_ProductLine',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spEXPORT_ProductLine','PARAMETER',NULL) */ CREATE PROCEDURE planning.spEXPORT_ProductLine @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @OutputForSQLServer INT = 1 AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername AS NVARCHAR(255) ,@ProcedureName AS NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString AS NVARCHAR (MAX) = N'''' + ISNULL(@Username , N'NULL') + N''',''' + ISNULL(@FactoryID , N'NULL') + N''',''' + ISNULL(@ProductLineID , N'NULL') + N''',''' + ISNULL(CAST(@OutputForSQLServer AS NVARCHAR(255)), N'NULL') + N'''' ,@EffectedRows AS INTEGER = 0 ,@ResultCode AS INTEGER = 501 ,@TimestampCall AS DATETIME = GETUTCDATE() ,@Comment AS NVARCHAR (2000) = N'' -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; BEGIN TRY DECLARE @ProductLineKey BIGINT = 0; -- Protect input parameters SET @Username = system.fProtectString(@Username); SET @FactoryID = system.fProtectID(@FactoryID); SET @ProductLineID = system.fProtectID(@ProductLineID); IF @FactoryID = N'' OR @ProductLineID = N'' BEGIN SET @ResultCode = 404; RAISERROR(N'Empty input parameters', 16, 10); END; -- Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR(N'Transaction user don`t exists', 16, 10); END; -- Determine keys SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID; IF @ProductLineKey = 0 BEGIN SET @ResultCode = 404; RAISERROR(N'Keys don`t exists', 16, 10); END; -- Check rights EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 BEGIN SET @ResultCode = 401; RAISERROR(N'Invalid rights', 16, 10); END; -- Determine dependend objects -- Determine dependend Lists and store in temporary table DECLARE @DependendListIDs AS TABLE (ListID NVARCHAR(255)); ;WITH lists AS ( -- MERGE dependend Lists, used in ProductDataTable SELECT ValueListID FROM planning.tdValueSeries WHERE ProductLineKey = @ProductLineKey -- MERGE dependend Lists, used in Globalattributes UNION ALL SELECT GlobalAttributeSource1 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource2 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource3 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource4 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource5 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource6 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource7 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource8 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource9 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource10 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource11 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource12 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource13 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource14 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource15 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource16 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource17 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource18 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource19 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource20 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource21 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource22 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource23 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource24 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey UNION ALL SELECT GlobalAttributeSource25 FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey ) INSERT INTO @DependendListIDs (ListID) SELECT DISTINCT ValueListID FROM lists; -- Declare temporary Table for export results DECLARE @ExportTable AS TABLE ( StepNumber INT NOT NULL ,RowNumber INT NOT NULL ,Command NVARCHAR(MAX) ); INSERT INTO @ExportTable VALUES (0, 1, N'--CONFIG: Adjust variable names manually to fit your needs !') ,(0, 1, N'DECLARE @Username NVARCHAR(255) = ''SQL''') ,(0, 1, N'DECLARE @FactoryID NVARCHAR(255) = ''' + @FactoryID + N'''') ,(0, 1, N'DECLARE @ProductLineID NVARCHAR(255) = ''' + @ProductLineID + N'''') ,(0, 1, N'--This ProductLineID will be deleted during import, if it exists. You should be sure !'); -- DELETE ProductLine if exists INSERT INTO @ExportTable VALUES (1000,1, N'EXEC planning.spDELETE_ProductLine @Username, @FactoryID, @ProductLineID;'); -- POST ProductLine INSERT INTO @ExportTable SELECT 1001,1, N'EXEC planning.spPOST_ProductLine @Username, @ProductLineID, @FactoryID,N''' + system.fMaskSQL(NameShort) + N''',N''' + system.fMaskSQL(NameLong) + N''',N''' + system.fMaskSQL(CommentUser) + N''',N''' + system.fMaskSQL(CommentDev) + N''',N''' + system.fMaskSQL(ResponsiblePerson) + N''',N''' + system.fMaskSQL(ImageName) + N''',N''' + system.fMaskSQL(DefaultTemplate) + N''',N''' + system.fMaskSQL(GlobalAttributeSource1) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias1) + N''',N''' + system.fMaskSQL(GlobalAttributeSource2) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias2) + N''',N''' + system.fMaskSQL(GlobalAttributeSource3) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias3) + N''',N''' + system.fMaskSQL(GlobalAttributeSource4) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias4) + N''',N''' + system.fMaskSQL(GlobalAttributeSource5) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias5) + N''',N''' + system.fMaskSQL(GlobalAttributeSource6) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias6) + N''',N''' + system.fMaskSQL(GlobalAttributeSource7) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias7) + N''',N''' + system.fMaskSQL(GlobalAttributeSource8) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias8) + N''',N''' + system.fMaskSQL(GlobalAttributeSource9) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias9) + N''',N''' + system.fMaskSQL(GlobalAttributeSource10) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias10) + N''',N''' + system.fMaskSQL(GlobalAttributeSource11) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias11) + N''',N''' + system.fMaskSQL(GlobalAttributeSource12) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias12) + N''',N''' + system.fMaskSQL(GlobalAttributeSource13) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias13) + N''',N''' + system.fMaskSQL(GlobalAttributeSource14) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias14) + N''',N''' + system.fMaskSQL(GlobalAttributeSource15) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias15) + N''',N''' + system.fMaskSQL(GlobalAttributeSource16) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias16) + N''',N''' + system.fMaskSQL(GlobalAttributeSource17) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias17) + N''',N''' + system.fMaskSQL(GlobalAttributeSource18) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias18) + N''',N''' + system.fMaskSQL(GlobalAttributeSource19) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias19) + N''',N''' + system.fMaskSQL(GlobalAttributeSource20) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias20) + N''',N''' + system.fMaskSQL(GlobalAttributeSource21) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias21) + N''',N''' + system.fMaskSQL(GlobalAttributeSource22) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias22) + N''',N''' + system.fMaskSQL(GlobalAttributeSource23) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias23) + N''',N''' + system.fMaskSQL(GlobalAttributeSource24) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias24) + N''',N''' + system.fMaskSQL(GlobalAttributeSource25) + N''',N''' + system.fMaskSQL(GlobalAttributeAlias25) + N''',N''' + system.fMaskSQL(LayoutJSON) + N''';' FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey; -- POST Products INSERT INTO @ExportTable SELECT 2000,1, N'EXEC planning.spPOST_ProductEmpty @Username,''' + ProductID + N''', @ProductLineID, @FactoryID,N''' + TimeType + N''',N''' + system.fMaskSQL(NameShort) + N''',N''' + system.fMaskSQL(NameLong) + N''',N''' + system.fMaskSQL( system.fUnifyLinebreaks ( CommentUser ) ) + N''',N''' + system.fMaskSQL(CommentDev) + N''',N''' + system.fMaskSQL(ResponsiblePerson) + N''',N''' + system.fMaskSQL(ImageName) + N''',N''' + system.fMaskSQL([Status]) + N''',N''' + system.fMaskSQL(Template) + N''',N''' + system.fMaskSQL(TemplateVersion) + N''',N''' + system.fMaskSQL(GlobalAttribute1) + N''',N''' + system.fMaskSQL(GlobalAttribute2) + N''',N''' + system.fMaskSQL(GlobalAttribute3) + N''',N''' + system.fMaskSQL(GlobalAttribute4) + N''',N''' + system.fMaskSQL(GlobalAttribute5) + N''',N''' + system.fMaskSQL(GlobalAttribute6) + N''',N''' + system.fMaskSQL(GlobalAttribute7) + N''',N''' + system.fMaskSQL(GlobalAttribute8) + N''',N''' + system.fMaskSQL(GlobalAttribute9) + N''',N''' + system.fMaskSQL(GlobalAttribute10) + N''',N''' + system.fMaskSQL(GlobalAttribute11) + N''',N''' + system.fMaskSQL(GlobalAttribute12) + N''',N''' + system.fMaskSQL(GlobalAttribute13) + N''',N''' + system.fMaskSQL(GlobalAttribute14) + N''',N''' + system.fMaskSQL(GlobalAttribute15) + N''',N''' + system.fMaskSQL(GlobalAttribute16) + N''',N''' + system.fMaskSQL(GlobalAttribute17) + N''',N''' + system.fMaskSQL(GlobalAttribute18) + N''',N''' + system.fMaskSQL(GlobalAttribute19) + N''',N''' + system.fMaskSQL(GlobalAttribute20) + N''',N''' + system.fMaskSQL(GlobalAttribute21) + N''',N''' + system.fMaskSQL(GlobalAttribute22) + N''',N''' + system.fMaskSQL(GlobalAttribute23) + N''',N''' + system.fMaskSQL(GlobalAttribute24) + N''',N''' + system.fMaskSQL(GlobalAttribute25) + N''';' FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey; -- POST Statements --> statements are not exported -- POST all ValueSeries as NewValueSeries INSERT INTO @ExportTable SELECT 3000 AS Step , ROW_NUMBER()OVER (ORDER BY ProductLineID,ProductID,ValueSeriesNo) AS RowNumber , N'EXEC planning.spPOST_ValueSerie @Username,N''' + ProductID + N''',@ProductLineID, @FactoryID,N''' + ValueSeriesID + N''',N''' + CAST(ValueSeriesNo AS NVARCHAR (255)) + N''',N''' + system.fMaskSQL(NameShort) + N''',N''' + system.fMaskSQL(NameLong) + N''',N''' + system.fMaskSQL(CommentUser) + N''',N''' + system.fMaskSQL(CommentDev) + N''',N''' + system.fMaskSQL(ImageName) + N''',N''' + CAST([IsNumeric] AS NVARCHAR (255)) + N''',N''' + CAST(VisibilityLevel AS NVARCHAR (255)) + N''',N''' + ValueSource + N''',N''' + ValueListID + N''',N''' + ValueFormatID + N''',N''' + Unit + N''',N''' + CAST(Scale AS NVARCHAR (255)) + N''',N''' + Effect + N''',N''' + EffectParameter + N''',N''' + CAST(AllowZero AS NVARCHAR (255)) + N''';' FROM planning.tdValueSeries WHERE ProductLineKey = @ProductLineKey ORDER BY ProductLineID ,ProductID ,ValueSeriesNo; -- POST dTime ;WITH tms AS ( SELECT dP.ProductID ,(SELECT N'[(' + CAST(t.TimeID AS NVARCHAR(10)) + N',0,0,'''''+ FormatID + N''''')],' -- with DeleteFlag, AddValue,FormatID FROM planning.tdTime t WHERE t.ProductKey = dP.ProductKey ORDER BY t.TimeID FOR XML PATH(''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)') AS vals FROM planning.tdProducts dP WHERE dP.ProductLineKey = @ProductLineKey ) INSERT INTO @ExportTable SELECT 4000,1, N'EXEC planning.spPOST_Timeline @Username, N''' + ProductID + N''', @ProductLineID, @FactoryID,1,N''' + LEFT(vals, LEN(vals) - 1) + N''';' FROM tms WHERE NOT vals IS NULL; -- POST ProductDataTableValues - no N in Array needed ;WITH tms AS ( SELECT dP.ProductID ,(SELECT N'[(''''' + ValueSeriesID + N''''',' + CAST(TimeID AS NVARCHAR(255)) + N',''''' + ValueFormula + N''''',''''' + CAST(ValueInt AS NVARCHAR(255)) + N''''',''''' + system.fMaskSQL(ValueText) + N''''',''''' + system.fMaskSQL(system.fUnifyLinebreaks(ValueComment)) + N''''')],' FROM planning.tfValues t WHERE t.ProductKey = dP.ProductKey FOR XML PATH(''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)') AS vals FROM planning.tdProducts dP WHERE dP.ProductLineKey = @ProductLineKey ) INSERT INTO @ExportTable SELECT 5000,1, N'EXEC planning.spPOST_ProductDataTableValues @Username,N''' + ProductID + N''', @ProductLineID, @FactoryID,0,N''' + LEFT(vals, LEN(vals) - 1) + N''';' FROM tms WHERE NOT vals IS NULL; -- POST ProductLine Properties INSERT INTO @ExportTable SELECT 6000,1, N'EXEC planning.spPOST_ProductLineProperty @Username, @ProductLineID, @FactoryID,N''' + PropertyID + N''',N''' + system.fMaskSQL(PropertyName) + N''',N''' + system.fMaskSQL(CommentUser) + N''',N''' + system.fMaskSQL(CommentDev) + N''',N''' + Unit + N''',N''' + system.fMaskSQL(ValueText) + N''',N''' + CAST(ValueInt AS NVARCHAR(255)) + N''',N''' + CAST(Scale AS NVARCHAR(255)) + N''',N''' + CAST(IsROSystemProperty AS NVARCHAR(255)) + N''',N''' + FormatID + N''';' FROM planning.tgProductLines WHERE ProductLineKey = @ProductLineKey; -- POST Tabs INSERT INTO @ExportTable SELECT 6003 ,Row_Number() OVER (ORDER BY ProductLineID, ProductID, Orderindex) AS Row ,N'EXEC planning.spPOST_Tab @Username, @FactoryID, @ProductLineID, N''' + ProductID + N''',''' + TabID + N''', ' + CAST(Orderindex AS NVARCHAR(255)) + N',N''' + system.fMaskSQL(TabName) + N''',N''' + system.fMaskSQL(TabHint) + N''',N''' + system.fMaskSQL(TabText) + N''',N''' + system.fMaskSQL(PresentationCODE) + N''',N''' + system.fMaskSQL(Datasource) + N''',N''' + CAST(IsVisibleBOOL AS NVARCHAR(255)) + N''',N''' + '' + N''',N''' -- LayoutJSON compatibility + system.fMaskSQL(ParameterJSON) + N''',N''' + system.fMaskSQL(SQLCommand) + N''';' FROM planning.tTabs WHERE ProductLineKey = @ProductLineKey; -- POST TabLayouts INSERT INTO @ExportTable SELECT 6004 ,Row_Number() OVER (ORDER BY FactoryID, ProductLineID, TabID, LayoutOwner, OrderIndex) AS Row ,N'EXEC planning.spPOST_TabLayout @Username, @FactoryID, @ProductLineID, N''' + TabID + N''',''' + LayoutID + N''',''' + system.fMaskSQL(LayoutName) + N''',''' + system.fMaskSQL(LayoutJSON) + N''',' + CAST(Orderindex AS NVARCHAR(255)) + N',''' + LayoutOwner + N'''; ' FROM planning.tTabLayouts WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID; -- POST Product Properties INSERT INTO @ExportTable SELECT 6001,1, N'EXEC planning.spPOST_ProductProperty @Username,N''' + ProductID + N''',@ProductLineID, @FactoryID,N''' + PropertyID + N''',N''' + system.fMaskSQL(PropertyName) + N''',N''' + system.fMaskSQL(CommentUser) + N''',N''' + system.fMaskSQL(CommentDev) + N''',N''' + Unit + N''',N''' + system.fMaskSQL(ValueText) + N''',N''' + CAST(ValueInt AS NVARCHAR(255)) + N''',N''' + CAST(Scale AS NVARCHAR(255)) + N''',N''' + CAST(IsROSystemProperty AS NVARCHAR(255)) + N''',N''' + FormatID + N''';' FROM planning.tgProducts WHERE ProductLineKey = @ProductLineKey; -- Create the lists INSERT INTO @ExportTable SELECT 7001,1, N'EXEC planning.spPOST_List @Username,N''' + ListID + N''',N''' + system.fMaskSQL(NameShort) + N''',N''' + system.fMaskSQL(NameLong) + N''',N''' + system.fMaskSQL(CommentUser) + N''',N''' + system.fMaskSQL(CommentDev) + N''',N''' + Datentyp + N''',N''' + Source + N''',N''' + SourceFormula + N''',N''' + FormatID + N''';' FROM planning.thLists WHERE ListID IN (SELECT ListID COLLATE DATABASE_DEFAULT FROM @DependendListIDs); -- Create List Values INSERT INTO @ExportTable SELECT 7002,1, N'EXEC planning.spPOST_ListValue @Username,0,N''' + ListID + N''',' + CAST(ValueInt AS NVARCHAR(255)) + N',' + CAST(Scale AS NVARCHAR(255)) + N',N''' + system.fMaskSQL(ValueText) + N''',N''' + system.fMaskSQL(ValueComment) + N''',N''' + FormatID + N''';' FROM planning.thListValues WHERE ListID IN (SELECT ListID COLLATE DATABASE_DEFAULT FROM @DependendListIDs); -- Create Formats ;WITH DependendFormats AS ( -- used in depended lists from global attributes SELECT FormatID FROM planning.thListValues WHERE ListID IN (SELECT ListID COLLATE DATABASE_DEFAULT FROM @DependendListIDs) UNION -- used to format ValueSeries SELECT DISTINCT ValueFormatID FROM planning.tdValueSeries WHERE ProductLineKey = @ProductLineKey UNION -- used to format Timeline SELECT DISTINCT FormatID FROM planning.tdTime WHERE ProductLineKey = @ProductLineKey UNION -- used to format Ranges SELECT DISTINCT FormatID FROM planning.tgProducts WHERE ProductLineKey = @ProductLineKey AND PropertyID LIKE N'PDTRange_%' ) INSERT INTO @ExportTable SELECT 7003,1, N'EXEC planning.spPOST_Format @Username,N''' + FormatID + N''',N''' + BackgroundColor + N''',N''' + FontColor + N''',N''' + ValueFormat + N''',N''' + CAST(Bold AS NVARCHAR(255)) + N''',N''' + CAST(Italic AS NVARCHAR(255)) + N''',N''' + AlignmentHorizontal + N''',N''' + AlignmentVertical + N''',N''' + CAST(TextUnderlined AS NVARCHAR(255)) + N''',N''' + CAST(BorderToNext AS NVARCHAR(255)) + N''',N''' + CAST(BorderToPrevious AS NVARCHAR(255)) + N''';' FROM planning.thFormats WHERE FormatID IN (SELECT DISTINCT FormatID FROM DependendFormats WHERE LEN(FormatID)>0); INSERT INTO @ExportTable VALUES (8000,1, N'GO'); -- Final Value Output ################################################################################################################### SELECT --StepNumber, --activate only for bcp test output Command FROM @ExportTable ORDER BY StepNumber,RowNumber; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spEXPORT_ProductLine'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'EXPORT operation of a single productline, including,properties in gtables, global attributes, products, values, dependend lists, which have to be merged in the existing lists during import in two styles: one for import as sql script, one for Import over the GUI Import'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@OutputForSQLServer'; SET @value = N'OutputForSQLServer - no longer in use.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_AllProductListValues; GO /* GET Operation for all ListValues a product needs for data entry in ProductDataTable - contains the GUI defined lists of the products (defined by user c* or solution su*) - contains the dynamic system lists (sx* - which are editable by the user) - adds interal system lists (sx* - which are not visible on GUI) Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call - Must return Table and Code 200 DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_AllProductListValues @Username = 'SQL', @FactoryID = 'ZT', @ProductLineID = 'U', @ProductID = '1'; PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_AllProductListValues',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_AllProductListValues','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_AllProductListValues @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @ProductID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''''; DECLARE @EffectedRows INTEGER = 0; -- SET during Execution DECLARE @ResultCode INTEGER = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_AllProductListValues; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16 ,10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductID = @ProductID; IF @ProductKey = 0 OR @ProductLineKey = 0 OR @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 BEGIN SET @ResultCode = 401; RAISERROR('Invalid rights', 16, 10); END; -- Helper Table with all list values ;WITH list AS ( SELECT hL.ListID ,hL.Datentyp ,CAST(ISNULL(hLV.ValueInt , 0) AS INT) AS ValueInt -- must return int ! ,CAST(ISNULL(hLV.Scale , 0) AS INT) AS Scale -- must return int ! ,ISNULL(hLV.ValueText , N'') AS ValueText ,ISNULL(hLV.ValueComment , N'') AS ValueComment ,ISNULL(hLV.FormatID , N'') AS FormatID ,ISNULL(hF.BackgroundColor , N'') AS BackgroundColor ,ISNULL(hF.FontColor , N'') AS FontColor ,ISNULL(hF.ValueFormat , N'') AS ValueFormat FROM planning.thLists hL LEFT JOIN planning.thListValues hLV ON hL.ListID = hLV.ListID -- for the value LEFT JOIN planning.thFormats hF ON hLV.FormatID = hF.FormatID -- filter the lists, which where till 4.0.57 GUI defined and are now internal system lists WHERE hL.ListID NOT IN ( 'sxIsNumeric' ,'sxVisibilityLevel' ,'sxValueLists' ,'sxValueSources' ,'sxValueFormats' ,'sxScales' ,'sxValueEffects') ) -- STEP 3.1 Dynamic System Lists SELECT * FROM list WHERE ListID IN ('sxUnits','sxValueEffectParameters') UNION ALL -- STEP 3.2 Product specific ListSet based on defined ValueLists SELECT * FROM list WHERE ListID IN ( SELECT DISTINCT ValueListID FROM planning.tdValueSeries WHERE ProductKey = @ProductKey) -- add internal system lists ---------------------------------------------------------- UNION ALL --sxIsNumeric - static TODO: Change Datatyp to Integer with next API SELECT * FROM ( VALUES ('sxIsNumeric','String',0,1,'0','','','','','') ,('sxIsNumeric','String',1,1,'1','','','','','') ) tmp (ListID, Datentyp, ValueInt, Scale, ValueText, ValueComment, FormatID, BackgroundColor, FontColor, ValueFormat) UNION ALL --sxVisibilityLevel - static TODO: Change Datatyp to Integer with next API SELECT * FROM ( VALUES ('sxVisibilityLevel','String',0,1,'0','','','','','') ,('sxVisibilityLevel','String',1,1,'1','','','','','') ) tmp (ListID, Datentyp, ValueInt, Scale, ValueText, ValueComment, FormatID, BackgroundColor, FontColor, ValueFormat) UNION ALL --sxValueLists - dynamic List of GUI Lists SELECT 'sxValueLists' ,'String' ,0 AS ValueInt ,1 AS Scale ,ListID AS ValueText ,'' AS ValueComment ,'' AS FormatID ,'' AS BackgroundColor ,'' AS FontColor ,'' AS ValueFormat FROM planning.thLists -- Filter the GUI Lists, which are not useful as ValueList WHERE ListID NOT IN ('sxValueEffectParameter') UNION ALL --sxValueSources - static (english strings for all languages SELECT * FROM ( VALUES ('sxValueSources','String',0,1,'Input' ,'','','','','') ,('sxValueSources','String',0,1,'List-Strict','','','','','') ,('sxValueSources','String',0,1,'XLS' ,'','','','','') ,('sxValueSources','String',0,1,'XLS-Strict' ,'','','','','') ,('sxValueSources','String',0,1,'Link' ,'','','','','') ) tmp (ListID, Datentyp, ValueInt, Scale, ValueText, ValueComment, FormatID, BackgroundColor, FontColor, ValueFormat) UNION ALL --sxValueFormats - dynamic, list of defined formats SELECT 'sxValueFormats' ,'String' ,0 AS ValueInt ,1 AS Scale ,FormatID AS ValueText ,'' AS ValueComment ,FormatID AS FormatID ,BackgroundColor AS BackgroundColor ,FontColor AS FontColor ,ValueFormat AS ValueFormat FROM planning.thFormats UNION ALL --sxScales - static - this List is also static defined in GET_ListValues SELECT * FROM ( VALUES ('sxScales','Integer',1 ,1,'','','','','','') ,('sxScales','Integer',10 ,1,'','','','','','') ,('sxScales','Integer',100 ,1,'','','','','','') ,('sxScales','Integer',1000 ,1,'','','','','','') ,('sxScales','Integer',10000,1,'','','','','','') ) tmp (ListID, Datentyp, ValueInt, Scale, ValueText, ValueComment, FormatID, BackgroundColor, FontColor, ValueFormat) UNION ALL --sxValueEffects -dynamic, list of defined ValueEffects in gValueEffects SELECT 'sxValueEffects' ,'String' ,0 AS ValueInt ,1 AS Scale ,EffectID AS ValueText ,'' AS ValueComment ,'' AS FormatID ,'' AS BackgroundColor ,'' AS FontColor ,'' AS ValueFormat FROM planning.tgValueEffects ORDER BY ListID, ValueText, ValueInt; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_AllProductListValues; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_AllProductListValues; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_AllProductListValues'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for all ListValues a product needs for data entry in ProductDataTable containing the GUI defined lists of the products (defined by user c* or solution su*) and the dynamic system lists (sx* - which are editable by the user) and adds interal system lists (sx* - which are not visible on GUI)'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_ClusterAccessRight; GO /* GET operation for User status check on cluster If User has an registered Username and his status is 'Active' then Cluster Access Rights are granted. (which still can mean he sees nothing than an empty cluster) Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call -- Should Return 200 DECLARE @RC AS NVARCHAR(255) EXEC @RC = system.spGET_ClusterAccessRight 'SQL' PRINT @RC -- Should Return 403 DECLARE @RC AS NVARCHAR(255) EXEC @RC = system.spGET_ClusterAccessRight 'SQL4' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_ClusterAccessRight',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_ClusterAccessRight','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_ClusterAccessRight @Username NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution DECLARE @LogMessage NVARCHAR(MAX) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_ClusterAccessRight; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; SET @LogMessage = 'Transaction user don`t exists, check was done for Username: ' + @Username + ' with Orginal_Login(): ' + ORIGINAL_LOGIN(); RAISERROR(@LogMessage, 16, 10); END; -- STEP 3 - Test whether the user is Active on this cluster SELECT @ResultCode = IIF(Status = N'Active', 200, 401) FROM system.trUser WHERE UserName = @TransactUsername; IF @ResultCode <> 200 BEGIN SET @LogMessage = 'User is not active, check was done for Username: ' + @Username + ' with Orginal_Login(): ' + ORIGINAL_LOGIN(); RAISERROR(@LogMessage, 16, 10); END SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_ClusterAccessRight; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_ClusterAccessRight; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ClusterAccessRight'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET operation for User status check on cluster If User has an registered Username and his status is "Active" then Cluster Access Rights are granted. (which still can mean he sees nothing than an empty cluster)'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_ClusterProperties GO /* GET Operation for Cluster Properties Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call --should return 200 DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_ClusterProperties 'SQL' PRINT @RC --should return 403 DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_ClusterProperties 'SQL4' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ClusterProperties',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ClusterProperties','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_ClusterProperties @Username NVARCHAR(255) AS BEGIN SET NOCOUNT ON; SET ANSI_WARNINGS OFF; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @Right NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_ClusterProperties; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ClusterPropertiesReadRight @TransactUsername; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights', 16, 10); END ELSE BEGIN EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode = 200 BEGIN SET @Right = 'Write'; END ELSE BEGIN SET @Right = 'Read'; SET @ResultCode = 200; END; END; SET NOCOUNT OFF; SET ANSI_WARNINGS ON; -- STEP 3. Show public values SELECT PropertyID ,PropertyName ,CommentUser ,CommentDev ,Unit ,ValueText ,ValueInt ,Scale ,IsROSystemProperty ,FormatID ,@Right AS 'Right' FROM planning.tgCluster ORDER BY COALESCE(TRY_CAST(PropertyID AS INT), 999999999), PropertyID; SET @EffectedRows = @@ROWCOUNT; SET NOCOUNT ON; SET ANSI_WARNINGS OFF; COMMIT TRANSACTION sx_pf_GET_ClusterProperties; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_ClusterProperties; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ClusterProperties'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for Cluster Properties'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_ClusterProperty; GO /* GET Operation for determining a cluster Property Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_ClusterProperty @Username = 'SQL', @PropertyID = 'LS01' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ClusterProperty',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ClusterProperty','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_ClusterProperty @Username NVARCHAR(255), @PropertyID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @PropertyKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@PropertyID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @PropertyID IS NULL SET @PropertyID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_ClusterProperty; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @PropertyID = system.fProtectID (@PropertyID); IF @PropertyID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16 ,10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); -- Allow db_octservice Access IF EXISTS ( SELECT r.name from sys.database_role_members rm INNER JOIN sys.database_principals r on rm.role_principal_id = r.principal_id INNER JOIN sys.database_principals m on rm.member_principal_id = m.principal_id WHERE r.name = 'db_octservice' AND m.name = ORIGINAL_LOGIN() ) BEGIN SET @TransactUsername = 'db_octservice'; END; IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @PropertyKey = PropertyKey FROM planning.tgCluster WHERE PropertyID = @PropertyID; IF @PropertyKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ClusterPropertiesReadRight @TransactUsername; IF @TransactUsername = 'db_octservice' SET @ResultCode = 200 IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights', 16, 10); END; -- STEP 3. Show public values SELECT PropertyID ,PropertyName ,CommentUser ,CommentDev ,Unit ,ValueText ,ValueInt ,Scale ,IsROSystemProperty ,FormatID FROM planning.tgCluster WHERE PropertyKey = @PropertyKey; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_ClusterProperty; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_ClusterProperty; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ClusterProperty'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for determining a cluster Property'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PropertyID'; SET @value = N'PropertyID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_Factories; GO /* GET Operation for all eligible Factories Returns the list of all the factories, for which the user has rights Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call - should return Table and 200 DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_Factories 'SQL' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Factories',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Factories','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_Factories @Username NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_Factories; -- STEP 0.1 - Protect input parameters SET @Username = system.fProtectString (@Username); -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 3. Show public values ;WITH rights AS ( SELECT FactoryID, [Right], ISNULL(TRY_CAST(FactoryID AS INT), 999999999) AS orderID FROM system.trUserRights WHERE ProductLineID = N'' AND UserName = @TransactUsername AND [Right] IN (N'Read', N'Write') ) SELECT dF.FactoryID ,dF.NameShort ,dF.NameLong ,dF.CommentUser ,dF.CommentDev ,dF.ResponsiblePerson ,dF.ImageName ,vUR.[Right] FROM planning.tdFactories dF INNER JOIN rights vUR ON dF.FactoryID = vUR.FactoryID ORDER BY vUR.orderID, dF.FactoryID; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; COMMIT TRANSACTION sx_pf_GET_Factories; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_Factories; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Factories' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for all eligible Factories Returns the list of all the factories, for which the user has rights'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_Factory; GO /* GET Operation for one Factory Delivers all informations about one factory Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_Factory @Username = 'SQL', @FactoryID = 'ZT' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Factory',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Factory','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_Factory @Username NVARCHAR(255), @FactoryID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @Right NVARCHAR(10) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_Factory; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); IF @FactoryID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16 ,10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; IF @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername, @FactoryID; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_FactoryReadRight @TransactUsername, @FactoryID; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT FactoryID ,NameShort ,NameLong ,CommentUser ,CommentDev ,ResponsiblePerson ,ImageName ,@Right AS 'Right' FROM planning.tdFactories WHERE FactoryKey = @FactoryKey; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_Factory; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_Factory; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Factory' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for one Factory Delivers all informations about one factory'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_FactoryProperties; GO /* GET Operation for determining a Factory Properties Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_FactoryProperties @Username = 'SQL', @FactoryID = 'D' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_FactoryProperties',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_FactoryProperties','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_FactoryProperties @Username NVARCHAR(255), @FactoryID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @Right NVARCHAR(10) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_FactoryProperties; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); IF @FactoryID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16 ,10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.3 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; IF @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername, @FactoryID; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_FactoryReadRight @TransactUsername, @FactoryID; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT FactoryID ,PropertyID ,PropertyName ,CommentUser ,CommentDev ,Unit ,ValueText ,ValueInt ,Scale ,IsROSystemProperty ,FormatID ,@Right AS 'Right' FROM planning.tgFactories WHERE FactoryKey = @FactoryKey ORDER BY ISNULL(TRY_CAST(PropertyID AS INT), 999999999), PropertyID; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_FactoryProperties; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_FactoryProperties; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_FactoryProperties'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for determining a Factory Properties'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_Format; GO /* GET Operation for a Format Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_Format @Username = 'SQL', @FormatID = 'HintergrundRot' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Format',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Format','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_Format @Username NVARCHAR(255), @FormatID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FormatKey INT = 0; DECLARE @Right NVARCHAR(10) = ''; DECLARE @TransactUsername NVARCHAR(255) = ''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FormatID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FormatID IS NULL SET @FormatID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_Format; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FormatID = system.fProtectID (@FormatID); IF @FormatID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16 ,10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine Keys SELECT @FormatKey = FormatKey FROM planning.thFormats WHERE FormatID = @FormatID; IF @FormatKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_ClusterPropertiesReadRight @TransactUsername; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT FormatID, BackgroundColor, FontColor, ValueFormat, Bold, Italic, AlignmentHorizontal, AlignmentVertical, TextUnderlined, BorderToNext, BorderToPrevious, @Right AS 'Right' FROM planning.thFormats WHERE FormatKey = @FormatKey; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_Format; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_Format; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Format' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for a Format'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FormatID'; SET @value = N'FormatID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_Formats; GO /* GET Operation for all Formats in Cluster Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_Formats 'SQL' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Formats',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Formats','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_Formats @Username NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @Right NVARCHAR(10) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_Formats; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); -- STEP 1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_ClusterPropertiesReadRight @TransactUsername; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT FormatID, BackgroundColor, FontColor, ValueFormat, Bold, Italic, AlignmentHorizontal, AlignmentVertical, TextUnderlined, BorderToNext, BorderToPrevious, @Right AS 'Right' FROM planning.thFormats ORDER BY ISNULL(TRY_CAST(FormatID AS INT), 999999999), FormatID; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_Formats; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_Formats; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Formats' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for all Formats in Cluster'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_GlobalSearch; GO /* GET Operation for receiving full text search results for a given textstring Search will be done in all DataFactory Elements Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC AS NVARCHAR(255) --should return Data and 200 EXEC @RC = planning.spGET_GlobalSearch 'SQL','whixc' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_GlobalSearch',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_GlobalSearch','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_GlobalSearch @Username NVARCHAR(255) ,@SearchString NVARCHAR(255) -- any Substring from searched list values AS BEGIN SET NOCOUNT ON; DECLARE @SearchDoneFlag INT = 0; -- Logging Block DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@SearchString, N'NULL') + N''''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; -- add wildcards to SeachString SET @SearchString = '%' + ISNULL(@SearchString, '') + '%'; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_GlobalSearch; -- Protect input parameters SET @Username = system.fProtectString (@Username); -- Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Table for Structure Cache -- **************************************************************************************************************************************************** IF OBJECT_ID('tempdb..#Structure') IS NOT NULL DROP TABLE #Structure; CREATE TABLE #Structure ( RowKey BIGINT IDENTITY (1,1) NOT NULL ,FactoryKey BIGINT NOT NULL ,ProductLineKey BIGINT NOT NULL ,ProductKey BIGINT NOT NULL ,ValueSeriesKey BIGINT NOT NULL ,FactoryID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,FactoryNameShort NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineNameShort NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductNameShort NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesNameShort NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ) INSERT INTO #Structure -- must also return Factories / ProductLines without Products SELECT dF.FactoryKey ,ISNULL(dPL.ProductLineKey ,-1) AS ProductLineKey ,ISNULL(dP.ProductKey ,-1) AS ProductKey ,ISNULL(dVS.ValueSeriesKey ,-1) AS ValueSeriesKey ,dF.FactoryID ,ISNULL(dPL.ProductLineID ,'-1') AS ProductLineID ,ISNULL(dP.ProductID ,'-1') AS ProductID ,ISNULL(dVS.ValueSeriesID ,'-1') AS ValueSeriesID ,dF.NameShort AS FactoryNameShort ,ISNULL(dPL.NameShort ,'') AS ProductLineNameShort ,ISNULL(dP.NameShort ,'') AS ProductNameShort ,ISNULL(dVS.NameShort ,'') AS ValueSeriesNameShort FROM planning.tdFactories dF LEFT JOIN planning.tdProductLines dPL ON dF.FactoryKey = dPL.FactoryKey LEFT JOIN planning.tdProducts dP ON dPL.ProductLineKey = dP.ProductLineKey LEFT JOIN planning.tdValueSeries dVS ON dP.ProductKey = dVS.ProductKey -- Helper Table for GlobalAttributes -- **************************************************************************************************************************************************** IF OBJECT_ID('tempdb..#GA') IS NOT NULL DROP TABLE #GA; CREATE TABLE #GA ( RowKey BIGINT IDENTITY (1,1) NOT NULL ,ProductKey BIGINT NOT NULL ,FactoryID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,Content NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ) INSERT INTO #GA SELECT ProductKey ,FactoryID ,ProductLineID ,ProductID ,UnpivotTable.GlobalAttribute ,UnpivotTable.Content FROM planning.tdProducts UNPIVOT ( Content FOR GlobalAttribute IN ( GlobalAttribute1,GlobalAttribute2,GlobalAttribute3,GlobalAttribute4,GlobalAttribute5 ,GlobalAttribute6,GlobalAttribute7,GlobalAttribute8,GlobalAttribute9,GlobalAttribute10 ,GlobalAttribute11,GlobalAttribute12,GlobalAttribute13,GlobalAttribute14,GlobalAttribute15 ,GlobalAttribute16,GlobalAttribute17,GlobalAttribute18,GlobalAttribute19,GlobalAttribute20 ,GlobalAttribute21,GlobalAttribute22,GlobalAttribute23,GlobalAttribute24,GlobalAttribute25 ) ) AS UnpivotTable WHERE Content <>'' -- Table for Search Result Collection -- **************************************************************************************************************************************************** IF OBJECT_ID('tempdb..#SearchResult') IS NOT NULL DROP TABLE #SearchResult; CREATE TABLE #SearchResult ( RowKey BIGINT IDENTITY (1,1) NOT NULL ,FactoryID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,TimeID NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,Resultgroup NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,Resulttype NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ,DataFactoryPath NVARCHAR (1000) COLLATE DATABASE_DEFAULT NOT NULL ,Result NVARCHAR (255) COLLATE DATABASE_DEFAULT NOT NULL ) -- Getting Content -- **************************************************************************************************************************************************** -- Factories BEGIN ##### INSERT INTO #SearchResult SELECT dF.FactoryID ,'' ,'' ,'' ,'' ,'Factory' ,'FactoryID' ,dF.NameShort + ' (' + dF.FactoryID + ')' AS Path ,dF.FactoryID FROM planning.tdFactories dF WHERE dF.FactoryID LIKE @SearchString SET @EffectedRows += @@ROWCOUNT; INSERT INTO #SearchResult SELECT dF.FactoryID ,'' ,'' ,'' ,'' ,'Factory' ,'NameShort' ,dF.NameShort + ' (' + dF.FactoryID + ')' AS Path ,dF.NameShort FROM planning.tdFactories dF WHERE dF.NameShort LIKE @SearchString SET @EffectedRows += @@ROWCOUNT; INSERT INTO #SearchResult SELECT dF.FactoryID ,'' ,'' ,'' ,'' ,'Factory' ,'ResponsiblePerson' ,dF.NameShort + ' (' + dF.FactoryID + ')' AS Path ,dF.ResponsiblePerson FROM planning.tdFactories dF WHERE dF.ResponsiblePerson LIKE @SearchString SET @EffectedRows += @@ROWCOUNT; -- Factories END ##### -- ProductLines BEGIN ##### INSERT INTO #SearchResult SELECT dPL.FactoryID ,dPL.ProductLineID ,'' ,'' ,'' ,'ProductLine' ,'ProductLineID' ,S.FactoryNameShort + '>' + dPL.NameShort + ' (' + dPL.FactoryID + '>'+dPL.ProductLineID +')' AS Path ,dPL.ProductLineID FROM planning.tdProductLines dPL LEFT JOIN ( SELECT DISTINCT FactoryKey,FactoryNameShort FROM #Structure ) S ON dPL.FactoryKey = S.FactoryKey WHERE dPL.ProductLineID LIKE @SearchString INSERT INTO #SearchResult SELECT dPL.FactoryID ,dPL.ProductLineID ,'' ,'' ,'' ,'ProductLine' ,'NameShort' ,S.FactoryNameShort + '>' + dPL.NameShort + ' (' + dPL.FactoryID + '>'+dPL.ProductLineID +')' AS Path ,dPL.NameShort FROM planning.tdProductLines dPL LEFT JOIN ( SELECT DISTINCT FactoryKey,FactoryNameShort FROM #Structure ) S ON dPL.FactoryKey = S.FactoryKey WHERE dPL.NameShort LIKE @SearchString INSERT INTO #SearchResult SELECT dPL.FactoryID ,dPL.ProductLineID ,'' ,'' ,'' ,'ProductLine' ,'ResponsiblePerson' ,S.FactoryNameShort + '>' + dPL.NameShort + ' (' + dPL.FactoryID + '>'+dPL.ProductLineID +')' AS Path ,dPL.ResponsiblePerson FROM planning.tdProductLines dPL LEFT JOIN ( SELECT DISTINCT FactoryKey,FactoryNameShort FROM #Structure ) S ON dPL.FactoryKey = S.FactoryKey WHERE dPL.ResponsiblePerson LIKE @SearchString SET @EffectedRows += @@ROWCOUNT; -- ProductLines END ##### -- Products BEGIN #### INSERT INTO #SearchResult SELECT dP.FactoryID ,dP.ProductLineID ,dP.ProductID ,'' ,'' ,'Product' ,'ProductID' ,S.FactoryNameShort + '>' + S.ProductLineNameShort +'>'+ dP.NameShort + ' (' + dP.FactoryID + '>'+dP.ProductLineID + '>' + dP.ProductID + ')' AS Path ,dP.ProductID FROM planning.tdProducts dP LEFT JOIN ( SELECT DISTINCT ProductLineKey,FactoryNameShort,ProductLineNameShort FROM #Structure ) S ON dP.ProductLineKey = S.ProductLineKey WHERE dP.ProductID LIKE @SearchString INSERT INTO #SearchResult SELECT dP.FactoryID ,dP.ProductLineID ,dP.ProductID ,'' ,'' ,'Product' ,'NameShort' ,S.FactoryNameShort + '>' + S.ProductLineNameShort +'>'+ dP.NameShort + ' (' + dP.FactoryID + '>'+dP.ProductLineID + '>' + dP.ProductID + ')' AS Path ,dP.NameShort FROM planning.tdProducts dP LEFT JOIN ( SELECT DISTINCT ProductLineKey,FactoryNameShort,ProductLineNameShort FROM #Structure ) S ON dP.ProductLineKey = S.ProductLineKey WHERE dP.NameShort LIKE @SearchString INSERT INTO #SearchResult SELECT dP.FactoryID ,dP.ProductLineID ,dP.ProductID ,'' ,'' ,'Product' ,'ResponsiblePerson' ,S.FactoryNameShort + '>' + S.ProductLineNameShort +'>'+ dP.NameShort + ' (' + dP.FactoryID + '>'+dP.ProductLineID + '>' + dP.ProductID + ')' AS Path ,dP.ResponsiblePerson FROM planning.tdProducts dP LEFT JOIN ( SELECT DISTINCT ProductLineKey,FactoryNameShort,ProductLineNameShort FROM #Structure ) S ON dP.ProductLineKey = S.ProductLineKey WHERE dP.ResponsiblePerson LIKE @SearchString INSERT INTO #SearchResult SELECT dP.FactoryID ,dP.ProductLineID ,dP.ProductID ,'' ,'' ,'Product' ,'CommmentUser' ,S.FactoryNameShort + '>' + S.ProductLineNameShort +'>'+ dP.NameShort + ' (' + dP.FactoryID + '>'+dP.ProductLineID + '>' + dP.ProductID + ')' AS Path ,LEFT(dP.CommentUser,250)+' ...' FROM planning.tdProducts dP LEFT JOIN ( SELECT DISTINCT ProductLineKey,FactoryNameShort,ProductLineNameShort FROM #Structure ) S ON dP.ProductLineKey = S.ProductLineKey WHERE dP.CommentUser LIKE @SearchString INSERT INTO #SearchResult SELECT dP.FactoryID ,dP.ProductLineID ,dP.ProductID ,'' ,'' ,'Product' ,GA.GlobalAttribute ,S.FactoryNameShort + '>' + S.ProductLineNameShort +'>'+ dP.NameShort + ' (' + dP.FactoryID + '>'+dP.ProductLineID + '>' + dP.ProductID + ')' AS Path ,GA.Content FROM planning.tdProducts dP LEFT JOIN ( SELECT DISTINCT ProductLineKey,FactoryNameShort,ProductLineNameShort FROM #Structure ) S ON dP.ProductLineKey = S.ProductLineKey LEFT JOIN #GA GA ON dP.ProductKey = GA.ProductKey WHERE GA.Content LIKE @SearchString -- Documents on Products INSERT INTO #SearchResult SELECT tF.FactoryID ,tF.ProductLineID ,tF.ProductID ,'' ,'' ,'Product' ,'Documents' ,S.FactoryNameShort + '>' + S.ProductLineNameShort +'>'+ S.ProductNameShort + ' (' + tF.FactoryID + '>'+tF.ProductLineID + '>' + tF.ProductID + ')' AS Path ,tF.[FileName] FROM planning.tFiles tF LEFT JOIN ( SELECT DISTINCT ProductLineKey,FactoryNameShort,ProductLineNameShort,ProductNameShort FROM #Structure ) S ON tF.ProductLineKey = S.ProductLineKey WHERE tf.[FileName] LIKE @SearchString -- Products END #### -- ValueSeries Start #### INSERT INTO #SearchResult SELECT dVS.FactoryID ,dVS.ProductLineID ,dVS.ProductID ,dVS.ValueSeriesID ,'' ,'Product' ,'ValueSeriesID' ,S.FactoryNameShort + '>' + S.ProductLineNameShort +'>'+ S.ProductNameShort + '>' + dVS.NameShort + ' (' + dVS.FactoryID + '>'+dVS.ProductLineID + '>' + dVS.ProductID + '>' + dVS.ValueSeriesID + ')' AS Path ,dVS.ValueSeriesID FROM planning.tdValueSeries dVS LEFT JOIN ( SELECT DISTINCT ProductKey,FactoryNameShort,ProductLineNameShort,ProductNameShort FROM #Structure ) S ON dVS.ProductKey = S.ProductKey WHERE dVS.ValueSeriesID LIKE @SearchString INSERT INTO #SearchResult SELECT dVS.FactoryID ,dVS.ProductLineID ,dVS.ProductID ,dVS.ValueSeriesID ,'' ,'Product' ,'NameShort' ,S.FactoryNameShort + '>' + S.ProductLineNameShort +'>'+ S.ProductNameShort + '>' + dVS.NameShort + ' (' + dVS.FactoryID + '>'+dVS.ProductLineID + '>' + dVS.ProductID + '>' + dVS.ValueSeriesID + ')' AS Path ,dVS.NameShort FROM planning.tdValueSeries dVS LEFT JOIN ( SELECT DISTINCT ProductKey,FactoryNameShort,ProductLineNameShort,ProductNameShort FROM #Structure ) S ON dVS.ProductKey = S.ProductKey WHERE dVS.NameShort LIKE @SearchString INSERT INTO #SearchResult SELECT dVS.FactoryID ,dVS.ProductLineID ,dVS.ProductID ,dVS.ValueSeriesID ,'' ,'Product' ,'NameLong' ,S.FactoryNameShort + '>' + S.ProductLineNameShort +'>'+ S.ProductNameShort + '>' + dVS.NameShort + ' (' + dVS.FactoryID + '>'+dVS.ProductLineID + '>' + dVS.ProductID + '>' + dVS.ValueSeriesID + ')' AS Path ,dVS.NameLong FROM planning.tdValueSeries dVS LEFT JOIN ( SELECT DISTINCT ProductKey,FactoryNameShort,ProductLineNameShort,ProductNameShort FROM #Structure ) S ON dVS.ProductKey = S.ProductKey WHERE dVS.NameLong LIKE @SearchString INSERT INTO #SearchResult SELECT dVS.FactoryID ,dVS.ProductLineID ,dVS.ProductID ,dVS.ValueSeriesID ,'' ,'Product' ,'CommentUser' ,S.FactoryNameShort + '>' + S.ProductLineNameShort +'>'+ S.ProductNameShort + '>' + dVS.NameShort + ' (' + dVS.FactoryID + '>'+dVS.ProductLineID + '>' + dVS.ProductID + '>' + dVS.ValueSeriesID + ')' AS Path ,LEFT(dVS.CommentUser,250)+' ...' FROM planning.tdValueSeries dVS LEFT JOIN ( SELECT DISTINCT ProductKey,FactoryNameShort,ProductLineNameShort,ProductNameShort FROM #Structure ) S ON dVS.ProductKey = S.ProductKey WHERE dVS.CommentUser LIKE @SearchString -- ValueSeries END #### -- Value Start ### INSERT INTO #SearchResult SELECT fV.FactoryID ,fV.ProductLineID ,fV.ProductID ,fV.ValueSeriesID ,fV.TimeID ,'Values' ,'ValueText' ,S.FactoryNameShort + '>' + S.ProductLineNameShort +'>'+ S.ProductNameShort + '>' + S.ValueSeriesNameShort + '>' + CAST(fV.TimeID AS NVARCHAR(255)) +' (' + fV.FactoryID + '>'+ fV.ProductLineID + '>' + fV.ProductID + '>' + fV.ValueSeriesID + '>' + CAST(fV.TimeID AS NVARCHAR(255)) + ')' AS Path ,LEFT(fV.ValueText,250)+' ...' FROM planning.tfValues fV LEFT JOIN ( SELECT DISTINCT ValueSeriesKey,FactoryNameShort,ProductLineNameShort,ProductNameShort,ValueSeriesNameShort FROM #Structure ) S ON fV.ValueSeriesKey = S.ValueSeriesKey WHERE fV.ValueText LIKE @SearchString INSERT INTO #SearchResult SELECT fV.FactoryID ,fV.ProductLineID ,fV.ProductID ,fV.ValueSeriesID ,fV.TimeID ,'Values' ,'ValueComment' ,S.FactoryNameShort + '>' + S.ProductLineNameShort +'>'+ S.ProductNameShort + '>' + S.ValueSeriesNameShort + '>' + CAST(fV.TimeID AS NVARCHAR(255)) +' (' + fV.FactoryID + '>'+ fV.ProductLineID + '>' + fV.ProductID + '>' + fV.ValueSeriesID + '>' + CAST(fV.TimeID AS NVARCHAR(255)) + ')' AS Path ,LEFT(fV.ValueComment,250)+' ...' FROM planning.tfValues fV LEFT JOIN ( SELECT DISTINCT ValueSeriesKey,FactoryNameShort,ProductLineNameShort,ProductNameShort,ValueSeriesNameShort FROM #Structure ) S ON fV.ValueSeriesKey = S.ValueSeriesKey WHERE fV.ValueComment LIKE @SearchString -- Output SELECT TOP (100) sr.FactoryID ,sr.ProductLineID ,sr.ProductID ,sr.ValueSeriesID ,sr.Resultgroup ,sr.Resulttype ,sr.DataFactoryPath ,sr.Result FROM #SearchResult sr INNER JOIN ( SELECT * FROM system.trUserRights WHERE Username = @TransactUsername AND [Right] IN ('Write','Read') AND ( FactoryID <> '' AND ProductLineID <> '' OR FactoryID <> '' AND ProductLineID = '' ) ) vUR ON sr.FactoryID = vUR.FactoryID AND (sr.ProductLineID = vUR.ProductLineID OR sr.ProductLineID = '-1') -- as Output for not existing ProductLine possible ORDER BY CASE sr.Resultgroup WHEN 'Factory' THEN 1 WHEN 'ProductLine' THEN 2 WHEN 'Product' THEN 3 WHEN 'ValueSeries' THEN 4 WHEN 'Values' THEN 5 ELSE 99 END ,sr.Resulttype,sr.FactoryID,sr.ProductLineID,sr.ProductID,sr.ValueSeriesID SET @SearchDoneFlag = 1 SET @ResultCode = 200 COMMIT TRANSACTION sx_pf_GET_GlobalSearch; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_GlobalSearch; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_GlobalSearch'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for receiving full text search results for a given textstring Search will be done in all DataFactory Elements'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SearchString'; SET @value = N'SearchString'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_Info_AllGlobalAttributeUsage; GO /* GET Operation for cross query of ALL attributes usage Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_Info_AllGlobalAttributeUsage 'SQL' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Info_AllGlobalAttributeUsage',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Info_AllGlobalAttributeUsage','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_Info_AllGlobalAttributeUsage @Username NVARCHAR(255) AS BEGIN SET NOCOUNT ON; SET ANSI_WARNINGS OFF; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_Info_AllGlobalAtt; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); -- STEP 1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; SET NOCOUNT OFF; SET ANSI_WARNINGS ON; -- STEP 3. Show public values ;WITH dt AS ( SELECT GlobalAttributeAlias1, GlobalAttributeAlias2, GlobalAttributeAlias3, GlobalAttributeAlias4, GlobalAttributeAlias5, GlobalAttributeAlias6, GlobalAttributeAlias7, GlobalAttributeAlias8, GlobalAttributeAlias9, GlobalAttributeAlias10, GlobalAttributeAlias11, GlobalAttributeAlias12, GlobalAttributeAlias13, GlobalAttributeAlias14, GlobalAttributeAlias15, GlobalAttributeAlias16, GlobalAttributeAlias17, GlobalAttributeAlias18, GlobalAttributeAlias19, GlobalAttributeAlias20, GlobalAttributeAlias21, GlobalAttributeAlias22, GlobalAttributeAlias23, GlobalAttributeAlias24, GlobalAttributeAlias25 FROM planning.tdProductLines dPL LEFT JOIN system.trUserRights vUR ON dPL.FactoryID = vUR.FactoryID AND dPL.ProductLineID = vUR.ProductLineID AND vUR.[Right] IN (N'Read', N'Write') AND vUR.UserName = @TransactUsername ) , attrs AS ( SELECT DISTINCT Attribute, Value FROM (SELECT * FROM dt) pvt UNPIVOT ([Value] FOR Attribute IN ( GlobalAttributeAlias1, GlobalAttributeAlias2, GlobalAttributeAlias3, GlobalAttributeAlias4, GlobalAttributeAlias5, GlobalAttributeAlias6, GlobalAttributeAlias7, GlobalAttributeAlias8, GlobalAttributeAlias9, GlobalAttributeAlias10, GlobalAttributeAlias11, GlobalAttributeAlias12, GlobalAttributeAlias13, GlobalAttributeAlias14, GlobalAttributeAlias15, GlobalAttributeAlias16, GlobalAttributeAlias17, GlobalAttributeAlias18, GlobalAttributeAlias19, GlobalAttributeAlias20, GlobalAttributeAlias21, GlobalAttributeAlias22, GlobalAttributeAlias23, GlobalAttributeAlias24, GlobalAttributeAlias25) ) AS unpvt ) SELECT CAST(SUBSTRING(Attribute, 21, 2) AS INT) AS N, Value AS GlobalAttribute FROM attrs ORDER BY N, GlobalAttribute; SET @EffectedRows = @@ROWCOUNT; SET @Resultcode = 200; COMMIT TRANSACTION sx_pf_GET_Info_AllGlobalAtt; SET NOCOUNT ON; SET ANSI_WARNINGS OFF; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_Info_AllGlobalAtt; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Info_AllGlobalAttributeUsage'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for cross query of ALL attributes usage'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_NextFreeFactoryID; GO /* GET Operation for the next free FactoryID Returns the next numeric ID or the number 1 if other FactoryIDs are non-numeric Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_NextFreeFactoryID @Username = 'SQL' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_NextFreeFactoryID',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_NextFreeFactoryID','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_NextFreeFactoryID @Username NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FreeFactoryID NVARCHAR(255) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N'''' DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_Info_NextFreeFactoryID; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights', 16, 10) END; -- STEP 3.1 - Get last determined Numeric ID ;WITH ids AS ( SELECT TRY_CAST(FactoryID AS BIGINT) id FROM planning.tdFactories ) SELECT @FreeFactoryID = MAX(id) FROM ids WHERE id IS NOT NULL; -- STEP 3.2 - Increment ID IF @FreeFactoryID IS NOT NULL BEGIN SET @FreeFactoryID = @FreeFactoryID + 1; END ELSE BEGIN SET @FreeFactoryID = N'1'; END; -- STEP 3.3 - Safety check whether determined ID already exists IF EXISTS(SELECT FactoryID FROM planning.tdFactories WHERE FactoryID = @FreeFactoryID) BEGIN SET @FreeFactoryID = N''; END ELSE BEGIN SELECT @FreeFactoryID AS NextFreeFactoryID; END SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_Info_NextFreeFactoryID; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_Info_NextFreeFactoryID; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_NextFreeFactoryID'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for the next free FactoryID Returns the next numeric ID or the number 1 if other FactoryIDs are non-numeric'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_NextFreeProductID; GO /* GET Operation for the next free ProductID Returns for numeric ID the next number for ProductLines with only string IDs the number 1 Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_NextFreeProductID @Username = 'SQL', @FactoryID = 'ZT', @ProductLineID = 'V' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_NextFreeProductID',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_NextFreeProductID','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_NextFreeProductID @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @FreeProductID NVARCHAR(255) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_Info_NextFreeProductID; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); IF @FactoryID = N'' OR @ProductLineID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16 ,10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; IF @ProductLineKey = 0 OR @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights', 16, 10); END; -- STEP 3.1 - Get last determined Numeric ID ;WITH ids AS ( SELECT TRY_CAST(ProductID AS BIGINT) id FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey ) SELECT @FreeProductID = MAX(id) FROM ids WHERE id IS NOT NULL; -- STEP 3.2 - Increment ID IF @FreeProductID IS NOT NULL BEGIN SET @FreeProductID = @FreeProductID + 1; END ELSE BEGIN SET @FreeProductID = N'1'; END; -- STEP 3.3 - Safety check whether determined ID already exists IF EXISTS(SELECT ProductID FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @FreeProductID) BEGIN SET @FreeProductID = N''; END ELSE BEGIN SELECT @FreeProductID AS NextFreeProductID; END SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_Info_NextFreeProductID; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_Info_NextFreeProductID; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_NextFreeProductID'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for the next free ProductID Returns for numeric ID the next number for ProductLines with only string IDs the number 1 '; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_NextFreeProductLineID; GO /* GET Operation for the next free ProductLineID Returns the next numeric ID or the number 1 if other ProductLineIDs are non-numeric Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_NextFreeProductLineID @Username = 'SQL', @FactoryID = '1' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_NextFreeProductLineID',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_NextFreeProductLineID','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_NextFreeProductLineID @Username NVARCHAR(255), @FactoryID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @FreeProductLineID NVARCHAR(255) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N'''' DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_Info_NextFreePLID; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); IF @FactoryID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16 ,10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; IF @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername, @FactoryID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights', 16, 10) END; -- STEP 3.1 - Get last determined Numeric ID ;WITH ids AS ( SELECT TRY_CAST(ProductLineID AS BIGINT) id FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey ) SELECT @FreeProductLineID = MAX(id) FROM ids WHERE id IS NOT NULL; -- STEP 3.2 - Increment ID IF @FreeProductLineID IS NOT NULL BEGIN SET @FreeProductLineID = @FreeProductLineID + 1; END ELSE BEGIN SET @FreeProductLineID = N'1'; END; -- STEP 3.3 - Safety check whether determined ID already exists IF EXISTS(SELECT ProductLineID FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @FreeProductLineID) BEGIN SET @FreeProductLineID = N''; END ELSE BEGIN SELECT @FreeProductLineID AS NextFreeProductLineID; END SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_Info_NextFreePLID; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_Info_NextFreePLID; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_NextFreeProductLineID'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for the next free ProductLineID Returns the next numeric ID or the number 1 if other ProductLineIDs are non-numeric'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_List; GO /* GET Operation for a List Shows the properties of one List This procedure don't shows internal system lists Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_List @Username = 'SQL', @ListID = 'sxStatus'; PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_List',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_List','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_List @Username NVARCHAR(255), @ListID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @ListKey INT = 0; DECLARE @Right NVARCHAR(10) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@ListID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ListID IS NULL SET @ListID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_List; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @ListID = system.fProtectID (@ListID); IF @ListID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16 ,10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine Keys SELECT @ListKey = ListKey FROM planning.thLists WHERE ListID = @ListID; IF @ListKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_ClusterPropertiesReadRight @TransactUsername; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT ListID ,NameShort ,NameLong ,CommentDev ,CommentUser ,Datentyp ,Source ,SourceFormula ,FormatID ,@Right AS 'Right' FROM planning.thLists WHERE ListKey = @ListKey; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_List; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_List; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_List' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for a List Shows the properties of one List This procedure dont shows internal system lists'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ListID'; SET @value = N'ListID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_Lists GO /* GET Operation for all GUI Lists in Cluster Internal system lists are not shown Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_Lists 'SQL' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Lists',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Lists','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_Lists @Username NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @Right NVARCHAR(10) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_Lists; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); -- STEP 1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_ClusterPropertiesReadRight @TransactUsername; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT ListID ,NameShort ,NameLong ,CommentDev ,CommentUser ,Datentyp ,Source ,SourceFormula ,FormatID ,@Right AS 'Right' FROM planning.thLists ORDER BY ISNULL(TRY_CAST(ListID AS INT), 999999999), ListID; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_Lists; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_Lists; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Lists' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for all GUI Lists in Cluster Internal system lists are not shown'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_ListValues GO /* GET Operation for all List Values - shows values of all GUI Lists - shows values of the internal list "sxScales", as this list is need for the other lists Saxess Software GmbH Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_ListValues @Username = 'SQL', @ListID = 'sxEvents' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ListValues',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ListValues','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_ListValues @Username NVARCHAR(255), @ListID NVARCHAR(255) AS BEGIN DECLARE @ListKey INT = 0; DECLARE @Right NVARCHAR(10) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@ListID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ListID IS NULL SET @ListID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_ListValues; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @ListID = system.fProtectID (@ListID); IF @ListID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16 ,10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_ClusterPropertiesReadRight @TransactUsername; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; IF @ListID != 'sxScales' BEGIN -- STEP 1.2 - Determine Keys SELECT @ListKey = ListKey FROM planning.thLists WHERE ListID = @ListID; IF @ListKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 3. Show public values, always one dummy line, if list exists SELECT ISNULL(hLV.ListValueKey, 0) AS ListValueKey --returns the Key as the Listvalue has no ID ,hL.ListID ,hL.Datentyp ,ISNULL(hLV.ValueInt, 0) AS ValueInt ,ISNULL(hLV.Scale, 1) AS Scale ,ISNULL(hLV.ValueText, N'') AS ValueText ,ISNULL(hLV.ValueComment, N'') AS ValueComment ,ISNULL(hLV.FormatID, N'') AS FormatID ,@Right AS 'Right' FROM planning.thLists hL LEFT JOIN planning.thListValues hLV ON hL.ListID = hLV.ListID WHERE hL.ListID = @ListID ORDER BY ISNULL(TRY_CAST(hLV.ValueText AS INT), 999999999), hLV.ValueText, hLV.ValueInt; SET @EffectedRows = @@ROWCOUNT; END; -- static definition of sxScales to be loaded for list defintion IF @ListID = 'sxScales' BEGIN SELECT * FROM ( VALUES (1,'sxScales' ,'INT',1 ,1,'','','','Read') ,(2,'sxScales','INT',10 ,1,'','','','Read') ,(3,'sxScales','INT',100 ,1,'','','','Read') ,(4,'sxScales','INT',1000 ,1,'','','','Read') ,(5,'sxScales','INT',10000,1,'','','','Read') ) tmp (ListValueKey, ListID, Datentyp, ValueInt, Scale, ValueText, ValueComment, FormatID, [Right]) SET @EffectedRows = @@ROWCOUNT; END; COMMIT TRANSACTION sx_pf_GET_ListValues; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_ListValues; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ListValues'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for all List Values - shows values of all GUI Lists - shows values of the internal list "sxScales", as this list is need for the other lists'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ListID'; SET @value = N'ListID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_Product; GO /* GET Operation for Product Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_Product @Username = 'SQL', @FactoryID = 'ZT', @ProductLineID = 'U', @ProductID = '1' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Product',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Product','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_Product @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @ProductID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @Right NVARCHAR(10) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_Product; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16 ,10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID; IF @ProductKey = 0 OR @ProductLineKey = 0 OR @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT dP.ProductID ,dP.ProductLineID ,dP.FactoryID ,TimeType ,NameShort ,NameLong ,CommentUser ,CommentDev ,ResponsiblePerson ,ImageName ,[Status] ,Template ,TemplateVersion ,GlobalAttribute1 ,GlobalAttribute2 ,GlobalAttribute3 ,GlobalAttribute4 ,GlobalAttribute5 ,GlobalAttribute6 ,GlobalAttribute7 ,GlobalAttribute8 ,GlobalAttribute9 ,GlobalAttribute10 ,GlobalAttribute11 ,GlobalAttribute12 ,GlobalAttribute13 ,GlobalAttribute14 ,GlobalAttribute15 ,GlobalAttribute16 ,GlobalAttribute17 ,GlobalAttribute18 ,GlobalAttribute19 ,GlobalAttribute20 ,GlobalAttribute21 ,GlobalAttribute22 ,GlobalAttribute23 ,GlobalAttribute24 ,GlobalAttribute25 ,@Right AS 'Right' ,vUR.ReadCommentMandatory ,vUR.WriteCommentMandatory FROM planning.tdProducts dP LEFT JOIN system.trUserRights vUR ON dP.FactoryID = vUR.FactoryID AND dP.ProductLineID = vUR.ProductLineID AND vUR.UserName = @TransactUsername WHERE dP.ProductKey = @ProductKey; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_Product; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_Product; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Product' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for Product'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_ProductDataTableValues; GO /* GET Operation for all Values of ProductDataTable Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_ProductDataTableValues @Username = 'SQL', @FactoryID = 'ZT', @ProductLineID = 'U', @ProductID = '1'; PRINT @RC --should return 404 DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_ProductDataTableValues @Username = 'SQL', @FactoryID = 'ZT2', @ProductLineID = 'U', @ProductID = '1'; PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ProductDataTableValues',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ProductDataTableValues','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_ProductDataTableValues @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @ProductID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @Right NVARCHAR(10) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_ProductDataTableValues; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16 ,10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID; IF @ProductKey = 0 OR @ProductLineKey = 0 OR @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT fV.ProductID ,fV.ProductLineID ,fV.FactoryID ,fV.ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueText ,ValueComment ,@Right AS 'Right' FROM planning.tfValues fV LEFT JOIN planning.tdValueSeries dVS ON fV.ValueSeriesKey = dVS.ValueSeriesKey WHERE fV.ProductKey = @ProductKey ORDER BY ValueSeriesNo, TimeID; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_ProductDataTableValues; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_ProductDataTableValues; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ProductDataTableValues'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for all Values of ProductDataTable'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_ProductLine; GO /* GET Operation for ProductLine Provides complete information about a Product Line Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_ProductLine @Username = 'SQL', @FactoryID = 'ZT', @ProductLineID = 'U' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ProductLine',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ProductLine','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_ProductLine @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @Right NVARCHAR(10) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_ProductLine; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); IF @FactoryID = N'' OR @ProductLineID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16 ,10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; IF @ProductLineKey = 0 OR @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT ProductLineID ,FactoryID ,NameShort ,NameLong ,CommentUser ,CommentDev ,ResponsiblePerson ,ImageName ,DefaultTemplate ,GlobalAttributeSource1 ,GlobalAttributeAlias1 ,GlobalAttributeSource2 ,GlobalAttributeAlias2 ,GlobalAttributeSource3 ,GlobalAttributeAlias3 ,GlobalAttributeSource4 ,GlobalAttributeAlias4 ,GlobalAttributeSource5 ,GlobalAttributeAlias5 ,GlobalAttributeSource6 ,GlobalAttributeAlias6 ,GlobalAttributeSource7 ,GlobalAttributeAlias7 ,GlobalAttributeSource8 ,GlobalAttributeAlias8 ,GlobalAttributeSource9 ,GlobalAttributeAlias9 ,GlobalAttributeSource10 ,GlobalAttributeAlias10 ,GlobalAttributeSource11 ,GlobalAttributeAlias11 ,GlobalAttributeSource12 ,GlobalAttributeAlias12 ,GlobalAttributeSource13 ,GlobalAttributeAlias13 ,GlobalAttributeSource14 ,GlobalAttributeAlias14 ,GlobalAttributeSource15 ,GlobalAttributeAlias15 ,GlobalAttributeSource16 ,GlobalAttributeAlias16 ,GlobalAttributeSource17 ,GlobalAttributeAlias17 ,GlobalAttributeSource18 ,GlobalAttributeAlias18 ,GlobalAttributeSource19 ,GlobalAttributeAlias19 ,GlobalAttributeSource20 ,GlobalAttributeAlias20 ,GlobalAttributeSource21 ,GlobalAttributeAlias21 ,GlobalAttributeSource22 ,GlobalAttributeAlias22 ,GlobalAttributeSource23 ,GlobalAttributeAlias23 ,GlobalAttributeSource24 ,GlobalAttributeAlias24 ,GlobalAttributeSource25 ,GlobalAttributeAlias25 ,@Right AS 'Right' ,LayoutJSON FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_ProductLine; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_ProductLine; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ProductLine'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for ProductLine Provides complete information about a Product Line'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_ProductLineProperties; GO /* GET Operation for determination of all properties of ProductLine Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_ProductLineProperties @Username = 'SQL', @FactoryID = 'ZT', @ProductLineID = 'U' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ProductLineProperties',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ProductLineProperties','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_ProductLineProperties @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @Right NVARCHAR(10) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_ProductLineProperties; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); IF @FactoryID = N'' OR @ProductLineID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16 ,10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; IF @ProductLineKey = 0 OR @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT FactoryID ,ProductLineID ,PropertyID ,PropertyName ,CommentUser ,CommentDev ,Unit ,ValueText ,ValueInt ,Scale ,IsROSystemProperty ,FormatID ,@Right AS 'Right' FROM planning.tgProductLines WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey ORDER BY ISNULL(TRY_CAST(PropertyID AS INT), 999999999), PropertyID; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_ProductLineProperties; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_ProductLineProperties; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ProductLineProperties'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for determination of all properties of ProductLine'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_ProductLines; GO /* GET Operation for ProductLines A: returns the ProductLines for a single Factory B: returns all ProductLines when Factory '' is requested Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_ProductLines @Username = 'SQL', @FactoryID = 'ZT' PRINT @RC EXEC @RC = planning.spGET_ProductLines @Username = 'SQL' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ProductLines',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ProductLines','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_ProductLines @Username NVARCHAR(255), @FactoryID NVARCHAR(255) = '' AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N'' ; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_ProductLines; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys if single Factory is requested IF @FactoryID <> '' BEGIN SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; IF @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; END -- STEP 3. Show public values ;WITH vUR AS ( SELECT FactoryID, ProductLineID, [Right] FROM system.trUserRights WHERE ProductLineID <> N'' AND UserName = @TransactUsername AND [Right] IN (N'Read', N'Write') ) SELECT dPL.ProductLineID ,dPL.FactoryID ,dPL.NameShort ,dPL.NameLong ,dPL.CommentUser ,dPL.CommentDev ,dPL.ResponsiblePerson ,dPL.ImageName ,dPL.DefaultTemplate ,dPL.GlobalAttributeSource1 ,dPL.GlobalAttributeAlias1 ,dPL.GlobalAttributeSource2 ,dPL.GlobalAttributeAlias2 ,dPL.GlobalAttributeSource3 ,dPL.GlobalAttributeAlias3 ,dPL.GlobalAttributeSource4 ,dPL.GlobalAttributeAlias4 ,dPL.GlobalAttributeSource5 ,dPL.GlobalAttributeAlias5 ,dPL.GlobalAttributeSource6 ,dPL.GlobalAttributeAlias6 ,dPL.GlobalAttributeSource7 ,dPL.GlobalAttributeAlias7 ,dPL.GlobalAttributeSource8 ,dPL.GlobalAttributeAlias8 ,dPL.GlobalAttributeSource9 ,dPL.GlobalAttributeAlias9 ,dPL.GlobalAttributeSource10 ,dPL.GlobalAttributeAlias10 ,dPL.GlobalAttributeSource11 ,dPL.GlobalAttributeAlias11 ,dPL.GlobalAttributeSource12 ,dPL.GlobalAttributeAlias12 ,dPL.GlobalAttributeSource13 ,dPL.GlobalAttributeAlias13 ,dPL.GlobalAttributeSource14 ,dPL.GlobalAttributeAlias14 ,dPL.GlobalAttributeSource15 ,dPL.GlobalAttributeAlias15 ,dPL.GlobalAttributeSource16 ,dPL.GlobalAttributeAlias16 ,dPL.GlobalAttributeSource17 ,dPL.GlobalAttributeAlias17 ,dPL.GlobalAttributeSource18 ,dPL.GlobalAttributeAlias18 ,dPL.GlobalAttributeSource19 ,dPL.GlobalAttributeAlias19 ,dPL.GlobalAttributeSource20 ,dPL.GlobalAttributeAlias20 ,dPL.GlobalAttributeSource21 ,dPL.GlobalAttributeAlias21 ,dPL.GlobalAttributeSource22 ,dPL.GlobalAttributeAlias22 ,dPL.GlobalAttributeSource23 ,dPL.GlobalAttributeAlias23 ,dPL.GlobalAttributeSource24 ,dPL.GlobalAttributeAlias24 ,dPL.GlobalAttributeSource25 ,dPL.GlobalAttributeAlias25 ,vUR.[Right] ,dPL.LayoutJSON FROM planning.tdProductLines dPL INNER JOIN vUR ON dPL.FactoryID = vUR.FactoryID AND dPL.ProductLineID = vUR.ProductLineID WHERE dPL.FactoryKey = @FactoryKey OR @FactoryID = '' ORDER BY ISNULL(TRY_CAST(dPL.ProductLineID AS BIGINT), 999999999), dPL.ProductLineID; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; COMMIT TRANSACTION sx_pf_GET_ProductLines; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_ProductLines; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ProductLines'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for ProductLines A: returns the ProductLines for a single Factory B: returns all ProductLines when Factory '' is requested'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_ProductProperties; GO /* GET Operation for identifying all Properties of Products Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_ProductProperties @Username = 'SQL', @FactoryID = 'ZT', @ProductLineID = 'U', @ProductID = '1' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ProductProperties',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ProductProperties','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_ProductProperties @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @ProductID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @Right NVARCHAR(10) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_ProductProperties; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16 ,10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductID = @ProductID; IF @ProductKey = 0 OR @ProductLineKey = 0 OR @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT FactoryID ,ProductLineID ,PropertyID ,PropertyName ,CommentUser ,CommentDev ,Unit ,ValueText ,ValueInt ,Scale ,IsROSystemProperty ,FormatID ,@Right AS 'Right' FROM planning.tgProducts WHERE ProductKey = @ProductKey ORDER BY ISNULL(TRY_CAST(PropertyID AS INT), 999999999), PropertyID; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_ProductProperties; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_ProductProperties; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ProductProperties'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for identifying all Properties of Products'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_ProductRanges; GO /* Procedure to GET all ProductRanges for an Product Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Samplescript to POST an ProductRange EXEC planning.spPOST_ProductProperty @Username = 'SQL' ,@ProductID = '1' ,@ProductLineID = 'U' ,@FactoryID = 'ZT' ,@PropertyID = 'PDTRange_1' -- fixed Name PDTRange_1...N ,@PropertyName = 'PDTRangeProperty' -- fixed Name PDTRangeProperty ,@CommentUser = '' ,@CommentDev = '' ,@Unit = '' ,@ValueText = 'K1:20170115:K2:20171215' -- Range in DF Coordinates, separated with : ,@ValueInt = '1' -- Protected 1 = TRUE ,@Scale = '1' ,@IsROSystemproperty = '0' ,@FormatID = 'rngRed' -- Name of FormatID or '<#NV>' to set only Protection -- the Format Property Border* will be ignored on Cell Level EXEC planning.spPOST_ProductProperty @Username = 'SQL' ,@ProductID = '1' ,@ProductLineID = 'U' ,@FactoryID = 'ZT' ,@PropertyID = 'PDTRange_2' ,@PropertyName = 'PDTRangeProperty' ,@CommentUser = '' ,@CommentDev = '' ,@Unit = '' ,@ValueText = 'K3:20180615:K3:20190115' ,@ValueInt = '1' ,@Scale = '1' ,@IsROSystemproperty = '0' ,@FormatID = 'rngRed' Testcall DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_ProductRanges @Username = 'SQL', @FactoryID = 'ZT', @ProductLineID = 'U', @ProductID = '1' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ProductRanges',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ProductRanges','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_ProductRanges @Username NVARCHAR(255) ,@FactoryID NVARCHAR(255) ,@ProductLineID NVARCHAR(255) ,@ProductID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @ProductKey INT = 0; DECLARE @Right NVARCHAR(255) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; BEGIN TRY BEGIN TRANSACTION GET_ProductRanges; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16 ,10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine Keys SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND ProductID = @ProductID; IF @ProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT PropertyID ,ValueText AS DFRange ,ValueInt AS ProtectedFlag ,FormatID ,@Right AS 'Right' FROM planning.tgProducts WHERE ProductKey = @ProductKey AND PropertyID LIKE 'PDTRange_%'; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION GET_ProductRanges; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION GET_ProductRanges; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ProductRanges'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET all ProductRanges for an Product'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_Products; GO /* GET Operation for all Product of ProductLine Gerd Tautenhahn for Saxess Software GmbH Last modified: 08/2022 for OCT 5.8 Test call -- should return 200 and the Products of one ProductLine DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_Products @Username = 'SQL', @FactoryID = 'ZT', @ProductLineID = 'U' PRINT @RC -- should return 200 and the Products of one Factory DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_Products @Username = 'SQL', @FactoryID = 'ZT', @ProductLineID = '' PRINT @RC -- should return 200 and the Products of all Factories DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_Products @Username = 'SQL', @FactoryID = '', @ProductLineID = '' PRINT @RC -- should return 404 DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_Products @Username = 'SQL', @FactoryID = 'ZT', @ProductLineID = 'U1' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Products',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Products','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_Products @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_Products; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys - maybe 0 if ProductLineID or FactoryID is empty SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; -- STEP 3. Show public values SELECT dP.ProductID ,dP.ProductLineID ,dP.FactoryID ,dP.TimeType ,dP.NameShort ,dP.NameLong ,dP.CommentUser ,dP.CommentDev ,dP.ResponsiblePerson ,dP.ImageName ,dP.[Status] ,dP.Template ,dP.TemplateVersion ,dP.GlobalAttribute1 ,dP.GlobalAttribute2 ,dP.GlobalAttribute3 ,dP.GlobalAttribute4 ,dP.GlobalAttribute5 ,dP.GlobalAttribute6 ,dP.GlobalAttribute7 ,dP.GlobalAttribute8 ,dP.GlobalAttribute9 ,dP.GlobalAttribute10 ,dP.GlobalAttribute11 ,dP.GlobalAttribute12 ,dP.GlobalAttribute13 ,dP.GlobalAttribute14 ,dP.GlobalAttribute15 ,dP.GlobalAttribute16 ,dP.GlobalAttribute17 ,dP.GlobalAttribute18 ,dP.GlobalAttribute19 ,dP.GlobalAttribute20 ,dP.GlobalAttribute21 ,dP.GlobalAttribute22 ,dP.GlobalAttribute23 ,dP.GlobalAttribute24 ,dP.GlobalAttribute25 ,vUR.[Right] AS [Right] ,vUR.ReadCommentMandatory ,vUR.WriteCommentMandatory ,(SELECT COUNT(FileID) FROM planning.tFiles tF WHERE dP.FactoryID = tF.FactoryID AND dP.ProductLineID = tF.ProductLineID AND dP.ProductID = tF.ProductID) AS FileCount FROM planning.tdProducts dP LEFT JOIN system.trUserRights vUR ON dP.FactoryID = vUR.FactoryID AND dP.ProductLineID = vUR.ProductLineID AND vUR.UserName = @TransactUsername WHERE ( dP.ProductLineKey = @ProductLineKey OR (dP.FactoryID = @FactoryID AND @ProductLineID = '') OR (@FactoryID = '') ) AND vUR.[Right] IS NOT NULL ORDER BY dP.FactoryID ,dP.ProductLineID ,ISNULL(TRY_CAST(dP.ProductID AS INT), 999999999), dP.ProductID; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; COMMIT TRANSACTION sx_pf_GET_Products; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_Products; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Products' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for all Product of ProductLine'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_Statements; GO /* GET Operation for all Statements of Products Gerd Tautenhahn for Saxess Software GmbH Last modified: 08/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_Statements @Username = 'SQL', @FactoryID = 'ZT', @ProductLineID = 'U', @ProductID = '1'; PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Statements',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Statements','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_Statements @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @ProductID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @Right NVARCHAR(10) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_Statements; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductID = @ProductID; IF @ProductKey = 0 OR @ProductLineKey = 0 OR @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT ProductID ,ProductLineID ,FactoryID ,ActionType ,[Statement] ,UserName ,PCName ,ProcessorCode ,IPAddresses ,[Timestamp] ,IsDeleted ,IsResolved ,@Right AS 'Right' FROM planning.tfStatements WHERE ProductKey = @ProductKey ORDER BY [Timestamp] DESC; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_Statements; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_Statements; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Statements'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for all Statements of Products'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_TimeIDs; GO /* GET Operation for all TimeIDs of ValueSeries Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_TimeIDs @Username = 'SQL', @FactoryID = 'ZT', @ProductLineID = 'U', @ProductID = '1'; PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_TimeIDs',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_TimeIDs','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_TimeIDs @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @ProductID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_TimeIDs; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID; IF @ProductKey = 0 OR @ProductLineKey = 0 OR @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights', 16, 10); END; -- STEP 3. Show public values SELECT ProductID ,ProductLineID ,FactoryID ,TimeID ,FormatID FROM planning.tdTime WHERE ProductKey = @ProductKey ORDER BY TimeID; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_TimeIDs; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_TimeIDs; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_TimeIDs' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for all TimeIDs of ValueSeries'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_Value; GO /* GET Operation for a Value Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_Value @Username = 'SQL', @FactoryID = 'ZT', @ProductLineID = 'U', @ProductID = '1', @ValueSerieID = 'K1', @TimeID = 20180115 PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Value',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Value','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_Value @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @ProductID NVARCHAR(255), @ValueSerieID NVARCHAR(255), @TimeID INT AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @ValueSerieKey INT = 0; DECLARE @ValueKey INT = 0; DECLARE @Right NVARCHAR(10) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL')+ N''',''' + ISNULL(@FactoryID, N'NULL')+ N''',''' + ISNULL(@ProductLineID, N'NULL')+ N''',''' + ISNULL(@ProductID, N'NULL')+ N''',''' + ISNULL(@ValueSerieID, N'NULL')+ N''',''' + ISNULL(CAST(@TimeID AS NVARCHAR(10)), N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @ValueSerieID IS NULL SET @ValueSerieID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_Value; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); SET @ValueSerieID = system.fProtectID (@ValueSerieID); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' OR @ValueSerieID = N'' OR ISNULL(@TimeID, 0) <= 0 BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID; SELECT @ValueSerieKey = ValueSeriesKey FROM planning.tdValueSeries WHERE ProductKey = @ProductKey AND ValueSeriesID = @ValueSerieID; SELECT @ValueKey = ValueKey FROM planning.tfValues WHERE ValueSeriesKey = @ValueSerieKey AND TimeID = @TimeID; IF @ProductKey = 0 OR @ProductLineKey = 0 OR @FactoryKey = 0 OR @ValueSerieKey = 0 OR @ValueKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2.1 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueText ,ValueComment ,@Right AS 'Right' FROM planning.tfValues WHERE ValueKey = @ValueKey; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_Value; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_Value; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Value' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for a Value'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueSerieID'; SET @value = N'ValueSerieID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TimeID'; SET @value = N'TimeID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_ValueSerie; GO /* GET Operation for all ValueSerie Saxess Software GmbH Last modified: 01/2024 for OCT 5.10 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_ValueSerie @Username = 'SQL', @FactoryID = 'ZT', @ProductLineID = 'U', @ProductID = '1', @ValueSerieID = 'K1' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ValueSerie',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ValueSerie','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_ValueSerie @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @ProductID NVARCHAR(255), @ValueSerieID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @ValueSerieKey INT = 0; DECLARE @Right NVARCHAR(10) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID,N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''',''' + ISNULL(@ValueSerieID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @ValueSerieID IS NULL SET @ValueSerieID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_ValueSerie; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); SET @ValueSerieID = system.fProtectID (@ValueSerieID); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' OR @ValueSerieID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID; SELECT @ValueSerieKey = ValueSeriesKey FROM planning.tdValueSeries WHERE ProductKey = @ProductKey AND ValueSeriesID = @ValueSerieID; IF @ProductKey = 0 OR @ProductLineKey = 0 OR @FactoryKey = 0 OR @ValueSerieKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,ValueSeriesNo ,NameShort ,NameLong ,CommentUser ,CommentDev ,ImageName ,[IsNumeric] ,VisibilityLevel ,ValueSource ,ValueListID ,ValueFormatID ,Unit ,Scale ,Effect ,EffectParameter ,@Right AS 'Right' ,AllowZero FROM planning.tdValueSeries WHERE ValueSeriesKey = @ValueSerieKey; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_ValueSerie; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_ValueSerie; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ValueSerie'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for all ValueSerie'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueSerieID'; SET @value = N'ValueSerieID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_ValueSeries; GO /* GET Operation for all ValueSeries of Product Saxess Software GmbH Last modified: 01/2024 for OCT 5.10 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = planning.spGET_ValueSeries @Username = 'SQL', @FactoryID = 'ZT', @ProductLineID = 'U', @ProductID = '1' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ValueSeries',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_ValueSeries','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_ValueSeries @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @ProductLineID NVARCHAR(255), @ProductID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @Right NVARCHAR(10); DECLARE @TransactUsername NVARCHAR(255); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID,N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_GET_ValueSeries; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID; IF @ProductKey = 0 OR @ProductLineKey = 0 OR @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode = 200 BEGIN SET @Right = N'Read'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,ValueSeriesNo ,NameShort ,NameLong ,CommentUser ,CommentDev ,ImageName ,[IsNumeric] ,VisibilityLevel ,ValueSource ,ValueListID ,ValueFormatID ,Unit ,Scale ,Effect ,EffectParameter ,@Right AS 'Right' ,AllowZero FROM planning.tdValueSeries WHERE ProductKey = @ProductKey ORDER BY ValueSeriesNo; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_GET_ValueSeries; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_GET_ValueSeries; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ValueSeries'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for all ValueSeries of Product'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spMOVE_Factory; GO /* MOVE Operation for a Factory Factory moved by the update IDs without key change Saxess Software GmbH Last modified: 09/2023 for OCT 5.10 Test call 1. Empty source or target FactoryID => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User has no Rights => 401 Unauthorized 4. Not exists source Factory => 404 Not Found 5. Exists target Factory => 403 Forbidden 6. Factory moved => 200 OK 7. Don`t moved anything => 204 No content Testcall DECLARE @RC INT ,@Username NVARCHAR(255) ='SQL' ,@SourceFactoryID NVARCHAR(255) ='ZT' ,@TargetFactoryID NVARCHAR(255) ='ZT1'; EXECUTE @RC = planning.spMOVE_Factory @Username ,@SourceFactoryID ,@TargetFactoryID; PRINT @RC; SELECT TOP 20 * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spMOVE_Factory',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spMOVE_Factory','PARAMETER',NULL) */ CREATE PROCEDURE planning.spMOVE_Factory @Username NVARCHAR(255) ,@SourceFactoryID NVARCHAR(255) ,@TargetFactoryID NVARCHAR(255) WITH EXECUTE AS 'dbo' --cross access and data operation in system table AS BEGIN DECLARE @TransactUserKey INT = 0 ,@SourceFactoryKey INT = 0 ,@SourceFactoryNameShort NVARCHAR(255) = N'' ,@TargetFactoryKey INT = 0 ,@TransactUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@SourceFactoryID, N'NULL') + N''',''' + ISNULL(@TargetFactoryID, N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR (2000) = N''; -- NULL Protection IF @Username IS NULL SET @Username = N''; IF @SourceFactoryID IS NULL SET @SourceFactoryID = N''; IF @TargetFactoryID IS NULL SET @TargetFactoryID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_MOVE_Factory; -- Protect input parameters SET @Username = system.fProtectString (@Username); SET @SourceFactoryID = system.fProtectID (@SourceFactoryID); SET @TargetFactoryID = system.fProtectID (@TargetFactoryID); IF @SourceFactoryID = N'' OR @TargetFactoryID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- Determine SourceKeys SELECT @SourceFactoryKey = FactoryKey, @SourceFactoryNameShort = NameShort FROM planning.tdFactories WHERE FactoryID = @SourceFactoryID; IF @SourceFactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Source keys don`t exists', 16, 10); END; -- Determine TargetKeys SELECT @TargetFactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @TargetFactoryID; IF @TargetFactoryKey <> 0 BEGIN SET @ResultCode = 403; RAISERROR('Target Factory key already exists', 16, 10); END; -- MOVE DATA ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --dFactory UPDATE planning.tdFactories SET FactoryID = @TargetFactoryID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; IF @EffectedRows = 0 BEGIN SET @ResultCode = 204; END ELSE BEGIN --dProductLine UPDATE planning.tdProductLines SET FactoryID = @TargetFactoryID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; --dProduct UPDATE planning.tdProducts SET FactoryID = @TargetFactoryID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; --dValueSeries UPDATE planning.tdValueSeries SET FactoryID = @TargetFactoryID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; --dTime UPDATE planning.tdTime SET FactoryID = @TargetFactoryID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; --fValues UPDATE planning.tfValues SET FactoryID = @TargetFactoryID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; --gFactories UPDATE planning.tgFactories SET FactoryID = @TargetFactoryID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; --gProductLines UPDATE planning.tgProductLines SET FactoryID = @TargetFactoryID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; --gProducts UPDATE planning.tgProducts SET FactoryID = @TargetFactoryID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; --rRights UPDATE system.trRights -- therefore, EXECUTE AS dbo is needed SET FactoryID = @TargetFactoryID WHERE FactoryID = @SourceFactoryID; SET @EffectedRows += @@ROWCOUNT; --fStatements UPDATE planning.tfStatements SET FactoryID = @TargetFactoryID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; -- tFiles UPDATE planning.tFiles SET FactoryID = @TargetFactoryID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; -- tTabs UPDATE planning.tTabs SET FactoryID = @TargetFactoryID WHERE FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; -- tTabLayouts UPDATE planning.tTabLayouts SET FactoryID = @TargetFactoryID WHERE FactoryID = @SourceFactoryID; SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = IIF(@EffectedRows > 0, 201, 204); -- write action statement INSERT INTO planning.tfStatements ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ActionType ,Statement ,UserName ,PCName ,ProcessorCode ,IPAddresses ,Timestamp ,IsDeleted ,IsResolved ) SELECT ProductKey , dp.ProductLineKey , @TargetFactoryKey AS FactoryKey , ProductID , dp.ProductLineID , @TargetFactoryID AS FactoryID , N'MOVE' AS ActionType , N'Factory is moved from Factory <' + @SourceFactoryID + N'>"' + @SourceFactoryNameShort + N'" / ProductLine <' + dp.ProductLineID + N'>"' + ISNULL(dpl.NameShort, N'NULL') + N'" / Product <' + ProductID + N'>"' + dp.NameShort + N'"' AS [Statement] , @TransactUsername AS UserName , N'' AS PCName , N'' AS ProcessorCode , N'' AS IPAddresses , GETUTCDATE() AS [Timestamp] , 0 AS IsDeleted , 0 AS IsResolved FROM planning.tdProducts dp LEFT JOIN planning.tdProductLines dpl ON dp.ProductLineKey = dpl.ProductLineKey WHERE dp.FactoryKey = @SourceFactoryKey; SET @EffectedRows += @@ROWCOUNT; -- Rights materialization EXEC system.spMATERIALIZE_trUserRights; END; COMMIT TRANSACTION sx_pf_MOVE_Factory; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_MOVE_Factory; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spMOVE_Factory' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N''; -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'MOVE Operation for a Factory Factory moved by the update IDs without key change'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceFactoryID'; SET @value = N'SourceFactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetFactoryID'; SET @value = N'TargetFactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spMOVE_Format; GO /* Move Operation for a Format - all objects, which an FormatID are switched to use a new FormatID. Replace the OldFormatID with the new FormatID in planning.tdValueSeries Replace the OldFormatID with the new FormatID in planning.thListValues Delete the OldFormatID in planning.thFormats Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call 1. Empty source or target FormatID => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User has no Rights => 401 Unauthorized 4. Not exists source Format => 404 Not Found 5. Not exists target Format => 404 Not Found 6. Format moved => 200 OK 7. Don`t moved anything => 204 No content DECLARE @RC int DECLARE @Username AS NVARCHAR(255) = 'SQL' DECLARE @OldFormatID AS NVARCHAR(255)='' DECLARE @NewFormatID AS NVARCHAR(255)='' EXECUTE @RC = planning.spMOVE_Format @Username ,@OldFormatID ,@NewFormatID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spMOVE_Format',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spMOVE_Format','PARAMETER',NULL) */ CREATE PROCEDURE planning.spMOVE_Format @Username AS NVARCHAR(255), @OldFormatID AS NVARCHAR(255), @NewFormatID AS NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @OldFormatKey AS INT = 0; DECLARE @NewFormatKey AS INT = 0; DECLARE @TransactUsername AS NVARCHAR(255)= N''; DECLARE @ProcedureName AS NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString AS NVARCHAR (MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@OldFormatID, N'NULL') + N''',''' + ISNULL(@NewFormatID, N'NULL') + N''''; DECLARE @EffectedRows AS INT = 0; -- SET during Execution DECLARE @ResultCode AS INT = 501; -- SET during Execution DECLARE @TimestampCall AS DATETIME = GETUTCDATE(); DECLARE @Comment AS NVARCHAR (2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @OldFormatID IS NULL SET @OldFormatID = N''; IF @NewFormatID IS NULL SET @NewFormatID = N''; BEGIN TRY BEGIN TRANSACTION ONE; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @OldFormatID = system.fProtectID (@OldFormatID); SET @NewFormatID = system.fProtectID (@NewFormatID); IF (@OldFormatID = N'' OR @NewFormatID = N'') BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine SourceKeys SELECT @OldFormatKey = FormatKey FROM planning.thFormats WHERE FormatID = @OldFormatID; IF @OldFormatKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Source keys don`t exists', 16, 10); END; -- STEP 1.3 - Determine TargetKeys SELECT @NewFormatKey = FormatKey FROM planning.thFormats WHERE FormatID = @NewFormatID; IF @NewFormatKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Target keys don`t exists', 16, 10); END; -- STEP 3 - Move data UPDATE planning.tdValueSeries SET ValueFormatID = @NewFormatID WHERE ValueFormatID = @OldFormatID; SET @EffectedRows += @@ROWCOUNT; UPDATE planning.thListValues SET FormatID = @NewFormatID WHERE FormatID = @OldFormatID; SET @EffectedRows += @@ROWCOUNT; UPDATE planning.tdTime SET FormatID = @NewFormatID WHERE FormatID = @OldFormatID; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.thFormats WHERE FormatID = @OldFormatID; SET @EffectedRows += @@ROWCOUNT; IF @EffectedRows = 0 BEGIN SET @ResultCode = 204; END; COMMIT TRANSACTION ONE; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION ONE; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spMOVE_Format' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Move Operation for a Format - all objects, which an FormatID are switched to use a new FormatID. Replace the OldFormatID with the new FormatID in planning.tdValueSeries Replace the OldFormatID with the new FormatID in planning.thListValues Delete the OldFormatID in planning.thFormats'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@OldFormatID'; SET @value = N'OldFormatID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@NewFormatID'; SET @value = N'NewFormatID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spMOVE_List; GO /* Move Operation for a List The Procedure will change the OldListID into the NewListID in tables: planning.tdValueSeries, column ValueListID planning.tdProductLines, columns GlobalAttributeSource 1..25 create members from OldListID in NewListID if they don't exist there (for Lists of Type string compare and create ValueText, for Lists of Type Integer compare and create ValueInt) delete the OldListID in planning.thLists delete the Old List Values in planning.thListValues Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call 1. Empty source or target ListID => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User has no Rights => 401 Unauthorized 4. Not exists source List => 404 Not Found 5. Not exists target List => 404 Not Found 6. List moved => 200 OK 7. Don`t moved anything => 204 No content DECLARE @RC int DECLARE @Username AS NVARCHAR(255) = 'SQL' DECLARE @OldListID AS NVARCHAR(255)='suEvents' DECLARE @NewListID AS NVARCHAR(255)='suYesNo' EXECUTE @RC = planning.spMOVE_List @Username ,@OldListID ,@NewListID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spMOVE_List',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spMOVE_List','PARAMETER',NULL) */ CREATE PROCEDURE planning.spMOVE_List @Username AS NVARCHAR(255), @OldListID AS NVARCHAR(255), @NewListID AS NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @OldListKey AS INT = 0; DECLARE @NewListKey AS INT = 0; DECLARE @NewListType AS NVARCHAR(255)= N''; DECLARE @TransactUsername AS NVARCHAR(255)= N''; DECLARE @ProcedureName AS NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString AS NVARCHAR (MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@OldListID, N'NULL') + N''',''' + ISNULL(@NewListID, N'NULL') + N''''; DECLARE @EffectedRows AS INT = 0; -- SET during Execution DECLARE @ResultCode AS INT = 501; -- SET during Execution DECLARE @TimestampCall AS DATETIME = GETUTCDATE(); DECLARE @Comment AS NVARCHAR (2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @OldListID IS NULL SET @OldListID = N''; IF @NewListID IS NULL SET @NewListID = N''; BEGIN TRY BEGIN TRANSACTION ONE; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @OldListID = system.fProtectID (@OldListID); SET @NewListID = system.fProtectID (@NewListID); IF (@OldListID = N'' OR @NewListID = N'') BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END ELSE IF @OldListID IN ('sxIsNumeric', 'sxScales', 'sxValueEffects', 'sxValueLists', 'sxValueFormats', 'sxValueSources', 'sxVisibilityLevel') OR @NewListID IN ('sxIsNumeric', 'sxScales', 'sxValueEffects', 'sxValueLists', 'sxValueFormats', 'sxValueSources', 'sxVisibilityLevel') BEGIN SET @ResultCode = 403; RAISERROR('Invalid input parameters', 16, 10); END;; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine SourceKeys SELECT @OldListKey = ListKey FROM planning.thLists WHERE ListID = @OldListID; IF @OldListKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Source keys don`t exists', 16, 10); END; -- STEP 1.3 - Determine TargetKeys SELECT @NewListKey = ListKey, @NewListType = Datentyp FROM planning.thLists WHERE ListID = @NewListID; IF @NewListKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Target keys don`t exists', 16, 10); END; -- STEP 3 - Move data -- STEP 3.1 - Move not exists UNIQUE list members IF @NewListType = 'Integer' BEGIN ;WITH vals AS ( SELECT v1.ListValueKey , v1.ValueInt , v1.Scale , v1.ValueText , v1.ValueComment , v1.FormatID FROM planning.thListValues v1 LEFT JOIN planning.thListValues v2 ON v2.ListID = @NewListID AND v1.ValueInt = v2.ValueInt WHERE v1.ListID = @OldListID AND v2.ListID IS NULL ), uniq AS ( SELECT TOP 1 WITH TIES ListValueKey FROM vals ORDER BY ROW_NUMBER() OVER (PARTITION BY ValueInt ORDER BY ListValueKey) ) INSERT INTO planning.thListValues (ListID, ValueInt, Scale, ValueText, ValueComment, FormatID) SELECT @NewListID, ValueInt, Scale, ValueText, ValueComment, FormatID FROM vals INNER JOIN uniq ON vals.ListValueKey = uniq.ListValueKey; SET @EffectedRows += @@ROWCOUNT; END ELSE IF @NewListType = 'String' BEGIN ;WITH vals AS ( SELECT v1.ListValueKey , v1.ValueInt , v1.Scale , v1.ValueText , v1.ValueComment , v1.FormatID FROM planning.thListValues v1 LEFT JOIN planning.thListValues v2 ON v2.ListID = @NewListID AND v1.ValueText = v2.ValueText WHERE v1.ListID = @OldListID AND v2.ListID IS NULL ), uniq AS ( SELECT TOP 1 WITH TIES ListValueKey FROM vals ORDER BY ROW_NUMBER() OVER (PARTITION BY ValueText ORDER BY ListValueKey) ) INSERT INTO planning.thListValues (ListID, ValueInt, Scale, ValueText, ValueComment, FormatID) SELECT @NewListID, ValueInt, Scale, ValueText, ValueComment, FormatID FROM vals INNER JOIN uniq ON vals.ListValueKey = uniq.ListValueKey; SET @EffectedRows += @@ROWCOUNT; END; -- STEP 3.2 - Force all objects, which use a old list, to use a new list now UPDATE planning.tdValueSeries SET ValueListID = @NewListID WHERE ValueListID = @OldListID; SET @EffectedRows += @@ROWCOUNT; ;WITH lines AS ( SELECT ProductLineKey FROM planning.tdProductLines WHERE GlobalAttributeSource1 = @OldListID OR GlobalAttributeSource2 = @OldListID OR GlobalAttributeSource3 = @OldListID OR GlobalAttributeSource4 = @OldListID OR GlobalAttributeSource5 = @OldListID OR GlobalAttributeSource6 = @OldListID OR GlobalAttributeSource7 = @OldListID OR GlobalAttributeSource8 = @OldListID OR GlobalAttributeSource9 = @OldListID OR GlobalAttributeSource10 = @OldListID OR GlobalAttributeSource11 = @OldListID OR GlobalAttributeSource12 = @OldListID OR GlobalAttributeSource13 = @OldListID OR GlobalAttributeSource14 = @OldListID OR GlobalAttributeSource15 = @OldListID OR GlobalAttributeSource16 = @OldListID OR GlobalAttributeSource17 = @OldListID OR GlobalAttributeSource18 = @OldListID OR GlobalAttributeSource19 = @OldListID OR GlobalAttributeSource20 = @OldListID OR GlobalAttributeSource21 = @OldListID OR GlobalAttributeSource22 = @OldListID OR GlobalAttributeSource23 = @OldListID OR GlobalAttributeSource24 = @OldListID OR GlobalAttributeSource25 = @OldListID ) UPDATE pl SET GlobalAttributeSource1 = IIF(GlobalAttributeSource1 = @OldListID, @NewListID, GlobalAttributeSource1) , GlobalAttributeSource2 = IIF(GlobalAttributeSource2 = @OldListID, @NewListID, GlobalAttributeSource2) , GlobalAttributeSource3 = IIF(GlobalAttributeSource3 = @OldListID, @NewListID, GlobalAttributeSource3) , GlobalAttributeSource4 = IIF(GlobalAttributeSource4 = @OldListID, @NewListID, GlobalAttributeSource4) , GlobalAttributeSource5 = IIF(GlobalAttributeSource5 = @OldListID, @NewListID, GlobalAttributeSource5) , GlobalAttributeSource6 = IIF(GlobalAttributeSource6 = @OldListID, @NewListID, GlobalAttributeSource6) , GlobalAttributeSource7 = IIF(GlobalAttributeSource7 = @OldListID, @NewListID, GlobalAttributeSource7) , GlobalAttributeSource8 = IIF(GlobalAttributeSource8 = @OldListID, @NewListID, GlobalAttributeSource8) , GlobalAttributeSource9 = IIF(GlobalAttributeSource9 = @OldListID, @NewListID, GlobalAttributeSource9) , GlobalAttributeSource10 = IIF(GlobalAttributeSource10 = @OldListID, @NewListID, GlobalAttributeSource10) , GlobalAttributeSource11 = IIF(GlobalAttributeSource11 = @OldListID, @NewListID, GlobalAttributeSource11) , GlobalAttributeSource12 = IIF(GlobalAttributeSource12 = @OldListID, @NewListID, GlobalAttributeSource12) , GlobalAttributeSource13 = IIF(GlobalAttributeSource13 = @OldListID, @NewListID, GlobalAttributeSource13) , GlobalAttributeSource14 = IIF(GlobalAttributeSource14 = @OldListID, @NewListID, GlobalAttributeSource14) , GlobalAttributeSource15 = IIF(GlobalAttributeSource15 = @OldListID, @NewListID, GlobalAttributeSource15) , GlobalAttributeSource16 = IIF(GlobalAttributeSource16 = @OldListID, @NewListID, GlobalAttributeSource16) , GlobalAttributeSource17 = IIF(GlobalAttributeSource17 = @OldListID, @NewListID, GlobalAttributeSource17) , GlobalAttributeSource18 = IIF(GlobalAttributeSource18 = @OldListID, @NewListID, GlobalAttributeSource18) , GlobalAttributeSource19 = IIF(GlobalAttributeSource19 = @OldListID, @NewListID, GlobalAttributeSource19) , GlobalAttributeSource20 = IIF(GlobalAttributeSource20 = @OldListID, @NewListID, GlobalAttributeSource20) , GlobalAttributeSource21 = IIF(GlobalAttributeSource21 = @OldListID, @NewListID, GlobalAttributeSource21) , GlobalAttributeSource22 = IIF(GlobalAttributeSource22 = @OldListID, @NewListID, GlobalAttributeSource22) , GlobalAttributeSource23 = IIF(GlobalAttributeSource23 = @OldListID, @NewListID, GlobalAttributeSource23) , GlobalAttributeSource24 = IIF(GlobalAttributeSource24 = @OldListID, @NewListID, GlobalAttributeSource24) , GlobalAttributeSource25 = IIF(GlobalAttributeSource25 = @OldListID, @NewListID, GlobalAttributeSource25) FROM planning.tdProductLines pl INNER JOIN lines l ON pl.ProductLineKey = l.ProductLineKey; SET @EffectedRows += @@ROWCOUNT; -- STEP 3.3 - Remove old list DELETE FROM planning.thListValues WHERE ListID = @OldListID; SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.thLists WHERE ListKey = @OldListKey; SET @EffectedRows += @@ROWCOUNT; IF @EffectedRows = 0 BEGIN SET @ResultCode = 204; END; COMMIT TRANSACTION ONE; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION ONE; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spMOVE_List' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Move Operation for a List The Procedure will change the OldListID into the NewListID in tables: planning.tdValueSeries, column ValueListID planning.tdProductLines, columns GlobalAttributeSource 1..25 create members from OldListID in NewListID if they dont exist there (for Lists of Type string compare and create ValueText, for Lists of Type Integer compare and create ValueInt) delete the OldListID in planning.thLists delete the Old List Values in planning.thListValues'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@OldListID'; SET @value = N'OldListID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@NewListID'; SET @value = N'NewListID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spMOVE_Product; GO /* Move Operation for a Product Product moved by ID update, the Key of the Product keeps the same Gerd Tautenhahn for Saxess Software GmbH Last modified: 05/2023 for OCT 5.9 Test call 1. Empty source or target FactoryID / ProductLineID / ProductID => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User has no Rights => 401 Unauthorized 4. Not exists source Factory / ProductLine / Product => 404 Not Found 5. Not exists target Factory / ProductLine => 404 6. Exists TargetProduct => 403 Forbidden 7. Product moved => 200 OK 8. Don`t moved anything => 204 No content DECLARE @RC int ,@Username NVARCHAR(255) = 'SQL' ,@SourceFactoryID NVARCHAR(255) ='ZT' ,@SourceProductLineID NVARCHAR(255) ='U' ,@SourceProductID NVARCHAR(255) ='1' ,@TargetFactoryID NVARCHAR(255) ='ZT' ,@TargetProductLineID NVARCHAR(255) ='U' ,@TargetProductID NVARCHAR(255) ='RZ888'; EXECUTE @RC = planning.spMOVE_Product @Username ,@SourceFactoryID ,@SourceProductLineID ,@SourceProductID ,@TargetFactoryID ,@TargetProductLineID ,@TargetProductID; PRINT @RC; SELECT TOP 5 * FROM system.tAPILog; SELECT * FROM planning.tdProducts; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spMOVE_Product',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spMOVE_Product','PARAMETER',NULL) */ CREATE PROCEDURE planning.spMOVE_Product @Username NVARCHAR(255), @SourceFactoryID NVARCHAR(255), @SourceProductLineID NVARCHAR(255), @SourceProductID NVARCHAR(255), @TargetFactoryID NVARCHAR(255), @TargetProductLineID NVARCHAR(255), @TargetProductID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @SourceFactoryKey INT = 0 ,@SourceProductKey INT = 0 ,@SourceProductLineKey INT = 0 ,@TargetProductKey INT = 0 ,@SourceFactoryNameShort NVARCHAR(255) = N'' ,@SourceProductLineNameShort NVARCHAR(255) = N'' ,@SourceProductNameShort NVARCHAR(255) = N'' ,@TargetFactoryKey INT = 0 ,@TargetProductLineKey INT = 0 ,@TransactUsername NVARCHAR(255)= N'' ,@ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString AS NVARCHAR (MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@SourceFactoryID, N'NULL') + N''',''' + ISNULL(@SourceProductLineID, N'NULL') + N''',''' + ISNULL(@SourceProductID, N'NULL') + N''',''' + ISNULL(@TargetFactoryID, N'NULL') + N''',''' + ISNULL(@TargetProductLineID, N'NULL') + N''',''' + ISNULL(@TargetProductID, N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR (2000) = N''; -- NULL Protection IF @Username IS NULL SET @Username = N''; IF @SourceFactoryID IS NULL SET @SourceFactoryID = N''; IF @SourceProductLineID IS NULL SET @SourceProductLineID = N''; IF @SourceProductID IS NULL SET @SourceProductID = N''; IF @TargetFactoryID IS NULL SET @TargetFactoryID = N''; IF @TargetProductLineID IS NULL SET @TargetProductLineID = N''; IF @TargetProductID IS NULL SET @TargetProductID = N''; BEGIN TRY BEGIN TRANSACTION MOVE_Product; -- Protect input parameters SET @Username = system.fProtectString (@Username); SET @SourceFactoryID = system.fProtectID (@SourceFactoryID); SET @SourceProductLineID = system.fProtectID (@SourceProductLineID); SET @SourceProductID = system.fProtectID (@SourceProductID); SET @TargetFactoryID = system.fProtectID (@TargetFactoryID); SET @TargetProductLineID = system.fProtectID (@TargetProductLineID); SET @TargetProductID = system.fProtectID (@TargetProductID); IF (@SourceFactoryID = N'' OR @SourceProductLineID = N'' OR @SourceProductID = N'' OR @TargetFactoryID = N'' OR @TargetProductLineID = N'' OR @TargetProductID = N'') BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @TargetFactoryID, @TargetProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights on target - no write rights.', 16, 10); EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @SourceFactoryID, @SourceProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights on source - no write rights.', 16, 10); -- STEP 1.2 - Determine SourceKeys SELECT @SourceFactoryKey = FactoryKey, @SourceFactoryNameShort = NameShort FROM planning.tdFactories WHERE FactoryID = @SourceFactoryID; SELECT @SourceProductLineKey = ProductLineKey, @SourceProductLineNameShort = NameShort FROM planning.tdProductLines WHERE FactoryKey = @SourceFactoryKey AND ProductLineID = @SourceProductLineID; SELECT @SourceProductKey = ProductKey, @SourceProductNameShort = NameShort FROM planning.tdProducts WHERE FactoryKey = @SourceFactoryKey AND ProductLineKey = @SourceProductLineKey AND ProductID = @SourceProductID; IF @SourceFactoryKey = 0 OR @SourceProductLineKey = 0 OR @SourceProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Source keys don`t exists', 16, 10); END; -- STEP 1.3 - Determine TargetKeys SELECT @TargetFactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @TargetFactoryID; SELECT @TargetProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @TargetFactoryKey AND ProductLineID = @TargetProductLineID; SELECT @TargetProductKey = ProductKey FROM planning.tdProducts WHERE FactoryKey = @TargetFactoryKey AND ProductLineKey = @TargetProductLineKey AND ProductID = @TargetProductID; IF @TargetFactoryKey = 0 OR @TargetProductLineKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Target keys don`t exists', 16, 10); END; IF @TargetProductKey <> 0 BEGIN SET @ResultCode = 403; RAISERROR('Target Product key already exists', 16, 10); END; -- STEP 3 - Move data --dProduct UPDATE planning.tdProducts SET FactoryKey = @TargetFactoryKey ,ProductLineKey = @TargetProductLineKey ,FactoryID = @TargetFactoryID ,ProductLineID = @TargetProductLineID ,ProductID = @TargetProductID WHERE ProductKey = @SourceProductKey; SET @EffectedRows += @@ROWCOUNT; IF @EffectedRows = 0 BEGIN SET @ResultCode = 204; END ELSE BEGIN --gProducts UPDATE planning.tgProducts SET FactoryKey = @TargetFactoryKey ,ProductLineKey = @TargetProductLineKey ,FactoryID = @TargetFactoryID ,ProductLineID = @TargetProductLineID ,ProductID = @TargetProductID WHERE ProductKey = @SourceProductKey; SET @EffectedRows += @@ROWCOUNT; --dValueSeries UPDATE planning.tdValueSeries SET FactoryKey = @TargetFactoryKey ,ProductLineKey = @TargetProductLineKey ,FactoryID = @TargetFactoryID ,ProductLineID = @TargetProductLineID ,ProductID = @TargetProductID WHERE ProductKey = @SourceProductKey; SET @EffectedRows += @@ROWCOUNT; --dTime UPDATE planning.tdTime SET FactoryKey = @TargetFactoryKey ,ProductLineKey = @TargetProductLineKey ,FactoryID = @TargetFactoryID ,ProductLineID = @TargetProductLineID ,ProductID = @TargetProductID WHERE ProductKey = @SourceProductKey; SET @EffectedRows += @@ROWCOUNT; --fValues UPDATE planning.tfValues SET FactoryKey = @TargetFactoryKey ,ProductLineKey = @TargetProductLineKey ,FactoryID = @TargetFactoryID ,ProductLineID = @TargetProductLineID ,ProductID = @TargetProductID WHERE ProductKey = @SourceProductKey; SET @EffectedRows += @@ROWCOUNT; --fStatements UPDATE planning.tfStatements SET FactoryKey = @TargetFactoryKey ,ProductLineKey = @TargetProductLineKey ,FactoryID = @TargetFactoryID ,ProductLineID = @TargetProductLineID ,ProductID = @TargetProductID WHERE ProductKey = @SourceProductKey; SET @EffectedRows += @@ROWCOUNT; -- tFiles UPDATE planning.tFiles SET FactoryKey = @TargetFactoryKey ,ProductLineKey = @TargetProductLineKey ,FactoryID = @TargetFactoryID ,ProductLineID = @TargetProductLineID ,ProductID = @TargetProductID WHERE ProductKey = @SourceProductKey; -- write action statement INSERT INTO planning.tfStatements ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ActionType ,Statement ,UserName ,PCName ,ProcessorCode ,IPAddresses ,Timestamp ,IsDeleted ,IsResolved ) SELECT @SourceProductKey AS ProductKey ,@TargetProductLineKey AS ProductLineKey ,@TargetFactoryKey AS FactoryKey ,@TargetProductID AS ProductID ,@TargetProductLineID AS ProductLineID ,@TargetFactoryID AS FactoryID ,N'MOVE' AS ActionType ,N'Product is moved from Factory <' + @SourceFactoryID + N'>"' + @SourceFactoryNameShort + N'"/ ProductLine <' + @SourceProductLineID + N'>"' + @SourceProductLineNameShort + N'"/ Product <' + @SourceProductID + N'>"' + @SourceProductNameShort + N'"' AS [Statement] ,@TransactUsername AS UserName ,N'' AS PCName ,N'' AS ProcessorCode ,N'' AS IPAddresses ,GETUTCDATE() AS [Timestamp] ,0 AS IsDeleted ,0 AS IsResolved; SET @EffectedRows += @@ROWCOUNT; END; COMMIT TRANSACTION MOVE_Product; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION MOVE_Product; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spMOVE_Product' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Move Operation for a Product Product moved by ID update'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceFactoryID'; SET @value = N'SourceFactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceProductLineID'; SET @value = N'SourceProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceProductID'; SET @value = N'SourceProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetFactoryID'; SET @value = N'TargetFactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetProductLineID'; SET @value = N'TargetProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetProductID'; SET @value = N'TargetProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spMOVE_ProductLine; GO /* MOVE Operation for a ProductLine ProductLine moved by the update IDs without key change Saxess Software GmbH Last modified: 09/2023 for OCT 5.10 Test call 1. Empty source or target FactoryID / ProductLineID => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User has no Rights => 401 Unauthorized 4. Not exists source Factory / ProductLine => 404 Not Found 5. Not exists target Factory und existierende Target ProductLine => 404 Not Found 6. Target ValueSerie key already exists => 403 Forbidden 7. ProductLine moved => 200 OK 8. Don`t moved anything => 204 No content Testcall DECLARE @RC INT ,@Username NVARCHAR(255) =N'SQL' ,@SourceFactoryID NVARCHAR(255) =N'ZT' ,@SourceProductLineID NVARCHAR(255) =N'U' ,@TargetFactoryID NVARCHAR(255) =N'ZT' ,@TargetProductLineID NVARCHAR(255) =N'U2'; EXECUTE @RC = planning.spMOVE_ProductLine @Username ,@SourceFactoryID ,@SourceProductLineID ,@TargetFactoryID ,@TargetProductLineID; PRINT @RC; SELECT TOP 20 * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spMOVE_ProductLine',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spMOVE_ProductLine','PARAMETER',NULL) */ CREATE PROCEDURE planning.spMOVE_ProductLine @Username NVARCHAR(255) ,@SourceFactoryID NVARCHAR(255) ,@SourceProductLineID NVARCHAR(255) ,@TargetFactoryID NVARCHAR(255) ,@TargetProductLineID NVARCHAR(255) WITH EXECUTE AS 'dbo' -- due to moving the Productline in the system tables / cross action on system schema AS BEGIN SET NOCOUNT ON; DECLARE @TransactUserKey INT = 0 ,@SourceFactoryKey INT = 0 ,@SourceProductLineKey INT = 0 ,@SourceFactoryNameShort NVARCHAR(255) = N'' ,@SourceProductLineNameShort NVARCHAR(255) = N'' ,@TargetFactoryKey INT = 0 ,@TargetProductLineKey INT = 0 ,@TransactUsername NVARCHAR(255)= N'' ,@ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@SourceFactoryID ,N'NULL') + N''',''' + ISNULL(@SourceProductLineID ,N'NULL') + N''',''' + ISNULL(@TargetFactoryID ,N'NULL') + N''',''' + ISNULL(@TargetProductLineID ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N''; -- NULL Protection IF @Username IS NULL SET @Username = N''; IF @SourceFactoryID IS NULL SET @SourceFactoryID = N''; IF @SourceProductLineID IS NULL SET @SourceProductLineID = N''; IF @TargetFactoryID IS NULL SET @TargetFactoryID = N''; IF @TargetProductLineID IS NULL SET @TargetProductLineID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_MOVE_ProductLine; -- Protect input parameters SET @Username = system.fProtectString (@Username); SET @SourceFactoryID = system.fProtectID (@SourceFactoryID); SET @SourceProductLineID = system.fProtectID (@SourceProductLineID); SET @TargetFactoryID = system.fProtectID (@TargetFactoryID); SET @TargetProductLineID = system.fProtectID (@TargetProductLineID); IF @SourceFactoryID = N'' OR @SourceProductLineID = N'' OR @TargetFactoryID = N'' OR @TargetProductLineID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Check rights EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername, @TargetFactoryID; IF @ResultCode <> 200 RAISERROR('Invalid rights on target - no write rights.', 16, 10); EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername, @SourceFactoryID; IF @ResultCode <> 200 RAISERROR('Invalid rights on source - no write rights.', 16, 10); -- Determine SourceKeys SELECT @SourceFactoryKey = FactoryKey, @SourceFactoryNameShort = NameShort FROM planning.tdFactories WHERE FactoryID = @SourceFactoryID; SELECT @SourceProductLineKey = ProductLineKey, @SourceProductLineNameShort = NameShort FROM planning.tdProductLines WHERE FactoryKey = @SourceFactoryKey AND ProductLineID = @SourceProductLineID; IF @SourceFactoryKey = 0 OR @SourceProductLineKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Source keys don`t exists', 16, 10); END; -- Determine TargetKeys SELECT @TargetFactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @TargetFactoryID; SELECT @TargetProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @TargetFactoryKey AND ProductLineID = @TargetProductLineID; IF @TargetFactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Target keys don`t exists', 16, 10); END; IF @TargetProductLineKey <> 0 BEGIN SET @ResultCode = 403; RAISERROR('Target ProductLine key already exists', 16, 10); END; -- MOVE DATA +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --dProductLine UPDATE planning.tdProductLines SET FactoryKey = @TargetFactoryKey ,ProductLineID = @TargetProductLineID ,FactoryID = @TargetFactoryID WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; IF @EffectedRows = 0 BEGIN SET @ResultCode = 204; END ELSE BEGIN --dProduct UPDATE planning.tdProducts SET FactoryKey = @TargetFactoryKey ,ProductLineID = @TargetProductLineID ,FactoryID = @TargetFactoryID WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; --dValueSeries UPDATE planning.tdValueSeries SET FactoryKey = @TargetFactoryKey ,ProductLineID = @TargetProductLineID ,FactoryID = @TargetFactoryID WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; --dTime UPDATE planning.tdTime SET FactoryKey = @TargetFactoryKey ,ProductLineID = @TargetProductLineID ,FactoryID = @TargetFactoryID WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; --fValues UPDATE planning.tfValues SET FactoryKey = @TargetFactoryKey ,ProductLineID = @TargetProductLineID ,FactoryID = @TargetFactoryID WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; --gProductLines UPDATE planning.tgProductLines SET FactoryKey = @TargetFactoryKey ,ProductLineID = @TargetProductLineID ,FactoryID = @TargetFactoryID WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; --gProducts UPDATE planning.tgProducts SET FactoryKey = @TargetFactoryKey ,ProductLineID = @TargetProductLineID ,FactoryID = @TargetFactoryID WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; --rRights UPDATE system.trRights -- this makes execute as dbo necessary ! SET FactoryID = @TargetFactoryID ,ProductLineID = @TargetProductLineID WHERE FactoryID = @SourceFactoryID AND ProductLineID = @SourceProductLineID; SET @EffectedRows += @@ROWCOUNT; --fStatements UPDATE planning.tfStatements SET FactoryKey = @TargetFactoryKey ,ProductLineID = @TargetProductLineID ,FactoryID = @TargetFactoryID WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; -- tFiles UPDATE planning.tFiles SET FactoryKey = @TargetFactoryKey ,FactoryID = @TargetFactoryID ,ProductLineID = @TargetProductLineID WHERE ProductLineKey = @SourceProductLineKey; INSERT INTO planning.tfStatements ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ActionType ,Statement ,UserName ,PCName ,ProcessorCode ,IPAddresses ,Timestamp ,IsDeleted ,IsResolved ) SELECT ProductKey ,@SourceProductLineKey AS ProductLineKey ,@TargetFactoryKey AS FactoryKey ,ProductID ,@TargetProductLineID AS ProductLineID ,@TargetFactoryID AS FactoryID ,N'MOVE' AS ActionType ,N'ProductLine is moved from Factory <' + @SourceFactoryID + N'>"' + @SourceFactoryNameShort + N'"/ ProductLine <' + @SourceProductLineID + N'>"' + @SourceProductLineNameShort + N'"/ Product <' + ProductID + N'>"' + NameShort + N'"' AS [Statement] ,@TransactUsername AS UserName ,N'' AS PCName ,N'' AS ProcessorCode ,N'' AS IPAddresses ,GETUTCDATE() [Timestamp] ,0 AS IsDeleted ,0 AS IsResolved FROM planning.tdProducts WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; -- tTabs UPDATE planning.tTabs SET FactoryKey = @TargetFactoryKey ,FactoryID = @TargetFactoryID ,ProductLineID = @TargetProductLineID WHERE ProductLineKey = @SourceProductLineKey; SET @EffectedRows += @@ROWCOUNT; -- tTabLayouts UPDATE planning.tTabLayouts SET FactoryID = @TargetFactoryID , ProductLineID = @TargetProductLineID WHERE FactoryID = @SourceFactoryID AND ProductLineID = @SourceProductLineID; SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = IIF(@EffectedRows > 0, 201, 204); -- Rights materialization EXEC system.spMATERIALIZE_trUserRights; END; COMMIT TRANSACTION sx_pf_MOVE_ProductLine; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_MOVE_ProductLine; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spMOVE_ProductLine' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'MOVE Operation for a ProductLine ProductLine moved by the update IDs without key change'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceFactoryID'; SET @value = N'SourceFactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceProductLineID'; SET @value = N'SourceProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetFactoryID'; SET @value = N'TargetFactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetProductLineID'; SET @value = N'TargetProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spMOVE_ValueSerie; GO /* Move Operation for one ValueSeries The ValueSeries will be moved to a new ID, only if this ID is free Works only inside one Product Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call 1. Empty Source or Target FactoryID / ProductLineID / ProductID / ValueSerieID => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. User has no Rights => 401 Unauthorized 4. Not exists Source Factory / ProductLine / Product / ValueSerie => 404 Not Found 5. Not exists Target Factory / ProductLine / Product => 404 Not Found 6. Target ValueSerie key already exists => 403 Forbidden 7. ValueSerie moved => 200 OK 8. Don`t moved anything => 204 No content DECLARE @RC int DECLARE @Username AS NVARCHAR(255) = 'SQL' DECLARE @FactoryID AS NVARCHAR(255) ='ZT' DECLARE @ProductLineID AS NVARCHAR(255) ='U' DECLARE @ProductID AS NVARCHAR(255) ='2' DECLARE @SourceValueSerieID AS NVARCHAR(255) ='K1' DECLARE @TargetValueSerieID AS NVARCHAR(255) ='K7' EXECUTE @RC = planning.spMOVE_ValueSerie @Username ,@FactoryID ,@ProductLineID ,@ProductID ,@SourceValueSerieID ,@TargetValueSerieID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spMOVE_ValueSerie',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spMOVE_ValueSerie','PARAMETER',NULL) */ CREATE PROCEDURE planning.spMOVE_ValueSerie @Username AS NVARCHAR(255), @FactoryID AS NVARCHAR(255), @ProductLineID AS NVARCHAR(255), @ProductID AS NVARCHAR(255), @SourceValueSerieID AS NVARCHAR(255), @TargetValueSerieID AS NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey AS INT = 0; DECLARE @ProductLineKey AS INT = 0; DECLARE @ProductKey AS INT = 0; DECLARE @SourceValueSerieKey AS INT = 0; DECLARE @TargetValueSerieKey AS INT = 0; DECLARE @TransactUsername AS NVARCHAR(255)= N''; DECLARE @ProcedureName AS NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID);; DECLARE @ParameterString AS NVARCHAR (MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''',''' + ISNULL(CAST(@SourceValueSerieID AS NVARCHAR(255)), N'NULL') + N''',''' + ISNULL(@TargetValueSerieID, N'NULL') + N''''; DECLARE @EffectedRows AS INT = 0; DECLARE @ResultCode AS INT = 501; DECLARE @TimestampCall AS DATETIME = GETUTCDATE(); DECLARE @Comment AS NVARCHAR (2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @SourceValueSerieID IS NULL SET @SourceValueSerieID = N''; IF @TargetValueSerieID IS NULL SET @TargetValueSerieID = N''; BEGIN TRY BEGIN TRANSACTION ONE; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); SET @SourceValueSerieID = system.fProtectID (@SourceValueSerieID); SET @TargetValueSerieID = system.fProtectID (@TargetValueSerieID); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' OR @SourceValueSerieID = N'' OR @TargetValueSerieID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine SourceKeys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID; SELECT @SourceValueSerieKey = ValueSeriesKey FROM planning.tdValueSeries WHERE ProductKey = @ProductKey AND ValueSeriesID = @SourceValueSerieID; IF @FactoryKey = 0 OR @ProductLineKey = 0 OR @ProductKey = 0 OR @SourceValueSerieKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Source keys don`t exists', 16, 10); END; -- STEP 1.3 - Determine TargetKeys SELECT @TargetValueSerieKey = ValueSeriesKey FROM planning.tdValueSeries WHERE ProductKey = @ProductKey AND ValueSeriesID = @TargetValueSerieID; IF @TargetValueSerieKey <> 0 BEGIN SET @ResultCode = 403; RAISERROR('Target ValueSerie key already exists', 16, 10); END; -- STEP 3 - Move data --dValueSeries UPDATE planning.tdValueSeries SET ValueSeriesID = @TargetValueSerieID WHERE ValueSeriesKey = @SourceValueSerieKey; SET @EffectedRows += @@ROWCOUNT; IF @EffectedRows = 0 BEGIN SET @ResultCode = 204; END ELSE BEGIN --fValues UPDATE planning.tfValues SET ValueSeriesID = @TargetValueSerieID WHERE ValueSeriesKey = @SourceValueSerieKey; SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = IIF(@EffectedRows = 0, 204, 200); INSERT INTO planning.tfStatements ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ActionType ,Statement ,UserName ,PCName ,ProcessorCode ,IPAddresses ,Timestamp ,IsDeleted ,IsResolved ) SELECT @ProductKey AS ProductKey , @ProductLineKey AS ProductLineKey , @FactoryKey AS FactoryKey , @ProductID AS ProductID , @ProductLineID AS ProductLineID , @FactoryID AS FactoryID , N'MOVE' AS ActionType , N'ValueSeries ' + @SourceValueSerieID + N' was moved to new ID ' + @TargetValueSerieID AS [Statement] , @TransactUsername AS UserName , N'' AS PCName , N'' AS ProcessorCode , N'' AS IPAddresses , GETUTCDATE() [Timestamp] , 0 AS IsDeleted , 0 AS IsResolved; SET @EffectedRows += @@ROWCOUNT; END; COMMIT TRANSACTION ONE; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION ONE; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spMOVE_ValueSerie' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Move Operation for one ValueSeries The ValueSeries will be moved to a new ID, only if this ID is free Works only inside one Product'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceValueSerieID'; SET @value = N'SourceValueSerieID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetValueSerieID'; SET @value = N'TargetValueSerieID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_ClusterProperty; GO /* POST Operation for a ClusterProperty A not existing Property will be created, an existing Property will be updated If the value <#NV> is passed as parameter, it means keep the existing value Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call 1. PropertyID is empty => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. Username does not have write access to the cluster at Create => 401 Unauthorized 4. Property created => 201 Created 5. Property updated => 200 OK DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@PropertyID NVARCHAR(255) = 'rre' ,@PropertyName NVARCHAR(255) = '' ,@CommentUser NVARCHAR(255) = '' ,@CommentDev NVARCHAR(255) = '' ,@Unit NVARCHAR(255) = '' ,@ValueText NVARCHAR(255) = '' ,@ValueInt NVARCHAR(255) = '678' ,@Scale NVARCHAR(255) = '<#NV>' ,@IsROSystemProperty NVARCHAR(255) = '0' ,@FormatID NVARCHAR(255) = '<#NV>' EXECUTE @RC = planning.spPOST_ClusterProperty @Username, @PropertyID, @PropertyName, @CommentUser, @CommentDev, @Unit, @ValueText, @ValueInt, @Scale, @IsROSystemProperty, @FormatID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ClusterProperty',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ClusterProperty','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_ClusterProperty @Username NVARCHAR(255), @PropertyID NVARCHAR(255), @PropertyName NVARCHAR(255), @CommentUser NVARCHAR(MAX), @CommentDev NVARCHAR(MAX), @Unit NVARCHAR(255), @ValueText NVARCHAR(MAX), @ValueInt NVARCHAR(255), @Scale NVARCHAR(255), @IsROSystemProperty NVARCHAR(255), @FormatID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @PropertyKey INT = 0; DECLARE @SytemROBlocker INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username , N'NULL') + N''',''' + ISNULL(@PropertyID , N'NULL') + N''',''' + ISNULL(@PropertyName , N'NULL') + N''',''' + ISNULL(@CommentUser , N'NULL') + N''',''' + ISNULL(@CommentDev , N'NULL') + N''',''' + ISNULL(@Unit , N'NULL') + N''',''' + ISNULL(@ValueText , N'NULL') + N''',''' + ISNULL(@ValueInt , N'NULL') + N''',''' + ISNULL(@Scale , N'NULL') + N''',''' + ISNULL(@IsROSystemProperty , N'NULL') + N''',''' + ISNULL(@FormatID , N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @PropertyID IS NULL SET @PropertyID = N''; IF @PropertyName IS NULL SET @PropertyName = N''; IF @CommentUser IS NULL SET @CommentUser = N''; IF @CommentDev IS NULL SET @CommentDev = N''; IF @Unit IS NULL SET @Unit = N''; IF @ValueText IS NULL SET @ValueText = N''; IF @ValueInt IS NULL SET @ValueInt = N''; IF @Scale IS NULL SET @Scale = N''; IF @IsROSystemProperty IS NULL SET @IsROSystemProperty = N''; IF @FormatID IS NULL SET @FormatID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_ClusterProperty; -- STEP 0.2 - Protect input parameters SET @PropertyID = system.fProtectID (@PropertyID); SET @ValueInt = system.fProtectInt (@ValueInt); SET @Scale = system.fProtectInt (@Scale); IF @PropertyID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); -- Allow db_octservice Access IF EXISTS ( SELECT r.name from sys.database_role_members rm INNER JOIN sys.database_principals r on rm.role_principal_id = r.principal_id INNER JOIN sys.database_principals m on rm.member_principal_id = m.principal_id WHERE r.name = 'db_octservice' AND m.name = ORIGINAL_LOGIN() ) BEGIN SET @TransactUsername = 'db_octservice'; END; IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @PropertyKey = PropertyKey FROM planning.tgCluster WHERE PropertyID = @PropertyID; -- STEP 1.3 - Don`t allow changing of system RO Properties if user is not db_octservice -- TEMPORARY DISABLE to set the version during Updatescript, which runs with db_owner role in most cases --SELECT @SytemROBlocker = IsROSystemProperty FROM planning.tgCluster WHERE PropertyID = @PropertyID; --BEGIN -- SET @ResultCode = 403; -- RAISERROR('Don`t allow changing of system RO Properties', 16, 10); --END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 AND NOT @TransactUsername = 'db_octservice' BEGIN RAISERROR('Invalid rights', 16, 10); END -- STEP 3.1 - If Key does not yet exist, it will be created in minimal configuration IF @PropertyKey = 0 BEGIN DECLARE @Keys AS TABLE ([Key] INT NOT NULL); INSERT INTO planning.tgCluster (PropertyID,PropertyName,CommentUser,CommentDev,Unit,ValueText,ValueInt,Scale,IsROSystemProperty,FormatID) OUTPUT INSERTED.PropertyKey INTO @Keys([Key]) VALUES (@PropertyID,'','','','','',0,1,0,''); SELECT TOP (1) @PropertyKey = [Key] FROM @Keys ORDER BY [Key]; SET @EffectedRows = 1; SET @Resultcode = 201; END; -- STEP 3.2 - Update Cluster attributes UPDATE planning.tgCluster SET PropertyName = IIF(@PropertyName = N'<#NV>', PropertyName, @PropertyName) ,CommentUser = IIF(@CommentUser = N'<#NV>', CommentUser, @CommentUser) ,CommentDev = IIF(@CommentDev = N'<#NV>', CommentDev, @CommentDev) ,Unit = IIF(@Unit = N'<#NV>', Unit, @Unit) ,ValueText = IIF(@ValueText = N'<#NV>', ValueText, @ValueText) ,ValueInt = IIF(@ValueInt = N'<#NV>', ValueInt, @ValueInt) ,Scale = IIF(@Scale = N'<#NV>', Scale, @Scale) ,IsROSystemProperty = IIF(@IsROSystemProperty = N'<#NV>', IsROSystemProperty, @IsROSystemProperty) ,FormatID = IIF(@FormatID = N'<#NV>', FormatID, @FormatID) FROM planning.tgCluster WHERE PropertyKey = @PropertyKey; SET @EffectedRows += @@ROWCOUNT; IF NOT @ResultCode = 201 SET @ResultCode = 200; PRINT @ResultCode; COMMIT TRANSACTION sx_pf_POST_ClusterProperty; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_ClusterProperty; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; PRINT @ResultCode; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_ClusterProperty' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for a ClusterProperty A not existing Property will be created, an existing Property will be updated If the value <#NV> is passed as parameter, it means keep the existing value'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PropertyID'; SET @value = N'PropertyID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PropertyName'; SET @value = N'PropertyName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentUser'; SET @value = N'CommentUser'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentDev'; SET @value = N'CommentDev'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Unit'; SET @value = N'Unit'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueText'; SET @value = N'ValueText'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueInt'; SET @value = N'ValueInt'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Scale'; SET @value = N'Scale'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@IsROSystemProperty'; SET @value = N'IsROSystemProperty'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FormatID'; SET @value = N'FormatID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_Factory; GO /* POST Operation for a Factory Not existing Factories are created, existing updated If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@FactoryID NVARCHAR(255) = 'ZT' ,@NameShort NVARCHAR(255) = '' ,@NameLong NVARCHAR(255) = '' ,@CommentUser NVARCHAR(255) = '' ,@CommentDev NVARCHAR(255) = '' ,@ResponsiblePerson NVARCHAR(255) = '' ,@ImageName NVARCHAR(255) = '' EXECUTE @RC = planning.spPOST_Factory @Username, @FactoryID, @NameShort, @NameLong, @CommentUser, @CommentDev, @ResponsiblePerson, @ImageName PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Factory',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Factory','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_Factory @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @NameShort NVARCHAR(255), @NameLong NVARCHAR(255), @CommentUser NVARCHAR(MAX), @CommentDev NVARCHAR(MAX), @ResponsiblePerson NVARCHAR(255), @ImageName NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username , N'NULL') + N''',''' + ISNULL(@FactoryID , N'NULL') + N''',''' + ISNULL(@NameShort , N'NULL') + N''',''' + ISNULL(@NameLong , N'NULL') + N''',''' + ISNULL(@CommentUser , N'NULL') + N''',''' + ISNULL(@CommentDev , N'NULL') + N''',''' + ISNULL(@ResponsiblePerson , N'NULL') + N''',''' + ISNULL(@ImageName , N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @NameShort IS NULL SET @NameShort = N''; IF @NameLong IS NULL SET @NameLong = N''; IF @CommentUser IS NULL SET @CommentUser = N''; IF @CommentDev IS NULL SET @CommentDev = N''; IF @ResponsiblePerson IS NULL SET @ResponsiblePerson = N''; IF @ImageName IS NULL SET @ImageName = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_Factory; -- STEP 0.2 - Protect input parameters SET @FactoryID = system.fProtectID (@FactoryID); SET @NameShort = system.fProtectString (@NameShort); SET @NameLong = system.fProtectString (@NameLong); SET @CommentUser = system.fProtectString (@CommentUser); SET @CommentDev = system.fProtectString (@CommentDev); SET @ResponsiblePerson = system.fProtectString (@ResponsiblePerson); SET @ImageName = system.fProtectString (@ImageName); IF @FactoryID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; -- STEP 2 - Check rights IF @FactoryKey = 0 EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; ELSE EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername, @FactoryID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 3.1 - If Factory does not yet exist, it will be created in minimal configuration IF @FactoryKey = 0 BEGIN DECLARE @Keys AS TABLE ([Key] INT NOT NULL); INSERT INTO planning.tdFactories (FactoryID, NameShort,NameLong, CommentUser, CommentDev, ResponsiblePerson, ImageName) OUTPUT INSERTED.FactoryKey INTO @Keys([Key]) VALUES (@FactoryID, @NameShort, '', '', '', '', ''); SELECT TOP(1) @FactoryKey = [Key] FROM @Keys ORDER BY [Key]; SET @EffectedRows = 1; SET @Resultcode = 201; END; -- STEP 3.2 - Update Factory attributes UPDATE planning.tdFactories SET NameShort = IIF(@NameShort = N'<#NV>', NameShort, @NameShort) ,NameLong = IIF(@NameLong = N'<#NV>', NameLong, @NameLong) ,CommentUser = IIF(@CommentUser = N'<#NV>', CommentUser, @CommentUser) ,CommentDev = IIF(@CommentDev = N'<#NV>', CommentDev, @CommentDev) ,ResponsiblePerson = IIF(@ResponsiblePerson = N'<#NV>', ResponsiblePerson, @ResponsiblePerson) ,ImageName = IIF(@ImageName = N'<#NV>', ImageName, @ImageName) FROM planning.tdFactories WHERE FactoryKey = @FactoryKey; SET @EffectedRows += @@ROWCOUNT; -- STEP 3.3 - Rights materialize (no insert needed because only ClusterAdmin can create Factory) EXEC system.spMATERIALIZE_trUserRights; COMMIT TRANSACTION sx_pf_POST_Factory; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_Factory; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Factory' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for a Factory Not existing Factories are created, existing updated If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@NameShort'; SET @value = N'NameShort'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@NameLong'; SET @value = N'NameLong'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentUser'; SET @value = N'CommentUser'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentDev'; SET @value = N'CommentDev'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ResponsiblePerson'; SET @value = N'ResponsiblePerson'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ImageName'; SET @value = N'ImageName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_FactoryProperty; GO /* POST Operation for a FactoryProperty Not existing Property will be created, existing updated. If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@FactoryID NVARCHAR(255) = 'ZT' ,@PropertyID NVARCHAR(255) = 'rr3e' ,@PropertyName NVARCHAR(255) = '' ,@CommentUser NVARCHAR(MAX) = '' ,@CommentDev NVARCHAR(MAX) = '' ,@Unit NVARCHAR(255) = '' ,@ValueText NVARCHAR(MAX) = '' ,@ValueInt NVARCHAR(255) = '678' ,@Scale NVARCHAR(255) = '<#NV>' ,@IsROSystemProperty NVARCHAR(255) = '2' ,@FormatID NVARCHAR(255) = '<#NV>' EXECUTE @RC = planning.spPOST_FactoryProperty @Username, @FactoryID, @PropertyID, @PropertyName, @CommentUser, @CommentDev, @Unit, @ValueText, @ValueInt, @Scale, @IsROSystemProperty, @FormatID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_FactoryProperty',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_FactoryProperty','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_FactoryProperty @Username NVARCHAR(255), @FactoryID NVARCHAR(255), @PropertyID NVARCHAR(255), @PropertyName NVARCHAR(255), @CommentUser NVARCHAR(MAX), @CommentDev NVARCHAR(MAX), @Unit NVARCHAR(255), @ValueText NVARCHAR(MAX), @ValueInt NVARCHAR(255), @Scale NVARCHAR(255), @IsROSystemProperty NVARCHAR(255), @FormatID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @PropertyKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@PropertyID, N'NULL') + N''',''' + ISNULL(@PropertyName, N'NULL') + N''',''' + ISNULL(@CommentUser, N'NULL') + N''',''' + ISNULL(@CommentDev, N'NULL') + N''',''' + ISNULL(@Unit, N'NULL') + N''',''' + ISNULL(@ValueText, N'NULL') + N''',''' + ISNULL(@ValueInt, N'NULL') + N''',''' + ISNULL(@Scale, N'NULL') + N''',''' + ISNULL(@IsROSystemProperty, N'NULL') + N''',''' + ISNULL(@FormatID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @PropertyID IS NULL SET @PropertyID = N''; IF @PropertyName IS NULL SET @PropertyName = N''; IF @CommentUser IS NULL SET @CommentUser = N''; IF @CommentDev IS NULL SET @CommentDev = N''; IF @Unit IS NULL SET @Unit = N''; IF @ValueText IS NULL SET @ValueText = N''; IF @ValueInt IS NULL SET @ValueInt = N''; IF @Scale IS NULL SET @Scale = N''; IF @IsROSystemProperty IS NULL SET @IsROSystemProperty = N''; IF @FormatID IS NULL SET @FormatID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_FactoryProperty; -- STEP 0.2 - Protect input parameters SET @FactoryID = system.fProtectID (@FactoryID); SET @PropertyID = system.fProtectID (@PropertyID); SET @PropertyName = system.fProtectString (@PropertyName); SET @CommentUser = system.fProtectString (@CommentUser); SET @CommentDev = system.fProtectString (@CommentDev); SET @Unit = system.fProtectString (@Unit); SET @ValueText = system.fProtectString (@ValueText); SET @ValueInt = system.fProtectInt (@ValueInt); SET @Scale = system.fProtectInt (@Scale); SET @FormatID = system.fProtectString (@FormatID); --has String Protection, as also # can be used as parameter IF @PropertyID = N'' OR @FactoryID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @PropertyKey = PropertyKey FROM planning.tgFactories WHERE FactoryKey = @FactoryKey AND PropertyID = @PropertyID; IF @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 1.3 - Don`t allow changing of system RO Properties - disabled from Version 4.0.63 on to enable export/import --SELECT @PropertyOwner = IsROSystemProperty FROM planning.tgFactories WHERE FactoryKey = @FactoryKey AND PropertyID = @PropertyID; --IF @PropertyOwner = 1 --BEGIN -- SET @ResultCode = 403; -- RAISERROR('Don`t allow changing of system RO Properties', 16, 10); --END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername, @FactoryID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); DECLARE @Keys AS TABLE ([Key] INT NOT NULL); -- STEP 3.1 - If property not exist, it will created minimalistic IF @PropertyKey = 0 BEGIN INSERT INTO planning.tgFactories (FactoryKey, FactoryID, PropertyID, PropertyName, CommentUser, CommentDev, Unit, ValueText, ValueInt, Scale, IsROSystemProperty, FormatID) OUTPUT INSERTED.PropertyKey INTO @Keys VALUES (@FactoryKey, @FactoryID, @PropertyID, '', '', '', '', '', 0, 1, 0, ''); SELECT TOP (1) @PropertyKey = [Key] FROM @Keys ORDER BY [Key]; SET @EffectedRows = 1; SET @Resultcode = 201; END; -- STEP 3.2 - Update Factory property UPDATE planning.tgFactories SET PropertyName = IIF(@PropertyName = N'<#NV>', PropertyName, @PropertyName) ,CommentUser = IIF(@CommentUser = N'<#NV>', CommentUser, @CommentUser) ,CommentDev = IIF(@CommentDev = N'<#NV>', CommentDev, @CommentDev) ,Unit = IIF(@Unit = N'<#NV>', Unit, @Unit) ,ValueText = IIF(@ValueText = N'<#NV>', ValueText, @ValueText) ,ValueInt = IIF(@ValueInt = N'<#NV>', ValueInt, @ValueInt) ,Scale = IIF(@Scale = N'<#NV>', Scale, @Scale) ,IsROSystemProperty = IIF(@IsROSystemProperty = N'<#NV>', IsROSystemProperty, @IsROSystemProperty) ,FormatID = IIF(@FormatID = N'<#NV>', FormatID, @FormatID) FROM planning.tgFactories WHERE PropertyKey = @PropertyKey; SET @EffectedRows += @@ROWCOUNT; COMMIT TRANSACTION sx_pf_POST_FactoryProperty; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_FactoryProperty; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_FactoryProperty' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for a FactoryProperty Not existing Property will be created, existing updated. If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PropertyID'; SET @value = N'PropertyID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PropertyName'; SET @value = N'PropertyName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentUser'; SET @value = N'CommentUser'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentDev'; SET @value = N'CommentDev'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Unit'; SET @value = N'Unit'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueText'; SET @value = N'ValueText'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueInt'; SET @value = N'ValueInt'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Scale'; SET @value = N'Scale'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@IsROSystemProperty'; SET @value = N'IsROSystemProperty'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FormatID'; SET @value = N'FormatID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_Format; GO /* POST Operation for Format Not existing Format will be created, existing updated. If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' 'SQL' ,@FormatID NVARCHAR(255) = 'sxTest4' 'sxTest3' ,@BackgroundColor NVARCHAR(255) = '100,100,100' '<#NV>' ,@FontColor NVARCHAR(255) = '200,200,200' '<#NV>' ,@ValueFormat NVARCHAR(255) = '' '<#NV>' ,@Bold NVARCHAR(255) = '' '<#NV>' ,@Italic NVARCHAR(255) = '' '<#NV>' ,@AlignmentHorizontal NVARCHAR(255) = '' '<#NV>' ,@AlignmentVertical NVARCHAR(255) = '' '<#NV>' ,@TextUnderlined NVARCHAR(255) = '' '<#NV>' ,@BorderToNext NVARCHAR(255) = '' '<#NV>' ,@BorderToPrevious NVARCHAR(255) = '' '<#NV>' EXECUTE @RC = planning.spPOST_Format @Username, @FormatID, @BackgroundColor, @FontColor, @ValueFormat, @Bold, @Italic, @AlignmentHorizontal, @AlignmentVertical, @TextUnderlined, @BorderToNext, @BorderToPrevious PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Format',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Format','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_Format @Username NVARCHAR(255), @FormatID NVARCHAR(255), @BackgroundColor NVARCHAR(255), @FontColor NVARCHAR(255), @ValueFormat NVARCHAR(255), @Bold NVARCHAR(255), @Italic NVARCHAR(255), @AlignmentHorizontal NVARCHAR(255), @AlignmentVertical NVARCHAR(255), @TextUnderlined NVARCHAR(255), @BorderToNext NVARCHAR(255), @BorderToPrevious NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FormatKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username , N'NULL') + N''',''' + ISNULL(@FormatID , N'NULL') + N''',''' + ISNULL(@BackgroundColor , N'NULL') + N''',''' + ISNULL(@FontColor , N'NULL') + N''',''' + ISNULL(@ValueFormat , N'NULL') + N''',''' + ISNULL(@Bold , N'NULL') + N''',''' + ISNULL(@Italic , N'NULL') + N''',''' + ISNULL(@AlignmentHorizontal , N'NULL') + N''',''' + ISNULL(@AlignmentVertical , N'NULL') + N''',''' + ISNULL(@TextUnderlined , N'NULL') + N''',''' + ISNULL(@BorderToNext , N'NULL') + N''',''' + ISNULL(@BorderToPrevious , N'NULL') + N''',''' + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR (2000) = N''; DECLARE @ErrorText NVARCHAR (2000) = N';' -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FormatID IS NULL SET @FormatID = N''; IF @BackgroundColor IS NULL SET @BackgroundColor = N''; IF @FontColor IS NULL SET @FontColor = N''; IF @ValueFormat IS NULL SET @ValueFormat = N''; IF @Bold IS NULL SET @Bold = N''; IF @Italic IS NULL SET @Italic = N''; IF @AlignmentHorizontal IS NULL SET @AlignmentHorizontal = N''; IF @AlignmentVertical IS NULL SET @AlignmentVertical = N''; IF @TextUnderlined IS NULL SET @TextUnderlined = N''; IF @BorderToNext IS NULL SET @BorderToNext = N''; IF @BorderToPrevious IS NULL SET @BorderToPrevious = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_Format; -- STEP 0.2 - Protect input parameters SET @FormatID = system.fProtectID (@FormatID); SET @BackgroundColor = system.fProtectString (@BackgroundColor); SET @FontColor = system.fProtectString (@FontColor); SET @ValueFormat = system.fProtectString (@ValueFormat); SET @Bold = system.fProtectInt (@Bold); SET @Italic = system.fProtectInt (@Italic); SET @AlignmentHorizontal = system.fProtectString (@AlignmentHorizontal); SET @AlignmentVertical = system.fProtectString (@AlignmentVertical); SET @TextUnderlined = system.fProtectInt (@TextUnderlined) SET @BorderToNext = system.fProtectInt (@BorderToNext) SET @BorderToPrevious = system.fProtectInt (@BorderToPrevious) IF @FormatID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; IF @Bold NOT IN ('<#NV>','-1','0','1') OR @Italic NOT IN ('<#NV>','-1','0','1') OR @BorderToNext NOT IN ('<#NV>','-1','0','1') OR @BorderToPrevious NOT IN ('<#NV>','-1','0','1') BEGIN SET @ResultCode = 401; SET @ErrorText = 'Parameter for Bold, Italic, BorderToNext, BorderToPrevios not supported (outside <#NV>,-1,0,1).' RAISERROR(@ErrorText, 16, 10); END; IF @AlignmentHorizontal NOT IN ('<#NV>','','left','center','right') BEGIN SET @ResultCode = 401; SET @ErrorText = 'Parameter ' + @AlignmentHorizontal + ' for AlignmentHorizontal not supported (outside <#NV>, left, center, rights, emptystring).' RAISERROR(@ErrorText, 16, 10); END; IF @AlignmentVertical NOT IN ('<#NV>','','top','center','bottom') BEGIN SET @ResultCode = 401; SET @ErrorText = 'Parameter ' + @AlignmentVertical + ' for AlignmentVertical not supported (outside <#NV>, top, center, bottom, emptystring).' RAISERROR(@ErrorText, 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine keys SELECT @FormatKey = FormatKey FROM planning.thFormats WHERE FormatID = @FormatID; -- STEP 3.1 - If format does not yet exist, it will be created IF @FormatKey = 0 BEGIN DECLARE @Keys AS TABLE ([Key] INT NOT NULL); INSERT INTO planning.thFormats (FormatID,BackgroundColor,FontColor,ValueFormat,Bold,Italic,AlignmentHorizontal,AlignmentVertical ,TextUnderlined,BorderToNext,BorderToPrevious ) OUTPUT INSERTED.FormatKey INTO @Keys([Key]) VALUES (@FormatID,'','','','-1','-1','','','-1','0','0'); SELECT TOP (1) @FormatKey = [Key] FROM @Keys ORDER BY [Key]; SET @EffectedRows = 1; SET @Resultcode = 201; END; -- STEP 3.2 - Update format attributes UPDATE planning.thFormats SET BackgroundColor = IIF(@BackgroundColor = N'<#NV>', BackgroundColor, @BackgroundColor) ,FontColor = IIF(@FontColor = N'<#NV>', FontColor, @FontColor) ,ValueFormat = IIF(@ValueFormat = N'<#NV>', ValueFormat, @ValueFormat) ,Bold = IIF(@Bold = N'<#NV>', Bold, @Bold) ,Italic = IIF(@Italic = N'<#NV>', Italic, @Italic) ,AlignmentHorizontal= IIF(@AlignmentHorizontal = N'<#NV>', AlignmentHorizontal, @AlignmentHorizontal) ,AlignmentVertical = IIF(@AlignmentVertical = N'<#NV>', AlignmentVertical, @AlignmentVertical) ,TextUnderlined = IIF(@TextUnderlined = N'<#NV>', TextUnderlined, @TextUnderlined) ,BorderToNext = IIF(@BorderToNext = N'<#NV>', BorderToNext, @BorderToNext) ,BorderToPrevious = IIF(@BorderToPrevious = N'<#NV>', BorderToPrevious, @BorderToPrevious) FROM planning.thFormats WHERE FormatKey = @FormatKey; SET @EffectedRows += @@ROWCOUNT; COMMIT TRANSACTION sx_pf_POST_Format; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_Format; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Format' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for Format Not existing Format will be created, existing updated. If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FormatID'; SET @value = N'FormatID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@BackgroundColor'; SET @value = N'BackgroundColor'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FontColor'; SET @value = N'FontColor'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueFormat'; SET @value = N'ValueFormat'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Bold'; SET @value = N'Bold'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Italic'; SET @value = N'Italic'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@AlignmentHorizontal'; SET @value = N'AlignmentHorizontal'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@AlignmentVertical'; SET @value = N'AlignmentVertical'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TextUnderlined'; SET @value = N'TextUnderlined'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@BorderToNext'; SET @value = N'BorderToNext'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@BorderToPrevious'; SET @value = N'BorderToPrevious'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_List; GO /* POST Operation for a List Not existing Format will be created, existing updated. If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@ListID NVARCHAR(255) = 'sxScales' ,@NameShort NVARCHAR(255) = '<#NV>' ,@NameLong NVARCHAR(255) = '<#NV>' ,@CommentDev NVARCHAR(255) = '<#NV>' ,@CommentUser NVARCHAR(255) = '<#NV>' ,@Datentyp NVARCHAR(255) = '<#NV>' ,@Source NVARCHAR(255) = '<#NV>' ,@SourceFormula NVARCHAR(255) = 'Testen' ,@FormatID NVARCHAR(255) = '<#NV>' EXECUTE @RC = planning.spPOST_List @Username, @ListID, @NameShort, @NameLong, @CommentDev, @CommentUser, @Datentyp, @Source, @SourceFormula, @FormatID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_List',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_List','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_List @Username NVARCHAR(255) ,@ListID NVARCHAR(255) ,@NameShort NVARCHAR(255) ,@NameLong NVARCHAR(255) ,@CommentDev NVARCHAR(255) ,@CommentUser NVARCHAR(255) ,@Datentyp NVARCHAR(255) ,@Source NVARCHAR(255) ,@SourceFormula NVARCHAR(255) ,@FormatID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @ListKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@ListID ,N'NULL') + N''',''' + ISNULL(@NameShort ,N'NULL') + N''',''' + ISNULL(@NameLong ,N'NULL') + N''',''' + ISNULL(@CommentDev ,N'NULL') + N''',''' + ISNULL(@CommentUser ,N'NULL') + N''',''' + ISNULL(@Datentyp ,N'NULL') + N''',''' + ISNULL(@Source ,N'NULL') + N''',''' + ISNULL(@SourceFormula ,N'NULL') + N''',''' + ISNULL(@FormatID ,N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR (2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ListID IS NULL SET @ListID = N''; IF @NameShort IS NULL SET @NameShort = N''; IF @NameLong IS NULL SET @NameLong = N''; IF @CommentUser IS NULL SET @CommentUser = N''; IF @CommentDev IS NULL SET @CommentDev = N''; IF @Datentyp IS NULL SET @Datentyp = N''; IF @Source IS NULL SET @Source = N''; IF @SourceFormula IS NULL SET @SourceFormula = N''; IF @FormatID IS NULL SET @FormatID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_List; -- STEP 0.2 - Protect input parameters SET @ListID = system.fProtectID (@ListID); SET @NameShort = system.fProtectString (@NameShort); SET @NameLong = system.fProtectString (@NameLong); SET @CommentUser = system.fProtectString (@CommentUser); SET @CommentDev = system.fProtectString (@CommentDev); SET @Datentyp = system.fProtectString (@Datentyp); SET @Source = system.fProtectString (@Source); SET @SourceFormula = system.fProtectString (@SourceFormula); SET @FormatID = system.fProtectString (@FormatID); -- as it can be # IF @ListID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END ELSE IF @ListID IN ('sxIsNumeric', 'sxScales', 'sxValueEffects', 'sxValueLists', 'sxValueFormats', 'sxValueSources', 'sxVisibilityLevel') BEGIN SET @ResultCode = 403; RAISERROR('Invalid input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine keys SELECT @ListKey = ListKey FROM planning.thLists WHERE ListID = @ListID; -- STEP 3.1 - If List is does not exist, it will be created IF @ListKey = 0 BEGIN DECLARE @Keys AS TABLE ([Key] INT NOT NULL); INSERT INTO planning.thLists (ListID, NameShort, NameLong, CommentDev, CommentUser, Datentyp, Source, SourceFormula, FormatID) OUTPUT INSERTED.ListKey INTO @Keys([Key]) VALUES (@ListID, '', '', '', '', '', '', '', ''); SELECT TOP(1) @ListKey = [Key] FROM @Keys ORDER BY [Key]; SET @EffectedRows = 1; SET @Resultcode = 201; END; -- STEP 3.2 - Update List attributes UPDATE planning.thLists SET NameShort = IIF(@NameShort = N'<#NV>', NameShort, @NameShort) ,NameLong = IIF(@NameLong = N'<#NV>', NameLong, @NameLong) ,CommentDev = IIF(@CommentDev = N'<#NV>', CommentDev, @CommentDev) ,CommentUser = IIF(@CommentUser = N'<#NV>', CommentUser, @CommentUser) ,Datentyp = IIF(@Datentyp = N'<#NV>', Datentyp, @Datentyp) ,Source = IIF(@Source = N'<#NV>', Source, @Source) ,SourceFormula = IIF(@SourceFormula = N'<#NV>', SourceFormula, @SourceFormula) ,FormatID = IIF(@FormatID = N'<#NV>', FormatID, @FormatID) FROM planning.thLists WHERE ListKey = @ListKey; SET @EffectedRows += @@ROWCOUNT; COMMIT TRANSACTION sx_pf_POST_List; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_List; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_List' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for a List Not existing Format will be created, existing updated. If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ListID'; SET @value = N'ListID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@NameShort'; SET @value = N'NameShort'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@NameLong'; SET @value = N'NameLong'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentDev'; SET @value = N'CommentDev'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentUser'; SET @value = N'CommentUser'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Datentyp'; SET @value = N'Datentyp'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Source'; SET @value = N'Source'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceFormula'; SET @value = N'SourceFormula'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FormatID'; SET @value = N'FormatID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_ListValue; GO /* POST Operation for an ListValue Not existing ListValue will be created (if Key = 0 is used), existing updated. Special: This procedure allows to pass a Key value, as there is no ID for a ListValue Saxess Software GmbH Last modified: 09/2023 for OCT 5.10 Test call DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@ListValueKey INT = 0 ,@ListID NVARCHAR(255) = 'sxStatus' ,@ValueInt INT = 5 ,@Scale INT = 1 ,@ValueText NVARCHAR(255) = 'Testo' ,@ValueComment NVARCHAR(255) = '' ,@FormatID NVARCHAR(255) = '' EXECUTE @RC = planning.spPOST_ListValue @Username, @ListValueKey, @ListID, @ValueInt, @Scale, @ValueText, @ValueComment, @FormatID PRINT @RC Test call API Log SELECT TOP 5 * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ListValue',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ListValue','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_ListValue @Username NVARCHAR(255) ,@ListValueKey INT ,@ListID NVARCHAR(255) ,@ValueInt INT ,@Scale INT ,@ValueText NVARCHAR(255) ,@ValueComment NVARCHAR(255) ,@FormatID NVARCHAR(255) AS BEGIN DECLARE @ListKey INT = 0 ,@TransactUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',' + ISNULL(CAST(@ListValueKey AS NVARCHAR(255)), N'NULL') + N',''' + ISNULL(@ListID, N'NULL') + N''',' + ISNULL(CAST(@ValueInt AS NVARCHAR(255)), N'NULL') + N',' + ISNULL(CAST(@Scale AS NVARCHAR(255)), N'NULL') + N',''' + ISNULL(@ValueText, N'NULL') + N''',''' + ISNULL(@ValueComment, N'NULL') + N''',''' + ISNULL(@FormatID, N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@tmpKey BIGINT = 0 ,@tmpListType NVARCHAR(255) = ''; -- NULL Protection IF @Username IS NULL SET @Username = N''; IF @ListValueKey IS NULL SET @ListValueKey = 0; IF @ListID IS NULL SET @ListID = N''; IF @ValueInt IS NULL SET @ValueInt = 0; IF @Scale IS NULL SET @Scale = 0; IF @ValueText IS NULL SET @ValueText = N''; IF @ValueComment IS NULL SET @ValueComment = N''; IF @FormatID IS NULL SET @FormatID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_ListValue; -- protect input parameters SET @ListValueKey = system.fProtectInt (@ListValueKey); SET @ListID = system.fProtectID (@ListID); SET @ValueInt = system.fProtectInt (@ValueInt); SET @Scale = system.fProtectInt (@Scale); SET @ValueText = system.fProtectString (@ValueText); SET @ValueComment = system.fProtectString (@ValueComment); SET @FormatID = system.fProtectID (@FormatID); IF @ListID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.2 - Determine keys and break if not existing SELECT @ListKey = ListKey FROM planning.thLists WHERE ListID = @ListID; IF @ListKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; IF @Scale = 0 BEGIN SET @Scale = 1 END -- Check if Listvalue exists, when ListValueKey 0 is submitted - 0 means new entry, but API must check if entry is really new IF @ListValueKey = 0 BEGIN SELECT @tmpListType = hL.Datentyp FROM planning.thLists hL WHERE hL.ListID = @ListID; IF @tmpListType = 'String' BEGIN SELECT @tmpKey = ListValueKey FROM planning.thListValues hLV WHERE ListID = @ListID AND hLV.ValueText = @ValueText; END ELSE BEGIN SELECT @tmpKey = ListValueKey FROM planning.thListValues hLV WHERE ListID = @ListID AND hLV.ValueInt = @ValueInt; END IF (@tmpKey IS NOT NULL AND @tmpKey != 0) SET @ListValueKey = @tmpKey; END -- If ListValueKey = 0 is submitted, create new value IF @ListValueKey = 0 BEGIN INSERT INTO planning.thListValues (ListID, ValueInt, Scale, ValueText, ValueComment, FormatID) VALUES (@ListID, @ValueInt, @Scale, @ValueText, @ValueComment, @FormatID); SET @Resultcode = 201; SET @EffectedRows = 1; END ELSE BEGIN -- existing key is committed, update the Key UPDATE planning.thListValues SET ValueInt = @ValueInt ,Scale = @Scale ,ValueText = @ValueText ,ValueComment = @ValueComment ,FormatID = @FormatID WHERE ListValueKey = @ListValueKey; SET @EffectedRows += @@ROWCOUNT; END; COMMIT TRANSACTION sx_pf_POST_ListValue; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_ListValue; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_ListValue' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N''; -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for ListValue Not existing ListValue will be created, existing updated. This procedure allows to pass a Key value, as there is no ID for a ListValue'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ListValueKey'; SET @value = N'Use existing key to update an ListValue, use 0 to create a new ListValue'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ListID'; SET @value = N'ListID where this Value belongs to.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueInt'; SET @value = N'ValueInt if List of Type integer'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Scale'; SET @value = N'Scale if List of Type integer'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueText'; SET @value = N'ValueText if List of Type String'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueComment'; SET @value = N'Comment for this value'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FormatID'; SET @value = N'FormatID for this value'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_Product; GO /* POST Operation for one Product Not existing Product will be created if it exists in ZT Factory. Existing Product updated If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty Saxess Software GmbH Last modified: 01/2024 for OCT 5.10 Test call DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@ProductID NVARCHAR(255) = '*' ,@ProductLineID NVARCHAR(255) = 'U' ,@FactoryID NVARCHAR(255) = 'ZT' ,@TimeType NVARCHAR(255) = 'VM' ,@NameShort NVARCHAR(255) = '' ,@NameLong NVARCHAR(255) = '' ,@CommentUser NVARCHAR(MAX) = '' ,@CommentDev NVARCHAR(MAX) = '' ,@ResponsiblePerson NVARCHAR(255) = '' ,@ImageName NVARCHAR(255) = '' ,@Status NVARCHAR(255) = 'Aktiv' ,@Template NVARCHAR(255) = 'Unikum_VM' ,@TemplateVersion NVARCHAR(255) = 'VT' ,@GA1 NVARCHAR(255) = 'Wert für GA1' --'<#NV>' ,@GA2 NVARCHAR(255) = '<#NV>' ,@GA3 NVARCHAR(255) = '<#NV>' ,@GA4 NVARCHAR(255) = 'rer' ,@GA5 NVARCHAR(255) = '<#NV>' ,@GA6 NVARCHAR(255) = '<#NV>' ,@GA7 NVARCHAR(255) = '<#NV>' ,@GA8 NVARCHAR(255) = '<#NV>' ,@GA9 NVARCHAR(255) = '<#NV>' ,@GA10 NVARCHAR(255) = '<#NV>' ,@GA11 NVARCHAR(255) = '<#NV>' ,@GA12 NVARCHAR(255) = '<#NV>' ,@GA13 NVARCHAR(255) = '<#NV>' ,@GA14 NVARCHAR(255) = '<#NV>' ,@GA15 NVARCHAR(255) = '<#NV>' ,@GA16 NVARCHAR(255) = '<#NV>' ,@GA17 NVARCHAR(255) = '<#NV>' ,@GA18 NVARCHAR(255) = '<#NV>' ,@GA19 NVARCHAR(255) = '<#NV>' ,@GA20 NVARCHAR(255) = '<#NV>' ,@GA21 NVARCHAR(255) = '<#NV>' ,@GA22 NVARCHAR(255) = '<#NV>' ,@GA23 NVARCHAR(255) = '<#NV>' ,@GA24 NVARCHAR(255) = '<#NV>' ,@GA25 NVARCHAR(255) = '<#NV>' EXECUTE @RC = planning.spPOST_Product @Username, @ProductID, @ProductLineID, @FactoryID, @TimeType, @NameShort, @NameLong, @CommentUser, @CommentDev, @ResponsiblePerson, @ImageName, @Status, @Template, @TemplateVersion, @GA1, @GA2, @GA3, @GA4, @GA5, @GA6, @GA7, @GA8, @GA9, @GA10, @GA11, @GA12, @GA13, @GA14, @GA15, @GA16, @GA17, @GA18, @GA19, @GA20, @GA21, @GA22, @GA23, @GA24, @GA25; PRINT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Product',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Product','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_Product @Username NVARCHAR(255), @ProductID NVARCHAR(255), @ProductLineID NVARCHAR(255), @FactoryID NVARCHAR(255), @TimeType NVARCHAR(255), @NameShort NVARCHAR(255), @NameLong NVARCHAR(255), @CommentUser NVARCHAR(MAX), @CommentDev NVARCHAR(MAX), @ResponsiblePerson NVARCHAR(255), @ImageName NVARCHAR(255), @Status NVARCHAR(255), @Template NVARCHAR(255), @TemplateVersion NVARCHAR(255), @GA1 NVARCHAR(255), @GA2 NVARCHAR(255), @GA3 NVARCHAR(255), @GA4 NVARCHAR(255), @GA5 NVARCHAR(255), @GA6 NVARCHAR(255), @GA7 NVARCHAR(255), @GA8 NVARCHAR(255), @GA9 NVARCHAR(255), @GA10 NVARCHAR(255), @GA11 NVARCHAR(255), @GA12 NVARCHAR(255), @GA13 NVARCHAR(255), @GA14 NVARCHAR(255), @GA15 NVARCHAR(255), @GA16 NVARCHAR(255), @GA17 NVARCHAR(255), @GA18 NVARCHAR(255), @GA19 NVARCHAR(255), @GA20 NVARCHAR(255), @GA21 NVARCHAR(255), @GA22 NVARCHAR(255), @GA23 NVARCHAR(255), @GA24 NVARCHAR(255), @GA25 NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username , N'NULL') + N''',''' + ISNULL(@ProductID , N'NULL') + N''',''' + ISNULL(@ProductLineID , N'NULL') + N''',''' + ISNULL(@FactoryID , N'NULL') + N''',''' + ISNULL(@TimeType , N'NULL') + N''',''' + ISNULL(@NameShort , N'NULL') + N''',''' + ISNULL(@NameLong , N'NULL') + N''',''' + ISNULL(@CommentUser , N'NULL') + N''',''' + ISNULL(@CommentDev , N'NULL') + N''',''' + ISNULL(@ResponsiblePerson , N'NULL') + N''',''' + ISNULL(@ImageName , N'NULL') + N''',''' + ISNULL(@Status , N'NULL') + N''',''' + ISNULL(@Template , N'NULL') + N''',''' + ISNULL(@TemplateVersion , N'NULL') + N''',''' + ISNULL(@GA1 , N'NULL') + N''',''' + ISNULL(@GA2 , N'NULL') + N''',''' + ISNULL(@GA3 , N'NULL') + N''',''' + ISNULL(@GA4 , N'NULL') + N''',''' + ISNULL(@GA5 , N'NULL') + N''',''' + ISNULL(@GA6 , N'NULL') + N''',''' + ISNULL(@GA7 , N'NULL') + N''',''' + ISNULL(@GA8 , N'NULL') + N''',''' + ISNULL(@GA9 , N'NULL') + N''',''' + ISNULL(@GA10 , N'NULL') + N''',''' + ISNULL(@GA11 , N'NULL') + N''',''' + ISNULL(@GA12 , N'NULL') + N''',''' + ISNULL(@GA13 , N'NULL') + N''',''' + ISNULL(@GA14 , N'NULL') + N''',''' + ISNULL(@GA15 , N'NULL') + N''',''' + ISNULL(@GA16 , N'NULL') + N''',''' + ISNULL(@GA17 , N'NULL') + N''',''' + ISNULL(@GA18 , N'NULL') + N''',''' + ISNULL(@GA19 , N'NULL') + N''',''' + ISNULL(@GA20 , N'NULL') + N''',''' + ISNULL(@GA21 , N'NULL') + N''',''' + ISNULL(@GA22 , N'NULL') + N''',''' + ISNULL(@GA23 , N'NULL') + N''',''' + ISNULL(@GA24 , N'NULL') + N''',''' + ISNULL(@GA25 , N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @TimeType IS NULL SET @TimeType = N''; IF @NameShort IS NULL SET @NameShort = N''; IF @NameLong IS NULL SET @NameLong = N''; IF @CommentUser IS NULL SET @CommentUser = N''; IF @CommentDev IS NULL SET @CommentDev = N''; IF @ResponsiblePerson IS NULL SET @ResponsiblePerson = N''; IF @ImageName IS NULL SET @ImageName = N''; IF @Status IS NULL SET @Status = N''; IF @Template IS NULL SET @Template = N''; IF @TemplateVersion IS NULL SET @TemplateVersion = N''; IF @GA1 IS NULL SET @GA1 = N''; IF @GA2 IS NULL SET @GA2 = N''; IF @GA3 IS NULL SET @GA3 = N''; IF @GA4 IS NULL SET @GA4 = N''; IF @GA5 IS NULL SET @GA5 = N''; IF @GA6 IS NULL SET @GA6 = N''; IF @GA7 IS NULL SET @GA7 = N''; IF @GA8 IS NULL SET @GA8 = N''; IF @GA9 IS NULL SET @GA9 = N''; IF @GA10 IS NULL SET @GA10 = N''; IF @GA11 IS NULL SET @GA11 = N''; IF @GA12 IS NULL SET @GA12 = N''; IF @GA13 IS NULL SET @GA13 = N''; IF @GA14 IS NULL SET @GA14 = N''; IF @GA15 IS NULL SET @GA15 = N''; IF @GA16 IS NULL SET @GA16 = N''; IF @GA17 IS NULL SET @GA17 = N''; IF @GA18 IS NULL SET @GA18 = N''; IF @GA19 IS NULL SET @GA19 = N''; IF @GA20 IS NULL SET @GA20 = N''; IF @GA21 IS NULL SET @GA21 = N''; IF @GA22 IS NULL SET @GA22 = N''; IF @GA23 IS NULL SET @GA23 = N''; IF @GA24 IS NULL SET @GA24 = N''; IF @GA25 IS NULL SET @GA25 = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_Product; -- STEP 0.2 - Protect input parameters SET @ProductID = system.fProtectID (@ProductID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @FactoryID = system.fProtectID (@FactoryID); SET @TimeType = system.fProtectString (@TimeType); SET @NameShort = system.fProtectString (@NameShort); SET @NameLong = system.fProtectString (@NameLong); SET @CommentUser = system.fProtectString (@CommentUser); SET @CommentDev = system.fProtectString (@CommentDev); SET @ResponsiblePerson = system.fProtectString (@ResponsiblePerson); SET @ImageName = system.fProtectString (@ImageName); SET @Status = system.fProtectString (@Status); SET @Template = system.fProtectString (@Template); SET @TemplateVersion = system.fProtectString (@TemplateVersion); SET @GA1 = system.fProtectString (@GA1); SET @GA2 = system.fProtectString (@GA2); SET @GA3 = system.fProtectString (@GA3); SET @GA4 = system.fProtectString (@GA4); SET @GA5 = system.fProtectString (@GA5); SET @GA6 = system.fProtectString (@GA6); SET @GA7 = system.fProtectString (@GA7); SET @GA8 = system.fProtectString (@GA8); SET @GA9 = system.fProtectString (@GA9); SET @GA10 = system.fProtectString (@GA10); SET @GA11 = system.fProtectString (@GA11); SET @GA12 = system.fProtectString (@GA12); SET @GA13 = system.fProtectString (@GA13); SET @GA14 = system.fProtectString (@GA14); SET @GA15 = system.fProtectString (@GA15); SET @GA16 = system.fProtectString (@GA16); SET @GA17 = system.fProtectString (@GA17); SET @GA18 = system.fProtectString (@GA18); SET @GA19 = system.fProtectString (@GA19); SET @GA20 = system.fProtectString (@GA20); SET @GA21 = system.fProtectString (@GA21); SET @GA22 = system.fProtectString (@GA22); SET @GA23 = system.fProtectString (@GA23); SET @GA24 = system.fProtectString (@GA24); SET @GA25 = system.fProtectString (@GA25); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID; IF @FactoryKey = 0 OR @ProductLineKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 1.3 - If ProductID * is submitted, search the next free numeric ProductID IF @ProductID = '*' BEGIN SELECT @ProductID = COALESCE(MAX(CAST(ProductID AS BIGINT)),0)+1 FROM planning.tdProducts dP WHERE dP.ProductLineKey = @ProductLineKey AND TRY_CAST(dP.ProductID AS BIGINT) IS NOT NULL; -- if it there is no others Products in ProductLine`s IF @ProductID = '*' SELECT @ProductID = 1; END -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 3.1 - If Product dont exists, create it from the Template IF @ProductKey = 0 BEGIN DECLARE @TemplateFactoryID NVARCHAR(255) = N'ZT'; DECLARE @TemplateProductLineID NVARCHAR(255) = N''; DECLARE @TemplateProductID NVARCHAR(255) = N''; DECLARE @TemplateFactoryKey INT = 0; DECLARE @TemplateProductLineKey INT = 0; DECLARE @TemplateProductKey INT = 0; DECLARE @TemplateFactoryNameShort NVARCHAR(255) = N''; DECLARE @TemplateProductLineNameShort NVARCHAR(255) = N''; DECLARE @TemplateProductNameShort NVARCHAR(255) = N''; -- Determine Templateproduct - it will be searched only in Factory ZT SELECT TOP (1) @TemplateProductID = ProductID, @TemplateProductLineID = ProductLineID FROM planning.tdProducts WHERE FactoryID = @TemplateFactoryID AND Template = @Template ORDER BY ProductID; IF @TemplateProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Template ProductID not found', 16, 10); END; -- Determine Template Keys SELECT @TemplateFactoryKey = FactoryKey ,@TemplateFactoryNameShort = NameShort FROM planning.tdFactories WHERE FactoryID = @TemplateFactoryID; SELECT TOP (1) @TemplateProductLineKey = ProductLineKey ,@TemplateProductLineNameShort = NameShort FROM planning.tdProductLines WHERE FactoryKey = @TemplateFactoryKey AND ProductLineID = @TemplateProductLineID ORDER BY ProductLineID; SELECT TOP (1) @TemplateProductKey = ProductKey ,@TemplateProductNameShort = NameShort FROM planning.tdProducts WHERE FactoryKey = @TemplateFactoryKey AND ProductLineKey = @TemplateProductLineKey AND ProductID = @TemplateProductID ORDER BY ProductID; IF @TemplateFactoryKey = 0 OR @TemplateProductLineKey = 0 OR @TemplateProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Template keys not found', 16, 10); END; -- Copy Template Product -- dProducts DECLARE @ProductKeyTable TABLE (KeyNumber BIGINT NOT NULL); INSERT INTO planning.tdProducts ( ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,TimeType ,NameShort ,NameLong ,CommentUser ,CommentDev ,ResponsiblePerson ,ImageName ,Status ,Template ,TemplateVersion ,GlobalAttribute1 ,GlobalAttribute2 ,GlobalAttribute3 ,GlobalAttribute4 ,GlobalAttribute5 ,GlobalAttribute6 ,GlobalAttribute7 ,GlobalAttribute8 ,GlobalAttribute9 ,GlobalAttribute10 ,GlobalAttribute11 ,GlobalAttribute12 ,GlobalAttribute13 ,GlobalAttribute14 ,GlobalAttribute15 ,GlobalAttribute16 ,GlobalAttribute17 ,GlobalAttribute18 ,GlobalAttribute19 ,GlobalAttribute20 ,GlobalAttribute21 ,GlobalAttribute22 ,GlobalAttribute23 ,GlobalAttribute24 ,GlobalAttribute25 ) OUTPUT INSERTED.ProductKey INTO @ProductKeyTable SELECT @ProductLineKey, @FactoryKey, @ProductID, @ProductLineID, @FactoryID, TimeType, @NameShort, NameLong, CommentUser, CommentDev, ResponsiblePerson, ImageName, [Status], Template, TemplateVersion, GlobalAttribute1, GlobalAttribute2, GlobalAttribute3, GlobalAttribute4, GlobalAttribute5, GlobalAttribute6, GlobalAttribute7, GlobalAttribute8, GlobalAttribute9, GlobalAttribute10, GlobalAttribute11, GlobalAttribute12, GlobalAttribute13, GlobalAttribute14, GlobalAttribute15, GlobalAttribute16, GlobalAttribute17, GlobalAttribute18, GlobalAttribute19, GlobalAttribute20, GlobalAttribute21, GlobalAttribute22, GlobalAttribute23, GlobalAttribute24, GlobalAttribute25 FROM planning.tdProducts WHERE ProductKey = @TemplateProductKey; SET @EffectedRows += @@ROWCOUNT; SELECT TOP (1) @ProductKey = KeyNumber FROM @ProductKeyTable ORDER BY KeyNumber; --gProducts INSERT INTO planning.tgProducts ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,PropertyID ,PropertyName ,CommentUser ,CommentDev ,Unit ,ValueText ,ValueInt ,Scale ,IsROSystemProperty ,FormatID ) SELECT @ProductKey, @ProductLineKey, @FactoryKey, @ProductID, @ProductLineID, @FactoryID, PropertyID, PropertyName, CommentUser, CommentDev, Unit, ValueText, ValueInt, Scale, IsROSystemProperty, FormatID FROM planning.tgProducts WHERE ProductKey = @TemplateProductKey; SET @EffectedRows += @@ROWCOUNT; --dValueSeries INSERT INTO planning.tdValueSeries ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,ValueSeriesNo ,NameShort ,NameLong ,CommentUser ,CommentDev ,ImageName ,IsNumeric ,VisibilityLevel ,ValueSource ,ValueListID ,ValueFormatID ,Unit ,Scale ,Effect ,EffectParameter ,AllowZero ) SELECT @ProductKey ,@ProductLineKey ,@FactoryKey ,@ProductID ,@ProductLineID ,@FactoryID ,ValueSeriesID ,ValueSeriesNo ,NameShort ,NameLong ,CommentUser ,CommentDev ,ImageName ,[IsNumeric] ,VisibilityLevel ,ValueSource ,ValueListID ,ValueFormatID ,Unit ,Scale ,Effect ,EffectParameter ,AllowZero FROM planning.tdValueSeries WHERE ProductKey = @TemplateProductKey; SET @EffectedRows += @@ROWCOUNT; --dTime INSERT INTO planning.tdTime ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,TimeID ,FormatID ) SELECT @ProductKey ,@ProductLineKey ,@FactoryKey ,@ProductID ,@ProductLineID ,@FactoryID ,TimeID ,FormatID FROM planning.tdTime WHERE ProductKey = @TemplateProductKey; SET @EffectedRows += @@ROWCOUNT; --fValues ;WITH VS AS ( SELECT ValueSeriesKey, ProductKey, ValueSeriesID FROM planning.tdValueSeries WHERE ProductKey = @ProductKey ) INSERT INTO planning.tfValues ( ValueSeriesKey ,ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueText ,ValueComment ) SELECT VS.ValueSeriesKey ,@ProductKey ,@ProductLineKey ,@FactoryKey ,@ProductID ,@ProductLineID ,@FactoryID ,fV.ValueSeriesID ,fV.TimeID ,fV.ValueFormula ,fV.ValueInt ,fV.ValueText ,fV.ValueComment FROM planning.tfValues fV LEFT JOIN VS ON VS.ValueSeriesID= fV.ValueSeriesID WHERE fV.ProductKey = @TemplateProductKey; SET @EffectedRows += @@ROWCOUNT; --fStatements - create copy informations INSERT INTO planning.tfStatements ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ActionType ,Statement ,UserName ,PCName ,ProcessorCode ,IPAddresses ,Timestamp ,IsDeleted ,IsResolved ) SELECT @ProductKey ,@ProductLineKey ,@FactoryKey ,@ProductID ,@ProductLineID ,@FactoryID ,N'COPY' AS ActionType ,N'Product produced as a copy of Factory <' + @TemplateFactoryID + N'>"' + @TemplateFactoryNameShort + N'"/ ProductLine <' + @TemplateProductLineID + N'>"' + @TemplateProductLineNameShort + N'"/ Product <' + @TemplateProductID + N'>"' + @TemplateProductNameShort + N'"' AS Statement ,@TransactUsername AS UserName ,N'' AS PCName ,N'' AS ProcessorCode ,N'' AS IPAddresses ,GETUTCDATE() AS Timestamp ,0 AS IsDeleted ,0 AS IsResolved; SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = 201 END; -- STEP 3.2 - Update the Product with the commited attributes UPDATE planning.tdProducts SET TimeType = IIF(@Template = N'<#NV>', TimeType, RIGHT(@Template, 2)) ,NameShort = IIF(@NameShort = N'<#NV>', NameShort, @NameShort) ,NameLong = IIF(@NameLong = N'<#NV>', NameLong, @NameLong) ,CommentUser = IIF(@CommentUser = N'<#NV>', CommentUser, @CommentUser) ,CommentDev = IIF(@CommentDev = N'<#NV>', CommentDev, @CommentDev) ,ResponsiblePerson = IIF(@ResponsiblePerson = N'<#NV>', ResponsiblePerson, @ResponsiblePerson) ,ImageName = IIF(@ImageName = N'<#NV>', ImageName, @ImageName) ,[Status] = IIF(@Status = N'<#NV>', [Status], @Status) ,Template = IIF(@Template = N'<#NV>', Template, @Template) ,TemplateVersion = IIF(@TemplateVersion = N'<#NV>', TemplateVersion, @TemplateVersion) ,GlobalAttribute1 = IIF(@GA1 = N'<#NV>', GlobalAttribute1, @GA1) ,GlobalAttribute2 = IIF(@GA2 = N'<#NV>', GlobalAttribute2, @GA2) ,GlobalAttribute3 = IIF(@GA3 = N'<#NV>', GlobalAttribute3, @GA3) ,GlobalAttribute4 = IIF(@GA4 = N'<#NV>', GlobalAttribute4, @GA4) ,GlobalAttribute5 = IIF(@GA5 = N'<#NV>', GlobalAttribute5, @GA5) ,GlobalAttribute6 = IIF(@GA6 = N'<#NV>', GlobalAttribute6, @GA6) ,GlobalAttribute7 = IIF(@GA7 = N'<#NV>', GlobalAttribute7, @GA7) ,GlobalAttribute8 = IIF(@GA8 = N'<#NV>', GlobalAttribute8, @GA8) ,GlobalAttribute9 = IIF(@GA9 = N'<#NV>', GlobalAttribute9, @GA9) ,GlobalAttribute10 = IIF(@GA10 = N'<#NV>', GlobalAttribute10, @GA10) ,GlobalAttribute11 = IIF(@GA11 = N'<#NV>', GlobalAttribute11, @GA11) ,GlobalAttribute12 = IIF(@GA12 = N'<#NV>', GlobalAttribute12, @GA12) ,GlobalAttribute13 = IIF(@GA13 = N'<#NV>', GlobalAttribute13, @GA13) ,GlobalAttribute14 = IIF(@GA14 = N'<#NV>', GlobalAttribute14, @GA14) ,GlobalAttribute15 = IIF(@GA15 = N'<#NV>', GlobalAttribute15, @GA15) ,GlobalAttribute16 = IIF(@GA16 = N'<#NV>', GlobalAttribute16, @GA16) ,GlobalAttribute17 = IIF(@GA17 = N'<#NV>', GlobalAttribute17, @GA17) ,GlobalAttribute18 = IIF(@GA18 = N'<#NV>', GlobalAttribute18, @GA18) ,GlobalAttribute19 = IIF(@GA19 = N'<#NV>', GlobalAttribute19, @GA19) ,GlobalAttribute20 = IIF(@GA20 = N'<#NV>', GlobalAttribute20, @GA20) ,GlobalAttribute21 = IIF(@GA21 = N'<#NV>', GlobalAttribute21, @GA21) ,GlobalAttribute22 = IIF(@GA22 = N'<#NV>', GlobalAttribute22, @GA22) ,GlobalAttribute23 = IIF(@GA23 = N'<#NV>', GlobalAttribute23, @GA23) ,GlobalAttribute24 = IIF(@GA24 = N'<#NV>', GlobalAttribute24, @GA24) ,GlobalAttribute25 = IIF(@GA25 = N'<#NV>', GlobalAttribute25, @GA25) FROM planning.tdProducts WHERE ProductKey = @ProductKey; SET @EffectedRows += @@ROWCOUNT; COMMIT TRANSACTION sx_pf_POST_Product; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_Product; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Product' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for one Product Not existing Product will be created if it exists in ZT Factory. Existing Product updated If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TimeType'; SET @value = N'TimeType'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@NameShort'; SET @value = N'NameShort'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@NameLong'; SET @value = N'NameLong'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentUser'; SET @value = N'CommentUser'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentDev'; SET @value = N'CommentDev'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ResponsiblePerson'; SET @value = N'ResponsiblePerson'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ImageName'; SET @value = N'ImageName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Status'; SET @value = N'Status'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Template'; SET @value = N'Template'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TemplateVersion'; SET @value = N'TemplateVersion'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA1'; SET @value = N'GA1'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA2'; SET @value = N'GA2'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA3'; SET @value = N'GA3'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA4'; SET @value = N'GA4'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA5'; SET @value = N'GA5'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA6'; SET @value = N'GA6'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA7'; SET @value = N'GA7'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA8'; SET @value = N'GA8'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA9'; SET @value = N'GA9'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA10'; SET @value = N'GA10'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA11'; SET @value = N'GA11'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA12'; SET @value = N'GA12'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA13'; SET @value = N'GA13'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA14'; SET @value = N'GA14'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA15'; SET @value = N'GA15'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA16'; SET @value = N'GA16'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA17'; SET @value = N'GA17'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA18'; SET @value = N'GA18'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA19'; SET @value = N'GA19'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA20'; SET @value = N'GA20'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA21'; SET @value = N'GA21'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA22'; SET @value = N'GA22'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA23'; SET @value = N'GA23'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA24'; SET @value = N'GA24'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA25'; SET @value = N'GA25'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_ProductDataTableValues; GO /* POST Operation for all values of the ProductDataTable The method can be switched between incremental POST and full POST Gerd Tautenhahn for Saxess Software GmbH Last modified: 12/2022 for OCT 5.8 Concept: - all Values are deliverd as Textarray - this requires a big split operation - the Text array must also deliver deleted values if it does a incrementel transmisson (deleted Values are delivered with empty ValueInt, ValueText, ValueFormula) - Values may be sended with '<#NV>' to keep existing values @ValuesInBracketsCommaSeparated format is: (ValueSeriesID,TimeID,Formula,Value,ValueText,ValueComment),(ValueSeriesIDTimeID,Formula,Value,ValueText,ValueComment) The whole string is single quoted and every parameter inside except the TimeID is single quoted if you check this in SQL Server Profiler or Table sx_pf_APILog, every String must have two inverted commas, the whole Parameter must have Single inverted comma this must show profiler: '[(''ValueSeriesID'',TimeID,''Formula'',''ValueInt'',''ValueText'',''ValueComment'')],[(''ValueSeriesID'',TimeID,''Formula'',''ValueInt'',''ValueText'',''ValueComment'')]' Informal: In further releases the )],[( delimiter was planend be replaced with CHAR(29) - which is no longer likely, as we will use JSON But CHAR(29) is at the moment used at internal temporary delimiter Test call 1. Empty FactoryID / ProductLineID / ProductID / ValueSeriesID / TimeID => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. Username does not have write access to ProductLine => 401 Unauthorized 4. Non exists Factory / ProductLine / Product keys => 404 Not Found 5. If ValueSeriesID not exists in planning.tdValueSeries for this ProductKey => 403 Forbidden 6. Product updated => 200 OK Test case 1 - Incremental Sending *************************************************************** 1. One Value for an existing Value must replace them 2. One Value for an not exiting Value must create it 3. If all Parameter are posted empty, the value is deleted 4. If one or more of the parameter are sended with <#NV> there Value is replaced with the existing value (or nothing if it exists not) 5. An ZeroEmpty Value in old or sended values is deleted DECLARE @RC INT DECLARE @Username NVARCHAR (255) = 'SQL' DECLARE @ProductID NVARCHAR (255) = '1' DECLARE @ProductLineID NVARCHAR (255) = 'U' DECLARE @FactoryID NVARCHAR (255) = 'ZT' DECLARE @IsIncrementalValuesFlag INT = 1 -- 3 Values with comments - NEVER place a linebreak in the sample array ! -- DECLARE @ValuesInBracketsCommaSeparated nvarchar(max) = '[(''K1'',20170115,''=IF(H25=4,3,2.1)'',''6'',''HaseTextValue'',''HaseComment'')],[(''K2'',20170215,''=IF(H25=4,3,2)'',''555'',''HaseTextValue'',''HaseComment2'')],[(''E1'',20170515,'''','''',''Text'',''Comment'')]' -- Delete the three values by sending them empty -- DECLARE @ValuesInBracketsCommaSeparated nvarchar(max) = '[(''K1'',20170115,'''','''','''',''HaseComment'')],[(''K2'',20170215,'''','''','''',''HaseComment2'')],[(''E1'',20170515,'''','''','''','''')]' -- Send an <#NV> Value for an existing numeric and an Text Value and delete one -- DECLARE @ValuesInBracketsCommaSeparated nvarchar(max) = '[(''K1'',20170115,'''',''<#NV>'','''',''HaseComment'')],[(''K2'',20170215,'''','''','''',''HaseComment2'')],[(''E1'',20170515,'''','''',''<#NV>'','''')]' EXECUTE @RC = planning.spPOST_ProductDataTableValues @Username ,@ProductID ,@ProductLineID ,@FactoryID ,@IsIncrementalValuesFlag ,@ValuesInBracketsCommaSeparated; PRINT @RC; Test case 2 - fill all Values of an Product ************************************************ DECLARE @RC INT DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @ProductID NVARCHAR(255) = '1' DECLARE @ProductLineID NVARCHAR(255) = 'U' DECLARE @FactoryID NVARCHAR(255) = 'ZT' DECLARE @IsIncrementalValuesFlag INT = 0 DECLARE @ValuesInBracketsCommaSeparated nvarchar(max) = '' DECLARE @start_time DATETIME -- Generate fictive values for all TimeIDs of Products SELECT @ValuesInBracketsCommaSeparated = @ValuesInBracketsCommaSeparated + '[(''' + dVS.ValueSeriesID +''',' + CAST(TimeID AS nvarchar(255)) +',''=A2'',''1000'',''Hase | Maus'',''Komment|ar'')],' FROM planning.tdValueSeries dVS LEFT JOIN planning.tdTime dT ON dVS.ProductKey = dT.ProductKey WHERE dVS.FactoryID = @FactoryID AND dVS.ProductLineID = @ProductLineID AND dVS.ProductID = @ProductID SET @ValuesInBracketsCommaSeparated = LEFT(@ValuesInBracketsCommaSeparated, LEN(@ValuesInBracketsCommaSeparated) -1) PRINT @ValuesInBracketsCommaSeparated SET @start_time = GETUTCDATE() EXECUTE @RC = planning.spPOST_ProductDataTableValues @Username ,@ProductID ,@ProductLineID ,@FactoryID ,@IsIncrementalValuesFlag ,@ValuesInBracketsCommaSeparated; PRINT @RC; SELECT RTRIM(CAST(DATEDIFF(MS, @start_time, GETUTCDATE()) AS CHAR(10))) AS 'TimeTaken' -- Testcase 3 - delete all values DECLARE @RC INT DECLARE @Username NVARCHAR (255) = 'SQL' DECLARE @ProductID NVARCHAR (255) = '1' DECLARE @ProductLineID NVARCHAR (255) = 'U' DECLARE @FactoryID NVARCHAR (255) = 'ZT' DECLARE @IsIncrementalValuesFlag INT = 0 DECLARE @ValuesInBracketsCommaSeparated NVARCHAR (255) = '' EXECUTE @RC = planning.spPOST_ProductDataTableValues @Username ,@ProductID ,@ProductLineID ,@FactoryID ,@IsIncrementalValuesFlag ,@ValuesInBracketsCommaSeparated; PRINT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ProductDataTableValues',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ProductDataTableValues','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_ProductDataTableValues @Username NVARCHAR(255), @ProductID NVARCHAR(255), @ProductLineID NVARCHAR(255), @FactoryID NVARCHAR(255), @IsIncrementalValuesFlag INT, @ValuesInBracketsCommaSeparated NVARCHAR(MAX) /* */ AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @errorvalue NVARCHAR(255); DECLARE @errormessage NVARCHAR(2000); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username , N'NULL') + N''',''' + ISNULL(@ProductID , N'NULL') + N''',''' + ISNULL(@ProductLineID , N'NULL') + N''',''' + ISNULL(@FactoryID , N'NULL') + N''',' + ISNULL(CAST(@IsIncrementalValuesFlag AS NVARCHAR(255)) , N'NULL') + N',''' + ISNULL(REPLACE(@ValuesInBracketsCommaSeparated, N'''', N'''''') , N'NULL') + N''''; -- WHY the replace - as there are double inverted comma's ? DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET Debug Flag for debugging and disable Transcation DECLARE @DebugFlag INT = 0 /* --Use for debugging SELECT * FROM ##Debug_ValuesInBracketsReceived SELECT * FROM ##Debug_ValueMergeTableFilled ORDER BY ValueSeriesID,TimeID SELECT * FROM ##Debug_ValueMergeTableBeforeTransmissions ORDER BY ValueSeriesID,TimeID */ -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @IsIncrementalValuesFlag IS NULL SET @IsIncrementalValuesFlag = 0; IF @ValuesInBracketsCommaSeparated IS NULL SET @ValuesInBracketsCommaSeparated = N''; -- To delete Values in NOT Incremental Transmission, an empty @ValuesInBracketsCommaSeparated Parameter is sended IF @ValuesInBracketsCommaSeparated = '' AND @IsIncrementalValuesFlag = 0 BEGIN SET @ValuesInBracketsCommaSeparated = '[(''DeleteFlagID'',99999999,'''',''0'','''','''')]' --special ID with prevents from rejection from errorhandler END BEGIN TRY BEGIN TRANSACTION POST_PDT; -- STEP 0.2 - Protect input parameters SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductID = system.fProtectID (@ProductID); SET @ProductLineID = system.fProtectID (@ProductLineID); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters, one or more IDs are empty', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID; IF @FactoryKey = 0 OR @ProductLineKey = 0 OR @ProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('One or more IDs dont exist (No Keys found)', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 BEGIN SET @errormessage = 'Invalid rights - no write Rights for ' + @TransactUsername + '.'; RAISERROR(@errormessage, 16, 10); END -- STEP 3.0 - Store Timeline, only TimeID, as Timeline is not changed during Posting IF OBJECT_ID('tempdb..#Timeline') IS NOT NULL DROP TABLE #Timeline; CREATE TABLE #Timeline ( TimeID NVARCHAR(10) COLLATE DATABASE_DEFAULT NOT NULL UNIQUE CLUSTERED ); INSERT INTO #Timeline SELECT TimeID FROM planning.tdTime WHERE ProductKey = @ProductKey; -- STEP 3.1 - ValueMergeTable Merge the current values of the product into the ValueMergeTable IF OBJECT_ID('tempdb..#ValueMergeTable') IS NOT NULL DROP TABLE #ValueMergeTable; CREATE TABLE #ValueMergeTable ( ValueSeriesKey BIGINT NOT NULL ,ValueSeriesID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,TimeID NVARCHAR(10) COLLATE DATABASE_DEFAULT NOT NULL ,ValueFormula NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueInt NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ValueText NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueComment NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,[IsNumeric] INT NOT NULL ,IsFormula INT NOT NULL ,ProcessCode NVARCHAR(20) COLLATE DATABASE_DEFAULT NOT NULL ,DocuCode NVARCHAR(20) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesID_TimeID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,AllowZero INT NOT NULL ); -- First Type "Old Values" is loaded in ValueMergeTable INSERT INTO #ValueMergeTable SELECT 0 AS ValueSeriesKey -- No Values from dValueSeries, this Informations are later added when old and sended Values are together ,ValueSeriesID ,v.TimeID ,ValueFormula ,ValueInt ,ValueText ,ValueComment ,0 AS IsNumeric ,0 AS IsFormula ,N'OLD VALUES' AS ProcessCode ,N'NONE' AS DocuCode ,ValueSeriesID + CAST(v.TimeID AS NVARCHAR(10)) AS ValueSeries_TimeID ,0 AS AllowZero FROM planning.tfValues v INNER JOIN #Timeline tl ON v.TimeID = tl.TimeID WHERE ProductKey = @ProductKey; -- Store Header, which is dValueSeries Information which will added to old and sended Values IF OBJECT_ID('tempdb..#Header') IS NOT NULL DROP TABLE #Header; CREATE TABLE #Header ( ValueSeriesKey BIGINT NOT NULL ,ValueSeriesID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,[IsNumeric] INT NOT NULL ,IsFormula INT NOT NULL ,AllowZero INT NOT NULL ,UNIQUE CLUSTERED (ValueSeriesID, ValueSeriesKey, [IsNumeric], IsFormula) ); INSERT INTO #Header SELECT ValueSeriesKey ,ValueSeriesID ,[IsNumeric] ,IIF(ValueSource IN (N'XLS',N'XLS-Strict'), 1, 0) AS IsFormula ,AllowZero FROM planning.tdValueSeries WHERE ProductKey = @ProductKey; -- STEP 3.3 - Check for count of delimiter in Array DECLARE @OpenDelimetersCount INT = 0; DECLARE @DelimetersIndex INT = CHARINDEX(N'[(', @ValuesInBracketsCommaSeparated); WHILE @DelimetersIndex > 0 BEGIN SET @OpenDelimetersCount += 1; SET @DelimetersIndex = CHARINDEX(N'[(', @ValuesInBracketsCommaSeparated, @DelimetersIndex + 1); END; IF @OpenDelimetersCount > 32765 BEGIN SET @ResultCode = 403; RAISERROR('ValueArray to big (More then 32750 Values need special API Update.)', 16, 10); END; IF @OpenDelimetersCount = 0 BEGIN SET @ResultCode = 404; RAISERROR('No delimiters found in @ValuesInBracketsCommaSeparated', 16, 10); END; SET @DelimetersIndex = CHARINDEX(N')]', @ValuesInBracketsCommaSeparated); WHILE (@DelimetersIndex > 0) BEGIN SET @OpenDelimetersCount -= 1; SET @DelimetersIndex = CHARINDEX(N')]', @ValuesInBracketsCommaSeparated, @DelimetersIndex + 1); END; IF @OpenDelimetersCount <> 0 BEGIN SET @ResultCode = 404; RAISERROR('Invalid count of delimiter in @ValuesInBracketsCommaSeparated', 16, 10); END; -- STEP 3.4 - Splitt the Array with the new values for the Product to a temporary table ####################################################################################################################### -- cut leading/following brackets and ' from ValueSeriesID SET @ValuesInBracketsCommaSeparated = LEFT(@ValuesInBracketsCommaSeparated, LEN(@ValuesInBracketsCommaSeparated)-2); SET @ValuesInBracketsCommaSeparated = RIGHT(@ValuesInBracketsCommaSeparated, LEN(@ValuesInBracketsCommaSeparated)-2); DROP TABLE IF EXISTS #Values1; CREATE TABLE #Values1 ( txtValues NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,txtValuesPipe NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ); DROP TABLE IF EXISTS #Values2; CREATE TABLE #Values2 ( txtValues NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,txtValuesPipe NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ); DROP TABLE IF EXISTS #Values3; CREATE TABLE #Values3 ( txtValues NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,TimeID NVARCHAR(10) COLLATE DATABASE_DEFAULT NOT NULL ,txtValuesPipe NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ); DROP TABLE IF EXISTS #Values4; CREATE TABLE #Values4 ( txtValues NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,TimeID NVARCHAR(10) COLLATE DATABASE_DEFAULT NOT NULL ,ValueFormula NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,txtValuesPipe NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ); DROP TABLE IF EXISTS #Values5; CREATE TABLE #Values5 ( txtValues NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,TimeID NVARCHAR(10) COLLATE DATABASE_DEFAULT NOT NULL ,ValueFormula NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueInt NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,txtValuesPipe NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ); DROP TABLE IF EXISTS #Values6; CREATE TABLE #Values6 ( txtValues NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,TimeID NVARCHAR(10) COLLATE DATABASE_DEFAULT NOT NULL ,ValueFormula NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueInt NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,Teilstring1 NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ); DROP TABLE IF EXISTS #Values7; CREATE TABLE #Values7 ( txtValues NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,TimeID NVARCHAR(10) COLLATE DATABASE_DEFAULT NOT NULL ,ValueFormula NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueInt NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,Teilstring1 NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,Teilstring2 NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ); DROP TABLE IF EXISTS #Values8; CREATE TABLE #Values8 ( txtValues NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,TimeID NVARCHAR(10) COLLATE DATABASE_DEFAULT NOT NULL ,ValueFormula NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueInt NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,Teilstring1 NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,Teilstring2 NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,TrennerIndex INT NOT NULL ,ValueText NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ); DROP TABLE IF EXISTS #Values9; CREATE TABLE #Values9 ( ValueSeriesID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,TimeID NVARCHAR(10) COLLATE DATABASE_DEFAULT NOT NULL ,ValueFormula NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueInt NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ValueText NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueComment NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ); DECLARE @spr NCHAR (1) = CHAR(29) -- Remove existing CHAR(29) = not visible character from string SET @ValuesInBracketsCommaSeparated = REPLACE(@ValuesInBracketsCommaSeparated,@spr,N''); --PRINT @ValuesInBracketsCommaSeparated -- Split String into Rows - all Values except the TimeID are single quoted after that an separated by @spr INSERT INTO #Values1 SELECT txtValues, REPLACE(REPLACE(txtValues,N',''', @spr + ''''), N''',', '''' + @spr) FROM system.fPivotStringIntoTable(@ValuesInBracketsCommaSeparated, N')],[('); --SELECT * FROM #Values1 -- Split string into two Columns (separate first column ValueSeriesID) INSERT INTO #Values2 SELECT txtValues ,LEFT(txtValuesPipe, CHARINDEX(@spr, txtValuesPipe) - 1) ,RIGHT(txtValuesPipe, LEN(txtValuesPipe) - CHARINDEX(@spr, txtValuesPipe)) FROM #Values1; --SELECT * FROM #Values2 -- Split last columns once more (separate TimeID) INSERT INTO #Values3 SELECT txtValues ,ValueSeriesID ,LEFT(txtValuesPipe, CHARINDEX(@spr, txtValuesPipe) - 1) ,RIGHT(txtValuesPipe, LEN(txtValuesPipe) - CHARINDEX(@spr, txtValuesPipe)) FROM #Values2; --SELECT * FROM #Values3 -- Delete Implicit sended periods (dont exit in current Product) DELETE FROM #Values3 WHERE TimeID NOT IN (SELECT CAST(TimeID AS NVARCHAR(10)) FROM #Timeline); --SELECT * FROM #Values3 -- Split last columns once more (separate ValueFormula) INSERT INTO #Values4 SELECT txtValues ,ValueSeriesID ,TimeID ,LEFT(txtValuesPipe, CHARINDEX(@spr, txtValuesPipe) - 1) ,RIGHT(txtValuesPipe, LEN(txtValuesPipe) - CHARINDEX(@spr, txtValuesPipe)) FROM #Values3; --SELECT * FROM #Values4 INSERT INTO #Values5 SELECT txtValues ,ValueSeriesID ,TimeID ,ValueFormula ,LEFT(txtValuesPipe, CHARINDEX(@spr, txtValuesPipe) - 1) ,RIGHT(txtValuesPipe, LEN(txtValuesPipe) - CHARINDEX(@spr, txtValuesPipe)) FROM #Values4; --SELECT * FROM #Values5 INSERT INTO #Values6 SELECT txtValues ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueSeriesID + ',' + TimeID + + ',' + ValueFormula + ',' + ValueInt + ',' AS Teilstring1 FROM #Values5; --SELECT * FROM #Values6 INSERT INTO #Values7 SELECT txtValues ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,Teilstring1 ,REPLACE(txtValues,Teilstring1,'') FROM #Values6; --SELECT * FROM #Values7 INSERT INTO #Values8 SELECT txtValues ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,Teilstring1 ,Teilstring2 -- wenn das erste Hochkomma nicht berücksichtigt wird, -- kann der Kommatrenner ',' zwischen ValueText und Comment auch bei einer Texteingabe von einem alleinstehenden Komma gefunden werden ,CHARINDEX(''',''',Teilstring2,2) AS TrennerIndex ,LEFT(Teilstring2,CHARINDEX(''',''',Teilstring2,2)) AS ValueText FROM #Values7; -- SELECT * FROM #Values8 INSERT INTO #Values9 SELECT ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueText ,RIGHT(Teilstring2, LEN(Teilstring2) - (TrennerIndex + 1)) AS ValueComment FROM #Values8; -- Replace Quotes from all strings (all except TimeID) UPDATE #Values9 SET ValueSeriesID = RIGHT(LEFT(ValueSeriesID, (LEN(ValueSeriesID)-1)), LEN(ValueSeriesID)-2) ,ValueFormula = RIGHT(LEFT(ValueFormula, (LEN(ValueFormula)-1)), LEN(ValueFormula)-2) ,ValueInt = RIGHT(LEFT(ValueInt, (LEN(ValueInt)-1)), LEN(ValueInt)-2) ,ValueText = RIGHT(LEFT(ValueText, (LEN(ValueText)-1)), LEN(ValueText)-2) ,ValueComment = RIGHT(LEFT(ValueComment, (LEN(ValueComment)-1)), LEN(ValueComment)-2); -- Replace Linebreaks in ValueText, but keep them in ValueComment UPDATE #Values9 SET ValueText =REPLACE(REPLACE(ValueText,CHAR(13),''),CHAR(10),''); IF @DebugFlag = 1 BEGIN IF OBJECT_ID('tempdb..##Debug_Values9') IS NOT NULL DROP TABLE ##Debug_Values9; SELECT * INTO ##Debug_Values9 FROM #Values9; END; -- STEP 3.5 - UNION both temporary tables ####################################################################################################################### -- Second Typ "Sended Values" is loaded in ValueMergeTable INSERT INTO #ValueMergeTable SELECT 0 AS ValueSeriesKey ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueText ,ValueComment ,0 AS [IsNumeric] ,0 AS IsFormula ,N'SENDED VALUES' ,N'NONE' ,ValueSeriesID+TimeID ,0 AS AllowZero FROM #Values9; -- Add the header informations to the values UPDATE VMT SET VMT.ValueSeriesKey = H.ValueSeriesKey ,VMT.[IsNumeric] = H.[IsNumeric] ,VMT.IsFormula = H.IsFormula ,VMT.AllowZero = H.AllowZero FROM #ValueMergeTable VMT INNER JOIN #Header H ON VMT.ValueSeriesID = H.ValueSeriesID; -- check for not existing ValueSeriesIDs which may have been sended IF EXISTS(SELECT TOP (1) ValueSeriesID FROM #ValueMergeTable WHERE ValueSeriesKey = 0) BEGIN SELECT TOP(1) @errorvalue = ValueSeriesID FROM #ValueMergeTable WHERE ValueSeriesKey = 0 ORDER BY TimeID; IF @errorvalue = 'DeleteFlagID' BEGIN DELETE FROM #ValueMergeTable END ELSE BEGIN SET @errormessage = 'One or more sended ValueSeriesIDs were not found, e.g. '+ @errorvalue +'.' SET @ResultCode = 403; RAISERROR(@errormessage, 16, 10); END END; -- check if more than one value is sended for a ValueSeries/TimeID combination IF EXISTS( SELECT COUNT(TimeID) FROM #ValueMergeTable WHERE ProcessCode = 'SENDED VALUES' GROUP BY ValueSeriesID_TimeID HAVING COUNT(TimeID) > 1 ) BEGIN SET @ResultCode = 403; RAISERROR('Douplicate Values found in ValueArray', 16, 10); END; -- STEP 3.6 - Prepare the ValueMergeTable ####################################################################################################################### -- FLAG OLD Values the user want to delete in Case of Numeric Series without formula -- Value 0 maybe sended as '000' UPDATE #ValueMergeTable SET ProcessCode = N'OLD VALUES TO DELETE' WHERE ProcessCode = N'OLD VALUES' AND [IsNumeric] = 1 AND IsFormula = 0 AND ValueSeriesID_TimeID IN ( SELECT ValueSeriesID_TimeID FROM #ValueMergeTable WHERE ProcessCode = N'SENDED VALUES' AND (TRY_CAST(ValueInt AS BIGINT) = 0 OR ValueInt = N'') ); -- FLAG OLD Values the user want to delete in Case of Numeric Series with formula UPDATE #ValueMergeTable SET ProcessCode = N'OLD VALUES TO DELETE' WHERE ProcessCode = N'OLD VALUES' AND [IsNumeric] = 1 AND IsFormula = 1 AND ValueSeriesID_TimeID IN ( SELECT ValueSeriesID_TimeID FROM #ValueMergeTable WHERE ProcessCode = N'SENDED VALUES' AND ValueFormula = N'' AND (TRY_CAST(ValueInt AS BIGINT) = 0 OR ValueInt = N'') ); -- FLAG OLD Values the user want to delete in Case of NON-Numeric Series without formula UPDATE #ValueMergeTable SET ProcessCode = N'OLD VALUES TO DELETE' WHERE ProcessCode = N'OLD VALUES' AND [IsNumeric] = 0 AND IsFormula = 0 AND ValueSeriesID_TimeID IN ( SELECT ValueSeriesID_TimeID FROM #ValueMergeTable WHERE ProcessCode = N'SENDED VALUES' AND ValueText = N'' ); -- FLAG OLD Values the user want to delete in Case of NON-Numeric Series with formula UPDATE #ValueMergeTable SET ProcessCode = N'OLD VALUES TO DELETE' WHERE ProcessCode = N'OLD VALUES' AND [IsNumeric] = 0 AND IsFormula = 1 AND ValueSeriesID_TimeID IN ( SELECT ValueSeriesID_TimeID FROM #ValueMergeTable WHERE ProcessCode = N'SENDED VALUES' AND ValueText = N'' AND ValueFormula = N'' ); -- Debugging Output IF @DebugFlag = 1 BEGIN IF OBJECT_ID('tempdb..##Debug_ValueMergeTableFilled') IS NOT NULL DROP TABLE ##Debug_ValueMergeTableFilled; SELECT * INTO ##Debug_ValueMergeTableFilled FROM #ValueMergeTable; END; -- STEP 3.7 - Handle VMT ####################################################################################################################### -- Documentation of changes in log table /* -- NEW is a Value which is not contained in existing Values UPDATE #ValueMergeTable SET DocuCode = N'NEW VALUE' WHERE ProcessCode = N'SENDED VALUES' AND DocuCode = N'NONE' AND ValueSeriesID + N'_' + TimeID NOT IN ( SELECT ValueSeriesID + N'_' + TimeID FROM #ValueMergeTable WHERE ProcessCode = N'OLD VALUES' ); -- CHANGED is a Value which is contained in existing Values UPDATE #ValueMergeTable SET DocuCode = N'CHANGED VALUE' WHERE ProcessCode = N'SENDED VALUES' AND DocuCode = N'NONE' AND ValueSeriesID + N'_' + TimeID IN ( SELECT ValueSeriesID + N'_' + TimeID FROM #ValueMergeTable WHERE ProcessCode = N'OLD VALUES' ); INSERT INTO sx_pf_AuditLog VALUES SELECT * FROM #ValueMergeTable WHERE ValueSeriesID + '_' + CAST(TimeID AS NVARCHAR(10)) IN ( SELECT ValueSeriesID + '_' + CAST(TimeID AS NVARCHAR(10)) FROM #ValueMergeTable WHERE ProcessCode = 'SENDED VALUES' AND DocuCode != 'NONE' ) */ -- Handle <#NV> Parameters - Replace them with the old Values (if exist !) UPDATE VMT SET VMT.ValueInt = OV.ValueInt FROM #ValueMergeTable VMT , (SELECT ValueInt, ValueSeriesID, TimeID FROM #ValueMergeTable WHERE ProcessCode IN (N'OLD VALUES', N'OLD VALUES TO DELETE')) OV WHERE VMT.ProcessCode = N'SENDED VALUES' AND VMT.ValueInt LIKE N'<#NV>' AND VMT.ValueSeriesID = OV.ValueSeriesID AND VMT.TimeID = OV.TimeID; UPDATE VMT SET VMT.ValueText = OV.ValueText FROM #ValueMergeTable VMT , (SELECT ValueText, ValueSeriesID, TimeID FROM #ValueMergeTable WHERE ProcessCode IN (N'OLD VALUES', N'OLD VALUES TO DELETE')) OV WHERE VMT.ProcessCode = N'SENDED VALUES' AND VMT.ValueText = N'<#NV>' AND VMT.ValueSeriesID = OV.ValueSeriesID AND VMT.TimeID = OV.TimeID; UPDATE VMT SET VMT.ValueFormula = OV.ValueFormula FROM #ValueMergeTable VMT , (SELECT ValueFormula, ValueSeriesID, TimeID FROM #ValueMergeTable WHERE ProcessCode IN (N'OLD VALUES', N'OLD VALUES TO DELETE')) OV WHERE VMT.ProcessCode = N'SENDED VALUES' AND VMT.ValueFormula = N'<#NV>' AND VMT.ValueSeriesID = OV.ValueSeriesID AND VMT.TimeID = OV.TimeID; UPDATE VMT SET VMT.ValueComment = OV.ValueComment FROM #ValueMergeTable VMT , (SELECT ValueComment, ValueSeriesID, TimeID FROM #ValueMergeTable WHERE ProcessCode IN (N'OLD VALUES', N'OLD VALUES TO DELETE')) OV WHERE VMT.ProcessCode = N'SENDED VALUES' AND VMT.ValueComment = N'<#NV>' AND VMT.ValueSeriesID = OV.ValueSeriesID AND VMT.TimeID = OV.TimeID; -- Handle <#NV> Parameter, where no old Values existed UPDATE #ValueMergeTable SET ValueInt = N'0' WHERE ValueInt = N'<#NV>' OR [IsNumeric] = 0; UPDATE #ValueMergeTable SET ValueText = N'' WHERE ValueText = N'<#NV>' OR [IsNumeric] = 1; UPDATE #ValueMergeTable SET ValueFormula = N'' WHERE ValueFormula = N'<#NV>' OR IsFormula = 0; UPDATE #ValueMergeTable SET ValueComment = N'' WHERE ValueComment = N'<#NV>'; -- Debugging Output IF @DebugFlag = 1 BEGIN IF OBJECT_ID('tempdb..##Debug_ValueMergeTableBeforeTransmissions') IS NOT NULL DROP TABLE ##Debug_ValueMergeTableBeforeTransmissions; SELECT * INTO ##Debug_ValueMergeTableBeforeTransmissions FROM #ValueMergeTable; END; -- STEP 3.8 - Save data IF @IsIncrementalValuesFlag <> 1 BEGIN -- CASE FULL transmission ######################################### -- Delete all Values DELETE FROM planning.tfValues WHERE ProductKey = @ProductKey; SET @EffectedRows += @@ROWCOUNT; -- DELETE Useless NullEmpty Values in ALL Values -- einfache Nullwerte DELETE FROM #ValueMergeTable WHERE [IsNumeric] = 1 AND IsFormula = 0 AND COALESCE(TRY_CAST(ValueInt AS BIGINT),0) = 0 AND (RIGHT(ValueSeriesID,2) <> '_0' AND AllowZero = 0); -- Numerische Formelzellen ohne Formel mit Wert 0 DELETE FROM #ValueMergeTable WHERE [IsNumeric] = 1 AND IsFormula = 1 AND ValueFormula = '' AND COALESCE(TRY_CAST(ValueInt AS BIGINT),0) = 0 AND (RIGHT(ValueSeriesID,2) <> '_0' AND AllowZero = 0); -- Gelöschte Werte in numerischen Zellen DELETE FROM #ValueMergeTable WHERE [IsNumeric] = 1 AND IsFormula = 0 AND ValueInt = N''; -- einfach Leertexte DELETE FROM #ValueMergeTable WHERE [IsNumeric] = 0 AND IsFormula = 0 AND ValueText = N''; -- Nichtnumerische Formelzellen ohne Formel mit Leertext DELETE FROM #ValueMergeTable WHERE [IsNumeric] = 0 AND IsFormula = 1 AND ValueFormula = '' AND ValueText = N''; DELETE FROM #ValueMergeTable WHERE ProcessCode = N'SENDED VALUES' AND [IsNumeric] = 1 AND IsFormula = 1 AND ValueInt = N'' AND ValueFormula = N''; -- Insert all Values INSERT INTO planning.tfValues ( ValueSeriesKey ,ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueText ,ValueComment ) SELECT ValueSeriesKey ,@ProductKey ,@ProductLineKey ,@FactoryKey ,@ProductID ,@ProductLineID ,@FactoryID ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueText ,ValueComment FROM #ValueMergeTable WHERE ProcessCode IN (N'SENDED VALUES'); SET @EffectedRows += @@ROWCOUNT; END ELSE BEGIN -- CASE INCREMENTAL Transmission - it also leads to a full delete and insert !! ######################################### -- DELETE Values, which are sended to delete in OLD VALUES DELETE FROM #ValueMergeTable WHERE ProcessCode = N'OLD VALUES TO DELETE'; -- DELETE sended non-formula values DELETE FROM #ValueMergeTable WHERE ProcessCode = N'SENDED VALUES' AND [IsNumeric] = 1 AND IsFormula = 0 AND COALESCE(TRY_CAST(ValueInt AS BIGINT),0) = 0 AND (RIGHT(ValueSeriesID,2) <> '_0' AND AllowZero = 0); -- send Null Values for ValueSeries *_0; --empty values must delete 0 in _0 Series DELETE FROM #ValueMergeTable WHERE ProcessCode = N'SENDED VALUES' AND [IsNumeric] = 1 AND IsFormula = 0 AND ValueInt = '' AND (RIGHT(ValueSeriesID,2) = '_0' AND AllowZero = 0); -- delete deleted formulas DELETE FROM #ValueMergeTable WHERE ProcessCode = N'SENDED VALUES' AND [IsNumeric] = 1 AND IsFormula = 1 AND COALESCE(TRY_CAST(ValueInt AS BIGINT),0) = 0 AND ValueFormula = N'' AND (RIGHT(ValueSeriesID,2) <> '_0' AND AllowZero = 0); -- send Null Values for ValueSeries *_0; -- Delete DELETE FROM #ValueMergeTable WHERE ProcessCode = N'SENDED VALUES' AND [IsNumeric] = 0 AND IsFormula = 0 AND ValueText = N'' DELETE FROM #ValueMergeTable WHERE ProcessCode = N'SENDED VALUES' AND [IsNumeric] = 0 AND IsFormula = 1 AND ValueText = N'' AND ValueFormula = N''; DELETE FROM #ValueMergeTable WHERE ProcessCode = N'SENDED VALUES' AND [IsNumeric] = 1 AND IsFormula = 0 AND ValueInt = N''; DELETE FROM #ValueMergeTable WHERE ProcessCode = N'SENDED VALUES' AND [IsNumeric] = 1 AND IsFormula = 1 AND ValueInt = N'' AND ValueFormula = N''; -- DELETE Useless NullEmpty Values in OLD Values DELETE FROM #ValueMergeTable WHERE ProcessCode = N'OLD VALUES' AND [IsNumeric] = 1 AND IsFormula = 0 AND COALESCE(TRY_CAST(ValueInt AS BIGINT),0) = 0 AND (RIGHT(ValueSeriesID,2) <> '_0' AND AllowZero = 0); -- send Null Values for ValueSeries *_0 DELETE FROM #ValueMergeTable WHERE ProcessCode = N'OLD VALUES' AND [IsNumeric] = 0 AND IsFormula = 0 AND ValueText = N''; -- DELETE all Values DELETE FROM planning.tfValues WHERE ProductKey = @ProductKey; SET @EffectedRows += @@ROWCOUNT; -- Delete OLD VALUES which are new sended DELETE FROM #ValueMergeTable WHERE ProcessCode = N'OLD VALUES' AND ValueSeriesID_TimeID IN ( SELECT ValueSeriesID_TimeID FROM #ValueMergeTable WHERE ProcessCode = N'SENDED VALUES' ); -- Insert all Values INSERT INTO planning.tfValues ( ValueSeriesKey ,ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueText ,ValueComment ) SELECT ValueSeriesKey ,@ProductKey ,@ProductLineKey ,@FactoryKey ,@ProductID ,@ProductLineID ,@FactoryID ,ValueSeriesID ,TimeID ,ValueFormula ,CAST(ValueInt AS BIGINT) ,ValueText ,ValueComment FROM #ValueMergeTable WHERE ProcessCode IN (N'SENDED VALUES', N'OLD VALUES'); SET @EffectedRows += @@ROWCOUNT; END; --- materialize Product, if it is in ZT-PARAM IF @FactoryID = 'ZT' AND @ProductLineID LIKE 'PARAM%' BEGIN EXEC planning.spPOST_ParamTable @ProductID END COMMIT TRANSACTION POST_PDT; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION POST_PDT; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_ProductDataTableValues' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for all values of the ProductDataTable The method can be switched between incremental POST and full POST'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@IsIncrementalValuesFlag'; SET @value = N'IsIncrementalValuesFlag'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValuesInBracketsCommaSeparated'; SET @value = N'ValuesInBracketsCommaSeparated'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_ProductEmpty; GO /* POST Operation for one empty Product Not existing Product will be created, existing gets an Update If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty If product will have no TimeIDs, as it has no values If product will have no ValueSeries, when created new Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@ProductID NVARCHAR(255) = '53' ,@ProductLineID NVARCHAR(255) = 'U' ,@FactoryID NVARCHAR(255) = 'ZT' ,@TimeType NVARCHAR(255) = '<#NV>'--'VM' ,@NameShort NVARCHAR(255) = 'Testproduct' ,@NameLong NVARCHAR(255) = '3 test' ,@CommentUser NVARCHAR(255) = 'komment' ,@CommentDev NVARCHAR(255) = 'DEVKOmm' ,@ResponsiblePerson NVARCHAR(255) = 'RR' ,@ImageName NVARCHAR(255) = 'ImgName test' ,@Status NVARCHAR(255) = 'Aktiv' ,@Template NVARCHAR(255) = 'Test_VM' ,@TemplateVersion NVARCHAR(255) = '4.0' ,@GA1 NVARCHAR(255) = 'Wert für GA1'--'<#NV>' ,@GA2 NVARCHAR(255) = '<#NV>' ,@GA3 NVARCHAR(255) = '<#NV>' ,@GA4 NVARCHAR(255) = 'rer' ,@GA5 NVARCHAR(255) = '<#NV>' ,@GA6 NVARCHAR(255) = '<#NV>' ,@GA7 NVARCHAR(255) = '<#NV>' ,@GA8 NVARCHAR(255) = '<#NV>' ,@GA9 NVARCHAR(255) = '<#NV>' ,@GA10 NVARCHAR(255) = '<#NV>' ,@GA11 NVARCHAR(255) = '<#NV>' ,@GA12 NVARCHAR(255) = '<#NV>' ,@GA13 NVARCHAR(255) = '<#NV>' ,@GA14 NVARCHAR(255) = '<#NV>' ,@GA15 NVARCHAR(255) = '<#NV>' ,@GA16 NVARCHAR(255) = '<#NV>' ,@GA17 NVARCHAR(255) = '<#NV>' ,@GA18 NVARCHAR(255) = '<#NV>' ,@GA19 NVARCHAR(255) = '<#NV>' ,@GA20 NVARCHAR(255) = '<#NV>' ,@GA21 NVARCHAR(255) = '<#NV>' ,@GA22 NVARCHAR(255) = '<#NV>' ,@GA23 NVARCHAR(255) = '<#NV>' ,@GA24 NVARCHAR(255) = '<#NV>' ,@GA25 NVARCHAR(255) = '<#NV>' EXECUTE @RC = planning.spPOST_ProductEmpty @Username, @ProductID, @ProductLineID, @FactoryID, @TimeType, @NameShort, @NameLong, @CommentUser, @CommentDev, @ResponsiblePerson, @ImageName, @Status, @Template, @TemplateVersion, @GA1, @GA2, @GA3, @GA4, @GA5, @GA6, @GA7, @GA8, @GA9, @GA10, @GA11, @GA12, @GA13, @GA14, @GA15, @GA16, @GA17, @GA18, @GA19, @GA20, @GA21, @GA22, @GA23, @GA24, @GA25 PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ProductEmpty',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ProductEmpty','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_ProductEmpty @Username NVARCHAR(255), @ProductID NVARCHAR(255), @ProductLineID NVARCHAR(255), @FactoryID NVARCHAR(255), @TimeType NVARCHAR(255), @NameShort NVARCHAR(255), @NameLong NVARCHAR(255), @CommentUser NVARCHAR(MAX), @CommentDev NVARCHAR(MAX), @ResponsiblePerson NVARCHAR(255), @ImageName NVARCHAR(255), @Status NVARCHAR(255), @Template NVARCHAR(255), @TemplateVersion NVARCHAR(255), @GA1 NVARCHAR(255), @GA2 NVARCHAR(255), @GA3 NVARCHAR(255), @GA4 NVARCHAR(255), @GA5 NVARCHAR(255), @GA6 NVARCHAR(255), @GA7 NVARCHAR(255), @GA8 NVARCHAR(255), @GA9 NVARCHAR(255), @GA10 NVARCHAR(255), @GA11 NVARCHAR(255), @GA12 NVARCHAR(255), @GA13 NVARCHAR(255), @GA14 NVARCHAR(255), @GA15 NVARCHAR(255), @GA16 NVARCHAR(255), @GA17 NVARCHAR(255), @GA18 NVARCHAR(255), @GA19 NVARCHAR(255), @GA20 NVARCHAR(255), @GA21 NVARCHAR(255), @GA22 NVARCHAR(255), @GA23 NVARCHAR(255), @GA24 NVARCHAR(255), @GA25 NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username , N'NULL') + N''',''' + ISNULL(@ProductID , N'NULL') + N''',''' + ISNULL(@ProductLineID , N'NULL') + N''',''' + ISNULL(@FactoryID , N'NULL') + N''',''' + ISNULL(@TimeType , N'NULL') + N''',''' + ISNULL(@NameShort , N'NULL') + N''',''' + ISNULL(@NameLong , N'NULL') + N''',''' + ISNULL(@CommentUser , N'NULL') + N''',''' + ISNULL(@CommentDev , N'NULL') + N''',''' + ISNULL(@ResponsiblePerson , N'NULL') + N''',''' + ISNULL(@ImageName , N'NULL') + N''',''' + ISNULL(@Status , N'NULL') + N''',''' + ISNULL(@Template , N'NULL') + N''',''' + ISNULL(@TemplateVersion , N'NULL') + N''',''' + ISNULL(@GA1 , N'NULL') + N''',''' + ISNULL(@GA2 , N'NULL') + N''',''' + ISNULL(@GA3 , N'NULL') + N''',''' + ISNULL(@GA4 , N'NULL') + N''',''' + ISNULL(@GA5 , N'NULL') + N''',''' + ISNULL(@GA6 , N'NULL') + N''',''' + ISNULL(@GA7 , N'NULL') + N''',''' + ISNULL(@GA8 , N'NULL') + N''',''' + ISNULL(@GA9 , N'NULL') + N''',''' + ISNULL(@GA10 , N'NULL') + N''',''' + ISNULL(@GA11 , N'NULL') + N''',''' + ISNULL(@GA12 , N'NULL') + N''',''' + ISNULL(@GA13 , N'NULL') + N''',''' + ISNULL(@GA14 , N'NULL') + N''',''' + ISNULL(@GA15 , N'NULL') + N''',''' + ISNULL(@GA16 , N'NULL') + N''',''' + ISNULL(@GA17 , N'NULL') + N''',''' + ISNULL(@GA18 , N'NULL') + N''',''' + ISNULL(@GA19 , N'NULL') + N''',''' + ISNULL(@GA20 , N'NULL') + N''',''' + ISNULL(@GA21 , N'NULL') + N''',''' + ISNULL(@GA22 , N'NULL') + N''',''' + ISNULL(@GA23 , N'NULL') + N''',''' + ISNULL(@GA24 , N'NULL') + N''',''' + ISNULL(@GA25 , N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @TimeType IS NULL SET @TimeType = N''; IF @NameShort IS NULL SET @NameShort = N''; IF @NameLong IS NULL SET @NameLong = N''; IF @CommentUser IS NULL SET @CommentUser = N''; IF @CommentDev IS NULL SET @CommentDev = N''; IF @ResponsiblePerson IS NULL SET @ResponsiblePerson = N''; IF @ImageName IS NULL SET @ImageName = N''; IF @Status IS NULL SET @Status = N''; IF @Template IS NULL SET @Template = N''; IF @TemplateVersion IS NULL SET @TemplateVersion = N''; IF @GA1 IS NULL SET @GA1 = N''; IF @GA2 IS NULL SET @GA2 = N''; IF @GA3 IS NULL SET @GA3 = N''; IF @GA4 IS NULL SET @GA4 = N''; IF @GA5 IS NULL SET @GA5 = N''; IF @GA6 IS NULL SET @GA6 = N''; IF @GA7 IS NULL SET @GA7 = N''; IF @GA8 IS NULL SET @GA8 = N''; IF @GA9 IS NULL SET @GA9 = N''; IF @GA10 IS NULL SET @GA10 = N''; IF @GA11 IS NULL SET @GA11 = N''; IF @GA12 IS NULL SET @GA12 = N''; IF @GA13 IS NULL SET @GA13 = N''; IF @GA14 IS NULL SET @GA14 = N''; IF @GA15 IS NULL SET @GA15 = N''; IF @GA16 IS NULL SET @GA16 = N''; IF @GA17 IS NULL SET @GA17 = N''; IF @GA18 IS NULL SET @GA18 = N''; IF @GA19 IS NULL SET @GA19 = N''; IF @GA20 IS NULL SET @GA20 = N''; IF @GA21 IS NULL SET @GA21 = N''; IF @GA22 IS NULL SET @GA22 = N''; IF @GA23 IS NULL SET @GA23 = N''; IF @GA24 IS NULL SET @GA24 = N''; IF @GA25 IS NULL SET @GA25 = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_ProductEmpty; -- STEP 0.2 - Protect input parameters SET @ProductID = system.fProtectID (@ProductID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @FactoryID = system.fProtectID (@FactoryID); SET @TimeType = system.fProtectString (@TimeType); SET @NameShort = system.fProtectString (@NameShort); SET @NameLong = system.fProtectString (@NameLong); SET @CommentUser = system.fProtectString (@CommentUser); SET @CommentDev = system.fProtectString (@CommentDev); SET @ResponsiblePerson = system.fProtectString (@ResponsiblePerson); SET @ImageName = system.fProtectString (@ImageName); SET @Status = system.fProtectString (@Status); SET @Template = system.fProtectString (@Template); SET @TemplateVersion = system.fProtectString (@TemplateVersion); SET @GA1 = system.fProtectString (@GA1); SET @GA2 = system.fProtectString (@GA2); SET @GA3 = system.fProtectString (@GA3); SET @GA4 = system.fProtectString (@GA4); SET @GA5 = system.fProtectString (@GA5); SET @GA6 = system.fProtectString (@GA6); SET @GA7 = system.fProtectString (@GA7); SET @GA8 = system.fProtectString (@GA8); SET @GA9 = system.fProtectString (@GA9); SET @GA10 = system.fProtectString (@GA10); SET @GA11 = system.fProtectString (@GA11); SET @GA12 = system.fProtectString (@GA12); SET @GA13 = system.fProtectString (@GA13); SET @GA14 = system.fProtectString (@GA14); SET @GA15 = system.fProtectString (@GA15); SET @GA16 = system.fProtectString (@GA16); SET @GA17 = system.fProtectString (@GA17); SET @GA18 = system.fProtectString (@GA18); SET @GA19 = system.fProtectString (@GA19); SET @GA20 = system.fProtectString (@GA20); SET @GA21 = system.fProtectString (@GA21); SET @GA22 = system.fProtectString (@GA22); SET @GA23 = system.fProtectString (@GA23); SET @GA24 = system.fProtectString (@GA24); SET @GA25 = system.fProtectString (@GA25); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID; IF @FactoryKey = 0 OR @ProductLineKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 3.1 - If Product dont exists, create a new row in dProducts IF @ProductKey = 0 BEGIN --Handle wrong time types, cut them to two characters IF LEN(@TimeType) > 2 SET @TimeType = LEFT(@TimeType,2); --Create dProducts DECLARE @ProductKeyTable AS TABLE (KeyNumber BIGINT NOT NULL); INSERT INTO planning.tdProducts ( ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,TimeType ,NameShort ,NameLong ,CommentUser ,CommentDev ,ResponsiblePerson ,ImageName ,Status ,Template ,TemplateVersion ,GlobalAttribute1 ,GlobalAttribute2 ,GlobalAttribute3 ,GlobalAttribute4 ,GlobalAttribute5 ,GlobalAttribute6 ,GlobalAttribute7 ,GlobalAttribute8 ,GlobalAttribute9 ,GlobalAttribute10 ,GlobalAttribute11 ,GlobalAttribute12 ,GlobalAttribute13 ,GlobalAttribute14 ,GlobalAttribute15 ,GlobalAttribute16 ,GlobalAttribute17 ,GlobalAttribute18 ,GlobalAttribute19 ,GlobalAttribute20 ,GlobalAttribute21 ,GlobalAttribute22 ,GlobalAttribute23 ,GlobalAttribute24 ,GlobalAttribute25 ) OUTPUT Inserted.ProductKey INTO @ProductKeyTable VALUES (@ProductLineKey ,@FactoryKey ,@ProductID ,@ProductLineID ,@FactoryID ,@TimeType ,@NameShort ,@NameLong ,@CommentUser ,@CommentDev ,@ResponsiblePerson ,@ImageName ,@Status ,@Template ,@TemplateVersion ,IIF(@GA1 = N'<#NV>', N'', @GA1) ,IIF(@GA2 = N'<#NV>', N'', @GA2) ,IIF(@GA3 = N'<#NV>', N'', @GA3) ,IIF(@GA4 = N'<#NV>', N'', @GA4) ,IIF(@GA5 = N'<#NV>', N'', @GA5) ,IIF(@GA6 = N'<#NV>', N'', @GA6) ,IIF(@GA7 = N'<#NV>', N'', @GA7) ,IIF(@GA8 = N'<#NV>', N'', @GA8) ,IIF(@GA9 = N'<#NV>', N'', @GA9) ,IIF(@GA10 = N'<#NV>', N'', @GA10) ,IIF(@GA11 = N'<#NV>', N'', @GA11) ,IIF(@GA12 = N'<#NV>', N'', @GA12) ,IIF(@GA13 = N'<#NV>', N'', @GA13) ,IIF(@GA14 = N'<#NV>', N'', @GA14) ,IIF(@GA15 = N'<#NV>', N'', @GA15) ,IIF(@GA16 = N'<#NV>', N'', @GA16) ,IIF(@GA17 = N'<#NV>', N'', @GA17) ,IIF(@GA18 = N'<#NV>', N'', @GA18) ,IIF(@GA19 = N'<#NV>', N'', @GA19) ,IIF(@GA20 = N'<#NV>', N'', @GA20) ,IIF(@GA21 = N'<#NV>', N'', @GA21) ,IIF(@GA22 = N'<#NV>', N'', @GA22) ,IIF(@GA23 = N'<#NV>', N'', @GA23) ,IIF(@GA24 = N'<#NV>', N'', @GA24) ,IIF(@GA25 = N'<#NV>', N'', @GA25)); SET @EffectedRows += @@ROWCOUNT; SELECT @ProductKey = KeyNumber FROM @ProductKeyTable; --fStatements - create copy informations INSERT INTO planning.tfStatements ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ActionType ,Statement ,UserName ,PCName ,ProcessorCode ,IPAddresses ,Timestamp ,IsDeleted ,IsResolved ) SELECT @ProductKey, @ProductLineKey, @FactoryKey, @ProductID, @ProductLineID, @FactoryID, N'COPY' AS ActionType, N'Product imported by Single Product Import ' AS Statement, @TransactUsername AS UserName, N'' AS PCName, N'' AS ProcessorCode, N'' AS IPAddresses, GETUTCDATE() Timestamp, 0 AS IsDeleted, 0 AS IsResolved; SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = 201; END; -- STEP 3.2 - Update the Product with the commited attributes UPDATE planning.tdProducts SET TimeType = IIF(@Template = N'<#NV>', TimeType, RIGHT(@Template, 2)) ,NameShort = IIF(@NameShort = N'<#NV>', NameShort, @NameShort) ,NameLong = IIF(@NameLong = N'<#NV>', NameLong, @NameLong) ,CommentUser = IIF(@CommentUser = N'<#NV>', CommentUser, @CommentUser) ,CommentDev = IIF(@CommentDev = N'<#NV>', CommentDev, @CommentDev) ,ResponsiblePerson = IIF(@ResponsiblePerson = N'<#NV>', ResponsiblePerson, @ResponsiblePerson) ,ImageName = IIF(@ImageName = N'<#NV>', ImageName, @ImageName) ,Status = IIF(@Status = N'<#NV>', Status, @Status) ,Template = IIF(@Template = N'<#NV>', Template, @Template) ,TemplateVersion = IIF(@TemplateVersion = N'<#NV>', TemplateVersion, @TemplateVersion) ,GlobalAttribute1 = IIF(@GA1 = N'<#NV>', GlobalAttribute1, @GA1) ,GlobalAttribute2 = IIF(@GA2 = N'<#NV>', GlobalAttribute2, @GA2) ,GlobalAttribute3 = IIF(@GA3 = N'<#NV>', GlobalAttribute3, @GA3) ,GlobalAttribute4 = IIF(@GA4 = N'<#NV>', GlobalAttribute4, @GA4) ,GlobalAttribute5 = IIF(@GA5 = N'<#NV>', GlobalAttribute5, @GA5) ,GlobalAttribute6 = IIF(@GA6 = N'<#NV>', GlobalAttribute6, @GA6) ,GlobalAttribute7 = IIF(@GA7 = N'<#NV>', GlobalAttribute7, @GA7) ,GlobalAttribute8 = IIF(@GA8 = N'<#NV>', GlobalAttribute8, @GA8) ,GlobalAttribute9 = IIF(@GA9 = N'<#NV>', GlobalAttribute9, @GA9) ,GlobalAttribute10 = IIF(@GA10 = N'<#NV>', GlobalAttribute10, @GA10) ,GlobalAttribute11 = IIF(@GA11 = N'<#NV>', GlobalAttribute11, @GA11) ,GlobalAttribute12 = IIF(@GA12 = N'<#NV>', GlobalAttribute12, @GA12) ,GlobalAttribute13 = IIF(@GA13 = N'<#NV>', GlobalAttribute13, @GA13) ,GlobalAttribute14 = IIF(@GA14 = N'<#NV>', GlobalAttribute14, @GA14) ,GlobalAttribute15 = IIF(@GA15 = N'<#NV>', GlobalAttribute15, @GA15) ,GlobalAttribute16 = IIF(@GA16 = N'<#NV>', GlobalAttribute16, @GA16) ,GlobalAttribute17 = IIF(@GA17 = N'<#NV>', GlobalAttribute17, @GA17) ,GlobalAttribute18 = IIF(@GA18 = N'<#NV>', GlobalAttribute18, @GA18) ,GlobalAttribute19 = IIF(@GA19 = N'<#NV>', GlobalAttribute19, @GA19) ,GlobalAttribute20 = IIF(@GA20 = N'<#NV>', GlobalAttribute20, @GA20) ,GlobalAttribute21 = IIF(@GA21 = N'<#NV>', GlobalAttribute21, @GA21) ,GlobalAttribute22 = IIF(@GA22 = N'<#NV>', GlobalAttribute22, @GA22) ,GlobalAttribute23 = IIF(@GA23 = N'<#NV>', GlobalAttribute23, @GA23) ,GlobalAttribute24 = IIF(@GA24 = N'<#NV>', GlobalAttribute24, @GA24) ,GlobalAttribute25 = IIF(@GA25 = N'<#NV>', GlobalAttribute25, @GA25) FROM planning.tdProducts WHERE ProductKey = @ProductKey; SET @EffectedRows += @@ROWCOUNT; COMMIT TRANSACTION sx_pf_POST_ProductEmpty; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_ProductEmpty; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_ProductEmpty' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for one empty Product Not existing Product will be created, existing gets an Update If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty If product will have no TimeIDs, as it has no values If product will have no ValueSeries, when created new'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TimeType'; SET @value = N'TimeType'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@NameShort'; SET @value = N'NameShort'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@NameLong'; SET @value = N'NameLong'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentUser'; SET @value = N'CommentUser'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentDev'; SET @value = N'CommentDev'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ResponsiblePerson'; SET @value = N'ResponsiblePerson'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ImageName'; SET @value = N'ImageName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Status'; SET @value = N'Status'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Template'; SET @value = N'Template'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TemplateVersion'; SET @value = N'TemplateVersion'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA1'; SET @value = N'GA1'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA2'; SET @value = N'GA2'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA3'; SET @value = N'GA3'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA4'; SET @value = N'GA4'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA5'; SET @value = N'GA5'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA6'; SET @value = N'GA6'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA7'; SET @value = N'GA7'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA8'; SET @value = N'GA8'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA9'; SET @value = N'GA9'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA10'; SET @value = N'GA10'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA11'; SET @value = N'GA11'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA12'; SET @value = N'GA12'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA13'; SET @value = N'GA13'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA14'; SET @value = N'GA14'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA15'; SET @value = N'GA15'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA16'; SET @value = N'GA16'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA17'; SET @value = N'GA17'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA18'; SET @value = N'GA18'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA19'; SET @value = N'GA19'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA20'; SET @value = N'GA20'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA21'; SET @value = N'GA21'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA22'; SET @value = N'GA22'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA23'; SET @value = N'GA23'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA24'; SET @value = N'GA24'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GA25'; SET @value = N'GA25'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_ProductLine; GO /* POST Operation for ProductLine Not existing ProductLine will be created, existing gets an Update If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call 1. Empty FactoryID / ProductLineID => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. Username does not have write access to ProductLine => 401 Unauthorized 3. Username does not have write access to Factory on Create => 401 Unauthorized 5. Non exists Factory / ProductLine keys => 404 Not Found 6. ProductLine created => 201 Created 7. ProductLine updated => 200 OK DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@ProductLineID NVARCHAR(255) = 'ee' ,@FactoryID NVARCHAR(255) = 'ZT' ,@NameShort NVARCHAR(255) = '' ,@NameLong NVARCHAR(255) = '' ,@CommentUser NVARCHAR(255) = '' ,@CommentDev NVARCHAR(255) = '' ,@ResponsiblePerson NVARCHAR(255) = '' ,@ImageName NVARCHAR(255) = '' ,@DefaultTemplate NVARCHAR(255) = '' ,@GlobalAttributeSource1 NVARCHAR(255) = '' ,@GlobalAttributeAlias1 NVARCHAR(255) = '' ,@GlobalAttributeSource2 NVARCHAR(255) = '' ,@GlobalAttributeAlias2 NVARCHAR(255) = '' ,@GlobalAttributeSource3 NVARCHAR(255) = '' ,@GlobalAttributeAlias3 NVARCHAR(255) = '' ,@GlobalAttributeSource4 NVARCHAR(255) = '' ,@GlobalAttributeAlias4 NVARCHAR(255) = '' ,@GlobalAttributeSource5 NVARCHAR(255) = '' ,@GlobalAttributeAlias5 NVARCHAR(255) = '' ,@GlobalAttributeSource6 NVARCHAR(255) = '' ,@GlobalAttributeAlias6 NVARCHAR(255) = '' ,@GlobalAttributeSource7 NVARCHAR(255) = '' ,@GlobalAttributeAlias7 NVARCHAR(255) = '' ,@GlobalAttributeSource8 NVARCHAR(255) = '' ,@GlobalAttributeAlias8 NVARCHAR(255) = '' ,@GlobalAttributeSource9 NVARCHAR(255) = '' ,@GlobalAttributeAlias9 NVARCHAR(255) = '' ,@GlobalAttributeSource10 NVARCHAR(255) = '' ,@GlobalAttributeAlias10 NVARCHAR(255) = '' ,@GlobalAttributeSource11 NVARCHAR(255) = '' ,@GlobalAttributeAlias11 NVARCHAR(255) = '' ,@GlobalAttributeSource12 NVARCHAR(255) = '' ,@GlobalAttributeAlias12 NVARCHAR(255) = '' ,@GlobalAttributeSource13 NVARCHAR(255) = '' ,@GlobalAttributeAlias13 NVARCHAR(255) = '' ,@GlobalAttributeSource14 NVARCHAR(255) = '' ,@GlobalAttributeAlias14 NVARCHAR(255) = '' ,@GlobalAttributeSource15 NVARCHAR(255) = '' ,@GlobalAttributeAlias15 NVARCHAR(255) = '' ,@GlobalAttributeSource16 NVARCHAR(255) = '' ,@GlobalAttributeAlias16 NVARCHAR(255) = '' ,@GlobalAttributeSource17 NVARCHAR(255) = '' ,@GlobalAttributeAlias17 NVARCHAR(255) = '' ,@GlobalAttributeSource18 NVARCHAR(255) = '' ,@GlobalAttributeAlias18 NVARCHAR(255) = '' ,@GlobalAttributeSource19 NVARCHAR(255) = '' ,@GlobalAttributeAlias19 NVARCHAR(255) = '' ,@GlobalAttributeSource20 NVARCHAR(255) = '' ,@GlobalAttributeAlias20 NVARCHAR(255) = '' ,@GlobalAttributeSource21 NVARCHAR(255) = '' ,@GlobalAttributeAlias21 NVARCHAR(255) = '' ,@GlobalAttributeSource22 NVARCHAR(255) = '' ,@GlobalAttributeAlias22 NVARCHAR(255) = '' ,@GlobalAttributeSource23 NVARCHAR(255) = '' ,@GlobalAttributeAlias23 NVARCHAR(255) = '' ,@GlobalAttributeSource24 NVARCHAR(255) = '' ,@GlobalAttributeAlias24 NVARCHAR(255) = '' ,@GlobalAttributeSource25 NVARCHAR(255) = '' ,@GlobalAttributeAlias25 NVARCHAR(255) = '' ,@LayoutJSON NVARCHAR(MAX) = '' EXECUTE @RC = planning.spPOST_ProductLine @Username, @ProductLineID, @FactoryID, @NameShort, @NameLong, @CommentUser, @CommentDev, @ResponsiblePerson, @ImageName, @DefaultTemplate, @GlobalAttributeSource1, @GlobalAttributeAlias1, @GlobalAttributeSource2, @GlobalAttributeAlias2, @GlobalAttributeSource3, @GlobalAttributeAlias3, @GlobalAttributeSource4, @GlobalAttributeAlias4, @GlobalAttributeSource5, @GlobalAttributeAlias5, @GlobalAttributeSource6, @GlobalAttributeAlias6, @GlobalAttributeSource7, @GlobalAttributeAlias7, @GlobalAttributeSource8, @GlobalAttributeAlias8, @GlobalAttributeSource9, @GlobalAttributeAlias9, @GlobalAttributeSource10, @GlobalAttributeAlias10, @GlobalAttributeSource11, @GlobalAttributeAlias11, @GlobalAttributeSource12, @GlobalAttributeAlias12, @GlobalAttributeSource13, @GlobalAttributeAlias13, @GlobalAttributeSource14, @GlobalAttributeAlias14, @GlobalAttributeSource15, @GlobalAttributeAlias15, @GlobalAttributeSource16, @GlobalAttributeAlias16, @GlobalAttributeSource17, @GlobalAttributeAlias17, @GlobalAttributeSource18, @GlobalAttributeAlias18, @GlobalAttributeSource19, @GlobalAttributeAlias19, @GlobalAttributeSource20, @GlobalAttributeAlias20, @GlobalAttributeSource21, @GlobalAttributeAlias21, @GlobalAttributeSource22, @GlobalAttributeAlias22, @GlobalAttributeSource23, @GlobalAttributeAlias23, @GlobalAttributeSource24, @GlobalAttributeAlias24, @GlobalAttributeSource25, @GlobalAttributeAlias25, @LayoutJSON PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ProductLine',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ProductLine','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_ProductLine @Username NVARCHAR(255), @ProductLineID NVARCHAR(255), @FactoryID NVARCHAR(255), @NameShort NVARCHAR(255), @NameLong NVARCHAR(255), @CommentUser NVARCHAR(MAX), @CommentDev NVARCHAR(MAX), @ResponsiblePerson NVARCHAR(255), @ImageName NVARCHAR(255), @DefaultTemplate NVARCHAR(255), @GlobalAttributeSource1 NVARCHAR(255), @GlobalAttributeAlias1 NVARCHAR(255), @GlobalAttributeSource2 NVARCHAR(255), @GlobalAttributeAlias2 NVARCHAR(255), @GlobalAttributeSource3 NVARCHAR(255), @GlobalAttributeAlias3 NVARCHAR(255), @GlobalAttributeSource4 NVARCHAR(255), @GlobalAttributeAlias4 NVARCHAR(255), @GlobalAttributeSource5 NVARCHAR(255), @GlobalAttributeAlias5 NVARCHAR(255), @GlobalAttributeSource6 NVARCHAR(255), @GlobalAttributeAlias6 NVARCHAR(255), @GlobalAttributeSource7 NVARCHAR(255), @GlobalAttributeAlias7 NVARCHAR(255), @GlobalAttributeSource8 NVARCHAR(255), @GlobalAttributeAlias8 NVARCHAR(255), @GlobalAttributeSource9 NVARCHAR(255), @GlobalAttributeAlias9 NVARCHAR(255), @GlobalAttributeSource10 NVARCHAR(255), @GlobalAttributeAlias10 NVARCHAR(255), @GlobalAttributeSource11 NVARCHAR(255), @GlobalAttributeAlias11 NVARCHAR(255), @GlobalAttributeSource12 NVARCHAR(255), @GlobalAttributeAlias12 NVARCHAR(255), @GlobalAttributeSource13 NVARCHAR(255), @GlobalAttributeAlias13 NVARCHAR(255), @GlobalAttributeSource14 NVARCHAR(255), @GlobalAttributeAlias14 NVARCHAR(255), @GlobalAttributeSource15 NVARCHAR(255), @GlobalAttributeAlias15 NVARCHAR(255), @GlobalAttributeSource16 NVARCHAR(255), @GlobalAttributeAlias16 NVARCHAR(255), @GlobalAttributeSource17 NVARCHAR(255), @GlobalAttributeAlias17 NVARCHAR(255), @GlobalAttributeSource18 NVARCHAR(255), @GlobalAttributeAlias18 NVARCHAR(255), @GlobalAttributeSource19 NVARCHAR(255), @GlobalAttributeAlias19 NVARCHAR(255), @GlobalAttributeSource20 NVARCHAR(255), @GlobalAttributeAlias20 NVARCHAR(255), @GlobalAttributeSource21 NVARCHAR(255), @GlobalAttributeAlias21 NVARCHAR(255), @GlobalAttributeSource22 NVARCHAR(255), @GlobalAttributeAlias22 NVARCHAR(255), @GlobalAttributeSource23 NVARCHAR(255), @GlobalAttributeAlias23 NVARCHAR(255), @GlobalAttributeSource24 NVARCHAR(255), @GlobalAttributeAlias24 NVARCHAR(255), @GlobalAttributeSource25 NVARCHAR(255), @GlobalAttributeAlias25 NVARCHAR(255), @LayoutJSON NVARCHAR(MAX) = NULL -- Kompatibilität für alte Versionen AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255)= N''; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@NameShort, N'NULL') + N''',''' + ISNULL(@NameLong, N'NULL') + N''',''' + ISNULL(@CommentUser, N'NULL') + N''',''' + ISNULL(@CommentDev, N'NULL') + N''',''' + ISNULL(@ResponsiblePerson, N'NULL') + N''',''' + ISNULL(@ImageName, N'NULL') + N''',''' + ISNULL(@DefaultTemplate, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource1, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias1, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource2, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias2, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource3, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias3, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource4, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias4, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource5, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias5, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource6, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias6, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource7, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias7, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource8, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias8, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource9, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias9, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource10, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias10, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource11, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias11, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource12, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias12, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource13, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias13, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource14, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias14, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource15, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias15, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource16, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias16, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource17, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias17, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource18, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias18, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource19, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias19, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource20, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias20, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource21, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias21, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource22, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias22, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource23, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias23, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource24, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias24, N'NULL') + N''',''' + ISNULL(@GlobalAttributeSource25, N'NULL') + N''',''' + ISNULL(@GlobalAttributeAlias25, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @NameShort IS NULL SET @NameShort = N''; IF @NameLong IS NULL SET @NameLong = N''; IF @CommentUser IS NULL SET @CommentUser = N''; IF @CommentDev IS NULL SET @CommentDev = N''; IF @ResponsiblePerson IS NULL SET @ResponsiblePerson = N''; IF @ImageName IS NULL SET @ImageName = N''; IF @DefaultTemplate IS NULL SET @DefaultTemplate = N''; IF @GlobalAttributeSource1 IS NULL SET @GlobalAttributeSource1 = N''; IF @GlobalAttributeAlias1 IS NULL SET @GlobalAttributeAlias1 = N''; IF @GlobalAttributeSource2 IS NULL SET @GlobalAttributeSource2 = N''; IF @GlobalAttributeAlias2 IS NULL SET @GlobalAttributeAlias2 = N''; IF @GlobalAttributeSource3 IS NULL SET @GlobalAttributeSource3 = N''; IF @GlobalAttributeAlias3 IS NULL SET @GlobalAttributeAlias3 = N''; IF @GlobalAttributeSource4 IS NULL SET @GlobalAttributeSource4 = N''; IF @GlobalAttributeAlias4 IS NULL SET @GlobalAttributeAlias4 = N''; IF @GlobalAttributeSource5 IS NULL SET @GlobalAttributeSource5 = N''; IF @GlobalAttributeAlias5 IS NULL SET @GlobalAttributeAlias5 = N''; IF @GlobalAttributeSource6 IS NULL SET @GlobalAttributeSource6 = N''; IF @GlobalAttributeAlias6 IS NULL SET @GlobalAttributeAlias6 = N''; IF @GlobalAttributeSource7 IS NULL SET @GlobalAttributeSource7 = N''; IF @GlobalAttributeAlias7 IS NULL SET @GlobalAttributeAlias7 = N''; IF @GlobalAttributeSource8 IS NULL SET @GlobalAttributeSource8 = N''; IF @GlobalAttributeAlias8 IS NULL SET @GlobalAttributeAlias8 = N''; IF @GlobalAttributeSource9 IS NULL SET @GlobalAttributeSource9 = N''; IF @GlobalAttributeAlias9 IS NULL SET @GlobalAttributeAlias9 = N''; IF @GlobalAttributeSource10 IS NULL SET @GlobalAttributeSource10 = N''; IF @GlobalAttributeAlias10 IS NULL SET @GlobalAttributeAlias10 = N''; IF @GlobalAttributeSource11 IS NULL SET @GlobalAttributeSource11 = N''; IF @GlobalAttributeAlias11 IS NULL SET @GlobalAttributeAlias11 = N''; IF @GlobalAttributeSource12 IS NULL SET @GlobalAttributeSource12 = N''; IF @GlobalAttributeAlias12 IS NULL SET @GlobalAttributeAlias12 = N''; IF @GlobalAttributeSource13 IS NULL SET @GlobalAttributeSource13 = N''; IF @GlobalAttributeAlias13 IS NULL SET @GlobalAttributeAlias13 = N''; IF @GlobalAttributeSource14 IS NULL SET @GlobalAttributeSource14 = N''; IF @GlobalAttributeAlias14 IS NULL SET @GlobalAttributeAlias14 = N''; IF @GlobalAttributeSource15 IS NULL SET @GlobalAttributeSource15 = N''; IF @GlobalAttributeAlias15 IS NULL SET @GlobalAttributeAlias15 = N''; IF @GlobalAttributeSource16 IS NULL SET @GlobalAttributeSource16 = N''; IF @GlobalAttributeAlias16 IS NULL SET @GlobalAttributeAlias16 = N''; IF @GlobalAttributeSource17 IS NULL SET @GlobalAttributeSource17 = N''; IF @GlobalAttributeAlias17 IS NULL SET @GlobalAttributeAlias17 = N''; IF @GlobalAttributeSource18 IS NULL SET @GlobalAttributeSource18 = N''; IF @GlobalAttributeAlias18 IS NULL SET @GlobalAttributeAlias18 = N''; IF @GlobalAttributeSource19 IS NULL SET @GlobalAttributeSource19 = N''; IF @GlobalAttributeAlias19 IS NULL SET @GlobalAttributeAlias19 = N''; IF @GlobalAttributeSource20 IS NULL SET @GlobalAttributeSource20 = N''; IF @GlobalAttributeAlias20 IS NULL SET @GlobalAttributeAlias20 = N''; IF @GlobalAttributeSource21 IS NULL SET @GlobalAttributeSource21 = N''; IF @GlobalAttributeAlias21 IS NULL SET @GlobalAttributeAlias21 = N''; IF @GlobalAttributeSource22 IS NULL SET @GlobalAttributeSource22 = N''; IF @GlobalAttributeAlias22 IS NULL SET @GlobalAttributeAlias22 = N''; IF @GlobalAttributeSource23 IS NULL SET @GlobalAttributeSource23 = N''; IF @GlobalAttributeAlias23 IS NULL SET @GlobalAttributeAlias23 = N''; IF @GlobalAttributeSource24 IS NULL SET @GlobalAttributeSource24 = N''; IF @GlobalAttributeAlias24 IS NULL SET @GlobalAttributeAlias24 = N''; IF @GlobalAttributeSource25 IS NULL SET @GlobalAttributeSource25 = N''; IF @GlobalAttributeAlias25 IS NULL SET @GlobalAttributeAlias25 = N''; IF @LayoutJSON IS NULL SET @LayoutJSON = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_ProductLine; -- STEP 0.2 - Protect input parameters SET @ProductLineID = system.fProtectID (@ProductLineID); SET @FactoryID = system.fProtectID (@FactoryID); SET @NameShort = system.fProtectString (@NameShort); SET @NameLong = system.fProtectString (@NameLong); SET @CommentUser = system.fProtectString (@CommentUser); SET @CommentDev = system.fProtectString (@CommentDev); SET @ResponsiblePerson = system.fProtectString (@ResponsiblePerson); SET @ImageName = system.fProtectString (@ImageName); SET @DefaultTemplate = system.fProtectString (@DefaultTemplate); SET @GlobalAttributeAlias1 = system.fProtectString (@GlobalAttributeAlias1); SET @GlobalAttributeAlias2 = system.fProtectString (@GlobalAttributeAlias2); SET @GlobalAttributeAlias3 = system.fProtectString (@GlobalAttributeAlias3); SET @GlobalAttributeAlias4 = system.fProtectString (@GlobalAttributeAlias4); SET @GlobalAttributeAlias5 = system.fProtectString (@GlobalAttributeAlias5); SET @GlobalAttributeAlias6 = system.fProtectString (@GlobalAttributeAlias6); SET @GlobalAttributeAlias7 = system.fProtectString (@GlobalAttributeAlias7); SET @GlobalAttributeAlias8 = system.fProtectString (@GlobalAttributeAlias8); SET @GlobalAttributeAlias9 = system.fProtectString (@GlobalAttributeAlias9); SET @GlobalAttributeAlias10 = system.fProtectString (@GlobalAttributeAlias10); SET @GlobalAttributeAlias11 = system.fProtectString (@GlobalAttributeAlias11); SET @GlobalAttributeAlias12 = system.fProtectString (@GlobalAttributeAlias12); SET @GlobalAttributeAlias13 = system.fProtectString (@GlobalAttributeAlias13); SET @GlobalAttributeAlias14 = system.fProtectString (@GlobalAttributeAlias14); SET @GlobalAttributeAlias15 = system.fProtectString (@GlobalAttributeAlias15); SET @GlobalAttributeAlias16 = system.fProtectString (@GlobalAttributeAlias16); SET @GlobalAttributeAlias17 = system.fProtectString (@GlobalAttributeAlias17); SET @GlobalAttributeAlias18 = system.fProtectString (@GlobalAttributeAlias18); SET @GlobalAttributeAlias19 = system.fProtectString (@GlobalAttributeAlias19); SET @GlobalAttributeAlias20 = system.fProtectString (@GlobalAttributeAlias20); SET @GlobalAttributeAlias21 = system.fProtectString (@GlobalAttributeAlias21); SET @GlobalAttributeAlias22 = system.fProtectString (@GlobalAttributeAlias22); SET @GlobalAttributeAlias23 = system.fProtectString (@GlobalAttributeAlias23); SET @GlobalAttributeAlias24 = system.fProtectString (@GlobalAttributeAlias24); SET @GlobalAttributeAlias25 = system.fProtectString (@GlobalAttributeAlias25); SET @LayoutJSON = system.fProtectString (@LayoutJSON); IF @GlobalAttributeSource1 !='<#NV>' SET @GlobalAttributeSource1 = system.fProtectID (@GlobalAttributeSource1) IF @GlobalAttributeSource2 !='<#NV>' SET @GlobalAttributeSource2 = system.fProtectID (@GlobalAttributeSource2) IF @GlobalAttributeSource3 !='<#NV>' SET @GlobalAttributeSource3 = system.fProtectID (@GlobalAttributeSource3) IF @GlobalAttributeSource4 !='<#NV>' SET @GlobalAttributeSource4 = system.fProtectID (@GlobalAttributeSource4) IF @GlobalAttributeSource5 !='<#NV>' SET @GlobalAttributeSource5 = system.fProtectID (@GlobalAttributeSource5) IF @GlobalAttributeSource6 !='<#NV>' SET @GlobalAttributeSource6 = system.fProtectID (@GlobalAttributeSource6) IF @GlobalAttributeSource7 !='<#NV>' SET @GlobalAttributeSource7 = system.fProtectID (@GlobalAttributeSource7) IF @GlobalAttributeSource8 !='<#NV>' SET @GlobalAttributeSource8 = system.fProtectID (@GlobalAttributeSource8) IF @GlobalAttributeSource9 !='<#NV>' SET @GlobalAttributeSource9 = system.fProtectID (@GlobalAttributeSource9) IF @GlobalAttributeSource10 !='<#NV>' SET @GlobalAttributeSource10 = system.fProtectID (@GlobalAttributeSource10) IF @GlobalAttributeSource11 !='<#NV>' SET @GlobalAttributeSource11 = system.fProtectID (@GlobalAttributeSource11) IF @GlobalAttributeSource12 !='<#NV>' SET @GlobalAttributeSource12 = system.fProtectID (@GlobalAttributeSource12) IF @GlobalAttributeSource13 !='<#NV>' SET @GlobalAttributeSource13 = system.fProtectID (@GlobalAttributeSource13) IF @GlobalAttributeSource14 !='<#NV>' SET @GlobalAttributeSource14 = system.fProtectID (@GlobalAttributeSource14) IF @GlobalAttributeSource15 !='<#NV>' SET @GlobalAttributeSource15 = system.fProtectID (@GlobalAttributeSource15) IF @GlobalAttributeSource16 !='<#NV>' SET @GlobalAttributeSource16 = system.fProtectID (@GlobalAttributeSource16) IF @GlobalAttributeSource17 !='<#NV>' SET @GlobalAttributeSource17 = system.fProtectID (@GlobalAttributeSource17) IF @GlobalAttributeSource18 !='<#NV>' SET @GlobalAttributeSource18 = system.fProtectID (@GlobalAttributeSource18) IF @GlobalAttributeSource19 !='<#NV>' SET @GlobalAttributeSource19 = system.fProtectID (@GlobalAttributeSource19) IF @GlobalAttributeSource20 !='<#NV>' SET @GlobalAttributeSource20 = system.fProtectID (@GlobalAttributeSource20) IF @GlobalAttributeSource21 !='<#NV>' SET @GlobalAttributeSource21 = system.fProtectID (@GlobalAttributeSource21) IF @GlobalAttributeSource22 !='<#NV>' SET @GlobalAttributeSource22 = system.fProtectID (@GlobalAttributeSource22) IF @GlobalAttributeSource23 !='<#NV>' SET @GlobalAttributeSource23 = system.fProtectID (@GlobalAttributeSource23) IF @GlobalAttributeSource24 !='<#NV>' SET @GlobalAttributeSource24 = system.fProtectID (@GlobalAttributeSource24) IF @GlobalAttributeSource25 !='<#NV>' SET @GlobalAttributeSource25 = system.fProtectID (@GlobalAttributeSource25) IF @FactoryID = N'' OR @ProductLineID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; IF @FactoryKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights IF @ProductLineKey = 0 EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername, @FactoryID; ELSE EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 3.1 - If Product Line does not yet exist, it will be created in minimal configuration IF @ProductLineKey = 0 BEGIN DECLARE @Keys AS TABLE ([Key] INT NOT NULL); INSERT INTO planning.tdProductLines (FactoryKey, ProductLineID, FactoryID, NameShort, NameLong, CommentUser, CommentDev, ResponsiblePerson, ImageName, DefaultTemplate , GlobalAttributeSource1, GlobalAttributeAlias1, GlobalAttributeSource2, GlobalAttributeAlias2, GlobalAttributeSource3, GlobalAttributeAlias3, GlobalAttributeSource4, GlobalAttributeAlias4, GlobalAttributeSource5, GlobalAttributeAlias5 , GlobalAttributeSource6, GlobalAttributeAlias6, GlobalAttributeSource7, GlobalAttributeAlias7, GlobalAttributeSource8, GlobalAttributeAlias8, GlobalAttributeSource9, GlobalAttributeAlias9, GlobalAttributeSource10, GlobalAttributeAlias10 , GlobalAttributeSource11, GlobalAttributeAlias11, GlobalAttributeSource12, GlobalAttributeAlias12, GlobalAttributeSource13, GlobalAttributeAlias13, GlobalAttributeSource14, GlobalAttributeAlias14, GlobalAttributeSource15, GlobalAttributeAlias15 , GlobalAttributeSource16, GlobalAttributeAlias16, GlobalAttributeSource17, GlobalAttributeAlias17, GlobalAttributeSource18, GlobalAttributeAlias18, GlobalAttributeSource19, GlobalAttributeAlias19, GlobalAttributeSource20, GlobalAttributeAlias20 , GlobalAttributeSource21, GlobalAttributeAlias21, GlobalAttributeSource22, GlobalAttributeAlias22, GlobalAttributeSource23, GlobalAttributeAlias23, GlobalAttributeSource24, GlobalAttributeAlias24, GlobalAttributeSource25, GlobalAttributeAlias25 , LayoutJSON ) OUTPUT INSERTED.ProductLineKey INTO @Keys([Key]) VALUES (@FactoryKey, @ProductLineID, @FactoryID, @NameShort, '', '', '', '', '', '' , '', '', '', '', '', '', '', '', '', '' , '', '', '', '', '', '', '', '', '', '' , '', '', '', '', '', '', '', '', '', '' , '', '', '', '', '', '', '', '', '', '' , '', '', '', '', '', '', '', '', '', '' , '' ); SELECT TOP (1) @ProductLineKey = [Key] FROM @Keys ORDER BY [Key]; SET @EffectedRows = 1; SET @Resultcode = 201; END; -- STEP 3.2 - Update values UPDATE planning.tdProductLines SET NameShort = IIF(@NameShort = N'<#NV>', NameShort, @NameShort) , NameLong = IIF(@NameLong = N'<#NV>', NameLong, @NameLong) , CommentUser = IIF(@CommentUser = N'<#NV>', CommentUser, @CommentUser) , CommentDev = IIF(@CommentDev = N'<#NV>', CommentDev, @CommentDev) , ResponsiblePerson = IIF(@ResponsiblePerson = N'<#NV>', ResponsiblePerson, @ResponsiblePerson) , ImageName = IIF(@ImageName = N'<#NV>', ImageName, @ImageName) , DefaultTemplate = IIF(@DefaultTemplate = N'<#NV>', DefaultTemplate, @DefaultTemplate) , GlobalAttributeSource1 = IIF(@GlobalAttributeSource1 = N'<#NV>', GlobalAttributeSource1, @GlobalAttributeSource1) , GlobalAttributeAlias1 = IIF(@GlobalAttributeAlias1 = N'<#NV>', GlobalAttributeAlias1, @GlobalAttributeAlias1) , GlobalAttributeSource2 = IIF(@GlobalAttributeSource2 = N'<#NV>', GlobalAttributeSource2, @GlobalAttributeSource2) , GlobalAttributeAlias2 = IIF(@GlobalAttributeAlias2 = N'<#NV>', GlobalAttributeAlias2, @GlobalAttributeAlias2) , GlobalAttributeSource3 = IIF(@GlobalAttributeSource3 = N'<#NV>', GlobalAttributeSource3, @GlobalAttributeSource3) , GlobalAttributeAlias3 = IIF(@GlobalAttributeAlias3 = N'<#NV>', GlobalAttributeAlias3, @GlobalAttributeAlias3) , GlobalAttributeSource4 = IIF(@GlobalAttributeSource4 = N'<#NV>', GlobalAttributeSource4, @GlobalAttributeSource4) , GlobalAttributeAlias4 = IIF(@GlobalAttributeAlias4 = N'<#NV>', GlobalAttributeAlias4, @GlobalAttributeAlias4) , GlobalAttributeSource5 = IIF(@GlobalAttributeSource5 = N'<#NV>', GlobalAttributeSource5, @GlobalAttributeSource5) , GlobalAttributeAlias5 = IIF(@GlobalAttributeAlias5 = N'<#NV>', GlobalAttributeAlias5, @GlobalAttributeAlias5) , GlobalAttributeSource6 = IIF(@GlobalAttributeSource6 = N'<#NV>', GlobalAttributeSource6, @GlobalAttributeSource6) , GlobalAttributeAlias6 = IIF(@GlobalAttributeAlias6 = N'<#NV>', GlobalAttributeAlias6, @GlobalAttributeAlias6) , GlobalAttributeSource7 = IIF(@GlobalAttributeSource7 = N'<#NV>', GlobalAttributeSource7, @GlobalAttributeSource7) , GlobalAttributeAlias7 = IIF(@GlobalAttributeAlias7 = N'<#NV>', GlobalAttributeAlias7, @GlobalAttributeAlias7) , GlobalAttributeSource8 = IIF(@GlobalAttributeSource8 = N'<#NV>', GlobalAttributeSource8, @GlobalAttributeSource8) , GlobalAttributeAlias8 = IIF(@GlobalAttributeAlias8 = N'<#NV>', GlobalAttributeAlias8, @GlobalAttributeAlias8) , GlobalAttributeSource9 = IIF(@GlobalAttributeSource9 = N'<#NV>', GlobalAttributeSource9, @GlobalAttributeSource9) , GlobalAttributeAlias9 = IIF(@GlobalAttributeAlias9 = N'<#NV>', GlobalAttributeAlias9, @GlobalAttributeAlias9) , GlobalAttributeSource10 = IIF(@GlobalAttributeSource10 = N'<#NV>', GlobalAttributeSource10, @GlobalAttributeSource10) , GlobalAttributeAlias10 = IIF(@GlobalAttributeAlias10 = N'<#NV>', GlobalAttributeAlias10, @GlobalAttributeAlias10) , GlobalAttributeSource11 = IIF(@GlobalAttributeSource11 = N'<#NV>', GlobalAttributeSource11, @GlobalAttributeSource11) , GlobalAttributeAlias11 = IIF(@GlobalAttributeAlias11 = N'<#NV>', GlobalAttributeAlias11, @GlobalAttributeAlias11) , GlobalAttributeSource12 = IIF(@GlobalAttributeSource12 = N'<#NV>', GlobalAttributeSource12, @GlobalAttributeSource12) , GlobalAttributeAlias12 = IIF(@GlobalAttributeAlias12 = N'<#NV>', GlobalAttributeAlias12, @GlobalAttributeAlias12) , GlobalAttributeSource13 = IIF(@GlobalAttributeSource13 = N'<#NV>', GlobalAttributeSource13, @GlobalAttributeSource13) , GlobalAttributeAlias13 = IIF(@GlobalAttributeAlias13 = N'<#NV>', GlobalAttributeAlias13, @GlobalAttributeAlias13) , GlobalAttributeSource14 = IIF(@GlobalAttributeSource14 = N'<#NV>', GlobalAttributeSource14, @GlobalAttributeSource14) , GlobalAttributeAlias14 = IIF(@GlobalAttributeAlias14 = N'<#NV>', GlobalAttributeAlias14, @GlobalAttributeAlias14) , GlobalAttributeSource15 = IIF(@GlobalAttributeSource15 = N'<#NV>', GlobalAttributeSource15, @GlobalAttributeSource15) , GlobalAttributeAlias15 = IIF(@GlobalAttributeAlias15 = N'<#NV>', GlobalAttributeAlias15, @GlobalAttributeAlias15) , GlobalAttributeSource16 = IIF(@GlobalAttributeSource16 = N'<#NV>', GlobalAttributeSource16, @GlobalAttributeSource16) , GlobalAttributeAlias16 = IIF(@GlobalAttributeAlias16 = N'<#NV>', GlobalAttributeAlias16, @GlobalAttributeAlias16) , GlobalAttributeSource17 = IIF(@GlobalAttributeSource17 = N'<#NV>', GlobalAttributeSource17, @GlobalAttributeSource17) , GlobalAttributeAlias17 = IIF(@GlobalAttributeAlias17 = N'<#NV>', GlobalAttributeAlias17, @GlobalAttributeAlias17) , GlobalAttributeSource18 = IIF(@GlobalAttributeSource18 = N'<#NV>', GlobalAttributeSource18, @GlobalAttributeSource18) , GlobalAttributeAlias18 = IIF(@GlobalAttributeAlias18 = N'<#NV>', GlobalAttributeAlias18, @GlobalAttributeAlias18) , GlobalAttributeSource19 = IIF(@GlobalAttributeSource19 = N'<#NV>', GlobalAttributeSource19, @GlobalAttributeSource19) , GlobalAttributeAlias19 = IIF(@GlobalAttributeAlias19 = N'<#NV>', GlobalAttributeAlias19, @GlobalAttributeAlias19) , GlobalAttributeSource20 = IIF(@GlobalAttributeSource20 = N'<#NV>', GlobalAttributeSource20, @GlobalAttributeSource20) , GlobalAttributeAlias20 = IIF(@GlobalAttributeAlias20 = N'<#NV>', GlobalAttributeAlias20, @GlobalAttributeAlias20) , GlobalAttributeSource21 = IIF(@GlobalAttributeSource21 = N'<#NV>', GlobalAttributeSource21, @GlobalAttributeSource21) , GlobalAttributeAlias21 = IIF(@GlobalAttributeAlias21 = N'<#NV>', GlobalAttributeAlias21, @GlobalAttributeAlias21) , GlobalAttributeSource22 = IIF(@GlobalAttributeSource22 = N'<#NV>', GlobalAttributeSource22, @GlobalAttributeSource22) , GlobalAttributeAlias22 = IIF(@GlobalAttributeAlias22 = N'<#NV>', GlobalAttributeAlias22, @GlobalAttributeAlias22) , GlobalAttributeSource23 = IIF(@GlobalAttributeSource23 = N'<#NV>', GlobalAttributeSource23, @GlobalAttributeSource23) , GlobalAttributeAlias23 = IIF(@GlobalAttributeAlias23 = N'<#NV>', GlobalAttributeAlias23, @GlobalAttributeAlias23) , GlobalAttributeSource24 = IIF(@GlobalAttributeSource24 = N'<#NV>', GlobalAttributeSource24, @GlobalAttributeSource24) , GlobalAttributeAlias24 = IIF(@GlobalAttributeAlias24 = N'<#NV>', GlobalAttributeAlias24, @GlobalAttributeAlias24) , GlobalAttributeSource25 = IIF(@GlobalAttributeSource25 = N'<#NV>', GlobalAttributeSource25, @GlobalAttributeSource25) , GlobalAttributeAlias25 = IIF(@GlobalAttributeAlias25 = N'<#NV>', GlobalAttributeAlias25, @GlobalAttributeAlias25) , LayoutJSON = IIF(@LayoutJSON = N'<#NV>', LayoutJSON, @LayoutJSON) FROM planning.tdProductLines WHERE ProductLineKey = @ProductLineKey; SET @EffectedRows += @@ROWCOUNT; -- STEP 3.3 - Rights materialize (only possible for users with Factory WriteRight or Cluster Admin) EXEC system.spMATERIALIZE_trUserRights; COMMIT TRANSACTION sx_pf_POST_ProductLine; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_ProductLine; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_ProductLine' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for ProductLine Not existing ProductLine will be created, existing gets an Update If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@NameShort'; SET @value = N'NameShort'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@NameLong'; SET @value = N'NameLong'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentUser'; SET @value = N'CommentUser'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentDev'; SET @value = N'CommentDev'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ResponsiblePerson'; SET @value = N'ResponsiblePerson'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ImageName'; SET @value = N'ImageName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@DefaultTemplate'; SET @value = N'DefaultTemplate'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource1'; SET @value = N'GlobalAttributeSource1'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias1'; SET @value = N'GlobalAttributeAlias1'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource2'; SET @value = N'GlobalAttributeSource2'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias2'; SET @value = N'GlobalAttributeAlias2'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource3'; SET @value = N'GlobalAttributeSource3'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias3'; SET @value = N'GlobalAttributeAlias3'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource4'; SET @value = N'GlobalAttributeSource4'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias4'; SET @value = N'GlobalAttributeAlias4'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource5'; SET @value = N'GlobalAttributeSource5'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias5'; SET @value = N'GlobalAttributeAlias5'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource6'; SET @value = N'GlobalAttributeSource6'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias6'; SET @value = N'GlobalAttributeAlias6'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource7'; SET @value = N'GlobalAttributeSource7'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias7'; SET @value = N'GlobalAttributeAlias7'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource8'; SET @value = N'GlobalAttributeSource8'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias8'; SET @value = N'GlobalAttributeAlias8'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource9'; SET @value = N'GlobalAttributeSource9'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias9'; SET @value = N'GlobalAttributeAlias9'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource10'; SET @value = N'GlobalAttributeSource10'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias10'; SET @value = N'GlobalAttributeAlias10'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource11'; SET @value = N'GlobalAttributeSource11'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias11'; SET @value = N'GlobalAttributeAlias11'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource12'; SET @value = N'GlobalAttributeSource12'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias12'; SET @value = N'GlobalAttributeAlias12'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource13'; SET @value = N'GlobalAttributeSource13'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias13'; SET @value = N'GlobalAttributeAlias13'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource14'; SET @value = N'GlobalAttributeSource14'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias14'; SET @value = N'GlobalAttributeAlias14'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource15'; SET @value = N'GlobalAttributeSource15'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias15'; SET @value = N'GlobalAttributeAlias15'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource16'; SET @value = N'GlobalAttributeSource16'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias16'; SET @value = N'GlobalAttributeAlias16'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource17'; SET @value = N'GlobalAttributeSource17'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias17'; SET @value = N'GlobalAttributeAlias17'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource18'; SET @value = N'GlobalAttributeSource18'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias18'; SET @value = N'GlobalAttributeAlias18'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource19'; SET @value = N'GlobalAttributeSource19'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias19'; SET @value = N'GlobalAttributeAlias19'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource20'; SET @value = N'GlobalAttributeSource20'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias20'; SET @value = N'GlobalAttributeAlias20'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource21'; SET @value = N'GlobalAttributeSource21'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias21'; SET @value = N'GlobalAttributeAlias21'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource22'; SET @value = N'GlobalAttributeSource22'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias22'; SET @value = N'GlobalAttributeAlias22'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource23'; SET @value = N'GlobalAttributeSource23'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias23'; SET @value = N'GlobalAttributeAlias23'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource24'; SET @value = N'GlobalAttributeSource24'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias24'; SET @value = N'GlobalAttributeAlias24'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeSource25'; SET @value = N'GlobalAttributeSource25'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@GlobalAttributeAlias25'; SET @value = N'GlobalAttributeAlias25'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_ProductLineProperty; GO /* POST Operation for ProductLineProperty Not existing ProductLine will be created, existing gets an Update If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call 1. PropertyID is empty => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. Username does not have write access to ProductLine => 401 Unauthorized 5. Property created => 201 Created 6. Property updated => 200 OK DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@ProductLineID NVARCHAR(255) = 'U' ,@FactoryID NVARCHAR(255) = 'ZT' ,@PropertyID NVARCHAR(255) = 'rreo' ,@PropertyName NVARCHAR(255) = '' ,@CommentUser NVARCHAR(MAX) = '' ,@CommentDev NVARCHAR(MAX) = '' ,@Unit NVARCHAR(255) = '' ,@ValueText NVARCHAR(MAX) = '' ,@ValueInt NVARCHAR(255) = '678' ,@Scale NVARCHAR(255) = '<#NV>' ,@IsROSystemProperty NVARCHAR(255) = '1' ,@FormatID NVARCHAR(255) = '<#NV>' EXECUTE @RC = planning.spPOST_ProductLineProperty @Username, @ProductLineID, @FactoryID, @PropertyID, @PropertyName, @CommentUser, @CommentDev, @Unit, @ValueText, @ValueInt, @Scale, @IsROSystemProperty, @FormatID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ProductLineProperty',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ProductLineProperty','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_ProductLineProperty @Username NVARCHAR(255), @ProductLineID NVARCHAR(255), @FactoryID NVARCHAR(255), @PropertyID NVARCHAR(255), @PropertyName NVARCHAR(255), @CommentUser NVARCHAR(MAX), @CommentDev NVARCHAR(MAX), @Unit NVARCHAR(255), @ValueText NVARCHAR(MAX), @ValueInt NVARCHAR(255), @Scale NVARCHAR(255), @IsROSystemProperty NVARCHAR(255), @FormatID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @PropertyKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@PropertyID, N'NULL') + N''',''' + ISNULL(@PropertyName, N'NULL') + N''',''' + ISNULL(@CommentUser, N'NULL') + N''',''' + ISNULL(@CommentDev, N'NULL') + N''',''' + ISNULL(@Unit, N'NULL') + N''',''' + ISNULL(@ValueText, N'NULL') + N''',''' + ISNULL(@ValueInt, N'NULL') + N''',''' + ISNULL(@Scale, N'NULL') + N''',''' + ISNULL(@IsROSystemProperty, N'NULL') + N''',''' + ISNULL(@FormatID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; -- SET during Execution DECLARE @ResultCode INT = 501; -- SET during Execution DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- SET during Execution -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @PropertyID IS NULL SET @PropertyID = N''; IF @PropertyName IS NULL SET @PropertyName = N''; IF @CommentUser IS NULL SET @CommentUser = N''; IF @CommentDev IS NULL SET @CommentDev = N''; IF @Unit IS NULL SET @Unit = N''; IF @ValueText IS NULL SET @ValueText = N''; IF @ValueInt IS NULL SET @ValueInt = N''; IF @Scale IS NULL SET @Scale = N''; IF @FormatID IS NULL SET @FormatID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_ProductLineProperty; -- STEP 0.2 - Protect input parameters SET @ProductLineID = system.fProtectID (@ProductLineID); SET @FactoryID = system.fProtectID (@FactoryID); SET @PropertyID = system.fProtectID (@PropertyID); SET @PropertyName = system.fProtectString (@PropertyName); SET @CommentUser = system.fProtectString (@CommentUser); SET @CommentDev = system.fProtectString (@CommentDev); SET @Unit = system.fProtectString (@Unit); SET @ValueText = system.fProtectString (@ValueText); SET @ValueInt = system.fProtectInt (@ValueInt); SET @Scale = system.fProtectInt (@Scale); SET @FormatID = system.fProtectString (@FormatID); --as it can be # IF @FactoryID = N'' OR @ProductLineID = N'' OR @PropertyID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @PropertyKey = PropertyKey FROM planning.tgProductLines WHERE ProductLineKey = @ProductLineKey AND PropertyID = @PropertyID; IF @FactoryKey = 0 OR @ProductLineKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 1.3 - Don`t allow changing of system RO Properties - Disabled from version 4.0.63 on to enable export/import --SELECT @PropertyOwner = IsROSystemProperty FROM planning.tgProductLines WHERE ProductLineKey = @ProductLineKey AND PropertyID = @PropertyID; --IF @PropertyOwner = 1 --BEGIN -- SET @ResultCode = 403; -- RAISERROR('Don`t allow changing of system RO Properties', 16, 10); --END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 3.1 - If Key does not yet exist, it will be created in minimal configuration IF @PropertyKey = 0 BEGIN DECLARE @Keys AS TABLE ([Key] INT NOT NULL); INSERT INTO planning.tgProductLines (ProductLineKey, FactoryKey, ProductLineID, FactoryID, PropertyID, PropertyName, CommentUser, CommentDev, Unit, ValueText, ValueInt, Scale, IsROSystemProperty, FormatID) OUTPUT INSERTED.PropertyKey INTO @Keys([Key]) VALUES (@ProductLineKey, @FactoryKey, @ProductLineID, @FactoryID, @PropertyID, '', '', '', '', '', 0, 1, 0, ''); SELECT TOP (1) @PropertyKey = [Key] FROM @Keys ORDER BY [Key]; SET @EffectedRows = 1; SET @Resultcode = 201; END; -- STEP 3.2 - The property receives the transmitted attributes undelivered be maintained UPDATE planning.tgProductLines SET PropertyName = IIF(@PropertyName = N'<#NV>', PropertyName, @PropertyName) , CommentUser = IIF(@CommentUser = N'<#NV>', CommentUser, @CommentUser) , CommentDev = IIF(@CommentDev = N'<#NV>', CommentDev, @CommentDev) , Unit = IIF(@Unit = N'<#NV>', Unit, @Unit) , ValueText = IIF(@ValueText = N'<#NV>', ValueText, @ValueText) , ValueInt = IIF(@ValueInt = N'<#NV>', ValueInt, @ValueInt) , Scale = IIF(@Scale = N'<#NV>', Scale, @Scale) , IsROSystemProperty = IIF(@IsROSystemProperty = N'<#NV>', IsROSystemProperty, @IsROSystemProperty) , FormatID = IIF(@FormatID = N'<#NV>', FormatID, @FormatID) FROM planning.tgProductLines WHERE PropertyKey = @PropertyKey; SET @EffectedRows += @@ROWCOUNT; COMMIT TRANSACTION sx_pf_POST_ProductLineProperty; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_ProductLineProperty; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_ProductLineProperty' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for ProductLineProperty Not existing ProductLine will be created, existing gets an Update If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PropertyID'; SET @value = N'PropertyID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PropertyName'; SET @value = N'PropertyName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentUser'; SET @value = N'CommentUser'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentDev'; SET @value = N'CommentDev'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Unit'; SET @value = N'Unit'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueText'; SET @value = N'ValueText'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueInt'; SET @value = N'ValueInt'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Scale'; SET @value = N'Scale'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@IsROSystemProperty'; SET @value = N'IsROSystemProperty'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FormatID'; SET @value = N'FormatID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_ProductProperty; GO /* POST Operation for ProductProperty Not existing ProductLine will be created, existing gets an Update If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call 1. PropertyID is empty => 404 Not Found 2. TransactionUsername not determined for User => 403 Forbidden 3. Username does not have write access to ProductLine => 401 Unauthorized 5. Property created => 201 Created 6. Property updated => 200 OK DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@ProductID NVARCHAR(255) = '1' ,@ProductLineID NVARCHAR(255) = 'U' ,@FactoryID NVARCHAR(255) = 'ZT' ,@PropertyID NVARCHAR(255) = 'rreo' ,@PropertyName NVARCHAR(255) = '' ,@CommentUser NVARCHAR(MAX) = '' ,@CommentDev NVARCHAR(MAX) = '' ,@Unit NVARCHAR(255) = '' ,@ValueText NVARCHAR(MAX) = '' ,@ValueInt NVARCHAR(255) = '678' ,@Scale NVARCHAR(255) = '<#NV>' ,@IsROSystemProperty NVARCHAR(255) = '1' ,@FormatID NVARCHAR(255) = '<#NV>' EXECUTE @RC = planning.spPOST_ProductProperty @Username, @ProductID, @ProductLineID, @FactoryID, @PropertyID, @PropertyName, @CommentUser, @CommentDev, @Unit, @ValueText, @ValueInt, @Scale, @IsROSystemProperty, @FormatID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ProductProperty',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ProductProperty','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_ProductProperty @Username NVARCHAR(255), @ProductID NVARCHAR(255), @ProductLineID NVARCHAR(255), @FactoryID NVARCHAR(255), @PropertyID NVARCHAR(255), @PropertyName NVARCHAR(255), @CommentUser NVARCHAR(MAX), @CommentDev NVARCHAR(MAX), @Unit NVARCHAR(255), @ValueText NVARCHAR(MAX), @ValueInt NVARCHAR(255), @Scale NVARCHAR(255), @IsROSystemProperty NVARCHAR(255), @FormatID NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @ProductKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @FactoryKey INT = 0; DECLARE @PropertyKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@PropertyID, N'NULL') + N''',''' + ISNULL(@PropertyName, N'NULL') + N''',''' + ISNULL(@CommentUser, N'NULL') + N''',''' + ISNULL(@CommentDev, N'NULL') + N''',''' + ISNULL(@Unit, N'NULL') + N''',''' + ISNULL(@ValueText, N'NULL') + N''',''' + ISNULL(@ValueInt, N'NULL') + N''',''' + ISNULL(@Scale, N'NULL') + N''',''' + ISNULL(@IsROSystemProperty, N'NULL') + N''',''' + ISNULL(@FormatID, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @PropertyID IS NULL SET @PropertyID = N''; IF @PropertyName IS NULL SET @PropertyName = N''; IF @CommentUser IS NULL SET @CommentUser = N''; IF @CommentDev IS NULL SET @CommentDev = N''; IF @Unit IS NULL SET @Unit = N''; IF @ValueText IS NULL SET @ValueText = N''; IF @ValueInt IS NULL SET @ValueInt = N''; IF @Scale IS NULL SET @Scale = N''; IF @FormatID IS NULL SET @FormatID = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_ProductProperty; -- STEP 0.2 - Protect input parameters SET @ProductID = system.fProtectID (@ProductID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @FactoryID = system.fProtectID (@FactoryID); SET @PropertyID = system.fProtectID (@PropertyID); SET @PropertyName = system.fProtectString (@PropertyName); SET @CommentUser = system.fProtectString (@CommentUser); SET @CommentDev = system.fProtectString (@CommentDev); SET @Unit = system.fProtectString (@Unit); SET @ValueText = system.fProtectString (@ValueText); SET @ValueInt = system.fProtectInt (@ValueInt); SET @Scale = system.fProtectInt (@Scale); IF @FormatID <> '<#NV>' SET @FormatID = system.fProtectID (@FormatID); --SET @PropertyOwner = system.fProtectBoolean (@IsROSystemProperty); -- NO Protection at the moment, as value can be 0,1,2 IF @FactoryID = N'' OR @ProductLineID = N'' OR @PropertyID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductID = @ProductID; SELECT @PropertyKey = PropertyKey FROM planning.tgProducts WHERE ProductKey = @ProductKey AND PropertyID = @PropertyID; IF @FactoryKey = 0 OR @ProductLineKey = 0 OR @ProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 1.3 - Don`t allow changing of system RO Properties - Disabled from version 4.0.63 on to enable export/import --SELECT @PropertyOwner = IsROSystemProperty FROM planning.tgProducts WHERE ProductKey = @ProductKey AND PropertyID = @PropertyID; --IF @PropertyOwner = 1 --BEGIN -- SET @ResultCode = 403; -- RAISERROR('Don`t allow changing of system RO Properties', 16, 10); --END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 3.1 - If Key does not yet exist, it will be created in minimal configuration IF @PropertyKey = 0 BEGIN DECLARE @Keys AS TABLE ([Key] INT NOT NULL); INSERT INTO planning.tgProducts (ProductKey, ProductLineKey, FactoryKey, ProductID, ProductLineID, FactoryID, PropertyID, PropertyName, CommentUser, CommentDev, Unit, ValueText, ValueInt, Scale, IsROSystemProperty, FormatID) OUTPUT INSERTED.PropertyKey INTO @Keys([Key]) VALUES (@ProductKey, @ProductLineKey, @FactoryKey, @ProductID, @ProductLineID, @FactoryID, @PropertyID, '', '', '', '', '', 0, 1, 0, ''); SELECT TOP (1) @PropertyKey = [Key] FROM @Keys ORDER BY [Key]; SET @Resultcode = 201; SET @EffectedRows = 1; END -- STEP 3.2 - The property receives the transmitted attributes undelivered be maintained UPDATE planning.tgProducts SET PropertyName = IIF(@PropertyName = N'<#NV>', PropertyName, @PropertyName) ,CommentUser = IIF(@CommentUser = N'<#NV>', CommentUser, @CommentUser) ,CommentDev = IIF(@CommentDev = N'<#NV>', CommentDev, @CommentDev) ,Unit = IIF(@Unit = N'<#NV>', Unit, @Unit) ,ValueText = IIF(@ValueText = N'<#NV>', ValueText, @ValueText) ,ValueInt = IIF(@ValueInt = N'<#NV>', ValueInt, @ValueInt) ,Scale = IIF(@Scale = N'<#NV>', Scale, @Scale) ,IsROSystemProperty = IIF(@IsROSystemProperty = N'<#NV>', IsROSystemProperty, @IsROSystemProperty) ,FormatID = IIF(@FormatID = N'<#NV>', FormatID, @FormatID) FROM planning.tgProducts WHERE PropertyKey = @PropertyKey; SET @EffectedRows += @@ROWCOUNT; COMMIT TRANSACTION sx_pf_POST_ProductProperty; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_ProductProperty; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_ProductProperty' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for ProductProperty Not existing ProductLine will be created, existing gets an Update If attributes passed with value <#NV>, it will be ignored during the update and maintain the existing value, during insert the attribute is created empty'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PropertyID'; SET @value = N'PropertyID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PropertyName'; SET @value = N'PropertyName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentUser'; SET @value = N'CommentUser'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentDev'; SET @value = N'CommentDev'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Unit'; SET @value = N'Unit'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueText'; SET @value = N'ValueText'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueInt'; SET @value = N'ValueInt'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Scale'; SET @value = N'Scale'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@IsROSystemProperty'; SET @value = N'IsROSystemProperty'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FormatID'; SET @value = N'FormatID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_Statement; GO /* POST Operation for a new Statement A Statement can only created, there is no Update or Delete Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC INT DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @ProductID NVARCHAR(255) = '1' DECLARE @ProductLineID NVARCHAR(255) = 'U' DECLARE @FactoryID NVARCHAR(255) = 'ZT' DECLARE @ActionType NVARCHAR(255) = 'SAVE' DECLARE @Statement NVARCHAR(255) = 'ALLES GUT' DECLARE @PCName NVARCHAR(255) = '' DECLARE @ProcessorCode NVARCHAR(255) = '' DECLARE @IPAddresses NVARCHAR(255) = '' DECLARE @IsDeleted NVARCHAR(255) = '' DECLARE @IsResolved NVARCHAR(255) = '' EXECUTE @RC = planning.spPOST_Statement @Username, @ProductID, @ProductLineID, @FactoryID, @ActionType, @Statement, @PCName, @ProcessorCode, @IPAddresses, @IsDeleted, @IsResolved PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Statement',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Statement','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_Statement @Username NVARCHAR(255), @ProductID NVARCHAR(255), @ProductLineID NVARCHAR(255), @FactoryID NVARCHAR(255), @ActionType NVARCHAR(255), @Statement NVARCHAR(255), @PCName NVARCHAR(255), @ProcessorCode NVARCHAR(255), @IPAddresses NVARCHAR(255), @IsDeleted NVARCHAR(255), @IsResolved NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @ProductKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @FactoryKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ActionType, N'NULL') + N''',''' + ISNULL(@Statement, N'NULL') + N''',''' + ISNULL(@PCName, N'NULL') + N''',''' + ISNULL(@ProcessorCode, N'NULL') + N''',''' + ISNULL(@IPAddresses, N'NULL') + N''',''' + ISNULL(@IsDeleted, N'NULL') + N''',''' + ISNULL(@IsResolved, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ActionType IS NULL SET @ActionType = N''; IF @Statement IS NULL SET @Statement = N''; IF @PCName IS NULL SET @PCName = N''; IF @ProcessorCode IS NULL SET @ProcessorCode = N''; IF @IPAddresses IS NULL SET @IPAddresses = N''; IF @IsDeleted IS NULL SET @IsDeleted = N''; IF @IsResolved IS NULL SET @IsResolved = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_Statement -- STEP 0.2 - Protect input parameters SET @ProductID = system.fProtectID (@ProductID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @FactoryID = system.fProtectID (@FactoryID); SET @ActionType = system.fProtectString (@ActionType); SET @Statement = system.fProtectString (@Statement); SET @PCName = system.fProtectString (@PCName); SET @ProcessorCode = system.fProtectString (@ProcessorCode); SET @IPAddresses = system.fProtectString (@IPAddresses); SET @IsDeleted = system.fProtectBoolean (@IsDeleted); SET @IsResolved = system.fProtectBoolean (@IsResolved); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE ProductLineID = @ProductLineID AND FactoryKey = @FactoryKey; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductID = @ProductID AND ProductLineKey = @ProductLinekey AND FactoryKey = @FactoryKey; IF @FactoryKey = 0 OR @ProductLineKey = 0 OR @ProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 3 - Insert new Statement INSERT INTO planning.tfStatements ( ProductKey, ProductLineKey, FactoryKey, ProductID, ProductLineID, FactoryID, ActionType, Statement, UserName, PCName, ProcessorCode, IPAddresses, Timestamp, IsDeleted, IsResolved) VALUES (@ProductKey, @ProductLineKey, @FactoryKey, @ProductID, @ProductLineID, @FactoryID, @ActionType, @Statement, @TransactUsername, @PCName, @ProcessorCode, @IPAddresses, GETUTCDATE(), @IsDeleted, @IsResolved); SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = 201; COMMIT TRANSACTION sx_pf_POST_Statement; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_Statement; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Statement' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for a new Statement A Statement can only created, there is no Update or Delete'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ActionType'; SET @value = N'ActionType'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Statement'; SET @value = N'Statement'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PCName'; SET @value = N'PCName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProcessorCode'; SET @value = N'ProcessorCode'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@IPAddresses'; SET @value = N'IPAddresses'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@IsDeleted'; SET @value = N'IsDeleted'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@IsResolved'; SET @value = N'IsResolved'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_Template_All; GO /* POST Operation to apply a template (again) to many Products Saxess Software GmbH Last modified: 01/2024 for OCT 5.10 Function principles: - Determine all Products in Scope and put them on a ToDo list - the fact table values from the existing products are temporary stored - numbers with scale information - formulas are not stored (but their result values, as this may now be a input value series) - the existing products are caved out - values are deleted - timeline is deleted - value series are deleted - the template is caved in - timeline is inserted - value series are inserted - all values and formulas are inserted - the old values are partly caved in - as long as they now belong of a value series of type input - they are also caved in for value series of type XLS/XLS Strict - to keep there values which are not formula based - transfers all existing data which now belongs to a column of type input Search Conditions: - Factory ZT is never used as target - empty Factory ID -> all Factories are effected - empty ProductLineID -> all ProductLines effected - empty Search Template -> all Products effected Testcall DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@ProductID NVARCHAR(255) = '2' ,@ProductLineID NVARCHAR(255) = '1' ,@FactoryID NVARCHAR(255) = '1' ,@SearchTemplateName NVARCHAR(255) = 'Unikum_VM' ,@TemplateProductID NVARCHAR(255) = '1' ,@TemplateProductLineID NVARCHAR(255) = 'U' ,@TemplateFactoryID NVARCHAR(255) = 'ZT' ,@CompareByNameInsteadOfIDFlag INT = 1; EXEC @RC = planning.spPOST_Template_All @Username, @ProductLineID, @FactoryID, @ProductID, @SearchTemplateName, @TemplateProductID, @TemplateProductLineID, @TemplateFactoryID, @CompareByNameInsteadOfIDFlag; PRINT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Template_All',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Template_All','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_Template_All @Username NVARCHAR(255) ,@ProductLineID NVARCHAR(255) ,@FactoryID NVARCHAR(255) ,@ProductID NVARCHAR(255) = '' ,@SearchTemplateName NVARCHAR(255) ,@TemplateProductID NVARCHAR(255) ,@TemplateProductLineID NVARCHAR(255) ,@TemplateFactoryID NVARCHAR(255) ,@CompareByNameInsteadOfIDFlag INT AS BEGIN DECLARE @TransactUsername NVARCHAR(255) = N'' ,@FactoryKey INT = 0 ,@ProductLineKey INT = 0 ,@ProductKey INT = 0 ,@TemplateFactoryKey INT = 0 ,@TemplateProductLineKey INT = 0 ,@TemplateProductKey INT = 0 ,@TemplateFactoryNameShort NVARCHAR(255) = N'' ,@TemplateProductLineNameShort NVARCHAR(255) = N'' ,@TemplateProductNameShort NVARCHAR(255) = N'' ,@TemplateName NVARCHAR(255) = N'' ,@ProductCount BIGINT ,@CounterSelf INT ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@SearchTemplateName, N'NULL') + N''',''' + ISNULL(@TemplateProductID, N'NULL') + N''',''' + ISNULL(@TemplateProductLineID, N'NULL') + N''',''' + ISNULL(@TemplateFactoryID, N'NULL') + N''',' + ISNULL(CAST(@CompareByNameInsteadOfIDFlag AS NVARCHAR(255)), N'NULL') ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N''; -- STEP A1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @SearchTemplateName IS NULL SET @SearchTemplateName = N''; IF @TemplateProductID IS NULL SET @TemplateProductID = N''; IF @TemplateProductLineID IS NULL SET @TemplateProductLineID = N''; IF @TemplateFactoryID IS NULL SET @TemplateFactoryID = N''; IF @CompareByNameInsteadOfIDFlag IS NULL SET @CompareByNameInsteadOfIDFlag = 0; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_Template_All; -- *********************************************** -- Do Standard Protection -- *********************************************** -- Protect input parameters SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); SET @SearchTemplateName = system.fProtectID (@SearchTemplateName); SET @TemplateFactoryID = system.fProtectID (@TemplateFactoryID); SET @TemplateProductLineID = system.fProtectID (@TemplateProductLineID); SET @TemplateProductID = system.fProtectID (@TemplateProductID); SET @CompareByNameInsteadOfIDFlag = system.fProtectInt (@CompareByNameInsteadOfIDFlag); -- Exit if CompareByNameInsteadOfIDFlag not 0 or 1 IF NOT (@CompareByNameInsteadOfIDFlag = 0 OR @CompareByNameInsteadOfIDFlag = 1) BEGIN SET @ResultCode = 404; RAISERROR('Invalid CompareByNameInsteadOfIDFlag parameter', 16, 10); END -- Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID; SELECT @TemplateFactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @TemplateFactoryID; SELECT @TemplateProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @TemplateFactoryKey AND ProductLineID = @TemplateProductLineID; SELECT @TemplateProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @TemplateProductLineKey AND ProductID = @TemplateProductID; SELECT @TemplateName = Template FROM planning.tdProducts WHERE ProductKey = @TemplateProductKey -- Stop if Template is invalid (Key of Target Factory / PL may all be 0) IF @TemplateProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- *********************************************** -- Create a ToDo List of all Products in scope -- *********************************************** DROP TABLE IF EXISTS #ProductToDoList; CREATE TABLE #ProductToDoList ( FactoryKey BIGINT NOT NULL ,ProductLineKey BIGINT NOT NULL ,ProductKey BIGINT NOT NULL ,FactoryID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ); -- Set all Products of scope on the ToDo List INSERT INTO #ProductToDoList SELECT dP.FactoryKey ,dP.ProductLineKey ,dP.ProductKey ,dP.FactoryID ,dP.ProductLineID ,dP.ProductID FROM planning.tdProducts dP WHERE (dP.FactoryKey = @FactoryKey OR @FactoryID = '') AND (dP.ProductLineKey = @ProductLineKey OR @ProductLineID = '') AND (dP.ProductKey = @ProductKey OR @ProductID = '') AND (dP.Template = @SearchTemplateName OR @SearchTemplateName = '') AND dP.ProductKey <> @TemplateProductKey; -- don't POST on yourself -- Stop if Template is inside target Products SELECT @CounterSelf = COUNT(ProductKey) FROM #ProductToDoList WHERE Productkey = @TemplateProductKey IF @CounterSelf > 0 BEGIN SET @ResultCode = 403; RAISERROR('Template is in same ProductLine as Target - stopped to not post template on itself.', 16, 10); END; -- Clean ToDo List from Products where user don't has rights DELETE FROM #ProductToDoList WHERE ProductKey IN ( SELECT ProductKey FROM #ProductToDoList ToDo LEFT JOIN system.trUserRights tUR ON ToDo.FactoryID = tUR.FactoryID AND ToDo.ProductLineID = tUR.ProductLineID WHERE tUR.[Right] != 'Write' AND tUR.UserName = @TransactUsername ); SELECT @ProductCount = COUNT(ProductKey) FROM #ProductToDoList; IF @ProductCount = 0 BEGIN SET @ResultCode = 404; RAISERROR('No fitting products found', 16, 10); END -- STEP 2.2 - Stop, if compare by name and there are doubled ValueSerieNames in any product IF @CompareByNameInsteadOfIDFlag = 1 AND EXISTS (SELECT dVS.ProductKey ,COUNT(NameShort) FROM planning.tdValueSeries dVS -- INNER JOIN to filter on Product on ToDo List INNER JOIN #ProductToDoList ToDO ON ToDo.ProductKey = dVS.ProductKey GROUP BY dVS.ProductKey, NameShort HAVING COUNT(NameShort)>1 ) BEGIN SET @ResultCode = 403; RAISERROR('ValueSeriesNames not unique', 16, 10); END; -- *********************************** -- STAGING Existing Data from Produts -- *********************************** -- Table for storing static Data CREATE TABLE #ProductDataStorage ( ProductKey BIGINT NOT NULL ,ValueSeriesID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesName NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,Scale INT NOT NULL ,TimeID INT NOT NULL ,ValueInt BIGINT NOT NULL ,ValueText NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueComment NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,[IsNumeric] INT NOT NULL ); -- Store Data of Products in Storage (but not formulas, they come from template) -- ToDo: Keep new formulas out in this step !! INSERT INTO #ProductDataStorage SELECT fV.ProductKey ,fV.ValueSeriesID ,dVS.NameShort AS ValueSeriesName ,dVS.Scale ,fV.TimeID ,fV.ValueInt ,fV.ValueText ,fV.ValueComment ,dVS.[IsNumeric] FROM planning.tfValues fV -- for ValueSeriesNames LEFT JOIN planning.tdValueSeries dVS ON fV.ValueSeriesKey = dVS.ValueSeriesKey -- filter Products on ToDo List INNER JOIN #ProductToDoList ToDo ON fV.ProductKey = ToDo.ProductKey; -- *********************************** -- Cave out existing Products -- *********************************** -- STEP 3.2 - Delete all Values and ValueSeries of old Products (but not the dProduct, Statements...) DELETE FROM planning.tfValues WHERE ProductKey IN (SELECT DISTINCT ProductKey FROM #ProductToDoList); SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tdValueSeries WHERE ProductKey IN (SELECT DISTINCT ProductKey FROM #ProductToDoList); SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tdTime WHERE ProductKey IN (SELECT DISTINCT ProductKey FROM #ProductToDoList); SET @EffectedRows += @@ROWCOUNT; DELETE FROM planning.tgProducts WHERE ProductKey IN (SELECT DISTINCT ProductKey FROM #ProductToDoList); SET @EffectedRows += @@ROWCOUNT; -- *********************************** -- Cave in Template -- *********************************** -- keep new VS Keys temporary stored CREATE TABLE #VSKeys( ValueSeriesKey BIGINT NOT NULL ,ProductKey BIGINT NOT NULL ,ProductLineKey BIGINT NOT NULL ,FactoryKey BIGINT NOT NULL ,ValueSeriesID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,FactoryID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesName NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,Scale INT NOT NULL ,ValueSource NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ); -- Copy ValueSeries of Template to all Products and store their Keys INSERT INTO planning.tdValueSeries ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,ValueSeriesNo ,NameShort ,NameLong ,CommentUser ,CommentDev ,ImageName ,IsNumeric ,VisibilityLevel ,ValueSource ,ValueListID ,ValueFormatID ,Unit ,Scale ,Effect ,EffectParameter ,AllowZero ) OUTPUT INSERTED.ValueSeriesKey ,INSERTED.ProductKey ,INSERTED.ProductLineKey ,INSERTED.FactoryKey ,INSERTED.ValueSeriesID ,INSERTED.ProductID ,INSERTED.ProductLineID ,INSERTED.FactoryID ,INSERTED.NameShort ,INSERTED.Scale ,INSERTED.ValueSource INTO #VSKeys SELECT ToDo.ProductKey ,ToDo.ProductLineKey ,ToDo.FactoryKey ,ToDo.ProductID ,ToDo.ProductLineID ,ToDo.FactoryID ,dVS.ValueSeriesID ,dVS.ValueSeriesNo ,dVS.NameShort ,dVS.NameLong ,dVS.CommentUser ,dVS.CommentDev ,dVS.ImageName ,dVS.IsNumeric ,dVS.VisibilityLevel ,dVS.ValueSource ,dVS.ValueListID ,dVS.ValueFormatID ,dVS.Unit ,dVS.Scale ,dVS.Effect ,dVS.EffectParameter ,dVs.AllowZero FROM planning.tdValueSeries dVS LEFT JOIN #ProductToDoList ToDO ON 1=1 WHERE dVS.ProductKey = @TemplateProductKey; -- Copy Timeline of Template to Product INSERT INTO planning.tdTime ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,TimeID ,FormatID ) SELECT ToDo.ProductKey ,ToDo.ProductLineKey ,ToDo.FactoryKey ,ToDo.ProductID ,ToDo.ProductLineID ,ToDo.FactoryID ,dT.TimeID ,dT.FormatID FROM planning.tdTime dT-- spread to all products LEFT JOIN #ProductToDoList ToDO ON 1=1 WHERE dT.ProductKey = @TemplateProductKey; -- Copy Values and Formulas of Template to Products INSERT INTO planning.tfValues ( ValueSeriesKey ,ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueText ,ValueComment ) SELECT vk.ValueSeriesKey ,vk.ProductKey ,vk.ProductLineKey ,vk.FactoryKey ,vk.ProductID ,vk.ProductLineID ,vk.FactoryID ,fV.ValueSeriesID ,fV.TimeID ,fV.ValueFormula ,fV.ValueInt ,fV.ValueText ,fV.ValueComment FROM planning.tfValues fV LEFT JOIN #VSKeys vk ON fV.ValueSeriesID = vk.ValueSeriesID WHERE fV.ProductKey = @TemplateProductKey; -- Step 3.4 Copy Entrys in gProducts INSERT INTO planning.tgProducts ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,PropertyID ,PropertyName ,CommentUser ,CommentDev ,Unit ,ValueText ,ValueInt ,Scale ,IsROSystemProperty ,FormatID ) SELECT ToDo.ProductKey ,ToDo.ProductLineKey ,ToDo.FactoryKey ,ToDo.ProductID ,ToDo.ProductLineID ,ToDo.FactoryID ,gP.PropertyID ,gP.PropertyName ,gP.CommentUser ,gP.CommentDev ,gP.Unit ,gP.ValueText ,gP.ValueInt ,gP.Scale ,gP.IsROSystemProperty ,gP.FormatID FROM planning.tgProducts gP -- spread to all products LEFT JOIN #ProductToDoList ToDO ON 1=1 WHERE gP.ProductKey = @TemplateProductKey -- =============================================================== --> Now all Products are complete fresh 1:1 copies of the Template -- =============================================================== -- ***************************************************************** -- cave in the transfer values if they still fit to the template -- ***************************************************************** -- Possible Changes: -- New ValueSeries where created => ok, exists empty -- ValueSeries where deleted => ok, values lost -- ValueSeries of type Formula where changed in its definition => ok, new formulas are used -- The timeline was changed => ok, only fitting values are transfered -- ValueSeriesID or Names may be changed => ok, but one must keept constant -- Typ of Input Series has changed from Int to Text etc. => ok, empty then -- Scale of numeric Input Series has changed => ok, old values will be rescaled -- Delete Transfervalues, which belong to no longer existing TimeIDs DELETE FROM #ProductDataStorage WHERE TimeID NOT IN ( SELECT dT.TimeID FROM planning.tdTime dT WHERE dT.ProductKey = @TemplateProductKey ); -- Transfer stored Values by ID or Name IF @CompareByNameInsteadOfIDFlag = 0 -- compare by ID BEGIN -- Delete Transfervalues, which belong to no longer existing IDs DELETE FROM #ProductDataStorage WHERE ValueSeriesID NOT IN ( SELECT dVS.ValueSeriesID FROM planning.tdValueSeries dVS WHERE dVS.ProductKey = @TemplateProductKey ); -- Delete Transfer Values, which belong to Value Series, which DataType has changed DELETE pds FROM #ProductDataStorage pds LEFT JOIN ( SELECT dVS.ValueSeriesID ,dVS.[IsNumeric] FROM planning.tdValueSeries dVS WHERE dVS.ProductKey = @TemplateProductKey ) Template ON pds.ValueSeriesID = Template.ValueSeriesID WHERE pds.[IsNumeric] <> Template.[IsNumeric]; -- Values, which belong to value series, which are formula based now are not deleted completely -- they are keept in case that the new cell has no formula -- this happens, to keep value in value series of type XLS-Strict, where XLS-Strict just protects the cell DELETE FROM #ProductDataStorage WHERE -- defined as formula in Template EXISTS ( SELECT fV.TimeID ,fV.ValueSeriesID FROM planning.tfValues fV LEFT JOIN planning.tdValueSeries dVS ON fV.ValueSeriesKey = dVS.ValueSeriesKey WHERE fV.ProductKey = @TemplateProductKey AND dVS.ValueSource IN ('XLS','XLS-Strict') AND fV.ValueFormula <> '' -- OUTER Correlation AND #ProductDataStorage.TimeID = fV.TimeID AND #ProductDataStorage.ValueSeriesID = fV.ValueSeriesID ); -- Delete Values in fact table to make space for Transfervalues -> WHY DO we need this, when all Products are fresh copies of the template ? DELETE fV FROM planning.tfValues fV INNER JOIN #ProductToDoList ToDo ON fV.ProductKey = ToDo.ProductKey INNER JOIN #ProductDataStorage PDS ON ToDo.ProductKey = PDS.ProductKey AND fV.ValueSeriesID = PDS.ValueSeriesID AND fV.TimeID = PDS.TimeID; -- Transfer OVER INSERT INSERT INTO planning.tfValues ( ValueSeriesKey ,ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueText ,ValueComment ) SELECT vk.ValueSeriesKey ,vk.ProductKey ,vk.ProductLineKey ,vk.FactoryKey ,vk.ProductID ,vk.ProductLineID ,vk.FactoryID ,pds.ValueSeriesID ,pds.TimeID ,'' AS ValueFormula ,CAST(CAST(pds.ValueInt AS MONEY) / pds.Scale * vk.Scale AS BIGINT) --rescaling the int Values ,pds.ValueText ,pds.ValueComment FROM #ProductDataStorage pds LEFT JOIN #VSKeys vk ON pds.ProductKey = vk.ProductKey AND pds.ValueSeriesID = vk.ValueSeriesID; END ELSE BEGIN -- compare by Name -- Delete Transfervalues, which belong to no longer existing Names DELETE FROM #ProductDataStorage WHERE ValueSeriesName NOT IN ( SELECT dVS.NameShort FROM planning.tdValueSeries dVS WHERE dVS.ProductKey = @TemplateProductKey ); -- Delete Transfer Values, which belong to Value Series, which DataType has changed DELETE pds FROM #ProductDataStorage pds LEFT JOIN ( SELECT dVS.NameShort ,dVS.[IsNumeric] FROM planning.tdValueSeries dVS WHERE dVS.ProductKey = @TemplateProductKey ) Template ON pds.ValueSeriesName = Template.NameShort WHERE pds.[IsNumeric] <> Template.[IsNumeric]; DELETE FROM #ProductDataStorage WHERE -- defined as formula in Template EXISTS ( SELECT fV.TimeID ,dVS.NameShort FROM planning.tfValues fV LEFT JOIN planning.tdValueSeries dVS ON fV.ValueSeriesKey = dVS.ValueSeriesKey WHERE fV.ProductKey = @TemplateProductKey AND dVS.ValueSource IN ('XLS','XLS-Strict') AND fV.ValueFormula <> '' -- OUTER Correlation AND #ProductDataStorage.TimeID = fV.TimeID AND #ProductDataStorage.ValueSeriesName = dVS.NameShort ); -- Delete Values in fact table to make space for Transfervalues DELETE fV FROM planning.tfValues fV LEFT JOIN planning.tdValueSeries dVS ON fV.ValueSeriesKey = dVS.ValueSeriesKey LEFT JOIN #ProductToDoList ToDo ON fV.ProductKey = ToDo.ProductKey LEFT JOIN #ProductDataStorage PDS ON ToDo.ProductKey = PDS.ProductKey AND dVS.NameShort = PDS.ValueSeriesName AND fV.TimeID = PDS.TimeID WHERE PDS.TimeID IS NOT NULL AND PDS.ValueSeriesName IS NOT NULL -- Insert all Transfervalues INSERT INTO planning.tfValues ( ValueSeriesKey ,ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueText ,ValueComment ) SELECT vk.ValueSeriesKey ,vk.ProductKey ,vk.ProductLineKey ,vk.FactoryKey ,vk.ProductID ,vk.ProductLineID ,vk.FactoryID ,dVS.ValueSeriesID ,pds.TimeID ,'' AS ValueFormula ,CAST(CAST(pds.ValueInt AS MONEY) / pds.Scale * vk.Scale AS BIGINT) --rescaling the int Values ,pds.ValueText ,pds.ValueComment FROM #ProductDataStorage pds -- to determine ValueSeries Key LEFT JOIN #VSKeys vk ON pds.ProductKey = vk.ProductKey AND pds.ValueSeriesName = vk.ValueSeriesName -- to determine new ValueSeriesID LEFT JOIN ( SELECT dVS.ValueSeriesID ,dVS.NameShort FROM planning.tdValueSeries dVS WHERE dVS.ProductKey = @TemplateProductKey ) dVS ON dVS.NameShort = pds.ValueSeriesName END; -- Create a System Statement INSERT INTO planning.tfStatements ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ActionType ,Statement ,UserName ,PCName ,ProcessorCode ,IPAddresses ,Timestamp ,IsDeleted ,IsResolved ) SELECT ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,N'POST_Template_All' AS ActionType ,N'Product Template updated with Template Factory <' + @TemplateFactoryID + N'>"' + @TemplateFactoryNameShort + N'"/ ProductLine <' + @TemplateProductLineID + N'>"' + @TemplateProductLineNameShort + N'"/ Product <' + @TemplateProductID + N'>"' + @TemplateProductNameShort + '"' AS Statement ,@TransactUsername AS UserName ,N'' AS PCName ,N'' AS ProcessorCode ,N'' AS IPAddresses ,GETUTCDATE() AS Timestamp ,0 AS IsDeleted ,0 AS IsResolved FROM #ProductToDoList -- Update the TemplateName of the Products UPDATE planning.tdProducts SET Template = @TemplateName ,TimeType = RIGHT(@TemplateName, 2) WHERE ProductKey IN (SELECT ProductKey FROM #ProductToDoList); SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = 200; COMMIT TRANSACTION sx_pf_POST_Template_All; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_Template_All; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Template_All' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'/* POST Operation to apply a template (again) to many Products'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SearchTemplateName'; SET @value = N'SearchTemplateName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TemplateProductID'; SET @value = N'TemplateProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TemplateProductLineID'; SET @value = N'TemplateProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TemplateFactoryID'; SET @value = N'TemplateFactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CompareByNameInsteadOfIDFlag'; SET @value = N'CompareByNameInsteadOfIDFlag'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_Template; GO /* POST Operation to apply a template (again) on a Product Existing Values in the Product will be saved, new/other logic will be added It means the Template inherits its properties again to the object The logical process is describe in procedure POST_TEMPLATE_ALL to which the work is delegated Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@ProductID NVARCHAR(255) = '2' ,@ProductLineID NVARCHAR(255) = 'U' ,@FactoryID NVARCHAR(255) = 'ZT' ,@TemplateProductID NVARCHAR(255) = '1' ,@TemplateProductLineID NVARCHAR(255) = 'U' ,@TemplateFactoryID NVARCHAR(255) = 'ZT' ,@CompareByNameInsteadOfIDFlag INT = 0 EXECUTE @RC = planning.spPOST_Template @Username ,@ProductID ,@ProductLineID ,@FactoryID ,@TemplateProductID ,@TemplateProductLineID ,@TemplateFactoryID ,@CompareByNameInsteadOfIDFlag PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Template',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Template','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_Template @Username NVARCHAR(255), @ProductID NVARCHAR(255), @ProductLineID NVARCHAR(255), @FactoryID NVARCHAR(255), @TemplateProductID NVARCHAR(255), @TemplateProductLineID NVARCHAR(255), @TemplateFactoryID NVARCHAR(255), @CompareByNameInsteadOfIDFlag INT AS BEGIN DECLARE @TransactUsername NVARCHAR(255) = N'' ,@FactoryKey INT = 0 ,@ProductLineKey INT = 0 ,@ProductKey INT = 0 ,@TemplateFactoryKey INT = 0 ,@TemplateProductLineKey INT = 0 ,@TemplateProductKey INT = 0 ,@TemplateName NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@TemplateProductID, N'NULL') + N''',''' + ISNULL(@TemplateProductLineID, N'NULL') + N''',''' + ISNULL(@TemplateFactoryID, N'NULL') + N''',' + ISNULL(CAST(@CompareByNameInsteadOfIDFlag AS NVARCHAR(255)), N'NULL') ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @TemplateProductID IS NULL SET @TemplateProductID = N''; IF @TemplateProductLineID IS NULL SET @TemplateProductLineID = N''; IF @TemplateFactoryID IS NULL SET @TemplateFactoryID = N''; IF @CompareByNameInsteadOfIDFlag IS NULL SET @CompareByNameInsteadOfIDFlag = 0; BEGIN TRY -- No transaction started, as delegated to other Procedure -- STEP 0.2 - Protect input parameters SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductID = system.fProtectID (@ProductID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @TemplateFactoryID = system.fProtectID (@TemplateFactoryID); SET @TemplateProductLineID = system.fProtectID (@TemplateProductLineID); SET @TemplateProductID = system.fProtectID (@TemplateProductID); SET @CompareByNameInsteadOfIDFlag = system.fProtectInt (@CompareByNameInsteadOfIDFlag); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 0.3. - Exit if CompareByNameInsteadOfIDFlag not 0 or 1 IF NOT (@CompareByNameInsteadOfIDFlag = 0 OR @CompareByNameInsteadOfIDFlag = 1) BEGIN SET @ResultCode = 404; RAISERROR('Invalid CompareByNameInsteadOfIDFlag parameter', 16, 10); END -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @TemplateFactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @TemplateFactoryID; SELECT @TemplateProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @TemplateFactoryKey AND ProductLineID = @TemplateProductLineID; -- Stop on invalid Keys IF @FactoryKey = 0 OR @ProductLineKey = 0 OR @TemplateFactoryKey = 0 OR @TemplateProductLineKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 BEGIN EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); END; -- STEP 2.1 - Try to identify the product keys SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID; SELECT @TemplateProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @TemplateProductLineKey AND ProductID = @TemplateProductID; -- Stop if Keys don't exist IF @ProductKey = 0 OR @TemplateProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- Stop if Template is posted on itself IF @ProductKey = @TemplateProductKey BEGIN SET @ResultCode = 404; RAISERROR('Stop, as Template cant be posted on itself.', 16, 10); END; -- STEP 2.2 - Stop, if compare by name and there are doubled ValueSerieNames in Template IF @CompareByNameInsteadOfIDFlag = 1 AND EXISTS (SELECT COUNT(NameShort) FROM planning.tdValueSeries WHERE ProductKey = @TemplateProductKey GROUP BY NameShort HAVING COUNT(NameShort) > 1) BEGIN SET @ResultCode = 403; RAISERROR('ValueSeriesNames not unique in Template', 16, 10); END -- STEP 2.3 - Stop, if compare by name and there are doubled ValueSerieNames in Product IF @CompareByNameInsteadOfIDFlag = 1 AND EXISTS (SELECT COUNT(NameShort) FROM planning.tdValueSeries WHERE ProductKey = @ProductKey GROUP BY NameShort HAVING COUNT(NameShort) > 1) BEGIN SET @ResultCode = 403; RAISERROR('ValueSeriesNames not unique in Target Product', 16, 10); END -- Determine Name of SearchTempale SELECT @TemplateName = Template FROM planning.tdProducts WHERE ProductKey = @TemplateProductKey; -- DELEGATE Operation to POST_TEMPLATE ALL ############################################################################# EXEC @Resultcode = planning.spPOST_Template_All 'SQL' ,@ProductLineID ,@FactoryID ,@ProductID ,@TemplateName --for @SearchTemplateName ,@TemplateProductID ,@TemplateProductLineID ,@TemplateFactoryID ,@CompareByNameInsteadOfIDFlag; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Template' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation to apply a template (again) on a Product Existing Values in the Product will be saved, new/other logic will be added It means the Template inherits its properties again to the object'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TemplateProductID'; SET @value = N'TemplateProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TemplateProductLineID'; SET @value = N'TemplateProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TemplateFactoryID'; SET @value = N'TemplateFactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CompareByNameInsteadOfIDFlag'; SET @value = N'CompareByNameInsteadOfIDFlag'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_TimeID; GO /* POST Operation for TimeID Create new TimeID Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test call DECLARE @RC INT DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @ProductID NVARCHAR(255) = '1' DECLARE @ProductLineID NVARCHAR(255) = 'U' DECLARE @FactoryID NVARCHAR(255) = 'ZT' DECLARE @TimeID INT = 20150107 DECLARE @FormatID NVARCHAR(255) = 'TimeBorderNext' EXECUTE @RC = planning.spPOST_TimeID @Username ,@ProductID ,@ProductLineID ,@FactoryID ,@TimeID ,@FormatID PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_TimeID',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_TimeID','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_TimeID @Username NVARCHAR(255), @ProductID NVARCHAR(255), @ProductLineID NVARCHAR(255), @FactoryID NVARCHAR(255), @TimeID INT, @FormatID NVARCHAR(255) = '' AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',' + ISNULL(CAST(@TimeID AS NVARCHAR(255)) + N',''' + ISNULL(@FormatID, N'NULL') + '', N'NULL'); DECLARE @EffectedRows INTEGER = 0; DECLARE @ResultCode INTEGER = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @TimeID IS NULL SET @TimeID = 0; IF @FormatID IS NULL SET @FormatID = ''; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_TimeID; IF @TimeID = 0 BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 0.2 - Protect input parameters SET @ProductID = system.fProtectID (@ProductID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @FactoryID = system.fProtectID (@FactoryID); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE ProductLineID = @ProductLineID AND FactoryKey = @FactoryKey; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductID = @ProductID AND ProductLineKey = @ProductLinekey AND FactoryKey = @FactoryKey; IF @FactoryKey = 0 OR @ProductLineKey = 0 OR @ProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 2.1 - Delete TimeID DELETE FROM planning.tdTime WHERE ProductKey = @ProductKey AND TimeID = @TimeID; IF @@RowCount = 1 SET @ResultCode = 200; ELSE SET @ResultCode = 0; -- STEP 3 - Create new TimeID INSERT INTO planning.tdTime ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,TimeID ,FormatID ) VALUES ( @ProductKey, @ProductLineKey, @FactoryKey, @ProductID, @ProductLineID, @FactoryID, @TimeID, @FormatID ); IF @ResultCode <> 200 SET @ResultCode = 201; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION sx_pf_POST_TimeID; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_TimeID; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF (@ResultCode IS NULL OR @ResultCode/100 = 2) AND @ResultCode <> 204 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_TimeID' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for TimeID Create new TimeID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TimeID'; SET @value = N'TimeID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FormatID'; SET @value = N'FormatID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_Timeline; GO /* POST Operation for the Timeline The method can be switched between incremental POST and full POST Values with DeleteFlag will be deleted Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.9 Test case 1 - Incremental Sended 1. One Value for an existing Value must replace them 2. One Value for an not exiting Value must create it 3. If all Parameter are posted empty, the value is deleted 4. if one or more of the parameter are sended with <#NV> there Value is replaced with the existing value (or nothing if it exists not) DECLARE @RC int DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @ProductID NVARCHAR(255) = '1' DECLARE @ProductLineID NVARCHAR(255) = 'U' DECLARE @FactoryID NVARCHAR(255) = 'ZT' DECLARE @IsIncrementalValuesFlag INT = 0 --3 TimeIDs DECLARE @TimeIDsInBracketsCommaSeparated nvarchar(max) = '[(20160115,0,0,''FormatID1'')],[(20160215,0,0,''FormatID2'')],[(20160415,0,0,''FormatID3'')]' EXECUTE @RC = planning.spPOST_Timeline @Username ,@ProductID ,@ProductLineID ,@FactoryID ,@IsIncrementalValuesFlag ,@TimeIDsInBracketsCommaSeparated PRINT @RC Test case 2 - all values in a Value Series of Products: Additional 1. Empty FactoryID / ProductLineID / ProductID / ValueSeriesID / TimeID => 404 Not Found DECLARE @RC int DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @ProductID NVARCHAR(255) = '21' DECLARE @ProductLineID NVARCHAR(255) = '1' DECLARE @FactoryID NVARCHAR(255) = 'U' DECLARE @IsIncrementalValuesFlag INT = 0 DECLARE @TimeIDsInBracketsCommaSeparated NVARCHAR(max) = '' DECLARE @start_time DATETIME -- Generate fictitious values for all TimeIDs of Products SELECT @TimeIDsInBracketsCommaSeparated = @TimeIDsInBracketsCommaSeparated + '[(''' + dVS2.ValueSeriesID + ''',' + CAST(TimeID AS nvarchar(255)) +',''=A2'',1000,''Hase'',''Kommentar'')],' FROM planning.tfValues fV LEFT JOIN planning.tdValueSeries dVS ON fV.ValueSeriesKey = dVS.ValueSeriesKey LEFT JOIN (SELECT ValueSeriesID FROM planning.tdValueSeries WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND ProductID = @ProductID GROUP BY ValueSeriesID) dVS2 ON 1=1 WHERE fV.FactoryID = @FactoryID AND fV.ProductLineID = @ProductLineID AND fV.ProductID = @ProductID AND dVS.ValueSeriesNo = 1 SET @TimeIDsInBracketsCommaSeparated = Left(@TimeIDsInBracketsCommaSeparated,Len(@TimeIDsInBracketsCommaSeparated) -1) PRINT @TimeIDsInBracketsCommaSeparated SET @start_time = GETUTCDATE() EXECUTE @RC = planning.spPOST_ProductDataTableValues @Username ,@ProductID ,@ProductLineID ,@FactoryID ,@IsIncrementalValuesFlag ,@TimeIDsInBracketsCommaSeparated PRINT @RC SELECT RTRIM(CAST(DATEDIFF(MS, @start_time, GETUTCDATE()) AS CHAR(10))) AS 'TimeTaken' @TimeIDsInBracketsCommaSeparated format: (TimeID,DeleteFlag,AddValue,FormatID) if you check this in SQL Server Profiler, every String must have two inverted commas, the whole Parameter must have Single inverted comma this must show profiler: '[(''TimeID,DeleteFlag,AddValue,''FormatID'')],[(TimeID,DeleteFlag,AddValue,FormatID)]' if you check this in table system.tAPILog, every String must have one inverted comma, whole String has no inverted comma this must show Log: [(TimeID,DeleteFlag,AddValue,FormatID)],[(TimeID,DeleteFlag,AddValue,'FormatID')] Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Timeline',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Timeline','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_Timeline @Username NVARCHAR(255), @ProductID NVARCHAR(255), @ProductLineID NVARCHAR(255), @FactoryID NVARCHAR(255), @IsIncrementalValuesFlag INT, @TimeIDsInBracketsCommaSeparated NVARCHAR(MAX) AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username , N'NULL') + N''',''' + ISNULL(@ProductID , N'NULL') + N''',''' + ISNULL(@ProductLineID , N'NULL') + N''',''' + ISNULL(@FactoryID , N'NULL') + N''',''' + ISNULL(@TimeIDsInBracketsCommaSeparated , N'NULL') + N''''; DECLARE @EffectedRows INTEGER = 0; DECLARE @ResultCode INTEGER = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR (2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @IsIncrementalValuesFlag IS NULL SET @IsIncrementalValuesFlag = 0; IF @TimeIDsInBracketsCommaSeparated IS NULL SET @TimeIDsInBracketsCommaSeparated = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_Timeline; -- STEP 0.2 - Protect input parameters SET @FactoryID = system.fProtectID (@FactoryID) SET @ProductID = system.fProtectID (@ProductID) SET @ProductLineID = system.fProtectID (@ProductLineID) IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID; IF @FactoryKey = 0 OR @ProductLineKey = 0 OR @ProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 3.2 - Check for count of delimiter in Array DECLARE @OpenDelimetersCount INT = 0; DECLARE @DelimetersIndex INT = CHARINDEX(N'[(', @TimeIDsInBracketsCommaSeparated); WHILE (@DelimetersIndex > 0) BEGIN SET @OpenDelimetersCount += 1; SET @DelimetersIndex = CHARINDEX(N'[(', @TimeIDsInBracketsCommaSeparated, @DelimetersIndex + 1); END; IF @OpenDelimetersCount = 0 BEGIN SET @ResultCode = 404; RAISERROR('No delimiters found in @TimeIDsInBracketsCommaSeparated', 16, 10); END; SET @DelimetersIndex = CHARINDEX(N')]', @TimeIDsInBracketsCommaSeparated); WHILE (@DelimetersIndex > 0) BEGIN SET @OpenDelimetersCount -= 1; SET @DelimetersIndex = CHARINDEX(N')]', @TimeIDsInBracketsCommaSeparated, @DelimetersIndex + 1); END; IF @OpenDelimetersCount <> 0 BEGIN SET @ResultCode = 404; RAISERROR('Invalid count of delimiter in @TimeIDsInBracketsCommaSeparated', 16, 10); END; -- STEP 3.3 - Splitt the Array with the new values for the Product to a temporary table ####################################################################################################################### -- cut leading/following brackets and ' from ValueSeriesID SET @TimeIDsInBracketsCommaSeparated = LEFT(@TimeIDsInBracketsCommaSeparated, LEN(@TimeIDsInBracketsCommaSeparated) - 2); SET @TimeIDsInBracketsCommaSeparated = RIGHT(@TimeIDsInBracketsCommaSeparated, LEN(@TimeIDsInBracketsCommaSeparated) - 2); DECLARE @tblValues1 TABLE (txtValuesPipe NVARCHAR(MAX) NOT NULL); DECLARE @tblValues2 TABLE (TimeID NVARCHAR(MAX) NOT NULL, txtValuesPipe NVARCHAR(MAX) NOT NULL); DECLARE @tblValues3 TABLE (TimeID BIGINT NOT NULL, DeleteFlag INT NOT NULL, txtValuesPipe NVARCHAR(MAX) NOT NULL); DECLARE @tblValues4 TABLE (TimeID BIGINT NOT NULL, DeleteFlag INT NOT NULL, AddValue BIGINT NOT NULL, FormatID NVARCHAR(255) NOT NULL); -- Split String into Rows INSERT INTO @tblValues1 SELECT txtValues FROM system.fPivotStringIntoTable(@TimeIDsInBracketsCommaSeparated, N')],[('); -- Split one column into two Columns (separate TimeID) INSERT INTO @tblValues2 SELECT LEFT(txtValuesPipe, CHARINDEX(N',', txtValuesPipe)-1 ) AS TimeID ,RIGHT(txtValuesPipe, LEN(txtValuesPipe) - CHARINDEX(N',', txtValuesPipe)) AS Rest FROM @tblValues1; IF EXISTS(SELECT TimeID FROM @tblValues2 GROUP BY TimeID HAVING(COUNT(TimeID) > 1)) BEGIN SET @ResultCode = 404; RAISERROR('Doubled TimeID in @TimeIDsInBracketsCommaSeparated', 16, 10); END; -- Split last columns once more (separate DeleteFlag) INSERT INTO @tblValues3 SELECT TimeID ,LEFT(txtValuesPipe, CHARINDEX(N',', txtValuesPipe) - 1) AS DeleteFlag ,RIGHT(txtValuesPipe, LEN(txtValuesPipe) - CHARINDEX(N',', txtValuesPipe)-1) AS Rest FROM @tblValues2; -- Split last columns once more (separate AddValue) INSERT INTO @tblValues4 SELECT TimeID ,DeleteFlag ,LEFT(txtValuesPipe, CHARINDEX(N',', txtValuesPipe) - 1) AS AddValue ,REPLACE(Right(txtValuesPipe, LEN(txtValuesPipe) - 1), N'''','') AS FormatID FROM @tblValues3; -- STEP 3.4 - Actions ####################################################################################################################### -- Delete TimeIDs which are sended as deleted in fValues and in dTime DELETE FROM planning.tdTime WHERE @ProductKey = @ProductKey AND TimeID IN (SELECT TimeID FROM @tblValues4 WHERE DeleteFlag = 1); SET @EffectedRows = @@ROWCOUNT; DELETE FROM planning.tfValues WHERE ProductKey = @ProductKey AND TimeID IN (SELECT TimeID FROM @tblValues4 WHERE DeleteFlag = 1); SET @EffectedRows += @@ROWCOUNT; -- STEP 3.5 - CASE FULL transmission or Incremental does not matter anymore ######################################### IF @IsIncrementalValuesFlag != 1 OR @IsIncrementalValuesFlag = 1 BEGIN -- Insert all New TimeIDs INSERT INTO planning.tdTime ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,TimeID ,FormatID ) SELECT @ProductKey, @ProductLineKey, @FactoryKey, @ProductID, @ProductLineID, @FactoryID, TimeID, FormatID FROM @tblValues4 WHERE TimeID NOT IN (SELECT TimeID FROM planning.tdTime WHERE ProductKey = @ProductKey); SET @EffectedRows += @@ROWCOUNT; END; COMMIT TRANSACTION sx_pf_POST_Timeline; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_Timeline; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Timeline' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for the Timeline The method can be switched between incremental POST and full POST Values with DeleteFlag will be deleted'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@IsIncrementalValuesFlag'; SET @value = N'IsIncrementalValuesFlag'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TimeIDsInBracketsCommaSeparated'; SET @value = N'TimeIDsInBracketsCommaSeparated'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_Value; GO /* POST Operation for Value The Value will be deleted and writen new Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Test calls DECLARE @RC INT ,@Username NVARCHAR(255) = 'SQL' ,@ProductID NVARCHAR(255) = '1' ,@ProductLineID NVARCHAR(255) = 'U' ,@FactoryID NVARCHAR(255) = 'ZT' ,@ValueSeriesID NVARCHAR(255) = 'E1' ,@TimeID BIGINT = 20180315 ,@ValueFormula NVARCHAR(max) = '<#NV>' ,@ValueInt NVARCHAR(255) = '555' ,@ValueText NVARCHAR(max) = 'Hase2' ,@ValueComment NVARCHAR(max) = 'Hase2' ,@i INT = 0 -- individual values -- Text with Comment EXECUTE @RC = planning.spPOST_Value @Username, @ProductID, @ProductLineID, @FactoryID, 'E1', 20180115, '', 0, 'Hase', 'Kommentar für Hase'; PRINT @RC -- Integer with Comment EXECUTE @RC = planning.spPOST_Value @Username, @ProductID, @ProductLineID, @FactoryID, 'K1', 20180215, '', 44, '', 'Kommentar für Hase44'; PRINT @RC -- 1b. Variant with loop --WHILE @i < 1000 --BEGIN EXECUTE @RC = planning.spPOST_Value @Username, @ProductID, @ProductLineID, @FactoryID, @ValueSeriesID, @TimeID, @ValueFormula, @i, @ValueText, @ValueComment --SET @i = @i +1 PRINT @RC --END -- 2. Variant with Script Creator PRINT 'DECLARE @RC as INTEGER = 0' WHILE @i < 5000 BEGIN PRINT 'EXECUTE @RC = planning.spPOST_Value ''' + @Username + ''','''+ @ProductID+ ''','''+@ProductLineID+ ''','''+ @FactoryID + ''','''+ @ValueSeriesID + ''','+ CAST(@TimeID AS NVARCHAR (8)) + ','''+ @ValueFormula + ''','+ CAST(@i AS NVARCHAR(10)) + ','''+@ValueText+ ''','''+ @ValueComment + '''' PRINT 'PRINT @RC' SET @i = @i +1 END --about 12 sec for 5000 Values Values = 400 / second Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Value',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Value','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_Value @Username NVARCHAR(255), @ProductID NVARCHAR(255), @ProductLineID NVARCHAR(255), @FactoryID NVARCHAR(255), @ValueSeriesID NVARCHAR(255), @TimeID INT, @ValueFormula NVARCHAR(MAX), @ValueInt NVARCHAR(255), -- to be able to send <#NV> @ValueText NVARCHAR(MAX), @ValueComment NVARCHAR(MAX) AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @ValueSeriesKey INT = 0; DECLARE @ValueKey BIGINT = 0; DECLARE @OLDValueFormula NVARCHAR(255) = N''; DECLARE @OLDValueInt BIGINT = 0; DECLARE @OLDValueText NVARCHAR(255) = N''; DECLARE @OLDValueComment NVARCHAR(MAX) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ValueSeriesID, N'NULL') + N''',' + ISNULL(CAST(@TimeID AS NVARCHAR(255)), N'NULL') + N',''' + ISNULL(@ValueFormula, N'NULL') + N''',''' + ISNULL(@ValueInt, N'NULL') + N''',''' + ISNULL(@ValueText, N'NULL') + N''',''' + ISNULL(@ValueComment, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ValueSeriesID IS NULL SET @ValueSeriesID = N''; IF @TimeID IS NULL SET @TimeID = 0; IF @ValueFormula IS NULL SET @ValueFormula = N''; IF @ValueInt IS NULL SET @ValueInt = N''; IF @ValueText IS NULL SET @ValueText = N''; IF @ValueComment IS NULL SET @ValueComment = N''; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_Value; -- STEP 0.2 - Protect input parameters SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductID = system.fProtectID (@ProductID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ValueSeriesID = system.fProtectID (@ValueSeriesID); SET @TimeID = system.fProtectID (@TimeID); SET @ValueFormula = system.fProtectString (@ValueFormula); SET @ValueInt = system.fProtectString (@ValueInt); SET @ValueText = system.fProtectString (@ValueText); SET @ValueComment = system.fProtectString (@ValueComment); -- Remove Linebreaks SET @ValueFormula = REPLACE(REPLACE(@ValueFormula ,CHAR(13),''),CHAR(10),''); SET @ValueText = REPLACE(REPLACE(@ValueText ,CHAR(13),''),CHAR(10),''); SET @ValueComment = REPLACE(REPLACE(@ValueComment ,CHAR(13),''),CHAR(10),''); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' OR @ValueSeriesID = N'' OR @TimeID = 0 BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID; IF @FactoryKey = 0 OR @ProductLineKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 1.3 - Try to detect and cancel the ValueSeriesKey if does not exist SELECT @ValueSeriesKey = ValueSeriesKey FROM planning.tdValueSeries WHERE ProductKey= @ProductKey AND ValueSeriesID = @ValueSeriesID; IF @ValueSeriesKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2.1 - Cancel if TimeID not exists IF NOT EXISTS(SELECT TimeID FROM planning.tdTime WHERE ProductKey = @ProductKey AND TimeID = @TimeID) BEGIN SET @ResultCode = 404; RAISERROR('TimeID not exists', 16, 10); END; -- STEP 3. Determine ValueKey identify and old values SELECT @ValueKey = ValueKey, @OLDValueFormula = ValueFormula, @OLDValueInt = ValueInt, @OLDValueText = ValueText, @OLDValueComment = ValueComment FROM planning.tfValues WHERE ValueSeriesKey = @ValueSeriesKey AND TimeID = @TimeID; -- STEP 3.1 - If Value Key exists delete IF @ValueKey > 0 BEGIN DELETE FROM planning.tfValues WHERE ValueKey = @ValueKey; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 204; -- Ok, but deleted SET @ValueFormula = IIF(@ValueFormula = N'<#NV>', @OLDValueFormula, @ValueFormula); SET @ValueText = IIF(@ValueText = N'<#NV>', @OLDValueText, @ValueText); SET @ValueComment = IIF(@ValueComment = N'<#NV>', @OLDValueComment, @ValueComment); SET @ValueInt = IIF(@ValueInt = N'<#NV>', @OLDValueInt, @ValueInt); END ELSE BEGIN SET @ValueFormula = IIF(@ValueFormula = N'<#NV>', N'', @ValueFormula); SET @ValueText = IIF(@ValueText = N'<#NV>', N'', @ValueText); SET @ValueComment = IIF(@ValueComment = N'<#NV>', N'', @ValueComment); SET @ValueInt = IIF(@ValueInt = N'<#NV>', N'0', @ValueInt); END; -- STEP 3.2 - Insert, if Value not empty outside *_0 ValueSeries IF @ValueFormula = N'' AND @ValueText = N'' AND @ValueInt = N'0' AND RIGHT(@ValueSeriesID,2)<>'_0' BEGIN SET @ResultCode = 204; --> discarded !!! END ELSE BEGIN SET @ValueInt = COALESCE(TRY_CAST(@ValueInt AS BIGINT), 0); INSERT INTO planning.tfValues ( ValueSeriesKey ,ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueText ,ValueComment ) VALUES ( @ValueSeriesKey, @ProductKey, @ProductLineKey, @FactoryKey, @ProductID, @ProductLineID, @FactoryID, @ValueSeriesID, @TimeID, @ValueFormula, @ValueInt, @ValueText, @ValueComment); SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = 201; END; COMMIT TRANSACTION sx_pf_POST_Value; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_Value; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Value' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for Value The Value will be deleted and writen new'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueSeriesID'; SET @value = N'ValueSeriesID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TimeID'; SET @value = N'TimeID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueFormula'; SET @value = N'ValueFormula'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueInt'; SET @value = N'ValueInt'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueText'; SET @value = N'ValueText'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueComment'; SET @value = N'ValueComment'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_ValueSerie; GO /* POST Operation for one ValueSeries Operation will create a ValueSeries if it not exist It will change an existing ValueSeries Works under the assumtion, that the ValueSeries ID is not changed ! The ID is used to identify the valueSeries Saxess Software GmbH Last modified: 01/2024 for OCT 5.10 Test call DECLARE @RC INT ,@Username NVARCHAR(255) ='SQL' ,@ProductID NVARCHAR(255) ='1' ,@ProductLineID NVARCHAR(255) ='U' ,@FactoryID NVARCHAR(255) ='ZT' ,@ValueSeriesID NVARCHAR(255) ='38550' ,@RequestedValueSeriesNo INT = 249 ,@NameShort NVARCHAR(255) ='<#NV>' ,@NameLong NVARCHAR(255) ='<#NV>' ,@CommentUser NVARCHAR(255) ='<#NV>' ,@CommentDev NVARCHAR(255) ='<#NV>' ,@ImageName NVARCHAR(255) ='<#NV>' ,@IsNumeric NVARCHAR(255) ='1' ,@VisibilityLevel NVARCHAR(255) ='<#NV>' ,@ValueSource NVARCHAR(255) ='<#NV>' ,@ValueListID NVARCHAR(255) ='<#NV>' ,@ValueFormatID NVARCHAR(255) ='<#NV>' ,@Unit NVARCHAR(255) ='<#NV>' ,@Scale NVARCHAR(255) ='100' ,@Effect NVARCHAR(255) ='<#NV>' ,@EffectParameter NVARCHAR(255) ='<#NV>' ,@AllowZero INT = 0 EXECUTE @RC = planning.spPOST_ValueSerie @Username, @ProductID, @ProductLineID, @FactoryID, @ValueSeriesID, @RequestedValueSeriesNo, @NameShort, @NameLong, @CommentUser, @CommentDev, @ImageName, @IsNumeric, @VisibilityLevel, @ValueSource, @ValueListID, @ValueFormatID, @Unit, @Scale, @Effect, @EffectParameter, @AllowZero PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ValueSerie',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_ValueSerie','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_ValueSerie @Username NVARCHAR(255), @ProductID NVARCHAR(255), @ProductLineID NVARCHAR(255), @FactoryID NVARCHAR(255), @ValueSeriesID NVARCHAR(255), @RequestedValueSeriesNo INT, @NameShort NVARCHAR(255), @NameLong NVARCHAR(255), @CommentUser NVARCHAR(255), @CommentDev NVARCHAR(255), @ImageName NVARCHAR(255), @IsNumeric NVARCHAR(255), @VisibilityLevel NVARCHAR(255), @ValueSource NVARCHAR(255), @ValueListID NVARCHAR(255), @ValueFormatID NVARCHAR(255), @Unit NVARCHAR(255), @Scale NVARCHAR(255), @Effect NVARCHAR(255), @EffectParameter NVARCHAR(255), @AllowZero INT = 0 AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @FactoryKey INT = 0; DECLARE @ProductLineKey INT = 0; DECLARE @ProductKey INT = 0; DECLARE @ValueSeriesKey INT = 0; DECLARE @OldValueSeriesNo INT = 0; DECLARE @OldIsNumeric INT = 0; DECLARE @OldScale INT = 0; DECLARE @OldValueSource NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ValueSeriesID, N'NULL') + N''',' + ISNULL(CAST(@RequestedValueSeriesNo AS NVARCHAR(255)), N'NULL') + N',''' + ISNULL(@NameShort, N'NULL') + N''',''' + ISNULL(@NameLong, N'NULL') + N''',''' + ISNULL(@CommentUser, N'NULL') + N''',''' + ISNULL(@CommentDev, N'NULL') + N''',''' + ISNULL(@ImageName, N'NULL') + N''',''' + ISNULL(@IsNumeric, N'NULL') + N''',''' + ISNULL(@VisibilityLevel, N'NULL') + N''',''' + ISNULL(@ValueSource, N'NULL') + N''',''' + ISNULL(@ValueListID, N'NULL') + N''',''' + ISNULL(@ValueFormatID, N'NULL') + N''',''' + ISNULL(@Unit, N'NULL') + N''',''' + ISNULL(@Scale, N'NULL') + N''',''' + ISNULL(@Effect, N'NULL') + N''',''' + ISNULL(@EffectParameter, N'NULL') + N''',' + ISNULL(CAST(@AllowZero AS NVARCHAR(255)), N'NULL') ; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ProductID IS NULL SET @ProductID = N'NULL'; IF @ProductLineID IS NULL SET @ProductLineID = N'NULL'; IF @FactoryID IS NULL SET @FactoryID = N'NULL'; IF @ValueSeriesID IS NULL SET @ValueSeriesID = N'NULL'; IF @RequestedValueSeriesNo IS NULL SET @RequestedValueSeriesNo = 0; IF @NameShort IS NULL SET @NameShort = N'NULL'; IF @NameLong IS NULL SET @NameLong = N'NULL'; IF @CommentUser IS NULL SET @CommentUser = N'NULL'; IF @CommentDev IS NULL SET @CommentDev = N'NULL'; IF @ImageName IS NULL SET @ImageName = N'NULL'; IF @IsNumeric IS NULL SET @IsNumeric = N'NULL'; IF @VisibilityLevel IS NULL SET @VisibilityLevel = N'NULL'; IF @ValueSource IS NULL SET @ValueSource = N'NULL'; IF @ValueListID IS NULL SET @ValueListID = N'NULL'; IF @ValueFormatID IS NULL SET @ValueFormatID = N'NULL'; IF @Unit IS NULL SET @Unit = N'NULL'; IF @Scale IS NULL SET @Scale = N'NULL'; IF @Effect IS NULL SET @Effect = N'NULL'; IF @EffectParameter IS NULL SET @EffectParameter = N'NULL'; IF @AllowZero IS NULL SET @AllowZero = N'NULL'; BEGIN TRY BEGIN TRANSACTION sx_pf_POST_ValueSerie; -- STEP 0.1 - NULL Protection SET @ProductID = system.fProtectID (@ProductID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @FactoryID = system.fProtectID (@FactoryID); SET @ValueSeriesID = system.fProtectID (@ValueSeriesID); SET @NameShort = system.fProtectString (@NameShort); SET @NameLong = system.fProtectString (@NameLong); SET @CommentUser = system.fProtectString (@CommentUser); SET @CommentDev = system.fProtectString (@CommentDev); SET @ImageName = system.fProtectString (@ImageName); SET @IsNumeric = system.fProtectBoolean (@IsNumeric); SET @VisibilityLevel = system.fProtectString (@VisibilityLevel); SET @ValueSource = system.fProtectString (@ValueSource); SET @Unit = system.fProtectString (@Unit); SET @Scale = system.fProtectInt (@Scale); SET @Effect = system.fProtectString (@Effect); SET @EffectParameter = system.fProtectString (@EffectParameter); SET @Allowzero = system.fProtectInt (@AllowZero); IF @ValueListID <> N'<#NV>' SET @ValueListID = system.fProtectID (@ValueListID); IF @ValueFormatID <> N'<#NV>' SET @ValueFormatID = system.fProtectID (@ValueFormatID); IF @FactoryID = N'' OR @ProductLineID = N'' OR @ProductID = N'' OR @ValueSeriesID = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Determine keys SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLinekey AND ProductID= @ProductID; SELECT @ValueSeriesKey = ValueSeriesKey ,@OldIsNumeric = IsNumeric ,@OldScale = Scale ,@OldValueSource = ValueSource FROM planning.tdValueSeries WHERE ProductKey = @ProductKey AND ValueSeriesID = @ValueSeriesID; IF @FactoryKey = 0 OR @ProductLineKey = 0 OR @ProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- if ValueSeriesID ends with '_0' then zeros are always allowed (old logic) IF RIGHT(@ValueSeriesID, 2) = '_0' SET @AllowZero = 1; -- STEP 3.1 - Insert the ValueSeries, if Key does not exist IF @ValueSeriesKey = 0 BEGIN -- INCREMENT all following Requested ValueSeriesNumbers based on requested No UPDATE planning.tdValueSeries SET ValueSeriesNo = ValueSeriesNo + 1 WHERE ProductKey = @ProductKey AND ValueSeriesNo >= @RequestedValueSeriesNo; IF @VisibilityLevel = '<#NV>' SET @VisibilityLevel = 1; SET @EffectedRows = @@ROWCOUNT; DECLARE @Keys AS TABLE ([Key] INT NOT NULL); -- Insert with requested number INSERT INTO planning.tdValueSeries ( ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,ValueSeriesNo ,NameShort ,NameLong ,CommentUser ,CommentDev ,ImageName ,IsNumeric ,VisibilityLevel ,ValueSource ,ValueListID ,ValueFormatID ,Unit ,Scale ,Effect ,EffectParameter ,AllowZero) OUTPUT INSERTED.ValueSeriesKey INTO @Keys([Key]) VALUES ( @ProductKey ,@ProductLineKey ,@FactoryKey ,@ProductID ,@ProductLineID ,@FactoryID ,@ValueSeriesID ,@RequestedValueSeriesNo ,@NameShort,@NameLong ,@CommentUser ,@CommentDev ,@ImageName ,@IsNumeric ,@VisibilityLevel ,@ValueSource ,@ValueListID ,@ValueFormatID ,@Unit ,@Scale ,@Effect ,@EffectParameter ,@AllowZero); SET @EffectedRows += @@ROWCOUNT; SELECT TOP (1) @ValueSeriesKey = [Key] FROM @Keys ORDER BY [Key]; END ELSE BEGIN -- STEP 3.2 - Update ValueSeries if it already exists SELECT @OldValueSeriesNo = ValueSeriesNo FROM planning.tdValueSeries WHERE ValueSeriesKey = @ValueSeriesKey; -- Use the old one, if no SeriesNo is commited (lower than one means not commited) IF @RequestedValueSeriesNo < 1 SET @RequestedValueSeriesNo = @OldValueSeriesNo; -- Adjust Numbers if not equal IF @OldValueSeriesNo < @RequestedValueSeriesNo BEGIN -- decrement all between actual and requsted Position UPDATE planning.tdValueSeries SET ValueSeriesNo = ValueSeriesNo - 1 WHERE ProductKey = @ProductKey AND ValueSeriesNo > @OldValueSeriesNo AND ValueSeriesNo <= @RequestedValueSeriesNo; SET @EffectedRows += @@ROWCOUNT; END ELSE IF @OldValueSeriesNo > @RequestedValueSeriesNo BEGIN -- increment all between actual and requsted Position UPDATE planning.tdValueSeries SET ValueSeriesNo = ValueSeriesNo + 1 WHERE ProductKey = @ProductKey AND ValueSeriesNo < @OldValueSeriesNo AND ValueSeriesNo >= @RequestedValueSeriesNo; SET @EffectedRows += @@ROWCOUNT; END; UPDATE planning.tdValueSeries SET ValueSeriesNo = @RequestedValueSeriesNo , NameShort = IIF(@NameShort = N'<#NV>', NameShort, @NameShort) , NameLong = IIF(@NameLong = N'<#NV>', NameLong, @NameLong) , CommentUser = IIF(@CommentUser = N'<#NV>', CommentUser, @CommentUser) , CommentDev = IIF(@CommentDev = N'<#NV>', CommentDev, @CommentDev) , ImageName = IIF(@ImageName = N'<#NV>', ImageName, @ImageName) , IsNumeric = IIF(@IsNumeric = N'<#NV>', IsNumeric, @IsNumeric) , VisibilityLevel = IIF(@VisibilityLevel = N'<#NV>', VisibilityLevel, @VisibilityLevel) , ValueSource = IIF(@ValueSource = N'<#NV>', ValueSource, @ValueSource) , ValueListID = IIF(@ValueListID = N'<#NV>', ValueListID, @ValueListID) , ValueFormatID = IIF(@ValueFormatID = N'<#NV>', ValueFormatID, @ValueFormatID) , Unit = IIF(@Unit = N'<#NV>', Unit, @Unit) , Scale = IIF(@Scale = N'<#NV>', Scale, @Scale) , Effect = IIF(@Effect = N'<#NV>', Effect, @Effect) , EffectParameter = IIF(@EffectParameter = N'<#NV>', EffectParameter, @EffectParameter) , AllowZero = @AllowZero FROM planning.tdValueSeries WHERE ValueSeriesKey = @ValueSeriesKey; SET @EffectedRows += @@ROWCOUNT; -- STEP 3.2a - Delete zero values if ValueSeries is now numeric input series IF @IsNumeric = '1' AND @ValueSource = N'Input' BEGIN DELETE FROM planning.tfValues WHERE ValueSeriesKey = @ValueSeriesKey AND ValueInt = 0; END; -- Step 3.2b - Delete empty Strings if ValueSeries is now non-numeric input series IF @IsNumeric = '0' AND @ValueSource = N'Input' BEGIN DELETE FROM planning.tfValues WHERE ValueSeriesKey = @ValueSeriesKey AND ValueText = N''; END; -- STEP 3.3 - Update exists Values due to possible rescaling or changing type from Numeric to Non-Numeric DECLARE @vals AS TABLE (ValueKey INT NOT NULL, ValueInt INT NOT NULL, ValueText NVARCHAR(MAX) NOT NULL); -- Changing type Numeric / Non Numeric IF (@IsNumeric <> N'<#NV>' AND @OldIsNumeric <> @IsNumeric) BEGIN -- Copy ValueText to ValueInt, after Try_Cast to Int to get only valid numbers and multiplied by OldScale, as the int values use scale, while the text value not IF @IsNumeric = N'1' BEGIN INSERT INTO @vals (ValueKey, ValueInt, ValueText) SELECT ValueKey, ISNULL(TRY_CAST(ValueText AS INT), 0) * @OldScale, '' FROM planning.tfValues WHERE ValueSeriesKey = @ValueSeriesKey; END ELSE BEGIN -- If Numeric is made to non numeric the value in the Database must be be divided by scale and converted to text, as text dont uses scale IF (@OldScale <> 0) BEGIN INSERT INTO @vals (ValueKey, ValueInt, ValueText) SELECT ValueKey, 0, CAST(ValueInt / @OldScale AS NVARCHAR(MAX)) FROM planning.tfValues WHERE ValueSeriesKey = @ValueSeriesKey; END ELSE BEGIN INSERT INTO @vals (ValueKey, ValueInt, ValueText) SELECT ValueKey, 0, CAST(ValueInt AS NVARCHAR(MAX)) FROM planning.tfValues WHERE ValueSeriesKey = @ValueSeriesKey; END; END; END; -- changing scaling -- If Isnumeric = 0 and not changed Nothing happens with the value, if scale is changed, as text values dont use the scale property. IF (@Scale <> N'<#NV>' AND @OldScale <> @Scale) AND (@IsNumeric = N'1' OR (@IsNumeric = N'<#NV>' AND @OldIsNumeric = 1)) BEGIN -- Change Scale of existing values if scale is changed (when scale is changed, e.g. from 100 to 1000 the value in the database must be multiplied by 10) DECLARE @k FLOAT = @Scale / CAST(@OldScale AS FLOAT); IF NOT EXISTS(SELECT 1 FROM @vals) BEGIN INSERT INTO @vals (ValueKey, ValueInt, ValueText) SELECT ValueKey, ValueInt * @k, '' FROM planning.tfValues WHERE ValueSeriesKey = @ValueSeriesKey; END ELSE BEGIN UPDATE @vals SET ValueInt = ValueInt * @k; END; END; IF EXISTS(SELECT 1 FROM @vals) BEGIN UPDATE v SET v.ValueInt = newV.ValueInt ,v.ValueText = newV.ValueText FROM planning.tfValues v INNER JOIN @vals newV ON v.ValueKey = newV.ValueKey WHERE v.ValueSeriesKey = @ValueSeriesKey; SET @EffectedRows += @@ROWCOUNT; END; END; SET @Resultcode = 201; COMMIT TRANSACTION sx_pf_POST_ValueSerie; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION sx_pf_POST_ValueSerie; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_ValueSerie' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'POST Operation for one ValueSeries Operation will create a ValueSeries if it not exist It will change an existing ValueSeries Works under the assumtion, that the ValueSeries ID is not changed ! The ID is used to identify the valueSeries'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueSeriesID'; SET @value = N'ValueSeriesID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@RequestedValueSeriesNo'; SET @value = N'RequestedValueSeriesNo'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@NameShort'; SET @value = N'NameShort'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@NameLong'; SET @value = N'NameLong'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentUser'; SET @value = N'CommentUser'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CommentDev'; SET @value = N'CommentDev'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ImageName'; SET @value = N'ImageName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@IsNumeric'; SET @value = N'IsNumeric'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@VisibilityLevel'; SET @value = N'VisibilityLevel'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueSource'; SET @value = N'ValueSource'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueListID'; SET @value = N'ValueListID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueFormatID'; SET @value = N'ValueFormatID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Unit'; SET @value = N'Unit'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Scale'; SET @value = N'Scale'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Effect'; SET @value = N'Effect'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@EffectParameter'; SET @value = N'EffectParameter'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spCOPY_Tab; GO /* Procedure to copy a Tab inside a object or to another target Saxess Software GmbH Last modified: 09/2023 for OCT 5.10 Testcall Procedure DECLARE @RC INT; EXEC @RC = planning.spCOPY_Tab @Username = 'W10\admin' ,@SourceFactoryID = 'ZT' ,@SourceProductLineID = '' ,@SourceProductID = '' ,@SourceTabID = 'SF' ,@TargetFactoryID = 'ZT' ,@TargetProductLineID = '' ,@TargetProductID = '' ,@TargetTabID = 'SF2' ,@TargetOrderIndex = 2 ,@TargetTabName = 'Test2'; PRINT @RC; SELECT * FROM planning.tTabs ORDER BY FactoryID, ProductlineID, OrderIndex; TRUNCATE TABLE planning.tTabs; SELECT TOP 5 * FROM planning.tAPI_Log; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spCOPY_Tab',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spCOPY_Tab','PARAMETER',NULL) */ CREATE PROCEDURE planning.spCOPY_Tab @Username NVARCHAR(255) ,@SourceFactoryID NVARCHAR(255) = '' ,@SourceProductLineID NVARCHAR(255) = '' ,@SourceProductID NVARCHAR(255) = '' ,@SourceTabID NVARCHAR(255) -- not optional, needed in all levels ,@TargetFactoryID NVARCHAR(255) = '' ,@TargetProductLineID NVARCHAR(255) = '' ,@TargetProductID NVARCHAR(255) = '' ,@TargetTabID NVARCHAR(255) -- not optional, needed in all levels ,@TargetOrderIndex INT = 1 ,@TargetTabName NVARCHAR(255) AS BEGIN DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@SourceFactoryID ,N'NULL') + N''',''' + ISNULL(@SourceProductLineID ,N'NULL') + N''',''' + ISNULL(@SourceProductID ,N'NULL') + N''',''' + ISNULL(@SourceTabID ,N'NULL') + N''',''' + ISNULL(@TargetFactoryID ,N'NULL') + N''',''' + ISNULL(@TargetProductLineID ,N'NULL') + N''',''' + ISNULL(@TargetProductID ,N'NULL') + N''',''' + ISNULL(@TargetTabID ,N'NULL') + N''',' + CAST( ISNULL(@TargetOrderIndex ,N'NULL') AS NVARCHAR(255)) + N',''' + ISNULL(@TargetTabName ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N''; -- Procedure specific declarations DECLARE @SourceFactoryKey INT = 0 ,@SourceProductLineKey INT = 0 ,@SourceProductKey INT = 0 ,@TargetFactoryKey INT = 0 ,@TargetProductLineKey INT = 0 ,@TargetProductKey INT = 0 ,@RowKey BIGINT = 0; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @SourceFactoryID IS NULL SET @SourceFactoryID = N''; IF @SourceProductLineID IS NULL SET @SourceProductLineID = N''; IF @SourceProductID IS NULL SET @SourceProductID = N''; IF @SourceTabID IS NULL SET @SourceTabID = N''; IF @TargetFactoryID IS NULL SET @TargetFactoryID = N''; IF @TargetProductLineID IS NULL SET @TargetProductLineID = N''; IF @TargetProductID IS NULL SET @TargetProductID = N''; IF @TargetTabID IS NULL SET @TargetTabID = N''; IF @TargetOrderIndex IS NULL SET @TargetOrderIndex = 1; IF @TargetTabName IS NULL SET @TargetTabName = N''; -- Content Protection for specific input parameters (e.g.IDs) SET @SourceFactoryID = system.fProtectID (@SourceFactoryID); SET @SourceProductLineID = system.fProtectID (@SourceProductLineID); SET @SourceProductID = system.fProtectID (@SourceProductID); SET @TargetFactoryID = system.fProtectID (@TargetFactoryID); SET @TargetProductLineID = system.fProtectID (@TargetProductLineID); SET @TargetProductID = system.fProtectID (@TargetProductID); -- START TRANSACTION *********************************************************************************** BEGIN TRY BEGIN TRANSACTION spCOPY_Tab; -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- determine Keys for requested IDs - empty IDs keep Key -1 IF @SourceFactoryID = '' SET @SourceFactoryKey = -1; IF @SourceProductLineID = '' SET @SourceProductLineKey = -1; IF @SourceProductID = '' SET @SourceProductKey = -1; IF @TargetFactoryID = '' SET @TargetFactoryKey = -1; IF @TargetProductLineID = '' SET @TargetProductLineKey = -1; IF @TargetProductID = '' SET @TargetProductKey = -1; SELECT @SourceFactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @SourceFactoryID AND @SourceFactoryKey <> -1; SELECT @SourceProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @SourceFactoryKey AND ProductLineID = @SourceProductLineID AND @SourceProductLineKey <> -1; SELECT @SourceProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @SourceProductLineKey AND ProductID = @SourceProductID AND @SourceProductKey <> -1; SELECT @TargetFactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @TargetFactoryID AND @TargetFactoryKey <> -1; SELECT @TargetProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @TargetFactoryKey AND ProductLineID = @TargetProductLineID AND @TargetProductLineKey <> -1; SELECT @TargetProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @TargetProductLineKey AND ProductID = @TargetProductID AND @TargetProductKey <> -1; -- error if an ID dont exists IF @SourceFactoryKey = 0 OR @SourceProductLineKey = 0 OR @SourceProductKey = 0 OR @TargetFactoryKey = 0 OR @TargetProductLineKey = 0 OR @TargetProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Not existing FID / PID / PLID - Keys don`t exists', 16, 10); END; -- check if SourceTabID exists IF NOT EXISTS ( SELECT RowKey FROM planning.tTabs WHERE FactoryKey = @SourceFactoryKey AND ProductLineKey = @SourceProductLineKey AND ProductKey = @SourceProductKey AND TabID = @SourceTabID ) BEGIN SET @ResultCode = 404; RAISERROR('Source TabID not found.', 16, 10); END; -- tab with ID 0 cant be moved IF @SourceTabID= '0' OR @TargetTabID = '0' BEGIN SET @ResultCode = 403; RAISERROR('Tab with ID 0 can''t be copied', 16, 10); END -- error if ID have gaps in the line IF @SourceFactoryKey = -1 AND (@SourceProductLineKey <> -1 OR @SourceProductKey <> -1) OR @SourceProductLineKey = -1 AND @SourceProductKey <> -1 OR @TargetFactoryKey = -1 AND (@TargetProductLineKey <> -1 OR @TargetProductKey <> -1) OR @TargetProductLineKey = -1 AND @TargetProductKey <> -1 BEGIN SET @ResultCode = 404; RAISERROR('ID combination can have only gaps at the end - Keys don`t exists', 16, 10); END; -- check user rights for cluster level on Source IF @SourceFactoryKey = -1 BEGIN EXEC @ResultCode = planning.spGET_ClusterPropertiesReadRight @TransactUsername; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for Cluster read.', 16, 10); END END -- check user rights for Factory level on Source IF @SourceFactoryKey <> -1 AND @SourceProductLineKey = -1 BEGIN EXEC @ResultCode = planning.spGET_FactoryReadRight @TransactUsername,@SourceFactoryID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for Factory read.', 16, 10); END END -- check user rights for ProductLine / Product level on Source IF @SourceFactoryKey <> -1 AND @SourceProductLineKey <> -1 BEGIN EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername,@SourceFactoryID,@SourceProductLineID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for ProductLine / Product read.', 16, 10); END END -- check user rights for cluster level on Target IF @TargetFactoryKey = -1 BEGIN EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for Cluster write.', 16, 10); END END -- check user rights for Factory level on Target IF @TargetFactoryKey <> -1 AND @TargetProductLineKey = -1 BEGIN EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername,@TargetFactoryID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for Factory write.', 16, 10); END END -- check user rights for ProductLine / Product level on Target IF @TargetFactoryKey <> -1 AND @TargetProductLineKey <> -1 BEGIN EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername,@TargetFactoryID,@TargetProductLineID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for ProductLine / Product write.', 16, 10); END END -- determine if posted Tab already exists SELECT @Rowkey = RowKey FROM planning.tTabs WHERE FactoryKey = @TargetFactoryKey AND ProductLineKey = @TargetProductLineKey AND ProductKey = @TargetProductKey AND TabID = @TargetTabID -- data operation starts *********************************************************************************** -- delete if exits, to determine Resultcode IF @RowKey > 0 BEGIN BEGIN SET @ResultCode = 401 RAISERROR('Target TabID already exists in Target.', 16, 10); END END -- copy tab layouts INSERT INTO planning.tTabLayouts ( FactoryID ,ProductLineID ,TabID ,LayoutID ,LayoutName ,LayoutJSON ,OrderIndex ,LayoutOwner ) SELECT @TargetFactoryID , @TargetProductLineID , @TargetTabID , LayoutID , LayoutName , LayoutJSON , OrderIndex , LayoutOwner FROM planning.tTabLayouts WHERE FactoryID = @SourceFactoryID AND ProductLineID = @SourceProductLineID AND TabID = @SourceTabID; -- Insert new Tab INSERT INTO planning.tTabs ( FactoryKey ,ProductLineKey ,ProductKey ,FactoryID ,ProductLineID ,ProductID ,TabID ,Orderindex ,TabName ,TabHint ,TabText ,PresentationCODE ,Datasource ,IsVisibleBOOL ,ParameterJSON ,SQLCommand ) SELECT @TargetFactoryKey ,@TargetProductLineKey ,@TargetProductKey ,@TargetFactoryID ,@TargetProductLineID ,@TargetProductID ,@TargetTabID ,@TargetOrderIndex -- TODO: Must be set to right index ,@TargetTabName ,TabHint ,TabText ,PresentationCODE ,Datasource ,IsVisibleBOOL ,ParameterJSON ,SQLCommand FROM planning.tTabs WHERE FactoryKey = @SourceFactoryKey AND ProductLineKey = @SourceProductLineKey AND ProductKey = @SourceProductKey AND TabID = @SourceTabID -- Increment all Tabs after the inserted Tab and the Tab with the same OrderIndex UPDATE planning.tTabs SET Orderindex = Orderindex + 1 WHERE FactoryKey = @TargetFactoryKey AND ProductLineKey = @TargetProductLineKey AND ProductKey = @TargetProductKey AND Orderindex >= @TargetOrderIndex AND TabID <> @TargetTabID; -- eliminate all Gaps in Tab OrderIndex UPDATE planning.tTabs SET Orderindex = Rownumbers.Number FROM planning.tTabs tT INNER JOIN ( SELECT ROW_NUMBER() OVER (ORDER BY Orderindex) AS Number ,TabID FROM planning.tTabs WHERE FactoryKey = @TargetFactoryKey AND ProductLineKey = @TargetProductLineKey AND ProductKey = @TargetProductKey ) AS Rownumbers ON tT.TabID = Rownumbers.TabID AND tT.FactoryKey = @TargetFactoryKey AND tT.ProductLineKey = @TargetProductLineKey AND tT.ProductKey = @TargetProductKey; SET @ResultCode = 201; COMMIT TRANSACTION spCOPY_Tab; END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spCOPY_Tab; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spCOPY_Tab' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = not shiped from saxess -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- 10 = shiped from saxess as customer specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag',@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to Copy a Tab - inside an object or to a different. The copied tab is always puted at the end.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Name of the user this action is requested for.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceFactoryID'; SET @value = N'FactoryID, skip / NULL / empty string if POST is for Cluster level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceProductLineID'; SET @value = N'SourceProductLineID, skip / NULL / empty string if POST is for Cluster or Factory level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceProductID'; SET @value = N'SourceProductD, skip / NULL / empty string if POST is for Cluster, Factory or ProductLine level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceTabID'; SET @value = N'SourceBusiness Identifier for the Tab.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetFactoryID'; SET @value = N'FactoryID, skip / NULL / empty string if POST is for Cluster level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetProductLineID'; SET @value = N'TargetProductLineID, skip / NULL / empty string if POST is for Cluster or Factory level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetProductID'; SET @value = N'TargetProductD, skip / NULL / empty string if POST is for Cluster, Factory or ProductLine level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetTabID'; SET @value = N'TargetBusiness Identifier for the Tab.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_File; GO /* Procedure to delete a single file or all files in a path Gerd Tautenhahn for Saxess Software GmbH Last modified: 12/2022 for OCT 5.9 Testcall EXEC planning.spDELETE_File 'SQL' ,'' ,'' ,'' ,'' --FileID ,'Rootfolder' --Filepath ; SELECT * FROM planning.tFiles; SELECT * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_File',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_File','PARAMETER',NULL) */ CREATE PROCEDURE planning.spDELETE_File @Username NVARCHAR(255) = N'' ,@FactoryID NVARCHAR(50) = N'' ,@ProductLineID NVARCHAR(50) = N'' ,@ProductID NVARCHAR(50) = N'' ,@FileID NVARCHAR(50) = N'' ,@Filepath NVARCHAR(50) = N'' AS BEGIN SET NOCOUNT ON DECLARE @TransactionUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@FactoryID ,N'NULL') + N''',''' + ISNULL(@ProductLineID ,N'NULL') + N''',''' + ISNULL(@ProductID ,N'NULL') + N''',''' + ISNULL(@FileID ,N'NULL') + N''',''' + ISNULL(@Filepath ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; BEGIN TRY BEGIN TRANSACTION DELETE_File; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); -- STEP 1.1 - Determine transaction user SELECT @TransactionUsername = system.fDetermineTransactionUsername (@Username); IF @TransactionUsername = N'403' BEGIN SET @ResultCode = 403; THROW 60000, N'Transaction user don`t exists', 100; END; IF @Filepath = N'' AND @FileID = N'' BEGIN SET @ResultCode = 403; THROW 60000, N'Filepath or FileID must be specified', 100; END; -- check if file exists in case of single file delete IF @FileID <> N'' AND NOT EXISTS ( SELECT TOP (1) 1 FROM planning.tFiles WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND ProductID = @ProductID AND FileID = @FileID ) BEGIN SET @ResultCode = 404; THROW 60000, N'File not found', 100; END; DELETE FROM planning.tFiles WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND ProductID = @ProductID AND (FileID = @FileID OR @FileID = '') AND (Filepath LIKE @Filepath + N'%' OR @Filepath = ''); SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; COMMIT TRANSACTION DELETE_File; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION DELETE_File; IF @Error_state = 100 BEGIN PRINT @Comment; END ELSE IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactionUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_File' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to delete exists file'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FileID'; SET @value = N'optional (not needed if Filepath is set) - FileID to specify the the file to be deleted'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Filepath'; SET @value = N'optional - Path or Pathsegment to delete all file in a path'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_Tab; GO /* Procedure to delete a Tab Saxess Software GmbH Last modified: 09/2023 for OCT 5.10 Testcall Procedure DECLARE @RC INT; EXEC @RC = planning.spDELETE_Tab @Username = 'W10\admin' ,@FactoryID = '' ,@ProductLineID = '' ,@ProductID = '' ,@TabID = 'T7' PRINT @RC SELECT * FROM planning.tTabs ORDER BY OrderIndex TRUNCATE TABLE planning.tTabs Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_Tab',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_Tab','PARAMETER',NULL) */ CREATE PROCEDURE planning.spDELETE_Tab ( @Username NVARCHAR(255) ,@FactoryID NVARCHAR(255) = '' ,@ProductLineID NVARCHAR(255) = '' ,@ProductID NVARCHAR(255) = '' ,@TabID NVARCHAR(255) ) AS BEGIN SET NOCOUNT ON; -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@FactoryID ,N'NULL') + N''',''' + ISNULL(@ProductLineID ,N'NULL') + N''',''' + ISNULL(@ProductID ,N'NULL') + N''',''' + ISNULL(@TabID ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' -- Procedure specific declarations DECLARE @FactoryKey INT = 0 ,@ProductLineKey INT = 0 ,@ProductKey INT = 0 ,@RowKey BIGINT = 0; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @TabID IS NULL SET @TabID = N''; -- Content Protection for specific input parameters (e.g.IDs) SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); -- START TRANSACTION *********************************************************************************** BEGIN TRY BEGIN TRANSACTION spDELETE_Tab; IF @TabID = '0' BEGIN SET @ResultCode = 403; RAISERROR('Tab with ID 0 can''t be deleted.', 16, 10); END; -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- determine Keys for requested IDs - empty IDs keep Key -1 IF @FactoryID = '' SET @FactoryKey = -1; IF @ProductLineID = '' SET @ProductLineKey = -1; IF @ProductID = '' SET @ProductKey = -1; SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID AND @FactoryKey <> -1; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID AND @ProductLineKey <> -1; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID AND @ProductKey <> -1; --PRINT @FactoryKey --PRINT @ProductLineKey --PRINT @ProductKey -- error if an ID dont exists IF @FactoryKey = 0 OR @ProductLineKey = 0 OR @ProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Not existing FID / PID / PLID - Keys don`t exists', 16, 10); END; -- check if TabID exists IF NOT EXISTS ( SELECT RowKey -- its not possible to assign this rowkey to @Rowkey FROM planning.tTabs WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey AND TabID = @TabID ) BEGIN SET @ResultCode = 404; RAISERROR('TabID not found.', 16, 10); END; -- RowKey ermitteln SELECT @RowKey=RowKey FROM planning.tTabs WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey AND TabID = @TabID -- error if ID have gaps in the line IF @FactoryKey = -1 AND (@ProductLineKey <> -1 OR @ProductKey <> -1) OR @ProductLineKey = -1 AND @ProductKey <> -1 BEGIN SET @ResultCode = 404; RAISERROR('ID combination can have only gaps at the end - Keys don`t exists', 16, 10); END; -- check user rights for cluster level IF @FactoryKey = -1 BEGIN EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for Cluster write.', 16, 10); END END -- check user rights for Factory level IF @FactoryKey <> -1 AND @ProductLineKey = -1 BEGIN EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername,@FactoryID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for Factory write.', 16, 10); END END -- check user rights for ProductLine / Product level IF @FactoryKey <> -1 AND @ProductLineKey <> -1 BEGIN EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername,@FactoryID,@ProductLineID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for ProductLine / Product write.', 16, 10); END END -- data operation starts *********************************************************************************** -- delete tab layouts DELETE FROM planning.tTabLayouts WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND TabID = @TabID; -- Insert new Tab DELETE FROM planning.tTabs WHERE RowKey = @RowKey; -- eliminate all Gaps UPDATE planning.tTabs SET Orderindex = Rownumbers.Number FROM planning.tTabs tT INNER JOIN ( SELECT ROW_NUMBER() OVER (ORDER BY Orderindex) AS Number ,TabID FROM planning.tTabs WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey ) AS Rownumbers ON tT.TabID = Rownumbers.TabID AND tT.FactoryKey = @FactoryKey AND tT.ProductLineKey = @ProductLineKey AND tT.ProductKey = @ProductKey; SET @ResultCode = 200; COMMIT TRANSACTION spDELETE_Tab; END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spDELETE_Tab; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_Tab' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = not shiped from saxess -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- 10 = shiped from saxess as customer specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag',@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to delete a Tab.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Name of the user this action is requested for.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'optional FactoryID, skip / NULL / empty string if DELETE is for Cluster level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'optional ProductLineID, skip / NULL / empty string if DELETE is for Cluster or Factory level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'optional ProductD, skip / NULL / empty string if DELETE is for Cluster, Factory or ProductLine level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TabID'; SET @value = N'TabID which is deleted.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spDELETE_TabLayout; GO /* Procedure to POST a layout for a tab Saxess Software GmbH Last modified: 09/2023 for OCT 5.10 Testcall DECLARE @RC INT; EXEC @RC = planning.spDELETE_TabLayout @Username = 'SQL' ,@FactoryID = '' ,@ProductLineID = '' ,@TabID = 'Test' ,@LayoutID = '1'; SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_TabLayout',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spDELETE_TabLayout','PARAMETER',NULL) */ CREATE PROCEDURE planning.spDELETE_TabLayout @Username NVARCHAR(255) , @FactoryID NVARCHAR(255) = '' , @ProductLineID NVARCHAR(255) = '' , @TabID NVARCHAR(255) , @LayoutID NVARCHAR(255) AS BEGIN BEGIN TRY BEGIN TRANSACTION planning_spDELETE_TabLayout; -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 5, @Username, @FactoryID, @ProductLineID, @TabID, @LayoutID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Parameter protection SET @Username = COALESCE(@Username, N''); SET @FactoryID = system.fProtectID(COALESCE(@FactoryID, N'')); SET @ProductLineID = system.fProtectID(COALESCE(@ProductLineID, N'')); SET @TabID = system.fProtectID(COALESCE(@TabID, N'')); SET @LayoutID = system.fProtectID(COALESCE(@LayoutID, N'')); -- Variables DECLARE @SettingUserEditPublicLayouts BIT; DECLARE @IsPublicLayout BIT; DECLARE @IsUserOwnedLayout BIT; DECLARE @IsUserAdministrator BIT; -- Get setting: regular users can edit public layouts? SELECT @SettingUserEditPublicLayouts = COALESCE(ValueInt, 0) FROM system.fGET_Setting('TabPreferencesUserEditPublicLayouts'); -- Public layout? SELECT @IsPublicLayout = IIF(LayoutOwner = '', 1, 0), @IsUserOwnedLayout = IIF(LayoutOwner = @Username, 1, 0) FROM planning.tTabLayouts WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND TabID = @TabID AND LayoutID = @LayoutID; -- Layout exists? IF @IsPublicLayout IS NULL BEGIN RAISERROR('Layout doesn''t exist.', 16, 10); END -- Is user administrator? SELECT @IsUserAdministrator = IsAdministratorFlag FROM system.trUser WHERE UserName = @Username; -- Right rules: -- Administrators are allowed to delete all layouts -- Regular users can delete their own personal layouts -- Regular users can not delete other personal layouts -- Regular users can delete public layouts if the setting "UserEditPublicLayouts" = 1 but they need write rights on the location of the tab -- Check if regular user is allowed to delete public layout or trying to delete layout from other user IF @IsUserAdministrator = 0 AND ((@SettingUserEditPublicLayouts = 0 AND @IsPublicLayout = 1) OR (@IsUserOwnedLayout = 0 AND @IsPublicLayout = 0)) BEGIN RAISERROR('Insufficient rights to delete this layout.', 16, 10); END -- Regular users can edit public layouts but need to have write rights IF @IsUserAdministrator = 0 AND @SettingUserEditPublicLayouts = 1 AND @IsPublicLayout = 1 BEGIN -- Cluster level IF @FactoryID = '' BEGIN EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Insufficient rights to delete this layout.', 16, 10); END ELSE BEGIN -- Factory level IF @ProductLineID = '' BEGIN EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername, @FactoryID; IF @ResultCode <> 200 RAISERROR('Insufficient rights to delete this layout.', 16, 10); END ELSE BEGIN -- ProductLine level EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Insufficient rights to delete this layout.', 16, 10); END END END -- Delete layout DELETE FROM planning.tTabLayouts WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND TabID = @TabID AND LayoutID = @LayoutID; SET @AffectedRows = @@ROWCOUNT; -- Rearrange Orderindex WITH Layouts AS ( SELECT * , ROW_NUMBER() OVER (PARTITION BY LayoutOwner ORDER BY OrderIndex) AS NewOrderIndex FROM planning.tTabLayouts WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND TabID = @TabID ) UPDATE Layouts SET OrderIndex = NewOrderIndex; COMMIT TRANSACTION planning_spDELETE_TabLayout; SET @ResultCode = 200 END TRY BEGIN CATCH ROLLBACK TRANSACTION planning_spDELETE_TabLayout; SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_TabLayout' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure for POST Bulkload'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'optional FactoryID, skip / NULL / empty string if request is for Cluster level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'optional ProductLineID, skip / NULL / empty string if request is for Cluster or Factory level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TabID'; SET @value = N'TabID which is requested'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spEXPORT_Format; GO /* Procedure to create an OCT Importscript for an format Gerd Tautenhahn for Saxess Software GmbH Last modified: 01/2023 for OCT 5.9 Testcall Procedure DECLARE @RC INT; EXEC @RC = planning.spEXPORT_Format @Username = 'SQL' ,@FormatID = 'EingabeDatum'; PRINT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spEXPORT_Format',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spEXPORT_Format','PARAMETER',NULL) */ CREATE PROCEDURE planning.spEXPORT_Format @Username NVARCHAR(255) ,@FormatID NVARCHAR(50) WITH EXECUTE AS 'dbo' -- as the Procedure must access the system schema AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) -- SET PARAMETER LOG STRING -inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',' + ISNULL(@FormatID ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime(); -- Procedure specific declarations DECLARE @SourceClusterName NVARCHAR(255) ,@SourceClusterAPI NVARCHAR(255); -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @FormatID IS NULL SET @FormatID = N''; -- START TRY *********************************************************************************** BEGIN TRY -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Create a temporary table to store the collection of export commands DROP TABLE IF EXISTS #tExport; CREATE TABLE #tExport ( RowKey BIGINT IDENTITY (1,1) ,MainOrderNumber INT NOT NULL ,SubOrderNumber INT NOT NULL ,Command NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,PRIMARY KEY CLUSTERED (RowKey) ); -- Determine Metadata SELECT @SourceClusterName = ValueText FROM system.tSettings WHERE SettingID = 'Clustername'; -- May be empty if not defined SET @SourceClusterName = CONCAT(DB_Name(),' ',@SourceClusterName); SELECT @SourceClusterAPI = ValueText FROM system.tSettings WHERE SettingID = 'DBVersion'; -- MetaHeader INSERT INTO #tExport VALUES (0, 1, N'-- {') ,(0, 2, N'-- "Type": "Format",') ,(0, 3, CONCAT(N'-- "SourceClusterName": "', @SourceClusterName,'",')) ,(0, 4, CONCAT(N'-- "SourceClusterAPI": "',@SourceClusterAPI,'",')) ,(0, 6, CONCAT(N'-- "SourceFormatID": "',@FormatID,'"')) ,(0, 7, N'-- }'); INSERT INTO #tExport VALUES (10, 1, N'--CONFIG: Adjust variable values manually to fit your needs !') ,(10, 2, N'DECLARE @Username NVARCHAR(255) = ''SQL''') ,(10, 3, N'DECLARE @FormatID NVARCHAR(255) = ''' + @FormatID + N'''') ,(10, 4, N'--This Format will be deleted during import, if it exists. You should be sure !'); -- Try_Delete existing Format INSERT INTO #tExport SELECT 900 AS MainOrderNumber ,1000 AS SubOrderNumber ,CONCAT( 'EXEC planning.spDELETE_Format ' ,'''SQL''' ,',@FormatID' ,';' ) AS Command FROM planning.thFormats WHERE FormatID = @FormatID; -- Create the Format as new Format INSERT INTO #tExport SELECT 1000 AS MainOrderNumber ,1000 AS SubOrderNumber ,CONCAT( 'EXEC planning.spPOST_Format ' ,'''SQL''' ,',@FormatID' ,',''' ,BackgroundColor ,'''' ,',''' ,FontColor ,'''' ,',''' ,ValueFormat ,'''' ,',''' ,Bold ,'''' ,',''' ,Italic ,'''' ,',''' ,AlignmentHorizontal ,'''' ,',''' ,AlignmentVertical ,'''' ,',''' ,TextUnderlined ,'''' ,',''' ,BorderToNext ,'''' ,',''' ,BorderToPrevious ,'''' ,';' ) AS Command FROM planning.thFormats WHERE FormatID = @FormatID; -- final GO INSERT INTO #tExport VALUES (9999, 1, N'GO'); -- Data Transaction PRINT CONCAT(N'Output of the import script',N' (started after ', DateDiff(millisecond, @StartTime, SysUTCDateTime()),N'ms)'); SELECT Command FROM #tExport ORDER BY MainOrderNumber ,SubOrderNumber SET @ResultCode = 200; PRINT CONCAT(N'Transaktion abgeschlossen',N' (after ', DateDiff(millisecond, @StartTime, SysUTCDateTime()),N'ms)') END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spEXPORT_Format' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N''; -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to create an SQL Script, which imports a Format in OCT.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation SET @level2name = N'@FormatID'; SET @value = N'ID of the Format to be exported.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spEXPORT_List; GO /* Procedure to create an OCT Importscript for a list with its values Gerd Tautenhahn for Saxess Software GmbH Last modified: 01/2023 for OCT 5.9 Testcall Procedure DECLARE @RC INT; EXEC @RC = planning.spEXPORT_List @Username = 'SQL' ,@ListID = 'suKontenfunktion'; PRINT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spEXPORT_List',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spEXPORT_List','PARAMETER',NULL) */ CREATE PROCEDURE planning.spEXPORT_List @Username NVARCHAR(255) ,@ListID NVARCHAR(50) WITH EXECUTE AS 'dbo' -- as the Procedure must access the system schema AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) -- SET PARAMETER LOG STRING -inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',' + ISNULL(@ListID ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime(); -- Procedure specific declarations DECLARE @SourceClusterName NVARCHAR(255) ,@SourceClusterAPI NVARCHAR(255); -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @ListID IS NULL SET @ListID = N''; -- START TRY *********************************************************************************** BEGIN TRY -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Create a temporary table to store the collection of export commands DROP TABLE IF EXISTS #tExport; CREATE TABLE #tExport ( RowKey BIGINT IDENTITY (1,1) ,MainOrderNumber INT NOT NULL ,SubOrderNumber INT NOT NULL ,Command NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,PRIMARY KEY CLUSTERED (RowKey) ); -- Determine Metadata SELECT @SourceClusterName = ValueText FROM system.tSettings WHERE SettingID = 'Clustername'; -- May be empty if not defined SET @SourceClusterName = CONCAT(DB_Name(),' ',@SourceClusterName); SELECT @SourceClusterAPI = ValueText FROM system.tSettings WHERE SettingID = 'DBVersion'; -- MetaHeader INSERT INTO #tExport VALUES (0, 1, N'-- {') ,(0, 2, N'-- "Type": "List",') ,(0, 3, CONCAT(N'-- "SourceClusterName": "', @SourceClusterName,'",')) ,(0, 4, CONCAT(N'-- "SourceClusterAPI": "',@SourceClusterAPI,'",')) ,(0, 6, CONCAT(N'-- "SourceListID": "',@ListID,'"')) ,(0, 7, N'-- }'); INSERT INTO #tExport VALUES (10, 1, N'--CONFIG: Adjust variable values manually to fit your needs !') ,(10, 2, N'DECLARE @Username NVARCHAR(255) = ''SQL''') ,(10, 3, N'DECLARE @ListID NVARCHAR(255) = ''' + @ListID + N'''') ,(10, 4, N'--This List will be deleted during import, if it exists. You should be sure !'); -- Try_Delete existing Pipeline INSERT INTO #tExport SELECT 900 AS MainOrderNumber ,1000 AS SubOrderNumber ,CONCAT( 'EXEC planning.spDELETE_List ' ,'''SQL''' ,',@ListID' ,';' ) AS Command FROM planning.thLists WHERE ListID = @ListID; -- Create the List as new List INSERT INTO #tExport SELECT 1000 AS MainOrderNumber ,1000 AS SubOrderNumber ,CONCAT( 'EXEC planning.spPOST_List ' ,'''SQL''' ,',@ListID' ,',''' ,system.fMaskSQL(NameShort) ,'''' ,',''' ,system.fMaskSQL(NameLong) ,'''' ,',''' ,system.fMaskSQL(CommentDev) ,'''' ,',''' ,system.fMaskSQL(CommentUser) ,'''' ,',''' ,system.fMaskSQL(Datentyp) ,'''' ,',''' ,system.fMaskSQL(Source) ,'''' ,',''' ,system.fMaskSQL(SourceFormula) ,'''' ,',''' ,FormatID ,'''' ,';' ) AS Command FROM planning.thLists WHERE ListID = @ListID; -- List Values INSERT INTO #tExport SELECT 1000 AS MainOrderNumber ,2000 AS SubOrderNumber ,CONCAT( 'EXEC planning.spPOST_ListValue ' ,'''SQL''' ,',' ,0 ,'' ,',@ListID' ,',' ,ValueInt ,'' ,',' ,Scale ,'' ,',''' ,system.fMaskSQL(ValueText) ,'''' ,',''' ,system.fMaskSQL(ValueComment) ,'''' ,',''' ,FormatID ,'''' ,';' ) AS Command FROM planning.thListValues WHERE ListID = @ListID; -- final GO INSERT INTO #tExport VALUES (9999, 1, N'GO'); -- Data Transaction PRINT CONCAT(N'Output of the import script',N' (started after ', DateDiff(millisecond, @StartTime, SysUTCDateTime()),N'ms)'); SELECT Command FROM #tExport ORDER BY MainOrderNumber ,SubOrderNumber SET @ResultCode = 200; PRINT CONCAT(N'Transaktion abgeschlossen',N' (after ', DateDiff(millisecond, @StartTime, SysUTCDateTime()),N'ms)') END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spEXPORT_List' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N''; -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to create an SQL Script, which imports a List in OCT.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@ListID'; SET @value = N'ID of the List to be exported.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spEXPORT_Tab; GO /* Procedure to create an OCT Importscript for an selected Tab Saxess Software GmbH Last modified: 09/2023 for OCT 5.10 Testcall Procedure DECLARE @RC INT; EXEC @RC = planning.spEXPORT_Tab @Username = 'SQL' ,@FactoryID = 'ZT' ,@ProductLineID = '' ,@ProductID = '' ,@TabID = 'SF' PRINT @RC Testing in SSMS is only limited possible - Tabs which contains images result in very large strings which can handled only in bcp -> see planning.spExport_Setting for more informations Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spEXPORT_Tab',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spEXPORT_Tab','PARAMETER',NULL) */ CREATE PROCEDURE planning.spEXPORT_Tab @Username NVARCHAR(255) ,@FactoryID NVARCHAR(50) ,@ProductLineID NVARCHAR(50) ,@ProductID NVARCHAR(50) ,@TabID NVARCHAR(50) WITH EXECUTE AS 'dbo' -- as the Procedure must access the system schem AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) -- SET PARAMETER LOG STRING -inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',' + ISNULL(@FactoryID ,N'NULL') + N''',' + ISNULL(@ProductLineID ,N'NULL') + N''',' + ISNULL(@ProductID ,N'NULL') + N''',' + ISNULL(@TabID ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime(); -- Procedure specific declarations DECLARE @SourceClusterName NVARCHAR(255) ,@SourceClusterAPI NVARCHAR(255); -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @TabID IS NULL SET @TabID = N''; -- START TRY *********************************************************************************** BEGIN TRY -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Create a temporary table to store the collection of export commands DROP TABLE IF EXISTS #tExport; CREATE TABLE #tExport ( RowKey BIGINT IDENTITY (1,1) ,MainOrderNumber INT NOT NULL ,SubOrderNumber INT NOT NULL ,Command NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,PRIMARY KEY CLUSTERED (RowKey) ); -- Determine Metadata SELECT @SourceClusterName = ValueText FROM system.tSettings WHERE SettingID = 'Clustername'; -- May be empty if not defined SET @SourceClusterName = CONCAT(DB_Name(),' ',@SourceClusterName); SELECT @SourceClusterAPI = ValueText FROM system.tSettings WHERE SettingID = 'DBVersion'; -- MetaHeader INSERT INTO #tExport VALUES (0, 1, N'-- {') ,(0, 2, N'-- "Type": "Tab",') ,(0, 3, CONCAT(N'-- "SourceClusterName": "', @SourceClusterName,'",')) ,(0, 4, CONCAT(N'-- "SourceClusterAPI": "',@SourceClusterAPI,'",')) ,(0, 5, CONCAT(N'-- "SourceFactoryID": "',@FactoryID,'",')) ,(0, 5, CONCAT(N'-- "SourceProductLineID": "',@ProductLineID,'",')) ,(0, 5, CONCAT(N'-- "SourceProductID": "',@ProductID,'",')) ,(0, 6, CONCAT(N'-- "SourceTabID": "',@TabID,'"')) ,(0, 7, N'-- }') INSERT INTO #tExport VALUES (10, 1, N'--CONFIG: Adjust variable values manually to fit your needs !') ,(10, 2, N'DECLARE @Username NVARCHAR(255) = ''SQL''') ,(10, 3, N'DECLARE @FactoryID NVARCHAR(255) = ''' + @FactoryID + N'''') ,(10, 3, N'DECLARE @ProductLineID NVARCHAR(255) = ''' + @ProductLineID + N'''') ,(10, 3, N'DECLARE @ProductID NVARCHAR(255) = ''' + @ProductID + N'''') ,(10, 3, N'DECLARE @TabID NVARCHAR(255) = ''' + @TabID + N'''') ,(10, 4, N'--This Tab will be deleted during import, if it exists. You should be sure !'); -- Try_Delete existing Tab INSERT INTO #tExport SELECT 900 AS MainOrderNumber ,1000 AS SubOrderNumber ,CONCAT( 'EXEC planning.spDELETE_Tab ' ,'''SQL''' ,',@FactoryID' ,',@ProductLineID' ,',@ProductID' ,',@TabID' ,';' ) AS Command FROM planning.tTabs WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND ProductID = @ProductID AND TabID = @TabID; -- Create the Tab as new Tab INSERT INTO #tExport SELECT 1000 AS MainOrderNumber ,1000 AS SubOrderNumber ,CONCAT( 'EXEC planning.spPOST_Tab ' ,'''SQL''' ,',@FactoryID' ,',@ProductLineID' ,',@ProductID' ,',@TabID' ,',' ,Orderindex ,'' ,',''' ,system.fMaskSQL(TabName) ,'''' ,',''' ,system.fMaskSQL(TabHint) ,'''' ,',''' ,system.fMaskSQL(TabText) ,'''' ,',''' ,PresentationCODE ,'''' ,',''' ,Datasource ,'''' ,',''' ,IsVisibleBOOL ,'''' ,',''''' -- LayoutJSON compatibility ,',''' ,system.fMaskSQL(ParameterJSON) ,'''' ,',''' ,system.fMaskSQL(SQLCommand) ,'''' ) AS Command FROM planning.tTabs WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND ProductID = @ProductID AND TabID = @TabID; -- Create tab layouts INSERT INTO #tExport SELECT 1100 AS MainOrderNumber ,1000 AS SubOrderNumber ,CONCAT( 'EXEC planning.spPOST_TabLayout ' , '''SQL''' , ',@FactoryID' , ',@ProductLineID' , ',@TabID' , ',''', LayoutID, '''' , ',''', system.fMaskSQL(LayoutName), '''' , ',''', system.fMaskSQL(LayoutJSON), '''' , ',''', OrderIndex, '''' , ',''', LayoutOwner, '''' ) AS Command FROM planning.tTabLayouts WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND TabID = @TabID; -- final GO INSERT INTO #tExport VALUES (9999, 1, N'GO'); -- Data Transaction PRINT CONCAT(N'Output of the import script',N' (started after ', DateDiff(millisecond, @StartTime, SysUTCDateTime()),N'ms)'); SELECT Command FROM #tExport ORDER BY MainOrderNumber ,SubOrderNumber SET @ResultCode = 200; PRINT CONCAT(N'Transaktion abgeschlossen',N' (after ', DateDiff(millisecond, @StartTime, SysUTCDateTime()),N'ms)') END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spEXPORT_Tab' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to create an SQL Script, which imports a Tab in OCT.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@TabID'; SET @value = N'ID of the Tab to be exported.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_EnduserObjects; GO /* Procedure to deliver SQL Object suggestions to the enduser. Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall Procedure DECLARE @RC INT; EXEC @RC = planning.spGET_EnduserObjects @Username = 'SQL' ,@ObjectType = 'P' ,@ShowAllBool = 1; PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_EnduserObjects',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_EnduserObjects','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_EnduserObjects ( @Username NVARCHAR(255) ,@ObjectType NVARCHAR(255) = 'P' ,@ShowAllBool INT = 0 ) AS BEGIN SET NOCOUNT ON; -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' --set Strings inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@ObjectType ,N'NULL') + N''',' + ISNULL(CAST(@ShowAllBool AS NVARCHAR(255)),N'NULL') + N'' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N''; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @ObjectType IS NULL SET @ObjectType = N''; IF @ShowAllBool IS NULL SET @ShowAllBool = 0; -- START TRANSACTION *********************************************************************************** BEGIN TRY BEGIN TRANSACTION spGET_EnduserObjects; -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Data Transaction ;WITH prms AS ( SELECT DISTINCT object_id FROM ( SELECT p1.object_id, p2.object_id other_id FROM [sys].[all_parameters] p1 LEFT JOIN [sys].[all_parameters] p2 ON p1.[object_id] = p2.[object_id] AND p2.[name] NOT IN ('@Username', '@FactoryID', '@ProductLineID', '@ProductID', '@ParameterJSON') WHERE p1.[name] = '@Username' ) all_p WHERE other_id is null ) SELECT CASE O.type WHEN 'P' THEN 'Stored Procedures' WHEN 'U' THEN 'Tables' WHEN 'V' THEN 'Views' WHEN 'IF' THEN 'Functions (Table Value Inline)' WHEN 'FN' THEN 'Functions (Scalar)' WHEN 'TF' THEN 'Functions (Table Value)' WHEN 'TR' THEN 'Trigger' ELSE 'Unknown Objecttype' END AS ObjectTypeName ,O.type AS ObjectType ,S.name +'.' + O.name AS ObjectName ,CASE WHEN sxo.SX_UserHint IS NOT NULL AND sxo.SX_UserHint <> '' THEN sxo.SX_UserHint WHEN EP.value = '' THEN 'Description not defined' WHEN EP.value IS NULL THEN '' WHEN EP.value <> '' THEN EP.value ELSE '' END AS Description FROM sys.all_objects O LEFT JOIN sys.extended_properties EP ON EP.major_id = O.object_id LEFT JOIN sys.schemas S ON O.schema_id = S.schema_id -- use as separte column LEFT JOIN ( SELECT major_id ,Value AS SX_UserHint FROM sys.extended_properties WHERE Name = 'SX_UserHint' ) sxo ON sxo.major_id = EP.major_id LEFT JOIN prms ON O.object_id = prms.object_id WHERE O.is_ms_shipped = 0 AND ISNULL(EP.minor_id,0) = 0 -- columns of tables and views, parameters filtered out AND O.type NOT IN ('PK' -- PrimaryKeys ,'D' -- default constraints ) AND (EP.name = 'MS_Description' OR EP.name IS NULL) AND (O.type = @ObjectType OR @ObjectType = '') AND (sxo.SX_UserHint IS NOT NULL AND sxo.SX_UserHint <> '' OR @ShowAllBool = 1) AND (prms.object_id IS NOT NULL OR O.type <> 'P') SET @ResultCode = 200; COMMIT TRANSACTION spGET_EnduserObjects; END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spGET_EnduserObjects; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_EnduserObjects' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure delivers List of all Objects, which are useful for enduser analytics.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@ObjectType'; SET @value = N'Allows to filter output to defined ObjectType. Default Value is P for stored procedure. Keep empty for all.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ShowAllBool'; SET @value = N'0 = show only objects with SX_UserHint set in Metadata, 1 = show all objects.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_File; GO /* Procedure to Return body and file name of existing file or error if file not exist or user have not rights to read Gerd Tautenhahn for Saxess Software GmbH Last modified: 05/2023 for OCT 5.9 Testcall EXEC planning.spGET_File 'SQL' ,'1' ,'U' ,'1000' ,'f09e645a-3624-4497-9606-3d20f0be52f6'; SELECT * FROM planning.tFiles; SELECT * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_File',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_File','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_File @Username NVARCHAR(255) = N'' ,@FactoryID NVARCHAR(50) = N'' ,@ProductLineID NVARCHAR(50) = N'' ,@ProductID NVARCHAR(50) = N'' ,@FileID NVARCHAR(50) = N'' AS BEGIN SET NOCOUNT ON DECLARE @TransactionUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''', '+ ISNULL(@FactoryID, N'NULL')+ N''', '+ ISNULL(@ProductLineID, N'NULL') + N''', '+ ISNULL(@ProductID, N'NULL')+ N''', '+ ISNULL(@FileID, N'NULL') ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N''; -- Procedure specific declarations DECLARE @FactoryKey INT = 0 ,@ProductLineKey INT = 0 ,@ProductKey INT = 0; -- NULL Protection IF @Username IS NULL SET @Username = N''; IF @FileID IS NULL SET @FileID = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @FactoryID = N'' SET @FactoryKey = -1; IF @ProductLineID = N'' SET @ProductLineKey = -1; IF @ProductID = N'' SET @ProductKey = -1; BEGIN TRY -- Protect input parameters SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); IF @FileID IS NULL OR LEN(@FileID) = 0 BEGIN SET @ResultCode = 403; THROW 60000, N'FileID is null or empty', 100; END; -- Determine transaction user SELECT @TransactionUsername = system.fDetermineTransactionUsername (@Username); IF @TransactionUsername = N'403' BEGIN SET @ResultCode = 403; THROW 60000, N'Transaction user don`t exists', 100; END; -- error if Keys chains has gaps IF (@FactoryKey = -1 AND (@ProductLineKey <> -1 OR @ProductKey <> -1)) OR (@ProductLineKey = -1 AND @ProductKey <> -1) BEGIN SET @ResultCode = 404; RAISERROR('Keys combination can have only gaps at the end', 16, 10); END; IF @FactoryKey <> -1 BEGIN SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; IF @ProductLineKey <> -1 BEGIN SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; IF @ProductKey <> -1 BEGIN SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductID = @ProductID; END END END -- check user rights for cluster level IF @FactoryKey = -1 BEGIN -- user already have read right to cluster SET @ResultCode = 200; END -- check user rights for Factory level ELSE IF @ProductLineKey = -1 BEGIN EXEC @ResultCode = planning.spGET_FactoryReadRight @TransactionUsername, @FactoryID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for Factory.', 16, 10); END END -- check user rights for ProductLine / Product level ELSE IF @ProductLineKey > 0 BEGIN EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactionUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for ProductLine.', 16, 10); END END -- error if and Keys dont exists IF @FactoryKey = 0 OR @ProductLineKey = 0 OR @ProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Not existing FID / PID / PLID - Keys don`t exists', 16, 10); END; DECLARE @FileKey BIGINT = NULL; SELECT TOP (1) @FileKey = FileKey FROM planning.tFiles WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey AND FileID = @FileID; IF @FileKey IS NULL BEGIN SET @ResultCode = 404; THROW 60000, N'File not found', 100; END; SELECT FactoryID ,ProductLineID ,ProductID ,FileID ,[FileName] ,ISNULL([FileName] + N'.' + FileExtension, [FileName]) AS 'FileNameWithExtension' ,FileExtension ,FileBody ,Filepath FROM planning.tFiles WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey AND FileID = @FileID; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state = 100 BEGIN PRINT @Comment; END ELSE IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactionUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_File' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to Return body and file name of existing file'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FileID'; SET @value = N'FileID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_FileList; GO /* Procedure to Return list of existing files on a product -Right Check missing -Key Determination missing Gerd Tautenhahn for Saxess Software GmbH Last modified: 06/2023 for OCT 5.9 Testcall EXEC planning.spGET_FileList 'SQL' ,'' ,'' ,'' ,'Rootfolder/Subfolder2'; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_FileList',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_FileList','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_FileList @Username NVARCHAR(255) = N'' ,@FactoryID NVARCHAR(50) = N'' ,@ProductLineID NVARCHAR(50) = N'' ,@ProductID NVARCHAR(50) = N'' ,@Filepath NVARCHAR(4000) = N'' -- this may be the full filepath or the start of it AS BEGIN SET NOCOUNT ON DECLARE @TransactionUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username , N'NULL') + N''', ''' + ISNULL(@FactoryID , N'NULL') + N''', ''' + ISNULL(@ProductLineID , N'NULL') + N''', ''' + ISNULL(@ProductID , N'NULL') + N''', ''' + ISNULL(@Filepath , N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N''; IF @Username IS NULL SET @Username = N''; BEGIN TRY SET @Username = system.fProtectString (@Username); SELECT @TransactionUsername = system.fDetermineTransactionUsername (@Username); IF @TransactionUsername = N'403' BEGIN SET @ResultCode = 403; THROW 60000, N'Transaction user don`t exists', 100; END; SELECT FactoryID ,ProductLineID ,ProductID ,FileID ,[FileName] ,[FileName] + ISNULL(N'.' + FileExtension, N'') AS 'FileNameWithExtension' ,FileExtension ,UploadedBy ,UploadedAt ,Filepath FROM planning.tFiles WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND ProductID = @ProductID AND ( Filepath LIKE @Filepath + N'%' -- to cover starting with this path OR @Filepath = N'' ) SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state = 100 BEGIN PRINT @Comment; END ELSE IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactionUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_FileList' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N''; -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to Return list of existing files on a product'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Filepath'; SET @value = N'This may be the full filepath or the start of it for a like search.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_Index_JSON; GO /* Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Procedure to GET the Structure of Planning Objects FactoryID and ProductLineID can be passed in as single Value or as empty string for all Output Limit may be F/PL/P/VS Testcall DECLARE @ParameterJSON NVARCHAR(MAX); SET @ParameterJSON = N'{ "connector_type":"read" ,"version":1 ,"FactoryID":"ZT" ,"ProductLineID":"U" ,"ProductID":"1" ,"OutputLimit" : "VS" } ' EXEC planning.spGET_Index_JSON 'SQL',@ParameterJSON Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Index_JSON',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Index_JSON','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_Index_JSON @Username NVARCHAR(255) = '' ,@ParameterJSON NVARCHAR(MAX) = '' AS BEGIN SET NOCOUNT ON; -- Logging Variables DECLARE @TransactionUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''', ''' + ISNULL(@ParameterJSON, N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N''; -- Procedure Variables DECLARE @StartTime TIME(4) ,@ProcessTime INT ,@FactoryID NVARCHAR(255) ,@ProductLineID NVARCHAR(255) ,@ProductID NVARCHAR(255) ,@OutputLimit NVARCHAR(255) = '' BEGIN TRY SET @StartTime = CONVERT (time, SYSUTCDATETIME()) ; -- Test for valid JSON ################################################################################ IF ISJSON(@ParameterJSON) = 0 BEGIN PRINT 'Exit due to invalid JSON' RETURN 403 END -- Extract Parameter FROM JSON ###################################################################### SET @FactoryID = JSON_Value(@ParameterJSON,'$.FactoryID') COLLATE DATABASE_DEFAULT; SET @ProductLineID = JSON_Value(@ParameterJSON,'$.ProductLineID') COLLATE DATABASE_DEFAULT; SET @ProductID = JSON_Value(@ParameterJSON,'$.ProductID') COLLATE DATABASE_DEFAULT; SET @OutputLimit = JSON_Value(@ParameterJSON,'$.OutputLimit') COLLATE DATABASE_DEFAULT; IF @OutputLimit = N'' OR @OutputLimit IS NULL SET @OutputLimit = N'VS' -- Check Right and Keys as valid ################################################################# SELECT @TransactionUsername = system.fDetermineTransactionUsername (@Username); IF @TransactionUsername = N'403' BEGIN SET @ResultCode = 403; THROW 60000, N'Transaction user don`t exists', 100; END; -- Check Rights and flag invalid rows -- Invalid IDs get 404 flag -- Output als Recordset ################################################################# IF @OutputLimit = N'VS' BEGIN SELECT dF.FactoryID ,dF.NameShort AS FactoryName ,dPL.ProductLineID ,dPL.NameShort AS ProductLineName ,dP.ProductID ,dP.NameShort AS ProductName ,dP.GlobalAttribute1 ,dP.GlobalAttribute2 ,dP.GlobalAttribute3 ,dP.GlobalAttribute4 ,dP.GlobalAttribute5 ,dP.GlobalAttribute6 ,dP.GlobalAttribute7 ,dP.GlobalAttribute8 ,dP.GlobalAttribute9 ,dP.GlobalAttribute10 ,dP.GlobalAttribute11 ,dP.GlobalAttribute12 ,dP.GlobalAttribute13 ,dP.GlobalAttribute14 ,dP.GlobalAttribute15 ,dP.GlobalAttribute16 ,dP.GlobalAttribute17 ,dP.GlobalAttribute18 ,dP.GlobalAttribute19 ,dP.GlobalAttribute20 ,dP.GlobalAttribute21 ,dP.GlobalAttribute22 ,dP.GlobalAttribute23 ,dP.GlobalAttribute24 ,dP.GlobalAttribute25 ,dVS.ValueSeriesID ,dVS.[IsNumeric] ,dVS.NameShort AS ValueSeriesName ,dVS.ValueSource ,dVS.ValueListID ,dVS.Scale ,dVS.Unit ,dVS.Effect ,dVS.VisibilityLevel ,dVS.ValueSeriesNo FROM planning.tdFactories dF LEFT JOIN planning.tdProductLines dPL ON dF.FactoryKey = dPL.FactoryKey LEFT JOIN planning.tdProducts dP ON dPL.ProductLineKey = dP.ProductLineKey LEFT JOIN planning.tdValueSeries dVS ON dP.ProductKey = dVS.ProductKey WHERE ( @FactoryID = N'' OR @FactoryID = dF.FactoryID ) AND ( @ProductLineID = N'' OR @ProductLineID = dPL.ProductLineID ) AND ( @ProductID = N'' OR @ProductID = dP.ProductID ) ORDER BY dF.FactoryID, dPL.ProductLineID, dP.ProductID,dVS.ValueSeriesID SET @EffectedRows = @@ROWCOUNT; END IF @OutputLimit = N'P' BEGIN SELECT dF.FactoryID ,dF.NameShort AS FactoryName ,dPL.ProductLineID ,dPL.NameShort AS ProductLineName ,dP.ProductID ,dP.NameShort AS ProductName ,dP.GlobalAttribute1 ,dP.GlobalAttribute2 ,dP.GlobalAttribute3 ,dP.GlobalAttribute4 ,dP.GlobalAttribute5 ,dP.GlobalAttribute6 ,dP.GlobalAttribute7 ,dP.GlobalAttribute8 ,dP.GlobalAttribute9 ,dP.GlobalAttribute10 ,dP.GlobalAttribute11 ,dP.GlobalAttribute12 ,dP.GlobalAttribute13 ,dP.GlobalAttribute14 ,dP.GlobalAttribute15 ,dP.GlobalAttribute16 ,dP.GlobalAttribute17 ,dP.GlobalAttribute18 ,dP.GlobalAttribute19 ,dP.GlobalAttribute20 ,dP.GlobalAttribute21 ,dP.GlobalAttribute22 ,dP.GlobalAttribute23 ,dP.GlobalAttribute24 ,dP.GlobalAttribute25 FROM planning.tdFactories dF LEFT JOIN planning.tdProductLines dPL ON dF.FactoryKey = dPL.FactoryKey LEFT JOIN planning.tdProducts dP ON dPL.ProductLineKey = dP.ProductLineKey WHERE ( @FactoryID = N'' OR @FactoryID = dF.FactoryID ) AND ( @ProductLineID = N'' OR @ProductLineID = dPL.ProductLineID ) AND ( @ProductID = N'' OR @ProductID = dP.ProductID ) ORDER BY dF.FactoryID, dPL.ProductLineID, dP.ProductID SET @EffectedRows = @@ROWCOUNT; END IF @OutputLimit = N'PL' BEGIN SELECT dF.FactoryID ,dF.NameShort AS FactoryName ,dPL.ProductLineID ,dPL.NameShort AS ProductLineName FROM planning.tdFactories dF LEFT JOIN planning.tdProductLines dPL ON dF.FactoryKey = dPL.FactoryKey WHERE ( @FactoryID = N'' OR @FactoryID = dF.FactoryID ) AND ( @ProductLineID = N'' OR @ProductLineID = dPL.ProductLineID ) ORDER BY dF.FactoryID, dPL.ProductLineID END IF @OutputLimit = N'F' BEGIN SELECT dF.FactoryID ,dF.NameShort AS FactoryName FROM planning.tdFactories dF WHERE ( @FactoryID = N'' OR @FactoryID = dF.FactoryID ) ORDER BY dF.FactoryID SET @EffectedRows = @@ROWCOUNT; END SET @ResultCode = 200; -- Runtime, at the moment without output SET @ProcessTime = DATEDIFF(ms,@StartTime,CONVERT (time, SYSUTCDATETIME())); END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactionUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN 200; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Index_JSON' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET the Structure of Planning Objects FactoryID and ProductLineID can be passed in as single Value or as empty string for all'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ParameterJSON'; SET @value = N'ParameterJSON'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_Products_JSON; GO /* Zweck - lesen der Globalattribute einer Liste von Products - die Liste der zu lesenden Products wird als JSON Array übergeben und umfasst X Products - die Prozedur gibt ein Recordset mit ebenfalls genau X Zeilen zurück - Pro Zeile wird ein Returncode zurückgegeben ob das Product gelesen werden konnte - Pro Zeile wird ein optionale Information mit zurückgegeben ToDo - lesende Abfrage schreiben, ähnlich planning.spGET_Values_JSON - prüfen, ob die angeforderten Products existieren - prüfen, ob der User Rechte auf den angeforderten Products hat - Products lesen - Products als Recordset zurückgeben Gerd Tautenhahn / Maria Rüdger for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall DECLARE @ProductsJSON NVARCHAR(MAX); DECLARE @RC INT; SET @ProductsJSON = N'{ "connector_type":"read" ,"version":1 ,"Values": { "Products": [ { "ProductNumber": 1 ,"FactoryID" : "ZT" ,"ProductLineID": "U" ,"ProductID" : "1" }, { "ProductNumber": 2 ,"FactoryID" : "ZT" ,"ProductLineID": "U" ,"ProductID" : "2" } ] } } '; EXEC @RC = planning.spGET_Products_JSON 'SQL', @ProductsJSON; PRINT @RC; Testcall 2 DECLARE @ProductsJSON NVARCHAR(MAX); DECLARE @RC INT; SET @ProductsJSON = N'{ "connector_type":"read" ,"version":1 ,"Values": { "Products": [ { "ProductNumber": 1 ,"FactoryID" : "ZT" ,"ProductLineID": "*" ,"ProductID":"*" } ] } } '; EXEC @RC = planning.spGET_Products_JSON 'SQL', @ProductsJSON; PRINT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Products_JSON',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Products_JSON','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_Products_JSON ( @Username NVARCHAR(255) ,@ProductsJSON NVARCHAR(MAX) = '' ) AS BEGIN -- Standard declaration for logging DECLARE @TransactionUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) -- SET PARAMETER LOG STRING -inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@ProductsJSON ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime(); -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @ProductsJSON IS NULL SET @ProductsJSON = N''; -- START TRY *********************************************************************************** BEGIN TRY -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); -- Test for valid JSON ################################################################################ IF @ProductsJSON = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; IF ISJSON(@ProductsJSON) = 0 BEGIN SET @ResultCode = 403; THROW 60000, N'Exit due to invalid JSON', 100; END; -- Extract Products FROM JSON ###################################################################### DROP TABLE IF EXISTS #RequestedValues; CREATE TABLE #RequestedValues ( ProductNumber INT NOT NULL ,FactoryID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ); INSERT INTO #RequestedValues SELECT DISTINCT ProductNumber ,FactoryID ,ProductLineID ,ProductID FROM OPENJSON (@ProductsJSON, N'$.Values.Products') WITH ( ProductNumber INT N'$.ProductNumber' ,FactoryID NVARCHAR(255) N'$.FactoryID' ,ProductLineID NVARCHAR(255) N'$.ProductLineID' ,ProductID NVARCHAR(255) N'$.ProductID' ); IF EXISTS( SELECT TOP(1) 1 FROM #RequestedValues WHERE ProductNumber IS NULL OR FactoryID IS NULL OR ProductLineID IS NULL OR ProductID IS NULL OR FactoryID = N'' OR ProductLineID = N'' OR ProductID = N'' ) OR EXISTS( SELECT TOP(1) 1 FROM #RequestedValues GROUP BY ProductNumber HAVING COUNT (ProductNumber) > 1 ) BEGIN SET @ResultCode = 403; THROW 60000, N'Exit due to invalid Request', 100; END; -- Determine transaction user ###################################################################### SELECT @TransactionUsername = system.fDetermineTransactionUsername (@Username); IF @TransactionUsername = N'403' BEGIN SET @ResultCode = 403; THROW 60000, N'Transaction user don`t exists', 100; END; -- Search ValueSeries and User`s rights ###################################################################### -- Query all requested Products -- only one row if a Product is multiple times requested -- classified for rights, but not filterd DROP TABLE IF EXISTS #Products; CREATE TABLE #Products ( FactoryID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,TimeType NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,NameShort NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,CommentUser NVARCHAR(4000) COLLATE DATABASE_DEFAULT NOT NULL ,ResponsiblePerson NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ImageName NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,[Status] NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,Template NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute1 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute2 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute3 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute4 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute5 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute6 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute7 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute8 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute9 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute10 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute11 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute12 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute13 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute14 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute15 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute16 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute17 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute18 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute19 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute20 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute21 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute22 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute23 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute24 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute25 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,[Right] NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ); INSERT INTO #Products SELECT dP.FactoryID ,dP.ProductLineID ,dP.ProductID ,dP.TimeType ,dP.NameShort ,dP.CommentUser ,dP.ResponsiblePerson ,dP.ImageName ,dP.[Status] ,dP.Template ,dP.GlobalAttribute1 ,dP.GlobalAttribute2 ,dP.GlobalAttribute3 ,dP.GlobalAttribute4 ,dP.GlobalAttribute5 ,dP.GlobalAttribute6 ,dP.GlobalAttribute7 ,dP.GlobalAttribute8 ,dP.GlobalAttribute9 ,dP.GlobalAttribute10 ,dP.GlobalAttribute11 ,dP.GlobalAttribute12 ,dP.GlobalAttribute13 ,dP.GlobalAttribute14 ,dP.GlobalAttribute15 ,dP.GlobalAttribute16 ,dP.GlobalAttribute17 ,dP.GlobalAttribute18 ,dP.GlobalAttribute19 ,dP.GlobalAttribute20 ,dP.GlobalAttribute21 ,dP.GlobalAttribute22 ,dP.GlobalAttribute23 ,dP.GlobalAttribute24 ,dP.GlobalAttribute25 ,UR.[Right] AS [Right] FROM planning.tdProducts dP -- Filter on resquested Values INNER JOIN #RequestedValues RV ON dP.FactoryID = RV.FactoryID AND dP.ProductLineID = RV.ProductLineID AND dP.ProductID = RV.ProductID -- right classification, but no filtering LEFT JOIN system.trUserRights UR ON dP.FactoryID = UR.FactoryID AND dP.ProductLineID = UR.ProductLineID AND UR.Username = @TransactionUsername; --PRINT 'Done 1'; -- Create Resultset ###################################################################### DROP TABLE IF EXISTS #ProductSet; CREATE TABLE #ProductSet ( ProductNumber INT NOT NULL ,FactoryID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,TimeType NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,NameShort NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,CommentUser NVARCHAR(4000) COLLATE DATABASE_DEFAULT NULL ,ResponsiblePerson NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,ImageName NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,[Status] NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,Template NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute1 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute2 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute3 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute4 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute5 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute6 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute7 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute8 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute9 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute10 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute11 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute12 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute13 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute14 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute15 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute16 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute17 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute18 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute19 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute20 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute21 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute22 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute23 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute24 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute25 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,ReturnMessage NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,ReturnCode INT NULL ,ProcessFlag INT NULL ); INSERT INTO #ProductSet -- Create Basic Rows SELECT ProductNumber ,FactoryID ,ProductLineID ,ProductID ,'' AS TimeType ,'' AS NameShort ,'' AS CommentUser ,'' AS ResponsiblePerson ,'' AS ImageName ,'' AS [Status] ,'' AS Template ,'' AS GlobalAttribute1 ,'' AS GlobalAttribute2 ,'' AS GlobalAttribute3 ,'' AS GlobalAttribute4 ,'' AS GlobalAttribute5 ,'' AS GlobalAttribute6 ,'' AS GlobalAttribute7 ,'' AS GlobalAttribute8 ,'' AS GlobalAttribute9 ,'' AS GlobalAttribute10 ,'' AS GlobalAttribute11 ,'' AS GlobalAttribute12 ,'' AS GlobalAttribute13 ,'' AS GlobalAttribute14 ,'' AS GlobalAttribute15 ,'' AS GlobalAttribute16 ,'' AS GlobalAttribute17 ,'' AS GlobalAttribute18 ,'' AS GlobalAttribute19 ,'' AS GlobalAttribute20 ,'' AS GlobalAttribute21 ,'' AS GlobalAttribute22 ,'' AS GlobalAttribute23 ,'' AS GlobalAttribute24 ,'' AS GlobalAttribute25 ,'' AS ReturnMessage ,500 AS ReturnCode ,'' AS ProcessFlag FROM #RequestedValues; -- Insert Informations for Products with Read Rights UPDATE PS SET PS.TimeType = P.TimeType ,PS.NameShort = P.NameShort ,PS.CommentUser = P.CommentUser ,PS.ResponsiblePerson = P.ResponsiblePerson ,PS.ImageName = P.ImageName ,PS.[Status] = P.[Status] ,PS.Template = P.Template ,PS.GlobalAttribute1 = P.GlobalAttribute1 ,PS.GlobalAttribute2 = P.GlobalAttribute2 ,PS.GlobalAttribute3 = P.GlobalAttribute3 ,PS.GlobalAttribute4 = P.GlobalAttribute4 ,PS.GlobalAttribute5 = P.GlobalAttribute5 ,PS.GlobalAttribute6 = P.GlobalAttribute6 ,PS.GlobalAttribute7 = P.GlobalAttribute7 ,PS.GlobalAttribute8 = P.GlobalAttribute8 ,PS.GlobalAttribute9 = P.GlobalAttribute9 ,PS.GlobalAttribute10 = P.GlobalAttribute10 ,PS.GlobalAttribute11 = P.GlobalAttribute11 ,PS.GlobalAttribute12 = P.GlobalAttribute12 ,PS.GlobalAttribute13 = P.GlobalAttribute13 ,PS.GlobalAttribute14 = P.GlobalAttribute14 ,PS.GlobalAttribute15 = P.GlobalAttribute15 ,PS.GlobalAttribute16 = P.GlobalAttribute16 ,PS.GlobalAttribute17 = P.GlobalAttribute17 ,PS.GlobalAttribute18 = P.GlobalAttribute18 ,PS.GlobalAttribute19 = P.GlobalAttribute19 ,PS.GlobalAttribute20 = P.GlobalAttribute20 ,PS.GlobalAttribute21 = P.GlobalAttribute21 ,PS.GlobalAttribute22 = P.GlobalAttribute22 ,PS.GlobalAttribute23 = P.GlobalAttribute23 ,PS.GlobalAttribute24 = P.GlobalAttribute24 ,PS.GlobalAttribute25 = P.GlobalAttribute25 ,ReturnMessage = 'Sucessful Read' ,ReturnCode = 200 ,ProcessFlag = '' FROM #ProductSet PS LEFT JOIN #Products P ON PS.FactoryID = P.FactoryID AND PS.ProductLineID = P.ProductLineID AND PS.ProductID = P.ProductID WHERE P.[Right] IN ('Read','Write'); -- Insert informations for Products without read Rights UPDATE PS SET ReturnMessage = 'Read denied' ,ReturnCode = 403 ,ProcessFlag = '' FROM #ProductSet PS LEFT JOIN #Products P ON PS.FactoryID = P.FactoryID AND PS.ProductLineID = P.ProductLineID AND PS.ProductID = P.ProductID WHERE P.[Right] IN ('Deny'); -- Insert informations for not found Products (still have ReturnCode 500) UPDATE #ProductSet SET ReturnMessage = 'Product not found' ,ReturnCode = 404 ,ProcessFlag = '' WHERE ReturnCode = 500; -- Output result ###################################################################### SELECT ProductNumber ,FactoryID ,ProductLineID ,ProductID ,TimeType ,NameShort ,CommentUser --Beschreibung -- keine Ausgabe von NameLong / CommentDev, da in Gui nicht sichtbar ,ResponsiblePerson ,ImageName ,[Status] ,Template ,GlobalAttribute1 ,GlobalAttribute2 ,GlobalAttribute3 ,GlobalAttribute4 ,GlobalAttribute5 ,GlobalAttribute6 ,GlobalAttribute7 ,GlobalAttribute8 ,GlobalAttribute9 ,GlobalAttribute10 ,GlobalAttribute11 ,GlobalAttribute12 ,GlobalAttribute13 ,GlobalAttribute14 ,GlobalAttribute15 ,GlobalAttribute16 ,GlobalAttribute17 ,GlobalAttribute18 ,GlobalAttribute19 ,GlobalAttribute20 ,GlobalAttribute21 ,GlobalAttribute22 ,GlobalAttribute23 ,GlobalAttribute24 ,GlobalAttribute25 ,ReturnMessage ,ReturnCode ,ProcessFlag FROM #ProductSet ps; SET @ResultCode = 200; END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Products_JSON' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure returns an in JSON Format requested Productarray as Recordset.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@Username' SET @value = N'Standardparameter'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductsJSON' SET @value = N'List of Products as JSON Array'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_TabLayouts; GO /* Procedure to POST a layout for a tab Saxess Software GmbH Last modified: 08/2024 for OCT 5.10 Testcall DECLARE @RC INT; EXEC @RC = planning.spGET_TabLayouts @Username = 'SQL' ,@FactoryID = '' ,@ProductLineID = '' ,@TabID = 'Test'; SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_TabLayouts',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_TabLayouts','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_TabLayouts @Username NVARCHAR(255) , @FactoryID NVARCHAR(255) = '' , @ProductLineID NVARCHAR(255) = '' , @TabID NVARCHAR(255) , @ReturnAllLayouts BIT = 0 AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 4, @Username, @FactoryID, @ProductLineID, @TabID, @ReturnAllLayouts; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Output SELECT LayoutID , LayoutName , LayoutJSON , OrderIndex , IIF(LayoutOwner = '', 1, 0) AS IsPublicLayout , LayoutOwner FROM planning.tTabLayouts WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND TabID = @TabID AND (COALESCE(NULLIF(LayoutOwner, ''), @Username) = @Username OR @ReturnAllLayouts = 1) ORDER BY IsPublicLayout ASC , OrderIndex ASC; SET @AffectedRows = @@ROWCOUNT; SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_TabLayouts' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure for POST Bulkload'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'optional FactoryID, skip / NULL / empty string if request is for Cluster level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'optional ProductLineID, skip / NULL / empty string if request is for Cluster or Factory level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TabID'; SET @value = N'TabID which is requested'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_Tabs; GO /* Procedure to GET Tabs for - the cluster - the factory - the productline - the product Saxess Software GmbH Last modified: 09/2023 for OCT 5.10 Testcall Procedure DECLARE @RC INT; EXEC @RC = planning.spGET_Tabs @Username = 'SQL' ,@FactoryID = 'ZT' ,@ProductLineID = 'U' ,@ProductID = '' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Tabs',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Tabs','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_Tabs ( @Username NVARCHAR(255) ,@FactoryID NVARCHAR(255) = '' ,@ProductLineID NVARCHAR(255) = '' ,@ProductID NVARCHAR(255) = '' ) AS BEGIN SET NOCOUNT ON; -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@FactoryID, N'NULL') + N''',''' + ISNULL(@ProductLineID, N'NULL') + N''',''' + ISNULL(@ProductID, N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' -- Procedure specific declarations DECLARE @FactoryKey INT = 0 ,@ProductLineKey INT = 0 ,@ProductKey INT = 0; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; -- Content Protection for specific input parameters (e.g.IDs) SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); -- START TRANSACTION *********************************************************************************** BEGIN TRY BEGIN TRANSACTION spGET_Tabs; -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- determine Keys for requested IDs - empty IDs keep Key -1 IF @FactoryID = '' SET @FactoryKey = -1; IF @ProductLineID = '' SET @ProductLineKey = -1; IF @ProductID = '' SET @ProductKey = -1; SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID AND @FactoryKey <> -1; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID AND @ProductLineKey <> -1; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID AND @ProductKey <> -1; --PRINT @FactoryKey --PRINT @ProductLineKey --PRINT @ProductKey -- error if and ID dont exists IF @FactoryKey = 0 OR @ProductLineKey = 0 OR @ProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Not existing FID / PID / PLID - Keys don`t exists', 16, 10); END; -- error if ID have gaps in the line IF @FactoryKey = -1 AND (@ProductLineKey <> -1 OR @ProductKey <> -1) OR @ProductLineKey = -1 AND @ProductKey <> -1 BEGIN SET @ResultCode = 404; RAISERROR('ID combination can have only gaps at the end - Keys don`t exists', 16, 10); END; -- check user rights for cluster level IF @FactoryKey = -1 BEGIN EXEC @ResultCode = planning.spGET_ClusterPropertiesReadRight @TransactUsername; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for Cluster read.', 16, 10); END END -- check user rights for Factory level IF @FactoryKey <> -1 AND @ProductLineKey = -1 BEGIN EXEC @ResultCode = planning.spGET_FactoryReadRight @TransactUsername,@FactoryID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for Factory read.', 16, 10); END END -- check user rights for ProductLine / Product level IF @FactoryKey <> -1 AND @ProductLineKey <> -1 BEGIN EXEC @ResultCode = planning.spGET_ProductLineReadRight @TransactUsername,@FactoryID,@ProductLineID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for ProductLine / Product read.', 16, 10); END END -- data operation starts *********************************************************************************** SELECT FactoryID ,ProductLineID ,ProductID ,TabID ,Orderindex ,TabName ,TabHint ,TabText ,PresentationCODE ,Datasource ,IsVisibleBOOL ,ParameterJSON ,SQLCommand FROM planning.tTabs WHERE (FactoryKey = @FactoryKey ) AND (ProductLineID = @ProductLineID) AND (ProductID = @ProductID ); SET @ResultCode = 200; COMMIT TRANSACTION spGET_Tabs; END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spGET_Tabs; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Tabs' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = not shiped from saxess -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- 10 = shiped from saxess as customer specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag',@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to get the Tabs for given planning Tree object. This may be the root level (Cluster), a Factory, a ProductLine or a Product.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Name of the user this action is requested for.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'optional FactoryID, skip / NULL / empty string if request is for Cluster level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'optional ProductLineID, skip / NULL / empty string if request is for Cluster or Factory level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'optional ProductD, skip / NULL / empty string if request is for Cluster, Factory or ProductLine level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; DROP PROCEDURE IF EXISTS planning.spGET_Tree; GO /* Saxess Software GmbH Last modified: 10/2023 for OCT 5.9 Testcall Procedure DECLARE @RC INT; EXEC @RC = planning.spGET_Tree @Username = 'SQL' ,@Searchstring = '' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Tree',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Tree','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_Tree ( @Username NVARCHAR(255) ,@Searchstring NVARCHAR(255) ) AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) -- SET PARAMETER LOG STRING -inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@Searchstring ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime(); -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @Searchstring IS NULL SET @Searchstring = N''; SET @Searchstring = CONCAT('%',@Searchstring,'%'); -- START TRY *********************************************************************************** BEGIN TRY PRINT CONCAT(N'Determine Righs of this user',N' (started after ', DateDiff(millisecond, @StartTime, SysUTCDateTime()),N'ms)'); -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- get ProductLine rights for this user DROP TABLE IF EXISTS #ProductLineRights; CREATE TABLE #ProductLineRights ( RowKey BIGINT IDENTITY (1,1) ,FactoryID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,[Right] NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ReadCommentMandatory INT NOT NULL ,WriteCommentMandatory INT NOT NULL ,PRIMARY KEY CLUSTERED (RowKey) ); INSERT INTO #ProductLineRights SELECT FactoryID ,ProductLineID ,[Right] ,ReadCommentMandatory ,WriteCommentMandatory FROM system.trUserRights WHERE UserName = @TransactUsername AND FactoryID <> ''; -- get Factory rights for this user (separat table for simple join later) DROP TABLE IF EXISTS #FactoryRights; CREATE TABLE #FactoryRights ( RowKey BIGINT IDENTITY (1,1) ,FactoryID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,[Right] NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,PRIMARY KEY CLUSTERED (RowKey) ); INSERT INTO #FactoryRights SELECT FactoryID ,[Right] FROM system.trUserRights WHERE UserName = @TransactUsername AND ProductLineID = ''; -- get all Products that have a file attached DROP TABLE IF EXISTS #FileExists; CREATE TABLE #FileExists ( RowKey BIGINT IDENTITY(1,1) , FactoryID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL , ProductlineID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL , ProductID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ); INSERT INTO #FileExists (FactoryID, ProductLineID, ProductID) SELECT DISTINCT FactoryID , ProductLineID , ProductID FROM planning.tFiles; -- Data Transaction PRINT CONCAT(N'Ausgabe Tree',N' (started after ', DateDiff(millisecond, @StartTime, SysUTCDateTime()),N'ms)'); SELECT dF.FactoryID AS FactoryID ,dF.NameShort AS FactoryNameShort ,dF.ImageName AS FactoryImageName ,RF.[Right] AS FactoryRight ,COALESCE(dPL.ProductLineID ,'') AS ProductLineID ,COALESCE(dPL.NameShort ,'') AS ProductLineNameShort ,COALESCE(dPL.ImageName ,'') AS ProductLineImageName ,COALESCE(RPL.[Right] ,'') AS ProductLineRight ,COALESCE(dP.ProductID ,'') AS ProductID ,COALESCE(dP.NameShort ,'') AS ProductNameShort ,COALESCE(dP.ImageName ,'') AS ProductImageName ,COALESCE(dP.Template ,'') AS ProductTemplate ,COALESCE(RPL.[Right] ,'') AS ProductRight --same as PL, but will become use later ,COALESCE(RPL.ReadCommentMandatory ,0) AS ReadCommentMandatory ,COALESCE(RPL.WriteCommentMandatory ,0) AS WriteCommentMandatory ,CASE WHEN fe.ProductID IS NULL THEN 0 ELSE 1 END AS FileExists FROM planning.tdFactories dF LEFT JOIN planning.tdProductLines dPL ON dPL.FactoryKey = dF.FactoryKey LEFT JOIN planning.tdProducts dP ON dP.ProductLineKey = dPL.ProductLineKey -- Rights on FactoryLevel INNER JOIN #FactoryRights RF ON dF.FactoryID = RF.FactoryID -- Rights on ProductLineLevel INNER JOIN #ProductLineRights RPL ON dF.FactoryID = RPL.FactoryID AND (dPL.ProductLineID = RPL.ProductLineID OR dPL.ProductLineID IS NULL) -- maybe there is no ProductLine under this Factory, then the Factory must not be filtered. -- File attached to this Product? LEFT JOIN #FileExists AS fe ON fe.FactoryID = df.FactoryID AND fe.ProductLineID = dPL.ProductLineID AND fe.ProductID = dP.ProductID -- Filtering due to search string WHERE ( dF.FactoryID LIKE @Searchstring OR dF.NameShort LIKE @Searchstring OR dPL.ProductLineID LIKE @Searchstring OR dPL.NameShort LIKE @Searchstring OR dP.ProductID LIKE @Searchstring OR dP.NameShort LIKE @Searchstring ) OR @Searchstring = '' ORDER BY -- primary sort numeric, fallback to sort alphabetic dF.CommentDev ,ISNULL(TRY_CAST(dF.FactoryID AS INT),99999999) ,dF.FactoryID ,dPL.CommentDev ,ISNULL(TRY_CAST(dPL.ProductLineID AS INT),99999999) ,dPL.ProductLineID ,dP.CommentDev ,ISNULL(TRY_CAST(dP.ProductID AS INT),99999999) ,dP.ProductID; SET @ResultCode = 200; PRINT CONCAT(N'Transaktion abgeschlossen',N' (after ', DateDiff(millisecond, @StartTime, SysUTCDateTime()),N'ms)') END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Tree' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Returns the Tree of all planning object - Factories/ProductLines/Products'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@Searchstring'; SET @value = N'String to filter the output values - will be handled as %Searchstring% search.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spGET_Values_JSON; GO /* Procedure to GET Values in JSON Format for MatrixConnector MatrixConnector: - Shows Values which are combined from a Time Axis and a Structure Axis - The Structure Axis has the attributes F/PL/P/VS where each can be substituted by a * to mean all - The Time Axis shows one TimeID (later with flex Time concept it will show Year, Month, Day where also each one can be a *) This GET Procedure returns for one TimeID all Values The Values for this TimeID are requsted as JSON Retruns in a Array per Requested Number - Nummerischen Wert nach Operation SUM, MAX,... - String if single request - Datum - ValueComment if single request - Statistik for Aggregatfunction - Anzahl Treffer - F/PL/P/VS bei MAX, MIN Aktuelle Limitierungen: - Es wird immer der Aggreationstyp Summe zurückgegeben - egal was angefragt wird - Es werden derzeit nur direkte Einzelwerte verarbeitet und zurückgegeben - Es werden somit alle mit * angefragten Zeilen als NULL zurückgegeben - Er werden nur nummerische Werte zurückgegeben, noch keine Texte - Es werden noch keine Rechte verarbeitet TODO: - Nur Werte für die berechtigten Strukturinhalten zurückgeben - Info über Berechtigung im Text - Berechtigung von nicht gefunden unterscheiden - ValueSeries kann bei Aggregation beide mehrere Datentypen lieferen -> muss bei Aggregation Typ wählen ? - Werte können in unterschiedlichen ValueSeries unterschiedlich skaliert sein - Nach dem Schreiben von Werten nach DF kann ein Powerloading notwendig sein - Comments ausgeben Gerd Tautenhahn for Saxess Software GmbH Last modified: 02/2023 for OCT 5.8 Testcall DECLARE @ValuesJSON NVARCHAR(MAX) SET @ValuesJSON = N'{ "connector_type":"read" ,"version":1 ,"Values": { "ValueSeries": [ { "SeriesNumber" : 1 ,"AggregateType": "SUM" ,"FactoryID" : "ZT" ,"ProductLineID": "*" ,"ProductID":"*" ,"ValueSeriesID": "*" } ] ,"TimeIDs": [ { "TimeNumber" : 1 ,"TimeID" : 201902 }, { "TimeNumber" : 2 ,"TimeID" : 20190215 }, { "TimeNumber" : 3 ,"TimeID" : 20190315 }, { "TimeNumber" : 4 ,"TimeID" : 20190415 } ] } } ' EXEC planning.spGET_Values_JSON 'SQL', @ValuesJSON EXEC planning.spGET_Values_JSON 'SQL','{"connector_type":"read","version":1,"Values":{"ValueSeries":[{"SeriesNumber":1,"AggregateType":"SUM","FactoryID":"ZT","ProductLineID":"U","ProductID":1,"ValueSeriesID":"K1"}],"TimeIDs":[{"TimeNumber":1,"TimeID":20200115},{"TimeNumber":2,"TimeID":20190215},{"TimeNumber":3,"TimeID":20190315},{"TimeNumber":4,"TimeID":20190415}]}}' Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Values_JSON',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spGET_Values_JSON','PARAMETER',NULL) */ CREATE PROCEDURE planning.spGET_Values_JSON @Username NVARCHAR(255) = '' ,@ValuesJSON NVARCHAR(MAX) = '' AS BEGIN SET NOCOUNT ON; DECLARE @TransactionUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''', '''+ ISNULL(@ValuesJSON, N'NULL')+ N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N''; IF @Username IS NULL SET @Username = N''; IF @ValuesJSON IS NULL SET @ValuesJSON = N''; BEGIN TRY SET @Username = system.fProtectString (@Username); -- Test for technical valid JSON ################################################################################ IF @ValuesJSON = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; IF ISJSON(@ValuesJSON) = 0 BEGIN SET @ResultCode = 403; THROW 60000, N'Exit due to technical invalid JSON', 100; END; -- Extract ValueSeries FROM JSON into tmp table###################################################################### DROP TABLE IF EXISTS #RequestedValues; SELECT DISTINCT SeriesNumber ,AggregateType COLLATE DATABASE_DEFAULT AS AggregateType ,CASE WHEN FactoryID = N'*' OR FactoryID LIKE N'*_%' THEN FactoryID ELSE system.fProtectID (FactoryID) END COLLATE DATABASE_DEFAULT AS 'FactoryID' ,CASE WHEN ProductLineID = N'*' OR ProductLineID LIKE N'*_%' THEN ProductLineID ELSE system.fProtectID (ProductLineID) END COLLATE DATABASE_DEFAULT AS 'ProductLineID' ,CASE WHEN ProductID = N'*' OR ProductID LIKE N'*_%' THEN ProductID ELSE system.fProtectID (ProductID) END COLLATE DATABASE_DEFAULT AS 'ProductID' ,CASE WHEN ValueSeriesID = N'*' OR ValueSeriesID LIKE N'*_%' THEN ValueSeriesID ELSE system.fProtectID (ValueSeriesID) END COLLATE DATABASE_DEFAULT AS 'ValueSeriesID' INTO #RequestedValues FROM OPENJSON (@ValuesJSON, N'$.Values.ValueSeries') WITH ( SeriesNumber INT N'$.SeriesNumber' ,AggregateType NVARCHAR(50) N'$.AggregateType' ,FactoryID NVARCHAR(255) N'$.FactoryID' ,ProductLineID NVARCHAR(255) N'$.ProductLineID' ,ProductID NVARCHAR(255) N'$.ProductID' ,ValueSeriesID NVARCHAR(255) N'$.ValueSeriesID' ); -- Test on mandatory parameter completness IF EXISTS( SELECT TOP(1) 1 FROM #RequestedValues WHERE SeriesNumber IS NULL OR AggregateType IS NULL OR FactoryID IS NULL OR ProductLineID IS NULL OR ProductID IS NULL OR ValueSeriesID IS NULL OR FactoryID = N'' OR ProductLineID = N'' OR ProductID = N'' OR ValueSeriesID = N'') OR EXISTS( SELECT TOP(1) 1 FROM #RequestedValues GROUP BY SeriesNumber HAVING COUNT (SeriesNumber) > 1 ) BEGIN SET @ResultCode = 403; THROW 60000, N'Exit due to logical failure in requested values (missing IDs, multiple SeriesNumber ..)', 100; END; -- Extract TimeIDs FROM JSON ####################################################################### DROP TABLE IF EXISTS #RequestedTimes; ;WITH times AS ( SELECT DISTINCT TimeNumber ,TimeID FROM OPENJSON (@ValuesJSON, N'$.Values.TimeIDs') WITH ( TimeNumber INT N'$.TimeNumber' ,TimeID INT N'$.TimeID' ) ) SELECT * --determine Max/Min TimeID from cutted TimeIDs which represend Year / Month / Day , CASE WHEN TimeID BETWEEN 1000 AND 9999 THEN TimeID * 10000 WHEN TimeID BETWEEN 100000 AND 999999 THEN TimeID * 100 WHEN TimeID BETWEEN 10000000 AND 99999999 THEN TimeID ELSE NULL END AS 'MinTimeID' , CASE WHEN TimeID BETWEEN 1000 AND 9999 THEN TimeID * 10000 + 9999 WHEN TimeID BETWEEN 100000 AND 999999 THEN TimeID * 100 + 99 WHEN TimeID BETWEEN 10000000 AND 99999999 THEN TimeID ELSE NULL END AS 'MaxTimeID' INTO #RequestedTimes FROM times; IF EXISTS( SELECT TOP(1) 1 FROM #RequestedTimes WHERE TimeNumber IS NULL OR MinTimeID IS NULL OR MaxTimeID IS NULL ) OR EXISTS( SELECT TOP(1) 1 FROM #RequestedTimes GROUP BY TimeNumber HAVING COUNT (TimeNumber) > 1 ) BEGIN SET @ResultCode = 403; THROW 60000, N'Exit due to problems in processing requested TimeIDs.', 100; END; -- STEP 1.1 - Determine transaction user SELECT @TransactionUsername = system.fDetermineTransactionUsername (@Username); IF @TransactionUsername = N'403' BEGIN SET @ResultCode = 403; THROW 60000, N'Transaction user don`t exists', 100; END; -- STEP 3.0 - Search ValueSeries and User`s rights DROP TABLE IF EXISTS #ValueSeries; ;WITH search AS ( SELECT SeriesNumber ,AggregateType ,FactoryID ,ProductLineID ,ProductID ,ValueSeriesID ,CASE WHEN FactoryID = N'*' THEN 1 WHEN FactoryID LIKE N'*_%' THEN 2 ELSE 0 END AS FactoryIDSearchType ,CASE WHEN ProductLineID = N'*' THEN 1 WHEN ProductLineID LIKE N'*_%' THEN 2 ELSE 0 END AS ProductLineIDSearchType ,CASE WHEN ProductID = N'*' THEN 1 WHEN ProductID LIKE N'*_%' THEN 2 ELSE 0 END AS ProductIDSearchType ,CASE WHEN ValueSeriesID = N'*' THEN 1 WHEN ValueSeriesID LIKE N'*_%' THEN 2 ELSE 0 END AS ValueSeriesIDSearchType ,CASE WHEN FactoryID LIKE N'*_%' THEN SUBSTRING(FactoryID, 2, 300) ELSE NULL END AS FactoryIDPattern ,CASE WHEN ProductLineID LIKE N'*_%' THEN SUBSTRING(ProductLineID, 2, 300) ELSE NULL END AS ProductLineIDPattern ,CASE WHEN ProductID LIKE N'*_%' THEN SUBSTRING(ProductID, 2, 300) ELSE NULL END AS ProductIDPattern ,CASE WHEN ValueSeriesID LIKE N'*_%' THEN SUBSTRING(ValueSeriesID, 2, 300) ELSE NULL END AS ValueSeriesIDPattern FROM #RequestedValues ) SELECT rv.SeriesNumber ,rv.AggregateType ,dF.FactoryID COLLATE DATABASE_DEFAULT AS FactoryID ,dPL.ProductLineID COLLATE DATABASE_DEFAULT AS ProductLineID ,dP.ProductID COLLATE DATABASE_DEFAULT AS ProductID ,dVS.ValueSeriesID COLLATE DATABASE_DEFAULT AS ValueSeriesID ,dF.FactoryKey ,dPL.ProductLineKey ,dP.ProductKey ,dVS.ValueSeriesKey ,dVS.Scale ,dVS.[IsNumeric] ,rF.[Row] AS 'FactoryRightRow' ,rPL.[Row] AS 'ProductLineRightRow' INTO #ValueSeries FROM search rv INNER JOIN planning.tdFactories dF ON rv.FactoryIDSearchType = 1 OR (rv.FactoryIDSearchType = 2 AND df.FactoryID LIKE rv.FactoryIDPattern) OR (rv.FactoryIDSearchType = 0 AND df.FactoryID = rv.FactoryID) INNER JOIN planning.tdProductLines dPL ON dPL.FactoryKey = dF.FactoryKey AND (rv.ProductLineIDSearchType = 1 OR (rv.ProductLineIDSearchType = 2 AND dPL.ProductLineID LIKE rv.ProductLineIDPattern) OR (rv.ProductLineIDSearchType = 0 AND dPL.ProductLineID = rv.ProductLineID)) INNER JOIN planning.tdProducts dP ON dP.FactoryKey = dF.FactoryKey AND dP.ProductLineKey = dPL.ProductLineKey AND (rv.ProductIDSearchType = 1 OR (rv.ProductIDSearchType = 2 AND dP.ProductID LIKE rv.ProductIDPattern) OR (rv.ProductIDSearchType = 0 AND dP.ProductID = rv.ProductID)) INNER JOIN planning.tdValueSeries dVS ON dVS.FactoryKey = dF.FactoryKey AND dVS.ProductLineKey = dPL.ProductLineKey AND dVS.ProductKey = dP.ProductKey AND (rv.ValueSeriesIDSearchType = 1 OR (rv.ValueSeriesIDSearchType = 2 AND dVS.ValueSeriesID LIKE rv.ValueSeriesIDPattern) OR (rv.ValueSeriesIDSearchType = 0 AND dVS.ValueSeriesID = rv.ValueSeriesID)) LEFT JOIN system.trUserRights rF ON df.FactoryID = rF.FactoryID AND rF.ProductLineID = N'' AND rF.Username = @TransactionUsername AND (rF.[Right] = N'Write' OR rF.[Right] = N'Read') LEFT JOIN system.trUserRights rPL ON df.FactoryID = rPL.FactoryID AND dPL.ProductLineID = rPL.ProductLineID AND rPL.Username = @TransactionUsername AND (rPL.[Right] = N'Write' OR rPL.[Right] = N'Read'); -- STEP 3.1 - Searching requested values DROP TABLE IF EXISTS #Values; SELECT s.SeriesNumber ,s.FactoryKey ,s.ProductLineKey ,s.ProductKey ,s.ValueSeriesKey ,s.FactoryID COLLATE DATABASE_DEFAULT AS FactoryID ,s.ProductLineID COLLATE DATABASE_DEFAULT AS ProductLineID ,s.ProductID COLLATE DATABASE_DEFAULT AS ProductID ,s.ValueSeriesID COLLATE DATABASE_DEFAULT AS ValueSeriesID ,ISNULL(CASE WHEN s.[IsNumeric] = 1 THEN CAST(fV.ValueInt AS Money) / s.Scale ELSE 0 END, 0) AS 'ValueNumeric' ,ISNULL(CASE WHEN s.[IsNumeric] = 0 THEN fV.ValueText ELSE N'' END, N'') COLLATE DATABASE_DEFAULT AS 'ValueString' ,fV.ValueComment COLLATE DATABASE_DEFAULT AS ValueComment ,t.TimeNumber ,fV.TimeID ,s.[IsNumeric] INTO #Values FROM #ValueSeries s INNER JOIN planning.tfValues fV ON -- why join over four Key fields ? fV.FactoryKey = s.FactoryKey AND fV.ProductLineKey = s.ProductLineKey AND fV.ProductKey = s.ProductKey AND fV.ValueSeriesKey = s.ValueSeriesKey AND NOT s.FactoryRightRow IS NULL AND NOT s.ProductLineRightRow IS NULL INNER JOIN #RequestedTimes t ON fV.TimeID BETWEEN t.MinTimeID AND t.MaxTimeID; -- STEP 3.2 - Insert requested values into result table DROP TABLE IF EXISTS #ValueSet; CREATE TABLE #ValueSet ( SeriesNumber INT NOT NULL ,AggregateType NVARCHAR(50) COLLATE DATABASE_DEFAULT NOT NULL ,FactoryID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,TimeNumber INT NOT NULL ,TimeID INT NOT NULL ,FactoryKey BIGINT NULL ,ProductLineKey BIGINT NULL ,ProductKey BIGINT NULL ,ValueSeriesKey BIGINT NULL ,FactoryCount INT NULL ,ProductLineCount INT NULL ,ProductCount INT NULL ,ValueSeriesCount INT NULL ,ValueNumeric MONEY NULL ,ValueString NVARCHAR(4000) COLLATE DATABASE_DEFAULT NULL ,ValueDate DATE NULL ,ValueComment NVARCHAR(4000) COLLATE DATABASE_DEFAULT NULL ,ReturnMessage NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,ReturnCode INT NULL ,ProcessFlag INT NULL ); INSERT INTO #ValueSet (SeriesNumber, AggregateType, FactoryID, ProductLineID, ProductID, ValueSeriesID, TimeNumber, TimeID) SELECT v.SeriesNumber ,v.AggregateType ,v.FactoryID ,v.ProductLineID ,v.ProductID ,v.ValueSeriesID ,t.TimeNumber ,t.TimeID FROM #RequestedValues v, #RequestedTimes t; -- STEP 3.1 - Set 'No rights' comments ;WITH series AS ( SELECT FactoryID ,ProductLineID ,MAX([IsNumeric]) AS 'IsNumeric' ,MIN([IsNumeric]) AS 'IsString' FROM #ValueSeries WHERE FactoryRightRow IS NULL OR ProductLineRightRow IS NULL GROUP BY FactoryID, ProductLineID ) UPDATE s SET ReturnMessage = N'No rights' ,ReturnCode = 403 ,FactoryCount = 0 ,ProductLineCount = 0 ,ProductCount = 0 ,ValueSeriesCount = 0 ,ValueNumeric = CASE WHEN v.[IsNumeric] = 1 THEN 0 ELSE NULL END ,ValueString = CASE WHEN v.IsString = 0 THEN N'' ELSE NULL END ,ValueComment = N'' FROM #ValueSet s INNER JOIN series v ON s.FactoryID = v.FactoryID AND s.ProductLineID = v.ProductLineID; ---- STEP 3.2 - Fill result table with exists values, make aggregation ;WITH vals AS ( SELECT SeriesNumber ,TimeNumber ,COUNT(DISTINCT FactoryKey) AS 'FactoryCount' ,COUNT(DISTINCT ProductLineKey) AS 'ProductLineCount' ,COUNT(DISTINCT ProductKey) AS 'ProductCount' ,COUNT(DISTINCT ValueSeriesKey) AS 'ValueSeriesCount' ,MAX(FactoryKey) AS 'FactoryKey' ,MAX(ProductLineKey) AS 'ProductLineKey' ,MAX(ProductKey) AS 'ProductKey' ,MAX(ValueSeriesKey) AS 'ValueSeriesKey' ,SUM(ValueNumeric) AS 'ValueNumeric' FROM #Values GROUP BY SeriesNumber, TimeNumber ), comments AS ( SELECT TOP (1) WITH TIES SeriesNumber ,TimeNumber ,ValueComment FROM #Values WHERE NOT ValueComment IS NULL ORDER BY ROW_NUMBER() OVER(PARTITION BY SeriesNumber, TimeNumber ORDER BY FactoryID, ProductLineID, ProductID, ValueSeriesID, TimeID DESC) ), stringValues AS ( SELECT TOP (1) WITH TIES SeriesNumber ,TimeNumber ,ValueString FROM #Values WHERE [IsNumeric] = 0 AND NOT ValueString IS NULL ORDER BY ROW_NUMBER() OVER(PARTITION BY SeriesNumber, TimeNumber ORDER BY FactoryID, ProductLineID, ProductID, ValueSeriesID, TimeID DESC) ) UPDATE s SET s.FactoryKey = CASE WHEN s.FactoryID LIKE N'*%' THEN NULL ELSE v.FactoryKey END ,s.ProductLineKey = CASE WHEN s.ProductLineID LIKE N'*%' THEN NULL ELSE v.ProductLineKey END ,s.ProductKey = CASE WHEN s.ProductID LIKE N'*%' THEN NULL ELSE v.ProductKey END ,s.ValueSeriesKey = CASE WHEN s.ValueSeriesID LIKE N'*%' THEN NULL ELSE v.ValueSeriesKey END ,s.FactoryCount = v.FactoryCount ,s.ProductLineCount = v.ProductLineCount ,s.ProductCount = v.ProductCount ,s.ValueSeriesCount = v.ValueSeriesCount ,s.ValueNumeric = v.ValueNumeric ,s.ValueString = ISNULL(sv.ValueString, N'') ,s.ValueComment = ISNULL(c.ValueComment, N'') FROM #ValueSet s INNER JOIN vals v ON s.SeriesNumber = v.SeriesNumber AND s.TimeNumber = v.TimeNumber LEFT JOIN comments c ON s.SeriesNumber = c.SeriesNumber AND s.TimeNumber = c.TimeNumber LEFT JOIN stringValues sv ON s.SeriesNumber = sv.SeriesNumber AND s.TimeNumber = sv.TimeNumber WHERE s.FactoryCount IS NULL; ---- STEP 3.4 - Fill ValueNumeric, ValueComment and ValueString columns and set zero-count for non exists values ;WITH series AS ( SELECT SeriesNumber ,MAX([IsNumeric]) AS 'IsNumeric' ,MIN([IsNumeric]) AS 'IsString' FROM #ValueSeries GROUP BY SeriesNumber ) UPDATE s SET FactoryCount = ISNULL(FactoryCount, 0) ,ProductLineCount = ISNULL(ProductLineCount, 0) ,ProductCount = ISNULL(ProductCount, 0) ,ValueSeriesCount = ISNULL(ValueSeriesCount, 0) ,ValueNumeric = NULL ,ValueString = NULL ,ValueComment = NULL FROM #ValueSet s INNER JOIN series v ON s.SeriesNumber = v.SeriesNumber WHERE s.FactoryCount IS NULL; -- STEP 3.5 - Output result SELECT ROW_NUMBER() OVER (ORDER BY SeriesNumber, FactoryID, ProductLineID, ProductID, ValueSeriesID, TimeNumber) AS RowKey ,SeriesNumber ,TimeNumber ,AggregateType ,FactoryID ,ProductLineID ,ProductID ,ValueSeriesID ,FactoryKey ,ProductLineKey ,ProductKey ,ValueSeriesKey ,FactoryCount ,ProductLineCount ,ProductCount ,ValueSeriesCount ,TimeID ,ValueNumeric ,ValueString ,ValueDate ,ValueComment ,IIF(ReturnMessage IS NULL,'',ReturnMessage) AS ReturnMessage ,IIF(ReturnCode IS NULL,200,ReturnCode) AS ReturnCode ,ProcessFlag FROM #ValueSet; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state = 100 BEGIN PRINT @Comment; END ELSE IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactionUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Values_JSON' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET Values in JSON Format for MatrixConnector'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValuesJSON'; SET @value = N'ValuesJSON'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spMOVE_Tab; GO /* Procedure to move a Tab to - another ID - another OrderIndex Saxess Software GmbH Last modified: 09/2023 for OCT 5.9 Testcall Procedure DECLARE @RC INT; EXEC @RC = planning.spMOVE_Tab @Username = 'W10\admin' ,@FactoryID = '' ,@ProductLineID = '' ,@ProductID = '' ,@TabID = '0' ,@NewTabID = 'T7' ,@NewOrderIndex = '188' PRINT @RC SELECT * FROM planning.tTabs ORDER BY OrderIndex TRUNCATE TABLE planning.tTabs Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spMOVE_Tab',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spMOVE_Tab','PARAMETER',NULL) */ CREATE PROCEDURE planning.spMOVE_Tab ( @Username NVARCHAR(255) ,@FactoryID NVARCHAR(255) = '' ,@ProductLineID NVARCHAR(255) = '' ,@ProductID NVARCHAR(255) = '' ,@TabID NVARCHAR(255) ,@NewTabID NVARCHAR(255) ,@NewOrderIndex INT ) AS BEGIN SET NOCOUNT ON; -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@FactoryID ,N'NULL') + N''',''' + ISNULL(@ProductLineID ,N'NULL') + N''',''' + ISNULL(@ProductID ,N'NULL') + N''',''' + ISNULL(@TabID ,N'NULL') + N''',' + ISNULL(@NewTabID ,N'NULL') + N''',' + CAST(ISNULL(@NewOrderIndex ,N'NULL') AS NVARCHAR(255)) + N'' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' -- Procedure specific declarations DECLARE @FactoryKey INT = 0 ,@ProductLineKey INT = 0 ,@ProductKey INT = 0 ,@RowKey BIGINT = 0 ,@OldOrderIndex INT = 0; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @TabID IS NULL SET @TabID = N''; IF @NewTabID IS NULL SET @NewTabID = N''; IF @NewOrderIndex IS NULL SET @NewOrderIndex = 0 ; -- dont use negative index values IF @NewOrderIndex <= 0 SET @NewOrderIndex = 1; -- Content Protection for specific input parameters (e.g.IDs) SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); -- START TRANSACTION *********************************************************************************** BEGIN TRY BEGIN TRANSACTION spMOVE_Tab; -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- determine Keys for requested IDs - empty IDs keep Key -1 IF @FactoryID = '' SET @FactoryKey = -1; IF @ProductLineID = '' SET @ProductLineKey = -1; IF @ProductID = '' SET @ProductKey = -1; SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID AND @FactoryKey <> -1; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID AND @ProductLineKey <> -1; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID AND @ProductKey <> -1; --PRINT @FactoryKey --PRINT @ProductLineKey --PRINT @ProductKey -- error if and ID dont exists IF @FactoryKey = 0 OR @ProductLineKey = 0 OR @ProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Not existing FID / PID / PLID - Keys don`t exists', 16, 10); END; -- check if TabID exists IF NOT EXISTS ( SELECT RowKey FROM planning.tTabs WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey AND TabID = @TabID ) BEGIN SET @ResultCode = 404; RAISERROR('TabID not found.', 16, 10); END; -- tab with ID 0 cant be moved IF @TabID = '0' OR @NewTabID = '0' BEGIN SET @ResultCode = 403; RAISERROR('Tab with ID 0 can''t be moved', 16, 10); END -- error if ID have gaps in the line IF @FactoryKey = -1 AND (@ProductLineKey <> -1 OR @ProductKey <> -1) OR @ProductLineKey = -1 AND @ProductKey <> -1 BEGIN SET @ResultCode = 404; RAISERROR('ID combination can have only gaps at the end - Keys don`t exists', 16, 10); END; -- check user rights for cluster level IF @FactoryKey = -1 BEGIN EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for Cluster write.', 16, 10); END END -- check user rights for Factory level IF @FactoryKey <> - 1 AND @ProductLineKey = -1 BEGIN EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername,@FactoryID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for Factory write.', 16, 10); END END -- check user rights for ProductLine / Product level IF @FactoryKey <> -1 AND @ProductLineKey <> -1 BEGIN EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername,@FactoryID,@ProductLineID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for ProductLine / Product write.', 16, 10); END END -- check if new TabID is free IF EXISTS ( SELECT RowKey FROM planning.tTabs WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey AND TabID = @NewTabID AND TabID <> @TabID ) BEGIN SET @ResultCode = 401; RAISERROR('New TabID already in use.', 16, 10); END; -- determine current Key and OrderIndex SELECT @Rowkey = RowKey ,@OldOrderIndex = Orderindex FROM planning.tTabs WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey AND TabID = @TabID -- data operation starts *********************************************************************************** -- update tab layouts UPDATE planning.tTabLayouts SET TabID = @NewTabID WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND TabID = @TabID; -- Insert new Tab UPDATE planning.tTabs SET TabID = @NewTabID ,Orderindex = @NewOrderIndex WHERE RowKey = @RowKey --PRINT 'OldOrderIndex ' + CAST (@OldOrderIndex AS NVARCHAR(10)) --PRINT 'NewOrderIndex ' + CAST (@NewOrderIndex AS NVARCHAR(10)) -- Update other numbers if Number is changed IF @OldOrderIndex <> @NewOrderIndex BEGIN -- case if OrderIndex is bigger now and existed before - decrement all in move window IF @OldOrderIndex < @NewOrderIndex BEGIN UPDATE planning.tTabs SET Orderindex = Orderindex - 1 WHERE Orderindex >= @OldOrderIndex AND Orderindex <= @NewOrderIndex AND FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey AND TabID <> @NewTabID END -- case if OrderIndex is smaller now and existed before - increment all in move window IF @OldOrderIndex > @NewOrderIndex BEGIN UPDATE planning.tTabs SET Orderindex = Orderindex + 1 WHERE Orderindex <= @OldOrderIndex AND Orderindex >= @NewOrderIndex AND FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey AND TabID <> @NewTabID END -- eliminate all Gaps UPDATE planning.tTabs SET Orderindex = Rownumbers.Number FROM planning.tTabs tT INNER JOIN ( SELECT ROW_NUMBER() OVER (ORDER BY Orderindex) AS Number ,TabID FROM planning.tTabs WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey ) AS Rownumbers ON tT.TabID = Rownumbers.TabID AND tT.FactoryKey = @FactoryKey AND tT.ProductLineKey = @ProductLineKey AND tT.ProductKey = @ProductKey END IF @ResultCode <> 201 SET @ResultCode = 200; COMMIT TRANSACTION spMOVE_Tab; END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spMOVE_Tab; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spMOVE_Tab' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = not shiped from saxess -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- 10 = shiped from saxess as customer specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag',@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to Move a Tab to a different ID and different OrderIndex. The requested TabID must be free. The requested Orderposition may be in use, reordering is done automaticly. To put a tab to the end, any high order number (like 100) can be submitted. The Tab with ID 0 can''t be moved.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Name of the user this action is requested for.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'optional FactoryID, skip / NULL / empty string if MOVE is for Cluster level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'optional ProductLineID, skip / NULL / empty string if MOVE is for Cluster or Factory level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'optional ProductD, skip / NULL / empty string if MOVE is for Cluster, Factory or ProductLine level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TabID'; SET @value = N'TabID which is moved. ID 0 is not accepted, its reserved for the system tab.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@NewTabID'; SET @value = N'TabID the tab will get. This must not be already in use.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@NewOrderIndex'; SET @value = N'Position in the list of Tabs. Any Number > 0 can be passed in, tabs reorder themself.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_File; GO /* Procedure to save new file or change exists file Files may be saved on Factories, Productlines, Products on Clusters - thefore the F/PL/PID is optional @FileBody can be NULL - this are files which represend folders If @FileID is NULL or Empty then error raised If @FileID is '*' will create new file @FileExtension can be null (will be created file without extension) @FileName can`t be null or empty If @FileID is NOT null will change exists file If @FileExtension is NULL then don`t change extension of exists file If @FileExtension is empty then set to NULL extension of exists file (file without extension) Otherwise change extension to @FileExtension If @FileName is NULL then don`t change name of exists file If @FileName is empty then error raised Otherwise change file name to @FileName Gerd Tautenhahn for Saxess Software GmbH Last modified: 02/2023 for OCT 5.9 Testcall DECLARE @RC INT ,@ReturnedFileID NVARCHAR (50); EXEC planning.spPOST_File 'SQL' ,'' ,'' ,'' ,'' -- FileID or * ,'Hallo22' ,'pdf' ,0x3 ,'/Rootfolder/Subfolder2' ,@OutputID = @ReturnedFileID OUTPUT; PRINT @RC; PRINT 'FileID Output: '+ ISNULL(@ReturnedFileID,'no Output set'); Testcall tables SELECT * FROM planning.tFiles; DELETE FROM planning.tFiles; Testcall Log SELECT TOP 5 * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_File',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_File','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_File @Username NVARCHAR(255) = N'' ,@FactoryID NVARCHAR(50) = N'' ,@ProductLineID NVARCHAR(50) = N'' ,@ProductID NVARCHAR(50) = N'' ,@FileID NVARCHAR(50) = N'' ,@FileName NVARCHAR(255) = N'' ,@FileExtension NVARCHAR(50) = NULL ,@FileBody VARBINARY(MAX) = NULL ,@Filepath NVARCHAR(4000) = N'/' ,@OutputID NVARCHAR(50) = NULL OUTPUT AS BEGIN SET NOCOUNT ON DECLARE @TransactionUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username , N'NULL') + N''', ''' + ISNULL(@FactoryID , N'NULL') + N''', ''' + ISNULL(@ProductLineID , N'NULL') + N''', ''' + ISNULL(@ProductID , N'NULL') + N''', ''' + ISNULL(@FileID , N'NULL') + N''', ''' + ISNULL(@FileName , N'NULL') + N''', ''' + ISNULL(@FileExtension , N'NULL') + N''', ' + ISNULL(CAST(DATALENGTH(@FileBody) AS NVARCHAR(MAX)) + N' bytes' , N'NULL') + N',''' + ISNULL(@Filepath,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@FactoryKey INT = 0 ,@ProductLineKey INT = 0 ,@ProductKey INT = 0 ,@FileExistsFlag INT = 0; --NULL Protection IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @FileID IS NULL SET @FileID = N''; IF @Filepath IS NULL SET @Filepath = N'/'; IF @FactoryID = N'' SET @FactoryKey = -1; IF @ProductLineID = N'' SET @ProductLineKey = -1; IF @ProductID = N'' SET @ProductKey = -1; BEGIN TRY BEGIN TRANSACTION spPOST_File; -- protect IDs SET @Username = system.fProtectString (@Username); SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); -- empty FileID will generate a new FileID IF @FileID = '' SET @FileID = '*'; -- determine Transactuser SELECT @TransactionUsername = system.fDetermineTransactionUsername (@Username); IF @TransactionUsername = N'403' BEGIN SET @ResultCode = 403; THROW 60000, N'Transaction user don`t exists', 100; END; -- check input parameters (NULL for FileBody is allowed to save files which represent folders) IF @FileName ='' BEGIN SET @ResultCode = 403; THROW 60000, N'FileName is empty', 100; END; -- error if Keys chains has gaps IF (@FactoryKey = -1 AND (@ProductLineKey <> -1 OR @ProductKey <> -1)) OR (@ProductLineKey = -1 AND @ProductKey <> -1) BEGIN SET @ResultCode = 404; RAISERROR('Keys combination can have only gaps at the end', 16, 10); END; IF @FactoryKey <> -1 BEGIN SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID; IF @ProductLineKey <> -1 BEGIN SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID; IF @ProductKey <> -1 BEGIN SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductID = @ProductID; END END END -- check user rights for cluster level IF @FactoryKey = -1 BEGIN EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactionUsername; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for Cluster write.', 16, 10); END END -- check user rights for Factory level ELSE IF @ProductLineKey = -1 BEGIN EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactionUsername, @FactoryID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for Factory write.', 16, 10); END END -- check user rights for ProductLine / Product level ELSE IF @ProductLineKey > 0 BEGIN EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactionUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for ProductLine.', 16, 10); END END -- error if and Keys dont exists IF @FactoryKey = 0 OR @ProductLineKey = 0 OR @ProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Not existing FID / PID / PLID - Keys don`t exists', 16, 10); END; -- if FileID does not exits or FileID = '*', create new file SELECT @FileExistsFlag = COUNT(FileID) FROM planning.tFiles WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND ProductID = @ProductID AND FileID = @FileID; IF @FileExistsFlag = 0 OR @FileID = '*' -- create new file BEGIN IF @FileID = '*' BEGIN -- Determine next free FileID - outer COALESCE FOR CASE no existing row, inner COALESCE for TRY_CAST catching SELECT @FileID = CONCAT('F', COALESCE( MAX(COALESCE(TRY_CAST(RIGHT(FileID,LEN(FileID)-1) AS INT),0)) ,0)+1 ) FROM planning.tFiles WHERE (@FactoryKey = -1 OR FactoryKey = @FactoryKey) AND (@ProductLineKey = -1 OR ProductLineKey = @ProductLineKey) AND (@ProductKey = -1 OR ProductKey = @ProductKey) AND FileID LIKE 'F%'; END IF LEN(@FileExtension) = 0 SET @FileExtension = NULL; -- create new file INSERT INTO planning.tFiles (FactoryKey, ProductLineKey, ProductKey, FactoryID, ProductLineID, ProductID, FileID, [FileName], FileExtension, FileBody, UploadedBy, UploadedAt,Filepath) VALUES (@FactoryKey, @ProductLineKey, @ProductKey, @FactoryID, @ProductLineID, @ProductID, @FileID, @FileName, @FileExtension, @FileBody,@Username,GETUTCDATE(),@Filepath); END ELSE -- update existing file BEGIN DECLARE @FileKey BIGINT = NULL; SELECT TOP (1) @FileKey = FileKey FROM planning.tFiles WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey AND FileID = @FileID; -- update existing file UPDATE planning.tFiles SET [FileName] = ISNULL(@FileName, [FileName]) ,FileExtension = CASE WHEN @FileExtension IS NULL THEN FileExtension WHEN LEN(@FileExtension) = 0 THEN NULL ELSE @FileExtension END ,FileBody = ISNULL(@FileBody,FileBody) ,UploadedBy = @Username ,UploadedAt = GETUTCDATE() ,Filepath = ISNULL(@Filepath,Filepath) WHERE FileKey = @FileKey; END -- OUTPUT Parameter SET @OutputID = @FileID; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; COMMIT TRANSACTION spPOST_File; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spPOST_File; IF @Error_state = 100 BEGIN PRINT @Comment; END ELSE IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactionUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_File' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to store a new file or change an existing file'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID - maybe empty string to store files on Clusterlevel'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID - maybe empty string to store files on Factorylevel'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'ProductID - maybe empty string to store files on ProductLineLevel'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FileID'; SET @value = N'FileID - use * to autocreate File ID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FileName'; SET @value = N'FileName - username for the file'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FileExtension'; SET @value = N'FileExtension - may be empty'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FileBody'; SET @value = N'FileBody - must not be NULL'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Filepath'; SET @value = N'Filepath - optional virtual file path'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_Products_JSON; GO /* Procedure to POST Products in JSON Format Preconditions - A value can be sended only on full qualified single product -> There is no write on summary level possible - multiple values sended for one product -> the last wins Functionality: - its only possible to change the attributes of an existing products - creating new products is not possible - not sended attributes must be sended as NULL - to delete an attribute value, an empty string must be sended - user can send only values to products with write rights - the Procedure returns all sended products as recordset with the process result Workflow - put all sended records in a temporary table (SendedRecords) - create a temporary classification table with all sended Products (SendedRecordClassification) - determine the UserRights in Classification table - flag not existing Products - return SendedRecords Gerd Tautenhahn / Maria Rüdger for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall DECLARE @RC INT; DECLARE @ValuesJSON NVARCHAR(MAX); SET @ValuesJSON = N'{ "connector_type":"write","version":1,"Values":[ {"ProductNumber":1 ,"FactoryID":"ZT" ,"ProductLineID":"U" ,"ProductID":"1" ,"NameShort":null ,"CommentUser":"" ,"ResponsiblePerson":null ,"ImageName":"" ,"Status":"" ,"Template":"Test_VM" ,"GlobalAttribute1":"" ,"GlobalAttribute2":"" ,"GlobalAttribute3":"" ,"GlobalAttribute4":"" ,"GlobalAttribute5":"" ,"GlobalAttribute6":"" ,"GlobalAttribute7":"" ,"GlobalAttribute8":"" ,"GlobalAttribute9":"" ,"GlobalAttribute10":"" ,"GlobalAttribute11":"" ,"GlobalAttribute12":"" ,"GlobalAttribute13":"" ,"GlobalAttribute14":"" ,"GlobalAttribute15":"" ,"GlobalAttribute16":"" ,"GlobalAttribute17":"" ,"GlobalAttribute18":"" ,"GlobalAttribute19":"" ,"GlobalAttribute20":"" ,"GlobalAttribute21":"" ,"GlobalAttribute22":"" ,"GlobalAttribute23":"" ,"GlobalAttribute24":"" ,"GlobalAttribute25":"" } ,{"ProductNumber":2 ,"FactoryID":"ZT" ,"ProductLineID":"U" ,"ProductID":"2" ,"NameShort":"" ,"CommentUser":"" ,"ResponsiblePerson":"" ,"ImageName":"" ,"Status":"" ,"Template":"Test" ,"GlobalAttribute1":"" ,"GlobalAttribute2":"" ,"GlobalAttribute3":"" ,"GlobalAttribute4":"" ,"GlobalAttribute5":"" ,"GlobalAttribute6":"" ,"GlobalAttribute7":"" ,"GlobalAttribute8":"" ,"GlobalAttribute9":"" ,"GlobalAttribute10":"" ,"GlobalAttribute11":"" ,"GlobalAttribute12":"" ,"GlobalAttribute13":"" ,"GlobalAttribute14":"" ,"GlobalAttribute15":"" ,"GlobalAttribute16":"" ,"GlobalAttribute17":"" ,"GlobalAttribute18":"" ,"GlobalAttribute19":"" ,"GlobalAttribute20":"" ,"GlobalAttribute21":"" ,"GlobalAttribute22":"" ,"GlobalAttribute23":"" ,"GlobalAttribute24":"" ,"GlobalAttribute25":"" } ,{"ProductNumber":3 ,"FactoryID":"ZT" ,"ProductLineID":"U" ,"ProductID":"3" ,"NameShort":"K3" ,"CommentUser":"" ,"ResponsiblePerson":"" ,"ImageName":"" ,"Status":"" ,"Template":null ,"GlobalAttribute1":"" ,"GlobalAttribute2":"" ,"GlobalAttribute3":"" ,"GlobalAttribute4":"" ,"GlobalAttribute5":"" ,"GlobalAttribute6":"" ,"GlobalAttribute7":"" ,"GlobalAttribute8":"" ,"GlobalAttribute9":"" ,"GlobalAttribute10":"" ,"GlobalAttribute11":"" ,"GlobalAttribute12":"" ,"GlobalAttribute13":"" ,"GlobalAttribute14":"" ,"GlobalAttribute15":"" ,"GlobalAttribute16":"" ,"GlobalAttribute17":"" ,"GlobalAttribute18":"" ,"GlobalAttribute19":"" ,"GlobalAttribute20":"" ,"GlobalAttribute21":"" ,"GlobalAttribute22":"" ,"GlobalAttribute23":"" ,"GlobalAttribute24":"" ,"GlobalAttribute25":"" } ,{"ProductNumber":4 ,"FactoryID":"ZT" ,"ProductLineID":"U" ,"ProductID":"4" ,"NameShort":"BETR" ,"CommentUser":"" ,"ResponsiblePerson":"" ,"ImageName":"" ,"Status":"" ,"Template":"Test_VM" ,"GlobalAttribute1":"" ,"GlobalAttribute2":"" ,"GlobalAttribute3":"" ,"GlobalAttribute4":"" ,"GlobalAttribute5":"" ,"GlobalAttribute6":"" ,"GlobalAttribute7":"" ,"GlobalAttribute8":"" ,"GlobalAttribute9":"" ,"GlobalAttribute10":"" ,"GlobalAttribute11":"" ,"GlobalAttribute12":"" ,"GlobalAttribute13":"" ,"GlobalAttribute14":"" ,"GlobalAttribute15":"" ,"GlobalAttribute16":"" ,"GlobalAttribute17":"" ,"GlobalAttribute18":"" ,"GlobalAttribute19":"" ,"GlobalAttribute20":"" ,"GlobalAttribute21":"" ,"GlobalAttribute22":"" ,"GlobalAttribute23":"" ,"GlobalAttribute24":"" ,"GlobalAttribute25":"" } ,{"ProductNumber":5 ,"FactoryID":"ZT" ,"ProductLineID":"U" ,"ProductID":"4" ,"NameShort":"BEM" ,"CommentUser":"" ,"ResponsiblePerson":"" ,"ImageName":"" ,"Status":"" ,"Template":"Test_V" ,"GlobalAttribute1":"" ,"GlobalAttribute2":"" ,"GlobalAttribute3":"" ,"GlobalAttribute4":"" ,"GlobalAttribute5":"" ,"GlobalAttribute6":"" ,"GlobalAttribute7":"" ,"GlobalAttribute8":"" ,"GlobalAttribute9":"" ,"GlobalAttribute10":"" ,"GlobalAttribute11":"" ,"GlobalAttribute12":"" ,"GlobalAttribute13":"" ,"GlobalAttribute14":"" ,"GlobalAttribute15":"" ,"GlobalAttribute16":"" ,"GlobalAttribute17":"" ,"GlobalAttribute18":"" ,"GlobalAttribute19":"" ,"GlobalAttribute20":"" ,"GlobalAttribute21":"" ,"GlobalAttribute22":"" ,"GlobalAttribute23":"" ,"GlobalAttribute24":"" ,"GlobalAttribute25":"" } ,{"ProductNumber":6 ,"FactoryID":"ZT" ,"ProductLineID":"U" ,"ProductID":"7" ,"NameShort":"STAT_BUD" ,"CommentUser":"" ,"ResponsiblePerson":"" ,"ImageName":"" ,"Status":"" ,"Template":"" ,"GlobalAttribute1":"" ,"GlobalAttribute2":"" ,"GlobalAttribute3":"" ,"GlobalAttribute4":"" ,"GlobalAttribute5":"" ,"GlobalAttribute6":"" ,"GlobalAttribute7":"" ,"GlobalAttribute8":"" ,"GlobalAttribute9":"" ,"GlobalAttribute10":"" ,"GlobalAttribute11":"" ,"GlobalAttribute12":"" ,"GlobalAttribute13":"" ,"GlobalAttribute14":"" ,"GlobalAttribute15":"" ,"GlobalAttribute16":"" ,"GlobalAttribute17":"" ,"GlobalAttribute18":"" ,"GlobalAttribute19":"" ,"GlobalAttribute20":"" ,"GlobalAttribute21":"" ,"GlobalAttribute22":"" ,"GlobalAttribute23":"" ,"GlobalAttribute24":"" ,"GlobalAttribute25":"" } ] }'; EXEC @RC = planning.spPOST_Products_JSON 'SQL', @ValuesJSON; PRINT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Products_JSON',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Products_JSON','PARAMETER',NULL); */ CREATE PROCEDURE planning.spPOST_Products_JSON @Username NVARCHAR(255) = '' ,@ValuesJSON NVARCHAR(MAX) = '' AS BEGIN SET NOCOUNT ON; -- Logging Variables DECLARE @TransactionUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''', '''+ ISNULL(@ValuesJSON, N'NULL')+ N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ValuesJSON IS NULL SET @ValuesJSON = N''; BEGIN TRY BEGIN TRANSACTION spPOST_Products_JSON -- INPUT PROTECTION ################################################################################# SET @Username = system.fProtectString (@Username); -- Test for valid JSON ################################################################################ IF @ValuesJSON = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; IF ISJSON(@ValuesJSON) = 0 BEGIN SET @ResultCode = 403; THROW 60000, N'Exit due to invalid JSON (technical)', 100; END; -- Extract Values FROM JSON and put them into #SendedRecords ##################################### DROP TABLE IF EXISTS #SendedRecords ;WITH vals AS ( SELECT ProductNumber AS 'ProductNumber' ,system.fProtectID (FactoryID) AS 'FactoryID' ,system.fProtectID (ProductLineID) AS 'ProductLineID' ,system.fProtectID (ProductID) AS 'ProductID' ,NameShort AS 'NameShort' ,CommentUser AS 'CommentUser' ,ResponsiblePerson AS 'ResponsiblePerson' ,ImageName AS 'ImageName' ,[Status] AS 'Status' ,Template AS 'Template' ,GlobalAttribute1 AS 'GlobalAttribute1' ,GlobalAttribute2 AS 'GlobalAttribute2' ,GlobalAttribute3 AS 'GlobalAttribute3' ,GlobalAttribute4 AS 'GlobalAttribute4' ,GlobalAttribute5 AS 'GlobalAttribute5' ,GlobalAttribute6 AS 'GlobalAttribute6' ,GlobalAttribute7 AS 'GlobalAttribute7' ,GlobalAttribute8 AS 'GlobalAttribute8' ,GlobalAttribute9 AS 'GlobalAttribute9' ,GlobalAttribute10 AS 'GlobalAttribute10' ,GlobalAttribute11 AS 'GlobalAttribute11' ,GlobalAttribute12 AS 'GlobalAttribute12' ,GlobalAttribute13 AS 'GlobalAttribute13' ,GlobalAttribute14 AS 'GlobalAttribute14' ,GlobalAttribute15 AS 'GlobalAttribute15' ,GlobalAttribute16 AS 'GlobalAttribute16' ,GlobalAttribute17 AS 'GlobalAttribute17' ,GlobalAttribute18 AS 'GlobalAttribute18' ,GlobalAttribute19 AS 'GlobalAttribute19' ,GlobalAttribute20 AS 'GlobalAttribute20' ,GlobalAttribute21 AS 'GlobalAttribute21' ,GlobalAttribute22 AS 'GlobalAttribute22' ,GlobalAttribute23 AS 'GlobalAttribute23' ,GlobalAttribute24 AS 'GlobalAttribute24' ,GlobalAttribute25 AS 'GlobalAttribute25' FROM OPENJSON (@ValuesJSON, N'$.Values') WITH ( ProductNumber INT N'$.ProductNumber' ,FactoryID NVARCHAR(255) N'$.FactoryID' ,ProductLineID NVARCHAR(255) N'$.ProductLineID' ,ProductID NVARCHAR(255) N'$.ProductID' ,NameShort NVARCHAR(255) N'$.NameShort' ,CommentUser NVARCHAR(255) N'$.CommentUser' ,ResponsiblePerson NVARCHAR(255) N'$.ResponsiblePerson' ,ImageName NVARCHAR(255) N'$.ImageName' ,Status NVARCHAR(255) N'$.Status' ,Template NVARCHAR(255) N'$.Template' ,GlobalAttribute1 NVARCHAR(255) N'$.GlobalAttribute1' ,GlobalAttribute2 NVARCHAR(255) N'$.GlobalAttribute2' ,GlobalAttribute3 NVARCHAR(255) N'$.GlobalAttribute3' ,GlobalAttribute4 NVARCHAR(255) N'$.GlobalAttribute4' ,GlobalAttribute5 NVARCHAR(255) N'$.GlobalAttribute5' ,GlobalAttribute6 NVARCHAR(255) N'$.GlobalAttribute6' ,GlobalAttribute7 NVARCHAR(255) N'$.GlobalAttribute7' ,GlobalAttribute8 NVARCHAR(255) N'$.GlobalAttribute8' ,GlobalAttribute9 NVARCHAR(255) N'$.GlobalAttribute9' ,GlobalAttribute10 NVARCHAR(255) N'$.GlobalAttribute10' ,GlobalAttribute11 NVARCHAR(255) N'$.GlobalAttribute11' ,GlobalAttribute12 NVARCHAR(255) N'$.GlobalAttribute12' ,GlobalAttribute13 NVARCHAR(255) N'$.GlobalAttribute13' ,GlobalAttribute14 NVARCHAR(255) N'$.GlobalAttribute14' ,GlobalAttribute15 NVARCHAR(255) N'$.GlobalAttribute15' ,GlobalAttribute16 NVARCHAR(255) N'$.GlobalAttribute16' ,GlobalAttribute17 NVARCHAR(255) N'$.GlobalAttribute17' ,GlobalAttribute18 NVARCHAR(255) N'$.GlobalAttribute18' ,GlobalAttribute19 NVARCHAR(255) N'$.GlobalAttribute19' ,GlobalAttribute20 NVARCHAR(255) N'$.GlobalAttribute20' ,GlobalAttribute21 NVARCHAR(255) N'$.GlobalAttribute21' ,GlobalAttribute22 NVARCHAR(255) N'$.GlobalAttribute22' ,GlobalAttribute23 NVARCHAR(255) N'$.GlobalAttribute23' ,GlobalAttribute24 NVARCHAR(255) N'$.GlobalAttribute24' ,GlobalAttribute25 NVARCHAR(255) N'$.GlobalAttribute25' ) ) SELECT ProductNumber ,FactoryID COLLATE DATABASE_DEFAULT AS FactoryID ,ProductLineID COLLATE DATABASE_DEFAULT AS ProductLineID ,ProductID COLLATE DATABASE_DEFAULT AS ProductID ,NameShort COLLATE DATABASE_DEFAULT AS NameShort ,CommentUser COLLATE DATABASE_DEFAULT AS CommentUser ,ResponsiblePerson COLLATE DATABASE_DEFAULT AS ResponsiblePerson ,ImageName COLLATE DATABASE_DEFAULT AS ImageName ,[Status] COLLATE DATABASE_DEFAULT AS [Status] ,Template COLLATE DATABASE_DEFAULT AS Template ,GlobalAttribute1 COLLATE DATABASE_DEFAULT AS GlobalAttribute1 ,GlobalAttribute2 COLLATE DATABASE_DEFAULT AS GlobalAttribute2 ,GlobalAttribute3 COLLATE DATABASE_DEFAULT AS GlobalAttribute3 ,GlobalAttribute4 COLLATE DATABASE_DEFAULT AS GlobalAttribute4 ,GlobalAttribute5 COLLATE DATABASE_DEFAULT AS GlobalAttribute5 ,GlobalAttribute6 COLLATE DATABASE_DEFAULT AS GlobalAttribute6 ,GlobalAttribute7 COLLATE DATABASE_DEFAULT AS GlobalAttribute7 ,GlobalAttribute8 COLLATE DATABASE_DEFAULT AS GlobalAttribute8 ,GlobalAttribute9 COLLATE DATABASE_DEFAULT AS GlobalAttribute9 ,GlobalAttribute10 COLLATE DATABASE_DEFAULT AS GlobalAttribute10 ,GlobalAttribute11 COLLATE DATABASE_DEFAULT AS GlobalAttribute11 ,GlobalAttribute12 COLLATE DATABASE_DEFAULT AS GlobalAttribute12 ,GlobalAttribute13 COLLATE DATABASE_DEFAULT AS GlobalAttribute13 ,GlobalAttribute14 COLLATE DATABASE_DEFAULT AS GlobalAttribute14 ,GlobalAttribute15 COLLATE DATABASE_DEFAULT AS GlobalAttribute15 ,GlobalAttribute16 COLLATE DATABASE_DEFAULT AS GlobalAttribute16 ,GlobalAttribute17 COLLATE DATABASE_DEFAULT AS GlobalAttribute17 ,GlobalAttribute18 COLLATE DATABASE_DEFAULT AS GlobalAttribute18 ,GlobalAttribute19 COLLATE DATABASE_DEFAULT AS GlobalAttribute19 ,GlobalAttribute20 COLLATE DATABASE_DEFAULT AS GlobalAttribute20 ,GlobalAttribute21 COLLATE DATABASE_DEFAULT AS GlobalAttribute21 ,GlobalAttribute22 COLLATE DATABASE_DEFAULT AS GlobalAttribute22 ,GlobalAttribute23 COLLATE DATABASE_DEFAULT AS GlobalAttribute23 ,GlobalAttribute24 COLLATE DATABASE_DEFAULT AS GlobalAttribute24 ,GlobalAttribute25 COLLATE DATABASE_DEFAULT AS GlobalAttribute25 INTO #SendedRecords FROM vals; -- Debug: -- SELECT * FROM #SendedRecords -- Check for logical correct values ############################################# PRINT 'Starting Check for logical correct Values' -- no NULL Values contained in IDs IF EXISTS ( SELECT TOP(1) 1 FROM #SendedRecords WHERE ProductNumber IS NULL OR FactoryID IS NULL OR ProductLineID IS NULL OR ProductID IS NULL OR FactoryID = N'' OR ProductLineID = N'' OR ProductID = N'' ) BEGIN SET @ResultCode = 403; THROW 60000, N'Exit due to invalid Factory/PL/P IDs.', 100; END; -- no doubled ProductNumbers contained IF EXISTS ( SELECT TOP (1) 1 FROM #SendedRecords GROUP BY ProductNumber HAVING COUNT(ProductNumber) > 1 ) BEGIN SET @ResultCode = 403; THROW 60000, N'Exit due to duplicate ProductNumbers.', 100; END; -- User Rights ################################################################## PRINT 'Starting User Rights Check' -- Determine transaction user SELECT @TransactionUsername = system.fDetermineTransactionUsername (@Username); IF @TransactionUsername = N'403' BEGIN SET @ResultCode = 403; THROW 60000, N'Transaction user don`t exists', 100; END; -- Build classification Table for sended Records ################################# PRINT 'Starting classification Process' DROP TABLE IF EXISTS #SendedRecordsClassification; CREATE TABLE #SendedRecordsClassification ( ProductNumber INT NOT NULL ,FactoryID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,NameShort NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,CommentUser NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,ResponsiblePerson NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,ImageName NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,Status NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,Template NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute1 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute2 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute3 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute4 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute5 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute6 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute7 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute8 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute9 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute10 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute11 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute12 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute13 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute14 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute15 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute16 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute17 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute18 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute19 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute20 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute21 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute22 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute23 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute24 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute25 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,UserRight NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,InvalidTemplateFlag INT NOT NULL ,TargetIDsNotExistingFlag INT NOT NULL ,MultipleRecordsFlag INT NOT NULL -- if more than one record per Product is sent, only the last one is posted ,ReturnCode INT NOT NULL ,StatusComment NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductKey BIGINT NOT NULL ,SaveRecordFlag INT NOT NULL ); INSERT INTO #SendedRecordsClassification SELECT ProductNumber AS ValueNumber ,FactoryID COLLATE DATABASE_DEFAULT AS FactoryID ,ProductLineID COLLATE DATABASE_DEFAULT AS ProductLineID ,ProductID COLLATE DATABASE_DEFAULT AS ProductID ,NameShort COLLATE DATABASE_DEFAULT AS NameShort ,CommentUser COLLATE DATABASE_DEFAULT AS CommentUser ,ResponsiblePerson COLLATE DATABASE_DEFAULT AS ResponsiblePerson ,ImageName COLLATE DATABASE_DEFAULT AS ImageName ,Status COLLATE DATABASE_DEFAULT AS Status ,Template COLLATE DATABASE_DEFAULT AS Template ,GlobalAttribute1 COLLATE DATABASE_DEFAULT AS GlobalAttribute1 ,GlobalAttribute2 COLLATE DATABASE_DEFAULT AS GlobalAttribute2 ,GlobalAttribute3 COLLATE DATABASE_DEFAULT AS GlobalAttribute3 ,GlobalAttribute4 COLLATE DATABASE_DEFAULT AS GlobalAttribute4 ,GlobalAttribute5 COLLATE DATABASE_DEFAULT AS GlobalAttribute5 ,GlobalAttribute6 COLLATE DATABASE_DEFAULT AS GlobalAttribute6 ,GlobalAttribute7 COLLATE DATABASE_DEFAULT AS GlobalAttribute7 ,GlobalAttribute8 COLLATE DATABASE_DEFAULT AS GlobalAttribute8 ,GlobalAttribute9 COLLATE DATABASE_DEFAULT AS GlobalAttribute9 ,GlobalAttribute10 COLLATE DATABASE_DEFAULT AS GlobalAttribute10 ,GlobalAttribute11 COLLATE DATABASE_DEFAULT AS GlobalAttribute11 ,GlobalAttribute12 COLLATE DATABASE_DEFAULT AS GlobalAttribute12 ,GlobalAttribute13 COLLATE DATABASE_DEFAULT AS GlobalAttribute13 ,GlobalAttribute14 COLLATE DATABASE_DEFAULT AS GlobalAttribute14 ,GlobalAttribute15 COLLATE DATABASE_DEFAULT AS GlobalAttribute15 ,GlobalAttribute16 COLLATE DATABASE_DEFAULT AS GlobalAttribute16 ,GlobalAttribute17 COLLATE DATABASE_DEFAULT AS GlobalAttribute17 ,GlobalAttribute18 COLLATE DATABASE_DEFAULT AS GlobalAttribute18 ,GlobalAttribute19 COLLATE DATABASE_DEFAULT AS GlobalAttribute19 ,GlobalAttribute20 COLLATE DATABASE_DEFAULT AS GlobalAttribute20 ,GlobalAttribute21 COLLATE DATABASE_DEFAULT AS GlobalAttribute21 ,GlobalAttribute22 COLLATE DATABASE_DEFAULT AS GlobalAttribute22 ,GlobalAttribute23 COLLATE DATABASE_DEFAULT AS GlobalAttribute23 ,GlobalAttribute24 COLLATE DATABASE_DEFAULT AS GlobalAttribute24 ,GlobalAttribute25 COLLATE DATABASE_DEFAULT AS GlobalAttribute25 ,'' AS UserRight ,0 AS InvalidTemplateFlag ,0 AS TargetIDsNotExistingFlag ,0 AS MultipleRecordsFlag ,501 AS ReturnCode ,N'Original' COLLATE DATABASE_DEFAULT AS StatusComment ,0 AS ProductKey ,0 AS SaveRecordFlag FROM #SendedRecords; -- add ProductKey and therefore check for the existence of the targeted products UPDATE SRC SET SRC.ProductKey = dp.ProductKey FROM #SendedRecordsClassification SRC INNER JOIN planning.tdProducts dp -- only for hits ON SRC.FactoryID = dp.FactoryID AND SRC.ProductLineID = dp.ProductLineID AND SRC.ProductID = dp.ProductID -- Flag target products which don't exist UPDATE #SendedRecordsClassification SET TargetIDsNotExistingFlag = 1 ,ReturnCode = 404 ,StatusComment = N'Error: Product does not exist' ,SaveRecordFlag = -1 WHERE ProductKey = 0; -- determine UserRights UPDATE SRC SET SRC.UserRight = tUR.[Right] FROM #SendedRecordsClassification SRC LEFT JOIN system.trUserRights tUR ON SRC.FactoryID = tUR.FactoryID AND SRC.ProductLineID = tUR.ProductLineID AND tUR.UserName = @TransactionUsername WHERE tUR.[Right] IS NOT NULL; UPDATE #SendedRecordsClassification SET SaveRecordFlag = -1 ,ReturnCode = 403 ,StatusComment = 'No write rights.' WHERE UserRight <> 'Write' AND SaveRecordFlag <> -1; -- multiple records per product; only the last one will be posted UPDATE #SendedRecordsClassification SET MultipleRecordsFlag = 1 ,ReturnCode = IIF(mf.Anzahl > 1 AND mf.Anzahl = mf.Zeile,200,204) ,StatusComment = IIF(mf.Anzahl > 1 AND mf.Anzahl = mf.Zeile,'Saved record (Multiple records per product)','Multiple records per product, only the last one has been posted') ,SaveRecordFlag = IIF(mf.Anzahl > 1 AND mf.Anzahl = mf.Zeile,0,-1) FROM #SendedRecordsClassification SRC RIGHT JOIN ( SELECT ProductNumber ,FactoryID ,ProductLineID ,ProductID ,ProductKey ,COUNT(ProductKey) OVER(PARTITION BY ProductKey) AS Anzahl ,ROW_NUMBER() OVER(PARTITION BY ProductKey ORDER BY ProductNumber ASC) AS Zeile FROM #SendedRecordsClassification WHERE ProductKey IS NOT NULL AND SaveRecordFlag <> -1) mf ON SRC.ProductNumber = mf.ProductNumber WHERE mf.Anzahl > 1 -- Flag invalid template types (valid are: _VM, _VT, _VW, _VJ, _HM, _HT, _HW, _HJ); old type will be kept (rest will still be posted) UPDATE #SendedRecordsClassification SET Template = NULL ,InvalidTemplateFlag = 1 ,ReturnCode = 200 ,StatusComment = IIF(MultipleRecordsFlag = 1, 'Partially saved record (Invalid template type, old type is kept; multiple records per product)','Partially saved record (Invalid template type, old type is kept)') ,SaveRecordFlag = 1 FROM #SendedRecordsClassification SRC WHERE SRC.Template IS NOT NULL AND (LEFT(RIGHT(SRC.Template,3),2) NOT IN ('_V', '_H') OR RIGHT(SRC.Template,1) NOT IN ('M','T','W','J')) AND SRC.SaveRecordFlag <> -1 -- Flag all remaining records as to be saved UPDATE #SendedRecordsClassification SET SaveRecordFlag = 1 ,ReturnCode = 200 ,StatusComment = IIF(MultipleRecordsFlag = 1, StatusComment,'Saved record') WHERE SaveRecordFlag = 0; -- Debug: -- SELECT * FROM #SendedRecordsClassification src -- Determination of the final record set and update of product values ####################################################### DROP TABLE IF EXISTS #SavedRecords; CREATE TABLE #SavedRecords ( ProductNumber BIGINT NOT NULL ,ProductKey BIGINT NOT NULL ,NameShort NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,CommentUser NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ResponsiblePerson NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ImageName NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,Status NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,Template NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute1 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute2 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute3 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute4 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute5 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute6 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute7 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute8 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute9 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute10 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute11 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute12 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute13 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute14 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute15 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute16 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute17 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute18 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute19 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute20 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute21 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute22 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute23 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute24 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,GlobalAttribute25 NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ) -- old values in case of NULL PRINT 'Determination of old values for sended NULL values' INSERT INTO #SavedRecords SELECT SRC.ProductNumber ,SRC.ProductKey ,IIF(SRC.NameShort IS NULL, dp.NameShort , SRC.NameShort ) ,IIF(SRC.CommentUser IS NULL, dp.CommentUser , SRC.CommentUser ) ,IIF(SRC.ResponsiblePerson IS NULL, dp.ResponsiblePerson, SRC.ResponsiblePerson ) ,IIF(SRC.ImageName IS NULL, dp.ImageName , SRC.ImageName ) ,IIF(SRC.Status IS NULL, dp.Status , SRC.Status ) ,IIF(SRC.Template IS NULL, dp.Template , SRC.Template ) ,IIF(SRC.GlobalAttribute1 IS NULL, dp.GlobalAttribute1 , SRC.GlobalAttribute1 ) ,IIF(SRC.GlobalAttribute2 IS NULL, dp.GlobalAttribute2 , SRC.GlobalAttribute2 ) ,IIF(SRC.GlobalAttribute3 IS NULL, dp.GlobalAttribute3 , SRC.GlobalAttribute3 ) ,IIF(SRC.GlobalAttribute4 IS NULL, dp.GlobalAttribute4 , SRC.GlobalAttribute4 ) ,IIF(SRC.GlobalAttribute5 IS NULL, dp.GlobalAttribute5 , SRC.GlobalAttribute5 ) ,IIF(SRC.GlobalAttribute6 IS NULL, dp.GlobalAttribute6 , SRC.GlobalAttribute6 ) ,IIF(SRC.GlobalAttribute7 IS NULL, dp.GlobalAttribute7 , SRC.GlobalAttribute7 ) ,IIF(SRC.GlobalAttribute8 IS NULL, dp.GlobalAttribute8 , SRC.GlobalAttribute8 ) ,IIF(SRC.GlobalAttribute9 IS NULL, dp.GlobalAttribute9 , SRC.GlobalAttribute9 ) ,IIF(SRC.GlobalAttribute10 IS NULL, dp.GlobalAttribute10, SRC.GlobalAttribute10 ) ,IIF(SRC.GlobalAttribute11 IS NULL, dp.GlobalAttribute11, SRC.GlobalAttribute11 ) ,IIF(SRC.GlobalAttribute12 IS NULL, dp.GlobalAttribute12, SRC.GlobalAttribute12 ) ,IIF(SRC.GlobalAttribute13 IS NULL, dp.GlobalAttribute13, SRC.GlobalAttribute13 ) ,IIF(SRC.GlobalAttribute14 IS NULL, dp.GlobalAttribute14, SRC.GlobalAttribute14 ) ,IIF(SRC.GlobalAttribute15 IS NULL, dp.GlobalAttribute15, SRC.GlobalAttribute15 ) ,IIF(SRC.GlobalAttribute16 IS NULL, dp.GlobalAttribute16, SRC.GlobalAttribute16 ) ,IIF(SRC.GlobalAttribute17 IS NULL, dp.GlobalAttribute17, SRC.GlobalAttribute17 ) ,IIF(SRC.GlobalAttribute18 IS NULL, dp.GlobalAttribute18, SRC.GlobalAttribute18 ) ,IIF(SRC.GlobalAttribute19 IS NULL, dp.GlobalAttribute19, SRC.GlobalAttribute19 ) ,IIF(SRC.GlobalAttribute20 IS NULL, dp.GlobalAttribute20, SRC.GlobalAttribute20 ) ,IIF(SRC.GlobalAttribute21 IS NULL, dp.GlobalAttribute21, SRC.GlobalAttribute21 ) ,IIF(SRC.GlobalAttribute22 IS NULL, dp.GlobalAttribute22, SRC.GlobalAttribute22 ) ,IIF(SRC.GlobalAttribute23 IS NULL, dp.GlobalAttribute23, SRC.GlobalAttribute23 ) ,IIF(SRC.GlobalAttribute24 IS NULL, dp.GlobalAttribute24, SRC.GlobalAttribute24 ) ,IIF(SRC.GlobalAttribute25 IS NULL, dp.GlobalAttribute25, SRC.GlobalAttribute25 ) FROM #SendedRecordsClassification SRC LEFT JOIN planning.tdProducts dp ON SRC.ProductKey = dp.ProductKey WHERE SRC.SaveRecordFlag = 1 -- Insert new Values PRINT 'Update of products' UPDATE dp SET dp.NameShort = SR.NameShort ,dp.CommentUser = SR.CommentUser ,dp.ResponsiblePerson = SR.ResponsiblePerson ,dp.ImageName = SR.ImageName ,dp.Status = SR.Status ,dp.Template = SR.Template ,dp.GlobalAttribute1 = SR.GlobalAttribute1 ,dp.GlobalAttribute2 = SR.GlobalAttribute2 ,dp.GlobalAttribute3 = SR.GlobalAttribute3 ,dp.GlobalAttribute4 = SR.GlobalAttribute4 ,dp.GlobalAttribute5 = SR.GlobalAttribute5 ,dp.GlobalAttribute6 = SR.GlobalAttribute6 ,dp.GlobalAttribute7 = SR.GlobalAttribute7 ,dp.GlobalAttribute8 = SR.GlobalAttribute8 ,dp.GlobalAttribute9 = SR.GlobalAttribute9 ,dp.GlobalAttribute10 = SR.GlobalAttribute10 ,dp.GlobalAttribute11 = SR.GlobalAttribute11 ,dp.GlobalAttribute12 = SR.GlobalAttribute12 ,dp.GlobalAttribute13 = SR.GlobalAttribute13 ,dp.GlobalAttribute14 = SR.GlobalAttribute14 ,dp.GlobalAttribute15 = SR.GlobalAttribute15 ,dp.GlobalAttribute16 = SR.GlobalAttribute16 ,dp.GlobalAttribute17 = SR.GlobalAttribute17 ,dp.GlobalAttribute18 = SR.GlobalAttribute18 ,dp.GlobalAttribute19 = SR.GlobalAttribute19 ,dp.GlobalAttribute20 = SR.GlobalAttribute20 ,dp.GlobalAttribute21 = SR.GlobalAttribute21 ,dp.GlobalAttribute22 = SR.GlobalAttribute22 ,dp.GlobalAttribute23 = SR.GlobalAttribute23 ,dp.GlobalAttribute24 = SR.GlobalAttribute24 ,dp.GlobalAttribute25 = SR.GlobalAttribute25 FROM planning.tdproducts dp INNER JOIN #SavedRecords SR ON dp.ProductKey = SR.ProductKey -- Create result table to be returned ################################################# DROP TABLE IF EXISTS #ResultSet; CREATE TABLE #ResultSet ( ProductNumber INT NOT NULL ,FactoryID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,NameShort NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,CommentUser NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,ResponsiblePerson NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,ImageName NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,[Status] NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,Template NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute1 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute2 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute3 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute4 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute5 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute6 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute7 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute8 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute9 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute10 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute11 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute12 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute13 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute14 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute15 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute16 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute17 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute18 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute19 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute20 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute21 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute22 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute23 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute24 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,GlobalAttribute25 NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,ReturnCode INT NULL ,ReturnComment NVARCHAR(4000) COLLATE DATABASE_DEFAULT NULL ); -- Fill result table with requested values PRINT 'Start filling result set.' INSERT INTO #ResultSet SELECT ProductNumber ,FactoryID ,ProductLineID ,ProductID ,NameShort ,CommentUser ,ResponsiblePerson ,ImageName ,[Status] ,Template ,GlobalAttribute1 ,GlobalAttribute2 ,GlobalAttribute3 ,GlobalAttribute4 ,GlobalAttribute5 ,GlobalAttribute6 ,GlobalAttribute7 ,GlobalAttribute8 ,GlobalAttribute9 ,GlobalAttribute10 ,GlobalAttribute11 ,GlobalAttribute12 ,GlobalAttribute13 ,GlobalAttribute14 ,GlobalAttribute15 ,GlobalAttribute16 ,GlobalAttribute17 ,GlobalAttribute18 ,GlobalAttribute19 ,GlobalAttribute20 ,GlobalAttribute21 ,GlobalAttribute22 ,GlobalAttribute23 ,GlobalAttribute24 ,GlobalAttribute25 ,0 ,'' FROM #SendedRecords; -- Set Return Codes an comments PRINT 'Setting ReturnCodes in Result Set.' UPDATE RS SET RS.ReturnCode = SRC.ReturnCode ,RS.ReturnComment = SRC.StatusComment FROM #ResultSet RS LEFT JOIN #SendedRecordsClassification SRC ON RS.ProductNumber = SRC.ProductNumber; DECLARE @ProcessTime INT; SET @ProcessTime = DATEDIFF(ms, @TimestampCall, GETUTCDATE()); SET @ResultCode = 200; -- Return result SELECT * ,@ProcessTime AS ProcessTime FROM #ResultSet; COMMIT TRANSACTION spPOST_Products_JSON; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spPOST_Products_JSON; IF @Error_state = 100 BEGIN PRINT @Comment; END ELSE IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactionUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Products_JSON' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to POST Values in JSON Format'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValuesJSON'; SET @value = N'Valus which are to be saved as JSON object.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_TabLayout; GO /* Procedure to POST a layout for a tab Saxess Software GmbH Last modified: 09/2023 for OCT 5.10 Testcall DECLARE @RC INT; DECLARE @OutputID NVARCHAR(255); EXEC @RC = planning.spPOST_TabLayout @Username = 'SQL' ,@FactoryID = 'ZT' ,@ProductLineID = '' ,@TabID = 'SF' ,@LayoutID = '*' ,@LayoutName = 'Test Layout nummer 2' ,@LayoutJSON = '{}' ,@OrderIndex = 2 ,@LayoutOwner = '' ,@OutputID = @OutputID OUTPUT; SELECT @RC; SELECT @OutputID; SELECT * FROM planning.tTabLayouts ORDER BY RowKey DESC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_TabLayout',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_TabLayout','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_TabLayout @Username NVARCHAR(255) , @FactoryID NVARCHAR(255) = '' , @ProductLineID NVARCHAR(255) = '' , @TabID NVARCHAR(255) , @LayoutID NVARCHAR(255) , @LayoutName NVARCHAR(255) , @LayoutJSON NVARCHAR(MAX) , @OrderIndex INT , @LayoutOwner NVARCHAR(255) , @OutputID NVARCHAR(255) = NULL OUTPUT AS BEGIN BEGIN TRY BEGIN TRANSACTION planning_spPOST_TabLayout; -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 9, @Username, @FactoryID, @ProductLineID, @TabID, @LayoutID, @LayoutName, @LayoutJSON, @OrderIndex, @LayoutOwner; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Parameter protection SET @Username = COALESCE(@Username, N''); SET @FactoryID = system.fProtectID(COALESCE(@FactoryID, N'')); SET @ProductLineID = system.fProtectID(COALESCE(@ProductLineID, N'')); SET @TabID = system.fProtectID(COALESCE(@TabID, N'')); SET @LayoutID = system.fProtectID(COALESCE(@LayoutID, N'')); SET @LayoutName = COALESCE(@LayoutName, N''); SET @LayoutJSON = COALESCE(@LayoutJSON, N''); SET @OrderIndex = COALESCE(@OrderIndex, 999); SET @LayoutOwner = COALESCE(@LayoutOwner, N''); -- Variables DECLARE @SettingUserEditPublicLayouts BIT; DECLARE @SettingPrivateLayouts BIT; DECLARE @SettingMultipleLayouts BIT; DECLARE @IsUserAdministrator BIT; DECLARE @IsPublicLayout BIT = CASE WHEN @LayoutOwner = N'' THEN 1 ELSE 0 END; -- Get setting: regular users can edit public layouts? SELECT @SettingUserEditPublicLayouts = COALESCE(ValueInt, 0) FROM system.fGET_Setting('TabPreferencesUserEditPublicLayouts'); -- Get setting: are private layouts allowed in general? SELECT @SettingPrivateLayouts = COALESCE(ValueInt, 1) FROM system.fGET_Setting('TabPreferencesPrivateLayouts'); -- Get setting: are multiple layouts allowed in general? SELECT @SettingMultipleLayouts = COALESCE(ValueInt, 1) FROM system.fGET_Setting('TabPreferencesMultipleLayouts'); -- Is user administrator? SELECT @IsUserAdministrator = IsAdministratorFlag FROM system.trUser WHERE UserName = @Username; -- Right rules: -- Administrators are allowed to create and edit all layouts -- Regular users can only create/edit private layouts -- Regular users can create/edit public layouts if the setting "UserEditPublicLayouts" = 1 but they need write rights on the location of the tab -- Regular users can only create/edit private layouts IF @IsUserAdministrator = 0 AND @SettingUserEditPublicLayouts = 0 AND @IsPublicLayout = 1 BEGIN RAISERROR('Insufficient rights to save this layout.', 16, 10); END -- Are private layouts allowed? IF @IsUserAdministrator = 0 AND @IsPublicLayout = 0 AND @SettingPrivateLayouts = 0 BEGIN RAISERROR('Private layouts are not allowed.', 16, 10); END -- Regular users can create/edit public layouts but need to have write rights IF @IsUserAdministrator = 0 AND @SettingUserEditPublicLayouts = 1 AND @IsPublicLayout = 1 BEGIN -- Cluster level IF @FactoryID = '' BEGIN EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Insufficient rights to save this layout.', 16, 10); END ELSE BEGIN -- Factory level IF @ProductLineID = '' BEGIN EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername, @FactoryID; IF @ResultCode <> 200 RAISERROR('Insufficient rights to save this layout.', 16, 10); END ELSE BEGIN -- ProductLine level EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername, @FactoryID, @ProductLineID; IF @ResultCode <> 200 RAISERROR('Insufficient rights to save this layout.', 16, 10); END END END -- New layout? IF @LayoutID = '*' OR NOT EXISTS(SELECT 1 FROM planning.tTabLayouts WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND TabID = @TabID AND LayoutID = @LayoutID) BEGIN IF NOT EXISTS(SELECT 1 FROM planning.tTabs WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND TabID = @TabID) RAISERROR('Tab doesn''t exist.', 16, 10); -- Get next free LayoutID IF @LayoutID = '*' BEGIN SELECT @LayoutID = COALESCE(MAX(CAST(LayoutID AS INT)), 0) + 1 FROM planning.tTabLayouts WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND TabID = @TabID; END -- Are multiple layouts allowed? IF @IsUserAdministrator = 0 AND @LayoutID > 1 AND @SettingMultipleLayouts = 0 BEGIN RAISERROR('Can''t create another layout because multiple layouts are not allowed.', 16, 10); END -- Insert new layout INSERT INTO planning.tTabLayouts (FactoryID, ProductLineID, TabID, LayoutID, LayoutName, LayoutJSON, OrderIndex, LayoutOwner) VALUES (@FactoryID, @ProductLineID, @TabID, @LayoutID, @LayoutName, @LayoutJSON, @OrderIndex, @LayoutOwner); END ELSE BEGIN -- Update existing layout UPDATE planning.tTabLayouts SET LayoutName = @LayoutName , LayoutJSON = @LayoutJSON , OrderIndex = @OrderIndex , LayoutOwner = @LayoutOwner WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND TabID = @TabID AND LayoutID = @LayoutID; END SET @AffectedRows = @@ROWCOUNT; SET @OutputID = @LayoutID; -- Rearrange OrderIndex WITH Layouts AS ( SELECT * , ROW_NUMBER() OVER (PARTITION BY LayoutOwner ORDER BY OrderIndex, CASE WHEN LayoutID = @LayoutID THEN 0 ELSE 1 END) AS NewOrderIndex FROM planning.tTabLayouts WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND TabID = @TabID ) UPDATE Layouts SET OrderIndex = NewOrderIndex; COMMIT TRANSACTION planning_spPOST_TabLayout; SET @ResultCode = 200 END TRY BEGIN CATCH ROLLBACK TRANSACTION planning_spPOST_TabLayout; SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_TabLayout' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure for POST Bulkload'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'optional FactoryID, skip / NULL / empty string if request is for Cluster level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'optional ProductLineID, skip / NULL / empty string if request is for Cluster or Factory level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TabID'; SET @value = N'TabID which is requested'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@LayoutID'; SET @value = N'ID of the layout which is updated. If LayoutID is * then a new layout is created.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@LayoutName'; SET @value = N'The name of the layout.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@LayoutJSON'; SET @value = N'The JSON definition of the layout.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@OrderIndex'; SET @value = N'Index to help with ordering the layouts. Needs to have unique values for all combinations of FactoryID, ProductLineID, TabID & LayoutOwner.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@LayoutOwner'; SET @value = N'Username if it is a private layout. Empty if it is a public layout.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_Tab; GO /* Procedure to POST a Tab for - a cluster - a factory - a productline - a product Saxess Software GmbH Last modified: 08/2024 for OCT 5.10 Testcall Procedure DECLARE @RC INT; EXEC @RC = planning.spPOST_Tab @Username = 'W10\admin' ,@FactoryID = '' ,@ProductLineID = '' ,@ProductID = '' ,@TabID = 'Td5dd' ,@OrderIndex = '3' ,@TabName = 'Name' ,@TabHint = 'Hint' ,@TabText = 'Text' ,@PresentationCODE = 'Pivot' ,@Datasource = 'fdafds' ,@IsVisibleBOOL = '1' ,@LayoutJSON = 'dafds' ,@ParameterJSON = 'fdafd' ,@SQLCommand = '' PRINT @RC SELECT * FROM planning.tTabs ORDER BY OrderIndex; TRUNCATE TABLE planning.tTabs; SELECT TOP 10 * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Tab',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Tab','PARAMETER',NULL) */ CREATE PROCEDURE planning.spPOST_Tab ( @Username NVARCHAR(255) ,@FactoryID NVARCHAR(255) = '' ,@ProductLineID NVARCHAR(255) = '' ,@ProductID NVARCHAR(255) = '' ,@TabID NVARCHAR(255) ,@OrderIndex INT ,@TabName NVARCHAR(255) ,@TabHint NVARCHAR(2000) ,@TabText NVARCHAR(2000) ,@PresentationCODE NVARCHAR(255) ,@Datasource NVARCHAR(2000) ,@IsVisibleBOOL INT ,@LayoutJSON NVARCHAR(MAX) = NULL ,@ParameterJSON NVARCHAR(MAX) ,@SQLCommand NVARCHAR(MAX) = NULL ) AS BEGIN SET NOCOUNT ON; -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@FactoryID ,N'NULL') + N''',''' + ISNULL(@ProductLineID ,N'NULL') + N''',''' + ISNULL(@ProductID ,N'NULL') + N''',''' + ISNULL(@TabID ,N'NULL') + N''',' + CAST(ISNULL(@OrderIndex ,N'NULL') AS NVARCHAR(255)) + N',''' + ISNULL(@TabName ,N'NULL') + N''',''' + ISNULL(@TabHint ,N'NULL') + N''',''' + ISNULL(@TabText ,N'NULL') + N''',''' + ISNULL(@PresentationCODE ,N'NULL') + N''',''' + ISNULL(@Datasource ,N'NULL') + N''',' + CAST(ISNULL(@IsVisibleBOOL ,N'NULL') AS NVARCHAR(255)) + N',''' + ISNULL(@LayoutJSON ,N'NULL') + N''',''' + ISNULL(@ParameterJSON ,N'NULL') + N''',''' + ISNULL(@SQLCommand ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' -- Procedure specific declarations DECLARE @FactoryKey INT = 0 ,@ProductLineKey INT = 0 ,@ProductKey INT = 0 ,@RowKey BIGINT = 0 ,@OldOrderIndex INT = 0; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @ProductID IS NULL SET @ProductID = N''; IF @TabID IS NULL SET @TabID = N''; IF @OrderIndex IS NULL SET @OrderIndex = 0 ; IF @TabName IS NULL SET @TabName = N''; IF @TabHint IS NULL SET @TabHint = N''; IF @TabText IS NULL SET @TabText = N''; IF @PresentationCODE IS NULL SET @PresentationCODE = N''; IF @Datasource IS NULL SET @Datasource = N''; IF @IsVisibleBOOL IS NULL SET @IsVisibleBOOL = 0 ; IF @LayoutJSON IS NULL SET @LayoutJSON = N''; IF @ParameterJSON IS NULL SET @ParameterJSON = N''; IF @SQLCommand IS NULL SET @SQLCommand = N''; -- dont use negative index values IF @OrderIndex < 0 SET @OrderIndex = 1; -- protect bool IF @IsVisibleBOOL <> 1 SET @IsVisibleBOOL = 0; -- Content Protection for specific input parameters (e.g.IDs) SET @FactoryID = system.fProtectID (@FactoryID); SET @ProductLineID = system.fProtectID (@ProductLineID); SET @ProductID = system.fProtectID (@ProductID); -- Handle updated PresentationCODEs IF @PresentationCODE = 'HTML_URL' SET @PresentationCODE = 'ExternalWebsite' IF @PresentationCODE = 'HTML_Static' SET @PresentationCODE = 'HTMLFromSQLQuery' IF @PresentationCODE = 'HTML_Editor' SET @PresentationCODE = 'Editor' -- START TRANSACTION *********************************************************************************** BEGIN TRY BEGIN TRANSACTION spPOST_Tab; -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- determine Keys for requested IDs - empty IDs keep Key -1 IF @FactoryID = '' SET @FactoryKey = -1; IF @ProductLineID = '' SET @ProductLineKey = -1; IF @ProductID = '' SET @ProductKey = -1; SELECT @FactoryKey = FactoryKey FROM planning.tdFactories WHERE FactoryID = @FactoryID AND @FactoryKey <> -1; SELECT @ProductLineKey = ProductLineKey FROM planning.tdProductLines WHERE FactoryKey = @FactoryKey AND ProductLineID = @ProductLineID AND @ProductLineKey <> -1; SELECT @ProductKey = ProductKey FROM planning.tdProducts WHERE ProductLineKey = @ProductLineKey AND ProductID = @ProductID AND @ProductKey <> -1; -- error if and ID dont exists IF @FactoryKey = 0 OR @ProductLineKey = 0 OR @ProductKey = 0 BEGIN SET @ResultCode = 404; RAISERROR(N'Not existing FID / PID / PLID - Keys don`t exists', 16, 10); END; -- error if ID chains has gaps IF @FactoryKey = -1 AND (@ProductLineKey <> -1 OR @ProductKey <> -1) OR @ProductLineKey = -1 AND @ProductKey <> -1 BEGIN SET @ResultCode = 404; RAISERROR('ID combination can have only gaps at the end - Keys don`t exists', 16, 10); END; -- check user rights for cluster level IF @FactoryKey = -1 BEGIN EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for Cluster write.', 16, 10); END END -- check user rights for Factory level IF @FactoryKey <> -1 AND @ProductLineKey = -1 BEGIN EXEC @ResultCode = planning.spGET_FactoryWriteRight @TransactUsername,@FactoryID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for Factory write.', 16, 10); END END -- check user rights for ProductLine / Product level IF @FactoryKey <> -1 AND @ProductLineKey <> -1 BEGIN EXEC @ResultCode = planning.spGET_ProductLineWriteRight @TransactUsername,@FactoryID,@ProductLineID; IF @ResultCode <> 200 BEGIN RAISERROR('Invalid rights for ProductLine / Product write.', 16, 10); END END SET @ResultCode = 501 -- reset -- determine if posted Tab already exists SELECT @Rowkey = RowKey ,@OldOrderIndex = COALESCE(Orderindex, CASE WHEN TabID = '0' THEN 0 END) FROM planning.tTabs WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey AND TabID = @TabID -- data operation starts *********************************************************************************** -- delete if exits, to determine Resultcode IF @RowKey > 0 BEGIN -- delete tab layouts if tab type was changed IF @PresentationCODE <> (SELECT PresentationCODE FROM planning.tTabs WHERE RowKey = @RowKey) BEGIN DELETE FROM planning.tTabLayouts WHERE FactoryID = @FactoryID AND ProductLineID = @ProductLineID AND TabID = @TabID; END DELETE FROM planning.tTabs WHERE RowKey = @RowKey; SET @Resultcode = 200 END -- System Tab with TabID 0 can only change visibility IF @TabID = '0' BEGIN INSERT INTO planning.tTabs ( FactoryKey ,ProductLineKey ,ProductKey ,FactoryID ,ProductLineID ,ProductID ,TabID ,Orderindex ,TabName ,TabHint ,TabText ,PresentationCODE ,Datasource ,IsVisibleBOOL ,ParameterJSON ,SQLCommand ) VALUES ( @FactoryKey ,@ProductLineKey ,@ProductKey ,@FactoryID ,@ProductLineID ,@ProductID ,@TabID ,@OrderIndex ,@TabName ,N'' ,N'' ,N'System' ,N'' ,@IsVisibleBOOL ,N'' ,N'' ) END ELSE BEGIN -- Insert new Tab INSERT INTO planning.tTabs ( FactoryKey ,ProductLineKey ,ProductKey ,FactoryID ,ProductLineID ,ProductID ,TabID ,Orderindex ,TabName ,TabHint ,TabText ,PresentationCODE ,Datasource ,IsVisibleBOOL ,ParameterJSON ,SQLCommand ) VALUES ( @FactoryKey ,@ProductLineKey ,@ProductKey ,@FactoryID ,@ProductLineID ,@ProductID ,@TabID ,@OrderIndex ,@TabName ,@TabHint ,@TabText ,@PresentationCODE ,@Datasource ,@IsVisibleBOOL ,@ParameterJSON ,@SQLCommand ) END -- Update other numbers if Number is changed or new IF (@OldOrderIndex <> @OrderIndex) BEGIN -- case if OrderIndex is bigger now and existed before - decrement all in move window IF @OldOrderIndex < @OrderIndex AND @RowKey <> 0 BEGIN UPDATE planning.tTabs SET Orderindex = Orderindex - 1 WHERE Orderindex >= @OldOrderIndex AND Orderindex <= @OrderIndex AND FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey AND TabID <> @TabID END -- case if OrderIndex is smaller now and existed before - increment all in move window IF @OldOrderIndex > @OrderIndex AND @RowKey <> 0 BEGIN UPDATE planning.tTabs SET Orderindex = Orderindex + 1 WHERE Orderindex <= @OldOrderIndex AND Orderindex >= @OrderIndex AND FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey AND TabID <> @TabID END -- case new Tab - increment all following IF @RowKey = 0 BEGIN UPDATE planning.tTabs SET Orderindex = Orderindex + 1 WHERE Orderindex >= @OrderIndex AND FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey AND TabID <> @TabID END -- eliminate all Gaps UPDATE planning.tTabs SET Orderindex = Rownumbers.Number FROM planning.tTabs tT INNER JOIN ( SELECT ROW_NUMBER() OVER (ORDER BY Orderindex) AS Number ,TabID FROM planning.tTabs WHERE FactoryKey = @FactoryKey AND ProductLineKey = @ProductLineKey AND ProductKey = @ProductKey ) AS Rownumbers ON tT.TabID = Rownumbers.TabID AND tT.FactoryKey = @FactoryKey AND tT.ProductLineKey = @ProductLineKey AND tT.ProductKey = @ProductKey END -- save TabLayout IF @LayoutJSON <> '' BEGIN EXEC planning.spPOST_TabLayout @Username = @Username, @FactoryID = @FactoryID, @ProductLineID = @ProductLineID, @TabID = @TabID, @LayoutID = '*', @LayoutName = 'Default', @LayoutJSON = @LayoutJSON, @OrderIndex = 1, @LayoutOwner = '' END IF @ResultCode <> 200 SET @ResultCode = 201; COMMIT TRANSACTION spPOST_Tab; END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spPOST_Tab; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Tab' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = not shiped from saxess -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- 10 = shiped from saxess as customer specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module',@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag',@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to POST a Tab. The Tab with ID 0 is the special system tab, but does not get special handling in databae.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Name of the user this action is requested for.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'optional FactoryID, skip / NULL / empty string if POST is for Cluster level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'optional ProductLineID, skip / NULL / empty string if POST is for Cluster or Factory level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductID'; SET @value = N'optional ProductD, skip / NULL / empty string if POST is for Cluster, Factory or ProductLine level'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TabID'; SET @value = N'Business Identifier for the Tab.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@OrderIndex'; SET @value = N'Position in the list of Tabs. Any Number > 0 can be passed in, tabs reorder themself.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TabName'; SET @value = N'Name of the Tab, used as Label.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TabHint'; SET @value = N'Hint on the Tab, displayed as Tooltip.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TabText'; SET @value = N'Text for the headerarea inside the tab.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PresentationCODE'; SET @value = N'CODE word for the type of presentation, e.g PIVOT, DATAGRID, HTML_URL, HTML_STATIC.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Datasource'; SET @value = N'Datasource fitting to PresentationCODE - name of the stored procedure or URL.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@IsVisibleBOOL'; SET @value = N'1 for visible, 0 for not visible'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@LayoutJSON'; SET @value = N'The JSON definition of the layout.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ParameterJSON'; SET @value = N'Parameter definition (List and default value) as JSON String'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SQLCommand'; SET @value = N'A custom SQL command that needs to return a result set which can be displayed by pivot tables and data grids.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS planning.spPOST_Values_JSON; GO /* The Procedure returns all sended values as recordset with the process result Procedure to POST Values in JSON Format Preconditions - A value can be sended only on full qualified single fact -> There is no write on summary level possible - multiple values may be allocated to one Valueseries / TimeID - they must be aggregated - A value "NULL" can be sended, this means delete the existing value Functionality: - Values for not existing ValueSeriesID are thrown away and reported as ignored - Values for not existing TimeIDs are thrown away and reported as ignored - only values for elements where the user has write rights are excepted - flag values without writerights - delete existing values for given F/PL/P/VS in table fValues - Scale numeric values based on scale in dValueSeries - Only values of type string are sended, DB must put it in the right type - if in dValueSeries IsNumeric = 0 -> put Value in ValueString IsNumeric = 1 -> put it in ValueInt, after scaling with Scale. Decimal separator is the dot "." - Special cases IsNumeric = 0 AND Value IS NULL -> DELETE, no write IsNumeric = 1 AND Value IS NULL -> DELETE, no write IsNumeric = 1 AND Value = 0 AND ValueSeriesID LIKE "_0" -> write Overhaul Workflow - put all sended records in a temporary table (SendedRecords) - create a temporary classification table with all sended F/PL/P/ID Values (SendedRecordClassification) - determine the ValueSeries in Classification table (Numeric, Source) - determine the UserRights in Classification table - determine additional properties - create an temporary table for all values which have to be inserted in OCT (ResultSetForOCT) - fill the ResultSetForOCT - with all Records which address Points the user has write rights - flag values which belong to not existing TimeIDs - aggregation of all records which address the same point - values are putted into type fitting columns - flag NULL Values in ResultSetForOCT as Value to Delete - Update SendedRecords to write Resultcodes / Comments about their fate in ResultSetForOCT - sended Summary Values not used - no Rights - not existing TimeIDs - aggregated and inserted - aggregated and deleted - deleted - inserted - which are sended on ValueSeries of Type XLS / XLS-String for Warning that formulas may ovewrite Result - write ResultSetForOCT - return SendedRecords Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall DECLARE @RC INT; DECLARE @ValuesJSON NVARCHAR(MAX); SET @ValuesJSON = N'{ "connector_type":"write","version":1,"Values":[ {"ValueNumber":1 ,"FactoryID":"ZT" ,"ProductLineID":"U" ,"ProductID":"1" ,"ValueSeriesID":"K1" ,"TimeID":"20210115" ,"Value":100 ,"ValueComment":null} ,{"ValueNumber":2 ,"FactoryID":"ZT" ,"ProductLineID":"U" ,"ProductID":"1" ,"ValueSeriesID":"K1" ,"TimeID":"20210115" ,"Value":200 ,"ValueComment":null} ,{"ValueNumber":3 ,"FactoryID":"ZT" ,"ProductLineID":"U" ,"ProductID":"1" ,"ValueSeriesID":"K3" ,"TimeID":"20210115" ,"Value":0 ,"ValueComment":null} ,{"ValueNumber":4 ,"FactoryID":"ZT" ,"ProductLineID":"U" ,"ProductID":"1" ,"ValueSeriesID":"BETR" ,"TimeID":"20210115" ,"Value":770000 ,"ValueComment":null} ,{"ValueNumber":5 ,"FactoryID":"ZT" ,"ProductLineID":"U" ,"ProductID":"1" ,"ValueSeriesID":"BEM" ,"TimeID":"20210115" ,"Value":null ,"ValueComment":null} ,{"ValueNumber":6 ,"FactoryID":"ZT" ,"ProductLineID":"U" ,"ProductID":"1" ,"ValueSeriesID":"STAT_BUD" ,"TimeID":"20210115" ,"Value":"Genehmigt" ,"ValueComment":null} ] }'; EXEC @RC = planning.spPOST_Values_JSON 'SQL', @ValuesJSON; PRINT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Values_JSON',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spPOST_Values_JSON','PARAMETER',NULL); */ CREATE PROCEDURE planning.spPOST_Values_JSON @Username NVARCHAR(255) = '' ,@ValuesJSON NVARCHAR(MAX) = '' AS BEGIN SET NOCOUNT ON; -- Logging Variables DECLARE @TransactionUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''', '''+ ISNULL(@ValuesJSON, N'NULL')+ N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @ValuesJSON IS NULL SET @ValuesJSON = N''; BEGIN TRY BEGIN TRANSACTION spPOST_Values_JSON -- INPUT PROTECTION ################################################################################# SET @Username = system.fProtectString (@Username); -- Test for valid JSON ################################################################################ IF @ValuesJSON = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; IF ISJSON(@ValuesJSON) = 0 BEGIN SET @ResultCode = 403; THROW 60000, N'Exit due to invalid JSON (technical)', 100; END; -- Extract Values FROM JSON and put them into #SendedRecords ##################################### DROP TABLE IF EXISTS #SendedRecords ;WITH vals AS ( SELECT ValueNumber AS 'ValueNumber' ,system.fProtectID (FactoryID) AS 'FactoryID' ,system.fProtectID (ProductLineID) AS 'ProductLineID' ,system.fProtectID (ProductID) AS 'ProductID' ,system.fProtectID (ValueSeriesID) AS 'ValueSeriesID' ,TimeID AS 'TimeID' ,[Value] AS 'Value' -- there maybe NULL inserted ,ValueComment AS 'ValueComment' FROM OPENJSON (@ValuesJSON, N'$.Values') WITH ( ValueNumber INT N'$.ValueNumber' ,FactoryID NVARCHAR(255) N'$.FactoryID' ,ProductLineID NVARCHAR(255) N'$.ProductLineID' ,ProductID NVARCHAR(255) N'$.ProductID' ,ValueSeriesID NVARCHAR(255) N'$.ValueSeriesID' ,TimeID INT N'$.TimeID' ,[Value] NVARCHAR(MAX) N'$.Value' ,ValueComment NVARCHAR(MAX) N'$.ValueComment' ) ) SELECT ValueNumber ,FactoryID COLLATE DATABASE_DEFAULT AS FactoryID ,ProductLineID COLLATE DATABASE_DEFAULT AS ProductLineID ,ProductID COLLATE DATABASE_DEFAULT AS ProductID ,ValueSeriesID COLLATE DATABASE_DEFAULT AS ValueSeriesID ,TimeID ,[Value] COLLATE DATABASE_DEFAULT AS [Value] ,ValueComment COLLATE DATABASE_DEFAULT AS ValueComment INTO #SendedRecords FROM vals; -- Debug: SELECT * FROM #SendedRecords -- Check for logical correct values ############################################# PRINT 'Starting Check for logical correct Values' -- no NULL Values contained in IDs - Value may be null due to deleted value IF EXISTS ( SELECT TOP(1) 1 FROM #SendedRecords WHERE ValueNumber IS NULL OR FactoryID IS NULL OR ProductLineID IS NULL OR ProductID IS NULL OR ValueSeriesID IS NULL OR TimeID IS NULL OR FactoryID = N'' OR ProductLineID = N'' OR ProductID = N'' OR ValueSeriesID = N'' OR TimeID = 0 ) BEGIN SET @ResultCode = 403; THROW 60000, N'Exit due to invalid Factory/PL/P/VS/T IDs.', 100; END; -- no doubled ValueNumbers contained IF EXISTS ( SELECT TOP (1) 1 FROM #SendedRecords GROUP BY ValueNumber HAVING COUNT(ValueNumber) > 1 ) BEGIN SET @ResultCode = 403; THROW 60000, N'Exit due to doubled ValueNumbers.', 100; END; -- User Rights ################################################################## PRINT 'Starting User Rights Check' -- Determine transaction user SELECT @TransactionUsername = system.fDetermineTransactionUsername (@Username); IF @TransactionUsername = N'403' BEGIN SET @ResultCode = 403; THROW 60000, N'Transaction user don`t exists', 100; END; -- Build classification Table for sended Records ################################# PRINT 'Starting classification Process' DROP TABLE IF EXISTS #SendedRecordsClassification; CREATE TABLE #SendedRecordsClassification ( ValueNumber INT NOT NULL ,FactoryID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,TimeID BIGINT NOT NULL ,[Value] NVARCHAR(255) COLLATE DATABASE_DEFAULT NULL ,ValueComment NVARCHAR(4000) COLLATE DATABASE_DEFAULT NULL ,VS_IsNumeric INT NOT NULL ,Scale INT NOT NULL ,ValueSource NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ValueInt BIGINT NOT NULL ,ValueText NVARCHAR(4000) COLLATE DATABASE_DEFAULT NOT NULL ,UserRight NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,AggregationFlag INT NOT NULL ,DeleteFlag INT NOT NULL ,SaveValueZeroFlag INT NOT NULL ,TargetIDsNotExistingFlag INT NOT NULL ,ReturnCode INT NOT NULL ,StatusComment NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductKey BIGINT NOT NULL ,ValueSeriesKey BIGINT NOT NULL ,SaveValueFlag INT NOT NULL ); INSERT INTO #SendedRecordsClassification SELECT ValueNumber AS ValueNumber ,FactoryID COLLATE DATABASE_DEFAULT AS FactoryID ,ProductLineID COLLATE DATABASE_DEFAULT AS ProductLineID ,ProductID COLLATE DATABASE_DEFAULT AS ProductID ,ValueSeriesID COLLATE DATABASE_DEFAULT AS ValueSeriesID ,TimeID AS TimeID ,[Value] COLLATE DATABASE_DEFAULT AS [Value] ,ValueComment COLLATE DATABASE_DEFAULT AS ValueComment ,0 AS VS_IsNumeric ,0 AS Scale ,N'' COLLATE DATABASE_DEFAULT AS ValueSource ,0 AS ValueInt ,N'' COLLATE DATABASE_DEFAULT AS ValueText ,'' AS UserRight ,0 AS AggregationFlag ,0 AS DeleteFlag ,0 AS SaveValueZeroFlag -- for _0 ValueSeries ,0 AS TargetIDsNotExistingFlag ,501 AS ReturnCode ,N'Original' COLLATE DATABASE_DEFAULT AS StatusComment ,0 AS ProductKey ,0 AS ValueSeriesKey ,0 AS SaveValueFlag FROM #SendedRecords; -- add ValuSeries Properties incl. ValueSeriesKey UPDATE SRC SET SRC.VS_IsNumeric = dVS.[IsNumeric] ,SRC.Scale = dVS.Scale ,SRC.ValueSource = dVS.ValueSource ,SRC.ProductKey = dVS.ProductKey ,SRC.ValueSeriesKey = dVS.ValueSeriesKey FROM #SendedRecordsClassification SRC INNER JOIN planning.tdValueSeries dVS -- only for hits ON SRC.FactoryID = dVS.FactoryID AND SRC.ProductLineID = dVS.ProductLineID AND SRC.ProductID = dVS.ProductID AND SRC.ValueSeriesID = dVS.ValueSeriesID; -- Flag Target VSIDs which don't exist UPDATE #SendedRecordsClassification SET TargetIDsNotExistingFlag = 1 ,ReturnCode = 404 ,StatusComment = N'Error VSID does not exits' ,SaveValueFlag = -1 WHERE ValueSeriesKey = 0; -- Flag Target TimeIDs which don't exist UPDATE SRC SET SRC.TargetIDsNotExistingFlag = 1 ,SRC.ReturnCode = 404 ,SRC.StatusComment = N'Error TimeID does not exits' ,SRC.SaveValueFlag = -1 FROM #SendedRecordsClassification SRC LEFT JOIN planning.tdTime dT ON SRC.ProductKey = dT.ProductKey AND SRC.TimeID = dT.TimeID WHERE dT.TimeID IS NULL AND SRC.SaveValueFlag <> -1; -- Flag _0 ValueSeries UPDATE #SendedRecordsClassification SET SaveValueZeroFlag = 1 WHERE ValueSeriesID LIKE N'%_0' AND SaveValueFlag <> -1; -- determine UserRights UPDATE SRC SET SRC.UserRight = tUR.[Right] FROM #SendedRecordsClassification SRC LEFT JOIN system.trUserRights tUR ON SRC.FactoryID = tUR.FactoryID AND SRC.ProductLineID = tUR.ProductLineID AND tUR.UserName = @TransactionUsername WHERE tUR.[Right] IS NOT NULL; UPDATE #SendedRecordsClassification SET SaveValueFlag = -1 ,ReturnCode = 403 ,StatusComment = 'No write rights.' WHERE UserRight <> 'Write' AND SaveValueFlag <> -1; -- Set ValueInt / ValueText based on Value and ValueSeries Type UPDATE #SendedRecordsClassification SET ValueInt = CAST(COALESCE(TRY_CAST([Value] AS MONEY),0) * SCALE AS BIGINT) WHERE VS_IsNumeric = 1 AND [Value] IS NOT NULL; UPDATE #SendedRecordsClassification SET ValueText = [Value] WHERE VS_IsNumeric = 0 AND [Value] IS NOT NULL; -- Set a warning, if XLS / XLS-Strict values are overwritten UPDATE #SendedRecordsClassification SET StatusComment = StatusComment + ' Warning - XLS/XLS Strict formula field will be overwritten' WHERE ValueSource IN ('XLS','XLS-Strict') -- Flag Values to be aggregated if they are numeric and NOT NULL and multiples are sended (from multiple sended Text Values, the last wins) UPDATE SRC SET SRC.AggregationFlag = 1 ,SRC.ReturnCode = 200 ,SRC.StatusComment = StatusComment + CONCAT(' Aggregated with ',Agg.ValueNumberCount - 1,' other values.') ,SRC.SaveValueFlag = 1 FROM #SendedRecordsClassification SRC LEFT JOIN ( SELECT COUNT(ValueNumber) AS ValueNumberCount ,SUM(TRY_CAST([Value] AS MONEY)) AS [Value] ,FactoryID ,ProductLineID ,ProductID ,ValueSeriesID ,TimeID FROM #SendedRecordsClassification WHERE VS_IsNumeric = 1 AND SaveValueFlag <> -1 AND [Value] IS NOT NULL --NULLs are not marked for Aggregation in this step GROUP BY FactoryID ,ProductLineID ,ProductID ,ValueSeriesID ,TimeID HAVING COUNT(ValueNumber) > 1 ) AS Agg ON Agg.FactoryID = SRC.FactoryID AND Agg.ProductLineID = SRC.ProductLineID AND Agg.ProductID = SRC.ProductID AND Agg.ValueSeriesID = SRC.ValueSeriesID AND Agg.TimeID = SRC.TimeID WHERE Agg.ValueNumberCount IS NOT NULL; -- Flag NULL Values as aggregated, if there are other values which write to the same poing UPDATE SRC SET SRC.AggregationFlag = 1 ,SRC.StatusComment = 'Value was not deleted, as other Rows send values to this VS/TID.' FROM #SendedRecordsClassification SRC LEFT JOIN ( SELECT ValueSeriesKey ,TimeID FROM #SendedRecordsClassification WHERE AggregationFlag = 1 ) Agg ON SRC.ValueSeriesKey = Agg.ValueSeriesKey AND SRC.TimeID = Agg.TimeID WHERE SRC.VS_IsNumeric = 1 AND SRC.[Value] IS NULL; -- Flag Values to be deleted - but not if they are flagged for Aggregation UPDATE #SendedRecordsClassification SET DeleteFlag = 1 ,ReturnCode = 200 ,StatusComment = 'Value deleted' WHERE [Value] IS NULL AND AggregationFlag = 0 AND SaveValueFlag <> -1; -- Flag Numeric Zero Values as to be deleted, except ValueSeries saving zeros UPDATE #SendedRecordsClassification SET DeleteFlag = 1 ,ReturnCode = 200 ,StatusComment = 'Value deleted as 0 was send' WHERE ValueInt = 0 AND VS_IsNumeric = 1 AND SaveValueZeroFlag = 0 AND SaveValueFlag <> -1; ---- Flag Zero Values as not to be saved, except VS ValueSeries - REPLACED by delete zero values to delete existing values --UPDATE #SendedRecordsClassification --SET -- SaveValueFlag = -1 -- ,ReturnCode = 202 -- ,StatusComment = 'Not saved as value is zero.' --WHERE -- ValueInt = 0 -- AND VS_IsNumeric = 1 -- AND SaveValueZeroFlag = 0 -- AND SaveValueFlag <> -1; -- Set Null Values for ValueComment to empty string UPDATE #SendedRecordsClassification SET ValueComment = '' WHERE ValueComment IS NULL AND SaveValueFlag <> -1; -- Flag all remaining Value as to be saved UPDATE #SendedRecordsClassification SET SaveValueFlag = 1 ,ReturnCode = 200 ,StatusComment = 'Value saved' WHERE SaveValueFlag = 0 AND DeleteFlag = 0; SELECT * FROM #SendedRecordsClassification --DEBUG -- Delete Values which have to be deleted ############################################ PRINT 'Starting data manipulation' DELETE fV FROM planning.tfValues fV INNER JOIN #SendedRecordsClassification SRC ON SRC.ValueSeriesKey = fV.ValueSeriesKey AND SRC.TimeID = fV.TimeID WHERE SRC.DeleteFlag = 1; -- Fill Values to #SavedRecords ####################################################### DROP TABLE IF EXISTS #SavedRecords; CREATE TABLE #SavedRecords ( ValueSeriesKey BIGINT NOT NULL ,TimeID BIGINT NOT NULL ,ValueInt BIGINT NOT NULL ,ValueText NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,ValueComment NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ) -- the aggregated Values PRINT 'Filling SaveSet with aggregated values' INSERT INTO #SavedRecords SELECT ValueSeriesKey ,TimeID ,SUM(ValueInt) AS ValueInt ,N'' AS ValueText ,N'' AS ValueComment FROM #SendedRecordsClassification WHERE AggregationFlag = 1 GROUP BY ValueSeriesKey ,TimeID -- the Values to be saved PRINT 'Filling SaveSet with standard values' INSERT INTO #SavedRecords SELECT ValueSeriesKey ,TimeID ,ValueInt ,ValueText ,COALESCE(ValueComment,'') FROM #SendedRecordsClassification WHERE SaveValueFlag = 1 AND DeleteFlag = 0 AND AggregationFlag = 0 -- Check #SavedRecords for security for doubled values PRINT 'Start validation for SaveSet' IF EXISTS (SELECT TimeID ,ValueSeriesKey ,COUNT(ValueSeriesKey) AS Anzahl FROM #SavedRecords GROUP BY TimeID ,ValueSeriesKey HAVING COUNT(ValueSeriesKey) > 1 ) BEGIN SET @ResultCode = 500; THROW 60000, N'Internal Error, Rows to save not unique.', 100; END -- Delete old Values PRINT 'Start deleting of old Values.' DELETE fV FROM planning.tfValues fV INNER JOIN #SavedRecords SR ON SR.ValueSeriesKey = fV.ValueSeriesKey AND SR.TimeID = fV.TimeID WHERE SR.ValueSeriesKey IS NOT NULL AND SR.TimeID IS NOT NULL; -- Insert new Values PRINT 'Start insert new Values.' INSERT INTO planning.tfValues ( ValueSeriesKey ,ProductKey ,ProductLineKey ,FactoryKey ,ProductID ,ProductLineID ,FactoryID ,ValueSeriesID ,TimeID ,ValueFormula ,ValueInt ,ValueText ,ValueComment ) SELECT SR.ValueSeriesKey ,dV.ProductKey ,dV.ProductLineKey ,dV.FactoryKey ,dV.ProductID ,dV.ProductLineID ,dV.FactoryID ,dV.ValueSeriesID ,SR.TimeID ,'' AS ValueFormula ,SR.ValueInt ,SR.ValueText ,SR.ValueComment FROM #SavedRecords SR LEFT JOIN planning.tdValueSeries dV ON SR.ValueSeriesKey = dV.ValueSeriesKey; -- Create result table to be returnd ################################################# DROP TABLE IF EXISTS #ResultSet; CREATE TABLE #ResultSet ( ValueNumber INT NOT NULL ,FactoryID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ValueSeriesID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,TimeID INT NOT NULL ,[Value] NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NULL ,ValueComment NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NULL ,ReturnCode INT NULL ,ReturnComment NVARCHAR(4000) COLLATE DATABASE_DEFAULT NULL ); -- Fill result table with requested values PRINT 'Start filling result set.' INSERT INTO #ResultSet (ValueNumber, FactoryID, ProductLineID, ProductID, ValueSeriesID, TimeID, [Value], ValueComment,ReturnCode,ReturnComment) SELECT ValueNumber ,FactoryID ,ProductLineID ,ProductID ,ValueSeriesID ,TimeID ,[Value] ,ValueComment ,0 ,'' FROM #SendedRecords; -- Set Return Codes an comments PRINT 'Setting ReturnCodes in Result Set.' UPDATE RS SET RS.ReturnCode = SRC.ReturnCode ,RS.ReturnComment = SRC.StatusComment FROM #ResultSet RS LEFT JOIN #SendedRecordsClassification SRC ON RS.ValueNumber = SRC.ValueNumber; DECLARE @ProcessTime INT; SET @ProcessTime = DATEDIFF(ms, @TimestampCall, GETUTCDATE()); SET @ResultCode = 200; -- Return result SELECT * ,@ProcessTime AS ProcessTime FROM #ResultSet; COMMIT TRANSACTION spPOST_Values_JSON; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spPOST_Values_JSON; IF @Error_state = 100 BEGIN PRINT @Comment; END ELSE IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactionUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'planning' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Values_JSON' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to POST Values in JSON Format'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValuesJSON'; SET @value = N'Valus which are to be saved as JSON object.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_Setting; GO /* ToDo - RightCheck Gerd Tautenhahn for Saxess Software GmbH Last modified: 03/2023 for OCT 5.9 Testcall EXEC system.spGET_Setting 'SQL','DBVersion' Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Setting',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Setting','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_Setting @Username NVARCHAR(255) ,@SettingID NVARCHAR(50) AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL')+ N''',''' + ISNULL(@SettingID, N'NULL')+ N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; DECLARE @IsOCTService INT = 0 -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @SettingID IS NULL SET @SettingID = N''; BEGIN TRY -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @SettingID = system.fProtectID (@SettingID); IF @SettingID = N'' BEGIN SET @ResultCode = 404; THROW 60000, N'Empty input parameters', 10; END; IF EXISTS ( SELECT r.name from sys.database_role_members rm INNER JOIN sys.database_principals r on rm.role_principal_id = r.principal_id INNER JOIN sys.database_principals m on rm.member_principal_id = m.principal_id WHERE r.name = 'db_octservice' AND m.name = ORIGINAL_LOGIN() ) BEGIN SET @IsOCTService = 1; END; -- STEP 1.1 - Determine transaction user IF @IsOCTService = 1 BEGIN SET @TransactUsername = 'db_octservice'; END ELSE BEGIN SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); END IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; THROW 60000, N'Transaction user don`t exists', 10; END; -- STEP 3 - Output result SELECT SettingID ,SettingDescription ,DataTypeCODE ,ValueText ,ValueDateTime ,ValueInt ,ValueIntScale ,ValueJSON ,Category ,ReadOnlySystemPropertyBool FROM [system].tSettings WHERE SettingID = @SettingID; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state = 100 BEGIN PRINT @Comment; END ELSE IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Setting' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to read one setting. To read all settings, use system.pGETSettings.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SettingID'; SET @value = N'ID of the Setting.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_Settings; GO /* ToDo - RightCheck - Documentation Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall EXEC system.spGET_Settings 'SQL' Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Settings',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Settings','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_Settings @Username NVARCHAR(255) ,@Category NVARCHAR(255) = NULL AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL')+ N''',''' + ISNULL(@Category, N'NULL')+ N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; DECLARE @IsOCTService INT = 0; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; BEGIN TRY -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); IF EXISTS ( SELECT r.name from sys.database_role_members rm INNER JOIN sys.database_principals r on rm.role_principal_id = r.principal_id INNER JOIN sys.database_principals m on rm.member_principal_id = m.principal_id WHERE r.name = 'db_octservice' AND m.name = ORIGINAL_LOGIN() ) BEGIN SET @IsOCTService = 1; END; -- STEP 1.1 - Determine transaction user IF @IsOCTService = 1 BEGIN SET @TransactUsername = 'db_octservice'; END ELSE BEGIN SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); END IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; THROW 60000, N'Transaction user don`t exists', 10; END; -- STEP 3 - Output result SELECT SettingID ,SettingDescription ,DataTypeCODE ,ValueText ,ValueDateTime ,ValueInt ,ValueIntScale ,ValueJSON ,Category ,ReadOnlySystemPropertyBool FROM [system].[tSettings] WHERE Category = @Category OR @Category IS NULL; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state = 100 BEGIN PRINT @Comment; END ELSE IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Settings' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'spGET_Settings'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Category'; SET @value = N'Category'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spPOST_Setting; GO /* ToDo: - Right Check - Documentation Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall DECLARE @ReturnCode INT; EXEC @ReturnCode = system.spPOST_Setting @Username = 'SQL' ,@SettingID = '1' ,@DataTypeCode = 'TEXT'; PRINT @ReturnCode; EXEC system.spGET_Setting 'SQL','1' Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_Setting',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_Setting','PARAMETER',NULL) */ CREATE PROCEDURE system.spPOST_Setting @Username NVARCHAR(255) ,@SettingID NVARCHAR(50) ,@DataTypeCODE NVARCHAR(255) ,@SettingDescription NVARCHAR(4000) = NULL ,@ValueText NVARCHAR(MAX) = NULL ,@ValueDateTime DATETIME = NULL ,@ValueInt INT = NULL ,@ValueIntScale INT = NULL ,@ValueJSON NVARCHAR(MAX) = NULL ,@Category NVARCHAR(255) = NULL ,@ReadOnlySystemPropertyBool INT = NULL AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@SettingID, N'NULL') + N''',''' + ISNULL(@DataTypeCODE, N'NULL') + N''',''' + ISNULL(@SettingDescription, N'NULL') + N''',''' + ISNULL(@ValueText, N'NULL') + N''',' + ISNULL(CAST(@ValueDateTime AS NVARCHAR(MAX)), N'NULL') + N',' + ISNULL(CAST(@ValueInt AS NVARCHAR(MAX)), N'NULL') + N',' + ISNULL(CAST(@ValueIntScale AS NVARCHAR(MAX)), N'NULL') + N',''' + ISNULL(@ValueJSON, N'NULL') + N''',''' + ISNULL(@Category, N'NULL') + N''',' + ISNULL(CAST(@ReadOnlySystemPropertyBool AS NVARCHAR(MAX)), N'NULL'); DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; DECLARE @IsOCTService INT = 0; -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @DataTypeCODE IS NULL SET @DataTypeCODE = N''; IF @SettingID IS NULL SET @SettingID = N''; BEGIN TRY BEGIN TRANSACTION pPOST_Setting; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); SET @SettingID = system.fProtectString (@SettingID); -- Check for mandatory parameter IF @SettingID = N'' OR @DataTypeCODE NOT IN ('TEXT','NUMBER','BOOLEAN','JSON','DATETIME') BEGIN SET @ResultCode = 403; THROW 60000, N'Invalid input parameters', 10; END -- STEP 1.1 - Determine transaction user IF EXISTS ( SELECT r.name from sys.database_role_members rm INNER JOIN sys.database_principals r on rm.role_principal_id = r.principal_id INNER JOIN sys.database_principals m on rm.member_principal_id = m.principal_id WHERE r.name = 'db_octservice' AND m.name = ORIGINAL_LOGIN() ) BEGIN SET @IsOCTService = 1; END; IF @IsOCTService = 1 BEGIN SET @TransactUsername = 'db_octservice'; END ELSE BEGIN SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); END IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; THROW 60000, N'Transaction user don`t exists', 10; END; IF EXISTS(SELECT TOP(1) 1 FROM [system].tSettings WHERE SettingID = @SettingID) -- Existing Property will be update, not passed Values are keept BEGIN UPDATE [system].tSettings SET SettingDescription = ISNULL(@SettingDescription, SettingDescription) ,DataTypeCODE = @DataTypeCODE ,ValueText = ISNULL(@ValueText, ValueText) ,ValueDateTime = ISNULL(@ValueDateTime, ValueDateTime) ,ValueInt = ISNULL(@ValueInt, ValueInt) ,ValueIntScale = ISNULL(@ValueIntScale, ValueIntScale) ,ValueJSON = ISNULL(@ValueJSON, ValueJSON) ,Category = ISNULL(@Category, Category) ,ReadOnlySystemPropertyBool = ISNULL(@ReadOnlySystemPropertyBool, ReadOnlySystemPropertyBool) WHERE SettingID = @SettingID; SET @EffectedRows = @@ROWCOUNT; SET @Resultcode = 200; END ELSE -- Not existing Property will be created, not passed Values with defaults BEGIN INSERT INTO [system].[tSettings] ( SettingID ,SettingDescription ,DataTypeCODE ,ValueText ,ValueDateTime ,ValueInt ,ValueIntScale ,ValueJSON ,Category ,ReadOnlySystemPropertyBool ) VALUES ( @SettingID , ISNULL(@SettingDescription, N'') , @DataTypeCODE , ISNULL(@ValueText, N'') , ISNULL(@ValueDateTime, '19000101') , ISNULL(@ValueInt, 0) , ISNULL(@ValueIntScale, 1) , ISNULL(@ValueJSON, N'') , ISNULL(@Category, N'') , ISNULL(@ReadOnlySystemPropertyBool, 1) ); SET @EffectedRows = @@ROWCOUNT; SET @Resultcode = 201; END COMMIT TRANSACTION pPOST_Setting; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION pPOST_Setting; IF @Error_state = 100 BEGIN PRINT @Comment; END ELSE IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Setting' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'spPOST_Setting'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SettingID'; SET @value = N'SettingID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@DataTypeCODE'; SET @value = N'DataTypeCODE'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SettingDescription'; SET @value = N'SettingDescription'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueText'; SET @value = N'ValueText'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueDateTime'; SET @value = N'ValueDateTime'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueInt'; SET @value = N'ValueInt'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueIntScale'; SET @value = N'ValueIntScale'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ValueJSON'; SET @value = N'ValueJSON'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Category'; SET @value = N'Category'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ReadOnlySystemPropertyBool'; SET @value = N'ReadOnlySystemPropertyBool'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spCOPY_Action; GO /* Procedure to COPY Action information Saxess Software GmbH Testcall DECLARE @RC INT; DECLARE @ReturnedActionID NVARCHAR(50); EXEC @RC = system.spCOPY_Action @Username = 'SQL' , @SourceActionID = 'A1' , @TargetActionID = '*' , @OutputID = @ReturnedActionID OUTPUT; SELECT @ReturnedActionID; SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spCOPY_Action', NULL, NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spCOPY_Action', 'PARAMETER', NULL) */ CREATE PROCEDURE system.spCOPY_Action @Username NVARCHAR(255) , @SourceActionID NVARCHAR(255) = NULL , @TargetActionID NVARCHAR(255) = NULL , @OutputID NVARCHAR(50) = NULL OUTPUT AS BEGIN BEGIN TRY -- Start transaction BEGIN TRANSACTION spCOPY_Action -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 3, @Username, @SourceActionID, @TargetActionID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Input parameter handling SET @SourceActionID = COALESCE(system.fProtectID(@SourceActionID), N''); SET @TargetActionID = COALESCE(system.fProtectID(@TargetActionID), N''); -- Source ActionID doesn't exist IF NOT EXISTS ( SELECT 1 FROM system.tActions WHERE ActionID = @SourceActionID ) BEGIN EXEC system.spSEND_Message 'ERROR', 'Source ActionID doesn''t exist'; END -- Target ActionID already exists IF EXISTS ( SELECT 1 FROM system.tActions WHERE ActionID = @TargetActionID ) BEGIN EXEC system.spSEND_Message 'ERROR', 'Target ActionID already exists'; END -- Determine next free ActionID if * is sent IF @TargetActionID = '*' BEGIN SET @TargetActionID = ( SELECT TOP 1 TRY_CAST(RIGHT(ActionID, LEN(ActionID) - 1) AS INT) + 1 FROM system.tActions WHERE ActionID LIKE N'A%' AND LEN(ActionID) > 1 AND TRY_CAST(RIGHT(ActionID, LEN(ActionID) - 1) AS INT) IS NOT NULL ORDER BY TRY_CAST(RIGHT(ActionID, LEN(ActionID) - 1) AS INT) DESC ); SET @TargetActionID = 'A' + @TargetActionID; END -- Copy the Action INSERT INTO system.tActions ( ActionID , OrderIndex , Active , Name , Description , RequiresClusterWriteRights , RequiresWriteRights , Condition , ListCustomTitle , ListSQL , ListLayout , ListExcelExport , SetCellValueAsFilter , ReloadListAfterChangingFilter , ActionSQL , ApplyFromField , SaveBeforeAction , SaveAfterAction , ReloadTreeAfterAction , ReloadDetailsAfterAction ) SELECT @TargetActionID , ( SELECT MAX(OrderIndex) + 1 FROM system.tActions ) , Active , Name , Description , RequiresClusterWriteRights , RequiresWriteRights , Condition , ListCustomTitle , ListSQL , ListLayout , ListExcelExport , SetCellValueAsFilter , ReloadListAfterChangingFilter , ActionSQL , ApplyFromField , SaveBeforeAction , SaveAfterAction , ReloadTreeAfterAction , ReloadDetailsAfterAction FROM system.tActions WHERE ActionID = @SourceActionID; SET @AffectedRows = @@ROWCOUNT; SET @OutputID = @TargetActionID; SET @ResultCode = 200 COMMIT TRANSACTION spCOPY_Action END TRY BEGIN CATCH ROLLBACK TRANSACTION spCOPY_Action SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 EXEC system.spSEND_Message 'ERROR', @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spCOPY_Action' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.Actions, Custom) ,@SX_Action NVARCHAR(255) = N'CORE' -- enter Action name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Action; EXEC sys.sp_addextendedproperty N'SX_Action' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to COPY Action information'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceActionID'; SET @value = N'ActionID that should be copied.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetActionID'; SET @value = N'ActionID that should be assigned to the newly created Action. Use * to assign the next free ActionID.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spCOPY_Content; GO /* Procedure to COPY content information Saxess Software GmbH Last modified: 10/2023 for OCT 5.10 Testcall DECLARE @RC INT; EXEC @RC = system.spCOPY_Content @Username = 'SQL', @SourceModuleID = 'FIN', @SourceContentID = 'result.tFIN_Konten', @TargetModuleID = 'FINCOPY', @TargetContentID = 'result.tFIN_Konten_Copy' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload','PARAMETER',NULL) */ CREATE PROCEDURE system.spCOPY_Content @Username NVARCHAR(255), @SourceModuleID NVARCHAR(255), @SourceContentID NVARCHAR(255), @TargetModuleID NVARCHAR(255), @TargetContentID NVARCHAR(255) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 5, @Username, @SourceModuleID, @SourceContentID, @TargetModuleID, @TargetContentID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Variables DECLARE @ModuleKey INT; -- Input parameter handling SET @SourceModuleID = COALESCE(system.fProtectID(@SourceModuleID), N''); SET @TargetModuleID = COALESCE(system.fProtectID(@TargetModuleID), N''); -- Error handling IF @SourceModuleID = N'' RAISERROR ('No SourceModuleID provided', 11, 1); IF @SourceContentID = N'' RAISERROR ('No SourceContentID provided', 11, 1); IF @TargetModuleID = N'' RAISERROR ('No TargetModuleID provided', 11, 1); IF @TargetContentID = N'' RAISERROR ('No TargetContentID provided', 11, 1); IF @TargetModuleID = @SourceModuleID AND @TargetContentID = @SourceContentID RAISERROR ('Source and target ModuleID & ContentID are the same', 11, 1); -- Copy content INSERT INTO system.tContents ( ModuleKey , ModuleID , ContentID , ContentType , TechnicalType , Description , OrderIndex ) SELECT ( SELECT ModuleKey FROM system.tModules WHERE ModuleID = @TargetModuleID ) , @TargetModuleID , @TargetContentID , ContentType , TechnicalType , Description , ( SELECT COALESCE(MAX(OrderIndex) + 1, 1) FROM system.tContents WHERE ModuleID = @TargetModuleID ) FROM system.tContents WHERE ModuleID = @SourceModuleID AND ContentID = @SourceContentID; SET @AffectedRows = @@ROWCOUNT; IF @AffectedRows = 0 BEGIN RAISERROR ('Source ContentID in SourceModuleID doesn''t exist', 11, 1); END -- Copy content versions INSERT INTO system.tContentVersions ( ContentKey , ModuleID , ContentID , Version , ContentSQL , ContentUninstallSQL , CreatedBy , CreatedDate , VersionComment , IsInstalled ) SELECT ( SELECT c.ContentKey FROM system.tContents AS c WHERE c.ModuleID = @TargetModuleID AND c.ContentID = @TargetContentID ) , @TargetModuleID , @TargetContentID , cv.Version , cv.ContentSQL , cv.ContentUninstallSQL , cv.CreatedBy , cv.CreatedDate , cv.VersionComment , 0 FROM system.tContentVersions AS cv WHERE cv.ModuleID = @SourceModuleID AND cv.ContentID = @SourceContentID; SET @AffectedRows += @@ROWCOUNT; SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spCOPY_Content' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET the module tree'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceModuleID'; SET @value = N'ID of the module that should be copied.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceContentID'; SET @value = N'ID of the content that should be copied.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetModuleID'; SET @value = N'New ModuleID of the copied content.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetContentID'; SET @value = N'New ID of the copied content.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spCOPY_Module; GO /* Procedure to COPY module information Saxess Software GmbH Last modified: 03/2024 for OCT 5.10 Testcall DECLARE @RC INT; EXEC @RC = system.spCOPY_Module @Username = 'SQL', @SourceModuleID = 'FIN', @TargetModuleID = 'FINCOPY' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload','PARAMETER',NULL) */ CREATE PROCEDURE system.spCOPY_Module @Username NVARCHAR(255), @SourceModuleID NVARCHAR(255), @TargetModuleID NVARCHAR(255) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 3, @Username, @SourceModuleID, @TargetModuleID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Variables DECLARE @ModuleKey INT; -- Input parameter handling SET @SourceModuleID = COALESCE(system.fProtectID(@SourceModuleID), N''); SET @TargetModuleID = COALESCE(system.fProtectID(@TargetModuleID), N''); -- Error handling IF @SourceModuleID = N'' RAISERROR ('No SourceModuleID provided', 11, 1); IF @TargetModuleID = N'' RAISERROR ('No TargetModuleID provided', 11, 1); IF EXISTS(SELECT 1 FROM system.tModules WHERE ModuleID = @TargetModuleID) RAISERROR ('ModuleID of TargetModuleID already exists', 11, 1); -- Copy Module INSERT INTO system.tModules ( ModuleID , ModuleType , Description , Version , AdminRequired , DescriptionURL ) SELECT @TargetModuleID , ModuleType , Description , Version , AdminRequired , DescriptionURL FROM system.tModules WHERE ModuleID = @SourceModuleID; SET @AffectedRows = @@ROWCOUNT; SET @ModuleKey = @@IDENTITY; IF @AffectedRows = 0 RAISERROR ('SourceModuleID doesn''t exist', 11, 1); -- Copy contents INSERT INTO system.tContents ( ModuleKey , ModuleID , ContentID , ContentType , TechnicalType , Description , OrderIndex ) SELECT @ModuleKey , @TargetModuleID , ContentID , ContentType , TechnicalType , Description , OrderIndex FROM system.tContents WHERE ModuleID = @SourceModuleID; SET @AffectedRows += @@ROWCOUNT; -- Copy content versions INSERT INTO system.tContentVersions ( ContentKey , ModuleID , ContentID , Version , ContentSQL , ContentUninstallSQL , CreatedBy , CreatedDate , VersionComment , IsInstalled ) SELECT ( SELECT c.ContentKey FROM system.tContents AS c WHERE c.ModuleID = @TargetModuleID AND c.ContentID = cv.ContentID ) , @TargetModuleID , cv.ContentID , cv.Version , cv.ContentSQL , cv.ContentUninstallSQL , cv.CreatedBy , cv.CreatedDate , cv.VersionComment , 0 FROM system.tContentVersions AS cv WHERE cv.ModuleID = @SourceModuleID SET @AffectedRows += @@ROWCOUNT; SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spCOPY_Module' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET the module tree'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceModuleID'; SET @value = N'ID of the module that should be copied.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetModuleID'; SET @value = N'New ID of the copied module.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spCOPY_Pipeline; GO /* Procedure to copy a Pipeline - including its steps and schedules Saxess Software GmbH Testcall Procedure DECLARE @RC INT ,@ReturnID NVARCHAR(50); EXEC @RC = system.spCOPY_Pipeline @Username = 'SQL' ,@SourcePipelineID = 'P1' ,@TargetPipelineID = '*' ,@OutputID = @ReturnID OUTPUT; PRINT CONCAT('ReturnCode: ', @RC, ' OutputID: ', @ReturnID); SELECT * FROM system.tPipelines; SELECT * FROM system.tPipelineSteps; SELECT * FROM system.tPipelineSchedules; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spCOPY_Pipeline',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spCOPY_Pipeline','PARAMETER',NULL) */ CREATE PROCEDURE system.spCOPY_Pipeline ( @Username NVARCHAR(255) ,@SourcePipelineID NVARCHAR(50) ,@TargetPipelineID NVARCHAR(50) ,@OutputID NVARCHAR(50) = NULL OUTPUT ) AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' --set Strings inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position + ISNULL(@Username ,N'NULL') + N''',' + ISNULL(@SourcePipelineID ,N'NULL') + N',''' + ISNULL(@TargetPipelineID ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime() ,@SourcePipelineKey BIGINT = 0 ,@TargetPipelineKey BIGINT = 0 ,@LastPipelineNum NVARCHAR(50) = 0 ,@TargetOrderOfDisplay INT = 0; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @SourcePipelineID IS NULL SET @SourcePipelineID = N''; IF @TargetPipelineID IS NULL SET @TargetPipelineID = N''; -- START TRANSACTION *********************************************************************************** BEGIN TRY BEGIN TRANSACTION spCOPY_Pipeline -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- check existence of IDs IF NOT EXISTS ( SELECT PipelineID FROM system.tPipelines WHERE PipelineID = @SourcePipelineID ) BEGIN SET @ResultCode = 404; RAISERROR('Source Pipeline don`t exits', 16, 10); END SELECT @SourcePipelineKey = PipelineKey FROM system.tPipelines WHERE PipelineID = @SourcePipelineID; IF EXISTS ( SELECT PipelineID FROM system.tPipelines WHERE PipelineID = @TargetPipelineID ) BEGIN SET @ResultCode = 403; RAISERROR('Target Pipeline already exits', 16, 10); END -- determine new ID if * is sended IF @TargetPipelineID = '*' BEGIN SELECT TOP 1 @LastPipelineNum = TRY_CAST(RIGHT(PipelineID,LEN(PipelineID)-1) AS INT ) FROM system.tPipelines WHERE PipelineID LIKE 'P%' AND LEN(PipelineID) > 1 AND TRY_CAST(RIGHT(PipelineID,LEN(PipelineID)-1) AS INT) IS NOT NULL ORDER BY TRY_CAST(RIGHT(PipelineID,LEN(PipelineID)-1) AS INT) DESC IF @LastPipelineNum IS NULL SET @LastPipelineNum = 0; SET @TargetPipelineID = 'P' + CAST(@LastPipelineNum +1 AS NVARCHAR(50)); END -- determine TargetOrderOfDisplay SELECT TOP 1 @TargetOrderOfDisplay = OrderOfDisplay + 1 FROM system.tPipelines ORDER BY OrderOfDisplay DESC; -- Copy the pipeline itself INSERT INTO system.tPipelines ( PipelineID ,PipelineName ,PipelineDescription ,PipelineDescriptionIconColor ,OrderOfDisplay ,EmailNotificationCODE ,ExcludedExecutionTimesJSON ) SELECT @TargetPipelineID ,PipelineName ,PipelineDescription ,PipelineDescriptionIconColor ,@TargetOrderOfDisplay ,EmailNotificationCODE ,ExcludedExecutionTimesJSON FROM system.tPipelines WHERE PipelineID = @SourcePipelineID; SET @TargetPipelineKey = SCOPE_IDENTITY(); -- Copy the steps of the Pipeline INSERT INTO system.tPipelineSteps ( PipelineKey ,StepID ,StepName ,StepDescription ,OrderOfExecution ,StepDataSourceID ,StepCompanyIDsJSON ,StepDetailsJSON ,TimeExecutionLimit ,Active ,RunConditionCODE ,RestartOnError ) SELECT @TargetPipelineKey ,StepID ,StepName ,StepDescription ,OrderOfExecution ,StepDataSourceID ,StepCompanyIDsJSON ,StepDetailsJSON ,TimeExecutionLimit ,Active ,RunConditionCODE ,RestartOnError FROM system.tPipelineSteps WHERE PipelineKey = @SourcePipelineKey; -- Copy the schedules of the Pipeline INSERT INTO system.tPipelineSchedules ( PipelineKey ,ScheduleID ,ScheduleName ,ScheduleDescription ,ScheduleDetailsJSON ,Active ) SELECT @TargetPipelineKey ,ScheduleID ,ScheduleName ,ScheduleDescription ,ScheduleDetailsJSON ,Active FROM system.tPipelineSchedules WHERE PipelineKey = @SourcePipelineKey; SET @ResultCode = 201; SET @OutputID = @TargetPipelineID; COMMIT TRANSACTION spCOPY_Pipeline END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spCOPY_Pipeline IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spCOPY_Pipeline'-- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to copy a Pipeline - including its steps and schedules'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@SourcePipelineID'; SET @value = N'ID of the Pipeline which will be copied.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetPipelineID'; SET @value = N'ID for the pipeline which will be created as copy of the SourcePipelineID, this Pipeline must not exists at the moment.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spCOPY_PipelineStep; GO /* Procedure to copy a PipelineStep to the same or a different Pipeline The step will be added at the end always. Saxess Software GmbH Testcall Procedure DECLARE @RC INT ,@ReturnID NVARCHAR(50); EXEC @RC = system.spCOPY_PipelineStep @Username = 'SQL' ,@SourcePipelineID = 'P1' ,@SourceStepID = 'S1' ,@TargetPipelineID = 'P2' ,@TargetStepID = '*' ,@OutputID = @ReturnID OUTPUT; PRINT CONCAT('ReturnCode: ', @RC, ' OutputID: ', @ReturnID); SELECT * FROM system.tPipelines; SELECT * FROM system.tPipelineSteps ORDER BY PipelineKey, StepID; SELECT * FROM system.tPipelineSchedules; Testcall Log SELECT * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spCOPY_PipelineStep',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spCOPY_PipelineStep','PARAMETER',NULL) */ CREATE PROCEDURE system.spCOPY_PipelineStep @Username NVARCHAR(255) ,@SourcePipelineID NVARCHAR(50) ,@SourceStepID NVARCHAR(50) ,@TargetPipelineID NVARCHAR(50) = '' ,@TargetStepID NVARCHAR(50) ,@OutputID NVARCHAR(50) = NULL OUTPUT AS BEGIN DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' --set Strings inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@SourcePipelineID ,N'NULL') + N''',''' + ISNULL(@SourceStepID ,N'NULL') + N''',''' + ISNULL(@TargetPipelineID ,N'NULL') + N''',''' + ISNULL(@TargetStepID ,N'NULL') + N'''' + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime() ,@SourcePipelineKey BIGINT = 0 ,@TargetPipelineKey BIGINT = 0 ,@LastStepNum NVARCHAR(50) = 0 ,@TargetOrderOfExecution INT = 0; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @SourcePipelineID IS NULL SET @SourcePipelineID = N''; IF @SourceStepID IS NULL SET @SourceStepID = N''; IF @TargetPipelineID IS NULL SET @TargetPipelineID = N''; IF @TargetStepID IS NULL SET @TargetStepID = N''; IF @TargetPipelineID = '' SET @TargetPipelineID = @SourcePipelineID; -- if no target PL is named, its copied in the source -- START TRANSACTION *********************************************************************************** BEGIN TRY BEGIN TRANSACTION spCOPY_PipelineStep -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- check existence of Pipeline and Step IDs -- Source Pipeline IF NOT EXISTS ( SELECT PipelineID FROM system.tPipelines WHERE PipelineID = @SourcePipelineID ) BEGIN SET @ResultCode = 404; RAISERROR('Source Pipeline don`t exits', 16, 10); END SELECT @SourcePipelineKey = PipelineKey FROM system.tPipelines WHERE PipelineID = @SourcePipelineID; -- Target Pipeline IF NOT EXISTS ( SELECT PipelineID FROM system.tPipelines WHERE PipelineID = @TargetPipelineID ) BEGIN SET @ResultCode = 404; RAISERROR('Target Pipeline don`t exits', 16, 10); END SELECT @TargetPipelineKey = PipelineKey FROM system.tPipelines WHERE PipelineID = @TargetPipelineID; -- Source Step ID IF NOT EXISTS ( SELECT StepID FROM system.tPipelineSteps WHERE PipelineKey = @SourcePipelineKey AND StepID = @SourceStepID ) BEGIN SET @ResultCode = 403; RAISERROR('Source StepID dont exits', 16, 10); END -- Target Step ID IF EXISTS ( SELECT StepID FROM system.tPipelineSteps WHERE PipelineKey = @TargetPipelineKey AND StepID = @TargetStepID ) BEGIN SET @ResultCode = 403; RAISERROR('Target StepID already exits', 16, 10); END -- determine Target OrderOfExecution SELECT TOP 1 @TargetOrderOfExecution = OrderOfExecution +1 FROM system.tPipelineSteps WHERE PipelineKey = @TargetPipelineKey ORDER BY OrderOfExecution DESC; -- determine new ID if * is sended IF @TargetStepID = '*' BEGIN SELECT TOP 1 @LastStepNum = TRY_CAST(RIGHT(StepID,LEN(StepID)-1) AS INT) FROM system.tPipelineSteps WHERE PipelineKey = @TargetPipelineKey AND StepID LIKE 'S%' AND LEN(StepID) > 1 AND TRY_CAST(RIGHT(StepID,LEN(StepID)-1) AS INT) IS NOT NULL ORDER BY TRY_CAST(RIGHT(StepID,LEN(StepID)-1) AS INT) DESC IF @LastStepNum IS NULL SET @LastStepNum = 0; SET @TargetStepID = 'S' + CAST(@LastStepNum +1 AS NVARCHAR(50)); END -- Copy the steps of the Pipeline INSERT INTO system.tPipelineSteps ( PipelineKey ,StepID ,StepName ,StepDescription ,OrderOfExecution ,StepDataSourceID ,StepCompanyIDsJSON ,StepDetailsJSON ,TimeExecutionLimit ,Active ,RunConditionCODE ,RestartOnError ) SELECT @TargetPipelineKey ,@TargetStepID ,StepName ,StepDescription ,@TargetOrderOfExecution ,StepDataSourceID ,StepCompanyIDsJSON ,StepDetailsJSON ,TimeExecutionLimit ,Active ,RunConditionCODE ,RestartOnError FROM system.tPipelineSteps WHERE PipelineKey = @SourcePipelineKey AND StepID = @SourceStepID; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 201; SET @OutputID = @TargetStepID; COMMIT TRANSACTION spCOPY_PipelineStep END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spCOPY_PipelineStep IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spCOPY_PipelineStep' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N''; -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to copy a Pipeline - including its steps and schedules'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@SourcePipelineID'; SET @value = N'ID of the Pipeline from which a step will be copied.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceStepID'; SET @value = N'ID for the step which will be copied'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetPipelineID'; SET @value = N'ID for the Pipeline, which is used as target. If keept empty, the source Productline is used as target.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetStepID'; SET @value = N'ID for the step which will be created, it must not exist at the moment.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spCOPY_User; GO /* COPY Operation for a User Gerd Tautenhahn for Saxess Software GmbH Last modified: 02/2023 for OCT 5.9 Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spCOPY_User @Username = 'SQL' ,@CopyUsername = 'W10\admin' ,@TargetUsername = 'W10\Maus2'; PRINT @RC; SELECT * FROM system.trUser; SELECT * FROM system.trRights; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spCOPY_User',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'planning', 'PROCEDURE', 'spCOPY_User','PARAMETER',NULL) */ CREATE PROCEDURE system.spCOPY_User @Username NVARCHAR(255), @CopyUserName NVARCHAR(255), @TargetUsername NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @CopyUserKey INT = 0; DECLARE @TargetUserKey INT = 0; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR (255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR (MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@CopyUserName, N'NULL') + N''',''' + ISNULL(@TargetUserName, N'NULL') + N'''' DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; DECLARE @tmpUserTable TABLE (UserKey INT NOT NULL); -- STEP 0.1 - NULL Protection IF @Username IS NULL SET @Username = N''; IF @CopyUserName IS NULL SET @CopyUserName = N''; IF @TargetUserName IS NULL SET @TargetUserName = N''; BEGIN TRY BEGIN TRANSACTION spCOPY_User; -- STEP 0.2 - Protect input parameters SET @CopyUserName = system.fProtectString (@CopyUserName); SET @TargetUserName = system.fProtectString (@TargetUserName); IF @CopyUserName = N'' OR @TargetUsername = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Don`t allow creation or changing of systemuser SQL -- the Username "db_octservice" is not allowed, as it would lead to confusion with the similar named db role IF @TargetUserName = N'SQL' OR @TargetUserName = N'db_octservice' BEGIN SET @ResultCode = 403; RAISERROR('Forbidden creation or changing of systemuser', 16, 10); END; -- STEP 1.2 - Don't allow posting of members of the DB role db_octservice (which are not visible in the frontend) - not over the function IS_ROLEMEMBER, its buggy IF EXISTS( SELECT r.name FROM sys.database_role_members rm INNER JOIN sys.database_principals r ON rm.role_principal_id = r.principal_id INNER JOIN sys.database_principals m ON rm.member_principal_id = m.principal_id WHERE r.name = 'db_octservice' AND m.name = @TargetUserName ) BEGIN SET @ResultCode = 403; RAISERROR('Don`t allow changes on the members of the DB role db_octservice', 16, 10); END; -- STEP 1.2 - Determine transaction user, the transactuser 'public' is not allowed to change rights SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END ELSE IF @TransactUsername = N'public' BEGIN SET @ResultCode = 403; RAISERROR('''Public'' user is not allowed to change rights', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 2 - Get keys SELECT @CopyUserKey = UserKey FROM system.trUser WHERE UserName = @CopyUserName; SELECT @TargetUserKey = UserKey FROM system.trUser WHERE UserName = @TargetUserName; -- If Copy user not exists, stop action IF @CopyUserKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('User to copy does not exits.', 16, 10); END -- If Target user already exists, stop action IF @TargetUserKey <> 0 BEGIN SET @ResultCode = 403; RAISERROR('Overwiting existing users is not allowed', 16, 10); END --Create Target User and output key into table -- the determination on the table variable is necessary because of TRIGGERS on rUser INSERT INTO system.trUser (UserName, PersonSurname, PersonFirstName, Email, LDAPIP, Status,DataFlowRightsCODE,IsAdministratorFlag) OUTPUT INSERTED.UserKey INTO @tmpUserTable SELECT @TargetUsername ,N'' ,N'' ,N'' ,N'' ,[Status] ,DataFlowRightsCODE ,IsAdministratorFlag FROM system.trUser WHERE UserKey = @CopyUserKey; SET @EffectedRows = 1; SELECT TOP (1) @TargetUserKey = UserKey FROM @tmpUserTable ORDER BY UserKey; SET @Resultcode = 201; -- STEP 3.2 - Copy the rights of the copy User INSERT INTO system.trRights ( UserKey ,UserName ,FactoryID ,ProductLineID ,ProductID ,[Right] ,ReadCommentMandatory ,WriteCommentMandatory ) SELECT @TargetUserKey AS UserKey ,@TargetUsername AS UserName ,FactoryID ,ProductLineID ,N'' AS ProductID ,[Right] ,ReadCommentMandatory ,WriteCommentMandatory FROM system.trRights WHERE UserKey = @CopyUserKey; SET @EffectedRows += @@ROWCOUNT; COMMIT TRANSACTION spCOPY_User; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spCOPY_User; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spCOPY_User' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Copy Operation for a User a new user will be created as copy of an existing user. Rights are copied, user attributes are not copied, user will be set to active.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CopyUserName'; SET @value = N'Name of the user which is basis of the copy.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetUsername'; SET @value = N'Name of the Target User'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spDELETE_Action; GO /* Procedure to DELETE Action information Saxess Software GmbH Testcall DECLARE @RC INT; EXEC @RC = system.spDELETE_Action @Username = 'SQL', @ActionID = 'A1' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spDELETE_Action', NULL, NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spDELETE_Action', 'PARAMETER', NULL) */ CREATE PROCEDURE system.spDELETE_Action @Username NVARCHAR(255), @ActionID NVARCHAR(255) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 2, @Username, @ActionID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Input parameter handling SET @ActionID = COALESCE(system.fProtectID(@ActionID), N''); -- Delete Action, content and versions (through foreign key relationships) DELETE FROM system.tActions WHERE ActionID = @ActionID; SET @AffectedRows = @@ROWCOUNT; IF @AffectedRows = 0 RAISERROR ('Action doesn''t exist', 11, 1); SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 EXEC system.spSEND_Message 'ERROR', @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_Action' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.Actions, Custom) ,@SX_Action NVARCHAR(255) = N'CORE' -- enter Action name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Action; EXEC sys.sp_addextendedproperty N'SX_Action' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET the Action tree'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ActionID'; SET @value = N'ID of the Action which belongs to the requested content.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spDELETE_Content; GO /* Procedure to DELETE content information Saxess Software GmbH Last modified: 10/2023 for OCT 5.10 Testcall DECLARE @RC INT; EXEC @RC = system.spDELETE_Content @Username = 'SQL', @ModuleID = 'FIN', @ContentID = 'result.tFIN_Konten' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload','PARAMETER',NULL) */ CREATE PROCEDURE system.spDELETE_Content @Username NVARCHAR(255), @ModuleID NVARCHAR(255), @ContentID NVARCHAR(255) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 3, @Username, @ModuleID, @ContentID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Input parameter handling SET @ModuleID = COALESCE(system.fProtectID(@ModuleID), N''); SET @ContentID = COALESCE(@ContentID, N''); -- Delete content and versions (through foreign key relationship) DELETE FROM system.tContents WHERE ModuleID = @ModuleID AND ContentID = @ContentID; SET @AffectedRows = @@ROWCOUNT; IF @AffectedRows = 0 RAISERROR ('Content doesn''t exist', 11, 1); SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_Content' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET the module tree'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ModuleID'; SET @value = N'ID of the module which belongs to the requested content.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ContentID'; SET @value = N'ID of the requested content.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spDELETE_DataSource; GO /* Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Procedure to delete a DataSource an all data which belong to it. A Datasource contains mostly three types of data - itselft, its definition of a connnection to an Datasource - the data extracted from this datasource (Companies, Dimensions, Facts) - process definitions for this datasource (Pipelines, Steps..) Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spDELETE_DataSource @Username = 'SQL' ,@DataSourceID = '2' PRINT @RC SELECT * FROM system.tDataSources Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spDELETE_DataSource',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spDELETE_DataSource','PARAMETER',NULL) */ CREATE PROCEDURE system.spDELETE_DataSource ( @Username NVARCHAR(255) ,@DataSourceID NVARCHAR(50) ) AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' --set Strings inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@DataSourceID ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@Tablename NVARCHAR(255) = N'' ,@SQL NVARCHAR(MAX) = N'' ,@DataSourceKey INT; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @DataSourceID IS NULL SET @DataSourceID = N''; -- START TRANSACTION *********************************************************************************** BEGIN TRY BEGIN TRANSACTION spDELETE_DataSource -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR(N'Transaction user don`t exists', 16, 10); END; -- Check for Cluster Write Rights -- Check if Datasource exits SELECT @DataSourceKey = DataSourceKey FROM system.tDataSources WHERE DataSourceID = @DataSourceID; IF @DataSourceKey IS NULL BEGIN SET @ResultCode = 404; RAISERROR(N'Datasource don`t exists', 16, 10); END; -- Generic delete is still done over DatasourceKey, as DatasourceID was till 5.5 the DatasourceKey PRINT ''; PRINT 'Starting generic Delete Process for all Tables outside system containing the Column "DatasourceKey"'; PRINT '#####################################################################################################'; PRINT ''; -- DELETE Date in all Non-Core Tables where the column DataSourceKey is used (usually global and integration schema) -- Generic delete over dynamic SQL is possible in all OCT schema except "system" in default DECLARE TableCursor CURSOR FOR SELECT CONCAT(infs.TABLE_SCHEMA COLLATE DATABASE_DEFAULT,N'.',infs.TABLE_NAME COLLATE DATABASE_DEFAULT) AS Tablename FROM INFORMATION_SCHEMA.COLUMNS infs -- Determination Objekttype LEFT JOIN ( SELECT Name COLLATE DATABASE_DEFAULT AS Objektname ,SCHEMA_NAME(Schema_id) COLLATE DATABASE_DEFAULT AS Schemaname ,type_desc COLLATE DATABASE_DEFAULT AS Objekttyp FROM sys.objects ) syo ON syo.Schemaname = infs.TABLE_SCHEMA AND syo.Objektname = infs.TABLE_NAME WHERE syo.Objekttyp = N'USER_TABLE' AND infs.COLUMN_NAME = N'DataSourceKey' -- ToDo: Determination not a Core table should be moved to metadata AND infs.TABLE_SCHEMA <> N'system' AND CONCAT(infs.TABLE_SCHEMA,N'.',infs.TABLE_NAME) <> N'global.tCompanies' OPEN TableCursor; FETCH TableCursor INTO @Tablename; IF @@CURSOR_ROWS = 0 BEGIN PRINT CONCAT(N'No Tables found for generic deletion over DataSourceKey ',@DataSourceKey,N'.') END WHILE @@FETCH_STATUS = 0 BEGIN -- clear variables SET @SQL = N''; SET @SQL = CONCAT(N'DELETE FROM ', @Tablename, N' WHERE DataSourceKey =', @DataSourceKey,';') -- ToDo: ID is numeric at the moment, change to string when changed PRINT CONCAT(N'Delete all Rows with DatasourceKey ', @DataSourceKey, N' in Table ',@Tablename) EXEC (@SQL); FETCH TableCursor INTO @Tablename; END; CLOSE TableCursor; DEALLOCATE TableCursor; -- Generic delete is still done over CompanyKey -- GET all Non-Core Tables where the column CompanyKey is used (usually result schema) and delete the Values for this ComanpanyKey -- The Company Key is an combinded Key "DataSourceKey | CompanyID" -- some are deleted the second time, as they have the Datasource Key also !! PRINT ''; PRINT 'Starting generic Delete Process for all Tables outside system containing the Column "CompanyKey" which is a concated Key with the Datasource.'; PRINT '#####################################################################################################'; PRINT ''; DECLARE TableCursor CURSOR FOR SELECT CONCAT(infs.TABLE_SCHEMA COLLATE DATABASE_DEFAULT,N'.',infs.TABLE_NAME COLLATE DATABASE_DEFAULT) AS Tablename FROM INFORMATION_SCHEMA.COLUMNS infs -- Determination Objekttype LEFT JOIN ( SELECT Name COLLATE DATABASE_DEFAULT AS Objektname ,SCHEMA_NAME(Schema_id) COLLATE DATABASE_DEFAULT AS Schemaname ,type_desc COLLATE DATABASE_DEFAULT AS Objekttyp FROM sys.objects ) syo ON syo.Schemaname = infs.TABLE_SCHEMA AND syo.Objektname = infs.TABLE_NAME WHERE syo.Objekttyp = N'USER_TABLE' AND infs.COLUMN_NAME = N'CompanyKey' -- ToDo: Determination not a Core table should be moved to metadata AND infs.TABLE_SCHEMA <> N'system' AND CONCAT(infs.TABLE_SCHEMA,N'.',infs.TABLE_NAME) <> N'global.tCompanies'; OPEN TableCursor; FETCH TableCursor INTO @Tablename; IF @@CURSOR_ROWS = 0 BEGIN PRINT CONCAT(N'No Tables found for generic deletion over CompanyKeys starting with ',@DataSourceKey,N'| .') END WHILE @@FETCH_STATUS = 0 BEGIN -- clear variables SET @SQL = N''; SET @SQL = CONCAT(N'DELETE FROM ', @Tablename, N' WHERE CompanyKey LIKE ''', @DataSourceKey,N'|%'';') PRINT CONCAT(N'Delete all Rows with DatasourceKey ', @DataSourceKey, N' in CompanyKey in Table ',@Tablename) EXEC (@SQL); FETCH TableCursor INTO @Tablename; END; CLOSE TableCursor; DEALLOCATE TableCursor; PRINT '' PRINT 'End of generic delition' PRINT '######################################################################' PRINT '' PRINT ''; PRINT 'Starting Delete for Core Data' PRINT '#####################################################################################################'; PRINT ''; -- PIPELINES - Company selection is deleted in all steps, but not the steps themself using the datasource -- system.tPipelineSteps - TODO - in new field CompaniesJSON -- global.tCompanies DELETE FROM global.tCompanies WHERE DataSourceKey = @DataSourceKey; PRINT 'Delete the datasource itself' -- DELETE Datasource (as last due to calculated / dependend columns) DELETE FROM system.tDataSources WHERE DataSourceID = @DataSourceID; SET @ResultCode = 200; COMMIT TRANSACTION spDELETE_DataSource END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spDELETE_DataSource IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT N'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_DataSource' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to delete a Datasouce - the data from its proceeded companies is deleted in all tables with Columns DatasourceKey or CompanyKey too, but the datesource is keept in Process Steps.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@DataSourceID'; SET @value = N'ID of the DataSource to be deleted.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spDELETE_Icon; GO /* Procedure to delete icons Return code 200 if file exists and was deleted succesful Saxess Software GmbH Testcall EXEC system.spDELETE_Icon 'SQL' ,'hase.svg'; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spDELETE_Icon',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spDELETE_Icon','PARAMETER',NULL) */ CREATE PROCEDURE system.spDELETE_Icon @UserName NVARCHAR(255) = N'' ,@IconName NVARCHAR(255) = N'' AS BEGIN SET NOCOUNT ON DECLARE @TransactionUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''', ''' + ISNULL(@IconName, N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @UserName IS NULL SET @UserName = N''; IF @IconName IS NULL SET @IconName = N''; BEGIN TRY BEGIN TRANSACTION DELETE_Icon; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); -- STEP 1.1 - Determine transaction user SELECT @TransactionUsername = system.fDetermineTransactionUsername (@Username); IF @TransactionUsername = N'403' BEGIN SET @ResultCode = 403; THROW 60000, N'Transaction user don`t exists', 100; END; IF NOT EXISTS ( SELECT TOP (1) 1 FROM system.tIcons WHERE IconName = @IconName ) BEGIN SET @ResultCode = 404; THROW 60000, N'File not found', 100; END; DELETE FROM system.tIcons WHERE IconName = @IconName SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; COMMIT TRANSACTION DELETE_Icon; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION DELETE_Icon; IF @Error_state = 100 BEGIN PRINT @Comment; END ELSE IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactionUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_Icon' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Return code 200 if file exists and was deleted succesful'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@UserName'; SET @value = N'UserName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@IconName'; SET @value = N'IconName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spDELETE_Module; GO /* Procedure to DELETE module information Saxess Software GmbH Last modified: 10/2023 for OCT 5.10 Testcall DECLARE @RC INT; EXEC @RC = system.spDELETE_Module @Username = 'SQL', @ModuleID = 'FIN' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload','PARAMETER',NULL) */ CREATE PROCEDURE system.spDELETE_Module @Username NVARCHAR(255), @ModuleID NVARCHAR(255) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 2, @Username, @ModuleID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Input parameter handling SET @ModuleID = COALESCE(system.fProtectID(@ModuleID), N''); -- Delete module, content and versions (through foreign key relationships) DELETE FROM system.tModules WHERE ModuleID = @ModuleID; SET @AffectedRows = @@ROWCOUNT; IF @AffectedRows = 0 RAISERROR ('Module doesn''t exist', 11, 1); SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_Module' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET the module tree'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ModuleID'; SET @value = N'ID of the module which belongs to the requested content.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spDELETE_Pipeline; GO /* Procedure to delete a Pipeline including all Steps Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spDELETE_Pipeline @Username = 'SQL' ,@PipelineID = 'P2' PRINT @RC SELECT * FROM system.tPipelines SELECT * FROM system.tPipelineSteps SELECT * FROM system.tPipelineSchedules Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spDELETE_Pipeline',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spDELETE_Pipeline','PARAMETER',NULL) */ CREATE PROCEDURE system.spDELETE_Pipeline ( @Username NVARCHAR(255) ,@PipelineID NVARCHAR(50) ) AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' --set Strings inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position + ISNULL(@Username ,N'NULL') + N''',' + ISNULL(@PipelineID ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime(); -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @PipelineID IS NULL SET @PipelineID = N''; -- START TRANSACTION *********************************************************************************** BEGIN TRY BEGIN TRANSACTION spDELETE_Pipeline -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; --Right Check !! -- can be executed as PreSQL from any integration user -- must be executeable during import IF NOT EXISTS ( SELECT PipelineID FROM system.tPipelines WHERE PipelineID = @PipelineID ) BEGIN SET @ResultCode = 404; RAISERROR('Pipeline does not exists', 16, 10); END; -- Delete Pipeline, this will also delete Steps and Schedules do to key relation DELETE FROM system.tPipelines WHERE PipelineID = @PipelineID; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; COMMIT TRANSACTION spDELETE_Pipeline END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spDELETE_Pipeline IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_Pipeline' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to delete a Pipeline including steps and schedules.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@PipelineID'; SET @value = N'ID of the Pipeline to delete.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spDELETE_PipelineSchedule; GO /* Procedure to delete one schedules of a pipeline Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spDELETE_PipelineSchedule @Username = 'SQL' ,@PipelineID = 'P1' ,@ScheduleID = 'S1' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spDELETE_PipelineSchedule',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spDELETE_PipelineSchedule','PARAMETER',NULL) */ CREATE PROCEDURE system.spDELETE_PipelineSchedule ( @Username NVARCHAR(255) ,@PipelineID NVARCHAR(50) ,@ScheduleID NVARCHAR(50) ) AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) -- SET PARAMETER LOG STRING -inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@PipelineID ,N'NULL') + N''',''' + ISNULL(@ScheduleID ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime() ,@PipelineKey BIGINT; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @PipelineID IS NULL SET @PipelineID = N''; IF @ScheduleID IS NULL SET @ScheduleID = N''; -- START TRY *********************************************************************************** BEGIN TRY -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- check existence of IDs IF NOT EXISTS ( SELECT PipelineID FROM system.tPipelines WHERE PipelineID = @PipelineID ) BEGIN SET @ResultCode = 404; RAISERROR('Source Pipeline don`t exits', 16, 10); END SELECT @PipelineKey = PipelineKey FROM system.tPipelines WHERE PipelineID = @PipelineID; IF NOT EXISTS ( SELECT ScheduleID FROM system.tPipelineSchedules WHERE PipelineKey = @PipelineKey AND ScheduleID = @ScheduleID ) BEGIN SET @ResultCode = 404; RAISERROR('Schedule don`t exits', 16, 10); END -- Data Transaction DELETE FROM system.tPipelineSchedules WHERE PipelineKey = @PipelineKey AND ScheduleID = @ScheduleID SET @ResultCode = 200; END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_PipelineSchedule' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to delete a schedule.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@PipelineID'; SET @value = N'ID of the Pipeline, which contains the schedule.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ScheduleID'; SET @value = N'ID of the schedule to delete.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spDELETE_PipelineStep; GO /* Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spDELETE_PipelineStep @Username = N'SQL' ,@PipelineID = N'P11' ,@StepID = N'S1'; PRINT @RC; SELECT * FROM system.tPipelineSteps; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spDELETE_PipelineStep',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spDELETE_PipelineStep','PARAMETER',NULL) */ CREATE PROCEDURE system.spDELETE_PipelineStep ( @Username NVARCHAR(255) ,@PipelineID NVARCHAR(50) ,@StepID NVARCHAR(50) ) AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' --set Strings inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position + ISNULL(@Username ,N'NULL') + N''',' + ISNULL(@PipelineID ,N'NULL') + N',''' + ISNULL(@StepID ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime() ,@PipelineKey BIGINT; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @PipelineID IS NULL SET @PipelineID = N''; IF @StepID IS NULL SET @StepID = N''; -- START TRANSACTION *********************************************************************************** BEGIN TRY BEGIN TRANSACTION spDELETE_PipelineStep -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; IF NOT EXISTS ( SELECT PipelineID FROM system.tPipelines WHERE PipelineID = @PipelineID ) BEGIN SET @ResultCode = 404; RAISERROR('PipelineID does not exists', 16, 10); END; SELECT @PipelineKey = PipelineKey FROM system.tPipelines WHERE PipelineID = @PipelineID; IF NOT EXISTS ( SELECT StepID FROM system.tPipelineSteps WHERE PipelineKey = @PipelineKey AND StepID = @StepID ) BEGIN SET @ResultCode = 404; RAISERROR('StepID does not exists', 16, 10); END; -- Data Transaction DELETE FROM system.tPipelineSteps WHERE PipelineKey = @PipelineKey AND StepID = @StepID; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; COMMIT TRANSACTION spDELETE_PipelineStep END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spDELETE_PipelineStep IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_PipelineStep' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to delete a step of a Pipeline.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@PipelineID'; SET @value = N'ID of the Pipeline to delete.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@StepID'; SET @value = N'ID of the Step to delete.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spDELETE_User; GO /* DELETE Operation for a User Deletes a User with all his rights. A cluster administrator can't delete himself Gerd Tautenhahn for Saxess Software GmbH Last modified: 01/2023 for OCT 5.9 Test call 1. DeleteUserName is empty => 404 Not Found 2. Delete command comes from non clusteradmin => 403 Forbidden 3. User wants to delete himself => 403 Forbidden 4. DeleteUser don`t exists => 204 No Content 5. deleted user successful => 200 OK DECLARE @RC INT DECLARE @Username NVARCHAR(255) = 'SQL' DECLARE @DeleteUserName NVARCHAR(255) = 'W10\admin' EXECUTE @RC = system.spDELETE_User @Username, @DeleteUserName PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spDELETE_User',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spDELETE_User','PARAMETER',NULL) */ CREATE PROCEDURE system.spDELETE_User @Username NVARCHAR(255), @DeleteUserName NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@DeleteUserName, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N'' -- NULL Protection IF @Username IS NULL SET @Username = N''; IF @DeleteUserName IS NULL SET @DeleteUserName = N''; BEGIN TRY BEGIN TRANSACTION spDELETE_User; -- Protect Strings SET @Username = system.fProtectString (@Username); SET @DeleteUserName = system.fProtectString (@DeleteUserName); IF @DeleteUserName = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- STEP 1.1 - Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- STEP 1.2 - Stop, if Transactuser wants to delete himself IF @TransactUsername = @DeleteUsername BEGIN SET @ResultCode = 403; RAISERROR('Transaction can`t delete himself', 16, 10); END; -- STEP 2 - Check rights EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 RAISERROR('Invalid rights', 16, 10); -- STEP 3 - delete User DELETE FROM system.trRights WHERE UserName = @DeleteUserName; SET @EffectedRows += @@ROWCOUNT; DELETE FROM system.trUser WHERE UserName = @DeleteUserName; SET @EffectedRows += @@ROWCOUNT; SET @ResultCode = IIF(@@ROWCOUNT = 0, 204, 200); COMMIT TRANSACTION spDELETE_User; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spDELETE_User; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spDELETE_User' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Deletes a User with all his rights. A cluster administrator cant delete himself'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@DeleteUserName'; SET @value = N'DeleteUserName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spEXPORT_Action; GO /* Procedure to EXPORT Action information in JSON format Saxess Software GmbH Testcall DECLARE @RC INT; EXEC @RC = system.spEXPORT_Action @Username = 'SQL', @ActionIDJSON = '["A1", "A2"]', @ExportType = 'JSON'; SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spEXPORT_Action', NULL, NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spEXPORT_Action', 'PARAMETER', NULL) */ CREATE PROCEDURE system.spEXPORT_Action @Username NVARCHAR(255), @ActionIDJSON NVARCHAR(MAX), @ExportType NVARCHAR(50) = 'JSON' AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 3, @Username, @ActionIDJSON, @ExportType; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Input parameter handling IF ISJSON(@ActionIDJSON) = 0 EXEC system.spSEND_Message 'ERROR', 'Invalid JSON in ActionIDJSON parameter.'; IF @ExportType = 'JSON' BEGIN -- Return Action information SELECT ( SELECT a.ActionID , a.OrderIndex , a.Active , a.Name , a.Description , a.RequiresClusterWriteRights , a.RequiresWriteRights , a.Condition , a.ListCustomTitle , a.ListSQL , a.ListLayout , a.ListExcelExport , a.SetCellValueAsFilter , a.ReloadListAfterChangingFilter , a.ActionSQL , a.ApplyFromField , a.SaveBeforeAction , a.SaveAfterAction , a.ReloadTreeAfterAction , a.ReloadDetailsAfterAction FROM system.tActions AS a JOIN OPENJSON(@ActionIDJSON) WITH (ActionID NVARCHAR(50) '$') AS ActionIDList ON ActionIDList.ActionID = a.ActionID FOR JSON PATH, ROOT('Actions') ) AS Command; SET @AffectedRows = @@ROWCOUNT; IF @AffectedRows = 0 EXEC system.spSEND_Message 'ERROR', 'No Action found'; END IF @ExportType = 'SQL' BEGIN -- Create a temporary table to store the collection of export commands DROP TABLE IF EXISTS #tExport; CREATE TABLE #tExport ( RowKey BIGINT IDENTITY(1,1) , MainOrderNumber INT NOT NULL , SubOrderNumber INT NOT NULL , Command NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL , PRIMARY KEY CLUSTERED (RowKey) ); INSERT INTO #tExport VALUES (10, 1, N'-- CONFIG: Adjust variable values manually to fit your needs!') , (10, 2, N'DECLARE @Username NVARCHAR(255) = ''SQL''') , (10, 3, N'') -- Create the connector as new connector INSERT INTO #tExport SELECT 1000 AS MainOrderNumber , 1000 AS SubOrderNumber , CONCAT( 'EXEC system.spPOST_Action' , ' @Username = @Username' , ', @ActionID = ''*''' , ', @OrderIndex = NULL' , ', @Active = ', a.Active , ', @Name = ''', system.fMaskSQL(a.Name), '''' , ', @Description = ''', system.fMaskSQL(a.Description), '''' , ', @RequiresClusterWriteRights = ', a.RequiresClusterWriteRights , ', @RequiresWriteRights = ', a.RequiresWriteRights , ', @Condition = ''', system.fMaskSQL(a.Condition), '''' , ', @ListCustomTitle = ''', system.fMaskSQL(a.ListCustomTitle), '''' , ', @ListSQL = ''', system.fMaskSQL(a.ListSQL), '''' , ', @ListLayout = ''', system.fMaskSQL(a.ListLayout), '''' , ', @ListExcelExport = ', a.ListExcelExport , ', @SetCellValueAsFilter = ', a.SetCellValueAsFilter , ', @ReloadListAfterChangingFilter = ', a.ReloadListAfterChangingFilter , ', @ActionSQL = ''', system.fMaskSQL(a.ActionSQL), '''' , ', @ApplyFromField = ''', system.fMaskSQL(a.ApplyFromField), '''' , ', @SaveBeforeAction = ''', a.SaveBeforeAction, '''' , ', @SaveAfterAction = ', a.SaveAfterAction , ', @ReloadTreeAfterAction = ', a.ReloadTreeAfterAction , ', @ReloadDetailsAfterAction = ', a.ReloadDetailsAfterAction ) AS Command FROM system.tActions AS a JOIN OPENJSON(@ActionIDJSON) WITH (ActionID NVARCHAR(50) '$') AS ActionIDList ON ActionIDList.ActionID = a.ActionID; -- Final GO INSERT INTO #tExport VALUES (9999, 1, N'GO'); -- Data Transaction SELECT Command FROM #tExport ORDER BY MainOrderNumber , SubOrderNumber END SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 EXEC system.spSEND_Message 'ERROR', @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spEXPORT_Action' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.Actions, Custom) ,@SX_Action NVARCHAR(255) = N'CORE' -- enter Action name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Action; EXEC sys.sp_addextendedproperty N'SX_Action' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to EXPORT an Action in JSON format.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ActionIDJSON'; SET @value = N'ID of the requested Actions in JSON format.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spEXPORT_Module; GO /* Procedure to EXPORT module information in JSON format Saxess Software GmbH Last modified: 03/2024 for OCT 5.10 Testcall DECLARE @RC INT; EXEC @RC = system.spEXPORT_Module @Username = 'SQL', @ModuleID = 'FIN' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spEXPORT_Module',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spEXPORT_Module','PARAMETER',NULL) */ CREATE PROCEDURE system.spEXPORT_Module @Username NVARCHAR(255), @ModuleID NVARCHAR(255) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 2, @Username, @ModuleID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Input parameter handling SET @ModuleID = COALESCE(system.fProtectID(@ModuleID), N''); -- Return module information SELECT ( SELECT m.ModuleID AS ModuleName , m.ModuleType AS ModuleType , m.Description AS ModuleDescription , m.Version AS ModuleVersion , m.AdminRequired AS AdminRequired , m.DescriptionURL AS DescriptionURL , ( SELECT c.ContentID AS ContentName , c.ContentType AS ContentType , c.TechnicalType AS TechnicalType , c.Description AS ContentDescription , c.OrderIndex AS OrderIndex , cv.ContentSQL AS ContentValue , cv.ContentUninstallSQL AS ContentDelete , cv.Version AS ContentVersion FROM system.tContents AS c LEFT JOIN system.tContentVersions AS cv ON cv.ContentKey = c.ContentKey AND cv.Version = ( SELECT MAX(Version) FROM system.tContentVersions AS cvMax WHERE cvMax.ContentKey = c.ContentKey ) WHERE c.ModuleKey = m.ModuleKey ORDER BY OrderIndex FOR JSON PATH ) AS 'ModuleContent' FROM system.tModules AS m WHERE m.ModuleID = @ModuleID FOR JSON PATH, ROOT('Module') ) AS Command; SET @AffectedRows = @@ROWCOUNT; IF @AffectedRows = 0 RAISERROR ('Module doesn''t exist', 11, 1); SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spEXPORT_Module' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET the module tree'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ModuleID'; SET @value = N'ID of the requested module.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spEXPORT_Pipeline; GO /* Procedure to create an OCT Importscript for an selected Pipeline - used for export / import of pipelines - exports the pipeline with its steps an schedules - NO global parameters used by the pipeline are exported (would be possible, but complex JSON operation) - conditions and consequences - a new Pipeline with the next free ID is created - the user must after the import or before - create a datasource(s) with type fitting to the used steps - download / activate modules needed by the steps - select companies in the datasource an in the steps - create global parameters used in the steps Saxess Software GmbH Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spEXPORT_Pipeline @Username = 'SQL' ,@PipelineID = 'P1' ,@ExportType = 'SQL' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spEXPORT_Pipeline',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spEXPORT_Pipeline','PARAMETER',NULL) */ CREATE PROCEDURE system.spEXPORT_Pipeline @Username NVARCHAR(255), @PipelineID NVARCHAR(50), @ExportType NVARCHAR(50) = 'SQL' -- aus Kompatibilitätsgründen AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 3, @Username, @PipelineID, @ExportType; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Procedure specific declarations DECLARE @SourceClusterName NVARCHAR(255) DECLARE @SourceClusterAPI NVARCHAR(255); -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @PipelineID IS NULL SET @PipelineID = N''; IF @ExportType = 'SQL' BEGIN -- Create a temporary table to store the collection of export commands DROP TABLE IF EXISTS #tExport; CREATE TABLE #tExport ( RowKey BIGINT IDENTITY (1,1) ,MainOrderNumber INT NOT NULL ,SubOrderNumber INT NOT NULL ,Command NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,PRIMARY KEY CLUSTERED (RowKey) ); -- Determine Metadata SELECT @SourceClusterName = ValueText FROM system.tSettings WHERE SettingID = 'Clustername'; -- May be empty if not defined SET @SourceClusterName = CONCAT(DB_Name(),' ',@SourceClusterName); SELECT @SourceClusterAPI = ValueText FROM system.tSettings WHERE SettingID = 'DBVersion'; -- MetaHeader INSERT INTO #tExport VALUES (0, 1, N'-- {') ,(0, 2, N'-- "Type": "Pipeline",') ,(0, 3, CONCAT(N'-- "SourceClusterName": "', @SourceClusterName,'",')) ,(0, 4, CONCAT(N'-- "SourceClusterAPI": "',@SourceClusterAPI,'",')) ,(0, 5, CONCAT(N'-- "SourcePipelineID": "',@PipelineID,'"')) ,(0, 6, N'-- }') INSERT INTO #tExport VALUES (10, 1, N'--CONFIG: Adjust variable values manually to fit your needs !') ,(10, 2, N'DECLARE @Username NVARCHAR(255) = ''SQL''') ,(10, 3, N'DECLARE @PipelineID NVARCHAR(255) = ''' + @PipelineID + N'''') ,(10, 4, N'--This PipelneID will be deleted during import, if it exists. You should be sure !') ,(10, 5, N'DECLARE @ReturnedPipelineID NVARCHAR (50);'); -- Try_Delete existing Pipeline INSERT INTO #tExport SELECT 900 AS MainOrderNumber ,1000 AS SubOrderNumber ,CONCAT( 'EXEC system.spDELETE_Pipeline ' ,'''SQL''' ,',@PipelineID' ,';' ) AS Command FROM system.tPipelines WHERE PipelineID = @PipelineID; -- Create the Pipeline as new Pipeline and catch the ID INSERT INTO #tExport SELECT 1000 AS MainOrderNumber ,1000 AS SubOrderNumber ,CONCAT( 'EXEC system.spPOST_Pipeline ' ,'''SQL''' ,',@PipelineID' ,',''' ,system.fMaskSQL(PipelineName) ,'''' ,',''' ,system.fMaskSQL(PipelineDescription),'''' ,',''' ,PipelineDescriptionIconColor ,'''' ,',' ,'NULL' ---NULL leads to positon at the end ,',''' ,EmailNotificationCODE , '''' ,',''' ,ExcludedExecutionTimesJSON , '''' ,', @OutputID = @ReturnedPipelineID OUTPUT;' ) AS Command FROM system.tPipelines WHERE PipelineID = @PipelineID; -- Create the Steps INSERT INTO #tExport SELECT 2000 AS MainOrderNumber ,1000 AS SubOrderNumber ,CONCAT( 'EXEC system.spPOST_PipelineStep ' ,'''SQL''' ,',' ,'@ReturnedPipelineID' ,',''*''' ,',''' ,system.fMaskSQL(StepName) ,'''' ,',''' ,system.fMaskSQL(StepDescription) ,'''' ,',' ,OrderOfExecution ,',''' ,StepDataSourceID , '''' ,',''' ,StepCompanyIDsJSON , '''' ,',''' ,system.fMaskSQL(StepDetailsJSON) , '''' --inverted commas inside sql statements etc. are handled by the pMaskSQL function ,',' ,TimeExecutionLimit ,',' ,Active ,',''''' -- keep old ModuleID parameter for compatibility reasons ,',''' ,RunConditionCODE ,'''' ,',' ,RestartOnError ,';' ) AS Command FROM system.tPipelineSteps tPS INNER JOIN system.tPipelines tP ON tPS.PipelineKey = tP.PipelineKey WHERE tP.PipelineID = @PipelineID ORDER BY OrderOfExecution; -- Create the Schedulers INSERT INTO #tExport SELECT 3000 AS MainOrderNumber ,1000 AS SubOrderNumber ,CONCAT( 'EXEC system.spPOST_PipelineSchedule ' ,'''SQL''' ,',' ,'@ReturnedPipelineID' ,',''*''' ,',''' ,ScheduleName ,'''' ,',''' ,system.fMaskSQL(ScheduleDescription),'''' ,',''' ,system.fMaskSQL(ScheduleDetailsJSON),'''' ,',' ,Active ,';' ) AS Command FROM system.tPipelineSchedules tPS INNER JOIN system.tPipelines tP ON tPS.PipelineKey = tP.PipelineKey WHERE tP.PipelineID = @PipelineID; -- final GO INSERT INTO #tExport VALUES (9999, 1, N'GO'); -- Data Transaction SELECT Command FROM #tExport ORDER BY MainOrderNumber ,SubOrderNumber END IF @ExportType = 'JSON' BEGIN -- Return Pipeline information as JSON SELECT ( SELECT p.PipelineID , p.PipelineName , p.PipelineDescription , p.PipelineDescriptionIconColor , p.OrderOfDisplay , p.EmailNotificationCODE , p.ExcludedExecutionTimesJSON , ( SELECT ps.StepID, ps.StepName, ps.StepDescription, ps.OrderOfExecution, ps.StepDataSourceID, ps.StepCompanyIDsJSON, ps.StepDetailsJSON, ps.TimeExecutionLimit, ps.Active, ps.RunConditionCODE, ps.RestartOnError FROM system.tPipelineSteps AS ps WHERE ps.PipelineKey = p.PipelineKey FOR JSON PATH ) AS Steps , ( SELECT psch.ScheduleID, psch.ScheduleName, psch.ScheduleDescription, psch.ScheduleDetailsJSON, psch.Active FROM system.tPipelineSchedules AS psch WHERE psch.PipelineKey = p.PipelineKey FOR JSON PATH ) AS Schedules FROM system.tPipelines AS p FOR JSON PATH, ROOT('Pipelines') ) AS Command; SET @AffectedRows = @@ROWCOUNT; IF @AffectedRows = 0 EXEC system.spSEND_Message 'ERROR', 'No Action found'; END SET @ResultCode = 200; END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 EXEC system.spSEND_Message 'ERROR', @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spEXPORT_Pipeline' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to create an SQL Script or JSON object which can be exported.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@PipelineID'; SET @value = N'ID of the Pipeline to be exported.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ExportType'; SET @value = N'As export type either SQL (default) or JSON.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_ActionLog; GO /* Procedure to get all Action logs Saxess Software GmbH Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spGET_ActionLog @Username = 'SQL'; SELECT @RC; SELECT * FROM system.tActionLog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_ActionLog', NULL, NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_ActionLog', 'PARAMETER', NULL) */ CREATE PROCEDURE system.spGET_ActionLog @Username NVARCHAR(255) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 1, @Username; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- NULL Protection for input parameters IF @Username IS NULL SET @Username = N''; -- Output SELECT ExecutionUser , ActionID , StartLocation , ExecutionTime , ExecutionType , ExecutionSQL FROM system.tActionLog ORDER BY ExecutionTime DESC; SET @AffectedRows = @@ROWCOUNT; SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 EXEC system.spSEND_Message 'ERROR', @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ActionLog' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to get all pipeline and step logs without details.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_Actions; GO /* Procedure to GET Actions information Saxess Software GmbH Testcall DECLARE @RC INT; EXEC @RC = system.spGET_Actions @Username = 'SQL' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Actions',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Actions','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_Actions @Username NVARCHAR(255) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 1, @Username; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Return Actions information SELECT ActionID , OrderIndex , Active , Name , Description , RequiresClusterWriteRights , RequiresWriteRights , Condition , ListCustomTitle , ListSQL , ListLayout , ListExcelExport , SetCellValueAsFilter , ReloadListAfterChangingFilter , ActionSQL , ApplyFromField , SaveBeforeAction , SaveAfterAction , ReloadTreeAfterAction , ReloadDetailsAfterAction FROM system.tActions ORDER BY OrderIndex ASC; SET @AffectedRows = @@ROWCOUNT; SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 EXEC system.spSEND_Message 'ERROR', @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Actions' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.Actionss, Custom) ,@SX_Actions NVARCHAR(255) = N'CORE' -- enter Actions name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Actions; EXEC sys.sp_addextendedproperty N'SX_Actions' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET the Actions'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_APILog; GO /* Procedure to read the last @RowCount Rows from the API Log table Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.9 Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spGET_APILog @Username = 'SQL' ,@RowCount = 1000; PRINT @RC; Testcall BaseTable SELECT * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_APILog',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_APILog','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_APILog ( @Username NVARCHAR(255) ,@RowCount INT ) AS BEGIN SET NOCOUNT ON; -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) -- SET PARAMETER LOG STRING -inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',' + ISNULL(CAST(@RowCount AS NVARCHAR(255)) ,N'NULL') + N'' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N''; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @RowCount IS NULL SET @RowCount = 0; -- START TRANSACTION *********************************************************************************** BEGIN TRY -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode <> 200 AND NOT @TransactUsername = 'db_octservice' BEGIN SET @ResultCode = 403; RAISERROR('Invalid rights', 16, 10); END -- Data Transaction SELECT TOP (@RowCount) * FROM system.tAPILog ORDER BY LogKey DESC; SET @ResultCode = 200; END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_APILog' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N''; -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure returns the last @RowCount Rows from the API Log table'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@RowCount'; SET @value = N'Number of rows the procedure should retun.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_Content; GO /* Procedure to GET content information Saxess Software GmbH Last modified: 10/2023 for OCT 5.10 Testcall DECLARE @RC INT; EXEC @RC = system.spGET_Content @Username = 'SQL', @ModuleID = 'FIN', @ContentID = 'result.tFIN_Konten' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_Content @Username NVARCHAR(255), @ModuleID NVARCHAR(255), @ContentID NVARCHAR(255) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 3, @Username, @ModuleID, @ContentID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Variables DECLARE @Version INT; -- Input parameter handling SET @ModuleID = COALESCE(system.fProtectID(@ModuleID), N''); SET @ContentID = COALESCE(@ContentID, N''); -- Get current version SELECT @Version = MAX(Version) FROM system.tContentVersions WHERE ModuleID = @ModuleID AND ContentID = @ContentID; -- Return content information SELECT c.ModuleID , c.ContentID , c.ContentType , c.TechnicalType , c.Description , c.OrderIndex , cv.Version , cv.ContentSQL , cv.ContentUninstallSQL , cv.CreatedBy , cv.CreatedDate , cv.VersionComment , cv.IsInstalled FROM system.tContents AS c LEFT JOIN system.tContentVersions AS cv ON cv.ContentKey = c.ContentKey AND cv.Version = @Version WHERE c.ModuleID = @ModuleID AND c.ContentID = @ContentID; SET @AffectedRows = @@ROWCOUNT; IF @AffectedRows = 0 RAISERROR ('Content doesn''t exist', 11, 1); SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Content' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET the module tree'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ModuleID'; SET @value = N'ID of the module which belongs to the requested content.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ContentID'; SET @value = N'ID of the requested content.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_ContentHistory; GO /* Procedure to GET the content version history Saxess Software GmbH Last modified: 10/2023 for OCT 5.10 Testcall DECLARE @RC INT; EXEC @RC = system.spGET_ContentHistory @Username = 'SQL', @ModuleID = 'FIN', @ContentID = 'result.tFIN_Konten' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_ContentHistory @Username NVARCHAR(255), @ModuleID NVARCHAR(255), @ContentID NVARCHAR(255) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 3, @Username, @ModuleID, @ContentID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Input parameter handling SET @ModuleID = COALESCE(system.fProtectID(@ModuleID), N''); SET @ContentID = COALESCE(@ContentID, N''); -- Return content history SELECT cv.Version , cv.ContentSQL , cv.ContentUninstallSQL , cv.CreatedBy , cv.CreatedDate , cv.VersionComment FROM system.tContents AS c LEFT JOIN system.tContentVersions AS cv ON cv.ContentKey = c.ContentKey WHERE c.ModuleID = @ModuleID AND c.ContentID = @ContentID; SET @AffectedRows = @@ROWCOUNT; IF @AffectedRows = 0 RAISERROR ('Content doesn''t exist', 11, 1); SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ContentHistory' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET the module tree'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ModuleID'; SET @value = N'ID of the module which belongs to the requested content.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ContentID'; SET @value = N'ID of the requested content.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_Datasources; GO /* Procedure to GET Datasources information Saxess Software GmbH Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spGET_Datasources @Username = N'SQL' , @DataSourceID = N'' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Datasources', NULL, NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Datasources', 'PARAMETER', NULL) */ CREATE PROCEDURE system.spGET_Datasources @Username NVARCHAR(255) , @DataSourceID NVARCHAR(50) = N'' AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 2, @Username, @DataSourceID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Input parameter handling SET @DataSourceID = COALESCE(system.fProtectID(@DataSourceID), N''); -- Return Datasources information SELECT DataSourceKey , DataSourceID , DataSourceName , SourceType , ConnectionValuesJSON FROM system.tDataSources WHERE DataSourceID = @DataSourceID OR @DataSourceID = ''; SET @AffectedRows = @@ROWCOUNT; SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 EXEC system.spSEND_Message N'ERROR', @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Datasources' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET one or all Datasourcess.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@DataSourceID'; SET @value = N'DataSourceID - ID of the data source.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_DBInfo; GO /* Procedure to get basic informations about one Database. Informations can be requested - from OCTService for an User - from OCTService for himself (passing SQL as Clientusername means check the Current SQL User) TODO: - resolve Rolemembership when assigned over Security Groups - check AccessRight (implicit sysadmin) over AzureAD SQL Server Administrator Group Membership Infolevel - 0 = information it clientuser can access this cluster or not - 1 = all the Service needs to display a tile - access - tileimage - number of users - database version - 2 = full Informations - user role - database size Gerd Tautenhahn for Saxess Software GmbH Last modified: 02/2023 for OCT 5.9 Testcall Procedure -- Service for an user DECLARE @RC INT; EXEC @RC = system.spGET_DBInfo @Username = 'SQL' ,@ClientUser = 'gerd.tautenhahn@saxess-software.com' ,@InfoLevel = 2; PRINT @RC; -- Service for Service DECLARE @RC INT; EXEC @RC = system.spGET_DBInfo @Username = 'SQL' ,@ClientUser = 'SQL' ,@InfoLevel = 2; PRINT @RC; SELECT TOP 3 * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_DBInfo',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_DBInfo','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_DBInfo @Username NVARCHAR(255) ,@ClientUser NVARCHAR(255) ,@InfoLevel INT = 1 AS BEGIN -- SET NOCOUNT ON; -- activate this, if you read the resultset with an external source -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) -- SET PARAMETER LOG STRING -inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@ClientUser ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime() ,@IsOCTService BIT = 0; -- NULL Protection for all mandatory Input parameters IF @Username IS NULL SET @Username = N''; IF @ClientUser IS NULL SET @ClientUser = N''; -- The string SQL means the octservice asks for himself IF @ClientUser = 'SQL' SELECT @ClientUser = CURRENT_USER; -- !! Clientuser will never exist in OCTUsermanagement -- !! Clientuser may be 'dbo' or an AzureActive Directory Identity -- passing SQL as Clientusername means check the Current SQL User IF EXISTS ( SELECT DP1.[name] FROM sys.database_role_members DRM RIGHT OUTER JOIN sys.database_principals DP1 ON DRM.role_principal_id = DP1.principal_id LEFT OUTER JOIN sys.database_principals DP2 ON DRM.member_principal_id = DP2.principal_id WHERE DP1.[type] = 'R' AND DP2.[name] = @ClientUser AND DP1.[name] IN ('db_octservice','db_owner') ) SET @IsOCTService = 1; -- is OCTService or can act like OCTService due to db_owner status DECLARE @ClientUserAccess_Flag INT = 0; -- START TRY *********************************************************************************** BEGIN TRY ---- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN -- nothing, to enable Footprint Protection PRINT 'Unknown user - eliminate footprint' -- SET @ResultCode = 403; -- RAISERROR('Transaction user don`t exists', 16, 10); END; -- Caching table DROP TABLE IF EXISTS #tInfoCache; CREATE TABLE #tInfoCache ( RowKey BIGINT IDENTITY (1,1) ,PropertyID NVARCHAR(50) COLLATE DATABASE_DEFAULT NOT NULL ,PropertyName NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,InfoLevel INT NOT NULL ,ValueInt INT NOT NULL ,ValueMoney MONEY NOT NULL ,ValueText NVARCHAR(4000) COLLATE DATABASE_DEFAULT NOT NULL ,ValueDatetime DATETIME NULL ,ValueBinary VARBINARY(MAX) NULL --max 8000 Byte to avoid Varbinary(MAX) ,PRIMARY KEY CLUSTERED (RowKey) ); -- INFO LEVEL 0 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -- check AccessRight for ClientUser IF @IsOCTService = 1 BEGIN SET @ClientUserAccess_Flag = 1; END ELSE BEGIN SELECT @ClientUserAccess_Flag = COUNT(UserKey) FROM system.trUser WHERE UserName = @ClientUser AND Status = 'Active'; END -- check if database has public user IF @ClientUserAccess_Flag = 0 AND EXISTS(SELECT 1 FROM system.trUser WHERE UserName = 'public' AND Status = 'Active') BEGIN SET @ClientUserAccess_Flag = 1 END INSERT INTO #tInfoCache SELECT 'CU01' AS PropertyID ,'Flag for ClientUser is an active User' AS PropertyName ,0 AS InfoLevel ,@ClientUserAccess_Flag AS ValueInt ,0 AS ValueMoney ,'' AS ValueText ,NULL AS ValueDatetime ,NULL AS ValueBinary; -- Footprint protection IF @ClientUserAccess_Flag = 0 BEGIN SET @ParameterString = REPLACE (@ParameterString,@ClientUser,''); SET @Username = ''; END -- INFO LEVEL 1 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -- Only if ClientUser has Access, further Informations are delivered (valid for all Info Levels) IF @ClientUserAccess_Flag = 1 BEGIN -- Clustername INSERT INTO #tInfoCache SELECT 'C01' AS PropertyID ,'ClusterName' AS PropertyName ,1 AS InfoLevel ,0 AS ValueInt ,0 AS ValueMoney ,'' AS ValueText ,NULL AS ValueDatetime ,NULL AS ValueBinary; UPDATE #tInfoCache SET ValueText = S.ValueText FROM system.tSettings S WHERE S.SettingID = 'ClusterName' AND #tInfoCache.PropertyID = 'C01'; -- Clusterdescription INSERT INTO #tInfoCache SELECT 'C02' AS PropertyID ,'ClusterDescription' AS PropertyName ,1 AS InfoLevel ,0 AS ValueInt ,0 AS ValueMoney ,'' AS ValueText ,NULL AS ValueDatetime ,NULL AS ValueBinary; UPDATE #tInfoCache SET ValueText = S.ValueText FROM system.tSettings S WHERE S.SettingID = 'ClusterDescription' AND #tInfoCache.PropertyID = 'C02'; -- Clusterversion INSERT INTO #tInfoCache SELECT 'C03' AS PropertyID ,'OCT.core Database Version' AS PropertyName ,1 AS InfoLevel ,0 AS ValueInt ,0 AS ValueMoney ,ValueText AS ValueText ,NULL AS ValueDatetime ,NULL AS ValueBinary FROM system.tSettings WHERE SettingID = 'DBVersion'; -- Cluster_Tile_Logo INSERT INTO #tInfoCache SELECT 'C04' AS PropertyID ,'Cluster_Tile_Logo' AS PropertyName ,1 AS InfoLevel ,0 AS ValueInt ,0 AS ValueMoney ,'' AS ValueText ,NULL AS ValueDatetime ,NULL AS ValueBinary; UPDATE #tInfoCache SET ValueBinary = FileBody FROM planning.tFiles WHERE FileID = 'Cluster_Tile_Logo' AND #tInfoCache.PropertyID = 'C04'; -- Maintenance Mode -> this property exists only after first saving of database properties INSERT INTO #tInfoCache SELECT 'C05' AS PropertyID ,'Cluster is in Maintenance Mode Flag' AS PropertyName ,1 AS InfoLevel ,0 AS ValueInt ,0 AS ValueMoney ,'' AS ValueText ,NULL AS ValueDatetime ,NULL AS ValueBinary; UPDATE #tInfoCache SET ValueInt = S.ValueInt FROM system.tSettings S WHERE S.SettingID = 'MaintenanceMode' AND #tInfoCache.PropertyID = 'C05'; -- Scheduler Owner -> this property exists only after first saving of database properties INSERT INTO #tInfoCache SELECT 'C06' AS PropertyID ,'IntegrationPreferencesSchedulerOwnerServer' AS PropertyName ,1 AS InfoLevel ,0 AS ValueInt ,0 AS ValueMoney ,'' AS ValueText ,NULL AS ValueDatetime ,NULL AS ValueBinary; UPDATE #tInfoCache SET ValueText = S.ValueText FROM system.tSettings S WHERE S.SettingID = 'IntegrationPreferencesSchedulerOwnerServer' AND #tInfoCache.PropertyID = 'C06'; -- Cluster_Logo INSERT INTO #tInfoCache SELECT 'C07' AS PropertyID ,'Cluster_Logo' AS PropertyName ,1 AS InfoLevel ,0 AS ValueInt ,0 AS ValueMoney ,'' AS ValueText ,NULL AS ValueDatetime ,NULL AS ValueBinary; UPDATE #tInfoCache SET ValueBinary = FileBody FROM planning.tFiles WHERE FileID = 'Cluster_Logo' AND #tInfoCache.PropertyID = 'C07'; -- Usercount for Licence information INSERT INTO #tInfoCache SELECT 'C10' AS PropertyID ,'Number of active Users' AS PropertyName ,1 AS InfoLevel ,COUNT(UserKey) AS ValueInt ,0 AS ValueMoney ,'' AS ValueText ,NULL AS ValueDatetime ,NULL AS ValueBinary FROM system.trUser WHERE Status = 'Active'; -- Information, if the active User is an OCT Clusteradmin INSERT INTO #tInfoCache SELECT 'CU01' AS PropertyID ,'User is OCT Administrator' AS PropertyName ,1 AS InfoLevel ,COUNT(UserKey) AS ValueInt ,0 AS ValueMoney ,'' AS ValueText ,NULL AS ValueDatetime ,NULL AS ValueBinary FROM system.trUser WHERE UserName = @ClientUser AND IsAdministratorFlag = 1; END -- INFO LEVEL 2 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -- Informations for higher Info Levels IF @ClientUserAccess_Flag = 1 AND @InfoLevel >= 2 BEGIN -- Databaseroles of the Clientuser -- Works only if the Clientuser is not the dbo -- returns no Serverroles -- returns no roles if the dabaserole is inherited from a serverrole (e.g. somebody has db_owner role due to sysadmin login etc.) INSERT INTO #tInfoCache SELECT 'CU10' AS PropertyID ,'Database Roles of the Clientuser' AS PropertyName ,2 AS InfoLevel ,0 AS ValueInt ,0 AS ValueMoney ,COALESCE( STUFF( -- Stuffs all names in one list ( SELECT DP1.[name] +', ' FROM sys.database_role_members DRM RIGHT OUTER JOIN sys.database_principals DP1 ON DRM.role_principal_id = DP1.principal_id LEFT OUTER JOIN sys.database_principals DP2 ON DRM.member_principal_id = DP2.principal_id WHERE DP1.[type] = 'R' AND DP2.[name] = @ClientUser FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,0,'' ) , 'No direct assigned Databaseroles (Serveroles,dbo status, groupmemberships or admin status over AAD may be given !)' ) AS ValueText --Databaserolename ,NULL AS ValueDatetime ,NULL AS ValueBinary; -- dbo determination - its different on Azure and on-prem IF CONVERT(NVARCHAR(255),SERVERPROPERTY ('Edition')) LIKE '%Azure%' BEGIN INSERT INTO #tInfoCache SELECT 'DB01' AS PropertyID ,'Name or sid of dbo' AS PropertyName ,2 AS InfoLevel ,0 AS ValueInt ,0 AS ValueMoney ,CONVERT(NVARCHAR(255),sid,2) AS ValueText ,NULL AS ValueDatetime ,NULL AS ValueBinary FROM sys.sysusers where name = 'dbo' END ELSE -- on prem BEGIN INSERT INTO #tInfoCache SELECT 'DB01' AS PropertyID ,'Name of dbo' AS PropertyName ,2 AS InfoLevel ,0 AS ValueInt ,0 AS ValueMoney ,SUSER_SNAME(owner_sid) AS ValueText ,NULL AS ValueDatetime ,NULL AS ValueBinary FROM sys.databases WHERE name = DB_NAME(); END -- Database Rows Size INSERT INTO #tInfoCache SELECT 'DB02' AS PropertyID ,'Database Size (Rows) in GB' AS PropertyName ,2 AS InfoLevel ,0 AS ValueInt ,SUM(CAST(size AS MONEY) * 8/1024/1024) AS ValueMoney ,'' AS ValueText ,NULL AS ValueDatetime ,NULL AS ValueBinary FROM sys.database_files WHERE type = 0; -- Database Log Size INSERT INTO #tInfoCache SELECT 'DB03' AS PropertyID ,'Database Size (Log) in GB' AS PropertyName ,2 AS InfoLevel ,0 AS ValueInt ,SUM(CAST(size AS MONEY) * 8/1024/1024) AS ValueMoney ,'' AS ValueText ,NULL AS ValueDatetime ,NULL AS ValueBinary FROM sys.database_files WHERE type = 1; -- Options to To: -- Last Database Read Access -- Last Database Write Access -- Number of scheduled Pipelines END -- OUTPUT SELECT * FROM #tInfoCache WHERE InfoLevel <= @InfoLevel ORDER BY InfoLevel, PropertyID; SET @ResultCode = 200; END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_DBInfo' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1; -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to get a collection of database informations. Pass the Clientusername SQL to request the rights of the service.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@ClientUser'; SET @value = N'Not logged if its not a clustermember to protect from footprinting access scans.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@InfoLevel'; SET @value = N'Control the amount of informations deliverd. 1 = Basic Info, 2 = extended Info.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_Emails; GO /* Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spGET_Emails @Username = 'SQL' ,@EmailID = '' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Emails',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Emails','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_Emails ( @Username NVARCHAR(255) ,@EmailID NVARCHAR(50) = '' ) AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) -- SET PARAMETER LOG STRING -inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',' + ISNULL(@EmailID ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime(); -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @EmailID IS NULL SET @EmailID = N''; -- START TRY *********************************************************************************** BEGIN TRY -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- check existence of IDs IF @EmailID <> '' AND NOT EXISTS ( SELECT EmailID FROM system.tEmails WHERE EmailID = @EmailID ) BEGIN SET @ResultCode = 404; RAISERROR('Email don`t exits', 16, 10); END -- Data Transaction SELECT EmailID ,EmailSubject ,EmailRecipients ,EmailText ,EmailAttachments ,EmailStatusCODE ,SourceTypeCODE ,SourceID ,CreationDate ,PostDate FROM system.tEmails WHERE EmailID = @EmailID OR @EmailID = '' ORDER BY EmailID; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Emails' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to get one or all Emails.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@EmailID'; SET @value = N'EmailID - optional to get only one Email, use empty string to get all Emails.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_Icons; GO /* Procedure to get a single icon or all Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall for all icons EXEC system.spGET_Icons ''; Testcall for one icon EXEC system.spGET_Icons 'hase.svg'; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Icons',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Icons','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_Icons @IconName NVARCHAR(255) = N'' AS BEGIN SET NOCOUNT ON DECLARE @UserName NVARCHAR(255) = N'anonym' DECLARE @TransactionUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@UserName, N'NULL') + N''', '+ ISNULL(@IconName, N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @UserName IS NULL SET @UserName = N''; IF @IconName IS NULL SET @IconName = N''; BEGIN TRY -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); -- STEP 1.1 - Determine transaction user SELECT @TransactionUsername = 'anonym' IF @IconName <> '' BEGIN IF NOT EXISTS ( SELECT TOP (1) 1 FROM system.tIcons WHERE IconName = @IconName ) BEGIN SET @ResultCode = 404; THROW 60000, N'File not found', 100; END; END; SELECT IconName ,IconName + ISNULL(N'.' + FileExtension, N'') AS IconNameWithExtension ,FileExtension ,FileBody FROM system.tIcons WHERE IconName= @IconName OR @IconName = '' SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state = 100 BEGIN PRINT @Comment; END ELSE IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactionUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Icons' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to get a single icon or all'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@IconName'; SET @value = N'IconName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_Module; GO /* Procedure to GET module information Saxess Software GmbH Last modified: 03/2024 for OCT 5.10 Testcall DECLARE @RC INT; EXEC @RC = system.spGET_Module @Username = 'SQL', @ModuleID = 'FIN' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Module',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Module','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_Module @Username NVARCHAR(255), @ModuleID NVARCHAR(255) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 2, @Username, @ModuleID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Input parameter handling SET @ModuleID = COALESCE(@ModuleID, N''); -- Return Module information SELECT ModuleID , ModuleType , Description , Version , AdminRequired , DescriptionURL FROM system.tModules WHERE ModuleID = @ModuleID; SET @AffectedRows = @@ROWCOUNT; IF @AffectedRows = 0 RAISERROR ('Module doesn''t exist', 11, 1); SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Module' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET the module tree'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ModuleID'; SET @value = N'ID of the requested module.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_ModuleTree; GO /* Procedure to GET the module tree Saxess Software GmbH Testcall DECLARE @RC INT; EXEC @RC = system.spGET_ModuleTree @Username = 'SQL', @SearchString = NULL SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_ModuleTree',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_ModuleTree','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_ModuleTree @Username NVARCHAR(255), @SearchString NVARCHAR(255) = NULL AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 2, @Username, @SearchString; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Return Module Tree WITH CurrentContentVersions AS ( SELECT ContentKey , MAX(Version) AS CurrentVersion FROM system.tContentVersions GROUP BY ContentKey ) SELECT m.ModuleID AS ModuleID , m.ModuleType AS ModuleType , m.Description AS ModuleDescription , c.ContentID AS ContentID , c.ContentType AS ContentType , c.Description AS ContentDescription , c.OrderIndex AS OrderIndex , COALESCE(cv.IsInstalled, 0) AS IsInstalled , IIF(cvo.IsInstalled IS NULL, 0, 1) AS OldVersionIsInstalled , c.TechnicalType AS TechnicalType , ccv.CurrentVersion AS CurrentVersion FROM system.tModules AS m LEFT JOIN system.tContents AS c ON c.ModuleKey = m.ModuleKey LEFT JOIN CurrentContentVersions AS ccv ON ccv.ContentKey = c.ContentKey LEFT JOIN system.tContentVersions AS cv ON cv.ContentKey = c.ContentKey AND cv.Version = ccv.CurrentVersion LEFT JOIN system.tContentVersions AS cvo ON cvo.ContentKey = c.ContentKey AND cvo.IsInstalled = 1 AND cvo.ContentVersionKey <> cv.ContentVersionKey WHERE @SearchString IS NULL OR cv.ContentSQL LIKE '%' + @SearchString + '%' ORDER BY ModuleID , OrderIndex; SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ModuleTree' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET the module tree'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SearchString'; SET @value = N'Only return content elements which include this string in their SQL definition.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_ObjectTree; GO /* Procedure to GET the ObjectTree, which lists all database objects with its metadata attribute The Procedure can't list objects on which the service don't has access rights. To see all object, execute this Stored procedure in SSMS as DBOwner. It looked promising to add in this procedure the clause "WITH EXECUTE AS OWNER" - this brings up in on-prem systems all objects. But it was no longer possible to excecute this stored procedure on Azure, even the owner is in both cases dbo. Maybe as in Azure the login behind dbo is Active Directory Account (which appears not as local login), wheras on-prem the dbo maps to a local login. https://docs.microsoft.com/de-de/sql/t-sql/statements/execute-as-clause-transact-sql?view=sql-server-ver15 Executing the Procedure with "EXECUTE AS OWNER 'dbo'" also don't worked. The Procedure therefore works on Azure only, if the Owner of the Database is an SQL-Login Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spGET_ObjectTree 'SQL' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_ObjectTree',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_ObjectTree','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_ObjectTree ( @Username NVARCHAR(255) ) -- must be always execute as owner to show all objects - but this don't work on Azure, if the Owner is an AAD User - OPEN PROBLEM -> alternative ALTER AUTHORIZATION to dbo ? WITH EXECUTE AS 'dbo' AS BEGIN SET NOCOUNT ON; -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' --set Strings inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position + ISNULL(@Username ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N''; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; -- START TRANSACTION *********************************************************************************** BEGIN TRY BEGIN TRANSACTION spGET_ObjectTree; -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Data Transaction ************************************************************************* SELECT CASE WHEN tree.Owner = '' THEN 'RGB(253,138,138)' --red WHEN tree.SX_ShipmentFlag = '1' THEN 'RGB(160,215,119)' --green WHEN tree.SX_ShipmentFlag IN ('2','3','4') THEN 'RGB(243,240,130)' --yellow WHEN tree.SX_ShipmentFlag IN ('10','11','12') THEN 'RGB(130,177,243)' --blue ELSE 'RGB(242,210,168)' --ocker END AS ColorRGB ,tree.[Owner] ,tree.Module ,tree.ObjectType ,tree.ShipmentStatus ,tree.ObjectName ,tree.[Description] FROM ( SELECT CASE WHEN sxo.SX_Owner <> '' THEN sxo.SX_Owner WHEN sxo.SX_Owner = '' THEN 'unknown Owner' WHEN sxo.SX_Owner IS NULL THEN '' ELSE '' END AS [Owner] ,CASE WHEN sxm.SX_Module IS NULL THEN '' WHEN sxm.SX_Module = '' THEN 'unknown Modul' ELSE sxm.SX_Module END AS Module ,CASE O.type WHEN 'P' THEN 'Stored Procedures' WHEN 'U' THEN 'Tables' WHEN 'V' THEN 'Views' WHEN 'IF' THEN 'Functions (Table Value Inline)' WHEN 'FN' THEN 'Functions (Scalar)' WHEN 'TF' THEN 'Functions (Table Value)' WHEN 'TR' THEN 'Trigger' WHEN 'SO' THEN 'Sequence' ELSE CONCAT('Unknown Objecttype ',O.type) END AS ObjectType ,sxs.SX_ShipmentFlag AS SX_ShipmentFlag ,CASE WHEN sxs.SX_ShipmentFlag = '0' THEN '0. Demo object - out of shipment process' WHEN sxs.SX_ShipmentFlag = '1' THEN '1. Standard - without modification' WHEN sxs.SX_ShipmentFlag = '2' THEN '2. Standard - modified from saxess for customer' WHEN sxs.SX_ShipmentFlag = '3' THEN '3. Standard - modified from partner for customer' WHEN sxs.SX_ShipmentFlag = '4' THEN '4. Standard - modified from customer for himself' WHEN sxs.SX_ShipmentFlag = '10' THEN '10. Created from Saxess for Customer' WHEN sxs.SX_ShipmentFlag = '11' THEN '11. Created from Partner for Customer' WHEN sxs.SX_ShipmentFlag = '12' THEN '12. Created from Customer for Customer' WHEN sxs.SX_ShipmentFlag IS NULL THEN '' ELSE 'unknown Shipment Status' END AS ShipmentStatus ,S.name +'.' + O.name AS ObjectName ,CASE WHEN EP.value = '' THEN 'Description not defined' WHEN EP.value IS NULL THEN '' WHEN EP.value <> '' THEN EP.value ELSE '' END AS Description FROM sys.all_objects O LEFT JOIN sys.extended_properties EP ON EP.major_id = O.object_id LEFT JOIN sys.schemas S ON O.schema_id = S.schema_id -- use as separte column LEFT JOIN ( SELECT major_id ,Value AS SX_Owner FROM sys.extended_properties WHERE Name = 'SX_Owner' ) sxo ON sxo.major_id = EP.major_id -- use as separte column LEFT JOIN ( SELECT major_id ,Value AS SX_ShipmentFlag FROM sys.extended_properties WHERE Name = 'SX_ShipmentFlag' ) sxs ON sxs.major_id = EP.major_id LEFT JOIN ( SELECT major_id ,Value AS SX_Module FROM sys.extended_properties WHERE Name = 'SX_Module' ) sxm ON sxm.major_id = EP.major_id WHERE O.is_ms_shipped = 0 AND ISNULL(EP.minor_id,0) = 0 -- columns of tables and views, parameters filtered out AND O.type NOT IN ('PK' -- primary keys ,'D' -- default constraints ,'UQ' -- unique constrains ,'F' -- foreign key ) AND (EP.name = 'MS_Description' OR EP.name IS NULL) --AND ISNULL(EP.name,'') NOT IN ('SX_Owner','SX_ShipmentFlag','SX_Module') -- separat joined --AND ISNULL(EP.name,'') NOT IN ('SX_UserHint','Is') -- not relevant ) tree ORDER BY Owner, Module, ObjectType, ObjectName -- ************************************************************************* SET @ResultCode = 200; COMMIT TRANSACTION spGET_ObjectTree; END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spGET_ObjectTree; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_ObjectTree' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- 10 = shiped from saxess as customer specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to create an object tree as list of all database objects with its metadata. The owner and shipment status of the objects will control update processes etc.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username for whom the action is down.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_PipelineLog; GO /* Procedure to get all pipeline and step logs without details Saxess Software GmbH Last modified: 08/2023 for OCT 5.10 Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spGET_PipelineLog @Username = 'SQL'; PRINT @RC; SELECT * FROM system.tPipelineLog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_PipelineLog',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_PipelineLog','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_PipelineLog @Username NVARCHAR(255) , @DateFrom DATETIME = NULL AS BEGIN DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) -- SET PARAMETER LOG STRING -inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(CAST(@DateFrom AS NVARCHAR(50)) ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime(); -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @DateFrom IS NULL SET @DateFrom = DATEADD(m, -1, GETUTCDATE()); BEGIN TRY -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user doesn''t exist', 16, 10); END; -- output resultset SELECT PipelineID , PipelineName , StepID , StepName , ExecutionID , ExecutionServerName , StatusCODE , StartedBy , StartTime , EndTime FROM system.tPipelineLog WHERE StartTime > @DateFrom; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; END TRY BEGIN CATCH SET @Comment = ERROR_MESSAGE(); SET @ResultCode = 500; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_PipelineLog' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to get all pipeline and step logs without details.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@DateFrom'; SET @value = N'Used to only return log entries starting from this date.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_Pipelines; GO /* Procedure to GET a Pipeline Saxess Software GmbH Testcall DECLARE @RC INT; EXEC @RC = system.spGET_Pipelines @Username = N'SQL' , @PipelineID = N'P1' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Pipelines', NULL, NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Pipelines', 'PARAMETER', NULL) */ CREATE PROCEDURE system.spGET_Pipelines @Username NVARCHAR(255) , @PipelineID NVARCHAR(50) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 2, @Username, @PipelineID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Input parameter handling SET @PipelineID = COALESCE(system.fProtectID(@PipelineID), N''); SELECT PipelineID , PipelineName , PipelineDescription , PipelineDescriptionIconColor , OrderOfDisplay , EmailNotificationCODE , ExcludedExecutionTimesJSON FROM system.tPipelines WHERE PipelineID = @PipelineID OR @PipelineID = '' ORDER BY OrderOfDisplay; SET @AffectedRows = @@ROWCOUNT; IF @AffectedRows = 0 AND @PipelineID <> N'' EXEC system.spSEND_Message N'ERROR', N'PipelineID doesn''t exist' SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 EXEC system.spSEND_Message N'ERROR', @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Pipelines' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to get one or all Pipelines.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@PipelineID'; SET @value = N'PipelineID - optional get only one Pipeline, use empty string to get all Pipelines.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_PipelineSchedules; GO /* Procedure to read one or all schedules of a pipeline or of the whole database Saxess Software GmbH Last modified: 09/2024 for OCT 5.10 Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spGET_PipelineSchedules @Username = 'SQL' ,@PipelineID = '' ,@ScheduleID = '' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_PipelineSchedules',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_PipelineSchedules','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_PipelineSchedules ( @Username NVARCHAR(255) ,@PipelineID NVARCHAR(50) = '' ,@ScheduleID NVARCHAR(50) = '' ) AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) -- SET PARAMETER LOG STRING -inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@PipelineID ,N'NULL') + N''',''' + ISNULL(@ScheduleID ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime() ,@PipelineKey BIGINT; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @PipelineID IS NULL SET @PipelineID = N''; IF @ScheduleID IS NULL SET @ScheduleID = N''; -- START TRY *********************************************************************************** BEGIN TRY -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- check existence of IDs IF @PipelineID <> '' AND NOT EXISTS ( SELECT PipelineID FROM system.tPipelines WHERE PipelineID = @PipelineID ) BEGIN SET @ResultCode = 404; RAISERROR('Source Pipeline don`t exits', 16, 10); END SELECT @PipelineKey = PipelineKey FROM system.tPipelines WHERE PipelineID = @PipelineID; IF @ScheduleID <> '' AND NOT EXISTS ( SELECT ScheduleID FROM system.tPipelineSchedules WHERE PipelineKey = @PipelineKey AND ScheduleID = @ScheduleID ) BEGIN SET @ResultCode = 404; RAISERROR('Schedule don`t exits', 16, 10); END -- Data Transaction SELECT P.PipelineID ,PS.ScheduleID ,PS.ScheduleName ,PS.ScheduleDescription ,PS.ScheduleDetailsJSON ,PS.Active FROM system.tPipelineSchedules PS LEFT JOIN system.tPipelines P ON PS.PipelineKey = P.PipelineKey WHERE (PS.PipelineKey = @PipelineKey OR @PipelineID = '') AND (PS.ScheduleID = @ScheduleID OR @ScheduleID = '') ORDER BY P.PipelineID ,PS.ScheduleID; SET @ResultCode = 200; END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_PipelineSchedules' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure returns one or all schedules of a given pipeline.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@PipelineID'; SET @value = N'ID of the Pipeline, which schedules are to read - use '' to read all schedules of this database.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ScheduleID'; SET @value = N'ID of the schedule to read - use '' to read all schedules of this pipeline.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_PipelineStepLog; GO /* Prozedur to get all detailed log entries for a step Saxess Software GmbH Last modified: 02/2024 for OCT 5.10 Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spGET_PipelineStepLog @Username = 'SQL' ,@PipelineID = 'P1' ,@ExecutionID = 'EID5'; PRINT @RC; SELECT * FROM system.tPipelineStepLog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_PipelineStepLog',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_PipelineStepLog','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_PipelineStepLog @Username NVARCHAR(255) , @PipelineID NVARCHAR(50) , @ExecutionID NVARCHAR(50) , @StepID NVARCHAR(50) = NULL AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) -- SET PARAMETER LOG STRING -inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@PipelineID ,N'NULL') + N''',''' + ISNULL(@ExecutionID ,N'NULL') + N''',''' + ISNULL(@StepID ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime(); -- NULL Protection for all mandatory Input parameters IF @Username IS NULL SET @Username = N''; IF @PipelineID IS NULL SET @PipelineID = N''; IF @ExecutionID IS NULL SET @ExecutionID = N''; BEGIN TRY -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user doesn''t exist', 16, 10); END; -- output resultset SELECT psl.StepID , pl.StepName , psl.LogTime , psl.LogMessage , psl.LogLevelCODE FROM system.tPipelineStepLog AS psl JOIN system.tPipelineLog AS pl ON pl.PipelineID = psl.PipelineID AND pl.StepID = psl.StepID AND pl.ExecutionID = psl.ExecutionID WHERE psl.PipelineID = @PipelineID AND psl.ExecutionID = @ExecutionID AND psl.StepID = COALESCE(@StepID, psl.StepID) ORDER BY LogTime ASC; SET @ResultCode = 200; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); SET @ResultCode = 500; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_PipelineStepLog' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Prozedur ot GET all or filterd StepLog Data'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@PipelineID'; SET @value = N'ID of the Pipeline to get the logs from.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ExecutionID'; SET @value = N'Execution ID of the Pipeline to get the logs from.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@StepID'; SET @value = N'Step ID of the Pipeline to get the logs for a specific step. Optional parameter.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_PipelineSteps; GO /* Procedure to read one or all Pipeline Steps Saxess Software GmbH Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spGET_PipelineSteps @Username = 'SQL' ,@PipelineID = 'P1' ,@StepID = '' PRINT @RC SELECT * FROM system.tPipelineSteps tPS LEFT JOIN system.tPipelines tP ON tPS.PipelineKey = tP.PipelineKey; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_PipelineSteps',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_PipelineSteps','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_PipelineSteps ( @Username NVARCHAR(255) ,@PipelineID NVARCHAR(50) ,@StepID NVARCHAR(50) = '' ) AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) -- SET PARAMETER LOG STRING -inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@PipelineID ,N'NULL') + N''',''' + ISNULL(@StepID ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime() ,@PipelineKey BIGINT = 0; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @PipelineID IS NULL SET @PipelineID = N''; -- START TRY *********************************************************************************** BEGIN TRY -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- check existence of IDs IF NOT EXISTS ( SELECT PipelineID FROM system.tPipelines WHERE PipelineID = @PipelineID ) BEGIN SET @ResultCode = 404; RAISERROR('Source Pipeline don`t exits', 16, 10); END; SELECT @PipelineKey = PipelineKey FROM system.tPipelines WHERE PipelineID = @PipelineID; IF @StepID <> '' AND NOT EXISTS ( SELECT StepID FROM system.tPipelineSteps WHERE PipelineKey = @PipelineKey AND StepID = @StepID ) BEGIN SET @ResultCode = 404; RAISERROR('Step don`t exits', 16, 10); END; -- Data Transaction SELECT @PipelineID AS PipelineID ,StepID ,StepName ,StepDescription ,OrderOfExecution ,StepDataSourceID ,StepCompanyIDsJSON ,StepDetailsJSON ,TimeExecutionLimit ,Active ,RunConditionCODE ,RestartOnError FROM system.tPipelineSteps WHERE PipelineKey = @PipelineKey AND (StepID = @StepID OR @StepID = '') ORDER BY OrderOfExecution; SET @ResultCode = 200; END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_PipelineSteps' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure returns one or all steps of a given pipeline.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@PipelineID'; SET @value = N'ID of the pipeline, whichs steps are requested.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@StepID'; SET @value = N'ID of the Step to get, use '''' to get all Steps.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_User; GO /* GET Operation for a User Saxess Software GmbH Last modified: 09/2023 for OCT 5.9 Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spGET_User @Username = 'SQL' ,@RequestedUserName = 'W10\admin' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_User',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_User','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_User @Username NVARCHAR(255), @RequestedUserName NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @UserKey INT = 0; DECLARE @Right NVARCHAR(10) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@RequestedUserName, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- NULL Protection IF @Username IS NULL SET @Username = N''; IF @RequestedUserName IS NULL SET @RequestedUserName = N''; BEGIN TRY BEGIN TRANSACTION spGET_User; -- Protect input parameters SET @Username = system.fProtectString (@Username); SET @RequestedUserName = system.fProtectString (@RequestedUserName); IF @RequestedUserName = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameters', 16, 10); END; -- Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Determine keys SELECT @UserKey = UserKey FROM system.trUser WHERE UserName = @RequestedUserName; -- Fallback to public user if it exists IF @UserKey = 0 BEGIN SELECT @UserKey = UserKey FROM system.trUser WHERE UserName = 'public' AND Status = 'Active' END -- User doesn't exist IF @UserKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- Check rights IF @TransactUsername = @RequestedUserName BEGIN --users must see themselves SET @Right = N'Read'; SET @ResultCode = 200; END ELSE BEGIN EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- STEP 3. Show public values SELECT UserKey ,UserName ,PersonSurname ,PersonFirstName ,Email ,LDAPIP ,[Status] ,@Right AS 'Right' ,DataFlowRightsCODE ,IsAdministratorFlag FROM system.trUser WHERE UserKey = @UserKey; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; COMMIT TRANSACTION spGET_User; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spGET_User; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_User' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N''; -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for a User'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@RequestedUserName'; SET @value = N'RequestedUserName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_UserRight; GO /* GET Operation for User rights in DataEntry Gerd Tautenhahn for Saxess Software GmbH Last modified: 01/2023 for OCT 5.9 Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spGET_UserRight @Username = 'SQL', @RequestedUserName = 'W10\admin'; PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_UserRight',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_UserRight','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_UserRight @Username NVARCHAR(255), @RequestedUserName NVARCHAR(255) = NULL AS BEGIN SET NOCOUNT ON; DECLARE @UserKey INT = 0; DECLARE @Right NVARCHAR(10) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''',''' + ISNULL(@RequestedUserName, N'NULL') + N''''; DECLARE @EffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @Comment NVARCHAR(2000) = N''; -- NULL Protection IF @Username IS NULL SET @Username = N''; IF @RequestedUserName IS NULL SET @RequestedUserName = N''; BEGIN TRY BEGIN TRANSACTION GET_UserRight; -- Protect input parameters SET @Username = system.fProtectString (@Username); SET @RequestedUserName = system.fProtectString (@RequestedUserName); -- Check on empty name or SystemUser IF @RequestedUserName = N'' BEGIN SET @ResultCode = 404; RAISERROR('Empty input parameter ', 16, 10); END; -- Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Determine keys SELECT @UserKey = UserKey FROM system.trUser WHERE UserName = @RequestedUserName; IF @UserKey = 0 BEGIN SET @ResultCode = 404; RAISERROR('Keys don`t exists', 16, 10); END; -- Check rights IF @TransactUsername = @RequestedUserName BEGIN --user Must see themselves SET @Right = N'Read'; SET @ResultCode = 200; END ELSE BEGIN EXEC @ResultCode = system.spGET_ClusterWriteRightTransactionUser @TransactUsername; IF @ResultCode = 200 BEGIN SET @Right = N'Write'; END ELSE BEGIN RAISERROR('Invalid rights', 16, 10); END; END; -- Helper Table with Full Stucture - to Output Deny Rights too ******************************************************************* IF OBJECT_ID('tempdb..#FullStructure') IS NOT NULL DROP TABLE #FullStructure; CREATE TABLE #FullStructure ( RowKey BIGINT IDENTITY (1,1) NOT NULL ,[Level] NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,FactoryKey BIGINT NOT NULL ,ProductLineKey BIGINT NOT NULL ,FactoryID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ); -- C Level INSERT INTO #FullStructure SELECT 'Cluster' AS 'Level' ,0 AS FactoryKey ,0 AS ProductLineKey ,'' AS FactoryID ,'' AS ProductLineID; -- F Level INSERT INTO #FullStructure SELECT 'Factory' AS 'Level' ,dF.FactoryKey ,0 AS ProductLineKey ,dF.FactoryID ,'' AS ProductLineID FROM planning.tdFactories dF; -- PL Level INSERT INTO #FullStructure SELECT 'ProductLine' AS 'Level' ,dF.FactoryKey ,dPL.ProductLineKey ,dF.FactoryID ,dPL.ProductLineID FROM planning.tdProductLines dPL LEFT JOIN planning.tdFactories dF ON dPL.FactoryKey = dF.FactoryKey; -- STEP 3. Show public values IF @Right = N'Read' --User is allowed to see of himself only positive rights BEGIN SELECT vUR.UserName AS UserName ,vUR.FactoryID AS FactoryID ,COALESCE(dF.NameShort,'') AS FactoryName ,vUR.ProductLineID AS ProductLineID ,COALESCE(dPL.NameShort,'') AS ProductLineName ,vUR.[Right] AS [Right] ,vUR.ReadCommentMandatory AS ReadCommentMandatory ,vUR.WriteCommentMandatory AS WriteCommentMandatory FROM system.trUserRights vUR -- for FactoryName LEFT JOIN planning.tdFactories dF ON vUR.FactoryID = dF.FactoryID -- for ProductLineName LEFT JOIN planning.tdProductLines dPL ON vUR.FactoryID = dPL.FactoryID AND vUR.ProductLineID = dPL.ProductLineID WHERE vUR.UserKey = @UserKey ORDER BY ISNULL(TRY_CAST(vUR.FactoryID AS INT), 999999999) ,vUR.FactoryID ,ISNULL(TRY_CAST(vUR.ProductLineID AS INT), 999999999) ,vUR.ProductLineID; END ELSE IF @Right = N'Write' -- Cluster admin can see also negative rights (really don`t exist) BEGIN SELECT @RequestedUsername AS UserName ,fs.FactoryID AS FactoryID ,COALESCE(dF.NameShort,'') AS FactoryName ,fs.ProductLineID AS ProductLineID ,COALESCE(dPL.NameShort,'') AS ProductLineName ,COALESCE(vUR.[Right],'Deny') AS 'Right' ,COALESCE(vUR.ReadCommentMandatory,0) AS ReadCommentMandatory ,COALESCE(vUR.WriteCommentMandatory,0) AS WriteCommentMandatory FROM #FullStructure fs -- for FactoryName LEFT JOIN planning.tdFactories dF ON fs.FactoryID = dF.FactoryID -- for ProductLineName LEFT JOIN planning.tdProductLines dPL ON fs.FactoryID = dPL.FactoryID AND fs.ProductLineID = dPL.ProductLineID -- positive Rechte LEFT JOIN ( SELECT vUR.FactoryID ,vUR.ProductLineID ,vUR.[Right] ,vUR.ReadCommentMandatory ,vUR.WriteCommentMandatory FROM system.trUserRights vUR WHERE vUR.UserKey = @UserKey ) vUR ON fs.FactoryID = vUR.FactoryID AND fs.ProductLineID = vUR.ProductLineID ORDER BY ISNULL(TRY_CAST(fs.FactoryID AS INT), 999999999) ,fs.FactoryID ,ISNULL(TRY_CAST(fs.ProductLineID AS INT), 999999999) ,fs.ProductLineID; END; SET @EffectedRows = @@ROWCOUNT; COMMIT TRANSACTION GET_UserRight; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION GET_UserRight; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_UserRight' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N''; -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation for User rights'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@RequestedUserName'; SET @value = N'RequestedUserName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spGET_UserRightInformation; GO /* Procedure to get right informations for an User Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spGET_UserRightInformation @Username = 'SQL' ,@UsernameRequestedUser = 'W10\admin' ,@RightAreaCODE = '' ,@FactoryID = '' ,@ProductLineID = '' ,@PipelineID = '' ,@FullStructureBOOL = 0 PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_UserRightInformation',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_UserRightInformation','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_UserRightInformation ( @Username NVARCHAR(255) ,@UsernameRequestedUser NVARCHAR(255) ,@RightAreaCODE NVARCHAR(50) = '' --DATAENTRY, DATAFLOWS ,@FactoryID NVARCHAR(50) = '' ,@ProductLineID NVARCHAR(50) = '' ,@PipelineID NVARCHAR(50) = '' ,@FullStructureBOOL BIT = 0 ) AS BEGIN -- SET NOCOUNT ON; -- activate this, if you read the resultset with an external source -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) -- SET PARAMETER LOG STRING -inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@UsernameRequestedUser ,N'NULL') + N''',''' + ISNULL(@RightAreaCODE ,N'NULL') + N''',''' + ISNULL(@FactoryID ,N'NULL') + N''',''' + ISNULL(@ProductLineID ,N'NULL') + N''',''' + ISNULL(@PipelineID ,N'NULL') + N''',' + ISNULL(CAST(@FullStructureBOOL AS NCHAR(1)) ,N'NULL') + N'' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime(); -- Procedure specific declarations --DECLARE @FactoryKey INT = 0 -- ,@ProductLineKey INT = 0 -- ,@ProductKey INT = 0; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @UsernameRequestedUser IS NULL SET @UsernameRequestedUser = N''; IF @RightAreaCODE IS NULL SET @RightAreaCODE = N''; IF @FactoryID IS NULL SET @FactoryID = N''; IF @ProductLineID IS NULL SET @ProductLineID = N''; IF @PipelineID IS NULL SET @PipelineID = N''; IF @FullStructureBOOL IS NULL SET @FullStructureBOOL = 0; -- START TRY *********************************************************************************** BEGIN TRY -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- if RequestedUser doesn't exist we can use fallback to public user IF NOT EXISTS(SELECT 1 FROM system.trUser WHERE UserName = @UsernameRequestedUser AND Status = 'Active') AND EXISTS(SELECT 1 FROM system.trUser WHERE UserName = 'public' AND Status = 'Active') BEGIN SET @UsernameRequestedUser = 'public' END DROP TABLE IF EXISTS #tRightInformation; CREATE TABLE #tRightInformation ( RowKey BIGINT IDENTITY (1,1) ,RightAreaCode NVARCHAR(50) COLLATE DATABASE_DEFAULT NOT NULL ,FactoryID NVARCHAR(50) COLLATE DATABASE_DEFAULT NOT NULL ,ProductLineID NVARCHAR(50) COLLATE DATABASE_DEFAULT NOT NULL ,PipelineID NVARCHAR(50) COLLATE DATABASE_DEFAULT NOT NULL ,UserRight NVARCHAR(50) COLLATE DATABASE_DEFAULT NOT NULL ,ReadCommentMandatory BIT NOT NULL ,WriteCommentMandatory BIT NOT NULL ,RightDescription NVARCHAR(2000) COLLATE DATABASE_DEFAULT NOT NULL ,PRIMARY KEY CLUSTERED (RowKey) ); -- Rights for DataEntry IF @RightAreaCode = 'DATAENTRY' OR @RightAreaCode = '' BEGIN -- For full structure requested IF @FullStructureBOOL = 1 BEGIN INSERT INTO #tRightInformation SELECT 'DATAENTRY' AS RightAreaCode ,FactoryID ,ProductLineID ,'' AS PipelineID ,[Right] AS UserRight ,ReadCommentMandatory ,WriteCommentMandatory ,'' AS RightDescription FROM system.trUserRights WHERE UserName = @UsernameRequestedUser AND (FactoryID = @FactoryID OR @FactoryID='' ) AND (ProductLineID = @ProductLineID OR @ProductLineID = ''); END -- For single line IF @FullStructureBOOL = 0 BEGIN INSERT INTO #tRightInformation SELECT 'DATAENTRY' AS RightAreaCode ,FactoryID ,ProductLineID ,'' AS PipelineID ,[Right] AS UserRight ,ReadCommentMandatory ,WriteCommentMandatory ,'' AS RightDescription FROM system.trUserRights WHERE UserName = @UsernameRequestedUser AND (FactoryID = @FactoryID) AND (ProductLineID = @ProductLineID); END END -- Rights for DataFlows IF @RightAreaCode = 'DATAFLOWS' OR @RightAreaCode = '' BEGIN -- One Query for full structure and single object INSERT INTO #tRightInformation SELECT 'DATAFLOWS' AS RightAreaCode ,'' AS FactoryID ,'' AS ProductLineID ,PipelineID AS PipelineID ,'EXECUTE' AS UserRight ,0 AS ReadCommentMandatory ,0 AS WriteCommentMandatory ,'temporary' AS RightDescription FROM system.tPipelines WHERE -- UserName = @UsernameRequestedUser -- AND (PipelineID = @PipelineID OR @PipelineID='') END -- OUTPUT SELECT * FROM #tRightInformation; SET @ResultCode = 200; END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_UserRightInformation' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to get right informations for an User - returns the Right as String - Read, Write, EXECUTE,DESIGN'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@Username'; SET @value = N'Standardparameter, executing User - use "SQL"'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@UsernameRequestedUser'; SET @value = N'Name of the user for which rights are requested'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@RightAreaCODE'; SET @value = N'CODE to specify the Rightarea, use DATAFLOWS or DATAENTRY, keep empty to get all'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FactoryID'; SET @value = N'FactoryID in case of requsting Rights for DATAENTRY, keep empty to get all'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ProductLineID'; SET @value = N'ProductLineID in case of requsting Rights for DATAENTRY, keep empty to get all'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name SET @level2name = N'@PipelineID'; SET @value = N'PipelineID in case of requsting Rights for DATAFLOWS, keep empty to get all'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name SET @level2name = N'@FullStructureBOOL'; SET @value = N'1 or 0 to flag if the full structure under the node (downwards) is included into the output'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name GO DROP PROCEDURE IF EXISTS system.spGET_Users; GO /* GET Operation for all Users Gerd Tautenhahn for Saxess Software GmbH Last modified: 02/2023 for OCT 5.9 Test call DECLARE @RC NVARCHAR(255) EXEC @RC = system.spGET_Users 'sxs' PRINT @RC SELECT TOP 3 * FROM system.tAPILog; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Users',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spGET_Users','PARAMETER',NULL) */ CREATE PROCEDURE system.spGET_Users @Username NVARCHAR(255) AS BEGIN SET NOCOUNT ON; DECLARE @TransactUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@Message NVARCHAR(2000) = N''; -- NULL Protection IF @Username IS NULL SET @Username = N''; BEGIN TRY BEGIN TRANSACTION spGET_Users; -- Protect input parameters SET @Username = system.fProtectString (@Username); -- Determine transaction user SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Only OCT Administrators can manage users IF NOT EXISTS (SELECT UserKey FROM system.trUser WHERE UserName = @TransactUsername AND IsAdministratorFlag = 1 AND Status = 'Active' ) BEGIN SET @Message = CONCAT('Invalid rights, User "',@Transactusername,'" is not an OCT Administrator.'); RAISERROR(@Message, 16, 10); END; -- Output SELECT u.UserKey ,u.UserName ,u.PersonSurname ,u.PersonFirstName ,u.Email ,u.LDAPIP ,u.[Status] ,N'Write' AS [Right] ,u.DataFlowRightsCODE ,u.IsAdministratorFlag FROM system.trUser u ORDER BY UserName; SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; COMMIT TRANSACTION spGET_Users; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spGET_Users; IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spGET_Users' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N''; -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'GET Operation to read all Users, only allowed for Clusteradmin.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spMOVE_Content; GO /* Procedure to MOVE a content Saxess Software GmbH Last modified: 10/2023 for OCT 5.10 Testcall DECLARE @RC INT; EXEC @RC = system.spMOVE_Content @Username = 'SQL', @SourceModuleID = 'FIN', @TargetModuleID = 'FINRENAMED' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload','PARAMETER',NULL) */ CREATE PROCEDURE system.spMOVE_Content @Username NVARCHAR(255), @ModuleID NVARCHAR(255), @SourceContentID NVARCHAR(255), @TargetContentID NVARCHAR(255) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 4, @Username, @ModuleID, @SourceContentID, @TargetContentID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Input parameter handling SET @ModuleID = COALESCE(system.fProtectID(@ModuleID), N''); -- Error handling IF @ModuleID = N'' RAISERROR ('No @ModuleID provided', 11, 1); IF @SourceContentID = N'' RAISERROR ('No SourceContentID provided', 11, 1); IF @TargetContentID = N'' RAISERROR ('No TargetContentID provided', 11, 1); IF EXISTS(SELECT 1 FROM system.tContents WHERE ModuleID = @ModuleID AND ContentID = @TargetContentID) RAISERROR ('ContentID already exists', 11, 1); -- Rename content UPDATE system.tContents SET ContentID = @TargetContentID WHERE ModuleID = @ModuleID AND ContentID = @SourceContentID; SET @AffectedRows = @@ROWCOUNT; IF @AffectedRows = 0 RAISERROR ('SourceContentID doesn''t exist', 11, 1); -- Rename ContentID in content versions UPDATE system.tContentVersions SET ContentID = @TargetContentID WHERE ModuleID = @ModuleID AND ContentID = @SourceContentID; SET @AffectedRows += @@ROWCOUNT; SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spMOVE_Content' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET the module tree'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ModuleID'; SET @value = N'ID of the module of the content that should be renamed.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceContentID'; SET @value = N'ID of the content that should be renamed.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetContentID'; SET @value = N'New ID of the renamed content.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spMOVE_Module; GO /* Procedure to MOVE a module Saxess Software GmbH Last modified: 10/2023 for OCT 5.10 Testcall DECLARE @RC INT; EXEC @RC = system.spMOVE_Module @Username = 'SQL', @SourceModuleID = 'FIN', @TargetModuleID = 'FINRENAMED' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload','PARAMETER',NULL) */ CREATE PROCEDURE system.spMOVE_Module @Username NVARCHAR(255), @SourceModuleID NVARCHAR(255), @TargetModuleID NVARCHAR(255) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 3, @Username, @SourceModuleID, @TargetModuleID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Input parameter handling SET @SourceModuleID = COALESCE(system.fProtectID(@SourceModuleID), N''); SET @TargetModuleID = COALESCE(system.fProtectID(@TargetModuleID), N''); -- Error handling IF @SourceModuleID = N'' RAISERROR ('No SourceModuleID provided', 11, 1); IF @TargetModuleID = N'' RAISERROR ('No TargetModuleID provided', 11, 1); IF EXISTS(SELECT 1 FROM system.tModules WHERE ModuleID = @TargetModuleID) RAISERROR ('ModuleID of TargetModuleID already exists', 11, 1); -- Rename module UPDATE system.tModules SET ModuleID = @TargetModuleID WHERE ModuleID = @SourceModuleID; SET @AffectedRows = @@ROWCOUNT; IF @AffectedRows = 0 RAISERROR ('SourceModuleID doesn''t exist', 11, 1); -- Rename ModuleID in contents UPDATE system.tContents SET ModuleID = @TargetModuleID WHERE ModuleID = @SourceModuleID; SET @AffectedRows += @@ROWCOUNT; -- Rename ModuleID in content versions UPDATE system.tContentVersions SET ModuleID = @TargetModuleID WHERE ModuleID = @SourceModuleID; SET @AffectedRows += @@ROWCOUNT; SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spMOVE_Module' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET the module tree'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceModuleID'; SET @value = N'ID of the module that should be renamed.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetModuleID'; SET @value = N'New ID of the copied module.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spMOVE_Pipeline; GO /* Procedure to MOVE a Pipeline Saxess Software GmbH Testcall DECLARE @RC INT; EXEC @RC = system.spMOVE_Pipeline @Username = N'SQL' , @SourcePipelineID = N'P1' , @TargetPipelineID = N'Pipeline1' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spMOVE_Pipeline', NULL, NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spMOVE_Pipeline', 'PARAMETER', NULL) */ CREATE PROCEDURE system.spMOVE_Pipeline @Username NVARCHAR(255) , @SourcePipelineID NVARCHAR(255) , @TargetPipelineID NVARCHAR(255) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 3, @Username, @SourcePipelineID, @TargetPipelineID; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Input parameter handling SET @SourcePipelineID = COALESCE(system.fProtectID(@SourcePipelineID), N''); SET @TargetPipelineID = COALESCE(system.fProtectID(@TargetPipelineID), N''); -- Error handling IF @SourcePipelineID = N'' EXEC system.spSEND_Message N'ERROR', N'No SourcePipelineID provided' IF @TargetPipelineID = N'' EXEC system.spSEND_Message N'ERROR', N'No TargetPipelineID provided' IF EXISTS( SELECT 1 FROM system.tPipelines WHERE PipelineID = @TargetPipelineID ) EXEC system.spSEND_Message N'ERROR', N'TargetPipelineID already exists' -- Rename Pipeline UPDATE system.tPipelines SET PipelineID = @TargetPipelineID WHERE PipelineID = @SourcePipelineID; SET @AffectedRows = @@ROWCOUNT; IF @AffectedRows = 0 EXEC system.spSEND_Message N'ERROR', N'SourcePipelineID doesn''t exist' SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 EXEC system.spSEND_Message N'ERROR', @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spMOVE_Pipeline' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.Pipelines, Custom) ,@SX_Pipeline NVARCHAR(255) = N'CORE' -- enter Pipeline name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Pipeline; EXEC sys.sp_addextendedproperty N'SX_Pipeline' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to MOVE a Pipeline'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourcePipelineID'; SET @value = N'PipelineID of the Pipeline that should be renamed.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TargetPipelineID'; SET @value = N'New PipelineID of the Pipeline.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spPOST_Action; GO /* Procedure to POST Action information Saxess Software GmbH Testcall DECLARE @RC INT; DECLARE @ReturnedActionID NVARCHAR(50); EXEC @RC = system.spPOST_Action @Username = 'SQL' , @ActionID = '*' , @OrderIndex = NULL , @Active = 1 , @Name = 'ActionTest' , @Description = 'Short description.' , @RequiresClusterWriteRights = 0 , @RequiresWriteRights = 1 , @Condition = '' , @ListCustomTitle = 'Eine Action' , @ListSQL = 'SELECT * FROM global.tCodes' , @ListLayout = '' , @ListExcelExport = 1 , @SetCellValueAsFilter = 0 , @ReloadListAfterChangingFilter = 0 , @ActionSQL = '' , @ApplyFromField = '' , @SaveBeforeAction = 'NO' , @SaveAfterAction = 0 , @ReloadTreeAfterAction = 0 , @ReloadDetailsAfterAction = 0 , @OutputID = @ReturnedActionID OUTPUT; SELECT @ReturnedActionID; SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_Action', NULL, NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_Action', 'PARAMETER', NULL) */ CREATE PROCEDURE system.spPOST_Action @Username NVARCHAR(255) , @ActionID NVARCHAR(255) = NULL , @OrderIndex INT = NULL , @Active BIT = NULL , @Name NVARCHAR(255) = NULL , @Description NVARCHAR(4000) = NULL , @RequiresClusterWriteRights BIT = NULL , @RequiresWriteRights BIT = NULL , @Condition NVARCHAR(MAX) = NULL , @ListCustomTitle NVARCHAR(255) = NULL , @ListSQL NVARCHAR(MAX) = NULL , @ListLayout NVARCHAR(MAX) = NULL , @ListExcelExport BIT = NULL , @SetCellValueAsFilter BIT = NULL , @ReloadListAfterChangingFilter BIT = NULL , @ActionSQL NVARCHAR(MAX) = NULL , @ApplyFromField NVARCHAR(255) = NULL , @SaveBeforeAction NVARCHAR(20) = NULL , @SaveAfterAction BIT = NULL , @ReloadTreeAfterAction BIT = NULL , @ReloadDetailsAfterAction BIT = NULL , @OutputID NVARCHAR(50) = NULL OUTPUT AS BEGIN BEGIN TRY -- Start transaction BEGIN TRANSACTION spPOST_Action -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 21, @Username, @ActionID, @OrderIndex, @Active, @Name, @Description, @RequiresClusterWriteRights, @RequiresWriteRights, @Condition, @ListCustomTitle, @ListSQL, @ListLayout, @ListExcelExport, @SetCellValueAsFilter, @ReloadListAfterChangingFilter, @ActionSQL, @ApplyFromField, @SaveBeforeAction, @SaveAfterAction, @ReloadTreeAfterAction, @ReloadDetailsAfterAction; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Input parameter handling SET @ActionID = COALESCE(system.fProtectID(@ActionID), N''); SET @Name = COALESCE(system.fProtectString(@Name), N''); SET @Description = COALESCE(system.fProtectString(@Description), N''); IF COALESCE(@ActionID, N'') = N'' EXEC system.spSEND_Message 'ERROR', 'No ActionID provided'; IF COALESCE(@Name, N'') = N'' EXEC system.spSEND_Message 'ERROR', 'No name provided'; -- Update existing data UPDATE system.tActions SET OrderIndex = @OrderIndex , Active = @Active , Name = @Name , Description = @Description , RequiresClusterWriteRights = @RequiresClusterWriteRights , RequiresWriteRights = @RequiresWriteRights , Condition = @Condition , ListCustomTitle = @ListCustomTitle , ListSQL = @ListSQL , ListLayout = @ListLayout , ListExcelExport = @ListExcelExport , SetCellValueAsFilter = @SetCellValueAsFilter , ReloadListAfterChangingFilter = @ReloadListAfterChangingFilter , ActionSQL = @ActionSQL , ApplyFromField = @ApplyFromField , SaveBeforeAction = @SaveBeforeAction , SaveAfterAction = @SaveAfterAction , ReloadTreeAfterAction = @ReloadTreeAfterAction , ReloadDetailsAfterAction = @ReloadDetailsAfterAction WHERE ActionID = @ActionID; SET @AffectedRows = @@ROWCOUNT -- Insert new data IF @AffectedRows = 0 BEGIN -- Get next free ActionID IF @ActionID IN (N'', N'*') BEGIN SET @ActionID = ( SELECT TOP 1 TRY_CAST(RIGHT(ActionID, LEN(ActionID) - 1) AS INT) + 1 FROM system.tActions WHERE ActionID LIKE N'A%' AND LEN(ActionID) > 1 AND TRY_CAST(RIGHT(ActionID, LEN(ActionID) - 1) AS INT) IS NOT NULL ORDER BY TRY_CAST(RIGHT(ActionID, LEN(ActionID) - 1) AS INT) DESC ); SET @ActionID = COALESCE(N'A' + @ActionID, N'A1'); END INSERT INTO system.tActions ( ActionID , OrderIndex , Active , Name , Description , RequiresClusterWriteRights , RequiresWriteRights , Condition , ListCustomTitle , ListSQL , ListLayout , ListExcelExport , SetCellValueAsFilter , ReloadListAfterChangingFilter , ActionSQL , ApplyFromField , SaveBeforeAction , SaveAfterAction , ReloadTreeAfterAction , ReloadDetailsAfterAction ) VALUES ( @ActionID , (SELECT COALESCE(MAX(OrderIndex), 0) + 1 FROM system.tActions) , COALESCE(@Active, 0) , COALESCE(@Name, N'Unknown') , COALESCE(@Description, N'') , COALESCE(@RequiresWriteRights, 0) , COALESCE(@RequiresWriteRights, 0) , COALESCE(@Condition, N'') , COALESCE(@ListCustomTitle, N'') , COALESCE(@ListSQL, N'') , COALESCE(@ListLayout, N'') , COALESCE(@ListExcelExport, 1) , COALESCE(@SetCellValueAsFilter, 0) , COALESCE(@ReloadListAfterChangingFilter, 0) , COALESCE(@ActionSQL, N'') , COALESCE(@ApplyFromField, N'') , COALESCE(@SaveBeforeAction, N'NO') , COALESCE(@SaveAfterAction, 0) , COALESCE(@ReloadTreeAfterAction, 0) , COALESCE(@ReloadDetailsAfterAction, 0) ); SET @AffectedRows = @@ROWCOUNT; END SET @OutputID = @ActionID; SET @ResultCode = 200 COMMIT TRANSACTION spPOST_Action END TRY BEGIN CATCH ROLLBACK TRANSACTION spPOST_Action SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 EXEC system.spSEND_Message 'ERROR', @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Action' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.Actions, Custom) ,@SX_Action NVARCHAR(255) = N'CORE' -- enter Action name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Action; EXEC sys.sp_addextendedproperty N'SX_Action' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to POST Action information'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ActionID'; SET @value = N'Unique ActionID for row identity.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@OrderIndex'; SET @value = N'Index which determines the order when displaying the list of all actions.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Active'; SET @value = N'Determines if the action is enabled or disabled.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Name'; SET @value = N'Name for the action which is displayed in the selection menu.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Description'; SET @value = N'Details about the action. Displayed in tooltips.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@RequiresClusterWriteRights'; SET @value = N'Determines if the action is available only if the user has cluster write rights.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@RequiresWriteRights'; SET @value = N'Determines if the action is available only if the user has write rights on the current object.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Condition'; SET @value = N'The condition JSON object that is created by the filter builder.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ListCustomTitle'; SET @value = N'Custom title for list SQL dialog.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ListSQL'; SET @value = N'SQL script which is executed when displaying the intial list.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ListLayout'; SET @value = N'Layout information for the list as JSON object.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ListExcelExport'; SET @value = N'Allow export of list dialog to Excel.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SetCellValueAsFilter'; SET @value = N'Apply cell value as filter for the list table.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ReloadListAfterChangingFilter '; SET @value = N'Determines if the list should be reloaded each time the filter in the list dialog is changed.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ActionSQL'; SET @value = N'SQL script which is executed during the action.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ApplyFromField'; SET @value = N'The value of this field is applied to the cell where the action originates from.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SaveBeforeAction'; SET @value = N'Save the system tab or PDT before executing the ActionSQL.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SaveAfterAction'; SET @value = N'Save the system tab or PDT after executing the ActionSQL.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ReloadTreeAfterAction'; SET @value = N'Reload the tree after executing the ActionSQL.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ReloadDetailsAfterAction'; SET @value = N'Reload the system tab or PDT after executing the ActionSQL.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spPOST_ActionLog; GO /* Procedure to POST Action execution log Saxess Software GmbH Testcall DECLARE @RC INT; EXEC @RC = system.spPOST_ActionLog @Username = 'SQL' , @ExecutionUser = 'Testuser' , @ActionID = 'A1' , @StartLocation = '{"Level": "Factory", "Area": "Tree", "FactoryID":"1"}' , @ExecutionType = 'LIST' , @ExecutionSQL = 'SELECT * FROM global.tCodes' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_ActionLog', NULL, NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_ActionLog', 'PARAMETER', NULL) */ CREATE PROCEDURE system.spPOST_ActionLog @Username NVARCHAR(255) , @ExecutionUser NVARCHAR(255) , @ActionID NVARCHAR(255) , @StartLocation NVARCHAR(MAX) , @ExecutionType NVARCHAR(50) , @ExecutionSQL NVARCHAR(MAX) AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 6, @Username, @ExecutionUser, @ActionID, @StartLocation, @ExecutionType, @ExecutionSQL; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; INSERT INTO system.tActionLog ( ExecutionUser , ActionID , StartLocation , ExecutionTime , ExecutionType , ExecutionSQL ) VALUES ( @ExecutionUser , @ActionID , @StartLocation , GETUTCDATE() , @ExecutionType , @ExecutionSQL ); SET @AffectedRows = @@ROWCOUNT; SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 EXEC system.spSEND_Message 'ERROR', @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_ActionLog' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.Actions, Custom) ,@SX_Action NVARCHAR(255) = N'CORE' -- enter Action name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Action; EXEC sys.sp_addextendedproperty N'SX_Action' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to POST Action execution log'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ExecutionUser'; SET @value = N'User who executed the action.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ActionID'; SET @value = N'ActionID which was executed.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@StartLocation'; SET @value = N'Location as JSON object where the Action was executed from.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ExecutionType'; SET @value = N'Type of execution (LIST, ACTION).'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ExecutionSQL'; SET @value = N'SQL script that was executed.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spPOST_Content; GO /* Procedure to POST content information Saxess Software GmbH Last modified: 10/2023 for OCT 5.10 Testcall DECLARE @RC INT; EXEC @RC = system.spPOST_Content @Username = 'SQL', @ModuleID = 'FIN', @ContentID = 'result.tFIN_Konten', @ContentType = 'module', @TechnicalType = 'table', @Description = 'Result Tabelle für Konten', @OrderIndex = 1, @ContentSQL = 'CREATE TABLE result.tFIN_Konten...', @ContentUninstallSQL = 'DROP TABLE IF EXISTS result.tFIN_Konten', @VersionComment = 'Initial', @IsInstalled = 0, @RenameContentID = NULL, @CopyModuleID = NULL, @CopyContentID = NULL SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload','PARAMETER',NULL) */ CREATE PROCEDURE system.spPOST_Content @Username NVARCHAR(255) , @ModuleID NVARCHAR(255) , @ContentID NVARCHAR(255) , @ContentType NVARCHAR(50) = NULL , @TechnicalType NVARCHAR(50) = NULL , @Description NVARCHAR(4000) = NULL , @OrderIndex INT = NULL , @ContentSQL NVARCHAR(MAX) = NULL , @ContentUninstallSQL NVARCHAR(MAX) = NULL , @VersionComment NVARCHAR(255) = NULL , @IsInstalled BIT = NULL AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 11, @Username, @ModuleID, @ContentID, @ContentType, @TechnicalType, @Description, @OrderIndex, @ContentSQL, @ContentUninstallSQL, @VersionComment, @IsInstalled; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Variables DECLARE @ContentKey INT; DECLARE @CurrentOrderIndex INT; DECLARE @CurrentContentVersion INT; DECLARE @CurrentContentSQL NVARCHAR(MAX); DECLARE @CurrentContentUninstallSQL NVARCHAR(MAX); -- Input parameter handling SET @ModuleID = COALESCE(system.fProtectID(@ModuleID), N''); SET @ContentID = COALESCE(@ContentID, N''); IF COALESCE(@ModuleID, N'') = N'' RAISERROR('No ModuleID provided', 11, 1); IF COALESCE(@ContentID, N'') = N'' RAISERROR('No ContentID provided', 11, 1); -- Update OrderIndex IF @OrderIndex IS NOT NULL BEGIN SELECT @CurrentOrderIndex = OrderIndex FROM system.tContents WHERE ModuleID = @ModuleID AND ContentID = @ContentID; IF @CurrentOrderIndex > @OrderIndex OR @CurrentOrderIndex IS NULL BEGIN UPDATE system.tContents SET OrderIndex = OrderIndex + 1 WHERE ModuleID = @ModuleID AND OrderIndex >= @OrderIndex; END IF @CurrentOrderIndex < @OrderIndex BEGIN UPDATE system.tContents SET OrderIndex = OrderIndex - 1 WHERE ModuleID = @ModuleID AND OrderIndex <= @OrderIndex; END SET @AffectedRows += @@ROWCOUNT; END -- Update master data UPDATE system.tContents SET ContentType = COALESCE(@ContentType, ContentType) , TechnicalType = COALESCE(@TechnicalType, TechnicalType) , Description = COALESCE(@Description, Description) , OrderIndex = COALESCE(@OrderIndex, OrderIndex) WHERE ModuleID = @ModuleID AND ContentID = @ContentID; SET @AffectedRows += @@ROWCOUNT; -- Insert new content element IF @AffectedRows = 0 BEGIN INSERT INTO system.tContents ( ModuleKey , ModuleID , ContentID , ContentType , TechnicalType , Description , OrderIndex ) VALUES ( (SELECT ModuleKey FROM system.tModules WHERE ModuleID = @ModuleID) , @ModuleID , @ContentID , COALESCE(@ContentType, N'module') , COALESCE(@TechnicalType, N'query') , COALESCE(@Description, N'') , (SELECT COALESCE(@OrderIndex, MAX(OrderIndex) + 1, 1) FROM system.tContents WHERE ModuleID = @ModuleID) ); SET @ContentKey = @@IDENTITY; SET @AffectedRows += @@ROWCOUNT; -- Create content version if any value is provided IF @ContentSQL IS NOT NULL BEGIN INSERT INTO system.tContentVersions ( ContentKey , ModuleID , ContentID , Version , ContentSQL , ContentUninstallSQL , CreatedBy , CreatedDate , VersionComment , IsInstalled ) VALUES ( @ContentKey , @ModuleID , @ContentID , 1 , COALESCE(@ContentSQL, '') , COALESCE(@ContentUninstallSQL, '') , @TransactUsername , GETUTCDATE() , COALESCE(@VersionComment, 'Initial') , 0 ); SET @AffectedRows += @@ROWCOUNT; END END ELSE BEGIN SELECT @ContentKey = ContentKey FROM system.tContents WHERE ModuleID = @ModuleID AND ContentID = @ContentID; -- Get current content information SELECT TOP 1 @CurrentContentVersion = Version , @CurrentContentSQL = ContentSQL , @CurrentContentUninstallSQL = ContentUninstallSQL FROM system.tContentVersions WHERE ModuleID = @ModuleID AND ContentID = @ContentID ORDER BY Version DESC; -- Create new content version if value or uninstallvalue has changed IF (COALESCE(@ContentSQL, '') <> COALESCE(@CurrentContentSQL, '') OR COALESCE(@ContentUninstallSQL, '') <> COALESCE(@CurrentContentUninstallSQL, '')) AND @ContentSQL IS NOT NULL BEGIN INSERT INTO system.tContentVersions ( ContentKey , ModuleID , ContentID , Version , ContentSQL , ContentUninstallSQL , CreatedBy , CreatedDate , VersionComment , IsInstalled ) VALUES ( @ContentKey , @ModuleID , @ContentID , COALESCE(@CurrentContentVersion, 0) + 1 , COALESCE(@ContentSQL, '') , COALESCE(@ContentUninstallSQL, '') , @TransactUsername , GETUTCDATE() , COALESCE(@VersionComment, 'Initial') , 0 ); SET @AffectedRows += @@ROWCOUNT; END END; -- Fill gaps in OrderIndex WITH Contents AS ( SELECT * , ROW_NUMBER() OVER (PARTITION BY ModuleKey ORDER BY OrderIndex) AS NewOrderIndex FROM system.tContents ) UPDATE Contents SET OrderIndex = NewOrderIndex; -- Set content installed status IF @IsInstalled IS NOT NULL BEGIN UPDATE system.tContentVersions SET IsInstalled = 0 WHERE ContentID = @ContentID; IF @IsInstalled = 1 BEGIN UPDATE system.tContentVersions SET IsInstalled = 1 WHERE ModuleID = @ModuleID AND ContentID = @ContentID AND Version = ( SELECT MAX(Version) FROM system.tContentVersions WHERE ModuleID = @ModuleID AND ContentID = @ContentID ); END END SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Content' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET the module tree'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ModuleID'; SET @value = N'ID of the module that should be saved.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ContentID'; SET @value = N'ID of the content that should be saved.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ContentType'; SET @value = N'OCT related type of the content (module, pipeline, list, format, factory, productline, product, tab, tabledata).'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TechnicalType'; SET @value = N'Technical type of the content (table, view, storedprocedure, query).'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Description'; SET @value = N'Details about the content.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ContentSQL'; SET @value = N'The SQL query that is executed when installing the content element.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ContentUninstallSQL'; SET @value = N'The SQL query that is executed when uninstalling the content element.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@VersionComment'; SET @value = N'A comment for a newly created version of the content element.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@IsInstalled'; SET @value = N'Flag if the content element is currently installed.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spPOST_DataSource; GO /* Procedure to POST a DataSource Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall Base tables SELECT * FROM system.tDataSources Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spPOST_DataSource @Username = N'SQL' ,@DataSourceID = N'0' -- must be numeric for compatibility with modules. Full ID Support from 6.0 on ,@DataSourceName = N'GT1' ,@SourceType = N'' ,@ConnectionValuesJSON = N'' PRINT @RC Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_DataSource',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_DataSource','PARAMETER',NULL) */ CREATE PROCEDURE system.spPOST_DataSource ( @Username NVARCHAR(255) ,@DataSourceID NVARCHAR(50) ,@DataSourceName NVARCHAR(255) = '' ,@SourceType NVARCHAR(50) = '' ,@ConnectionValuesJSON NVARCHAR(MAX) = '' ) -- must be always execute as owner as it uses IDENTITY INSERT - but this don't work on Azure, if the Owner is an AAD User - OPEN PROBLEM WITH EXECUTE AS 'dbo' AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' --set Strings inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@DataSourceID ,N'NULL') + N''',''' + ISNULL(@DataSourceName ,N'NULL') + N''',''' + ISNULL(@SourceType ,N'NULL') + N''',''' + ISNULL(@ConnectionValuesJSON ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @DataSourceName IS NULL SET @DataSourceName = N''; IF @SourceType IS NULL SET @SourceType = N''; IF @ConnectionValuesJSON IS NULL SET @ConnectionValuesJSON = N''; SET @DataSourceID = COALESCE(system.fProtectID(@DataSourceID), N''); -- START TRANSACTION *********************************************************************************** BEGIN TRY BEGIN TRANSACTION spPOST_DataSource -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR(N'Transaction user don`t exists', 16, 10); END; IF @DataSourceID = '' BEGIN SET @ResultCode = 403; RAISERROR(N'DataSourceID must not be empty.', 16, 10); END; IF @DataSourceID = '0' BEGIN SET @ResultCode = 403; RAISERROR(N'DataSourceID 0 is reserved for Companies without Datasource and cant be created.', 16, 10); END; IF @DataSourceName = '' BEGIN SET @ResultCode = 403; RAISERROR('DataSourceName must not be empty.', 16, 10); END; -- Data Transactions ################################################# -- delete / recreate DataSource with same Key DELETE FROM system.tDataSources WHERE DataSourceID = @DataSourceID; INSERT INTO system.tDataSources (DataSourceID,DataSourceName,SourceType,ConnectionValuesJSON) VALUES (@DataSourceID,@DataSourceName,@SourceType,@ConnectionValuesJSON) SET @ResultCode = 200; COMMIT TRANSACTION spPOST_DataSource END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spPOST_DataSource IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_DataSource' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to POST a DataSource.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@DataSourceID'; SET @value = N'The ID of the DataSource, an ID which is used to identify the datasource'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@DataSourceName'; SET @value = N'The name of the DataSource, free description'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceType'; SET @value = N'The Source type of the datasource, its a CODE Word like MSSQL and determines the way a connection is established.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ConnectionValuesJSON'; SET @value = N'Encrypted string containing the Connection String including credentials.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spPOST_Email; GO /* Procedure to POST one Email - send NULL as Parametervalue to keep the existing Value - you can skip Parameter you would send with NULL, as NULL is the default value - you can pass * as EmailID to create a new Email E1..En This Procedure will be executed - from the OCT Application Edit GUI - always as User which is logged in - from Consultant in SSMS - as User SQL (which there must have role FactorySerivce or db_owner) - from a control script (create Email for exections) as User SQL Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall Procedure DECLARE @RC INT ,@ReturnedEmailID NVARCHAR (50) EXEC @RC = system.spPOST_Email @Username = 'SQL' ,@EmailID = '*' ,@EmailSubject = 'Wichtig' ,@EmailRecipients = 'gerd.tautenhahn@saxess-software.com' ,@EmailText = 'Hello World' ,@EmailAttachments = NULL -- List for FileIDs ,@EmailStatusCODE = 'PENDING' ,@SourceTypeCODE = 'PIPELINE' ,@SourceID = 'DS1-P2' ,@CreationDate = '20220613 08:04:02.111' ,@PostDate = '20220613 08:08:02.111' ,@OutputID = @ReturnedEmailID OUTPUT; PRINT @RC; PRINT 'EmailID Output: '+ ISNULL(@ReturnedEmailID,'no Output set'); SELECT * FROM system.tEmails; DELETE FROM system.tEmails; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_Email',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_Email','PARAMETER',NULL) */ CREATE PROCEDURE system.spPOST_Email ( @Username NVARCHAR(255) ,@EmailID NVARCHAR(50) ,@EmailSubject NVARCHAR(2000) = NULL ,@EmailRecipients NVARCHAR(4000) = NULL ,@EmailText NVARCHAR(MAX) = NULL ,@EmailAttachments NVARCHAR(2000) = NULL ,@EmailStatusCODE NVARCHAR(50) = NULL ,@SourceTypeCODE NVARCHAR(50) = NULL ,@SourceID NVARCHAR(50) = NULL ,@CreationDate DATETIME = NULL ,@PostDate DATETIME = NULL ,@OutputID NVARCHAR(50) = NULL OUTPUT ) AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' --set Strings inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@EmailID ,N'NULL') + N''',''' + ISNULL(@EmailSubject ,N'NULL') + N''',''' + ISNULL(@EmailRecipients ,N'NULL') + N''',''' + ISNULL(@EmailText ,N'NULL') + N''',''' + ISNULL(@EmailAttachments ,N'NULL') + N''',''' + ISNULL(@EmailStatusCODE ,N'NULL') + N''',''' + ISNULL(@SourceTypeCODE ,N'NULL') + N''',''' + ISNULL(@SourceID ,N'NULL') + N''',''' + ISNULL(CONVERT(NVARCHAR(50),@CreationDate,104) ,N'NULL') + N''',''' + ISNULL(CONVERT(NVARCHAR(50),@PostDate,104) ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime() ,@RowCount INT = 0 ,@LastEmail INT = 0; -- NULL Protection for all mandatory Input parameters IF @Username IS NULL SET @Username = N''; IF @EmailID IS NULL SET @EmailID = N''; -- Content Protection for specific input parameters (e.g.IDs) SET @EmailID = system.fProtectID (@EmailID); IF @EmailStatusCODE NOT IN ('PENDING','ERROR','SENT') SET @EmailStatusCODE = 'CODEERROR'; -- START TRANSACTION *********************************************************************************** BEGIN TRY BEGIN TRANSACTION spPOST_Email -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Data Transaction DROP TABLE IF EXISTS #CurrentEmail; CREATE TABLE #CurrentEmail ( EmailKey BIGINT NOT NULL ,EmailID NVARCHAR(50) COLLATE DATABASE_DEFAULT NOT NULL ,EmailSubject NVARCHAR(2000) COLLATE DATABASE_DEFAULT NULL ,EmailRecipients NVARCHAR(4000) COLLATE DATABASE_DEFAULT NULL ,EmailText NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NULL ,EmailAttachments NVARCHAR(2000) COLLATE DATABASE_DEFAULT NULL ,EmailStatusCODE NVARCHAR(50) COLLATE DATABASE_DEFAULT NULL ,SourceTypeCODE NVARCHAR(50) COLLATE DATABASE_DEFAULT NULL ,SourceID NVARCHAR(50) COLLATE DATABASE_DEFAULT NULL ,CreationDate DATETIME NULL ,PostDate DATETIME NULL ) -- Cache current values INSERT INTO #CurrentEmail SELECT EmailKey ,EmailID ,EmailSubject ,EmailRecipients ,EmailText ,EmailAttachments ,EmailStatusCODE ,SourceTypeCODE ,SourceID ,CreationDate ,PostDate FROM system.tEmails WHERE EmailID = @EmailID; -- insert or update Email SELECT @RowCount = Count (*) FROM #CurrentEmail; IF @RowCount = 0 -- case new Email BEGIN --- determine next free ID if * is sended as ID, AutoID is generated as P1..Pn IF @EmailID = '*' BEGIN SELECT TOP 1 @LastEmail = TRY_CAST(RIGHT(EmailID,LEN(EmailID)-1) AS INT ) FROM system.tEmails WHERE EmailID LIKE 'E%' AND LEN(EmailID) > 1 AND TRY_CAST(RIGHT(EmailID,LEN(EmailID)-1) AS INT) IS NOT NULL ORDER BY TRY_CAST(RIGHT(EmailID,LEN(EmailID)-1) AS INT) DESC IF @LastEmail IS NULL SET @LastEmail = 0; SET @EmailID = 'E' + CAST(@LastEmail +1 AS NVARCHAR(50)); END -- INSERT with default values if NULL INSERT INTO system.tEmails ( EmailID ,EmailSubject ,EmailRecipients ,EmailText ,EmailAttachments ,EmailStatusCODE ,SourceTypeCODE ,SourceID ,CreationDate ,PostDate ) VALUES ( @EmailID ,ISNULL(@EmailSubject ,'') ,ISNULL(@EmailRecipients ,'') ,ISNULL(@EmailText ,'') ,ISNULL(@EmailAttachments ,'') ,ISNULL(@EmailStatusCODE ,'') ,ISNULL(@SourceTypeCODE ,'') ,ISNULL(@SourceID ,'') ,ISNULL(@CreationDate ,'1900-01-01 08:08:00.000') ,ISNULL(@PostDate ,NULL) ) SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 201; END ELSE -- Case existing Email BEGIN -- UPDATE UPDATE tE SET EmailSubject = ISNULL(@EmailSubject ,cE.EmailSubject ) ,EmailRecipients = ISNULL(@EmailRecipients ,cE.EmailRecipients ) ,EmailText = ISNULL(@EmailText ,cE.EmailText ) ,EmailAttachments = ISNULL(@EmailAttachments ,cE.EmailAttachments ) ,EmailStatusCODE = ISNULL(@EmailStatusCODE ,cE.EmailStatusCODE ) ,SourceTypeCODE = ISNULL(@SourceTypeCODE ,cE.SourceTypeCODE ) ,SourceID = ISNULL(@SourceID ,cE.SourceID ) ,CreationDate = ISNULL(@CreationDate ,cE.CreationDate ) ,PostDate = ISNULL(@PostDate ,cE.PostDate ) FROM system.tEmails tE LEFT JOIN #CurrentEmail cE ON tE.EmailKey = cE.EmailKey WHERE tE.EmailKey = cE.EmailKey SET @ResultCode = 200; SET @EffectedRows = @@ROWCOUNT; END -- OUTPUT Parameter SET @OutputID = @EmailID; COMMIT TRANSACTION spPOST_Email END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spPOST_Email IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Email' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to POST a Email.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@EmailID'; SET @value = N'ID of the Email, will be updated or replaced, use * to create an new Email with AutoID E1..En'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@EmailSubject'; SET @value = N'Subject of the Email'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@EmailRecipients'; SET @value = N'List of Email Recipients, semicolon separated'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@EmailText'; SET @value = N'EmailText'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@EmailAttachments'; SET @value = N'List of FileIDs, semicolon separated'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@EmailStatusCODE'; SET @value = N'Status Code for the Email, can be PENDING,ERROR,SENT'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceTypeCODE'; SET @value = N'Source Type (who created this mail) - can be PIPELINE, EVENT, MANUALLY'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@SourceID'; SET @value = N'ID combination of the source oftthe mail - e.g. [DatasourceID]-[PipelineID] or [FactoryID]-[ProductLineID]-[ProductID] or someting else - only for Debugging, not technical used'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@CreationDate'; SET @value = N'Datetime of Email creation'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PostDate'; SET @value = N'Datetime of Email posting'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; DROP PROCEDURE IF EXISTS system.spPOST_Icon; GO /* Gerd Tautenhahn for Saxess Software GmbH Last modified: 10/2022 for OCT 5.8 Testcall EXEC system.spPOST_Icon 'SQL' ,'Hase' ,'svg' ,0x3; SELECT * FROM system.tIcons Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_Icon',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_Icon','PARAMETER',NULL) */ CREATE PROCEDURE system.spPOST_Icon @UserName NVARCHAR(255) = N'' ,@IconName NVARCHAR(255) = N'' ,@FileExtension NVARCHAR(50) = NULL ,@FileBody VARBINARY(MAX) = NULL AS BEGIN SET NOCOUNT ON DECLARE @TransactionUsername NVARCHAR(255) = N'' ,@ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' + ISNULL(@Username, N'NULL') + N''', ''' + ISNULL(@IconName, N'NULL') + N''', ''' + ISNULL(@FileExtension, N'NULL') + N''', ' + ISNULL(CAST(DATALENGTH(@FileBody) AS NVARCHAR(MAX)) + N' bytes', N'NULL') ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N''; -- STEP 0.1 - NULL Protection IF @UserName IS NULL SET @UserName = N''; BEGIN TRY BEGIN TRANSACTION POST_Icon; -- STEP 0.2 - Protect input parameters SET @Username = system.fProtectString (@Username); -- STEP 1.1 - Determine transaction user SELECT @TransactionUsername = system.fDetermineTransactionUsername (@Username); IF @TransactionUsername = N'403' BEGIN SET @ResultCode = 403; THROW 60000, N'Transaction user don`t exists', 100; END; -- STEP 1.2 - Check input parameters IF @FileBody IS NULL BEGIN SET @ResultCode = 403; THROW 60000, N'FileBody is null', 100; END; IF DATALENGTH(@FileBody) = 0 BEGIN SET @ResultCode = 403; THROW 60000, N'FileBody is empty', 100; END; -- Delete File DELETE FROM system.tIcons WHERE IconName = @IconName IF LEN(@FileExtension) = 0 BEGIN SET @FileExtension = NULL; END; -- STEP 2.2 - Creating new file INSERT INTO system.tIcons (IconName,FileExtension, FileBody) VALUES (@IconName,@FileExtension, @FileBody); SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; COMMIT TRANSACTION POST_Icon; END TRY BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION POST_Icon; IF @Error_state = 100 BEGIN PRINT @Comment; END ELSE IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactionUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Icon' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'spPOST_Icon'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; SET @level2name = N'@UserName'; SET @value = N'UserName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@IconName'; SET @value = N'IconName'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FileExtension'; SET @value = N'FileExtension'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@FileBody'; SET @value = N'FileBody'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spPOST_Module; GO /* Procedure to POST module information Saxess Software GmbH Last modified: 03/2024 for OCT 5.10 Testcall DECLARE @RC INT; EXEC @RC = system.spPOST_Module @Username = 'SQL', @ModuleID = 'FIN', @ModuleType = 'integration', @Description = 'Finanzdaten', @Version = 1, @AdminRequired = 0, @DescriptionURL = '' SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'agent', 'PROCEDURE', 'pPostBulkload','PARAMETER',NULL) */ CREATE PROCEDURE system.spPOST_Module @Username NVARCHAR(255), @ModuleID NVARCHAR(255), @ModuleType NVARCHAR(50) = NULL, @Description NVARCHAR(4000) = NULL, @Version INT = NULL, @AdminRequired BIT = NULL, @DescriptionURL NVARCHAR(255) = NULL AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 7, @Username, @ModuleID, @ModuleType, @Description, @Version, @AdminRequired, @DescriptionURL; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Input parameter handling SET @ModuleID = COALESCE(system.fProtectID(@ModuleID), N''); SET @Description = COALESCE(system.fProtectString(@Description), N''); SET @Version = COALESCE(@Version, 1); IF COALESCE(@ModuleID, N'') = N'' RAISERROR ('No ModuleID provided', 11, 1); -- Update master data UPDATE system.tModules SET ModuleType = @ModuleType , Description = @Description , AdminRequired = COALESCE(@AdminRequired, 0) , DescriptionURL = @DescriptionURL WHERE ModuleID = @ModuleID; SET @AffectedRows = @@ROWCOUNT -- Insert new data IF @AffectedRows = 0 BEGIN INSERT INTO system.tModules ( ModuleID , ModuleType , Description , Version , AdminRequired , DescriptionURL ) VALUES ( @ModuleID , @ModuleType , COALESCE(@Description, N'') , COALESCE(@Version, 1) , COALESCE(@AdminRequired, 0) , COALESCE(@DescriptionURL, N'') ); SET @AffectedRows = @@ROWCOUNT; END SET @ResultCode = 200 END TRY BEGIN CATCH SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 RAISERROR(@Comment, 16, 10); RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Module' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to GET the module tree'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- SET parameter documentation SET @level2name = N'@Username'; SET @value = N'Username'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ModuleID'; SET @value = N'ID of the module that should be saved.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ModuleType'; SET @value = N'Type of the module (integration, planning, visualization, staging).'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Description'; SET @value = N'Details about the module.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Version'; SET @value = N'Version of the module if it was downloaded from the content server. NULL will automatically set it to 1.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@AdminRequired'; SET @value = N'Determines if this module needs to be installed with administrator rights.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@DescriptionURL'; SET @value = N'Link to Saxess website with detailed description for the module.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spPOST_Pipeline; GO /* Procedure to POST a Pipeline Saxess Software GmbH Testcall DECLARE @RC INT; EXEC @RC = system.spPOST_Pipeline @Username = 'SQL' , @PipelineID = '*' , @PipelineName = NULL , @PipelineDescription = 'Hello World' , @PipelineDescriptionIconColor = '#808080' , @OrderOfDisplay = NULL , @EmailNotificationCODE = NULL , @ExcludedExecutionTimesJSON = NULL , @OutputID = @ReturnedPipelineID OUTPUT; SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_Pipeline', NULL, NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_Pipeline', 'PARAMETER', NULL) */ CREATE PROCEDURE system.spPOST_Pipeline @Username NVARCHAR(255) , @PipelineID NVARCHAR(50) , @PipelineName NVARCHAR(255) = NULL , @PipelineDescription NVARCHAR(4000) = NULL , @PipelineDescriptionIconColor NVARCHAR(50) = NULL , @OrderOfDisplay INT = NULL , @EmailNotificationCODE NVARCHAR(50) = NULL , @ExcludedExecutionTimesJSON NVARCHAR(MAX) = NULL , @OutputID NVARCHAR(50) = NULL OUTPUT AS BEGIN BEGIN TRY -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 8, @Username, @PipelineID, @PipelineName, @PipelineDescription, @PipelineDescriptionIconColor, @OrderOfDisplay, @EmailNotificationCODE, @ExcludedExecutionTimesJSON; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Declare variables DECLARE @LastPipelineID INT; DECLARE @RowCount INT; -- Input parameter handling SET @PipelineID = COALESCE(system.fProtectID(@PipelineID), N''); IF @EmailNotificationCODE NOT IN ('NEVER','ALWAYS','ONERROR') SET @EmailNotificationCODE = 'NEVER'; BEGIN TRANSACTION spPOST_Pipeline DROP TABLE IF EXISTS #CurrentPipeline; CREATE TABLE #CurrentPipeline ( PipelineKey BIGINT NOT NULL , PipelineID NVARCHAR(50) COLLATE DATABASE_DEFAULT NOT NULL , PipelineName NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL , PipelineDescription NVARCHAR(4000) COLLATE DATABASE_DEFAULT NOT NULL , PipelineDescriptionIconColor NVARCHAR(50) COLLATE DATABASE_DEFAULT NOT NULL , OrderOfDisplay INT NOT NULL , EmailNotificationCODE NVARCHAR(50) COLLATE DATABASE_DEFAULT NOT NULL , ExcludedExecutionTimesJSON NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ); -- Cache current values INSERT INTO #CurrentPipeline SELECT PipelineKey , PipelineID , PipelineName , PipelineDescription , PipelineDescriptionIconColor , OrderOfDisplay , EmailNotificationCODE , ExcludedExecutionTimesJSON FROM system.tPipelines WHERE PipelineID = @PipelineID; SELECT @RowCount = COUNT(1) FROM #CurrentPipeline; IF @RowCount = 0 BEGIN -- Determine next free ID if * is sent as ID, AutoID is generated as P1..Pn IF @PipelineID = '*' BEGIN SELECT TOP 1 @LastPipelineID = TRY_CAST(RIGHT(PipelineID, LEN(PipelineID) - 1) AS INT) FROM system.tPipelines WHERE PipelineID LIKE 'P%' AND LEN(PipelineID) > 1 AND TRY_CAST(RIGHT(PipelineID, LEN(PipelineID) - 1) AS INT) IS NOT NULL ORDER BY TRY_CAST(RIGHT(PipelineID, LEN(PipelineID) - 1) AS INT) DESC IF @LastPipelineID IS NULL SET @LastPipelineID = 0; SET @PipelineID = 'P' + CAST(@LastPipelineID + 1 AS NVARCHAR(50)); END -- Determine Orderindex IF @OrderOfDisplay IS NULL BEGIN SELECT @OrderOfDisplay = MAX(OrderOfDisplay) + 1 FROM system.tPipelines END -- Insert with default values if NULL INSERT INTO system.tPipelines ( PipelineID , PipelineName , PipelineDescription , PipelineDescriptionIconColor , OrderOfDisplay , EmailNotificationCODE , ExcludedExecutionTimesJSON ) VALUES ( @PipelineID , ISNULL(@PipelineName, 'Unknown') , ISNULL(@PipelineDescription, '') , ISNULL(@PipelineDescriptionIconColor, '') , ISNULL(@OrderOfDisplay, 0) , ISNULL(@EmailNotificationCODE, 'NEVER') , ISNULL(@ExcludedExecutionTimesJSON, '[]') ); SET @AffectedRows = @@ROWCOUNT; SET @ResultCode = 201; END ELSE BEGIN -- update as insert / delete is not possible due to primary key constraints UPDATE tP SET PipelineName = ISNULL(@PipelineName, cP.PipelineName) , PipelineDescription = ISNULL(@PipelineDescription, cP.PipelineDescription) , PipelineDescriptionIconColor = ISNULL(@PipelineDescriptionIconColor, cP.PipelineDescriptionIconColor) , OrderOfDisplay = ISNULL(@OrderOfDisplay, cP.OrderOfDisplay) , EmailNotificationCODE = ISNULL(@EmailNotificationCODE, cP.EmailNotificationCODE) , ExcludedExecutionTimesJSON = ISNULL(@ExcludedExecutionTimesJSON, cP.ExcludedExecutionTimesJSON) FROM system.tPipelines tP LEFT JOIN #CurrentPipeline cP ON tP.PipelineKey = cP.PipelineKey WHERE tP.PipelineKey = cP.PipelineKey; SET @ResultCode = 200; SET @AffectedRows = @@ROWCOUNT; END SET @OutputID = @PipelineID; COMMIT TRANSACTION spPOST_Pipeline END TRY BEGIN CATCH ROLLBACK TRANSACTION spPOST_Pipeline; SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 EXEC system.spSEND_Message N'ERROR', @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_Pipeline' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to POST a Pipeline.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@PipelineID'; SET @value = N'ID of the Pipeline, will be updated or replaced, use * to create an new Pipeline with AutoID P1..Pn'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PipelineName'; SET @value = N'Name of the Pipeline'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PipelineDescription'; SET @value = N'Description of the Pipeline'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@PipelineDescriptionIconColor'; SET @value = N'Icon Color for the description, written as HEX Code.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@OrderOfDisplay'; SET @value = N'Ordernumber for displaying the Pipelines'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@EmailNotificationCODE'; SET @value = N'EmailNotificationCODE of the Pipeline - must be NEVER, ALWAYS or ONERROR.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ExcludedExecutionTimesJSON'; SET @value = N'Times that are excluded when the pipeline is started by a scheduled job (in JSON format).'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spPOST_PipelineLog; GO /* Procedure to POST a PipelineLog - can be posted with ExecutionID = NULL and will return a new generated ExecutionID, which is a unique number for the database - optional Values can be sended as NULL or skiped, which means "keep what is" - the PipelineName will be materialized in the Background to be avaiable in historic logs after deleting the pipeline Saxess Software GmbH Last modified: 08/2023 for OCT 5.10 Testcall Procedure DECLARE @RC INT ,@ReturnID NVARCHAR(50) ,@StartTime DATETIME = GETUTCDATE(); EXEC @RC = system.spPOST_PipelineLog @Username = 'SQL' ,@PipelineID = 'P1' ,@StepID = 'S1' ,@ExecutionID = '*' ,@StatusCODE = 'RUNNING' -- WAITING,RUNNING, STOPPED (from User), DIED (computer restarted etc.), FINISHED (succesful), FINISHEDWITHERROR'; ,@StartedBy = 'W10\admin' -- Username or the word "Scheduler" ,@StartTime = @StartTime -- Timestamp ,@EndTime = NULL -- Timestamp ,@OutPutID = @ReturnID OUTPUT PRINT CONCAT('ReturnCode: ', @RC, ' OutputID: ', @ReturnID); SELECT * FROM system.tPipelines; SELECT * FROM system.tPipelineLog; select * from system.tAPILog Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_PipelineLog',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_PipelineLog','PARAMETER',NULL) */ CREATE PROCEDURE system.spPOST_PipelineLog @Username NVARCHAR(255) , @PipelineID NVARCHAR(50) , @StepID NVARCHAR(50) = N'' , @ExecutionID NVARCHAR(50) , @ExecutionServerName NVARCHAR(255) = NULL , @StatusCODE NVARCHAR(50) = NULL , @StartedBy NVARCHAR(255) = NULL , @StartTime DATETIME = NULL , @EndTime DATETIME = NULL , @OutputID NVARCHAR(50) = NULL OUTPUT AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' --set Strings inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@PipelineID ,N'NULL') + N''',''' + ISNULL(@StepID ,N'NULL') + N''',''' + ISNULL(@ExecutionID ,N'NULL') + N''',''' + ISNULL(@ExecutionServerName ,N'NULL') + N''',''' + ISNULL(@StatusCode ,N'NULL') + N''',''' + ISNULL(@StartedBy ,N'NULL') + N''',''' + ISNULL(CONVERT(NVARCHAR(50),@StartTime,104) ,N'NULL') + N''',''' + ISNULL(CONVERT(NVARCHAR(50),@EndTime,104) ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTimeRun DATETIME2 = SysUTCDateTime() ,@RowCount INT = 0 ,@PipelineName NVARCHAR(255) = N'' ,@StepName NVARCHAR(255) = N'' ,@LogCount INT = 0; -- NULL Protection for all non-optional Input parameters IF @Username IS NULL SET @Username = N''; IF @PipelineID IS NULL SET @PipelineID = N''; IF @ExecutionID IS NULL SET @ExecutionID = N''; SET @PipelineID = system.fProtectID(@PipelineID); BEGIN TRY BEGIN TRANSACTION spPOST_PipelineLog -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user doesn''t exist', 16, 10); END; -- check if PipelineID exists IF NOT EXISTS ( SELECT 1 FROM system.tPipelines WHERE PipelineID = @PipelineID ) BEGIN SET @ResultCode = 404; RAISERROR('PipelineID doesn''t exist', 16, 10); END -- check if ExecutionID exists SELECT @LogCount = COUNT(1) FROM system.tPipelineLog WHERE PipelineID = @PipelineID AND ExecutionID = @ExecutionID; -- check if StepID exists IF @StepID <> '' AND NOT EXISTS ( SELECT 1 FROM system.tPipelineSteps AS ps JOIN system.tPipelines AS p ON p.PipelineKey = ps.PipelineKey WHERE ps.StepID = @StepID AND p.PipelineID = @PipelineID ) BEGIN SET @ResultCode = 404; RAISERROR('StepID doesn''t exist', 16, 10); END -- check if log exists IF @StepID <> '' AND @ExecutionID <> '' AND @ExecutionID <> '*' BEGIN SELECT @LogCount = COUNT(1) FROM system.tPipelineLog WHERE PipelineID = @PipelineID AND ExecutionID = @ExecutionID AND StepID = @StepID; END -- insert or update data IF @LogCount = 0 BEGIN IF @ExecutionID = '' OR @ExecutionID = '*' BEGIN -- create new ExecutionID SELECT @ExecutionID = 'EID'+ CAST(NEXT VALUE FOR system.seqPipelineLog AS NVARCHAR(50)); END -- insert new row -- get pipeline name SELECT @PipelineName = PipelineName FROM system.tPipelines WHERE PipelineID = @PipelineID; -- get step name SELECT @StepName = ps.StepName FROM system.tPipelineSteps AS ps JOIN system.tPipelines AS p ON p.PipelineKey = ps.PipelineKey WHERE p.PipelineID = @PipelineID AND ps.StepID = @StepID; -- insert new row INSERT INTO system.tPipelineLog ( PipelineID , PipelineName , StepID , StepName , ExecutionID , ExecutionServerName , StatusCODE , StartedBy , StartTime , EndTime ) VALUES ( @PipelineID , @PipelineName , @StepID , @StepName , @ExecutionID , ISNULL(@ExecutionServerName,'') , ISNULL(@StatusCODE,'') , ISNULL(@StartedBy,'') , ISNULL(@StartTime,NULL) , ISNULL(@EndTime,NULL) ); SET @ResultCode = 201; END ELSE BEGIN UPDATE system.tPipelineLog SET StatusCODE = @StatusCODE , EndTime = @EndTime WHERE PipelineID = @PipelineID AND StepID = @StepID AND ExecutionID = @ExecutionID; SET @ResultCode = 200; END SET @EffectedRows = @@ROWCOUNT; -- Output SET @OutputID = @ExecutionID; COMMIT TRANSACTION spPOST_PipelineLog END TRY BEGIN CATCH SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spPOST_PipelineLog SET @ResultCode = 500; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_PipelineLog' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to POST an Logentry for an Pipeline'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@PipelineID'; SET @value = N'ID of the Pipeline this LogEntry belongs to'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@StepID'; SET @value = N'ID of the Step this LogEntry belongs to (empty if entry is only for pipeline)'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ExecutionID'; SET @value = N'ExecutionID in case of update operation - POST as * to generate a new ExecutionID '; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ExecutionServerName'; SET @value = N'Name of the Server, which executed this task'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@StatusCODE'; SET @value = N'Status CODE of the PipelineLog - see list at table definiton.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@StartedBy'; SET @value = N'Name of the user who started this pipeline, is Scheduler in case of scheduler started'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@StartTime'; SET @value = N'Time of starting the Pipeline'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@EndTime'; SET @value = N'Time the pipeline was finished or setted to finish due to not succesful ending.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@OutputID'; SET @value = N'Returns the used or newly generated ExecutionID'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spPOST_PipelineSchedule; GO /* Gerd Tautenhahn for Saxess Software GmbH Last modified: 09/2024 for OCT 5.9 Testcall Procedure DECLARE @RC INT ,@ReturnedScheduleID NVARCHAR(50); EXEC @RC = system.spPOST_PipelineSchedule @Username = 'SQL' ,@PipelineID = 'P1' ,@ScheduleID = '*' ,@ScheduleName = 'Hallo' ,@ScheduleDescription = 'Desc' ,@ScheduleDetailsJSON = '{}' ,@Active = 1 ,@OutputID = @ReturnedScheduleID OUTPUT; PRINT @RC PRINT 'ScheduleID OUT: ' + @ReturnedScheduleID; SELECT * FROM system.tPipelineSchedules; SELECT * FROM system.tPipelines; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_PipelineSchedule',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_PipelineSchedule','PARAMETER',NULL) */ CREATE PROCEDURE system.spPOST_PipelineSchedule ( @Username NVARCHAR(255) ,@PipelineID NVARCHAR(50) ,@ScheduleID NVARCHAR(50) ,@ScheduleName NVARCHAR(255) = NULL ,@ScheduleDescription NVARCHAR(4000) = NULL ,@ScheduleDetailsJSON NVARCHAR(MAX) = NULL ,@Active BIT = NULL ,@OutputID NVARCHAR(50) = NULL OUTPUT ) AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' --set Strings inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@PipelineID ,N'NULL') + N''',''' + ISNULL(@ScheduleID ,N'NULL') + N''',''' + ISNULL(@ScheduleName ,N'NULL') + N''',''' + ISNULL(@ScheduleDescription ,N'NULL') + N''',''' + ISNULL(@ScheduleDetailsJSON ,N'NULL') + N''',' + ISNULL(CAST(@Active AS NCHAR(1)) ,N'NULL') + N'' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N'' ,@StartTime DATETIME2 = SysUTCDateTime() ,@PipelineKey BIGINT = 0 ,@RowCount INT = 0 ,@LastSchedule INT = 0; -- NULL Protection for all Input parameters IF @Username IS NULL SET @Username = N''; IF @ScheduleID IS NULL SET @ScheduleID = N''; IF @PipelineID IS NULL SET @PipelineID = N''; -- Content Protection for specific input parameters (e.g.IDs) SET @ScheduleID = system.fProtectID (@ScheduleID); -- START TRANSACTION *********************************************************************************** BEGIN TRY BEGIN TRANSACTION spPOST_PipelineSchedule -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user don`t exists', 16, 10); END; -- Check Existence of Pipeline IF NOT EXISTS ( SELECT PipelineID FROM system.tPipelines WHERE PipelineID = @PipelineID ) BEGIN SET @ResultCode = 404; RAISERROR('Pipeline don`t exits', 16, 10); END SELECT @PipelineKey = PipelineKey FROM system.tPipelines WHERE PipelineID = @PipelineID; -- Cache DROP TABLE IF EXISTS #CurrentSchedule; CREATE TABLE #CurrentSchedule ( ScheduleKey BIGINT NOT NULL ,ScheduleID NVARCHAR(50) COLLATE DATABASE_DEFAULT NOT NULL ,ScheduleName NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL ,ScheduleDescription NVARCHAR(4000) COLLATE DATABASE_DEFAULT NOT NULL ,ScheduleDetailsJSON NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL ,Active BIT NOT NULL ) INSERT INTO #CurrentSchedule SELECT ScheduleKey ,ScheduleID ,ScheduleName ,ScheduleDescription ,ScheduleDetailsJSON ,Active FROM system.tPipelineSchedules WHERE PipelineKey = @PipelineKey AND ScheduleID = @ScheduleID -- Data transaction SELECT @RowCount = Count (*) FROM #CurrentSchedule; IF @RowCount = 0 BEGIN --- determine next free ID if * is sended as ID, AutoID is generated as S1..Sn per Pipeline IF @ScheduleID = '*' BEGIN SELECT TOP 1 @LastSchedule = TRY_CAST(RIGHT(ScheduleID,LEN(ScheduleID)-1) AS INT ) FROM system.tPipelineSchedules WHERE ScheduleID LIKE 'J%' AND LEN(ScheduleID) > 1 AND PipelineKey = @PipelineKey AND TRY_CAST(RIGHT(ScheduleID,LEN(ScheduleID)-1) AS INT) IS NOT NULL ORDER BY TRY_CAST(RIGHT(ScheduleID,LEN(ScheduleID)-1) AS INT) DESC IF @LastSchedule IS NULL SET @LastSchedule = 0; SET @ScheduleID = 'J' + CAST(@LastSchedule + 1 AS NVARCHAR(50)); END -- INSERT with default values if NULL INSERT INTO system.tPipelineSchedules ( PipelineKey ,ScheduleID ,ScheduleName ,ScheduleDescription ,ScheduleDetailsJSON ,Active ) VALUES ( @PipelineKey ,@ScheduleID ,ISNULL(@ScheduleName,'') ,ISNULL(@ScheduleDescription,'') ,ISNULL(@ScheduleDetailsJSON,'') ,ISNULL(@Active,1) ) SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 201; END ELSE BEGIN -- UPDATE as Insert / delete not possible due to primary key constraints UPDATE tPS SET ScheduleName = ISNULL(@ScheduleName ,cS.ScheduleName) ,ScheduleDescription = ISNULL(@ScheduleDescription ,cS.ScheduleDescription) ,ScheduleDetailsJSON = ISNULL(@ScheduleDetailsJSON ,cS.ScheduleDetailsJSON) ,Active = ISNULL(@Active ,cS.Active) FROM system.tPipelineSchedules tPS LEFT JOIN #CurrentSchedule cS ON tPS.ScheduleKey = cS.ScheduleKey WHERE tPS.ScheduleKey = cS.ScheduleKey; SET @ResultCode = 200; SET @EffectedRows = @@ROWCOUNT; END -- Output SET @OutputID = @ScheduleID; COMMIT TRANSACTION spPOST_PipelineSchedule END TRY -- START CATCH *********************************************************************************** BEGIN CATCH DECLARE @Error_state INT = ERROR_STATE(); SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spPOST_PipelineSchedule IF @Error_state <> 10 BEGIN SET @ResultCode = 500; PRINT 'Rollback due to not executable command.'; END ELSE IF @ResultCode IS NULL OR @ResultCode/100 = 2 BEGIN SET @ResultCode = 500; END; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_PipelineSchedule' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to call demo values.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@PipelineID'; SET @value = N'ID of the Pipeline which contains this schedule.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ScheduleID'; SET @value = N'ID of the Schedule - POST * to create a new autogenerated ID.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ScheduleName'; SET @value = N'optional - name for the schedule.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ScheduleDescription'; SET @value = N'optional - description for the schedule.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ScheduleDetailsJSON'; SET @value = N'optional - definition of the schedule.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Active'; SET @value = N'optional - Flag to set schedule to active / inactive.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spPOST_PipelineStep; GO /* Procedure to create a new pipeline step. - as StepID you can pass * to create an AutoID S1..Sn Saxess Software GmbH Testcall Procedure DECLARE @RC INT ,@ReturnedStepID NVARCHAR(50); EXEC @RC = system.spPOST_PipelineStep @Username = 'SQL' ,@PipelineID = 'P1' ,@StepID = '*' ,@StepName = 'Step1' ,@StepDescription = 'Hallo' ,@OrderOfExecution = 1 ,@StepDataSourceID = '1' -- not in use, Datasource is saved in StepDetailsJSON ,@StepCompanyIDsJSON = '{}' ,@StepDetailsJSON = '{}' ,@TimeExecutionLimit = 0 ,@Active = 1 ,@ModuleID = 'FIN' ,@RunConditionCODE = 'NOERROR' ,@RestartOnError = 0 ,@OutputID = @ReturnedStepID OUTPUT; PRINT @RC; PRINT 'StepID_OUT :'+ @ReturnedStepID; SELECT * FROM system.tPipelines; SELECT * FROM system.tPipelineSteps; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_PipelineStep',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_PipelineStep','PARAMETER',NULL) */ CREATE PROCEDURE system.spPOST_PipelineStep @Username NVARCHAR(255) , @PipelineID NVARCHAR(50) , @StepID NVARCHAR(50) , @StepName NVARCHAR(255) = NULL , @StepDescription NVARCHAR(4000) = NULL , @OrderOfExecution INT = NULL , @StepDataSourceID NVARCHAR(50) = NULL , @StepCompanyIDsJSON NVARCHAR(MAX) = NULL , @StepDetailsJSON NVARCHAR(MAX) = NULL , @TimeExecutionLimit INT = NULL , @Active BIT = NULL , @ModuleID NVARCHAR(50) = NULL , @RunConditionCODE NVARCHAR(50) = NULL , @RestartOnError BIT = NULL , @OutputID NVARCHAR(50) = NULL OUTPUT AS BEGIN BEGIN TRY -- Start transaction BEGIN TRANSACTION spPOST_PipelineStep -- Logging DECLARE @TimestampCall DATETIME = GETUTCDATE(); DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID); DECLARE @AffectedRows INT = 0; DECLARE @ResultCode INT = 501; DECLARE @Comment NVARCHAR(4000) = N''; DECLARE @ParameterString NVARCHAR(MAX) = N''; DECLARE @TransactUsername NVARCHAR(255) = N''; EXEC system.spGET_ParameterString @ParameterString OUTPUT, 14, @Username, @PipelineID, @StepID, @StepName, @StepDescription, @OrderOfExecution, @StepDataSourceID, @StepCompanyIDsJSON, @StepDetailsJSON, @TimeExecutionLimit, @Active, @ModuleID, @RunConditionCODE, @RestartOnError; EXEC system.spGET_TransactUsername @TransactUsername OUTPUT, @Username; -- Variables DECLARE @PipelineKey BIGINT; DECLARE @LastStep INT -- Input parameter handling SET @PipelineID = COALESCE(system.fProtectID(@PipelineID), N''); SET @StepID = COALESCE(system.fProtectString(@StepID), N''); SET @StepName = COALESCE(system.fProtectString(@StepName), N''); SET @StepDescription = COALESCE(system.fProtectString(@StepDescription), N''); SET @ModuleID = COALESCE(system.fProtectID(@ModuleID), N''); -- Check if PipelineID is valid IF COALESCE(@PipelineID, N'') = N'' OR NOT EXISTS (SELECT PipelineID FROM system.tPipelines WHERE PipelineID = @PipelineID) EXEC system.spSEND_Message 'ERROR', 'Provided PipelineID doesn''t exist.'; -- Get PipelineKey SELECT @PipelineKey = PipelineKey FROM system.tPipelines WHERE PipelineID = @PipelineID; -- Cache existing content DROP TABLE IF EXISTS #CurrentStep; CREATE TABLE #CurrentStep ( PipelineKey BIGINT NOT NULL , StepKey BIGINT NOT NULL , StepID NVARCHAR(50) COLLATE DATABASE_DEFAULT NOT NULL , StepName NVARCHAR(255) COLLATE DATABASE_DEFAULT NOT NULL , StepDescription NVARCHAR(4000) COLLATE DATABASE_DEFAULT NOT NULL , OrderOfExecution INT NOT NULL , StepDataSourceID NVARCHAR(50) COLLATE DATABASE_DEFAULT NOT NULL , StepCompanyIDsJSON NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL , StepDetailsJSON NVARCHAR(MAX) COLLATE DATABASE_DEFAULT NOT NULL , TimeExecutionLimit INT NOT NULL , Active BIT NOT NULL , RunConditionCODE NVARCHAR(50) NOT NULL , RestartOnError BIT NOT NULL ); INSERT INTO #CurrentStep SELECT PipelineKey , StepKey , StepID , StepName , StepDescription , OrderOfExecution , StepDataSourceID , StepCompanyIDsJSON , StepDetailsJSON , TimeExecutionLimit , Active , RunConditionCODE , RestartOnError FROM system.tPipelineSteps WHERE PipelineKey = @PipelineKey AND StepID = @StepID; -- Data Transaction IF (SELECT COUNT(1) FROM #CurrentStep) = 0 BEGIN -- determine next free ID if * is sended as ID, AutoID is generated as S1..Sn per Pipeline IF @StepID = '*' BEGIN SELECT TOP 1 @LastStep = TRY_CAST(RIGHT(StepID, LEN(StepID) - 1) AS INT) FROM system.tPipelineSteps WHERE StepID LIKE 'S%' AND LEN(StepID) > 1 AND PipelineKey = @PipelineKey AND TRY_CAST(RIGHT(StepID, LEN(StepID) - 1) AS INT) IS NOT NULL ORDER BY TRY_CAST(RIGHT(StepID,LEN(StepID) - 1) AS INT) DESC IF @LastStep IS NULL SET @LastStep = 0; SET @StepID = 'S' + CAST(@LastStep + 1 AS NVARCHAR(50)); END -- INSERT with default values if NULL INSERT INTO system.tPipelineSteps ( PipelineKey , StepID , StepName , StepDescription , OrderOfExecution , StepDataSourceID , StepCompanyIDsJSON , StepDetailsJSON , TimeExecutionLimit , Active , RunConditionCODE , RestartOnError ) VALUES ( @PipelineKey , @StepID , ISNULL(@StepName, '') , ISNULL(@StepDescription, '') , ISNULL(@OrderOfExecution, 0) , ISNULL(@StepDataSourceID, '') , ISNULL(@StepCompanyIDsJSON, '') , ISNULL(@StepDetailsJSON, '') , ISNULL(@TimeExecutionLimit, 0) , ISNULL(@Active, 1) , ISNULL(@RunConditionCODE, 0) , ISNULL(@RestartOnError, 0) ); SET @AffectedRows = @@ROWCOUNT; SET @ResultCode = 201; END ELSE BEGIN -- UPDATE as Insert / delete not possible due to primary key constraints UPDATE tPS SET StepName = ISNULL(@StepName, cS.StepName) , StepDescription = ISNULL(@StepDescription, cS.StepDescription) , OrderOfExecution = ISNULL(@OrderOfExecution, cS.OrderOfExecution) , StepDataSourceID = ISNULL(@StepDataSourceID, cS.StepDataSourceID) , StepCompanyIDsJSON = ISNULL(@StepCompanyIDsJSON, cS.StepCompanyIDsJSON) , StepDetailsJSON = ISNULL(@StepDetailsJSON, cS.StepDetailsJSON) , TimeExecutionLimit = ISNULL(@TimeExecutionLimit, cS.TimeExecutionLimit) , Active = ISNULL(@Active, cS.Active) , RunConditionCODE = ISNULL(@RunConditionCODE, cS.RunConditionCODE) , RestartOnError = ISNULL(@RestartOnError, cS.RestartOnError) FROM system.tPipelineSteps AS tPS LEFT JOIN #CurrentStep AS cS ON tPS.StepKey = cS.StepKey WHERE tPS.StepKey = cS.StepKey; SET @ResultCode = 200; SET @AffectedRows = @@ROWCOUNT; END SET @OutputID = @StepID; COMMIT TRANSACTION spPOST_PipelineStep END TRY BEGIN CATCH ROLLBACK TRANSACTION spPOST_PipelineStep SET @ResultCode = 500; SET @Comment = ERROR_MESSAGE(); END CATCH; EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @AffectedRows, @ResultCode, @TimestampCall, @Comment; IF @ResultCode >= 500 EXEC system.spSEND_Message 'ERROR', @Comment; RETURN @ResultCode; END; GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_PipelineStep' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N'' -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to POST a Pipeline step, optional Parameter can be skiped, which will keep existing values.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@PipelineID'; SET @value = N'ID of the Pipeline of the step.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@StepID'; SET @value = N'ID of the Step to POST, as StepID you can pass * to create an AutoID S1..Sn'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@StepName'; SET @value = N'optional - name of the Step'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@StepDescription'; SET @value = N'optional - description of the Step'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@OrderOfExecution'; SET @value = N'optional - number for order of execution'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@StepDataSourceID'; SET @value = N'not in use, Datasource is saved in StepDetailsJSON'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@StepCompanyIDsJSON'; SET @value = N'optional - JSON Array of the Company IDs to use in this Step'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@StepDetailsJSON'; SET @value = N'Configuration of the step'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@TimeExecutionLimit'; SET @value = N'Execution Time Limit in minutes'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@Active'; SET @value = N'Flag if the step is active'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ModuleID'; SET @value = N'ID of the accociated Module, for Steps which have a module parameter.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@RunConditionCODE'; SET @value = N'The condition under which the step is executed (NOERROR = run when previous steps are successful, ERRORONLY = run when any previous step has error, ALWAYS = always execute).'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@RestartOnError'; SET @value = N'Restart step execution if error occurs.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO DROP PROCEDURE IF EXISTS system.spPOST_PipelineStepLog; GO /* Procedure to POST one Step Log - StepLog for not existing ExecutionID will be created - StepLog for existing ExcecutionID will be updated - optional Parameters posted with NULL leads to Keep what exists - Step name is materialized in the background for history reasons - Logmessage is replaced or appended together with a line break - Output Parameter not in use Saxess Software GmbH Last modified: 08/2023 for OCT 5.10 Testcall Procedure DECLARE @RC INT; EXEC @RC = system.spPOST_PipelineStepLog @Username = 'SQL' ,@PipelineID = 'P1' ,@StepID = 'S1' ,@ExecutionID = 'EID5' ,@LogTime = NULL ,@LogMessage = 'Hallogsdfgsdfg' ,@LogLevelCODE = 'ERROR'; SELECT @RC; Testcall Documentation SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_PipelineStepLog',NULL,NULL) UNION ALL SELECT * FROM ::fn_listextendedproperty (NULL, 'SCHEMA', 'system', 'PROCEDURE', 'spPOST_PipelineStepLog','PARAMETER',NULL); */ CREATE PROCEDURE system.spPOST_PipelineStepLog @Username NVARCHAR(255) , @PipelineID NVARCHAR(50) , @StepID NVARCHAR(50) , @ExecutionID NVARCHAR(50) , @LogTime DATETIME = NULL , @LogMessage NVARCHAR(MAX) , @LogLevelCODE NVARCHAR(50) AS BEGIN -- Standard declaration for logging DECLARE @ProcedureName NVARCHAR(255) = OBJECT_SCHEMA_NAME(@@PROCID) + N'.' + OBJECT_NAME(@@PROCID) ,@ParameterString NVARCHAR(MAX) = N'''' --set Strings inside in single quotes (N''','''), Numbers without strings inside without quotes (N','), end list with '''' in case of string or '' in case of number on last position + ISNULL(@Username ,N'NULL') + N''',''' + ISNULL(@PipelineID ,N'NULL') + N''',''' + ISNULL(@StepID ,N'NULL') + N''',''' + ISNULL(@ExecutionID ,N'NULL') + N''',''' + ISNULL(CAST(@LogTime AS NVARCHAR(50)) ,N'NULL') + N''',''' + ISNULL(@LogMessage ,N'NULL') + N''',''' + ISNULL(@LogLevelCODE ,N'NULL') + N'''' ,@EffectedRows INT = 0 ,@ResultCode INT = 501 ,@TimestampCall DATETIME = GETUTCDATE() ,@Comment NVARCHAR(2000) = N'' ,@TransactUsername NVARCHAR(255) = N''; -- NULL Protection for all mandatory Input parameters IF @Username IS NULL SET @Username = N''; IF @PipelineID IS NULL SET @PipelineID = N''; IF @StepID IS NULL SET @StepID = N''; IF @ExecutionID IS NULL SET @ExecutionID = N''; IF @LogTime IS NULL SET @LogTime = GETUTCDATE(); -- Content Protection for specific input parameters (e.g.IDs) SET @PipelineID = system.fProtectID(@PipelineID); SET @StepID = system.fProtectID(@StepID); SET @ExecutionID = system.fProtectID(@ExecutionID); BEGIN TRY BEGIN TRANSACTION spPOST_PipelineStepLog -- check transaction user existence SELECT @TransactUsername = system.fDetermineTransactionUsername (@Username); IF @TransactUsername = N'403' BEGIN SET @ResultCode = 403; RAISERROR('Transaction user doesn''t exist', 16, 10); END; -- check if PipelineLog entry exists IF NOT EXISTS ( SELECT 1 FROM system.tPipelineLog WHERE PipelineID = @PipelineID AND StepID = @StepID AND ExecutionID = @ExecutionID ) BEGIN SET @ResultCode = 404; RAISERROR('Log entry in system.tPipelineLog doesn''t exists', 16, 10); END; -- insert log entry INSERT INTO system.tPipelineStepLog ( PipelineID , StepID , ExecutionID , LogTime , LogMessage , LogLevelCODE ) VALUES ( @PipelineID , @StepID , @ExecutionID , @LogTime , @LogMessage , @LogLevelCODE ); SET @EffectedRows = @@ROWCOUNT; SET @ResultCode = 200; COMMIT TRANSACTION spPOST_PipelineStepLog END TRY BEGIN CATCH SET @Comment = ERROR_MESSAGE(); ROLLBACK TRANSACTION spPOST_PipelineStepLog SET @ResultCode = 500; END CATCH EXEC system.spPOST_APILogEntry @Username, @TransactUsername, @ProcedureName, @ParameterString, @EffectedRows, @ResultCode, @TimestampCall, @Comment; RETURN @ResultCode; END GO -- SET documentation variables *********************************************************************** DECLARE @level0name NVARCHAR(255) = N'system' -- enter schema name of the table ,@level1name NVARCHAR(255) = N'spPOST_PipelineStepLog' -- enter procedure name ,@SX_Owner NVARCHAR(255) = N'OCT.core' -- enter owner name of the procedure from list (OCT.core, OCT.modules, Custom) ,@SX_Module NVARCHAR(255) = N'CORE' -- enter module name as free text (CORE,FIN, DEBKRED, HR, ...) ,@SX_ShipmentFlag INT = 1 -- 0 = Demo object - out of shipment process -- STANDARD OBJECTS -- 1 = shiped from saxess standard without modification -- 2 = shiped from saxess standard modified FOR customer from saxess -- 3 = shiped from saxess standard modified FOR customer from partner -- 4 = shiped from saxess standard modified FROM customer themself for own needs -- CUSTOM OBJECTS -- 10 = shiped from saxess as customer specific object -- 11 = shiped from partner as customer specific object -- 12 = shiped from customer as own specific object ,@SX_UserHint NVARCHAR(2000) = N''; -- optional, fill if Procedure shall be offerend for end user (e.g. for Pivot / Datagrid usage) -- KEEP this default constants ************************************************************************* DECLARE @name NVARCHAR(255) = N'MS_Description' ,@level0type NVARCHAR(255) = N'SCHEMA' ,@level1type NVARCHAR(255) = N'PROCEDURE' ,@level2type NVARCHAR(255) = N'PARAMETER' ,@level2name NVARCHAR(255) = N'' ,@value NVARCHAR(1000) = N''; SET @value = @SX_Owner; EXEC sys.sp_addextendedproperty N'SX_Owner' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_Module; EXEC sys.sp_addextendedproperty N'SX_Module' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_ShipmentFlag; EXEC sys.sp_addextendedproperty N'SX_ShipmentFlag' ,@value,@level0type,@level0name,@level1type,@level1name; SET @value = @SX_UserHint; EXEC sys.sp_addextendedproperty N'SX_UserHint' ,@value,@level0type,@level0name,@level1type,@level1name; -- SET documententation ************************************************************************* -- SET Procedure documentation SET @value = N'Procedure to POST Step LogEntries'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name; -- optional SET parameter documentation (only for Core / Standardmodules) SET @level2name = N'@PipelineID'; SET @value = N'ID of the Pipeline this log belongs to.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@StepID'; SET @value = N'ID of the Step this log belongs to.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@ExecutionID'; SET @value = N'Execution - taken from the PipelineLog to keep Pipeline Log and Step Log together'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@LogTime'; SET @value = N'Timestamp of the log entry.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@LogMessage'; SET @value = N'Log Message for this specific entry. Should be short without a timestamp.'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; SET @level2name = N'@LogLevelCODE'; SET @value = N'CODE for Log level - DEBUG, INFO, WARNING, ERROR'; EXEC sys.sp_addextendedproperty @name,@value,@level0type,@level0name,@level1type,@level1name,@level2type,@level2name; GO UPDATE system.tSettings SET ValueText = '2026.05.0', ValueInt = 2026050 WHERE SettingID = 'DBVersion'