Example #1
0
        public override object Deserialize(IDictionary <string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            esEntity entity = Activator.CreateInstance(type) as esEntity;

            if (entity != null)
            {
                List <string> propertyNames = (from esColumnMetadata c in entity.es.Meta.Columns select c.PropertyName).ToList();
                var           properties    = dictionary.Where(e => propertyNames.Contains(e.Key)).ToDictionary(e => e.Key, e => e.Value);
                var           extraColumns  = dictionary.Where(e => !propertyNames.Contains(e.Key)).ToDictionary(e => e.Key, e => e.Value);
                entity.SetProperties(properties);
                entity.SetExtraColumns(extraColumns);
            }
            return(entity);
        }
        public override void Update
        (
            System.Collections.IDictionary keys,
            System.Collections.IDictionary values,
            System.Collections.IDictionary oldValues,
            DataSourceViewOperationCallback callback
        )
        {
            esDataSourceUpdateEventArgs e = null;

            try
            {
                if (keys != null && keys.Count > 0)
                {
                    e = new esDataSourceUpdateEventArgs();

                    e.Keys      = keys;
                    e.Values    = values;
                    e.OldValues = oldValues;

                    this.OnPreUpdate(e);
                    if (e.Cancel)
                    {
                        return;
                    }

                    // Find the proper esEntity and set it's values
                    object[] pks = new object[keys.Count];

                    int index = 0;
                    foreach (object value in keys.Values)
                    {
                        pks[index++] = value;
                    }

                    esDataSourceCreateEntityEventArgs ce = new esDataSourceCreateEntityEventArgs();
                    ce.PrimaryKeys = pks;
                    this.OnCreateEntity(ce);

                    esEntity entity = ce.Entity;
                    e.Entity = entity;

                    //this.OnPreUpdate(e);

                    if (entity != null)
                    {
                        entity.SetProperties(values);
                    }

                    e.EventWasHandled = false;
                    this.OnUpdate(e);

                    if (!e.EventWasHandled)
                    {
                        entity.Save();
                        this.OnDataSourceViewChanged(EventArgs.Empty);
                    }

                    this.OnPostUpdate(e);
                }
            }
            catch (Exception ex)
            {
                esDataSourceExceptionEventArgs exArgs = new esDataSourceExceptionEventArgs(ex);
                exArgs.EventType  = esDataSourceEventType.Delete;
                exArgs.UpdateArgs = e;

                try
                {
                    this.OnException(exArgs);
                }
                catch { }

                if (!exArgs.ExceptionWasHandled)
                {
                    throw;
                }
            }
            finally
            {
                callback(1, null);
            }
        }
        public override void Insert(System.Collections.IDictionary values, DataSourceViewOperationCallback callback)
        {
            esDataSourceInsertEventArgs e = null;

            try
            {
                e        = new esDataSourceInsertEventArgs();
                e.Values = values;

                this.OnPreInsert(e);
                if (e.Cancel)
                {
                    return;
                }

                esDataSourceCreateEntityEventArgs ce = new esDataSourceCreateEntityEventArgs();
                this.OnCreateEntity(ce);

                esEntity entity = ce.Entity;
                e.Entity = entity;

                if (entity != null)
                {
                    entity.SetProperties(values);
                }

                //this.OnPreInsert(e);

                e.EventWasHandled = false;
                this.OnInsert(e);

                if (!e.EventWasHandled)
                {
                    entity.Save();
                    this.OnDataSourceViewChanged(EventArgs.Empty);
                }

                e.EventWasHandled = false;
                this.OnPostInsert(e);
            }
            catch (Exception ex)
            {
                esDataSourceExceptionEventArgs exArgs = new esDataSourceExceptionEventArgs(ex);
                exArgs.EventType  = esDataSourceEventType.Insert;
                exArgs.InsertArgs = e;

                try
                {
                    this.OnException(exArgs);
                }
                catch { }

                if (!exArgs.ExceptionWasHandled)
                {
                    throw;
                }
            }
            finally
            {
                callback(1, null);
            }
        }