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 _auditTrail; //The event handler need to be non serialized //public event ObjectIDChanged IDChanged; public ObjectTemplate() { _objectState = ObjectState.New; } /// /// ID of the object /// public int ID { get { return _id; } set { _id = value; } } /// /// State of the object IsNew/Not /// //public bool IsNew //{ // get { return _objectState == ObjectState.New; } //} public bool IsNew { get { return _id <= 0; } } /// /// State of the object IsModified/Not /// //public bool IsModified //{ // get { return _objectState == ObjectState.Modified; } //} /// /// State of the object IsSaved/Not /// //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; } } /// /// Set modified state of the object /// protected void SetObjectStateModified() { if (_objectState == ObjectState.Saved) _objectState = ObjectState.Modified; } /// /// Set ID of the object /// /// A valid ID for the object. public void SetID(int id) { _id = id; //if (IDChanged != null) // IDChanged(_id); } /// /// Set state of the object /// /// A valid state. 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); } /// /// Make clone of the current instance rather reference /// /// Return another instance of current instance rather reference. protected ObjectTemplate Clone() { return this.CopyObject(); } /// /// Create string value from current instance. /// /// For further reconstruct. /// Delimiter for each property for current instance. /// Name of the property(ies) by which string will consturct. /// Return delimited string. 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; } /// /// Create an instatnce of BaseType. /// /// Type of object to create an instance. /// Delimiter in the string to separate each property. /// Delimited string to create object. /// Set property(ies) of object. /// Return an instance of basetype. 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; } /// /// Keep Track whether value of property has been changed or not /// and property value cannot be null. /// /// Type of property i.e string/int/DateTime e.t.c. /// Name of the property i.e "Description". /// Existing value of the property i.e "Computer Ease Ltd" /// New value of the property i.e. "CEL" //protected void OnPropertyChange(string propertyName, T currentValue, T newValue) // where T : IEquatable //{ // if ((newValue != null && !newValue.Equals(currentValue)) || (newValue == null && currentValue != null)) // { // if (_objectState != ObjectState.New) // { // if (_auditTrail == null) // _auditTrail = new Dictionary(); // _auditTrail[propertyName] = string.Format("{0} => {1}", currentValue, newValue); // _objectState = ObjectState.Modified; // } // } //} /// /// Keep Track whether value of property has been changed or not /// and property value may be null. /// /// Type of property i.e string/int/DateTime e.t.c. /// Name of the property i.e "Description". /// Existing value of the property i.e "Computer Ease Ltd" /// New value of the property i.e. "CEL" //protected void OnPropertyChange(string propertyName, Nullable currentValue, Nullable newValue) // where T : struct //{ // if ((newValue != null && !newValue.Equals(currentValue)) || (newValue == null && currentValue != null)) // { // if (_objectState != ObjectState.New) // { // if (_auditTrail == null) // _auditTrail = new Dictionary(); // _auditTrail[propertyName] = string.Format("{0} => {1}", currentValue, newValue); // _objectState = ObjectState.Modified; // } // } //} } #endregion }