Beispiel #1
0
        DbDataReader IDataEngine.ExecuteReader(PageRequest request)
        {
            ViewPage page = new ViewPage(request);

            if (_config == null)
            {
                _config = CreateConfiguration(request.Controller);
                SelectView(request.Controller, request.View);
            }
            page.ApplyDataFilter(_config.CreateDataFilter(), request.Controller, request.View, null, null, null);
            DbConnection connection    = CreateConnection();
            DbCommand    selectCommand = CreateCommand(connection);

            ConfigureCommand(selectCommand, page, CommandConfigurationType.Select, null);
            return(selectCommand.ExecuteReader(CommandBehavior.CloseConnection));
        }
Beispiel #2
0
        object[] IDataController.GetListOfValues(string controller, string view, DistinctValueRequest request)
        {
            SelectView(controller, view);
            ViewPage page = new ViewPage(request);

            page.ApplyDataFilter(_config.CreateDataFilter(), controller, view, request.LookupContextController, request.LookupContextView, request.LookupContextFieldName);
            List <object> distinctValues = new List <object>();
            BusinessRules rules          = _config.CreateBusinessRules();

            if (rules != null)
            {
                rules.Page = page;
                rules.BeforeSelect(request);
            }
            else
            {
                CreateBusinessRules().ExecuteServerRules(request, ActionPhase.Before);
            }
            using (DbConnection connection = CreateConnection())
            {
                DbCommand command = CreateCommand(connection);
                ConfigureCommand(command, page, CommandConfigurationType.SelectDistinct, null);
                DbDataReader reader = command.ExecuteReader();
                while (reader.Read() && (distinctValues.Count < page.PageSize))
                {
                    object v = reader.GetValue(0);
                    if (!(DBNull.Value.Equals(v)))
                    {
                        v = ConvertObjectToValue(v);
                    }
                    distinctValues.Add(v);
                }
                reader.Close();
            }
            if (rules != null)
            {
                rules.AfterSelect(request);
            }
            else
            {
                CreateBusinessRules().ExecuteServerRules(request, ActionPhase.After);
            }
            return(distinctValues.ToArray());
        }
Beispiel #3
0
        ViewPage IDataController.GetPage(string controller, string view, PageRequest request)
        {
            SelectView(controller, view);
            request.AssignContext(controller, this._viewId);
            ViewPage page = new ViewPage(request);

            if (_config.PlugIn != null)
            {
                _config.PlugIn.PreProcessPageRequest(request, page);
            }
            _config.AssignDynamicExpressions(page);
            page.ApplyDataFilter(_config.CreateDataFilter(), request.Controller, request.View, request.LookupContextController, request.LookupContextView, request.LookupContextFieldName);
            BusinessRules rules = _config.CreateBusinessRules();

            if (rules != null)
            {
                rules.Page = page;
                rules.BeforeSelect(request);
            }
            else
            {
                CreateBusinessRules().ExecuteServerRules(request, ActionPhase.Before);
            }
            using (DbConnection connection = CreateConnection())
            {
                if (page.RequiresRowCount && !((request.Inserting || request.DoesNotRequireData)))
                {
                    DbCommand countCommand = CreateCommand(connection);
                    ConfigureCommand(countCommand, page, CommandConfigurationType.SelectCount, null);
                    if (YieldsSingleRow(countCommand))
                    {
                        page.TotalRowCount = 1;
                    }
                    else
                    {
                        page.TotalRowCount = Convert.ToInt32(countCommand.ExecuteScalar());
                    }
                    if (page.RequiresAggregates)
                    {
                        DbCommand aggregateCommand = CreateCommand(connection);
                        ConfigureCommand(aggregateCommand, page, CommandConfigurationType.SelectAggregates, null);
                        DbDataReader reader = aggregateCommand.ExecuteReader();
                        if (reader.Read())
                        {
                            object[] aggregates = new object[page.Fields.Count];
                            for (int i = 0; (i < aggregates.Length); i++)
                            {
                                DataField field = page.Fields[i];
                                if (field.Aggregate != DataFieldAggregate.None)
                                {
                                    object v = reader[field.Name];
                                    if (!(DBNull.Value.Equals(v)))
                                    {
                                        aggregates[i] = v;
                                    }
                                }
                            }
                            page.Aggregates = aggregates;
                        }
                        reader.Close();
                    }
                }
                if (page.RequiresMetaData)
                {
                    PopulatePageCategories(page);
                }
                DbCommand selectCommand = CreateCommand(connection);
                ConfigureCommand(selectCommand, page, CommandConfigurationType.Select, null);
                if ((page.PageSize > 0) && !((request.Inserting || request.DoesNotRequireData)))
                {
                    DbDataReader reader = null;
                    if (selectCommand == null)
                    {
                        reader = ExecuteVirtualReader(request, page);
                    }
                    else
                    {
                        reader = TransactionManager.ExecuteReader(request, page, selectCommand);
                    }
                    while (page.SkipNext())
                    {
                        reader.Read();
                    }
                    while (page.ReadNext() && reader.Read())
                    {
                        object[] values = new object[page.Fields.Count];
                        for (int i = 0; (i < values.Length); i++)
                        {
                            DataField field = page.Fields[i];
                            object    v     = reader[field.Name];
                            if (!(DBNull.Value.Equals(v)))
                            {
                                if (field.IsMirror)
                                {
                                    v = String.Format(field.DataFormatString, v);
                                }
                                else
                                {
                                    v = ConvertObjectToValue(v);
                                }
                                values[i] = v;
                            }
                            if (!(String.IsNullOrEmpty(field.SourceFields)))
                            {
                                values[i] = CreateValueFromSourceFields(field, reader);
                            }
                        }
                        page.Rows.Add(values);
                    }
                    reader.Close();
                }
                if (request.RequiresFirstLetters && this._viewType != "Form")
                {
                    if (!(page.RequiresRowCount))
                    {
                        page.FirstLetters = String.Empty;
                    }
                    else
                    {
                        DbCommand firstLettersCommand = CreateCommand(connection);
                        string[]  oldFilter           = page.Filter;
                        ConfigureCommand(firstLettersCommand, page, CommandConfigurationType.SelectFirstLetters, null);
                        page.Filter = oldFilter;
                        if (!(String.IsNullOrEmpty(page.FirstLetters)))
                        {
                            DbDataReader  reader       = firstLettersCommand.ExecuteReader();
                            StringBuilder firstLetters = new StringBuilder(page.FirstLetters);
                            while (reader.Read())
                            {
                                firstLetters.Append(",");
                                string letter = Convert.ToString(reader[0]);
                                if (!(String.IsNullOrEmpty(letter)))
                                {
                                    firstLetters.Append(letter);
                                }
                            }
                            reader.Close();
                            page.FirstLetters = firstLetters.ToString();
                        }
                    }
                }
            }
            if (_config.PlugIn != null)
            {
                _config.PlugIn.ProcessPageRequest(request, page);
            }
            if (rules != null)
            {
                IRowHandler rowHandler = rules;
                if (request.Inserting)
                {
                    if (rowHandler.SupportsNewRow(request))
                    {
                        page.NewRow = new object[page.Fields.Count];
                        rowHandler.NewRow(request, page, page.NewRow);
                    }
                }
                else
                if (rowHandler.SupportsPrepareRow(request))
                {
                    foreach (object[] row in page.Rows)
                    {
                        rowHandler.PrepareRow(request, page, row);
                    }
                }
                if (rules != null)
                {
                    rules.ProcessPageRequest(request, page);
                }
            }
            page = page.ToResult(_config, _view);
            if (rules != null)
            {
                rules.AfterSelect(request);
                rules.Result.Merge(page);
            }
            else
            {
                CreateBusinessRules().ExecuteServerRules(request, ActionPhase.After);
            }
            return(page);
        }
Beispiel #4
0
        private void ExecuteDataExport(ActionArgs args, ActionResult result)
        {
            if (!(String.IsNullOrEmpty(args.CommandArgument)))
            {
                string[] arguments = args.CommandArgument.Split(',');
                if (arguments.Length > 0)
                {
                    bool sameController = (args.Controller == arguments[0]);
                    args.Controller = arguments[0];
                    if (arguments.Length == 1)
                    {
                        args.View = "grid1";
                    }
                    else
                    {
                        args.View = arguments[1];
                    }
                    if (sameController)
                    {
                        args.SortExpression = null;
                    }
                    SelectView(args.Controller, args.View);
                }
            }
            PageRequest request = new PageRequest(-1, -1, null, null);

            request.SortExpression = args.SortExpression;
            request.Filter         = args.Filter;
            request.ContextKey     = null;
            request.PageIndex      = 0;
            request.PageSize       = Int32.MaxValue;
            if (args.CommandName.EndsWith("Template"))
            {
                request.PageSize = 0;
                args.CommandName = "ExportCsv";
            }
            // store export data to a temporary file
            string       fileName = Path.GetTempFileName();
            StreamWriter writer   = File.CreateText(fileName);

            try
            {
                ViewPage page = new ViewPage(request);
                page.ApplyDataFilter(_config.CreateDataFilter(), args.Controller, args.View, null, null, null);
                using (DbConnection connection = CreateConnection())
                {
                    DbCommand selectCommand = CreateCommand(connection);
                    ConfigureCommand(selectCommand, page, CommandConfigurationType.Select, null);
                    DbDataReader reader = selectCommand.ExecuteReader();
                    if (args.CommandName.EndsWith("Csv"))
                    {
                        ExportDataAsCsv(page, reader, writer);
                    }
                    if (args.CommandName.EndsWith("Rss"))
                    {
                        ExportDataAsRss(page, reader, writer);
                    }
                    if (args.CommandName.EndsWith("Rowset"))
                    {
                        ExportDataAsRowset(page, reader, writer);
                    }
                    reader.Close();
                }
            }
            finally
            {
                writer.Close();
            }
            result.Values.Add(new FieldValue("FileName", null, fileName));
        }