/*
Copyright (c) 2006, Marc Clifton
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name Marc Clifton nor the names of contributors may be
used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// DataTable.PrimaryKey
using System;
using System.Collections.Generic;
using System.Data;
using Clifton.Tools.Data;
namespace Clifton.Data
{
public class DataTableSynchronizationManagerException : ApplicationException
{
public DataTableSynchronizationManagerException(string msg)
: base(msg)
{
}
}
public class DataTableSynchronizationManager
{
protected DataTableTransactionLog logger;
/// <summary>
/// The first unsync'd transaction in the logger's transaction log.
/// </summary>
protected int firstSyncTransaction;
/// <summary>
/// Synchronization state. Set to false after the first set of transactions is applied.
/// Set to true on construction and after synchronization.
/// </summary>
protected bool syncd;
public DataTableTransactionLog Logger
{
get { return logger; }
set { logger = value; }
}
public DataTableSynchronizationManager()
{
syncd = true;
}
public DataTableSynchronizationManager(DataTableTransactionLog logger)
{
this.logger = logger;
syncd = true;
}
public List<TransactionRecordPacket> GetTransactions()
{
if (logger.SourceTable.PrimaryKey == null)
{
throw new DataTableTransactionException("GetTransactions requires at least one PK.");
}
List<TransactionRecordPacket> packets = new List<TransactionRecordPacket>();
foreach (DataTableTransactionRecord record in logger.Log)
{
TransactionRecordPacket trp = new TransactionRecordPacket(record);
packets.Add(trp);
}
return packets;
}
// TODO: These column names might collide with PK column names!
public static DataTable GetEmptyTransactionTable()
{
DataTable dt = new DataTable("Transactions");
dt.Columns.Add(new DataColumn("TransType", typeof(Int32)));
dt.Columns.Add(new DataColumn("ColumnName", typeof(string)));
dt.Columns.Add(new DataColumn("ValueType", typeof(string)));
dt.Columns.Add(new DataColumn("NewValue", typeof(string)));
dt.Columns.Add(new DataColumn("OldValue", typeof(string)));
dt.Columns["ValueType"].AllowDBNull = true;
dt.Columns["NewValue"].AllowDBNull = true;
dt.Columns["OldValue"].AllowDBNull = true;
return dt;
}
public virtual DataTable GetTransactionsAsDataTable()
{
DataTable dt = InitializeTransactionTableColumns();
lock (logger.Log)
{
foreach (DataTableTransactionRecord record in logger.Log)
{
DataRow dr = InitializeTransactionRow(dt, record);
dt.Rows.Add(dr);
}
}
return dt;
}
public List<TransactionRecordPacket> GetTransactions(Dictionary<string, object> pkValues)
{
if (logger.SourceTable.PrimaryKey == null)
{
throw new DataTableTransactionException("GetTransactions requires at least one PK.");
}
if (pkValues == null)
{
throw new ArgumentNullException("pkValues cannot be null.");
}
List<TransactionRecordPacket> packets = new List<TransactionRecordPacket>();
foreach (DataTableTransactionRecord record in logger.Log)
{
TransactionRecordPacket trp = new TransactionRecordPacket(record, pkValues);
packets.Add(trp);
}
return packets;
}
public void AddTransactions(List<TransactionRecordPacket> transactionPackets)
{
// If sync'd, save the first non-syncd transaction index.
if (syncd)
{
firstSyncTransaction = logger.Log.Count;
syncd = false;
}
foreach (TransactionRecordPacket trp in transactionPackets)
{
logger.Log.Add(new DataTablePKTransactionRecord(trp));
}
}
public void AddTransactions(DataTable table)
{
// If sync'd, save the first non-syncd transaction index.
if (syncd)
{
firstSyncTransaction = logger.Log.Count;
syncd = false;
}
foreach (DataRow dr in table.Rows)
{
TransactionRecordPacket trp = new TransactionRecordPacket();
foreach (DataColumn dc in logger.SourceTable.PrimaryKey)
{
trp.PrimaryKeyValues[dc.ColumnName] = dr[dc.ColumnName];
}
trp.TransactionType = (DataTableTransactionRecord.RecordType)dr["TransType"];
trp.ColumnName = (string)dr["ColumnName"];
// An insert transaction will have NewValue set to DBNull.Value
if (dr["NewValue"] != DBNull.Value)
{
string str = (string)dr["NewValue"];
Type t = Type.GetType((string)dr["ValueType"]);
trp.NewValue = Converter.Convert(str, t);
}
logger.Log.Add(new DataTablePKTransactionRecord(trp));
}
}
/// <summary>
/// Synchronizes, by applying, the records in the transaction log. This method assumes
/// that rows will be identified by their primary keys. Adding a new row does not fix up
/// forward references, as does DataTableTransactionLog.Apply, because it is assumed that
/// a new row transaction is going to be seen here for the first time (rather than undoing
/// a delete, for example).
/// </summary>
public void Sync()
{
SetupSort();
// overridden by the option LogOnlyNewFieldChanges
logger.SuspendLogging();
//logger.LogOnlyNewFieldChanges();
// Ignore constraints, allowing us to add rows and then update not-null fields.
logger.SourceTable.BeginLoadData();
// Start with the first transaction to be sync'd.
int i = firstSyncTransaction;
int j = logger.Log.Count;
// Excludes additional transactions that might occur when the row is added.
while (i < j)
{
DataTableTransactionRecord record = logger.Log[i];
if (!(record is DataTablePKTransactionRecord))
{
throw new DataTableSynchronizationManagerException("Expected a record of type DataTablePKTransactionRecord.");
}
// Only log read-writable table columns changes...
// This prevents us from logging fields that contain SQL expressions (as per the Interacx schema).
if (record.TransactionType == DataTableTransactionRecord.RecordType.ChangeField)
{
if (!logger.DataView.Table.Columns[record.ColumnName].ReadOnly)
{
record.Apply(logger.DataView);
}
}
else
{
// or insert/delete row changes.
record.Apply(logger.DataView);
}
++i;
}
syncd = true;
// Resume constraints.
logger.SourceTable.EndLoadData();
logger.ResumeLogging();
}
/// <summary>
/// Sets up the Sort property for the log's internal DataView. This method need only
/// be used when synchronizing a DataTable with TransactionRecordPacket records.
/// </summary>
protected void SetupSort()
{
if (logger.SourceTable.PrimaryKey == null)
{
throw new DataTableTransactionException("GetTransactions requires at least one PK.");
}
string sortBy = String.Empty;
string comma = String.Empty;
foreach (DataColumn dc in logger.SourceTable.PrimaryKey)
{
sortBy += comma + dc.ColumnName;
comma = ", ";
}
logger.DataView.Sort = sortBy;
}
protected virtual DataTable InitializeTransactionTableColumns()
{
DataTable dt = new DataTable("Transactions");
DataColumn[] pkCols = new DataColumn[logger.SourceTable.PrimaryKey.Length];
int i = 0;
// TODO: See other todo's, potential collision between the PK column name and the
// internal column names we're adding to the table.
foreach (DataColumn dc in logger.SourceTable.PrimaryKey)
{
DataColumn newdc = new DataColumn(dc.ColumnName, dc.DataType);
dt.Columns.Add(newdc);
pkCols[i] = newdc;
}
//dt.PrimaryKey = pkCols;
// TODO: These column names might collide with PK column names!
dt.Columns.Add(new DataColumn("TransType", typeof(Int32)));
dt.Columns.Add(new DataColumn("ColumnName", typeof(string)));
dt.Columns.Add(new DataColumn("ValueType", typeof(string)));
dt.Columns.Add(new DataColumn("NewValue", typeof(string)));
dt.Columns.Add(new DataColumn("OldValue", typeof(string)));
dt.Columns["ValueType"].AllowDBNull = true;
dt.Columns["NewValue"].AllowDBNull = true;
dt.Columns["OldValue"].AllowDBNull = true;
return dt;
}
protected virtual DataRow InitializeTransactionRow(DataTable dt, DataTableTransactionRecord record)
{
// For Interacx, for each transaction, we need to include the other fields involved in
// all relationships (masters and details) for the row associated with the transaction record.
// But we want to do this in a way that doesn't affect this code base.
DataRow dr = dt.NewRow();
// Allows a derived class to add additional information to the transaction row.
TransactionRecordPacket trp = new TransactionRecordPacket(record);
foreach (KeyValuePair<string, object> entry in trp.PrimaryKeyValues)
{
dr[entry.Key] = entry.Value;
}
dr["TransType"] = (int)trp.TransactionType;
// ColumnName is required, but for insert transactions, it's null, so we spoof it.
if (trp.ColumnName == null)
{
dr["ColumnName"] = DBNull.Value.ToString();
}
else
{
dr["ColumnName"] = trp.ColumnName;
}
// Null values are preserved as DBNull.Value
if ((trp.NewValue != null) && (trp.NewValue != DBNull.Value))
{
dr["ValueType"] = trp.NewValue.GetType().AssemblyQualifiedName;
dr["NewValue"] = trp.NewValue;
dr["OldValue"] = trp.OldValue;
}
return dr;
}
}
}