예제 #1
0
        public HttpResponseMessage Get(string entity, int pageIndex, int pageSize, string filter = null, string sort = null)
        {
            try
            {
                var        collection = entity;
                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));
            }
        }
예제 #2
0
        public HttpResponseMessage GetExcelByQuery(int moduleId, int tabId, string queryName, string filter = null, string sort = null)
        {
            RestSelect restSelect = new RestSelect()
            {
                PageIndex = 0,
                PageSize  = 100000
            };

            if (!string.IsNullOrEmpty(filter))
            {
                restSelect.Query = JsonConvert.DeserializeObject <RestGroup>(filter);
            }
            if (!string.IsNullOrEmpty(sort))
            {
                restSelect.Sort = JsonConvert.DeserializeObject <List <RestSort> >(sort);
            }
            IEnumerable <IDataItem> dataList = new List <IDataItem>();
            var module   = OpenContentModuleConfig.Create(moduleId, tabId, PortalSettings);
            var manifest = module.Settings.Template.Manifest;

            if (!module.HasAllUsersViewPermissions())
            {
                return(Request.CreateResponse(HttpStatusCode.Unauthorized));
            }
            string filename  = queryName;
            bool   useLucene = module.Settings.Template.Manifest.Index;

            if (useLucene)
            {
                var indexConfig = OpenContentUtils.GetIndexConfig(module.Settings.Template);

                QueryBuilder queryBuilder = new QueryBuilder(indexConfig);
                queryBuilder.Build(module.Settings.Query, true, UserInfo.UserID, DnnLanguageUtils.GetCurrentCultureCode(), UserInfo.Social.Roles.FromDnnRoles());
                RestQueryBuilder.MergeQuery(indexConfig, queryBuilder.Select, restSelect, DnnLanguageUtils.GetCurrentCultureCode());
                IDataSource ds        = DataSourceManager.GetDataSource(module.Settings.Manifest.DataSource);
                var         dsContext = OpenContentUtils.CreateDataContext(module, UserInfo.UserID);

                if (string.IsNullOrEmpty(queryName))
                {
                    var dsItems = ds.GetAll(dsContext, queryBuilder.Select);
                    dataList = dsItems.Items;
                    filename = dsContext.Collection;
                }
                else
                {
                    var qds   = ds as IDataQueries;
                    var query = qds?.GetQueries(dsContext).SingleOrDefault(q => q.Name == queryName);
                    if (query != null)
                    {
                        var dsItems = query.GetAll(dsContext, queryBuilder.Select);
                        dataList = dsItems.Items;
                    }
                }
            }

            var     mf    = new ModelFactoryMultiple(dataList, null, manifest, null, null, module);
            dynamic model = mf.GetModelAsDictionary(true);

            var    rssTemplate = new FileUri(module.Settings.TemplateDir, filename + "-excel.hbs");
            string source      = rssTemplate.FileExists ? FileUriUtils.ReadFileFromDisk(rssTemplate) : GenerateTemplateFromModel(model, rssTemplate);

            HandlebarsEngine hbEngine = new HandlebarsEngine();
            string           res      = hbEngine.Execute(source, model);

            var fileBytes = ExcelUtils.CreateExcel(res);

            return(ExcelUtils.CreateExcelResponseMessage(filename + ".xlsx", fileBytes));
        }