Exemplo n.º 1
0
        protected override void CreateChildControls()
        {
            var settings = new SummarySettings(SummaryMode.Words, WordLength, false);

            Text = SummaryParser.GetSummary(Text, settings);
            base.CreateChildControls();
        }
Exemplo n.º 2
0
        public static IEnumerable <ContentListSearchResult> GetSearchResults(string catalog, ContentListSearchCriteria criteria, int skip, int take, int summaryWordCount, out int hitCount)
        {
            var searchService = ServiceBus.ResolveService <LuceneSearchService>();

            var searchQuery = new SearchQuery();

            if (String.IsNullOrWhiteSpace(criteria.Term) && criteria.Topics == null && criteria.Types == null && string.IsNullOrEmpty(criteria.StartDate))
            {
                searchQuery.Text         = "a* OR b* OR c* OR d* OR e* OR f* OR g* OR h* OR i* OR j* OR k* OR l* OR m* OR n* OR o* OR p* OR r* OR s* OR t* OR u* OR v* OR w*";
                searchQuery.SearchFields = new[] { "Title" };

                if (string.IsNullOrEmpty(criteria.OrderBy))
                {
                    criteria.OrderBy = "PublishDate DESC";
                }
            }
            else
            {
                BuildSearchQuery(out searchQuery, criteria, criteria.SearchFieldNames);
            }

            searchQuery.IndexName         = catalog;
            searchQuery.HighlightedFields = null;
            searchQuery.Skip    = skip;
            searchQuery.Take    = take;
            searchQuery.OrderBy = criteria.OrderBy == null ? null : new string[] { criteria.OrderBy, "PublishDate DESC" };

            var resultSet = searchService.Search(searchQuery);


            hitCount = resultSet.HitCount;

            var results = new List <ContentListSearchResult>();

            foreach (var result in resultSet.SetContentLinks())
            {
                var searchResult = new ContentListSearchResult();
                foreach (var fieldName in criteria.FieldsToShow)
                {
                    switch (fieldName)
                    {
                    case "Title":
                        searchResult.Title = result.GetValue("Title");
                        break;

                    case "PublicationDate":
                        searchResult.PublicationDate = DateTime.Parse(result.GetValue("PublicationDate")).ToLocalTime();
                        break;

                    case "Link":
                        searchResult.Link = result.GetValue("Link");
                        break;

                    case "Type":
                        var type = result.GetValue("ContentType");
                        searchResult.Type = _searchTypes.Where(t => t.Item2 == type).FirstOrDefault().Item1;
                        break;

                    default:
                        searchResult.Summary = result.GetValue(fieldName);
                        if (string.IsNullOrWhiteSpace(searchResult.Summary))
                        {
                            var content = result.GetValue("Content").StripHtmlTags();
                            searchResult.Summary = SummaryParser.GetSummary(content, new SummarySettings(SummaryMode.Words, summaryWordCount, true, true));
                        }
                        break;
                    }
                }
                results.Add(searchResult);
            }

            return(results);
        }
        //***********Helper Methods***********//
        //************************************//
        //************************************//
        #region SetBlendedListItems

        private List <BlendedListItem> SetBlendedListItems(IResultSet results)
        {
            log.Info("Begin SetBlendedListItems.");
            var resultSet       = new List <BlendedListItem>();
            var taxonomyManager = TaxonomyManager.GetManager();

            log.Debug("Iterating through the results.");
            foreach (IDocument result in results)
            {
                log.Trace("Getting a new instance of BlendedListItem.");
                var newsResult = new BlendedListItem();

                log.Trace(() => String.Join("\n", result.Fields.Select(f => f.Name + ": " + f.Value)));

                if (result.Fields.Any(x => x.Name == "Title"))
                {
                    newsResult.Title = result.GetValue("Title").ToString();
                    log.Trace("Title Set.");
                }

                if (result.Fields.Any(x => x.Name == "LocationStreet"))
                {
                    newsResult.LocationStreet = result.GetValue("LocationStreet").ToString();
                    log.Trace("LocationStreet Set.");
                }

                if (result.Fields.Any(x => x.Name == "LocationState"))
                {
                    newsResult.LocationState = result.GetValue("LocationState").ToString();
                    log.Trace("LocationState Set.");
                }

                if (result.Fields.Any(x => x.Name == "LocationCity"))
                {
                    newsResult.LocationCity = result.GetValue("LocationCity").ToString();
                    log.Trace("LocationCity Set.");
                }

                if (result.Fields.Any(x => x.Name == "Summary"))
                {
                    newsResult.Summary = result.GetValue("Summary").ToString();
                    log.Trace("Summary Set.");
                }

                if (result.Fields.Any(x => x.Name == "Content"))
                {
                    newsResult.Content = result.GetValue("Content").ToString();
                    log.Trace("Content Set.");
                }

                var featured = result.GetValue("FeaturedRank").ToString() == "1" ? true : false;
                newsResult.Featured = featured;

                if (result.Fields.Any(x => x.Name == "Link"))
                {
                    newsResult.Link = result.GetValue("Link").ToString();
                    log.Trace("Link  Set.");
                }

                if (result.Fields.Any(x => x.Name == "EventStart"))
                {
                    try
                    {
                        DateTime dp = DateTime.MinValue;
                        //newsResult.EventStartDate = DateTime.ParseExact(result.GetValue("EventStart").ToString(), "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
                        DateTime.TryParseExact(result.GetValue("EventStart").ToString(), "MM/dd/yyyy HH:mm:ss", new CultureInfo("en-US"), DateTimeStyles.None, out dp);
                        newsResult.EventStartDate = dp;
                        //log.Trace("EventStart Set.");
                    }
                    catch (Exception ex)
                    {
                        log.Error("Source: {0}", ex.Source);
                        log.Error("Stack Trace: {0}", ex.StackTrace);
                        log.Error("Message: {0}", ex.Message);
                    }
                }

                if (result.Fields.Any(x => x.Name == "EventEnd"))
                {
                    try
                    {
                        /*newsResult.EventEndDate = DateTime.ParseExact(result.GetValue("EventStart").ToString(), "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
                         * log.Trace("EventStart Set.");*/
                        DateTime dp = DateTime.MinValue;
                        DateTime.TryParseExact(result.GetValue("EventEnd").ToString(), "MM/dd/yyyy HH:mm:ss", new CultureInfo("en-US"), DateTimeStyles.None, out dp);
                        newsResult.EventEndDate = dp;
                    }
                    catch (Exception ex)
                    {
                        log.Error("Source: {0}", ex.Source);
                        log.Error("Stack Trace: {0}", ex.StackTrace);
                        log.Error("Message: {0}", ex.Message);
                    }
                }

                if (result.Fields.Any(x => x.Name == "PublishDate"))
                {
                    try
                    {
                        /*newsResult.PublicationDate = DateTime.ParseExact(result.GetValue("PublishDate").ToString(), "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
                         * log.Trace("PublishDate Set.");*/
                        DateTime dp = DateTime.MinValue;
                        DateTime.TryParseExact(result.GetValue("PublishDate").ToString(), "MM/dd/yyyy HH:mm:ss", new CultureInfo("en-US"), DateTimeStyles.None, out dp);
                        newsResult.PublicationDate = dp;
                    }
                    catch (Exception ex)
                    {
                        log.Error("Source: {0}", ex.Source);
                        log.Error("Stack Trace: {0}", ex.StackTrace);
                        log.Error("Message: {0}", ex.Message);
                    }
                }

                if (result.Fields.Any(x => x.Name == "CategoryIds"))
                {
                    newsResult.Categories = new List <CategoryPair>();

                    var categoryIds = result.GetValue("CategoryIds").ToString().Split(',').ToList();
                    foreach (var categoryItem in categoryIds)
                    {
                        try
                        {
                            if (!categoryItem.IsNullOrWhitespace())
                            {
                                var categoryPair = new CategoryPair();
                                if (categoryItem.IsGuid())
                                {
                                    categoryPair.Guid = categoryItem;
                                    var taxon = taxonomyManager.GetTaxon(Guid.Parse(categoryItem));
                                    categoryPair.Name = taxon.Title.Value;
                                    newsResult.Categories.Add(categoryPair);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            log.Error("Source: {0}", ex.Source);
                            log.Error("Stack Trace: {0}", ex.StackTrace);
                            log.Error("Message: {0}", ex.Message);
                        }
                    }
                    //newsResult.Categories = result.GetValue("CategoryList").ToString().Split(',').Select(x => x.Trim()).ToList();
                    //log.Trace("CategoryList Set.");
                }

                if (result.Fields.Any(x => x.Name == "ResourceTypesIds"))
                {
                    newsResult.ResourceTypes = new List <CategoryPair>();

                    var resourceTypesIds = result.GetValue("ResourceTypesIds").ToString().Split(',').ToList();
                    foreach (var resourceTypeItem in resourceTypesIds)
                    {
                        try
                        {
                            if (!resourceTypeItem.IsNullOrWhitespace())
                            {
                                var categoryPair = new CategoryPair();
                                if (resourceTypeItem.IsGuid())
                                {
                                    categoryPair.Guid = resourceTypeItem;
                                    var taxon = taxonomyManager.GetTaxon(Guid.Parse(resourceTypeItem));
                                    categoryPair.Name = taxon.Title.Value;
                                    newsResult.Categories.Add(categoryPair);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            log.Error("Source: {0}", ex.Source);
                            log.Error("Stack Trace: {0}", ex.StackTrace);
                            log.Error("Message: {0}", ex.Message);
                        }
                    }
                    //newsResult.ResourceTypes = result.GetValue("ResourceTypesList").ToString().Split(',').Select(x => x.Trim()).ToList();
                    //log.Trace("ResourceTypesList Set.");
                }

                if (result.Fields.Any(x => x.Name == "OrganizationalAuthorsIds"))
                {
                    newsResult.OrganizationalAuthors = new List <CategoryPair>();

                    var organizationalAuthorIds = result.GetValue("OrganizationalAuthorsIds").ToString().Split(',').ToList();
                    foreach (var organizationalAuthorItem in organizationalAuthorIds)
                    {
                        try
                        {
                            if (!organizationalAuthorItem.IsNullOrWhitespace())
                            {
                                var categoryPair = new CategoryPair();
                                if (organizationalAuthorItem.IsGuid())
                                {
                                    categoryPair.Guid = organizationalAuthorItem;
                                    var taxon = taxonomyManager.GetTaxon(Guid.Parse(organizationalAuthorItem));
                                    categoryPair.Name = taxon.Title.Value;
                                    newsResult.Categories.Add(categoryPair);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            log.Error("Source: {0}", ex.Source);
                            log.Error("Stack Trace: {0}", ex.StackTrace);
                            log.Error("Message: {0}", ex.Message);
                        }
                    }
                    //newsResult.OrganizationalAuthors = result.GetValue("OrganizationalAuthorsList").ToString().Split(',').Select(x => x.Trim()).ToList();
                    //log.Trace("OrganizationalAuthorsList Set.");
                }

                if (result.Fields.Any(x => x.Name == "DefaultLinkBase"))
                {
                    newsResult.DefaultLinkBase = result.GetValue("DefaultLinkBase").ToString();
                    log.Trace("DefaultLinkBase Set.");
                }

                if (result.Fields.Any(x => x.Name == "Selfpaced"))
                {
                    var selfPaced = result.GetValue("Selfpaced").ToString();
                    if (!selfPaced.IsNullOrWhitespace())
                    {
                        newsResult.SelfPaced = bool.Parse(result.GetValue("Selfpaced").ToString());
                        log.Trace("SelfPaced Set.");
                    }
                }

                if (result.Fields.Any(x => x.Name == "DisplayDate"))
                {
                    newsResult.DisplayDate = result.GetValue("DisplayDate").ToString();
                    log.Trace("DisplayDate Set.");
                }

                if (result.Fields.Any(x => x.Name == "DateField"))
                {
                    try
                    {
                        /*newsResult.DateField = DateTime.ParseExact(result.GetValue("DateField").ToString(), "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
                         * log.Trace("DateField Set.");*/
                        DateTime dp = DateTime.MinValue;
                        DateTime.TryParseExact(result.GetValue("DateField").ToString(), "MM/dd/yyyy HH:mm:ss", new CultureInfo("en-US"), DateTimeStyles.None, out dp);
                        newsResult.DateField = dp;
                    }
                    catch (Exception ex)
                    {
                        log.Error("Source: {0}", ex.Source);
                        log.Error("Stack Trace: {0}", ex.StackTrace);
                        log.Error("Message: {0}", ex.Message);
                    }
                }

                if (result.Fields.Any(x => x.Name == "ContentTypeDescription"))
                {
                    newsResult.ContentType = result.GetValue("ContentTypeDescription").ToString();
                    log.Trace("ContentTypeDescription Set.");
                }

                if (result.Fields.Any(x => x.Name == "ImageUrl"))
                {
                    newsResult.Image = result.GetValue("ImageUrl").ToString();
                    log.Trace("Image Set.");
                }

                if (result.Fields.Any(x => x.Name == "Protected"))
                {
                    newsResult.Protected = result.GetValue("Protected") == "1";
                    log.Trace("Protected Set.");
                }

                if (string.IsNullOrWhiteSpace(newsResult.Summary) && !string.IsNullOrWhiteSpace(newsResult.Content))
                {
                    log.Trace("The summary field is empty and the content has a value, so we will make a summary.");
                    newsResult.Summary = SummaryParser.GetSummary(newsResult.Content, new SummarySettings(SummaryMode.Words, 40, true));
                    log.Trace("Summary made and set.");
                }

                resultSet.Add(newsResult);
                log.Trace("result added to the resturn set.");
            }

            log.Info("Finishing SetBlendedListItems.");
            return(resultSet);
        }
Exemplo n.º 4
0
        private List <NewsResult> GetBlenedResult_org(out int hitCount, string term, int skip, int take)
        {
            var results = BlendedNewsHelper.GetNewsItems(Providers, SearchIndex, out hitCount, Categories, term, skip, take);

            log.InfoFormat("There are {0}, results, prov:{1}, idx:{2}, term:{3}, take:{4}",
                           results.HitCount,
                           Providers?.Join(","),
                           SearchIndex,
                           Categories?.Join(","),
                           term, take);

            List <NewsResult> resultSet = new List <NewsResult>();
            //log.Info("content, title, Id \r\n");
            StringBuilder sb = new StringBuilder();

            foreach (var result in results)
            {
                //newsResult.DisplayDate = result.GetValue("DisplayDate").ToString();
                //
                try
                {
                    var ctnType = result.GetValue("ContentType")?.ToString() ?? string.Empty;

                    var        featured   = result.GetValue("FeaturedRank")?.ToString() ?? string.Empty;
                    NewsResult newsResult = new NewsResult()
                    {
                        ImageId  = result.GetValue("ImageId")?.ToString() ?? string.Empty,
                        Title    = result.GetValue("Title")?.ToString() ?? string.Empty,
                        Summary  = result.GetValue("Summary")?.ToString() ?? string.Empty,
                        Featured = !string.IsNullOrEmpty(featured) && (featured == "1" ? true : false),
                        Content  = result.GetValue("Content")?.ToString() ?? string.Empty,
                        Link     = result.GetValue("Link")?.ToString() ?? string.Empty
                    };

                    var    dynaManager = DynamicModuleManager.GetManager();
                    string txtId       = result.GetValue("Id")?.ToString() ?? String.Empty;
                    log.InfoFormat("try to get from iD:{0} ofType:{1}-title:{2}", txtId, ctnType, newsResult.Title);
                    try
                    {
                        if (!string.IsNullOrEmpty(ctnType) && !string.IsNullOrEmpty(txtId) && txtId.IsGuid())
                        {
                            Guid itemId = new Guid(txtId);
                            if (ctnType.IndexOf("BlogPost") > 0)
                            {
                                var manager = BlogsManager.GetManager();
                                var bp      = manager.GetBlogPosts()
                                              //.Where(c => c.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live)
                                              .FirstOrDefault(b => b.Id == itemId);
                                if (bp != null)
                                {
                                    bp = manager.Lifecycle.GetLive(bp) as BlogPost;
                                    log.InfoFormat("has item:{0}-date:{1}", bp?.GetType()?.FullName, bp?.GetValue("DisplayDate")?.ToString() ?? string.Empty);
                                    newsResult.DisplayDate = bp?.GetValue("DisplayDate")?.ToString() ?? string.Empty;
                                }
                            }
                            else
                            {
                                var item = dynaManager.GetDataItems(TypeResolutionService.ResolveType(ctnType))
                                           .Where(c => c.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live)
                                           .FirstOrDefault(c => c.Id == itemId);

                                log.InfoFormat("has item:{0}-date:{1}", item?.GetType()?.FullName, item?.GetValue("DisplayDate")?.ToString() ?? string.Empty);
                                newsResult.DisplayDate = item?.GetValue("DisplayDate")?.ToString() ?? string.Empty;
                            }

                            /*if (result.Fields.Any(x => x.Name == "DisplayDate") && item?.GetValue("DisplayDate") != null)
                             * {
                             *  newsResult.DisplayDate = item.GetValue("DisplayDate").ToString() ?? string.Empty;
                             * }*/
                        }
                    }
                    catch (Exception ex)
                    {
                        log.InfoFormat("failedToGetDateItemType:{0}-id:{1}, msg:{2}", ctnType, txtId, ex.Message);
                    }

                    string formatString = "yyyyMMddHHmmssfff";
                    // seem like there are already assing below
                    if (!String.IsNullOrEmpty(result.GetValue("PublicationDate")?.ToString()))
                    {
                        DateTime pubd = DateTime.MinValue;
                        //DateTime dt = DateTime.ParseExact(result.GetValue("PublicationDate").ToString(), formatString, null);
                        //newsResult.PublicationDate = dt.ToLocalTime();
                        DateTime.TryParseExact(result.GetValue("PublicationDate").ToString(), formatString, new CultureInfo("en-US"), DateTimeStyles.None, out pubd);
                        newsResult.PublicationDate = pubd;
                        //log.InfoFormat("pubd:{0}", pubd.ToString("MMMM d, yyyy"));
                        //sb.Append("PubDate:").Append(pubd).Append("\r");
                    }

                    if (result.Fields.Any(x => x.Name == "DateField"))
                    {
                        try
                        {
                            /*DateTime dt2 = DateTime.ParseExact(result.GetValue("DateField").ToString(), "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
                             * newsResult.DateField = dt2.ToLocalTime();*/
                            if (!String.IsNullOrEmpty(result.GetValue("DateField")?.ToString()))
                            {
                                DateTime eDateTime = DateTime.MinValue;
                                DateTime.TryParseExact(result.GetValue("DateField").ToString(), "MM/dd/yyyy HH:mm:ss", new CultureInfo("en-US"), DateTimeStyles.None, out eDateTime);
                                newsResult.DateField = eDateTime;
                                //log.InfoFormat("datef:{0}", eDateTime.ToString("MMMM d, yyyy"));
                            }
                        }
                        catch (Exception ex)
                        {
                            log.ErrorFormat("GetBlendedResult-DateField:{0}, inner:{1}, stack:{2}", ex.Message, ex.InnerException?.Message, ex.StackTrace);
                        }
                    }

                    if (string.IsNullOrWhiteSpace(newsResult.Summary) && !string.IsNullOrWhiteSpace(newsResult.Content))
                    {
                        newsResult.Summary = SummaryParser.GetSummary(newsResult.Content, new SummarySettings(SummaryMode.Words, 40, true));
                    }

                    resultSet.Add(newsResult);
                }
                catch (Exception ex)
                {
                    log.InfoFormat("bled-createNewsResult:{0}", ex.Message);
                }



                //newsResult.ImageCaption = result.GetValue("ImageCaption").ToString();

                // testing below

                /*if (!String.IsNullOrEmpty(result.GetValue("PublicationDate")?.ToString()))
                 * {
                 *  //DateTime dt = DateTime.ParseExact(result.GetValue("PublicationDate").ToString(), formatString, null);
                 *  //newsResult.PublicationDate = dt.ToLocalTime();
                 *  DateTime.TryParseExact(result.GetValue("PublicationDate").ToString(), formatString, new CultureInfo("en-US"), DateTimeStyles.None, out eDateTime);
                 *  newsResult.DisplayDate = eDateTime.ToLocalTime().ToShortDateString();
                 * }*/
                ///
            }
            return(resultSet);
        }
Exemplo n.º 5
0
        //***********Helper Methods***********//
        //************************************//
        //************************************//
        #region SetBlendedListItems

        private List <BlendedListItem> SetBlendedListItems(IEnumerable <IDocument> results)
        {
            log.Info("Begin SetBlendedListItems");


            var resultSet       = new List <BlendedListItem>();
            var taxonomyManager = TaxonomyManager.GetManager();

            if (results != null)
            {
                foreach (var result in results)
                {
                    //log.Trace("Getting a new instance of BlendedListItem.");
                    var featured   = result.Fields.Any(c => c.Name == "FeaturedRank") && (result.GetValue("FeaturedRank")?.ToString() == "1" ? true : false);
                    var newsResult = new BlendedListItem()
                    {
                        Title           = result.GetValue("Title")?.ToString() ?? string.Empty,
                        LocationStreet  = result.Fields.Any(c => c.Name == "LocationStreet") ? (result.GetValue("LocationStreet")?.ToString() ?? string.Empty):String.Empty,
                        LocationState   = result.Fields.Any(c => c.Name == "LocationState") ? result.GetValue("LocationState")?.ToString() ?? string.Empty: string.Empty,
                        LocationCity    = result.Fields.Any(c => c.Name == "LocationCity") ? result.GetValue("LocationCity")?.ToString() ?? string.Empty: string.Empty,
                        Summary         = result.GetValue("Summary")?.ToString() ?? string.Empty,
                        Content         = result.GetValue("Content")?.ToString() ?? String.Empty,
                        Featured        = featured,
                        Link            = result.Fields.Any(c => c.Name == "Link") ? result.GetValue("Link")?.ToString() ?? string.Empty: string.Empty,
                        DefaultLinkBase = result.Fields.Any(c => c.Name == "DefaultLinkBase") ? result.GetValue("DefaultLinkBase")?.ToString() ?? string.Empty: string.Empty,
                        DisplayDate     = result.Fields.Any(c => c.Name == "DisplayDate") ? result.GetValue("DisplayDate")?.ToString() ?? string.Empty: string.Empty,
                        ContentType     = result.Fields.Any(c => c.Name == "ContentTypeDescription") ? result.GetValue("ContentTypeDescription")?.ToString() ?? String.Empty: string.Empty,
                        Image           = result.Fields.Any(c => c.Name == "ImageUrl") ? result.GetValue("ImageUrl")?.ToString() ?? string.Empty: string.Empty
                    };


                    if (result.Fields.Any(x => x.Name == "Selfpaced"))
                    {
                        var selfPaced = result.GetValue("Selfpaced")?.ToString() ?? String.Empty;
                        if (!selfPaced.IsNullOrWhitespace())
                        {
                            newsResult.SelfPaced = bool.Parse(result.GetValue("Selfpaced").ToString());
                        }
                    }
                    if (result.Fields.Any(x => x.Name == "DateField"))
                    {
                        if (result.Fields.Any(x => x.Name == "EventStart"))
                        {
                            DateTime dt;
                            if (!String.IsNullOrEmpty(result.GetValue("DateField")?.ToString()))
                            {
                                //newsResult.DateField = DateTime.ParseExact(result.GetValue("DateField").ToString(), "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

                                DateTime.TryParseExact(result.GetValue("DateField").ToString(), "MM/dd/yyyy HH:mm:ss", new CultureInfo("en-US"), DateTimeStyles.None, out dt);
                                newsResult.DateField = dt;
                            }
                        }
                    }

                    if (result.Fields.Any(x => x.Name == "Protected"))
                    {
                        string IsProtected = result.GetValue("Protected")?.ToString() ?? "0";
                        newsResult.Protected = !string.IsNullOrEmpty(IsProtected) && (IsProtected == "1" ? true : false);
                    }

                    DateTime eDateTime;
                    if (result.Fields.Any(x => x.Name == "EventStart"))
                    {
                        if (!String.IsNullOrEmpty(result.GetValue("EventStart")?.ToString()))
                        {
                            //ParseExact(result.GetValue("EventStart").ToString(), "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
                            DateTime.TryParseExact(result.GetValue("EventStart").ToString(), "MM/dd/yyyy HH:mm:ss", new CultureInfo("en-US"), DateTimeStyles.None, out eDateTime);
                            newsResult.EventStartDate = eDateTime;
                        }
                    }

                    if (result.Fields.Any(x => x.Name == "EventEnd"))
                    {
                        //newsResult.EventEndDate = DateTime.ParseExact(result.GetValue("EventStart").ToString(), "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
                        if (!String.IsNullOrEmpty(result.GetValue("EventEnd")?.ToString()))
                        {
                            DateTime.TryParseExact(result.GetValue("EventEnd").ToString(), "MM/dd/yyyy HH:mm:ss", new CultureInfo("en-US"), DateTimeStyles.None, out eDateTime);
                            newsResult.EventEndDate = eDateTime;
                        }
                    }

                    if (result.Fields.Any(x => x.Name == "PublishDate"))
                    {
                        //newsResult.PublicationDate = DateTime.ParseExact(result.GetValue("PublishDate").ToString(), "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
                        if (!String.IsNullOrEmpty(result.GetValue("PublishDate")?.ToString()))
                        {
                            DateTime.TryParseExact(result.GetValue("PublishDate").ToString(), "MM/dd/yyyy HH:mm:ss", new CultureInfo("en-US"), DateTimeStyles.None, out eDateTime);
                            newsResult.PublicationDate = eDateTime;
                        }
                    }

                    if (result.Fields.Any(x => x.Name == "CategoryIds"))
                    {
                        newsResult.Categories = new List <CategoryPair>();

                        var categoryIds = result.GetValue("CategoryIds")?.ToString()?.Split(',')?.ToList();
                        if (categoryIds != null && categoryIds.Any())
                        {
                            foreach (var categoryItem in categoryIds)
                            {
                                try
                                {
                                    var categoryPair = new CategoryPair();
                                    if (!String.IsNullOrEmpty(categoryItem) && categoryItem.IsGuid())
                                    {
                                        categoryPair.Guid = categoryItem;
                                        var taxon = taxonomyManager.GetTaxon(Guid.Parse(categoryItem));
                                        categoryPair.Name = taxon.Title.Value;
                                        newsResult.Categories.Add(categoryPair);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    log.Error("Source: {0}", ex.Source);
                                    log.Error("Stack Trace: {0}", ex.StackTrace);
                                    log.Error("RelatedContentDyn-GetCategoryIds: {0}", ex.Message);
                                }
                            }
                        }

                        //newsResult.Categories = result.GetValue("CategoryList").ToString().Split(',').Select(x => x.Trim()).ToList();
                        //log.Trace("CategoryList Set.");
                    }

                    if (result.Fields.Any(x => x.Name == "ResourceTypesIds"))
                    {
                        newsResult.ResourceTypes = new List <CategoryPair>();

                        var resourceTypesIds = result.GetValue("ResourceTypesIds")?.ToString()?.Split(',')?.ToList();
                        if (resourceTypesIds != null && resourceTypesIds.Any())
                        {
                            foreach (var resourceTypeItem in resourceTypesIds)
                            {
                                try
                                {
                                    var categoryPair = new CategoryPair();
                                    if (!String.IsNullOrEmpty(resourceTypeItem) && resourceTypeItem.IsGuid())
                                    {
                                        categoryPair.Guid = resourceTypeItem;
                                        var taxon = taxonomyManager.GetTaxon(Guid.Parse(resourceTypeItem));
                                        categoryPair.Name = taxon.Title.Value;
                                        newsResult.Categories.Add(categoryPair);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    log.Error("Source: {0}", ex.Source);
                                    log.Error("Stack Trace: {0}", ex.StackTrace);
                                    log.Error("RelatedContentDyn-GetResourceTypesIds: {0}", ex.Message);
                                }
                            }
                        }

                        //newsResult.ResourceTypes = result.GetValue("ResourceTypesList").ToString().Split(',').Select(x => x.Trim()).ToList();
                        //log.Trace("ResourceTypesList Set.");
                    }

                    if (result.Fields.Any(x => x.Name == "OrganizationalAuthorsIds"))
                    {
                        newsResult.OrganizationalAuthors = new List <CategoryPair>();

                        var organizationalAuthorIds = result.GetValue("OrganizationalAuthorsIds")?.ToString()?.Split(',')?.ToList();
                        if (organizationalAuthorIds != null && organizationalAuthorIds.Any())
                        {
                            foreach (var organizationalAuthorItem in organizationalAuthorIds)
                            {
                                try
                                {
                                    var categoryPair = new CategoryPair();
                                    if (!String.IsNullOrEmpty(organizationalAuthorItem) && organizationalAuthorItem.IsGuid())
                                    {
                                        categoryPair.Guid = organizationalAuthorItem;
                                        var taxon = taxonomyManager.GetTaxon(Guid.Parse(organizationalAuthorItem));
                                        categoryPair.Name = taxon.Title.Value;
                                        newsResult.Categories.Add(categoryPair);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    log.Error("Source: {0}", ex.Source);
                                    log.Error("Stack Trace: {0}", ex.StackTrace);
                                    log.Error("RelatedContentDyn-GetOrganizationalAuthorsIds: {0}", ex.Message);
                                }
                            }
                        }

                        //newsResult.OrganizationalAuthors = result.GetValue("OrganizationalAuthorsList").ToString().Split(',').Select(x => x.Trim()).ToList();
                        //log.Trace("OrganizationalAuthorsList Set.");
                    }

                    if (string.IsNullOrWhiteSpace(newsResult.Summary) && !string.IsNullOrWhiteSpace(newsResult.Content))
                    {
                        newsResult.Summary = SummaryParser.GetSummary(newsResult.Content, new SummarySettings(SummaryMode.Words, 40, true));
                    }

                    resultSet.Add(newsResult);
                }
            }
            return(resultSet);
        }
Exemplo n.º 6
0
        public static IEnumerable <SearchResult> GetSearchResults_stv(string catalog, SearchCriteria criteria, int skip, int take, int summaryWordCount, out int hitCount)
        {
            var searchService = ServiceBus.ResolveService <ISearchService>();
            var searchQuery   = ObjectFactory.Resolve <ISearchQuery>();

            var           queryBuilder = ObjectFactory.Resolve <IQueryBuilder>();
            StringBuilder sb           = new StringBuilder();


            searchQuery.Skip              = skip;
            searchQuery.Take              = take;
            searchQuery.OrderBy           = criteria.OrderBy == null ? new string[] { "PublicationDate DESC" } : new string[] { criteria.OrderBy };
            searchQuery.HighlightedFields = null;
            searchQuery.IndexName         = catalog;
            searchQuery.SearchGroup       = BuildSearchQuery(criteria, new[] { "Title", "Content", "DocumentText" });

            var resultSet = searchService.Search(searchQuery);

            hitCount = resultSet.HitCount;

            var results         = new List <SearchResult>();
            var taxonomyManager = TaxonomyManager.GetManager();

            log.InfoFormat("ResourceLib:{0};",
                           criteria.ToQueryString());

            foreach (var result in resultSet.SetContentLinks())
            {
                var searchResult = new SearchResult();

                try
                {
                    if (!SitefinityExtensions.IsNullOrEmpty(result.GetValue("CategoryIds").ToString()))
                    {
                        var categoryIds = result.GetValue("CategoryIds").ToString().Split(' ').ToList();

                        searchResult.CategoryPair = new List <CategoryPair>();
                        var parentGuidId = AppSettingsUtility.GetValue <Guid>("ResourceLibraryTaxonomyId");

                        foreach (var categoryId in categoryIds)
                        {
                            log.InfoFormat("ResourceLibraryTaxonomyId-cateids:{0}, parentGuidId:{1}", categoryIds, parentGuidId);
                            var category = taxonomyManager.GetTaxon(Guid.Parse(categoryId)) as HierarchicalTaxon;

                            do
                            {
                                if (category.Parent != null && category.Parent.Id == parentGuidId)
                                {
                                    category = taxonomyManager.GetTaxon(Guid.Parse(categoryId)) as HierarchicalTaxon;
                                    var categoryPair = new CategoryPair();
                                    categoryPair.Name = category.Title.Value;
                                    categoryPair.Guid = categoryId.ToString().Replace("-", "");

                                    log.InfoFormat("CateInParent, Title:{0}, guid:{1}", categoryPair.Name, categoryPair.Guid);
                                    sb.Append(String.Format("CateInParent, Title:{0}, guid:{1}", categoryPair.Name,
                                                            categoryPair.Guid));
                                    searchResult.CategoryPair.Add(categoryPair);
                                    break;
                                }

                                category = taxonomyManager.GetTaxon(category.Parent.Id) as HierarchicalTaxon;
                            } while (category.Parent != null);
                        }
                    }
                    if (!SitefinityExtensions.IsNullOrEmpty(result.GetValue("OrganizationalAuthorsIds").ToString()))
                    {
                        var categoryIds = result.GetValue("OrganizationalAuthorsIds").ToString().Split(' ').ToList();
                        searchResult.Author = new List <CategoryPair>();
                        var taxonomyGuidId = AppSettingsUtility.GetValue <Guid>("ResourceLibraryTaxonomyAuthorId");

                        foreach (var categoryId in categoryIds)
                        {
                            var category = taxonomyManager.GetTaxon(Guid.Parse(categoryId)) as HierarchicalTaxon;

                            if (category.Taxonomy.Id == taxonomyGuidId)
                            {
                                var categoryPair = new CategoryPair();
                                categoryPair.Name = category.Title.Value;
                                categoryPair.Guid = categoryId.ToString().Replace("-", "");
                                searchResult.Author.Add(categoryPair);

                                log.InfoFormat("ResourceLibraryTaxonomyAuthorId-Title:{0}, guid:{1}", categoryPair.Name, categoryPair.Guid);
                                sb.AppendFormat("ResourceLibraryTaxonomyAuthorId-Title:{0}, guid:{1}", categoryPair.Name,
                                                categoryPair.Guid);
                            }
                        }
                    }
                    if (!SitefinityExtensions.IsNullOrEmpty(result.GetValue("ResourceTypesIds").ToString()))
                    {
                        var categoryIds = result.GetValue("ResourceTypesIds").ToString().Split(' ').ToList();
                        searchResult.ResourceTypes = new List <CategoryPair>();
                        var taxonomyGuidId = AppSettingsUtility.GetValue <Guid>("ResourceLibraryTaxonomyTypeId");

                        foreach (var categoryId in categoryIds)
                        {
                            var category = taxonomyManager.GetTaxon(Guid.Parse(categoryId)) as HierarchicalTaxon;

                            if (category.Taxonomy.Id == taxonomyGuidId)
                            {
                                var categoryPair = new CategoryPair();
                                categoryPair.Name = category.Title.Value;
                                categoryPair.Guid = categoryId.ToString().Replace("-", "");
                                searchResult.ResourceTypes.Add(categoryPair);

                                log.InfoFormat("ResourceLibraryTaxonomyTypeId-Title:{0}, guid:{1}", categoryPair.Name, categoryPair.Guid);
                                sb.AppendFormat("ResourceLibraryTaxonomyTypeId-Title:{0}, guid:{1}", categoryPair.Name, categoryPair.Guid);
                            }
                        }
                    }
                    if (!SitefinityExtensions.IsNullOrEmpty(result.GetValue("Title").ToString()))
                    {
                        searchResult.Title = result.GetValue("Title").ToString();
                    }
                    if (!SitefinityExtensions.IsNullOrEmpty(result.GetValue("Link").ToString()))
                    {
                        searchResult.Link = result.GetValue("Link").ToString();
                    }
                    if (!SitefinityExtensions.IsNullOrEmpty(result.GetValue("PublishDate").ToString()))
                    {
                        searchResult.PublicationDate = DateTime.Parse(result.GetValue("PublishDate").ToString());
                    }
                    if (!SitefinityExtensions.IsNullOrEmpty(result.GetValue("DocumentLibrary").ToString()))
                    {
                        searchResult.DocumentFolder = result.GetValue("DocumentLibrary").ToString();
                    }
                    if (!SitefinityExtensions.IsNullOrEmpty(result.GetValue("Summary").ToString()))
                    {
                        searchResult.Summary = result.GetValue("Summary").ToString();
                    }
                    if (!SitefinityExtensions.IsNullOrEmpty(result.GetValue("ContentType").ToString()))
                    {
                        searchResult.Type = result.GetValue("ContentType").ToString();
                    }

                    if (!SitefinityExtensions.IsNullOrEmpty(result.GetValue("ThumbnailUrl").ToString()))
                    {
                        searchResult.ImageUrl = result.GetValue("ThumbnailUrl").ToString();
                    }
                    else if (!SitefinityExtensions.IsNullOrEmpty(result.GetValue("ImageUrl").ToString()))
                    {
                        searchResult.ImageUrl = result.GetValue("ImageUrl").ToString();
                    }

                    var contentType = TypeResolutionService.ResolveType(searchResult.Type);

                    searchResult.Type = contentType.Name;



                    if (result.Fields.Any(f => f.Name == "EventStart") && !string.IsNullOrWhiteSpace(result.GetValue("EventStart").ToString()))
                    {
                        searchResult.EventStart = DateTime.Parse(result.GetValue("EventStart").ToString());
                    }

                    if (result.Fields.Any(f => f.Name == "EventEnd") && !string.IsNullOrWhiteSpace(result.GetValue("EventEnd").ToString()))
                    {
                        searchResult.EventEnd = DateTime.Parse(result.GetValue("EventEnd").ToString());
                    }
                    var content      = result.GetValue("Content").ToString().StripHtmlTags();
                    var docLink      = result.GetValue("DocumentLink");
                    var documentText = result.GetValue("DocumentText");
                    if (string.IsNullOrWhiteSpace(content) && !string.IsNullOrWhiteSpace(docLink.ToString()))
                    {
                        searchResult.Link = docLink.ToString();
                        content           = documentText.ToString();
                    }

                    if (string.IsNullOrWhiteSpace(searchResult.Summary))
                    {
                        searchResult.Summary = SummaryParser.GetSummary(content, new SummarySettings(SummaryMode.Words, summaryWordCount, true, true));
                    }
                }
                catch (Exception ex)
                {
                    //ex.ToString();
                    log.InfoFormat("Resources-Exception:{0}, details:{1}", ex.Message, sb.ToString());
                }

                results.Add(searchResult);
            }

            return(results);
        }