//****************************************************************************
// (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.cs
//
// SAMPLE: Creates a library assembly RootCOM.dll for LCTrans.cs 
// LCTrans.cs 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 RootCOM.snk by entering the 
//    following at the command prompt:
//
//      sn -k RootCOM.snk
//
// 2. Compile the RootCOM.cs 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
//
//****************************************************************************

using  System.EnterpriseServices;
using System;
using System.IO;
using System.Data;
using IBM.Data.DB2;
using Microsoft.VisualBasic;
using System.Reflection;
using System.Runtime.InteropServices;
using 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("AFF7B07A-75B7-4b26-9D90-56E42940FF7A")]
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyKeyFile("RootCOM.snk")]

namespace RootCOM 
{
	[Transaction(TransactionOption.Required)]
	public class UpdateEmpInfoCOM:ServicedComponent
	{

		public int UpdateEmpInfo(int IntEmpID, Decimal DecIncAmount, String StrEmpJob, String StrUID, String StrPWD)
		{
			int intReturnCode;

			// Update Job Title in this Root component
			intReturnCode = UpdateJob(IntEmpID, DecIncAmount, StrEmpJob, StrUID, StrPWD);
			switch (intReturnCode)
			{
				case -1:
					//UpdateJob() failed, abort the transaction
					ContextUtil.SetAbort();
					Console.WriteLine("    RootCOM: Transaction Aborted.");
					break;
				case 0:
					//UpdateJob() succeeded, invoke the sub component to update the salary
					UpdateSalaryCOM comSubCOM = new UpdateSalaryCOM();
					intReturnCode = comSubCOM.UpdateEmpSalary(IntEmpID, DecIncAmount, StrUID, StrPWD);
				switch (intReturnCode)
				{
					case -1:
						//UpdateSalary() failed in the sub component, abort the transaction, which rollback the operation in UpdateJob()
						ContextUtil.SetAbort();
						Console.WriteLine("    RootCOM: Transaction Aborted.");
						break;
					case 0:
						//Both UpdateJob() and UpdateSalary() succeeded, commit the transaction
						ContextUtil.SetComplete();
						Console.WriteLine("    RootCOM: Transaction Committed.");
						break;
				}
					break;
			}

			return intReturnCode;
		}

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

		private int UpdateJob(int IntEmpID, Decimal DecIncAmount, String StrEmpJob, String StrUID, String StrPWD)
		{
			DB2Command objCommand = new DB2Command();
			DB2Connection objConnection;

			if (StrUID == "") 
			{
				objConnection = new DB2Connection("database=sample;enlist=false");
			}
			else
			{
				objConnection = new DB2Connection("database=sample;UID=" + 
                                                                  StrUID + ";PWD=" + StrPWD + 
                                                                  ";enlist=false");
			}

			// 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("\n    RootCOM: Connected to the SAMPLE database");
			}
			catch (Exception E)
			{
				Console.WriteLine();
				Console.WriteLine("    RootCOM: Failed to open connection to the SAMPLE database!");
				Console.WriteLine(E.ToString());
				Console.WriteLine();
				return -1;
			}

			// 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) 
				{
					Console.WriteLine("    RootCOM: JOB UPDATED SUCCESSFULLY");
					return 0;
				}
				else
				{
					throw (new Exception("ExecuteNonQuery() returned error. "));
				}
			}
			catch (Exception E)
			{
				Console.WriteLine("    RootCOM: FAILED TO UPDATE JOB TITLE");
				Console.WriteLine(E.ToString());
				return -1;
			}
			finally
			{
				objConnection.Close();
				Console.WriteLine("    RootCOM: Connection Closed.");
			}
		}
	}
}