Ejemplo n.º 1
0
        public static Select MergeQuery(FieldConfig config, Select select, RestSelect restSelect, string cultureCode)
        {
            var query = select.Query;

            select.PageSize  = restSelect.PageSize;
            select.PageIndex = restSelect.PageIndex;
            if (restSelect.Query != null && restSelect.Query.FilterRules != null)
            {
                foreach (var rule in restSelect.Query.FilterRules)
                {
                    if (rule.FieldOperator == OperatorEnum.IN)
                    {
                        if (rule.MultiValue != null)
                        {
                            query.AddRule(FieldConfigUtils.CreateFilterRule(config, cultureCode,
                                                                            rule.Field,
                                                                            rule.FieldOperator,
                                                                            rule.MultiValue.Select(v => new StringRuleValue(v.ToString()))
                                                                            ));
                        }
                    }
                    else if (rule.FieldOperator == OperatorEnum.BETWEEN)
                    {
                        // not yet implemented
                    }
                    else
                    {
                        if (rule.Value != null)
                        {
                            query.AddRule(FieldConfigUtils.CreateFilterRule(config, cultureCode,
                                                                            rule.Field,
                                                                            rule.FieldOperator,
                                                                            new StringRuleValue(rule.Value.ToString())
                                                                            ));
                        }
                    }
                }
            }

            if (restSelect.Sort != null && restSelect.Sort.Any())
            {
                select.Sort.Clear();
                foreach (var sort in restSelect.Sort)
                {
                    select.Sort.Add(FieldConfigUtils.CreateSortRule(config, cultureCode,
                                                                    sort.Field,
                                                                    sort.Descending
                                                                    ));
                }
            }
            return(select);
        }
Ejemplo n.º 2
0
        public HttpResponseMessage Get(string entity, int pageIndex, int pageSize, string filter = null, string sort = null)
        {
            try
            {
                RestSelect restSelect = new RestSelect()
                {
                    PageIndex = pageIndex,
                    PageSize  = pageSize
                };
                if (!string.IsNullOrEmpty(filter))
                {
                    restSelect.Query = JsonConvert.DeserializeObject <RestGroup>(filter);
                }
                if (!string.IsNullOrEmpty(sort))
                {
                    restSelect.Sort = JsonConvert.DeserializeObject <List <RestSort> >(sort);
                }

                //int ModuleId = int.Parse(Request.Headers.GetValues("ModuleId").First());
                //int TabId = int.Parse(Request.Headers.GetValues("TabId").First());
                ModuleController mc           = new ModuleController();
                ModuleInfo       activeModule = ActiveModule; //mc.GetModule(ModuleId, TabId, false);

                OpenContentSettings settings = activeModule.OpenContentSettings();
                ModuleInfo          module   = activeModule;
                if (settings.ModuleId > 0)
                {
                    //ModuleController mc = new ModuleController();
                    module = mc.GetModule(settings.ModuleId, settings.TabId, false);
                }
                var     manifest         = settings.Template.Manifest;
                var     templateManifest = settings.Template;
                JObject reqOptions       = null;
                //if (!string.IsNullOrEmpty(req.options))
                //{
                //    reqOptions = JObject.Parse(req.options);
                //}
                //string editRole = manifest.GetEditRole();
                bool listMode = templateManifest != null && templateManifest.IsListTemplate;
                if (listMode)
                {
                    var          indexConfig  = OpenContentUtils.GetIndexConfig(settings.Template.Key.TemplateDir);
                    QueryBuilder queryBuilder = new QueryBuilder(indexConfig);
                    bool         isEditable   = ActiveModule.CheckIfEditable(PortalSettings);//portalSettings.UserMode != PortalSettings.Mode.Edit;
                    queryBuilder.Build(settings.Query, !isEditable, UserInfo.UserID, DnnLanguageUtils.GetCurrentCultureCode(), UserInfo.Social.Roles);

                    RestQueryBuilder.MergeQuery(indexConfig, queryBuilder.Select, restSelect, DnnLanguageUtils.GetCurrentCultureCode());
                    IDataItems dsItems;
                    if (queryBuilder.DefaultNoResults && queryBuilder.Select.IsQueryEmpty)
                    {
                        dsItems = new DefaultDataItems()
                        {
                            Items = new List <DefaultDataItem>(),
                            Total = 0
                        };
                    }
                    else
                    {
                        var ds        = DataSourceManager.GetDataSource(manifest.DataSource);
                        var dsContext = new DataSourceContext()
                        {
                            ModuleId       = module.ModuleID,
                            UserId         = UserInfo.UserID,
                            TemplateFolder = settings.TemplateDir.FolderPath,
                            Config         = manifest.DataSourceConfig,
                            Options        = reqOptions
                        };
                        dsItems = ds.GetAll(dsContext, queryBuilder.Select);
                    }
                    int          mainTabId = settings.DetailTabId > 0 ? settings.DetailTabId : settings.TabId;
                    ModelFactory mf        = new ModelFactory(dsItems.Items, activeModule, PortalSettings, mainTabId);
                    mf.Options = reqOptions;
                    var model = mf.GetModelAsJson(false);
                    var res   = new JObject();
                    res["meta"] = new JObject();
                    if (LogContext.IsLogActive)
                    {
                        var logKey = "Query";
                        LogContext.Log(activeModule.ModuleID, logKey, "select", queryBuilder.Select);
                        LogContext.Log(activeModule.ModuleID, logKey, "result", dsItems);
                        LogContext.Log(activeModule.ModuleID, logKey, "model", model);
                        res["meta"]["logs"] = JToken.FromObject(LogContext.Current.ModuleLogs(activeModule.ModuleID));

                        if (restSelect != null)
                        {
                            //res["meta"]["select"] = JObject.FromObject(restSelect);
                        }
                    }
                    foreach (var item in model["Items"] as JArray)
                    {
                        item["id"] = item["Context"]["Id"];
                        JsonUtils.IdJson(item);
                    }
                    res[entity]          = model["Items"];
                    res["meta"]["total"] = dsItems.Total;

                    return(Request.CreateResponse(HttpStatusCode.OK, res));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "not supported because not in multi items template "));
                }
            }
            catch (Exception exc)
            {
                Log.Logger.Error(exc);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc));
            }
        }
Ejemplo n.º 3
0
        public HttpResponseMessage Get(string entity, int pageIndex, int pageSize, string filter = null, string sort = null)
        {
            try
            {
                var collection = entity;
                //if (entity == "items")
                collection = AppConfig.DEFAULT_COLLECTION; // backward compatibility
                RestSelect restSelect = new RestSelect()
                {
                    PageIndex = pageIndex,
                    PageSize  = pageSize
                };
                if (!string.IsNullOrEmpty(filter))
                {
                    restSelect.Query = JsonConvert.DeserializeObject <RestGroup>(filter);
                }
                if (!string.IsNullOrEmpty(sort))
                {
                    restSelect.Sort = JsonConvert.DeserializeObject <List <RestSort> >(sort);
                }

                ModuleInfo activeModule = ActiveModule;

                OpenContentSettings   settings = activeModule.OpenContentSettings();
                OpenContentModuleInfo module   = new OpenContentModuleInfo(ActiveModule);
                JObject reqOptions             = null;

                if (module.IsListMode())
                {
                    var          indexConfig  = OpenContentUtils.GetIndexConfig(settings.TemplateDir, collection);
                    QueryBuilder queryBuilder = new QueryBuilder(indexConfig);
                    bool         isEditable   = ActiveModule.CheckIfEditable(PortalSettings);
                    queryBuilder.Build(settings.Query, !isEditable, UserInfo.UserID, DnnLanguageUtils.GetCurrentCultureCode(), UserInfo.Social.Roles);

                    RestQueryBuilder.MergeQuery(indexConfig, queryBuilder.Select, restSelect, DnnLanguageUtils.GetCurrentCultureCode());
                    IDataItems dsItems;
                    if (queryBuilder.DefaultNoResults && queryBuilder.Select.IsQueryEmpty)
                    {
                        dsItems = new DefaultDataItems()
                        {
                            Items = new List <DefaultDataItem>(),
                            Total = 0
                        };
                    }
                    else
                    {
                        IDataSource ds        = DataSourceManager.GetDataSource(module.Settings.Manifest.DataSource);
                        var         dsContext = OpenContentUtils.CreateDataContext(module, UserInfo.UserID, false, reqOptions);
                        dsContext.Collection = collection;
                        dsItems = ds.GetAll(dsContext, queryBuilder.Select);
                    }
                    var mf = new ModelFactoryMultiple(dsItems.Items, module, PortalSettings, collection);
                    mf.Options = reqOptions;
                    var model = mf.GetModelAsJson(false);
                    var res   = new JObject();
                    res["meta"] = new JObject();
                    if (LogContext.IsLogActive)
                    {
                        var logKey = "Query";
                        LogContext.Log(activeModule.ModuleID, logKey, "select", queryBuilder.Select);
                        LogContext.Log(activeModule.ModuleID, logKey, "debuginfo", dsItems.DebugInfo);
                        LogContext.Log(activeModule.ModuleID, logKey, "model", model);
                        res["meta"]["logs"] = JToken.FromObject(LogContext.Current.ModuleLogs(activeModule.ModuleID));

                        if (restSelect != null)
                        {
                            //res["meta"]["select"] = JObject.FromObject(restSelect);
                        }
                    }
                    foreach (var item in model["Items"] as JArray)
                    {
                        item["id"] = item["Context"]["Id"];
                        JsonUtils.IdJson(item);
                    }
                    res[entity]          = model[collection];
                    res["meta"]["total"] = dsItems.Total;
                    return(Request.CreateResponse(HttpStatusCode.OK, res));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "not supported because not in multi items template "));
                }
            }
            catch (Exception exc)
            {
                Log.Logger.Error(exc);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc));
            }
        }
Ejemplo n.º 4
0
        public static Select MergeQuery(FieldConfig config, Select select, RestSelect restSelect, string cultureCode)
        {
            var query = select.Query;

            select.PageSize  = restSelect.PageSize;
            select.PageIndex = restSelect.PageIndex;
            if (restSelect.Query != null && restSelect.Query.FilterRules != null)
            {
                foreach (var rule in restSelect.Query.FilterRules)
                {
                    if (rule.FieldOperator == OperatorEnum.IN)
                    {
                        if (rule.MultiValue != null)
                        {
                            query.AddRule(FieldConfigUtils.CreateFilterRule(config, cultureCode,
                                                                            rule.Field,
                                                                            rule.FieldOperator,
                                                                            rule.MultiValue.Select(v => new StringRuleValue(v.ToString()))
                                                                            ));
                        }
                    }
                    else if (rule.FieldOperator == OperatorEnum.BETWEEN)
                    {
                        // not yet implemented
                    }
                    else
                    {
                        if (rule.Value != null)
                        {
                            RuleValue val;
                            if (rule.Value.Type == Newtonsoft.Json.Linq.JTokenType.Boolean)
                            {
                                val = new BooleanRuleValue((bool)rule.Value.Value);
                            }
                            else if (rule.Value.Type == Newtonsoft.Json.Linq.JTokenType.Integer)
                            {
                                val = new IntegerRuleValue((int)rule.Value.Value);
                            }
                            else if (rule.Value.Type == Newtonsoft.Json.Linq.JTokenType.Float)
                            {
                                val = new FloatRuleValue((float)rule.Value.Value);
                            }
                            else
                            {
                                val = new StringRuleValue(rule.Value.ToString());
                            }

                            query.AddRule(FieldConfigUtils.CreateFilterRule(config, cultureCode,
                                                                            rule.Field,
                                                                            rule.FieldOperator,
                                                                            val
                                                                            ));
                        }
                    }
                }
            }

            if (restSelect.Sort != null && restSelect.Sort.Any())
            {
                select.Sort.Clear();
                foreach (var sort in restSelect.Sort)
                {
                    select.Sort.Add(FieldConfigUtils.CreateSortRule(config, cultureCode,
                                                                    sort.Field,
                                                                    sort.Descending
                                                                    ));
                }
            }
            return(select);
        }