using System;
using System.Text;
using Medivators.BusinessObjects;
using Medivators.Common;
using Medivators.Common.Logging;

namespace Medivators.Data.Queries
{
	public class UpdateCycleReport : AsynchronousExecute, IUpdateQuery
	{
		private CycleReport _report = null;
		public UpdateCycleReport(CycleReport Record)
		{
			_report = Record;
		}
		public void Update()
		{
			try
			{
				// note that Key is not included!
				string columnList = "MachineID," +
									"Station," +
									"StartTimestamp," +
									"EndTimestamp," +
									"CycleCount," +
									"DisinfectantCycleCount," +
									"OperatorID," +
									"PatientID," +
									"PhysicianID," +
									"ScopeID," +
									"SSGRatio";

				string valueList = _report.MachineID.ToString() + ", " +
								   Constants.DoubleQuote + _report.Station + Constants.DoubleQuote + ", " +
								   Constants.DateDelimiter + _report.StartTimestamp.ToString() + Constants.DateDelimiter + ", " +
								   Constants.DateDelimiter + _report.EndTimestamp.ToString() + Constants.DateDelimiter + ", " +
								   _report.CycleCount.ToString() + ", " +
								   _report.DisinfectantCycleCount.ToString() + ", " +
								   _report.OperatorID.ToString() + ", " +
								   _report.PatientID.ToString() + ", " +
								   _report.PhysicianID.ToString() + ", " +
								   _report.ScopeID.ToString() + ", " +
								   Constants.DoubleQuote + _report.SSGRatio + Constants.DoubleQuote;

				string[] columns = columnList.Split(",".ToCharArray());
				string[] values = valueList.Split(",".ToCharArray());

				string sql = string.Empty;

				for (int field = 0; field < columns.Length; field++)
				{
					if (!sql.Equals(string.Empty))
						sql += ", ";
					sql += columns[field].Trim() + "=" + values[field].Trim();
				}
				sql = "update CycleReports set " +
					  sql +
					  " where Key=" +
					  Constants.DoubleQuote + _report.Key + Constants.DoubleQuote;

				base.Update(sql);
			}
			catch (Exception ex)
			{
				ErrorHandler.GetInstance().LogException("UpdateCycleReport.Insert error: ", ex);
			}
		}
	}
}
