I need to perform business logic on the historical data.
If the system user need make a historical process, the user only have to inform the date and the system will make process whith all the information that is valid for the date the user inform.
I am using the fields initial_date final_date to know by example, if the client is valid for the date the user inform
Your solution to make the transaction table, is to create a table whit the id and another with the transaction like this
Client
ID
ClientTransaction
name, initial_date, final_date
is that?
to save the data in my system i am using this logic, but i am very unhappy with the result, the system works but i think it isnt a good solution
Here is the method to save a object called LayoutColeta
public void salvarLayoutColeta()
{
int codigoLayout = 0;
IList<LayoutColeta> listaLayoutColetaAtualizado = new List<LayoutColeta>();
try
{
DataTable datatable = this.datatable.GetChanges();
//Se existir diferenças
if ( datatable != null )
{
foreach (DataRow datarow in datatable.Rows)
{
switch (datarow.RowState)
{
case DataRowState.Added:
listaLayoutColetaAtualizado.Add(new LayoutColeta(0,
DateTime.Now,
DateTime.MaxValue,
(string)datarow["NomeLayout"],
(int)datarow["Posicao"],
"teste",
null,
null));
break;
case DataRowState.Modified:
codigoLayout = ((LayoutColeta.LayoutColetaKey)datarow["LayoutColetaKey"]).CodigoLayout;
IList<Caracteristica> teste = new List<Caracteristica>();
foreach (Caracteristica caracteristica in (IList<Caracteristica>)datarow["Caracteristica"])
{
teste.Add((Caracteristica)caracteristica.cloneBySerialization());
}
//(IList<Bem>)datarow["Bem"]
listaLayoutColetaAtualizado.Add(new LayoutColeta(((LayoutColeta.LayoutColetaKey)datarow["LayoutColetaKey"]).CodigoLayout,
DateTime.Now,
DateTime.MaxValue,
(string)datarow["NomeLayout"],
(int)datarow["Posicao"],
"teste",
teste,
null));
foreach (LayoutColeta layoutColeta in this.layoutsColetaBase)
{
if (codigoLayout == layoutColeta.LayoutColetakey.CodigoLayout)
{
layoutColeta.DataFim = DateTime.Now;
}
}
break;
case DataRowState.Deleted:
codigoLayout = ((LayoutColeta.LayoutColetaKey)datarow["LayoutColetaKey", DataRowVersion.Original]).CodigoLayout;
foreach (LayoutColeta layoutColeta in this.layoutsColetaBase)
{
if (codigoLayout == layoutColeta.LayoutColetakey.CodigoLayout)
{
layoutColeta.DataFim = DateTime.Now;
}
}
break;
}
}
layoutColetaDao.salvarLayoutColeta(listaLayoutColetaAtualizado);
this.setupViewFromModel();
layoutColetaView.showMensagemSucesso("LayoutColetaSucessoAtualizar");
}
}
catch (InvalidCastException)
{
layoutColetaView.showMensagemErro("LayoutColetaPosicaoNull");
}
catch (LayoutColetaException exception)
{
layoutColetaView.showMensagemErro(exception.Message);
log.Error(Mensagem.retornaMensagem(exception.Message), exception);
}
catch (Exception exception)
{
layoutColetaView.showMensagemErro("LayoutColetaErroAtualizar");
log.Error(Mensagem.retornaMensagem("LayoutColetaErroAtualizar"), exception);
}
}
here is the dao class
public void salvarLayoutColeta(IList<LayoutColeta> layoutsColeta)
{
ITransaction transaction = null;
CaracteristicaDao caracteristicaDao = new CaracteristicaDao();
try
{
transaction = session.BeginTransaction();
foreach (LayoutColeta layoutColeta in layoutsColeta)
{
//Se nao tiver um codigo, cria um codigo a partir da base de dados
if (layoutColeta.LayoutColetakey.CodigoLayout == 0)
{
//Verifica se o nome do layout coleta ja existe
Object quantidade = 0;
quantidade = session.CreateQuery("select count(*) from LayoutColeta as layoutColeta where layoutColeta.NomeLayout = :nomeLayoutColeta").SetParameter("nomeLayoutColeta", layoutColeta.NomeLayout).UniqueResult();
if (Convert.ToInt32(quantidade) > 0)
{
throw new LayoutColetaException("LayoutColetaNomeJaExiste");
}
//Seleciona o maior valor de codigo de processo no banco e soma um para criar o codigo do processo a ser salvo
object maxCodigo = session.CreateQuery("select max(layoutColeta.LayoutColetakey.CodigoLayout) from LayoutColeta as layoutColeta").UniqueResult();
if (maxCodigo == null)
{
layoutColeta.LayoutColetakey.CodigoLayout = 1;
}
else
{
layoutColeta.LayoutColetakey.CodigoLayout = (int)maxCodigo;
layoutColeta.LayoutColetakey.CodigoLayout++;
}
}
session.Save(layoutColeta);
if (layoutColeta.Caracteristica != null)
{
foreach (Caracteristica caracteristica in layoutColeta.Caracteristica)
{
caracteristica.LayoutColeta.LayoutColetakey.DataInicial = layoutColeta.LayoutColetakey.DataInicial;
caracteristicaDao.atualizaCaracteristicaAtual(caracteristica);
caracteristica.DataFinal = layoutColeta.LayoutColetakey.DataInicial;
}
}
}
session.Flush();
transaction.Commit();
}
catch (Exception exception)
{
transaction.Rollback();
log.Error(exception);
throw;
}
}
because Layout Coleta have a relation whith Caracteristica, i need to save a new caracteristica with the new initial_date of layoutColeta to maintain the consistency of the database
that´s my problem, i am looking for a better solution then if you save object A that is relationed to object B, i need to save object B again to update the FK between the two objects, since my pk is the id of the object plus the initial date
thank in advance
|