Example #1
0
        private PXModel LoadData(PCAxis.Query.SavedQuery sq)
        {
            //Only loads the first table source otherwise redirects to a page
            if (sq.Sources.Count != 1)
            {
                //TODO redirect to error page incopatable query for PX-Web
            }

            TableSource src = sq.Sources[0];

            IPXModelBuilder builder = null;

            if (src.Type == "CNMM")
            {
                if (RouteInstance.RouteExtender == null)
                {
                    DatabaseInfo db = PXWeb.Settings.Current.General.Databases.GetCnmmDatabase(src.DatabaseId);

                    if (db == null)
                    {
                        //TODO Redirect database does not exist
                        return(null);
                    }

                    var tableName = QueryHelper.GetTableName(src);
                    builder = PxContext.CreatePaxiomBuilder(src.DatabaseId, tableName);
                }
                else
                {
                    DatabaseInfo db = PXWeb.Settings.Current.General.Databases.GetCnmmDatabase(RouteInstance.RouteExtender.GetDatabase());

                    var tableName = QueryHelper.GetTableName(src);
                    builder = PxContext.CreatePaxiomBuilder(RouteInstance.RouteExtender.Db.Database.id, tableName);
                }
            }
            else if (src.Type == "PX")
            {
                DatabaseInfo db = PXWeb.Settings.Current.General.Databases.GetPxDatabase(src.DatabaseId);
                if (db == null)
                {
                }
                else
                {
                    if (!db.HasLanguage(src.Language))
                    {
                        //TODO Redirect that the database is missing
                        return(null);
                    }

                    if (src.Source.StartsWith("-/"))
                    {
                        src.Source = src.DatabaseId + src.Source.Substring(1);
                    }

                    builder = PxContext.CreatePaxiomBuilder(src.DatabaseId, src.Source);
                }
            }
            else
            {
                //TODO redirect to error page incompatible datasource type
                return(null);
            }

            builder.SetPreferredLanguage(src.Language);

            //If languefe set in reques we must read all langauges to be able to run workflow operations
            if (_language != null)
            {
                builder.ReadAllLanguages = true;
            }

            builder.BuildForSelection();
            var model = builder.Model;

            List <PCAxis.Paxiom.Selection> sel = new List <PCAxis.Paxiom.Selection>();

            foreach (var variable in model.Meta.Variables)
            {
                var query = src.Quieries.FirstOrDefault(q => q.Code == variable.Code);
                PCAxis.Paxiom.Selection s = null;

                if (query == null)
                {
                    //Selects all values for the variable if it can't be eliminated
                    s = new PCAxis.Paxiom.Selection(variable.Code);
                    if (variable.IsContentVariable || !variable.Elimination)
                    {
                        s.ValueCodes.AddRange(variable.Values.Select(v => v.Code).ToArray());
                    }
                }
                else
                {
                    if (PCAxis.Query.QueryHelper.IsAggregation(query.Selection.Filter))
                    {
                        s = QueryHelper.SelectAggregation(variable, query, builder);
                    }
                    else if (query.Selection.Filter.StartsWith("vs:", StringComparison.InvariantCultureIgnoreCase))
                    {
                        s = QueryHelper.SelectValueSet(variable, query, builder);
                    }
                    else
                    {
                        switch (query.Selection.Filter)
                        {
                        case "item":
                            s = QueryHelper.SelectItem(variable, query);
                            break;

                        case "top":
                            s = QueryHelper.SelectTop(variable, query);
                            break;

                        case "from":
                            s = QueryHelper.SelectFrom(variable, query);
                            break;

                        case "all":
                            s = QueryHelper.SelectAll(variable, query);
                            break;

                        default:
                            //TODO unsupported filter
                            break;
                        }
                    }
                }

                if (s != null)
                {
                    sel.Add(s);
                }
            }

            var selection = sel.ToArray();

            //TODO fixa till
            //if (sq.Output.Type == "SCREEN")
            PCAxis.Query.TableQuery tbl = new PCAxis.Query.TableQuery(builder.Model, selection);
            PaxiomManager.QueryModel = tbl;

            BuildModelForPresentation(builder, selection);

            return(builder.Model);
        }