Exemplo n.º 1
0
        /// <summary>
        /// Load the data
        /// </summary>
        /// <param name="operation">The operation to perform</param>
        /// <param name="skip">The number of records to skip</param>
        /// <param name="take">The number of records to take</param>
        private void LoadData(SelectedOperation operation, int skip = 0, int take = 10)
        {
            try
            {
                switch (operation)
                {
                case SelectedOperation.First:
                    // Should the load continue or be cancelled
                    if (OnBeforeFirst != null)
                    {
                        Nequeo.Custom.OperationArgs op = new Nequeo.Custom.OperationArgs(false);
                        OnBeforeFirst(this, op);

                        // Cancel operation if true.
                        if (op.Cancel)
                        {
                            return;
                        }
                    }
                    break;

                case SelectedOperation.Previous:
                    // Should the load continue or be cancelled
                    if (OnBeforePrevious != null)
                    {
                        Nequeo.Custom.OperationArgs op = new Nequeo.Custom.OperationArgs(false);
                        OnBeforePrevious(this, op);

                        // Cancel operation if true.
                        if (op.Cancel)
                        {
                            return;
                        }
                    }
                    break;

                case SelectedOperation.Next:
                    // Should the load continue or be cancelled
                    if (OnBeforeNext != null)
                    {
                        Nequeo.Custom.OperationArgs op = new Nequeo.Custom.OperationArgs(false);
                        OnBeforeNext(this, op);

                        // Cancel operation if true.
                        if (op.Cancel)
                        {
                            return;
                        }
                    }
                    break;

                case SelectedOperation.Last:
                    // Should the load continue or be cancelled
                    if (OnBeforeLast != null)
                    {
                        Nequeo.Custom.OperationArgs op = new Nequeo.Custom.OperationArgs(false);
                        OnBeforeLast(this, op);

                        // Cancel operation if true.
                        if (op.Cancel)
                        {
                            return;
                        }
                    }
                    break;

                case SelectedOperation.Custom:
                    // Should the load continue or be cancelled
                    if (OnBeforeCustom != null)
                    {
                        Nequeo.Custom.OperationArgs op = new Nequeo.Custom.OperationArgs(false);
                        OnBeforeCustom(this, op);

                        // Cancel operation if true.
                        if (op.Cancel)
                        {
                            return;
                        }
                    }
                    break;
                }

                // Build the current data object type and
                // the  select data model generic type.
                Type dataType = Type.GetType(ConnectionTypeModel.DataObjectTypeName, true, true);
                Type dataAccessProviderType = Type.GetType(ConnectionTypeModel.DataAccessProvider, true, true);
                Type listGenericType        = typeof(SelectDataGenericBase <>);

                // Create the generic type parameters
                // and create the genric type.
                Type[] typeArgs = { dataType };
                Type   listGenericTypeConstructor = listGenericType.MakeGenericType(typeArgs);

                // Add the genric tyoe contructor parameters
                // and create the generic type instance.
                object[] parameters = new object[] {
                    ConnectionTypeModel.DatabaseConnection,
                    ConnectionTypeModel.ConnectionType,
                    ConnectionTypeModel.ConnectionDataType,
                    ((Nequeo.Data.DataType.IDataAccess)Activator.CreateInstance(dataAccessProviderType))
                };
                object listGeneric = Activator.CreateInstance(listGenericTypeConstructor, parameters);

                // Get all properites in the data object type.
                PropertyInfo[] properties = dataType.GetProperties();

                // If the query exists.
                Object resultExpression = null;
                if (Query != null)
                {
                    // Create the expression constructor.
                    Nequeo.Data.DataType.DataTypeConversion dataTypeConversion =
                        new Nequeo.Data.DataType.DataTypeConversion(ConnectionTypeModel.ConnectionDataType);
                    Nequeo.Data.Linq.SqlStatementConstructor sql = new Nequeo.Data.Linq.SqlStatementConstructor(dataTypeConversion);

                    // Create the lambda expression.
                    resultExpression = sql.CreateLambdaExpression(Query.Queries, dataType, Query.Operand);
                }

                string where = WhereClause;
                string   orderBy     = OrderByClause;
                Object[] argsRecords = new Object[] { };

                // Get the current object.
                if (resultExpression != null)
                {
                    // Add the query arguments
                    argsRecords = new Object[] { resultExpression };
                }
                else
                {
                    if (!String.IsNullOrEmpty(where))
                    {
                        argsRecords = new Object[] { where }
                    }
                    ;
                }

                // Add the current data row to the
                // business object collection.
                object count = listGeneric.GetType().InvokeMember("GetRecordCount",
                                                                  BindingFlags.DeclaredOnly | BindingFlags.Public |
                                                                  BindingFlags.Instance | BindingFlags.InvokeMethod,
                                                                  null, listGeneric, argsRecords);

                // Assign the total number of records.
                _totalRecords = Convert.ToInt32(count);

                switch (operation)
                {
                case SelectedOperation.First:
                    skip = 0;
                    break;

                case SelectedOperation.Previous:
                    skip = skip < 0 ? 0 : skip;
                    break;

                case SelectedOperation.Next:
                    skip = skip > _totalRecords ? _totalRecords - take : skip;
                    break;

                case SelectedOperation.Last:
                    skip = _totalRecords - take;
                    break;

                case SelectedOperation.Custom:
                    skip = skip * take;
                    break;
                }

                // Get the current object.
                Object[] args = new Object[]
                {
                    (skip > 0 ? skip : 0),
                    (String.IsNullOrEmpty(orderBy) ? properties[0].Name : orderBy)
                };

                // If a take count is set.
                if (take > 0)
                {
                    args = new Object[]
                    {
                        (skip > 0 ? skip : 0),
                        take,
                        (String.IsNullOrEmpty(orderBy) ? properties[0].Name : orderBy)
                    }
                }
                ;

                // If an expression has been created.
                if (resultExpression != null)
                {
                    args = new Object[]
                    {
                        (skip > 0 ? skip : 0),
                        (String.IsNullOrEmpty(orderBy) ? properties[0].Name : orderBy),
                        resultExpression
                    };
                }
                else
                {
                    // If the where clause is set.
                    if (!String.IsNullOrEmpty(where))
                    {
                        args = new Object[]
                        {
                            (skip > 0 ? skip : 0),
                            (String.IsNullOrEmpty(orderBy) ? properties[0].Name : orderBy),
                            where
                        }
                    }
                    ;
                }

                // If an expression has been created.
                if (resultExpression != null)
                {
                    args = new Object[]
                    {
                        (skip > 0 ? skip : 0),
                        take,
                        (String.IsNullOrEmpty(orderBy) ? properties[0].Name : orderBy),
                        resultExpression
                    };
                }
                else
                {
                    // If the take count and where clause are set.
                    if ((take > 0) && (!String.IsNullOrEmpty(where)))
                    {
                        args = new Object[]
                        {
                            (skip > 0 ? skip : 0),
                            take,
                            (String.IsNullOrEmpty(orderBy) ? properties[0].Name : orderBy),
                            where
                        }
                    }
                    ;
                }

                // Add the current data row to the
                // business object collection.
                object ret = listGeneric.GetType().InvokeMember("SelectData",
                                                                BindingFlags.DeclaredOnly | BindingFlags.Public |
                                                                BindingFlags.Instance | BindingFlags.InvokeMethod,
                                                                null, listGeneric, args);

                // Return the collection.
                DataModel            = (IEnumerable)ret;
                dataGrid.ItemsSource = (IEnumerable)DataModel;

                switch (operation)
                {
                case SelectedOperation.First:
                    if (OnFirst != null)
                    {
                        OnFirst(this, new EventArgs());
                    }
                    break;

                case SelectedOperation.Previous:
                    if (OnPrevious != null)
                    {
                        OnPrevious(this, new EventArgs());
                    }
                    break;

                case SelectedOperation.Next:
                    if (OnNext != null)
                    {
                        OnNext(this, new EventArgs());
                    }
                    break;

                case SelectedOperation.Last:
                    if (OnLast != null)
                    {
                        OnLast(this, new EventArgs());
                    }
                    break;

                case SelectedOperation.Custom:
                    if (OnCustom != null)
                    {
                        OnCustom(this, new EventArgs());
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                string inner = ex.InnerException != null ? ex.InnerException.Message : string.Empty;
                switch (operation)
                {
                case SelectedOperation.First:
                    if (OnFirstError != null)
                    {
                        OnFirstError(this, new Nequeo.Custom.MessageArgs(ex.Message + " " + inner));
                    }
                    break;

                case SelectedOperation.Previous:
                    if (OnPreviousError != null)
                    {
                        OnPreviousError(this, new Nequeo.Custom.MessageArgs(ex.Message + " " + inner));
                    }
                    break;

                case SelectedOperation.Next:
                    if (OnNextError != null)
                    {
                        OnNextError(this, new Nequeo.Custom.MessageArgs(ex.Message + " " + inner));
                    }
                    break;

                case SelectedOperation.Last:
                    if (OnLastError != null)
                    {
                        OnLastError(this, new Nequeo.Custom.MessageArgs(ex.Message + " " + inner));
                    }
                    break;

                case SelectedOperation.Custom:
                    if (OnCustomError != null)
                    {
                        OnCustomError(this, new Nequeo.Custom.MessageArgs(ex.Message + " " + inner));
                    }
                    break;
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// On before delete item
 /// </summary>
 /// <param name="e"></param>
 protected virtual void OnBeforeDeleteItem(Nequeo.Custom.OperationArgs e)
 {
 }
Exemplo n.º 3
0
 /// <summary>
 /// On before insert item
 /// </summary>
 /// <param name="e"></param>
 protected virtual void OnBeforeInsertItem(Nequeo.Custom.OperationArgs e)
 {
 }
Exemplo n.º 4
0
 /// <summary>
 /// On before load
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 void GenericDataAccess_OnBeforeLoad(object sender, Nequeo.Custom.OperationArgs e)
 {
     OnBeforeLoadItem(e);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Delete the data.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Should the delete continue or be cancelled
                if (OnBeforeDelete != null)
                {
                    Nequeo.Custom.OperationArgs operation = new Nequeo.Custom.OperationArgs(false);
                    OnBeforeDelete(this, operation);

                    // Cancel operation if true.
                    if (operation.Cancel)
                    {
                        return;
                    }
                }

                // Build the current data object type and
                // the  select data model generic type.
                Type dataType = Type.GetType(ConnectionTypeModel.DataObjectTypeName, true, true);
                Type dataAccessProviderType = Type.GetType(ConnectionTypeModel.DataAccessProvider, true, true);
                Type listGenericType        = typeof(DeleteDataGenericBase <>);

                // Create the generic type parameters
                // and create the genric type.
                Type[] typeArgs = { dataType };
                Type   listGenericTypeConstructor = listGenericType.MakeGenericType(typeArgs);

                // Add the genric tyoe contructor parameters
                // and create the generic type instance.
                object[] parameters = new object[] {
                    ConnectionTypeModel.DatabaseConnection,
                    ConnectionTypeModel.ConnectionType,
                    ConnectionTypeModel.ConnectionDataType,
                    ((Nequeo.Data.DataType.IDataAccess)Activator.CreateInstance(dataAccessProviderType))
                };
                object listGeneric = Activator.CreateInstance(listGenericTypeConstructor, parameters);

                // If the data model is enumerable
                if (DataModel is System.Collections.IEnumerable)
                {
                    // Cast the data object type as an enumerable object,
                    // get the enumerator.
                    System.Collections.IEnumerable items       = (System.Collections.IEnumerable)DataModel;
                    System.Collections.IEnumerator dataObjects = items.GetEnumerator();
                    List <PropertyInfo>            properties  = dataType.GetProperties().ToList();

                    // Iterate through the collection.
                    while (dataObjects.MoveNext())
                    {
                        object currentDataObject = dataObjects.Current;

                        // Get the current object.
                        Object[] args = new Object[]
                        {
                            currentDataObject
                        };

                        // Add the current data row to the
                        // business object collection.
                        object ret = listGeneric.GetType().InvokeMember("DeleteItem",
                                                                        BindingFlags.DeclaredOnly | BindingFlags.Public |
                                                                        BindingFlags.Instance | BindingFlags.InvokeMethod,
                                                                        null, listGeneric, args);
                    }
                    dataObjects.Reset();
                }
                else
                {
                    // Get the current object.
                    Object[] args = new Object[]
                    {
                        DataModel
                    };

                    // Add the current data row to the
                    // business object collection.
                    object ret = listGeneric.GetType().InvokeMember("DeleteItem",
                                                                    BindingFlags.DeclaredOnly | BindingFlags.Public |
                                                                    BindingFlags.Instance | BindingFlags.InvokeMethod,
                                                                    null, listGeneric, args);
                }

                if (OnDelete != null)
                {
                    OnDelete(this, new EventArgs());
                }
            }
            catch (Exception ex)
            {
                string inner = ex.InnerException != null ? ex.InnerException.Message : string.Empty;
                if (OnDeleteError != null)
                {
                    OnDeleteError(this, new Nequeo.Custom.MessageArgs(ex.Message + " " + inner));
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Load the data.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Should the load continue or be cancelled
                if (OnBeforeLoad != null)
                {
                    Nequeo.Custom.OperationArgs operation = new Nequeo.Custom.OperationArgs(false);
                    OnBeforeLoad(this, operation);

                    // Cancel operation if true.
                    if (operation.Cancel)
                    {
                        return;
                    }
                }

                // Build the current data object type and
                // the  select data model generic type.
                Type dataType = Type.GetType(ConnectionTypeModel.DataObjectTypeName, true, true);
                Type dataAccessProviderType = Type.GetType(ConnectionTypeModel.DataAccessProvider, true, true);
                Type listGenericType        = typeof(SelectDataGenericBase <>);

                // Create the generic type parameters
                // and create the genric type.
                Type[] typeArgs = { dataType };
                Type   listGenericTypeConstructor = listGenericType.MakeGenericType(typeArgs);

                // Add the genric tyoe contructor parameters
                // and create the generic type instance.
                object[] parameters = new object[] {
                    ConnectionTypeModel.DatabaseConnection,
                    ConnectionTypeModel.ConnectionType,
                    ConnectionTypeModel.ConnectionDataType,
                    ((Nequeo.Data.DataType.IDataAccess)Activator.CreateInstance(dataAccessProviderType))
                };
                object listGeneric = Activator.CreateInstance(listGenericTypeConstructor, parameters);

                // Get all properites in the data object type.
                PropertyInfo[] properties = dataType.GetProperties();
                int            skip       = 0;
                int            take       = 1;
                string where = WhereClause;
                string orderBy = OrderByClause;

                // Get the current object.
                Object[] args = new Object[]
                {
                    (skip > 0 ? skip : 0),
                    (String.IsNullOrEmpty(orderBy) ? properties[0].Name : orderBy)
                };

                // If a take count is set.
                if (take > 0)
                {
                    args = new Object[]
                    {
                        (skip > 0 ? skip : 0),
                        take,
                        (String.IsNullOrEmpty(orderBy) ? properties[0].Name : orderBy)
                    }
                }
                ;

                // If the where clause is set.
                if (!String.IsNullOrEmpty(where))
                {
                    args = new Object[]
                    {
                        (skip > 0 ? skip : 0),
                        (String.IsNullOrEmpty(orderBy) ? properties[0].Name : orderBy),
                        where
                    }
                }
                ;

                // If the take count and where clause are set.
                if ((take > 0) && (!String.IsNullOrEmpty(where)))
                {
                    args = new Object[]
                    {
                        (skip > 0 ? skip : 0),
                        take,
                        (String.IsNullOrEmpty(orderBy) ? properties[0].Name : orderBy),
                        where
                    }
                }
                ;

                // Add the current data row to the
                // business object collection.
                object ret = listGeneric.GetType().InvokeMember("SelectData",
                                                                BindingFlags.DeclaredOnly | BindingFlags.Public |
                                                                BindingFlags.Instance | BindingFlags.InvokeMethod,
                                                                null, listGeneric, args);

                // Return the collection.
                DataModel = (IEnumerable)ret;

                if (OnLoad != null)
                {
                    OnLoad(this, new EventArgs());
                }
            }
            catch (Exception ex)
            {
                string inner = ex.InnerException != null ? ex.InnerException.Message : string.Empty;
                if (OnLoadError != null)
                {
                    OnLoadError(this, new Nequeo.Custom.MessageArgs(ex.Message + " " + inner));
                }
            }
        }