CEL_Payroll/Payroll.Service/Mail/Service/MailSenderErrorListService.cs
2024-09-17 14:30:13 +06:00

377 lines
14 KiB
C#

using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using Ease.CoreV35;
using Ease.CoreV35.Model;
using Ease.CoreV35.DataAccess;
using System.Collections.Generic;
using Payroll.BO;
using Ease.CoreV35.Caching;
namespace Payroll.Service
{
#region MailSenderErrorList Service
[Serializable]
public class MailSenderErrorListService : ServiceTemplate, IMailSenderErrorListService
{
#region Private functions and declaration
Cache _cache = new Cache(typeof(MailSenderErrorList));
#endregion
public MailSenderErrorListService() { }
private void MapObject(MailSenderErrorList oMailSenderErrorList, DataReader oReader)
{
base.SetObjectID(oMailSenderErrorList, oReader.GetID("MailSenderErrorListID"));
oMailSenderErrorList.BatchNo =oReader.GetID("BatchNo");
oMailSenderErrorList.ToEmpID =oReader.GetID("ToEmpID");
oMailSenderErrorList.ReceiveTime = oReader.GetDateTime("ReceiveTime").Value;
oMailSenderErrorList.SendTime = oReader.GetDateTime("SendTime").Value;
oMailSenderErrorList.IsSuccessfull = oReader.GetBoolean("IsSuccessfull").Value;
oMailSenderErrorList.Reason = oReader.GetString("Reason");
oMailSenderErrorList.From = oReader.GetString("From");
oMailSenderErrorList.To = oReader.GetString("To");
oMailSenderErrorList.CC = oReader.GetString("CC");
oMailSenderErrorList.Subject = oReader.GetString("Subject");
oMailSenderErrorList.Body = null;
oMailSenderErrorList.Attachments = oReader.GetString("Attachments");
this.SetObjectState(oMailSenderErrorList,Ease.CoreV35.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
MailSenderErrorList oMailSenderErrorList = new MailSenderErrorList();
MapObject(oMailSenderErrorList, oReader);
return oMailSenderErrorList as T;
}
protected MailSenderErrorList CreateObject(DataReader oReader)
{
MailSenderErrorList oMailSenderErrorList = new MailSenderErrorList();
MapObject(oMailSenderErrorList, oReader);
return oMailSenderErrorList;
}
#region Service implementation
public MailSenderErrorList Get(ID id)
{
MailSenderErrorList oMailSenderErrorList = new MailSenderErrorList();
#region Cache Header
oMailSenderErrorList = _cache["Get", id] as MailSenderErrorList;
if (oMailSenderErrorList != null)
return oMailSenderErrorList;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(MailSenderErrorListDA.Get(tc, id));
if (oreader.Read())
{
oMailSenderErrorList = this.CreateObject<MailSenderErrorList>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
#region Cache Footer
_cache.Add(oMailSenderErrorList, "Get", id);
#endregion
return oMailSenderErrorList;
}
public ObjectsTemplate<MailSenderErrorList> Get()
{
#region Cache Header
ObjectsTemplate<MailSenderErrorList> MailSenderErrorLists = _cache["Get"] as ObjectsTemplate<MailSenderErrorList>;
if (MailSenderErrorLists != null)
return MailSenderErrorLists;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(MailSenderErrorListDA.Get(tc));
MailSenderErrorLists = this.CreateObjects<MailSenderErrorList>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
#region Cache Footer
_cache.Add(MailSenderErrorLists, "Get");
#endregion
return MailSenderErrorLists;
}
public ID Save(MailSenderErrorList oMailSenderErrorList, byte[] body)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
if (oMailSenderErrorList.IsNew)
{
int id = tc.GenerateID("MailSenderErrorList", "MailSenderErrorListID");
base.SetObjectID(oMailSenderErrorList, ID.FromInteger(id));
MailSenderErrorListDA.Insert(tc, oMailSenderErrorList);
string ConnString = tc.Connection.ConnectionString;
SqlConnection connection = new SqlConnection(ConnString);
tc.End();
MailSenderErrorListDA.UpdateBody(connection, oMailSenderErrorList.ID, body);
connection.Close();
}
else
{
MailSenderErrorListDA.Update(tc, oMailSenderErrorList);
string ConnString = tc.Connection.ConnectionString;
SqlConnection connection = new SqlConnection(ConnString);
tc.End();
MailSenderErrorListDA.UpdateBody(connection, oMailSenderErrorList.ID, body);
connection.Close();
}
return oMailSenderErrorList.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void Delete(ID id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
MailSenderErrorListDA.Delete(tc, id);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public ID GetNewBatchNo()
{
TransactionContext tc = null;
int id;
try
{
tc = TransactionContext.Begin();
id = tc.GenerateID("MailSenderErrorList", "BatchNo");
}
catch (Exception exception)
{
throw exception;
}
return ID.FromInteger(id);
}
public DataTable GetBatchNoWiseErrorList()
{
DataTable dataTable=new DataTable();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
dataTable = MailSenderErrorListDA.GetBatchNoWiseErrorList(tc);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return dataTable;
}
public ObjectsTemplate<MailSenderErrorList> GetErrorListByBatchNo(int batchNo)
{
#region Cache Header
ObjectsTemplate<MailSenderErrorList> MailSenderErrorLists = _cache["Get"] as ObjectsTemplate<MailSenderErrorList>;
if (MailSenderErrorLists != null)
return MailSenderErrorLists;
#endregion
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(MailSenderErrorListDA.GetByBatch(tc,batchNo));
MailSenderErrorLists = this.CreateObjects<MailSenderErrorList>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
#region Cache Footer
_cache.Add(MailSenderErrorLists, "Get");
#endregion
return MailSenderErrorLists;
}
public MailSenderErrorList GetWithBody(ID id)
{
MailSenderErrorList oMailSenderErrorList;
FileStream stream = null;
SqlDataReader DataReader = null;
// Writes the BLOB to a file (*.bmp).
BinaryWriter writer; // Streams the BLOB to the FileStream object.
int bufferSize = 100; // Size of the BLOB buffer.
byte[] outByte = new byte[bufferSize];// The BLOB byte[] buffer to be filled by GetBytes.
long retval; // The bytes returned from GetBytes.
long startIndex = 0; // The starting position in the BLOB output.
string pubID = ""; // The publisher id to use in the file name.
#region CacheHeader
oMailSenderErrorList = (MailSenderErrorList)_cache["Get", id];
if (oMailSenderErrorList != null)
return oMailSenderErrorList;
#endregion
TransactionContext tc = null;
try
{
#region Retrieving data
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(MailSenderErrorListDA.Get(tc, id));
if (oreader.Read())
{
oMailSenderErrorList = CreateObject(oreader);
}
oreader.Close();
string sConnectionString = tc.Connection.ConnectionString;
SqlConnection connection = new SqlConnection(sConnectionString);
connection.Open();
DataReader = MailSenderErrorListDA.GetBody(connection, oMailSenderErrorList.ID.Integer);
while (DataReader.Read())
{
// Get the publisher id, which must occur before getting the logo.
// pubID = reader.GetString(1);
// Create a file to hold the output.
stream = new FileStream("Test" + pubID + ".rtf", FileMode.OpenOrCreate, FileAccess.Write);
writer = new BinaryWriter(stream);
startIndex = 0; // Reset the starting byte for the new BLOB.
retval = DataReader.GetBytes(0, startIndex, outByte, 0, bufferSize); // Read bytes into outByte[] and retain the number of bytes returned.
while (retval == bufferSize) // Continue while there are bytes beyond the size of the buffer.
{
writer.Write(outByte);
writer.Flush();
startIndex += bufferSize; // Reposition start index to end of last buffer and fill buffer.
retval = DataReader.GetBytes(0, startIndex, outByte, 0, bufferSize);
}
if (retval != 0)
writer.Write(outByte, 0, (int)retval - 1); // Write the remaining buffer.
writer.Flush();
writer.Close(); // Close the output file.
}
oMailSenderErrorList.Body = stream;
connection.Close();
stream.Close();
DataReader.Close();
// Close the reader and the connection.
#endregion
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
throw new ServiceException(e.Message, e);
#endregion
}
return oMailSenderErrorList;
}
public void UpdateSendStatus(int nID, bool bStatus)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
MailSenderErrorListDA.UpdateSendStatus(tc, nID, bStatus);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
#endregion
}
#endregion
}