s-RootCOM-vb

'****************************************************************************
' (c) Copyright IBM Corp. 2007 All rights reserved.
'
' The following sample of source code ("Sample") is owned by International
' Business Machines Corporation or one of its subsidiaries ("IBM") and is
' copyrighted and licensed, not sold. You may use, copy, modify, and
' distribute the Sample in any form without payment to IBM, for the purpose of
' assisting you in the development of your applications.
'
' The Sample code is provided to you on an "AS IS" basis, without warranty of
' any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR
' IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do
' not allow for the exclusion or limitation of implied warranties, so the above
' limitations or exclusions may not apply to you. IBM shall not be liable for
' any damages you suffer as a result of using, copying, modifying or
' distributing the Sample, even if IBM has been advised of the possibility of
' such damages.
'****************************************************************************
'
' SOURCE FILE NAME: RootCOM.vb
'
' SAMPLE: Creates a library assembly RootCOM.dll for LCTrans.vb 
' LCTrans.vb refers to the classes and methods defined in this file.
'
' SQL Statements USED:
'         UPDATE
'
' DB2 .NET Data Provider Classes USED:
'         DB2Connection
'         DB2Command
'         
'****************************************************************************
'
' Creating the library assembly RootCOM.dll
'
' 1. Create a new key pair and store it in TransCOM.snk by entering the 
'    following at the command prompt:
'
'      sn -k RootCOM.snk
'
' 2. Compile the RootCOM.vb file with bldapp.bat by entering the following 
'    at the command prompt:
'
'      bldapp RootCOM
'
'****************************************************************************
'
' For more information on the sample programs, see the README file.
'
' For information on developing applications, see the Application
' Development Guide.
'
' For information on using SQL statements, see the SQL Reference.
'
' For the latest information on programming, compiling, and running DB2
' applications, visit the DB2 Information Center at
'     http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
'
'****************************************************************************

Imports System.EnterpriseServices
Imports System
Imports System.IO
Imports System.Data
Imports IBM.Data.DB2
Imports Microsoft.VisualBasic
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports SubCOM

' Set the assembly attributes
' Review the values of the assembly attributes

<Assembly: AssemblyTitle("")> 
<Assembly: AssemblyDescription("")> 
<Assembly: AssemblyCompany("")> 
<Assembly: AssemblyProduct("")> 
<Assembly: AssemblyCopyright("")> 
<Assembly: AssemblyTrademark("")> 
<Assembly: CLSCompliant(True)> 
<Assembly: Guid("F0761CFA-935D-44F2-9F79-9066EF2FA151")> 
<Assembly: AssemblyVersion("1.0.*")> 
<Assembly: AssemblyKeyFile("RootCOM.snk")> 

Namespace RootCOM

    <Transaction(TransactionOption.Required)> _
        Public Class UpdateEmpInfoCOM
        Inherits ServicedComponent

        Public Function UpdateEmpInfo(ByVal IntEmpID As Integer, ByVal DecIncAmount As Decimal, ByVal StrEmpJob As String, ByVal StrUID As String, ByVal StrPWD As String) As Integer
            ' Update Job Title in this Root component
            UpdateEmpInfo = UpdateJob(IntEmpID, DecIncAmount, StrEmpJob, StrUID, StrPWD)
            Select Case UpdateEmpInfo
                Case -1
                    'UpdateJob() failed, abort the transaction
                    ContextUtil.SetAbort()
                    Console.WriteLine("    RootCOM: Transaction Aborted.")
                Case 0
                    'UpdateJob() succeeded, invoke the sub component to update the salary
                    Dim comSubCOM As New UpdateSalaryCOM()

                    UpdateEmpInfo = comSubCOM.UpdateEmpSalary(IntEmpID, DecIncAmount, StrUID, StrPWD)
                    Select Case UpdateEmpInfo
                        Case -1
                            'UpdateSalary() failed in the sub component, abort the transaction, which rollback the operation in UpdateJob()
                            ContextUtil.SetAbort()
                            Console.WriteLine("    RootCOM: Transaction Aborted.")
                        Case 0
                            'Both UpdateJob() and UpdateSalary() succeeded, commit the transaction
                            ContextUtil.SetComplete()
                            Console.WriteLine("    RootCOM: Transaction Committed.")
                    End Select
                    comSubCOM = Nothing
            End Select

            Return UpdateEmpInfo

        End Function



        ' This method updates the job title of employee id = empID in the
        ' STAFF table of the SAMPLE database

        Private Function UpdateJob(ByVal IntEmpID As Integer, ByVal DecIncAmount As Decimal, ByVal StrEmpJob As String, ByVal StrUID As String, ByVal StrPWD As String) As Integer

            Dim objCommand As DB2Command = New DB2Command()
            Dim objConnection As DB2Connection

            If StrUID Is Nothing Then
                objConnection = New DB2Connection("database=sample;enlist=false")
            Else
                objConnection = New DB2Connection("database=sample;UID=" & StrUID & _
                                                  ";PWD=" & StrPWD & ";enlist=false")
            End If

            ' Connect to the SAMPLE database using the entered user id and password 
            Try
                objConnection.Open()
                'For convenience, we display the message to the console. 
                'In a production system, you should write the info to a log file.
                Console.WriteLine(vbNewLine & "    RootCOM: Connected to the SAMPLE database")
            Catch E As Exception
                Console.WriteLine()
                Console.WriteLine("    RootCOM: Failed to open connection to the SAMPLE database!")
                Console.WriteLine(E.ToString())
                Console.WriteLine()
                Return -1
            End Try

            ' Update the job title of the the employee
            Try
                Console.WriteLine("    RootCOM: UPDATE STAFF SET JOB = '" & StrEmpJob & "' WHERE ID = " & IntEmpID)

                objCommand.CommandText = "update STAFF " & _
                                         "  set JOB = '" & StrEmpJob & _
                                         "'  where ID = " & IntEmpID
                objCommand.Connection = objConnection

                ' Check if the job was updated
                If objCommand.ExecuteNonQuery() > 0 Then
                    Console.WriteLine("    RootCOM: JOB UPDATED SUCCESSFULLY")
                    Return 0
                Else
                    Throw (New Exception("ExecuteNonQuery() returned error. "))
                End If
            Catch E As Exception
                Console.WriteLine("    RootCOM: FAILED TO UPDATE JOB TITLE")
                Console.WriteLine(E.ToString())
                Return -1
            Finally
                objConnection.Close()
                Console.WriteLine("    RootCOM: Connection Closed.")
            End Try

        End Function

    End Class

End Namespace