/// <summary> /// If old value exists in transData, then revert to it otherwise delete the employee payitems. /// </summary> public void UndoPayItems() { using (SsepsIISynEntities ents = new SsepsIISynEntities()) { foreach (TransData table in Tables.Where(x => x.TableName == Constant.EMPLOYEE_PAYITEM_TABLE)) { switch (table.ActionType) { case Constant.TRANSDATA_ACTION_TYPE_INSERT: //Remove all those inserted XmlAttributeCollection attributes = table.GetNewAttributesFromXml(); int payItemId = int.Parse(attributes.GetNamedItem("payItemID").Value); EmployeePayItem payItem = ents.EmployeePayItems.Single(x => x.EmployeeID == EmployeeId && x.payItemID == payItemId); ents.EmployeePayItems.Remove(payItem); break; case Constant.TRANSDATA_ACTION_TYPE_DELETE: //Restore the deleted ones XmlAttributeCollection attributesOld = table.GetOldAttributesFromXml(); EmployeePayItem payItemOld = new EmployeePayItem(); payItemOld.EmployeeID = EmployeeId; payItemOld.payItemID = int.Parse(attributesOld.GetNamedItem("payItemID").Value); if (attributesOld.GetNamedItem("startPeriod") != null) payItemOld.startPeriod = int.Parse(attributesOld.GetNamedItem("startPeriod").Value); if (attributesOld.GetNamedItem("endPeriod") != null) payItemOld.endPeriod = int.Parse(attributesOld.GetNamedItem("endPeriod").Value); if (attributesOld.GetNamedItem("paramValue") != null) payItemOld.paramValue = double.Parse(attributesOld.GetNamedItem("paramValue").Value); payItemOld.dateCreated = DateTime.Parse(attributesOld.GetNamedItem("dateCreated").Value); if (attributesOld.GetNamedItem("dateUpdated") != null) payItemOld.dateUpdated = DateTime.Parse(attributesOld.GetNamedItem("dateUpdated").Value); payItemOld.whoCreated = attributesOld.GetNamedItem("whoCreated").Value; if (attributesOld.GetNamedItem("whoUpdated") != null) payItemOld.whoUpdated = attributesOld.GetNamedItem("whoUpdated").Value; if (attributesOld.GetNamedItem("LogRefID") != null) payItemOld.LogRefID = attributesOld.GetNamedItem("LogRefID").Value; ents.EmployeePayItems.Add(payItemOld); break; } ents.Database.ExecuteSqlCommand("DISABLE TRIGGER dbo.EmployeePayItemEventLog ON dbo.EmployeePayItem"); ents.SaveChanges(); ents.Database.ExecuteSqlCommand("ENABLE TRIGGER dbo.EmployeePayItemEventLog ON dbo.EmployeePayItem"); } } }
public void InsertPayItems() { using (SsepsIISynEntities ents = new SsepsIISynEntities()) { foreach (TransData table in Tables.Where(x => x.TableName == Constant.EMPLOYEE_PAYITEM_TABLE && x.ActionType == Constant.TRANSDATA_ACTION_TYPE_DELETE)) { XmlAttributeCollection attributes = table.GetOldAttributesFromXml(); ents.Database.ExecuteSqlCommand("DISABLE TRIGGER dbo.EmployeePayItemEventLog ON dbo.EmployeePayItem"); ents.Database.ExecuteSqlCommand(string.Format("DELETE FROM dbo.EmployeePayItem WHERE EmployeeID = {0} AND payItemID = {1}", EmployeeId, int.Parse(attributes.GetNamedItem("payItemID").Value))); ents.Database.ExecuteSqlCommand("ENABLE TRIGGER dbo.EmployeePayItemEventLog ON dbo.EmployeePayItem"); } foreach (TransData table in Tables.Where(x => x.TableName == Constant.EMPLOYEE_PAYITEM_TABLE && x.ActionType != Constant.TRANSDATA_ACTION_TYPE_DELETE)) { XmlAttributeCollection attributes = table.GetNewAttributesFromXml(); EmployeePayItem payItem = new EmployeePayItem(); payItem.EmployeeID = EmployeeId; payItem.payItemID = int.Parse(attributes.GetNamedItem("payItemID").Value); if (attributes.GetNamedItem("startPeriod") != null) payItem.startPeriod = int.Parse(attributes.GetNamedItem("startPeriod").Value); if (attributes.GetNamedItem("endPeriod") != null) payItem.endPeriod = int.Parse(attributes.GetNamedItem("endPeriod").Value); if (attributes.GetNamedItem("paramValue") != null) payItem.paramValue = double.Parse(attributes.GetNamedItem("paramValue").Value); payItem.dateCreated = DateTime.Parse(attributes.GetNamedItem("dateCreated").Value); if (attributes.GetNamedItem("dateUpdated") != null) payItem.dateUpdated = DateTime.Parse(attributes.GetNamedItem("dateUpdated").Value); payItem.whoCreated = attributes.GetNamedItem("whoCreated").Value; if (attributes.GetNamedItem("whoUpdated") != null) payItem.whoUpdated = attributes.GetNamedItem("whoUpdated").Value; if (attributes.GetNamedItem("LogRefID") != null) payItem.LogRefID = attributes.GetNamedItem("LogRefID").Value; ents.Database.ExecuteSqlCommand("DISABLE TRIGGER dbo.EmployeePayItemEventLog ON dbo.EmployeePayItem"); ents.EmployeePayItems.Add(payItem); ents.SaveChanges(); ents.Database.ExecuteSqlCommand("ENABLE TRIGGER dbo.EmployeePayItemEventLog ON dbo.EmployeePayItem"); } } }