Sunteți pe pagina 1din 9

using using using using using using using using using using using using using

System; System.Collections.Generic; System.Linq; System.Text; System.ComponentModel; Interaction.Interfaces; System.Windows; System.Reflection; Interaction.Attributes; Interaction.Utilities; System.Xml.Serialization; Interaction.Classes; Interaction.EventArguments;

namespace Interaction.Base { [Serializable()] public class ItemBase : IItem, INotifyPropertyChanged { #region INotifyPropertyChanged [field: NonSerialized] public event PropertyChangedEventHandler PropertyChanged; /// <summary> /// Raised when propertie's value changed. /// </summary> public void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { PropertyChanged(this, e); if (this.CollectionManager != null) { this.CollectionManager.SourceCollection.CollectionState = CollectionState.Changed; } //this.ItemState = global::ItemState.Changed; IItemPropertyChangedEventArgs iItemPropertyChanged = (IItemP ropertyChangedEventArgs)e; PropertyInfo pi = this.GetType().GetProperty(iItemPropertyCh anged.PropertyName); if (pi != null) { RelationAttribute relationAttribute = (RelationAttribute )pi.GetCustomAttribute(typeof(RelationAttribute), false); if (relationAttribute != null) {

for (Int32 n = 0; n < relationAttribute.TargetTables .Count(); n++) { IDataManager targerDataManager = this.Collection Manager.DataManager.DataSet.DataManagers.Where(dm => dm.TableName == relationAtt ribute.TargetTables[n]).FirstOrDefault(); if (targerDataManager != null) { if ((pi.PropertyType == typeof(Id)) == false ) { IEnumerable<IItem> foundItems = ((IEnume rable<IItem>)targerDataManager.CollectionManager.SourceCollection).Where(it => i t.GetValue(relationAttribute.TargetProperties[n]).Equals(iItemPropertyChanged.Ol dValue)); foreach (Object item in foundItems) { PropertyInfo propVal = item.GetT ype().GetProperty(relationAttribute.TargetProperties[n]); propVal.SetValue(item, iItemProp ertyChanged.NewValue); ((IItem)item).ItemState = global ::ItemState.Changed; } } else { Id oldId = (Id)iItemPropertyChanged.OldV alue; IEnumerable<IItem> foundItems = ((IEnume rable<IItem>)targerDataManager.CollectionManager.SourceCollection).Where(it => i t.GetValue(relationAttribute.TargetProperties[n]).Equals(oldId.UnFormatedId)); foreach (Object item in foundItems) { PropertyInfo propVal = item.GetT ype().GetProperty(relationAttribute.TargetProperties[n]); Id newId = (Id)iItemPropertyChan ged.NewValue; propVal.SetValue(item, newId.UnF ormatedId); ((IItem)item).ItemState = global ::ItemState.Changed; } } } }

} } //PropertyInfo pi = this.GetType().GetProperty("ListOfRelate dCollections"); //if (iItemPropertyChanged != null & pi != null) //{ // List<Object> ListOfRelatedCollections = (List<Object>) pi.GetValue(this); // // // // // Object>)coll; // //MessageBox.Show(foundItems.Where(i => i.GetT ype().GetProperty(e.PropertyName).GetValue(i).Equals(iItemPropertyChanged.OldVal ue)).Count().ToString()); // if (foundItems.Count() > 0) // { // foreach (Object item in foundItems.Where(i => i.GetType().GetProperty(e.PropertyName).GetValue(i).Equals(iItemPropertyChan ged.OldValue))) // { // PropertyInfo propVal = item.GetType(). GetProperty(e.PropertyName); // propVal.SetValue(item, iItemPropertyCh anged.NewValue); // // // // //} } } /// <summary> /// Raised when propertie's value changed. /// </summary> public void OnPropertyChanged(String propertyName) { IItemPropertyChangedEventArgs iItemPropertyChanged = new IItemProper tyChangedEventArgs(propertyName, "", ""); this.OnPropertyChanged((PropertyChangedEventArgs)iItemPropertyChange d); } public void OnPropertyChanged(String propertyName, Object oldValue, Obje ct newValue) } } } } if (ListOfRelatedCollections.Count > 0) { foreach (Object coll in ListOfRelatedCollections) { IEnumerable<Object> foundItems = (IEnumerable<

{ IItemPropertyChangedEventArgs iItemPropertyChanged = new IItemProper tyChangedEventArgs(propertyName, oldValue, newValue); this.OnPropertyChanged((PropertyChangedEventArgs)iItemPropertyChange d); } #endregion #region Constructors protected virtual void Initialize() { //MessageBox.Show("Base"); } protected void SetDefaultValues() { PropertyInfo[] pi = this.ItemType.GetProperties(); foreach (PropertyInfo p in pi) { if (p.CanWrite == true) { DefaultValueAttribute defaultValueAttribute = (DefaultValueA ttribute)p.GetCustomAttributes(typeof(DefaultValueAttribute), false).FirstOrDefa ult(); FieldAttribute fieldAttribute = (FieldAttribute)p.GetCustomA ttributes(typeof(FieldAttribute), false).FirstOrDefault(); if (defaultValueAttribute != null & fieldAttribute != null) { p.SetValue(this, defaultValueAttribute.Value); } } } } [Field(false)] public ItemBase() { this.Initialize(); this.SetDefaultValues(); } [Field(false)] public ItemBase(ICollectionManager collectionManager) { this.Initialize(); this.SetDefaultValues(); this.collectionManager = collectionManager; } #endregion

#region Item properties private Id id; /// <summary> /// Gets or sets a ItemBase Id. /// </summary> [Field(true)] public virtual Id Id { get { return this.id; } set { this.id = value; this.OnPropertyChanged("Id"); } } private String backupUnformatedId = ""; [EditorBrowsable(EditorBrowsableState.Never)] public String BackupUnformatedId { get { return this.backupUnformatedId; } set { this.backupUnformatedId = value; } } private Int32 itemIdentifier; [Field(false)] public virtual Int32 ItemIdentifier { get { return this.itemIdentifier; } set { this.itemIdentifier = value; this.OnPropertyChanged("ItemIdentifier"); } } //private Type itemType; [Field(false)] public Type ItemType { get { return this.GetType(); } } //// (FieldCount) System.Int32 //[Field(false), XmlIgnore, DataObjectField(false)] //public Int32 FieldCount //{ // get // { // Int32 attCount = 0; // PropertyInfo[] pi = this.ItemType.GetProperties(); // for (int i = 0; i < pi.Count() - 1; i++) // { // FieldAttribute fieldAtt = (FieldAttribute)pi[i].GetCustomA ttributes(typeof(FieldAttribute), false).FirstOrDefault();

// // // // // // // // // // //}

if (fieldAtt != null) { if (fieldAtt.IsField == true) { attCount += 1; } } } return attCount; }

// (ItemState) ItemState private ItemState itemState; /// <summary> /// Gets or sets the current item state. /// </summary> [Field(false), XmlIgnore] public ItemState ItemState { get { return this.itemState; } set { this.itemState = value; this.OnPropertyChanged("ItemState"); //this.CollectionManager.OnItemChanged((IItem)this); } } private Boolean isEditable = true; [Field(false)] public Boolean IsEditable { get { return this.isEditable; } set { this.isEditable = value; this.OnPropertyChanged("IsEditable"); //if (value) //{ // ion) // // // // // // //} } } private Boolean excludeFiltering = false; { if (it != this & it.isEditable == true) { it.isEditable = false; } } foreach (IItem it in this.CollectionManager.ISourceCollect

[Field(false)] public Boolean ExcludeFiltering { get { return this.excludeFiltering; } set { this.excludeFiltering = value; this.OnPropertyChanged("ExcludeFiltering"); //if (value) //{ // ion) // // // // // // //} } } private ICollectionManager collectionManager; public ICollectionManager CollectionManager { get { return this.collectionManager; } } #endregion #region Funtions public Object GetValue(String propertyName) { Object value = null; PropertyInfo pi = this.ItemType.GetProperty(propertyName); value = pi.GetGetMethod().Invoke(this, null); return value; } public Object GetValue(IItem item) { Object value = null; if (item.GetLabeledField() != null) { PropertyInfo pi = item.GetType().GetProperty(item.GetLabeledFiel d()); value = pi.GetGetMethod().Invoke(item, null); } return value; } { if (it != this & it.isEditable == true) { it.isEditable = false; } } foreach (IItem it in this.CollectionManager.ISourceCollect

public String GetLabeledField() { String result = ""; PropertyInfo[] pi = this.ItemType.GetProperties(); for (Int32 n = 0; n < pi.Count(); n++) { LabelAttribute labeldAtt = (LabelAttribute)pi[n].GetCustomAttrib utes(typeof(LabelAttribute), false).FirstOrDefault(); if (labeldAtt != null) { result = pi[n].Name; } } return result; } #endregion #region Pendences private Boolean areTherePendencies; [Field(false), XmlIgnore] public virtual Boolean AreTherePendencies { get { return this.areTherePendencies; } set { this.areTherePendencies = value; this.OnPropertyChanged("AreTherePendencies"); } } [NonSerialized, XmlIgnore] private IList<Pendence> pendings = new List<Pendence>(); [Field(false), XmlIgnore] public IList<Pendence> Pendences { get { return this.pendings; } } #endregion #region Commands private dynamic commands = new DynamicCommands(); [Field(false), XmlIgnore] public dynamic Commands { get { return this.commands; } } #endregion #region #endregion

public object Clone() { return this.MemberwiseClone(); } //public event ItemChangedHandler ItemStateChanged //{ // add { this.itemStateChanged += value; } // remove { this.itemStateChanged -= value; } //} //private ItemChangedHandler itemStateChanged; //protected void OnItemStateChanged() //{ // if (this.itemStateChanged != null) // { // itemStateChanged(this, new ItemChangedEventArgs(this.ItemState )); // //} }

public static void SetBackup(IItem sourceItem, IItem targetItem) { PropertyInfo[] pi = sourceItem.GetType().GetProperties(); for (Int32 i = 0; i < pi.Count(); i++) { FieldAttribute fieldAtt = (FieldAttribute)pi[i].GetCustomAttribu tes(typeof(FieldAttribute), false).FirstOrDefault(); if ((fieldAtt != null) && (fieldAtt.IsField == true)) { Object value = pi[i].GetValue(sourceItem, null); pi[i].SetValue(targetItem, value, null); } } } } }

S-ar putea să vă placă și