Exemple #1
0
        public override TElement MapToType <TElement>(Document document, Sitecore.ContentSearch.Linq.Methods.SelectMethod selectMethod, IEnumerable <IFieldQueryTranslator> virtualFieldProcessors, Sitecore.ContentSearch.Security.SearchSecurityOptions securityOptions)
        {
            // if the result type is not IStandardTemplateItem, use the default functionality
            if (!typeof(IStandardTemplateItem).IsAssignableFrom(typeof(TElement)))
            {
                return(base.MapToType <TElement>(document, selectMethod, virtualFieldProcessors, securityOptions));
            }

            // initializers can't really support sub-selects of objects. Error if that's what's being used.
            if (selectMethod != null)
            {
                throw new NotSupportedException("Using Select on a Synthesis object type is supported. Convert the query to a list or array before selecting, then select using LINQ to objects.");
            }

            var fields = ExtractFieldsFromDocument(document, virtualFieldProcessors);

            ShortID templateId;

            if (!fields.ContainsKey("_template") || !ShortID.TryParse(fields["_template"], out templateId))
            {
                templateId = ID.Null.ToShortID();
            }

            var result = Initializers.GetInitializer(templateId.ToID()).CreateInstanceFromSearch(fields);

            if (result is TElement)
            {
                return((TElement)result);
            }

            return(default(TElement));
        }
        public DocumentMappingResult <TElement> MapToType <TElement>(Func <Dictionary <string, string> > getFieldsMethod, SelectMethod selectMethod)
        {
            // if the result type is not IStandardTemplateItem, use the default functionality
            if (!IsSynthesisType <TElement>())
            {
                return(new DocumentMappingResult <TElement>(default(TElement), false));
            }

            // initializers can't really support sub-selects of objects. Error if that's what's being used.
            if (selectMethod != null)
            {
                throw new NotSupportedException("Using Select on a Synthesis object type is supported. Convert the query to a list or array before selecting, then select using LINQ to objects.");
            }

            var evaluatedFields = getFieldsMethod();

            ShortID templateId;

            if (!evaluatedFields.ContainsKey("_template") || !ShortID.TryParse(evaluatedFields["_template"], out templateId))
            {
                templateId = ID.Null.ToShortID();
            }

            var initializer = _overrideInitializer ?? ProviderResolver.FindGlobalInitializer(templateId.ToID());

            var result = initializer.CreateInstanceFromSearch(evaluatedFields);

            if (result is TElement)
            {
                return(new DocumentMappingResult <TElement>((TElement)result, true));
            }

            return(new DocumentMappingResult <TElement>(default(TElement), true));           // note that this is still 'success', because we mapped onto a Synthesis type so we do not want to use default mapping
        }
Exemple #3
0
        internal virtual void ParseCurrentBaseLayout()
        {
            ShortID id;
            var     value = GetQueryString(CurrentBaseLayoutQueryKey);

            if (!string.IsNullOrEmpty(value) && ShortID.TryParse(value, out id))
            {
                CurrentBaseLayoutId = id.ToID();
            }
        }
Exemple #4
0
        /// <summary>
        /// Converts a GUID into ShortID.
        /// </summary>
        /// <param name="guid">String that contains a GUID.</param>
        /// <returns></returns>
        public static string NormalizeGuid(string guid)
        {
            // checking if it is not a short guid
            if (!String.IsNullOrEmpty(guid))
            {
                ShortID result;
                if (ShortID.TryParse(guid, out result))
                {
                    return(result.ToString().ToLowerInvariant());
                }
            }

            return(guid);
        }
 internal static ID[] ParseIndexIdList(string indexValue)
 {
     return(indexValue.Split('|')
            .Select(x =>
     {
         ShortID id;
         if (!ShortID.TryParse(x, out id))
         {
             return null;
         }
         return id.ToID();
     })
            .Where(x => x != (ID)null)
            .ToArray());
 }
        protected virtual string GetFilterLabel(string filterKey)
        {
            if (ID.TryParse(filterKey, out var id))
            {
                var item = Context.Database.GetItem(id);
                return(item?.DisplayName ?? filterKey);
            }

            if (ShortID.TryParse(filterKey, out var shortId))
            {
                var item = Context.Database.GetItem(shortId.ToID());
                return(item?.DisplayName ?? filterKey);
            }

            return(filterKey);
        }
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            object model = base.BindModel(controllerContext, bindingContext);

            if (typeof(IFormModel).IsAssignableFrom(bindingContext.ModelType))
            {
                var formModel = model as IFormModel;
                Assert.IsNotNull(formModel, "viewModel must not be null");
                var     formDataValue = controllerContext.HttpContext.Request["__AjaxFormData"];
                ShortID dataSourceId;
                if (ShortID.TryParse(formDataValue, out dataSourceId))
                {
                    if (formModel != null)
                    {
                        formModel.Item = Sitecore.Context.Database.GetItem(dataSourceId.ToID());
                    }
                }
            }
            return(model);
        }
        protected string GetValue(string value, string dateFormat)
        {
            if (ID.TryParse(value, out var id))
            {
                var item = Context.Database.GetItem(id);
                return(item?.DisplayName ?? value);
            }

            if (ShortID.TryParse(value, out var shortId))
            {
                var item = Context.Database.GetItem(shortId.ToID());
                return(item?.DisplayName ?? value);
            }

            if (DateTime.TryParse(value, out var date))
            {
                return(date.ToLocalTime().ToString(dateFormat));
            }
            return(value);
        }
        protected override Expression <Func <IndexedContact, bool> > GetResultPredicate(T ruleContext)
        {
            ID      valueAsId;
            var     parsedValueToId = ID.TryParse(Value, out valueAsId);
            ShortID valueAsShortId;
            var     parsedValueToShortId = ShortID.TryParse(Value, out valueAsShortId);

            if (!parsedValueToId && !parsedValueToShortId)
            {
                return(c => false);
            }

            if (parsedValueToShortId && valueAsId == (ID)null)
            {
                valueAsId = valueAsShortId.ToID();
            }

            var idConvertedToString = valueAsId.Guid.ToString("N").ToLowerInvariant();

            return(GetCompareExpression(c => c[IndexField], idConvertedToString));
        }
        public bool ProcessResult(Item item, string result, out string errorMessage)
        {
            errorMessage = string.Empty;

            ShortID sid;

            if (!ShortID.TryParse(result, out sid))
            {
                errorMessage = "Could not get the ID of the selected base layout.";
                return(false);
            }

            var  itemId         = sid.ToID();
            Item baseLayoutItem = null;

            if (itemId != ID.Null)
            {
                baseLayoutItem = item.Database.GetItem(itemId);
                if (baseLayoutItem == null)
                {
                    errorMessage = "The selected base layout item was not found.";
                    return(false);
                }
            }

            var args = new SaveBaseLayoutArgs(item)
            {
                NewBaseLayoutItem = baseLayoutItem
            };

            _pipelineRunner.Run(args);
            if (!string.IsNullOrEmpty(args.Message))
            {
                errorMessage = args.Message;
            }

            return(args.Successful);
        }
        public static List <string> Search(string renderingShortID)
        {
            ShortID renderingID;

            if (!ShortID.TryParse(renderingShortID, out renderingID))
            {
                return(null);
            }

            using (IProviderSearchContext RecommendedRenderingSearchContext = Index.CreateSearchContext())
            {
                var RecommendedRenderingQueryable =
                    RecommendedRenderingSearchContext.GetQueryable <RecommendedRenderingsSearchResultItem>();

                var queryBuilder = PredicateBuilder.True <RecommendedRenderingsSearchResultItem>();

                queryBuilder = queryBuilder.And(itm => itm.RecommendedRenderings.Contains(renderingShortID));

                RecommendedRenderingQueryable = RecommendedRenderingQueryable.Where(queryBuilder);

                var searchresults = RecommendedRenderingQueryable.GetResults();

                var result = new List <string>();

                if (searchresults.TotalSearchResults > 0)
                {
                    foreach (
                        var document in
                        searchresults.Hits.Select(q => q.Document)
                        .Where(q => q.RecommendedRenderings.Any(w => w.Contains(renderingShortID))))
                    {
                        foreach (var rendering in document.RecommendedRenderings.Where(w => w.Contains(renderingShortID))
                                 )
                        {
                            result.Add(rendering.Split('|')[0]);
                        }
                    }
                }

                using (var recommendedPlaceholdersSearchContext = PlaceholdersIndex.CreateSearchContext())
                {
                    var placeholderSettingsQueryable =
                        recommendedPlaceholdersSearchContext.GetQueryable <RecommendedRenderingsSearchResultItem>();

                    var placeholderSettingsQueryBuilder = PredicateBuilder.True <RecommendedRenderingsSearchResultItem>();

                    placeholderSettingsQueryBuilder = placeholderSettingsQueryBuilder.And(itm => itm.AllowedControls.Contains(renderingID.ToID()));

                    placeholderSettingsQueryable = placeholderSettingsQueryable.Where(placeholderSettingsQueryBuilder);

                    var placeholdersResult = placeholderSettingsQueryable.GetResults();

                    if (placeholdersResult.TotalSearchResults > 0)
                    {
                        foreach (var rendering in placeholdersResult.Hits)
                        {
                            if (String.IsNullOrWhiteSpace(rendering.Document.PlaceholderKey))
                            {
                                continue;
                            }

                            result.Add(rendering.Document.PlaceholderKey);
                        }
                    }
                }

                if (result.Any())
                {
                    return(result.Distinct().ToList());
                }

                return(null);
            }
        }