//****************************************************************************
// (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: SubCOM.cs
//
// SAMPLE: Creates a library assembly SubCOM.dll for LCTrans.cs
// LCTrans refers to the classes and methods defined in this file.
//
// SQL Statements USED:
//         SELECT
//         UPDATE
//
// DB2 .NET Data Provider Classes USED:
//         DB2Connection
//         DB2Command
//         
///****************************************************************************
//
// Creating the library assembly SubCOM.dll
//
// 1. Create a new key pair and store it in SubCOM.snk by entering the 
//    following at the command prompt:
//
//      sn -k SubCOM.snk
//
// 2. Compile the SubCOM.cs file with bldapp.bat by entering the following 
//    at the command prompt:
//
//      bldapp SubCOM
//
//****************************************************************************
//
// 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;

// 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("2F30F1C4-636A-4cdc-971A-98DBBA546F29")]
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyKeyFile("SubCOM.snk")]

namespace SubCOM 
{
	[Transaction(TransactionOption.Required)]
	public class UpdateSalaryCOM:ServicedComponent
	{
		public int UpdateEmpSalary(int IntEmpID, Decimal DecIncrease, String StrUID, String StrPWD)
		{
			int intReturnCode;

			// Update salary in this Sub component
			intReturnCode = UpdateSalary(IntEmpID, DecIncrease, StrUID, StrPWD);
			switch (intReturnCode)
			{
				case -1:
					//UpdateSalary() failed, abort the operation
					ContextUtil.SetAbort();
					Console.WriteLine("        SubCOM: Transaction Aborted.");
					Console.WriteLine();
					break;
				case 0:
					//UpdateSalary() succeeded, commit the operation
					ContextUtil.SetComplete();
					Console.WriteLine("        SubCOM: Transaction Committed.");
					Console.WriteLine();
					break;
			}
			
			return intReturnCode;

		}


		// This method updates the salary of employee id = empID in the
		// STAFF table of the SAMPLE database
		private int UpdateSalary(int IntEmpID, Decimal DecIncrease, String StrUID, String StrPWD)
		{
			String strSELECT;
			String strUPDATE;
			int intEmpYears;
			Decimal decEmpSalary;
			DB2Connection objConnection;
			DB2DataAdapter objAdapter;
			DataSet objDataset;

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

			strSELECT = "select SALARY, YEARS from STAFF where ID = " + IntEmpID;
			strUPDATE = "update STAFF set SALARY = ? where ID = " + IntEmpID;

			try
			{

				objAdapter = new DB2DataAdapter(strSELECT, objConnection);
				objAdapter.UpdateCommand = new DB2Command(strUPDATE, objConnection);
				objAdapter.UpdateCommand.Parameters.Add(new DB2Parameter("SALARY", DB2Type.Decimal, 8, ParameterDirection.Input, false, 0, 0, "SALARY", DataRowVersion.Current, 0));

				objDataset = new DataSet();
				objAdapter.Fill(objDataset);

				intEmpYears = Convert.ToInt32(objDataset.Tables[0].Rows[0]["YEARS"]);
				decEmpSalary = Convert.ToDecimal(objDataset.Tables[0].Rows[0]["SALARY"]) + DecIncrease;
				objDataset.Tables[0].Rows[0]["SALARY"] = decEmpSalary;

				if ((decEmpSalary > 20000) && (intEmpYears < 5) )
				{
					throw (new Exception("Salary too large for a new employee." +
						" An employee for less than 5 years" +
						" should not have a salary greater than" +
						" 20000."));
				}

				Console.WriteLine("        SubCOM: UPDATE STAFF SET SALARY = " + Convert.ToString(decEmpSalary) + " WHERE ID = " + Convert.ToString(IntEmpID));
				objAdapter.Update(objDataset);
				Console.WriteLine("        SubCOM: SALARY UPDATED SUCCESSFULLY");
				return 0;
			}

			catch (Exception E)
			{
				Console.WriteLine("        SubCOM: FAILED TO UPDATE SALARY!");
				Console.WriteLine(E.ToString());
				return -1;
			}
			finally 
			{
				objConnection.Close();
				Console.WriteLine("        SubCOM: Connection Closed.");
			}
		}
	}
}