Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Database
Imports DigitalData.Services.EDMIService.IDB
Imports System.Data.SqlClient

Namespace Methods.IDB.SetAttributeValue
    Public Class SetAttributeValueMethod
        Inherits BaseMethod

        Private Connection As SqlConnection
        Private Transaction As SqlTransaction

        Public Sub New(pLogConfig As LogConfig, pDatabaseIDB As MSSQLServer, pDatabaseECM As MSSQLServer, pGlobalState As GlobalState)
            MyBase.New(pLogConfig, pDatabaseIDB, pDatabaseECM, pGlobalState)

            Connection = DatabaseIDB.GetConnection()
            Transaction = Connection.BeginTransaction()
        End Sub

        Public Function Run(pData As SetAttributeValueRequest) As SetAttributeValueResponse
            Try
                If Helpers.TestObjectIdExists(pData.ObjectId) = False Then
                    LogAndThrow("ObjectId does not exist!")
                End If

                Dim oResult = Helpers.SetAttributeValue(Connection, Transaction,
                                                        pData.ObjectId, pData.AttributeName, pData.AttributeValue, pData.Language, pData.Who)

                If oResult = False Then
                    LogAndThrow("Attribute value could not be set!")
                End If

                Return New SetAttributeValueResponse(pData.ObjectId)
            Catch ex As Exception
                Logger.Warn("Error occurred while setting attribute value!")
                Return New SetAttributeValueResponse(ex)
            End Try
        End Function
    End Class

End Namespace