164 lines
5.7 KiB
C#
164 lines
5.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Dbas;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
|
|
namespace Sarand.Net.Standard.Base.UpdateLog
|
|
{
|
|
//=======================================================================
|
|
public class SaUpdateLogItemMonitor : SacsStateItem
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Configuration.
|
|
public class Config
|
|
{
|
|
public Config (SaUpdateLogConsole updateLogConsole, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
UpdateLogConsole = updateLogConsole;
|
|
DbUpdateInfo = dbUpdateInfo;
|
|
}
|
|
public SaUpdateLogConsole UpdateLogConsole { get; }
|
|
public short ItemType { get; set; }
|
|
public SysId ItemId { get; set; }
|
|
public DbUpdateInfo DbUpdateInfo { get; }
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Definitions.
|
|
public class AttrData
|
|
{
|
|
public AttrData (String name)
|
|
{
|
|
Name = name;
|
|
}
|
|
public String Name { get; private set; }
|
|
public DataValue ValueBefore { get; internal set; } = new DataValue();
|
|
public DataValue ValueAfter { get; internal set; } = new DataValue();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private Config config = null;
|
|
private readonly Dictionary<String,AttrData> attrUpdateMap = new Dictionary<String,AttrData>();
|
|
|
|
//-------------------------------------------------------------------
|
|
// Static initialisation
|
|
static SaUpdateLogItemMonitor ()
|
|
{
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Protected construction - use derived classes.
|
|
protected SaUpdateLogItemMonitor (Config config)
|
|
{
|
|
this.config = config;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
public short ItemType { get { return config.ItemType; } }
|
|
public SysId ItemId { get { return config.ItemId; } }
|
|
public List<AttrData> AttrUpdates
|
|
{
|
|
get
|
|
{
|
|
// Compile list of attributes whose value has changed.
|
|
List<AttrData> attrUpdates = new List<AttrData>();
|
|
foreach (AttrData attrData in attrUpdateMap.Values)
|
|
{
|
|
if (!FormatDataValue(attrData.ValueBefore).Equals(FormatDataValue(attrData.ValueAfter)))
|
|
{
|
|
// Data values are ppre-formatted as Strings for database storage.
|
|
attrUpdates.Add(new AttrData(attrData.Name) { ValueBefore=new DataValue(FormatDataValue(attrData.ValueBefore)), ValueAfter= new DataValue(FormatDataValue(attrData.ValueAfter)) });
|
|
}
|
|
}
|
|
return attrUpdates;
|
|
}
|
|
}
|
|
public DbUpdateInfo DbUpdateInfo { get { return config.DbUpdateInfo; } }
|
|
|
|
//-------------------------------------------------------------------
|
|
// Log updates.
|
|
public bool LogUpdates ()
|
|
{
|
|
ClearErrorState();
|
|
if (!config.UpdateLogConsole.LogUpdates(this))
|
|
SetErrorState(config.UpdateLogConsole.ErrorStateMessage);
|
|
return !HasErrorState();
|
|
}
|
|
protected virtual bool IsColumnChangeLogged (String attrName)
|
|
{
|
|
if (!config.UpdateLogConsole.CanUpdateDatabase) return false;
|
|
return true;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Populate attribute values.
|
|
protected bool RecordAttrValue (String attrName, String value, bool newValue)
|
|
{
|
|
return RecordAttrValue(attrName, new DataValue(value), newValue);
|
|
}
|
|
protected bool RecordAttrValue (String attrName, int value, bool newValue, bool blankZeroValue = true)
|
|
{
|
|
return RecordAttrValue(attrName, new DataValue(value == 0 && blankZeroValue ? "" : value.ToString()), newValue);
|
|
}
|
|
protected bool RecordAttrValue (String attrName, decimal value, bool newValue, bool blankZeroValue = true, ushort decimals = 2)
|
|
{
|
|
return RecordAttrValue(attrName, new DataValue(value == 0.0m && blankZeroValue ? "" : value.ToString("N" + decimals.ToString())), newValue);
|
|
}
|
|
protected bool RecordAttrValue (String attrName, DateTime value, bool newValue, bool includeTime = true)
|
|
{
|
|
return RecordAttrValue(attrName, new DataValue(DateUtil.Format(value, (includeTime ? "d-MMM-yyyy HH:mm" : "d-MMM-yyyy"))), newValue);
|
|
}
|
|
protected bool RecordAttrValue (String attrName, DataValue value, bool newValue)
|
|
{
|
|
ClearErrorState();
|
|
if (attrName.IsEmpty()) return SetErrorState("Blank attribute name ["+GetType().Name+"]");
|
|
if (!IsColumnChangeLogged(attrName)) return true;
|
|
if (newValue)
|
|
SetAttrValueAfter(attrName, value);
|
|
else
|
|
SetAttrValueBefore(attrName, value);
|
|
return !HasErrorState();
|
|
}
|
|
public void SetAttrValueBefore (String attrName, DataValue value)
|
|
{
|
|
AttrData attrData = (attrUpdateMap.ContainsKey(attrName) ? attrUpdateMap[attrName] : null);
|
|
if (attrData == null)
|
|
{
|
|
attrData = new AttrData(attrName);
|
|
attrUpdateMap[attrName] = attrData;
|
|
}
|
|
attrData.ValueBefore = new DataValue(value);
|
|
}
|
|
public void SetAttrValueAfter (String attrName, DataValue value)
|
|
{
|
|
AttrData attrData = (attrUpdateMap.ContainsKey(attrName) ? attrUpdateMap[attrName] : null);
|
|
if (attrData == null)
|
|
{
|
|
attrData = new AttrData(attrName);
|
|
attrUpdateMap[attrName] = attrData;
|
|
}
|
|
attrData.ValueAfter = new DataValue(value);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Format data value for database storage.
|
|
private static String FormatDataValue (DataValue dataValue)
|
|
{
|
|
String value = (dataValue != null ? dataValue.ToString() : "");
|
|
StringUtil.ReplaceAll(value, "\t", " ");
|
|
StringUtil.ReplaceAll(value, "\n\r", "\n");
|
|
StringUtil.ReplaceAll(value, "\r\n", "\n");
|
|
StringUtil.ReplaceAll(value, "\r", " ");
|
|
StringUtil.ReplaceAll(value, "\n", " ");
|
|
return value;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|