EchoTex_Payroll/Ease.Core/Model/ObjectTemplate.cs
2024-10-14 10:01:49 +06:00

354 lines
13 KiB
C#

using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ease.Core.Model;
using Ease.Core.Utility;
using System.ComponentModel.DataAnnotations.Schema;
namespace Ease.Core.Model
{
#region Framwwork: Business Object
#region Framwwork: Event delegate
public delegate void ObjectIDChanged(long newID);
#endregion
public abstract class ObjectTemplate
{
private int _sortOrder;
protected ObjectState _objectState;
protected int _id = int.MinValue;
//private Dictionary<string, string> _auditTrail;
//The event handler need to be non serialized
//public event ObjectIDChanged IDChanged;
public ObjectTemplate()
{
_objectState = ObjectState.New;
}
/// <summary>
/// ID of the object
/// </summary>
public int ID
{
get { return _id; }
set { _id = value; }
}
/// <summary>
/// State of the object IsNew/Not
/// </summary>
//public bool IsNew
//{
// get { return _objectState == ObjectState.New; }
//}
public bool IsNew
{
get { return _id <= 0; }
}
/// <summary>
/// State of the object IsModified/Not
/// </summary>
//public bool IsModified
//{
// get { return _objectState == ObjectState.Modified; }
//}
/// <summary>
/// State of the object IsSaved/Not
/// </summary>
//public bool IsSaved
//{
// get { return _objectState == ObjectState.Saved; }
//}
//public bool IsUnassigned
//{
// get { return _id == long.MinValue; }
//}
[NotMapped]
public int SortOrder
{
get { return _sortOrder; }
set { _sortOrder = value; }
}
/// <summary>
/// Set modified state of the object
/// </summary>
protected void SetObjectStateModified()
{
if (_objectState == ObjectState.Saved)
_objectState = ObjectState.Modified;
}
/// <summary>
/// Set ID of the object
/// </summary>
/// <param name="id">A valid ID for the object.</param>
public void SetID(int id)
{
_id = id;
//if (IDChanged != null)
// IDChanged(_id);
}
/// <summary>
/// Set state of the object
/// </summary>
/// <param name="state">A valid state.</param>
protected internal void SetState(ObjectState state)
{
_objectState = state;
}
protected virtual internal void SetReadOnlyProperties(params object[] propertyValues)
{
}
public override string ToString()
{
return GetType().Name + " (" + _id.ToString() + ")";
}
public override bool Equals(object obj)
{
if (obj == null || (obj.GetType() != this.GetType()))
return false;
return _id.Equals((obj as ObjectTemplate)._id);
}
public override int GetHashCode()
{
if ((object)_id == null)
return base.GetHashCode();
else
return _id.GetHashCode();
}
public static bool operator ==(ObjectTemplate template1, ObjectTemplate template2)
{
if ((object)template1 != null)
return template1.Equals(template2);
else
return ((object)template2 == null);
}
public static bool operator !=(ObjectTemplate template1, ObjectTemplate template2)
{
if ((object)template1 != null)
return !template1.Equals(template2);
else
return !((object)template2 == null);
}
/// <summary>
/// Make clone of the current instance rather reference
/// </summary>
/// <returns>Return another instance of current instance rather reference.</returns>
protected ObjectTemplate Clone()
{
return this.CopyObject();
}
/// <summary>
/// Create string value from current instance.
/// </summary>
/// <param name="key">For further reconstruct.</param>
/// <param name="delimiter">Delimiter for each property for current instance.</param>
/// <param name="properties">Name of the property(ies) by which string will consturct.</param>
/// <returns>Return delimited string.</returns>
protected virtual string LineData(int key, string delimiter, params string[] properties)
{
if (delimiter.IndexOf('|') >= 0)
throw new Exception("Invalid use of separator: '|'. User separator other than '|'.");
string line = string.Format("{0}", key);
PropertyInfo[] pis = this.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (string property in properties)
{
foreach (PropertyInfo pi in pis)
{
if (pi.Name.ToLower().Equals(property.ToLower()))
{
string value = string.Empty;
object val = pi.GetValue(this, null);
if (val != null)
{
if (pi.PropertyType.IsEnum)
value = Convert.ToInt32(val).ToString("0");
else if (pi.PropertyType == typeof(long))
value = Convert.ToInt32(val).ToString("0");
else if (pi.PropertyType == typeof(int))
value = Convert.ToInt32(val).ToString("0");
else if (pi.PropertyType == typeof(double))
value = Convert.ToDouble(val).ToString("0.0000");
else if (pi.PropertyType == typeof(DateTime))
value = Convert.ToDateTime(val).ToString("MM/dd/yyyy");
else if (pi.PropertyType == typeof(bool))
value = Convert.ToBoolean(val) ? "1" : "0";
else
{
value = val.ToString();
value = value.Replace(Environment.NewLine, "*^*");
}
}
line = string.Format("{0}{1}{2}", line, delimiter, value);
break;
}
}
}
return line;
}
/// <summary>
/// Create an instatnce of BaseType.
/// </summary>
/// <param name="baseType">Type of object to create an instance.</param>
/// <param name="delimiter">Delimiter in the string to separate each property.</param>
/// <param name="objString">Delimited string to create object.</param>
/// <param name="properties">Set property(ies) of object.</param>
/// <returns>Return an instance of basetype.</returns>
protected ObjectTemplate CreateObjectFromString(Type baseType, string delimiter, string objString,
params string[] properties)
{
if (delimiter.IndexOf('|') >= 0)
throw new Exception("Invalid use of separator: '|'. User separator other than '|'.");
ObjectTemplate ot =
(ObjectTemplate)ObjectUtility.CreateInstance(baseType.FullName, new object[] { }, baseType);
PropertyInfo[] props = baseType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
int index = objString.IndexOf(delimiter);
if (index != -1)
objString = objString.Substring(index + 1);
string[] values = objString.Split(delimiter.ToCharArray());
index = -1;
if (values.Length != properties.Length)
throw new Exception("No of properties and values are not same");
foreach (string property in properties)
{
index++;
foreach (PropertyInfo prop in props)
{
if (prop.Name.ToLower().Equals(property.ToLower()))
{
object value = values[index];
if (prop.PropertyType.IsEnum)
prop.SetValue(ot, Enum.Parse(prop.PropertyType, value.ToString(), true), null);
else if (prop.PropertyType.Equals(typeof(bool)))
prop.SetValue(ot, Convert.ToBoolean(Convert.ToInt32(value)), null);
else if (prop.PropertyType.Equals(typeof(DateTime)))
prop.SetValue(ot, Convert.ToDateTime(value), null);
else if (prop.PropertyType.Equals(typeof(double)))
prop.SetValue(ot, Convert.ToDouble(value), null);
else if (prop.PropertyType.Equals(typeof(decimal)))
prop.SetValue(ot, Convert.ToDecimal(value), null);
else if (prop.PropertyType.Equals(typeof(Int16)))
prop.SetValue(ot, Convert.ToInt16(value), null);
else if (prop.PropertyType.Equals(typeof(Int32)))
prop.SetValue(ot, Convert.ToInt32(value), null);
else if (prop.PropertyType.Equals(typeof(Int64)))
prop.SetValue(ot, Convert.ToInt64(value), null);
else if (prop.PropertyType.Equals(typeof(byte)))
prop.SetValue(ot, Convert.ToByte(value), null);
else
{
value = value.ToString().Replace("*^*", Environment.NewLine);
prop.SetValue(ot, value, null);
}
break;
}
}
}
return ot;
}
internal ObjectTemplate CopyObject()
{
ObjectTemplate cot =
Ease.Core.Utility.ObjectUtility.CreateInstance(this.GetType().FullName, new object[] { },
this.GetType()) as ObjectTemplate;
foreach (FieldInfo fi in this.GetType()
.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
fi.SetValue(cot, fi.GetValue(this));
return cot;
}
/// <summary>
/// Keep Track whether value of property has been changed or not
/// and property value cannot be null.
/// </summary>
/// <typeparam name="T">Type of property i.e string/int/DateTime e.t.c.</typeparam>
/// <param name="propertyName">Name of the property i.e "Description".</param>
/// <param name="currentValue">Existing value of the property i.e "Computer Ease Ltd"</param>
/// <param name="newValue">New value of the property i.e. "CEL"</param>
//protected void OnPropertyChange<T>(string propertyName, T currentValue, T newValue)
// where T : IEquatable<T>
//{
// if ((newValue != null && !newValue.Equals(currentValue)) || (newValue == null && currentValue != null))
// {
// if (_objectState != ObjectState.New)
// {
// if (_auditTrail == null)
// _auditTrail = new Dictionary<string, string>();
// _auditTrail[propertyName] = string.Format("{0} => {1}", currentValue, newValue);
// _objectState = ObjectState.Modified;
// }
// }
//}
/// <summary>
/// Keep Track whether value of property has been changed or not
/// and property value may be null.
/// </summary>
/// <typeparam name="T">Type of property i.e string/int/DateTime e.t.c.</typeparam>
/// <param name="propertyName">Name of the property i.e "Description".</param>
/// <param name="currentValue">Existing value of the property i.e "Computer Ease Ltd"</param>
/// <param name="newValue">New value of the property i.e. "CEL"</param>
//protected void OnPropertyChange<T>(string propertyName, Nullable<T> currentValue, Nullable<T> newValue)
// where T : struct
//{
// if ((newValue != null && !newValue.Equals(currentValue)) || (newValue == null && currentValue != null))
// {
// if (_objectState != ObjectState.New)
// {
// if (_auditTrail == null)
// _auditTrail = new Dictionary<string, string>();
// _auditTrail[propertyName] = string.Format("{0} => {1}", currentValue, newValue);
// _objectState = ObjectState.Modified;
// }
// }
//}
}
#endregion
}