internal static PublishedContentSet<IPublishedContent> ConvertSearchResultToPublishedContent(this IEnumerable<SearchResult> results, ContextualPublishedCache cache) { //TODO: The search result has already returned a result which SHOULD include all of the data to create an IPublishedContent, // however this is currently not the case: // http://examine.codeplex.com/workitem/10350 var list = new List<IPublishedContent>(); var set = new PublishedContentSet<IPublishedContent>(list); foreach (var result in results.OrderByDescending(x => x.Score)) { var content = cache.GetById(result.Id); if (content == null) continue; // skip if this doesn't exist in the cache // need to extend the content as we're going to add a property to it, // and we should not ever do it to the content we get from the cache, // precisely because it is cached and shared by all requests. // but we cannot wrap it because we need to respect the type that was // returned by the cache, in case the cache can create real types. // so we have to ask it to please extend itself. list.Add(content); var extend = set.MapContent(content); var property = new PropertyResult("examineScore", result.Score, Guid.Empty, PropertyResultType.CustomProperty); extend.AddProperty(property); } return set; }
// note: I'm not sure this class fully complies with IPublishedContent rules especially // I'm not sure that _properties contains all properties including those without a value, // neither that GetProperty will return a property without a value vs. null... @zpqrtbnk public DictionaryPublishedContent( IDictionary<string, string> valueDictionary, Func<DictionaryPublishedContent, IPublishedContent> getParent, Func<DictionaryPublishedContent, IEnumerable<IPublishedContent>> getChildren, Func<DictionaryPublishedContent, string, IPublishedProperty> getProperty, bool fromExamine) { if (valueDictionary == null) throw new ArgumentNullException("valueDictionary"); if (getParent == null) throw new ArgumentNullException("getParent"); if (getProperty == null) throw new ArgumentNullException("getProperty"); _getParent = getParent; _getChildren = getChildren; _getProperty = getProperty; LoadedFromExamine = fromExamine; ValidateAndSetProperty(valueDictionary, val => _id = int.Parse(val), "id", "nodeId", "__NodeId"); //should validate the int! ValidateAndSetProperty(valueDictionary, val => _templateId = int.Parse(val), "template", "templateId"); ValidateAndSetProperty(valueDictionary, val => _sortOrder = int.Parse(val), "sortOrder"); ValidateAndSetProperty(valueDictionary, val => _name = val, "nodeName", "__nodeName"); ValidateAndSetProperty(valueDictionary, val => _urlName = val, "urlName"); ValidateAndSetProperty(valueDictionary, val => _documentTypeAlias = val, "nodeTypeAlias", UmbracoContentIndexer.NodeTypeAliasFieldName); ValidateAndSetProperty(valueDictionary, val => _documentTypeId = int.Parse(val), "nodeType"); ValidateAndSetProperty(valueDictionary, val => _writerName = val, "writerName"); ValidateAndSetProperty(valueDictionary, val => _creatorName = val, "creatorName", "writerName"); //this is a bit of a hack fix for: U4-1132 ValidateAndSetProperty(valueDictionary, val => _writerId = int.Parse(val), "writerID"); ValidateAndSetProperty(valueDictionary, val => _creatorId = int.Parse(val), "creatorID", "writerID"); //this is a bit of a hack fix for: U4-1132 ValidateAndSetProperty(valueDictionary, val => _path = val, "path", "__Path"); ValidateAndSetProperty(valueDictionary, val => _createDate = ParseDateTimeValue(val), "createDate"); ValidateAndSetProperty(valueDictionary, val => _updateDate = ParseDateTimeValue(val), "updateDate"); ValidateAndSetProperty(valueDictionary, val => _level = int.Parse(val), "level"); ValidateAndSetProperty(valueDictionary, val => { int pId; ParentId = -1; if (int.TryParse(val, out pId)) { ParentId = pId; } }, "parentID"); _contentType = PublishedContentType.Get(PublishedItemType.Media, _documentTypeAlias); _properties = new Collection<IPublishedProperty>(); //loop through remaining values that haven't been applied foreach (var i in valueDictionary.Where(x => !_keysAdded.Contains(x.Key))) { IPublishedProperty property; // must ignore that one if (i.Key == "version") continue; if (i.Key.InvariantStartsWith("__")) { // no type for tha tone, dunno how to convert property = new PropertyResult(i.Key, i.Value, Guid.Empty, PropertyResultType.CustomProperty); } else { // use property type to ensure proper conversion var propertyType = _contentType.GetPropertyType(i.Key); property = new XmlPublishedProperty(propertyType, false, i.Value); // false :: never preview a media } _properties.Add(property); } }