private static SelectQueryDefinition BuildQuery(DataSourceContext context, Select selectQuery)
        {
            SelectQueryDefinition def = new SelectQueryDefinition();

            def.Build(selectQuery);
            if (LogContext.IsLogActive)
            {
                var logKey = "Lucene query";
                LogContext.Log(context.ActiveModuleId, logKey, "Filter", def.Filter.ToString());
                LogContext.Log(context.ActiveModuleId, logKey, "Query", def.Query.ToString());
                LogContext.Log(context.ActiveModuleId, logKey, "Sort", def.Sort.ToString());
                LogContext.Log(context.ActiveModuleId, logKey, "PageIndex", def.PageIndex);
                LogContext.Log(context.ActiveModuleId, logKey, "PageSize", def.PageSize);
            }

            return(def);
        }
        public virtual IDataItems GetAll(DataSourceContext context, Select select)
        {
            if (select == null)
            {
                return(GetAll(context));
            }
            else
            {
                OpenContentController ctrl = new OpenContentController();
                SelectQueryDefinition def  = new SelectQueryDefinition();
                def.Build(@select);
                if (LogContext.IsLogActive)
                {
                    var logKey = "Lucene query";
                    LogContext.Log(context.ActiveModuleId, logKey, "Filter", def.Filter.ToString());
                    LogContext.Log(context.ActiveModuleId, logKey, "Query", def.Query.ToString());
                    LogContext.Log(context.ActiveModuleId, logKey, "Sort", def.Sort.ToString());
                    LogContext.Log(context.ActiveModuleId, logKey, "PageIndex", def.PageIndex);
                    LogContext.Log(context.ActiveModuleId, logKey, "PageSize", def.PageSize);
                }

                SearchResults docs     = LuceneController.Instance.Search(GetModuleId(context).ToString(), def.Filter, def.Query, def.Sort, def.PageSize, def.PageIndex);
                int           total    = docs.TotalResults;
                var           dataList = new List <IDataItem>();
                foreach (string item in docs.ids)
                {
                    var content = ctrl.GetContent(int.Parse(item));
                    if (content != null)
                    {
                        dataList.Add(CreateDefaultDataItem(content));
                    }
                    else
                    {
                        Log.Logger.DebugFormat("OpenContentDataSource.GetAll() ContentItem not found [{0}]", item);
                    }
                }
                return(new DefaultDataItems()
                {
                    Items = dataList,
                    Total = total,
                    DebugInfo = def.Filter.ToString() + " - " + def.Query.ToString() + " - " + def.Sort.ToString()
                });
            }
        }
Exemple #3
0
        public HttpResponseMessage List(RequestDTO req)
        {
            try
            {
                Log.Logger.DebugFormat("OpenFiles.JplistApiController.List() called with request [{0}].", req.ToJson());

                FieldConfig  indexConfig  = FilesRepository.GetIndexConfig(PortalSettings.PortalId);
                bool         addWorkFlow  = PortalSettings.UserMode != PortalSettings.Mode.Edit;
                QueryBuilder queryBuilder = new QueryBuilder(indexConfig);
                queryBuilder.BuildFilter(PortalSettings.PortalId, req.folder, addWorkFlow, PortalSettings.UserInfo.Social.Roles);
                JplistQueryBuilder.MergeJpListQuery(FilesRepository.GetIndexConfig(PortalSettings), queryBuilder.Select, req.StatusLst, DnnLanguageUtils.GetCurrentCultureCode());

                string curFolder = NormalizePath(req.folder);
                foreach (var item in queryBuilder.Select.Query.FilterRules.Where(f => f.Field == LuceneMappingUtils.FolderField))
                {
                    curFolder  = NormalizePath(item.Value.AsString);
                    item.Value = new StringRuleValue(NormalizePath(item.Value.AsString)); //any file of current folder
                }

                var def = new SelectQueryDefinition();
                def.Build(queryBuilder.Select);

                var docs  = LuceneController.Instance.Search(def);
                int total = docs.TotalResults;



                var ratio = string.IsNullOrEmpty(req.imageRatio) ? new Ratio(100, 100) : new Ratio(req.imageRatio);

                Log.Logger.DebugFormat("OpenFiles.JplistApiController.List() Searched for [{0}], found [{1}] items", def.Filter.ToString() + " / " + def.Query.ToString(), total);

                //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);
                //    model["Logs"] = JToken.FromObject(LogContext.Current.ModuleLogs(ActiveModule.ModuleID));
                //}

                var fileManager = FileManager.Instance;
                var data        = new List <FileDTO>();
                var breadcrumbs = new List <IFolderInfo>();
                if (req.withSubFolder)
                {
                    //hier blijken we resultaten toe te voegen die niet uit lucene komen
                    breadcrumbs = AddFolders(NormalizePath(req.folder), curFolder, fileManager, data, ratio);
                }

                foreach (var doc in docs.ids)
                {
                    IFileInfo f = fileManager.GetFile(doc.FileId);
                    if (f == null)
                    {
                        //file seems to have been deleted
                        LuceneController.Instance.Delete(LuceneMappingUtils.CreateLuceneItem(doc.PortalId, doc.FileId));
                        total -= 1;
                    }
                    else
                    {
                        if (f.FileName == "_folder.jpg")
                        {
                            continue; // skip
                        }
                        dynamic title  = null;
                        var     custom = GetCustomFileDataAsDynamic(f);
                        if (custom != null && custom.meta != null)
                        {
                            try
                            {
                                title = Normalize.DynamicValue(custom.meta.title, "");
                            }
                            catch (Exception ex)
                            {
                                Log.Logger.Debug("OpenFiles.JplistApiController.List() Failed to get title.", ex);
                            }
                        }

                        data.Add(new FileDTO()
                        {
                            Id                 = f.FileId,
                            Name               = Normalize.DynamicValue(title, f.FileName),
                            FileName           = f.FileName,
                            CreatedOnDate      = f.CreatedOnDate,
                            LastModifiedOnDate = f.LastModifiedOnDate,
                            FolderName         = f.Folder,
                            Url                = fileManager.GetUrl(f),
                            IsImage            = fileManager.IsImageFile(f),
                            ImageUrl           = ImageHelper.GetImageUrl(f, ratio),
                            Custom             = custom,
                            IconUrl            = GetFileIconUrl(f.Extension),
                            IsEditable         = IsEditable,
                            EditUrl            = IsEditable ? GetFileEditUrl(f) : ""
                        });
                    }
                }

                var res = new ResultExtDTO <FileDTO>()
                {
                    data = new ResultDataDTO <FileDTO>()
                    {
                        items       = data,
                        breadcrumbs = breadcrumbs.Select(f => new ResultBreadcrumbDTO
                        {
                            name = f.FolderName,
                            path = f.FolderPath.Trim('/')
                        })
                    },
                    count = total
                };
                return(Request.CreateResponse(HttpStatusCode.OK, res));
            }
            catch (Exception exc)
            {
                Log.Logger.Error(exc);
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc));
            }
        }